Skip to content

Commit

Permalink
provider/schema: Initial package (#553)
Browse files Browse the repository at this point in the history
Reference: #132
Reference: #326
Reference: #437
Reference: #491
Reference: #508
Reference: #532

This change introduces a new `provider/schema` package, which contains schema interfaces and types relevant to providers, such as omitting `Computed`, plan modifiers, and schema versioning. This new schema implementation also provides strongly typed attributes, nested attributes, and blocks with customizable types. Nested attributes and blocks are exposed with a separate nested object for customization and validation.

The implementation leans heavily on the design choice of the framework being responsible for preventing provider developer runtime errors. The tailored fields no longer expose functionality that is not available for providers. The framework design will also raise compiler-time errors for errant typing of validators.

No changes are required for data handling in the `Read` method.

Example definition:

```go
package test

import (
	"context"

	"github.com/bflad/terraform-plugin-framework-type-time/timetypes"
	"github.com/hashicorp/terraform-plugin-framework-validators/float64validator"
	"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
	"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
	"github.com/hashicorp/terraform-plugin-framework/provider"
	"github.com/hashicorp/terraform-plugin-framework/provider/schema"
	"github.com/hashicorp/terraform-plugin-framework/schema/validator"
	"github.com/hashicorp/terraform-plugin-framework/types"
)

type ExampleCloudProvider struct{}

func (p ExampleCloudProvider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) {
	resp.Schema = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"string_attribute": schema.StringAttribute{
				Required: true,
				Validators: []validator.String{
					stringvalidator.LengthBetween(3, 256),
				},
			},
			"custom_string_attribute": schema.StringAttribute{
				CustomType: timetypes.RFC3339Type,
				Optional:   true,
			},
			"list_attribute": schema.ListAttribute{
				ElementType: types.StringType,
				Optional:    true,
			},
			"list_nested_attribute": schema.ListNestedAttribute{
				NestedObject: schema.NestedAttributeObject{
					Attributes: map[string]schema.Attribute{
						"bool_attribute": schema.BoolAttribute{
							Optional: true,
						},
					},
					Validators: []validator.Object{ /*...*/ },
				},
				Optional: true,
				Validators: []validator.List{
					listvalidator.SizeAtMost(2),
				},
			},
			"single_nested_attribute": schema.SingleNestedAttribute{
				Attributes: map[string]schema.Attribute{
					"int64_attribute": schema.Int64Attribute{
						Optional: true,
					},
				},
				Optional: true,
			},
		},
		Blocks: map[string]schema.Block{
			"list_block": schema.ListNestedBlock{
				NestedObject: schema.NestedBlockObject{
					Attributes: map[string]schema.Attribute{
						"float64_attribute": schema.Float64Attribute{
							Optional: true,
							Validators: []validator.Float64{
								float64validator.OneOf(1.2, 2.4),
							},
						},
					},
					Validators: []validator.Object{ /*...*/ },
				},
				Validators: []validator.List{
					listvalidator.SizeAtMost(2),
				},
			},
		},
	}
}
```

To migrate a provider schema:

- Add `github.com/hashicorp/terraform-plugin-framework/provider/schema` to the `import` statement
- Switch the `provider.Provider` implementation `GetSchema` method to `Schema` whose response includes a `schema.Schema` from the new package.

Prior implementation:

```go
func (p ExampleCloudProvider) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
  return tfsdk.Schema{/* ... */}, nil
}
```

Migrated implementation:

```go
func (p ExampleCloudProvider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) {
  resp.Schema = schema.Schema{/*...*/}
}
```

If the provider requires no schema, the method can be entirely empty.

- Switch `map[string]tfsdk.Attribute` with `map[string]schema.Attribute`
- Switch `map[string]tfsdk.Block` with `map[string]schema.Block`
- Switch individual attribute and block definitions. Unless the code was already taking advantage of custom attribute types (uncommon so far), the `Type` field will be removed and the map entries must declare the typed implementation, e.g. a `tfsdk.Attribute` with `Type: types.StringType` is equivalent to `schema.StringAttribute`. Custom attribute types can be specified via the `CustomType` field in each of the implementations.

Prior primitive type (`types.BoolType`, `types.Float64Type`, `types.Int64Type`, `types.NumberType`, `types.StringType`) attribute implementation:

```go
// The "tfsdk.Attribute" could be omitted inside a map[string]tfsdk.Attribute
tfsdk.Attribute{
  Required: true,
  Type: types.StringType,
}
```

Migrated implementation:

```go
// The schema.XXXAttribute must be declared inside map[string]schema.Attribute
schema.StringAttribute{
  Required: true,
}
```

Prior collection type (`types.ListType`, `types.MapType`, `types.SetType`) attribute implementation:

```go
// The "tfsdk.Attribute" could be omitted inside a map[string]tfsdk.Attribute
tfsdk.Attribute{
  Required: true,
  Type: types.ListType{
    ElemType: types.StringType,
  },
}
```

Migrated implementation:

```go
// The schema.XXXAttribute must be declared inside map[string]schema.Attribute
schema.ListAttribute{
  ElementType: types.StringType,
  Required: true,
}
```

Prior single nested attributes type (`tfsdk.SingleNestedAttributes()`) attribute implementation:

```go
// The "tfsdk.Attribute" could be omitted inside a map[string]tfsdk.Attribute
tfsdk.Attribute{
  Attributes: tfsdk.SingleNestedAttributes(map[string]tfsdk.Attribute{/*...*/}),
  Required: true,
},
```

Migrated implementation:

```go
// The schema.XXXAttribute must be declared inside map[string]schema.Attribute
schema.SingleNestedAttribute{
  Attributes: map[string]schema.Attribute{/*...*/},
  Required: true,
}
```

Prior collection nested attributes type (`tfsdk.ListNestedAttributes()`, `tfsdk.MapNestedAttributes()`, `tfsdk.SetNestedAttributes()`) attribute implementation:

```go
// The "tfsdk.Attribute" could be omitted inside a map[string]tfsdk.Attribute
tfsdk.Attribute{
  Attributes: tfsdk.ListNestedAttributes(map[string]tfsdk.Attribute{/*...*/}),
  Required: true,
},
```

Migrated implementation:

```go
// The schema.XXXAttribute must be declared inside map[string]schema.Attribute
schema.ListNestedAttribute{
  NestedObject: schema.NestedAttributeObject{
    Attributes: map[string]schema.Attribute{/*...*/},
  },
  Required: true,
}
```

Prior collection blocks type (`tfsdk.Block`) attribute implementation:

```go
// The "tfsdk.Block" could be omitted inside a map[string]tfsdk.Block
tfsdk.Block{
  Attributes: map[string]tfsdk.Attribute{/*...*/},
  Blocks: map[string]tfsdk.Block{/*...*/},
  NestingMode: tfsdk.BlockNestingModeList,
},
```

Migrated implementation:

```go
// The schema.XXXBlock must be declared inside map[string]schema.Block
schema.ListNestedBlock{
  NestedObject: schema.NestedBlockObject{
    Attributes: map[string]schema.Attribute{/*...*/},
    Blocks: map[string]schema.Block{/*...*/},
  },
}
```
  • Loading branch information
bflad committed Nov 28, 2022
1 parent 486e611 commit 6d0a863
Show file tree
Hide file tree
Showing 56 changed files with 12,969 additions and 228 deletions.
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

0 comments on commit 6d0a863

Please sign in to comment.