Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider/metaschema: Initial package #562

Merged
merged 3 commits into from Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/562.txt
@@ -0,0 +1,3 @@
```release-note:breaking-change
provider: The `ProviderWithMetaSchema` type `GetMetaSchema` method has been replaced with the `MetaSchema` method
```
15 changes: 9 additions & 6 deletions internal/fwserver/server.go
Expand Up @@ -315,7 +315,7 @@ func (s *Server) ProviderSchema(ctx context.Context) (fwschema.Schema, diag.Diag
// it implements the ProviderWithMetaSchema interface. The Schema and
// Diagnostics are cached on first use.
func (s *Server) ProviderMetaSchema(ctx context.Context) (fwschema.Schema, diag.Diagnostics) {
providerWithProviderMeta, ok := s.Provider.(provider.ProviderWithMetaSchema)
providerWithMetaSchema, ok := s.Provider.(provider.ProviderWithMetaSchema)

if !ok {
return nil, nil
Expand All @@ -330,12 +330,15 @@ func (s *Server) ProviderMetaSchema(ctx context.Context) (fwschema.Schema, diag.
return s.providerMetaSchema, s.providerMetaSchemaDiags
}

logging.FrameworkDebug(ctx, "Calling provider defined Provider GetMetaSchema")
providerMetaSchema, diags := providerWithProviderMeta.GetMetaSchema(ctx)
logging.FrameworkDebug(ctx, "Called provider defined Provider GetMetaSchema")
req := provider.MetaSchemaRequest{}
resp := &provider.MetaSchemaResponse{}

s.providerMetaSchema = &providerMetaSchema
s.providerMetaSchemaDiags = diags
logging.FrameworkDebug(ctx, "Calling provider defined Provider MetaSchema")
providerWithMetaSchema.MetaSchema(ctx, req, resp)
logging.FrameworkDebug(ctx, "Called provider defined Provider MetaSchema")

s.providerMetaSchema = resp.Schema
s.providerMetaSchemaDiags = resp.Diagnostics

return s.providerMetaSchema, s.providerMetaSchemaDiags
}
Expand Down
21 changes: 9 additions & 12 deletions internal/fwserver/server_getproviderschema_test.go
Expand Up @@ -12,11 +12,10 @@ import (
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
"github.com/hashicorp/terraform-plugin-framework/internal/testing/testprovider"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/provider/metaschema"
providerschema "github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/resource"
resourceschema "github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
)

func TestServerGetProviderSchema(t *testing.T) {
Expand Down Expand Up @@ -280,27 +279,25 @@ func TestServerGetProviderSchema(t *testing.T) {
server: &fwserver.Server{
Provider: &testprovider.ProviderWithMetaSchema{
Provider: &testprovider.Provider{},
GetMetaSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"test": {
MetaSchemaMethod: func(_ context.Context, _ provider.MetaSchemaRequest, resp *provider.MetaSchemaResponse) {
resp.Schema = metaschema.Schema{
Attributes: map[string]metaschema.Attribute{
"test": metaschema.StringAttribute{
Required: true,
Type: types.StringType,
},
},
}, nil
}
},
},
},
request: &fwserver.GetProviderSchemaRequest{},
expectedResponse: &fwserver.GetProviderSchemaResponse{
DataSourceSchemas: map[string]fwschema.Schema{},
Provider: providerschema.Schema{},
ProviderMeta: &tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"test": {
ProviderMeta: metaschema.Schema{
Attributes: map[string]metaschema.Attribute{
"test": metaschema.StringAttribute{
Required: true,
Type: types.StringType,
},
},
},
Expand Down
27 changes: 13 additions & 14 deletions internal/proto5server/server_applyresourcechange_test.go
Expand Up @@ -9,13 +9,13 @@ import (
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tftypes"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
"github.com/hashicorp/terraform-plugin-framework/internal/privatestate"
"github.com/hashicorp/terraform-plugin-framework/internal/testing/testprovider"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/provider/metaschema"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
)

Expand Down Expand Up @@ -57,11 +57,10 @@ func TestServerApplyResourceChange(t *testing.T) {
"test_provider_meta_attribute": tftypes.NewValue(tftypes.String, "test-provider-meta-value"),
})

testProviderMetaSchema := tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"test_provider_meta_attribute": {
testProviderMetaSchema := metaschema.Schema{
Attributes: map[string]metaschema.Attribute{
"test_provider_meta_attribute": metaschema.StringAttribute{
Optional: true,
Type: types.StringType,
},
},
}
Expand Down Expand Up @@ -233,8 +232,8 @@ func TestServerApplyResourceChange(t *testing.T) {
}
},
},
GetMetaSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return testProviderMetaSchema, nil
MetaSchemaMethod: func(_ context.Context, _ provider.MetaSchemaRequest, resp *provider.MetaSchemaResponse) {
resp.Schema = testProviderMetaSchema
},
},
},
Expand Down Expand Up @@ -559,8 +558,8 @@ func TestServerApplyResourceChange(t *testing.T) {
}
},
},
GetMetaSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return testProviderMetaSchema, nil
MetaSchemaMethod: func(_ context.Context, _ provider.MetaSchemaRequest, resp *provider.MetaSchemaResponse) {
resp.Schema = testProviderMetaSchema
},
},
},
Expand Down Expand Up @@ -946,8 +945,8 @@ func TestServerApplyResourceChange(t *testing.T) {
}
},
},
GetMetaSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return testProviderMetaSchema, nil
MetaSchemaMethod: func(_ context.Context, _ provider.MetaSchemaRequest, resp *provider.MetaSchemaResponse) {
resp.Schema = testProviderMetaSchema
},
},
},
Expand Down Expand Up @@ -1015,8 +1014,8 @@ func TestServerApplyResourceChange(t *testing.T) {
}
},
},
GetMetaSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return testProviderMetaSchema, nil
MetaSchemaMethod: func(_ context.Context, _ provider.MetaSchemaRequest, resp *provider.MetaSchemaResponse) {
resp.Schema = testProviderMetaSchema
},
},
},
Expand Down
15 changes: 6 additions & 9 deletions internal/proto5server/server_getproviderschema_test.go
Expand Up @@ -8,16 +8,14 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/datasource"
datasourceschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
"github.com/hashicorp/terraform-plugin-framework/internal/logging"
"github.com/hashicorp/terraform-plugin-framework/internal/testing/testprovider"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/provider/metaschema"
providerschema "github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/resource"
resourceschema "github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/hashicorp/terraform-plugin-log/tfsdklogtest"
Expand Down Expand Up @@ -253,15 +251,14 @@ func TestServerGetProviderSchema(t *testing.T) {
FrameworkServer: fwserver.Server{
Provider: &testprovider.ProviderWithMetaSchema{
Provider: &testprovider.Provider{},
GetMetaSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"test": {
MetaSchemaMethod: func(_ context.Context, _ provider.MetaSchemaRequest, resp *provider.MetaSchemaResponse) {
resp.Schema = metaschema.Schema{
Attributes: map[string]metaschema.Attribute{
"test": metaschema.StringAttribute{
Required: true,
Type: types.StringType,
},
},
}, nil
}
},
},
},
Expand Down
23 changes: 11 additions & 12 deletions internal/proto5server/server_planresourcechange_test.go
Expand Up @@ -5,13 +5,13 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
"github.com/hashicorp/terraform-plugin-framework/internal/testing/testprovider"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/provider/metaschema"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tftypes"
Expand Down Expand Up @@ -55,11 +55,10 @@ func TestServerPlanResourceChange(t *testing.T) {
"test_provider_meta_attribute": tftypes.NewValue(tftypes.String, "test-provider-meta-value"),
})

testProviderMetaSchema := tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"test_provider_meta_attribute": {
testProviderMetaSchema := metaschema.Schema{
Attributes: map[string]metaschema.Attribute{
"test_provider_meta_attribute": metaschema.StringAttribute{
Optional: true,
Type: types.StringType,
},
},
}
Expand Down Expand Up @@ -207,8 +206,8 @@ func TestServerPlanResourceChange(t *testing.T) {
}
},
},
GetMetaSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return testProviderMetaSchema, nil
MetaSchemaMethod: func(_ context.Context, _ provider.MetaSchemaRequest, resp *provider.MetaSchemaResponse) {
resp.Schema = testProviderMetaSchema
},
},
},
Expand Down Expand Up @@ -473,8 +472,8 @@ func TestServerPlanResourceChange(t *testing.T) {
}
},
},
GetMetaSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return testProviderMetaSchema, nil
MetaSchemaMethod: func(_ context.Context, _ provider.MetaSchemaRequest, resp *provider.MetaSchemaResponse) {
resp.Schema = testProviderMetaSchema
},
},
},
Expand Down Expand Up @@ -836,8 +835,8 @@ func TestServerPlanResourceChange(t *testing.T) {
}
},
},
GetMetaSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return testProviderMetaSchema, nil
MetaSchemaMethod: func(_ context.Context, _ provider.MetaSchemaRequest, resp *provider.MetaSchemaResponse) {
resp.Schema = testProviderMetaSchema
},
},
},
Expand Down
37 changes: 24 additions & 13 deletions internal/proto5server/server_readdatasource_test.go
Expand Up @@ -7,10 +7,10 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
"github.com/hashicorp/terraform-plugin-framework/internal/testing/testprovider"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/provider/metaschema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tftypes"
Expand All @@ -33,6 +33,19 @@ func TestServerReadDataSource(t *testing.T) {

testEmptyDynamicValue := testNewDynamicValue(t, tftypes.Object{}, nil)

testProviderMetaDynamicValue := testNewDynamicValue(t,
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"test_optional": tftypes.String,
"test_required": tftypes.String,
},
},
map[string]tftypes.Value{
"test_optional": tftypes.NewValue(tftypes.String, nil),
"test_required": tftypes.NewValue(tftypes.String, "test-config-value"),
},
)

testStateDynamicValue := testNewDynamicValue(t, testType, map[string]tftypes.Value{
"test_computed": tftypes.NewValue(tftypes.String, "test-state-value"),
"test_required": tftypes.NewValue(tftypes.String, "test-config-value"),
Expand Down Expand Up @@ -142,7 +155,7 @@ func TestServerReadDataSource(t *testing.T) {
},
ReadMethod: func(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var config struct {
TestComputed types.String `tfsdk:"test_computed"`
TestOptional types.String `tfsdk:"test_optional"`
TestRequired types.String `tfsdk:"test_required"`
}

Expand All @@ -157,26 +170,24 @@ func TestServerReadDataSource(t *testing.T) {
}
},
},
GetMetaSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"test_computed": {
Computed: true,
Type: types.StringType,
MetaSchemaMethod: func(_ context.Context, _ provider.MetaSchemaRequest, resp *provider.MetaSchemaResponse) {
resp.Schema = metaschema.Schema{
Attributes: map[string]metaschema.Attribute{
"test_optional": metaschema.StringAttribute{
Optional: true,
},
"test_required": {
"test_required": metaschema.StringAttribute{
Required: true,
Type: types.StringType,
},
},
}, nil
}
},
},
},
},
request: &tfprotov5.ReadDataSourceRequest{
Config: testEmptyDynamicValue,
ProviderMeta: testConfigDynamicValue,
ProviderMeta: testProviderMetaDynamicValue,
TypeName: "test_data_source",
},
expectedResponse: &tfprotov5.ReadDataSourceResponse{
Expand Down