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/schema: Initial package #553

Merged
merged 3 commits into from Nov 28, 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
7 changes: 7 additions & 0 deletions .changelog/553.txt
@@ -0,0 +1,7 @@
```release-note:note
provider: The `Provider` type `GetSchema` method has been deprecated. Use the `Schema` method instead.
```

```release-note:feature
provider/schema: New package which contains schema interfaces and types relevant to providers
```
31 changes: 25 additions & 6 deletions internal/fwserver/server.go
Expand Up @@ -282,12 +282,31 @@ func (s *Server) ProviderSchema(ctx context.Context) (fwschema.Schema, diag.Diag
return s.providerSchema, s.providerSchemaDiags
}

logging.FrameworkDebug(ctx, "Calling provider defined Provider GetSchema")
providerSchema, diags := s.Provider.GetSchema(ctx)
logging.FrameworkDebug(ctx, "Called provider defined Provider GetSchema")

s.providerSchema = &providerSchema
s.providerSchemaDiags = diags
switch providerIface := s.Provider.(type) {
case provider.ProviderWithSchema:
schemaReq := provider.SchemaRequest{}
schemaResp := provider.SchemaResponse{}

logging.FrameworkDebug(ctx, "Calling provider defined Provider Schema")
providerIface.Schema(ctx, schemaReq, &schemaResp)
logging.FrameworkDebug(ctx, "Called provider defined Provider Schema")

s.providerSchema = schemaResp.Schema
s.providerSchemaDiags = schemaResp.Diagnostics
case provider.ProviderWithGetSchema:
logging.FrameworkDebug(ctx, "Calling provider defined Provider GetSchema")
schema, diags := providerIface.GetSchema(ctx) //nolint:staticcheck // Required internal usage until removal
logging.FrameworkDebug(ctx, "Called provider defined Provider GetSchema")

s.providerSchema = schema
s.providerSchemaDiags = diags
default:
s.providerSchemaDiags.AddError(
"Provier Missing Schema",
"While attempting to load provider schemas, the provider itself was missing a Schema method. "+
"This is always an issue in the provider and should be reported to the provider developers.",
)
}

return s.providerSchema, s.providerSchemaDiags
}
Expand Down
28 changes: 10 additions & 18 deletions internal/fwserver/server_configureprovider_test.go
Expand Up @@ -10,6 +10,7 @@ import (
"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/schema"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tftypes"
Expand All @@ -28,11 +29,10 @@ func TestServerConfigureProvider(t *testing.T) {
"test": tftypes.NewValue(tftypes.String, "test-value"),
})

testSchema := tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"test": {
testSchema := schema.Schema{
Attributes: map[string]schema.Attribute{
"test": schema.StringAttribute{
Required: true,
Type: types.StringType,
},
},
}
Expand All @@ -56,8 +56,8 @@ func TestServerConfigureProvider(t *testing.T) {
"request-config": {
server: &fwserver.Server{
Provider: &testprovider.Provider{
GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return testSchema, nil
SchemaMethod: func(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = testSchema
},
ConfigureMethod: func(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
var got types.String
Expand All @@ -82,9 +82,7 @@ func TestServerConfigureProvider(t *testing.T) {
"request-terraformversion": {
server: &fwserver.Server{
Provider: &testprovider.Provider{
GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{}, nil
},
SchemaMethod: func(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {},
ConfigureMethod: func(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
if req.TerraformVersion != "1.0.0" {
resp.Diagnostics.AddError("Incorrect req.TerraformVersion", "expected 1.0.0, got "+req.TerraformVersion)
Expand All @@ -100,9 +98,7 @@ func TestServerConfigureProvider(t *testing.T) {
"response-datasourcedata": {
server: &fwserver.Server{
Provider: &testprovider.Provider{
GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{}, nil
},
SchemaMethod: func(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {},
ConfigureMethod: func(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
resp.DataSourceData = "test-provider-configure-value"
},
Expand All @@ -116,9 +112,7 @@ func TestServerConfigureProvider(t *testing.T) {
"response-diagnostics": {
server: &fwserver.Server{
Provider: &testprovider.Provider{
GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{}, nil
},
SchemaMethod: func(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {},
ConfigureMethod: func(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
resp.Diagnostics.AddWarning("warning summary", "warning detail")
resp.Diagnostics.AddError("error summary", "error detail")
Expand All @@ -142,9 +136,7 @@ func TestServerConfigureProvider(t *testing.T) {
"response-resourcedata": {
server: &fwserver.Server{
Provider: &testprovider.Provider{
GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{}, nil
},
SchemaMethod: func(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {},
ConfigureMethod: func(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
resp.ResourceData = "test-provider-configure-value"
},
Expand Down
89 changes: 44 additions & 45 deletions internal/fwserver/server_getproviderschema_test.go
Expand Up @@ -6,12 +6,13 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
datasourceschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
"github.com/hashicorp/terraform-plugin-framework/internal/testing/testprovider"
"github.com/hashicorp/terraform-plugin-framework/provider"
providerschema "github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand All @@ -31,7 +32,7 @@ func TestServerGetProviderSchema(t *testing.T) {
},
expectedResponse: &fwserver.GetProviderSchemaResponse{
DataSourceSchemas: map[string]fwschema.Schema{},
Provider: &tfsdk.Schema{},
Provider: providerschema.Schema{},
ResourceSchemas: map[string]fwschema.Schema{},
ServerCapabilities: &fwserver.ServerCapabilities{
PlanDestroy: true,
Expand All @@ -46,9 +47,9 @@ func TestServerGetProviderSchema(t *testing.T) {
func() datasource.DataSource {
return &testprovider.DataSource{
SchemaMethod: func(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"test1": schema.StringAttribute{
resp.Schema = datasourceschema.Schema{
Attributes: map[string]datasourceschema.Attribute{
"test1": datasourceschema.StringAttribute{
Required: true,
},
},
Expand All @@ -62,9 +63,9 @@ func TestServerGetProviderSchema(t *testing.T) {
func() datasource.DataSource {
return &testprovider.DataSource{
SchemaMethod: func(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"test2": schema.StringAttribute{
resp.Schema = datasourceschema.Schema{
Attributes: map[string]datasourceschema.Attribute{
"test2": datasourceschema.StringAttribute{
Required: true,
},
},
Expand All @@ -82,22 +83,22 @@ func TestServerGetProviderSchema(t *testing.T) {
request: &fwserver.GetProviderSchemaRequest{},
expectedResponse: &fwserver.GetProviderSchemaResponse{
DataSourceSchemas: map[string]fwschema.Schema{
"test_data_source1": schema.Schema{
Attributes: map[string]schema.Attribute{
"test1": schema.StringAttribute{
"test_data_source1": datasourceschema.Schema{
Attributes: map[string]datasourceschema.Attribute{
"test1": datasourceschema.StringAttribute{
Required: true,
},
},
},
"test_data_source2": schema.Schema{
Attributes: map[string]schema.Attribute{
"test2": schema.StringAttribute{
"test_data_source2": datasourceschema.Schema{
Attributes: map[string]datasourceschema.Attribute{
"test2": datasourceschema.StringAttribute{
Required: true,
},
},
},
},
Provider: &tfsdk.Schema{},
Provider: providerschema.Schema{},
ResourceSchemas: map[string]fwschema.Schema{},
ServerCapabilities: &fwserver.ServerCapabilities{
PlanDestroy: true,
Expand All @@ -112,9 +113,9 @@ func TestServerGetProviderSchema(t *testing.T) {
func() datasource.DataSource {
return &testprovider.DataSource{
SchemaMethod: func(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"test1": schema.StringAttribute{
resp.Schema = datasourceschema.Schema{
Attributes: map[string]datasourceschema.Attribute{
"test1": datasourceschema.StringAttribute{
Required: true,
},
},
Expand All @@ -128,9 +129,9 @@ func TestServerGetProviderSchema(t *testing.T) {
func() datasource.DataSource {
return &testprovider.DataSource{
SchemaMethod: func(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"test2": schema.StringAttribute{
resp.Schema = datasourceschema.Schema{
Attributes: map[string]datasourceschema.Attribute{
"test2": datasourceschema.StringAttribute{
Required: true,
},
},
Expand All @@ -156,7 +157,7 @@ func TestServerGetProviderSchema(t *testing.T) {
"This is always an issue with the provider and should be reported to the provider developers.",
),
},
Provider: &tfsdk.Schema{},
Provider: providerschema.Schema{},
ResourceSchemas: map[string]fwschema.Schema{},
ServerCapabilities: &fwserver.ServerCapabilities{
PlanDestroy: true,
Expand Down Expand Up @@ -189,7 +190,7 @@ func TestServerGetProviderSchema(t *testing.T) {
"This is always an issue with the provider and should be reported to the provider developers.",
),
},
Provider: &tfsdk.Schema{},
Provider: providerschema.Schema{},
ResourceSchemas: map[string]fwschema.Schema{},
ServerCapabilities: &fwserver.ServerCapabilities{
PlanDestroy: true,
Expand All @@ -208,9 +209,9 @@ func TestServerGetProviderSchema(t *testing.T) {
func() datasource.DataSource {
return &testprovider.DataSource{
SchemaMethod: func(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"test": schema.StringAttribute{
resp.Schema = datasourceschema.Schema{
Attributes: map[string]datasourceschema.Attribute{
"test": datasourceschema.StringAttribute{
Required: true,
},
},
Expand All @@ -229,15 +230,15 @@ func TestServerGetProviderSchema(t *testing.T) {
request: &fwserver.GetProviderSchemaRequest{},
expectedResponse: &fwserver.GetProviderSchemaResponse{
DataSourceSchemas: map[string]fwschema.Schema{
"testprovidertype_data_source": schema.Schema{
Attributes: map[string]schema.Attribute{
"test": schema.StringAttribute{
"testprovidertype_data_source": datasourceschema.Schema{
Attributes: map[string]datasourceschema.Attribute{
"test": datasourceschema.StringAttribute{
Required: true,
},
},
},
},
Provider: &tfsdk.Schema{},
Provider: providerschema.Schema{},
ResourceSchemas: map[string]fwschema.Schema{},
ServerCapabilities: &fwserver.ServerCapabilities{
PlanDestroy: true,
Expand All @@ -247,26 +248,24 @@ func TestServerGetProviderSchema(t *testing.T) {
"provider": {
server: &fwserver.Server{
Provider: &testprovider.Provider{
GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"test": {
SchemaMethod: func(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = providerschema.Schema{
Attributes: map[string]providerschema.Attribute{
"test": providerschema.StringAttribute{
Required: true,
Type: types.StringType,
},
},
}, nil
}
},
},
},
request: &fwserver.GetProviderSchemaRequest{},
expectedResponse: &fwserver.GetProviderSchemaResponse{
DataSourceSchemas: map[string]fwschema.Schema{},
Provider: &tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"test": {
Provider: providerschema.Schema{
Attributes: map[string]providerschema.Attribute{
"test": providerschema.StringAttribute{
Required: true,
Type: types.StringType,
},
},
},
Expand Down Expand Up @@ -295,7 +294,7 @@ func TestServerGetProviderSchema(t *testing.T) {
request: &fwserver.GetProviderSchemaRequest{},
expectedResponse: &fwserver.GetProviderSchemaResponse{
DataSourceSchemas: map[string]fwschema.Schema{},
Provider: &tfsdk.Schema{},
Provider: providerschema.Schema{},
ProviderMeta: &tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"test": {
Expand Down Expand Up @@ -356,7 +355,7 @@ func TestServerGetProviderSchema(t *testing.T) {
request: &fwserver.GetProviderSchemaRequest{},
expectedResponse: &fwserver.GetProviderSchemaResponse{
DataSourceSchemas: map[string]fwschema.Schema{},
Provider: &tfsdk.Schema{},
Provider: providerschema.Schema{},
ResourceSchemas: map[string]fwschema.Schema{
"test_resource1": tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
Expand Down Expand Up @@ -434,7 +433,7 @@ func TestServerGetProviderSchema(t *testing.T) {
"This is always an issue with the provider and should be reported to the provider developers.",
),
},
Provider: &tfsdk.Schema{},
Provider: providerschema.Schema{},
ResourceSchemas: nil,
ServerCapabilities: &fwserver.ServerCapabilities{
PlanDestroy: true,
Expand Down Expand Up @@ -467,7 +466,7 @@ func TestServerGetProviderSchema(t *testing.T) {
"This is always an issue with the provider and should be reported to the provider developers.",
),
},
Provider: &tfsdk.Schema{},
Provider: providerschema.Schema{},
ResourceSchemas: nil,
ServerCapabilities: &fwserver.ServerCapabilities{
PlanDestroy: true,
Expand Down Expand Up @@ -508,7 +507,7 @@ func TestServerGetProviderSchema(t *testing.T) {
request: &fwserver.GetProviderSchemaRequest{},
expectedResponse: &fwserver.GetProviderSchemaResponse{
DataSourceSchemas: map[string]fwschema.Schema{},
Provider: &tfsdk.Schema{},
Provider: providerschema.Schema{},
ResourceSchemas: map[string]fwschema.Schema{
"testprovidertype_resource": tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
Expand Down