Skip to content

Commit

Permalink
Migrate schema package into tfsdk package
Browse files Browse the repository at this point in the history
Reference: #76

This is now stuck at the below:

```
package github.com/hashicorp/terraform-plugin-framework/internal/proto6
        imports github.com/hashicorp/terraform-plugin-framework/tfsdk
        imports github.com/hashicorp/terraform-plugin-framework/internal/proto6: import cycle not allowed
```
  • Loading branch information
bflad committed Jul 27, 2021
1 parent b61fdeb commit bcb5ce1
Show file tree
Hide file tree
Showing 19 changed files with 68 additions and 89 deletions.
19 changes: 9 additions & 10 deletions internal/proto6/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import (
"errors"
"sort"

"github.com/hashicorp/terraform-plugin-framework/schema"

"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

// Schema returns the *tfprotov6.Schema equivalent of a schema.Schema. At least
// Schema returns the *tfprotov6.Schema equivalent of a tfsdk.Schema. At least
// one attribute must be set in the schema, or an error will be returned.
func Schema(ctx context.Context, s schema.Schema) (*tfprotov6.Schema, error) {
func Schema(ctx context.Context, s tfsdk.Schema) (*tfprotov6.Schema, error) {
result := &tfprotov6.Schema{
Version: s.Version,
}
Expand Down Expand Up @@ -55,9 +54,9 @@ func Schema(ctx context.Context, s schema.Schema) (*tfprotov6.Schema, error) {
}

// Attribute returns the *tfprotov6.SchemaAttribute equivalent of a
// schema.Attribute. Errors will be tftypes.AttributePathErrors based on
// tfsdk.Attribute. Errors will be tftypes.AttributePathErrors based on
// `path`. `name` is the name of the attribute.
func Attribute(ctx context.Context, name string, attr schema.Attribute, path *tftypes.AttributePath) (*tfprotov6.SchemaAttribute, error) {
func Attribute(ctx context.Context, name string, attr tfsdk.Attribute, path *tftypes.AttributePath) (*tfprotov6.SchemaAttribute, error) {
a := &tfprotov6.SchemaAttribute{
Name: name,
Required: attr.Required,
Expand Down Expand Up @@ -85,13 +84,13 @@ func Attribute(ctx context.Context, name string, attr schema.Attribute, path *tf
}
nm := attr.Attributes.GetNestingMode()
switch nm {
case schema.NestingModeSingle:
case tfsdk.NestingModeSingle:
object.Nesting = tfprotov6.SchemaObjectNestingModeSingle
case schema.NestingModeList:
case tfsdk.NestingModeList:
object.Nesting = tfprotov6.SchemaObjectNestingModeList
case schema.NestingModeSet:
case tfsdk.NestingModeSet:
object.Nesting = tfprotov6.SchemaObjectNestingModeSet
case schema.NestingModeMap:
case tfsdk.NestingModeMap:
object.Nesting = tfprotov6.SchemaObjectNestingModeMap
default:
return nil, path.NewErrorf("unrecognized nesting mode %v", nm)
Expand Down
2 changes: 1 addition & 1 deletion schema/attribute.go → tfsdk/attribute.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package schema
package tfsdk

import (
"errors"
Expand Down
3 changes: 1 addition & 2 deletions tfsdk/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import (

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/internal/reflect"
"github.com/hashicorp/terraform-plugin-framework/schema"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

// Config represents a Terraform config.
type Config struct {
Raw tftypes.Value
Schema schema.Schema
Schema Schema
}

// Get populates the struct passed as `target` with the entire config.
Expand Down
3 changes: 1 addition & 2 deletions tfsdk/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package tfsdk
import (
"context"

"github.com/hashicorp/terraform-plugin-framework/schema"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
)

Expand All @@ -12,7 +11,7 @@ import (
// return an instance of it in the map returned by Provider.GetDataSources.
type DataSourceType interface {
// GetSchema returns the schema for this data source.
GetSchema(context.Context) (schema.Schema, []*tfprotov6.Diagnostic)
GetSchema(context.Context) (Schema, []*tfprotov6.Diagnostic)

// NewDataSource instantiates a new DataSource of this DataSourceType.
NewDataSource(context.Context, Provider) (DataSource, []*tfprotov6.Diagnostic)
Expand Down
2 changes: 1 addition & 1 deletion schema/nested_attributes.go → tfsdk/nested_attributes.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package schema
package tfsdk

import (
"fmt"
Expand Down
3 changes: 1 addition & 2 deletions tfsdk/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import (

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/internal/reflect"
"github.com/hashicorp/terraform-plugin-framework/schema"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

// Plan represents a Terraform plan.
type Plan struct {
Raw tftypes.Value
Schema schema.Schema
Schema Schema
}

// Get populates the struct passed as `target` with the entire plan.
Expand Down
5 changes: 2 additions & 3 deletions tfsdk/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package tfsdk
import (
"context"

"github.com/hashicorp/terraform-plugin-framework/schema"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
)

// Provider is the core interface that all Terraform providers must implement.
type Provider interface {
// GetSchema returns the schema for this provider's configuration. If
// this provider has no configuration, return an empty schema.Schema.
GetSchema(context.Context) (schema.Schema, []*tfprotov6.Diagnostic)
GetSchema(context.Context) (Schema, []*tfprotov6.Diagnostic)

// Configure is called at the beginning of the provider lifecycle, when
// Terraform sends to the provider the values the user specified in the
Expand All @@ -38,5 +37,5 @@ type Provider interface {
type ProviderWithProviderMeta interface {
Provider
// GetMetaSchema returns the provider meta schema.
GetMetaSchema(context.Context) (schema.Schema, []*tfprotov6.Diagnostic)
GetMetaSchema(context.Context) (Schema, []*tfprotov6.Diagnostic)
}
3 changes: 1 addition & 2 deletions tfsdk/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package tfsdk
import (
"context"

"github.com/hashicorp/terraform-plugin-framework/schema"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
)

Expand All @@ -12,7 +11,7 @@ import (
// instance of it in the map returned by Provider.GeResources.
type ResourceType interface {
// GetSchema returns the schema for this resource.
GetSchema(context.Context) (schema.Schema, []*tfprotov6.Diagnostic)
GetSchema(context.Context) (Schema, []*tfprotov6.Diagnostic)

// NewResource instantiates a new Resource of this ResourceType.
NewResource(context.Context, Provider) (Resource, []*tfprotov6.Diagnostic)
Expand Down
2 changes: 1 addition & 1 deletion schema/schema.go → tfsdk/schema.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package schema
package tfsdk

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion schema/schema_test.go → tfsdk/schema_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package schema
package tfsdk

import (
"testing"
Expand Down
6 changes: 2 additions & 4 deletions tfsdk/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"sync"

"github.com/hashicorp/terraform-plugin-framework/internal/proto6"
"github.com/hashicorp/terraform-plugin-framework/schema"

"github.com/hashicorp/terraform-plugin-go/tfprotov6"
tf6server "github.com/hashicorp/terraform-plugin-go/tfprotov6/server"
"github.com/hashicorp/terraform-plugin-go/tftypes"
Expand Down Expand Up @@ -379,14 +377,14 @@ func (s *server) ReadResource(ctx context.Context, req *tfprotov6.ReadResourceRe
return resp, nil
}

func markComputedNilsAsUnknown(ctx context.Context, resourceSchema schema.Schema) func(*tftypes.AttributePath, tftypes.Value) (tftypes.Value, error) {
func markComputedNilsAsUnknown(ctx context.Context, resourceSchema Schema) func(*tftypes.AttributePath, tftypes.Value) (tftypes.Value, error) {
return func(path *tftypes.AttributePath, val tftypes.Value) (tftypes.Value, error) {
if !val.IsNull() {
return val, nil
}
attribute, err := resourceSchema.AttributeAtPath(path)
if err != nil {
if errors.Is(err, schema.ErrPathInsideAtomicAttribute) {
if errors.Is(err, ErrPathInsideAtomicAttribute) {
// ignore attributes/elements inside schema.Attributes, they have no schema of their own
return val, nil
}
Expand Down
8 changes: 3 additions & 5 deletions tfsdk/serve_data_source_one_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/schema"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

type testServeDataSourceTypeOne struct{}

func (dt testServeDataSourceTypeOne) GetSchema(_ context.Context) (schema.Schema, []*tfprotov6.Diagnostic) {
return schema.Schema{
Attributes: map[string]schema.Attribute{
func (dt testServeDataSourceTypeOne) GetSchema(_ context.Context) (Schema, []*tfprotov6.Diagnostic) {
return Schema{
Attributes: map[string]Attribute{
"current_time": {
Type: types.StringType,
Computed: true,
Expand Down
8 changes: 3 additions & 5 deletions tfsdk/serve_data_source_two_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/schema"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

type testServeDataSourceTypeTwo struct{}

func (dt testServeDataSourceTypeTwo) GetSchema(_ context.Context) (schema.Schema, []*tfprotov6.Diagnostic) {
return schema.Schema{
Attributes: map[string]schema.Attribute{
func (dt testServeDataSourceTypeTwo) GetSchema(_ context.Context) (Schema, []*tfprotov6.Diagnostic) {
return Schema{
Attributes: map[string]Attribute{
"family": {
Type: types.StringType,
Optional: true,
Expand Down
42 changes: 20 additions & 22 deletions tfsdk/serve_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,54 @@ import (
"context"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/schema"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

type testServeProvider struct {
// configure
configuredVal tftypes.Value
configuredSchema schema.Schema
configuredSchema Schema
configuredTFVersion string

// read resource request
readResourceCurrentStateValue tftypes.Value
readResourceCurrentStateSchema schema.Schema
readResourceCurrentStateSchema Schema
readResourceProviderMetaValue tftypes.Value
readResourceProviderMetaSchema schema.Schema
readResourceProviderMetaSchema Schema
readResourceImpl func(context.Context, ReadResourceRequest, *ReadResourceResponse)
readResourceCalledResourceType string

// apply resource change
applyResourceChangeCalledResourceType string
applyResourceChangeCalledAction string
applyResourceChangePriorStateValue tftypes.Value
applyResourceChangePriorStateSchema schema.Schema
applyResourceChangePriorStateSchema Schema
applyResourceChangePlannedStateValue tftypes.Value
applyResourceChangePlannedStateSchema schema.Schema
applyResourceChangePlannedStateSchema Schema
applyResourceChangeConfigValue tftypes.Value
applyResourceChangeConfigSchema schema.Schema
applyResourceChangeConfigSchema Schema
applyResourceChangeProviderMetaValue tftypes.Value
applyResourceChangeProviderMetaSchema schema.Schema
applyResourceChangeProviderMetaSchema Schema
createFunc func(context.Context, CreateResourceRequest, *CreateResourceResponse)
updateFunc func(context.Context, UpdateResourceRequest, *UpdateResourceResponse)
deleteFunc func(context.Context, DeleteResourceRequest, *DeleteResourceResponse)

// read data source request
readDataSourceConfigValue tftypes.Value
readDataSourceConfigSchema schema.Schema
readDataSourceConfigSchema Schema
readDataSourceProviderMetaValue tftypes.Value
readDataSourceProviderMetaSchema schema.Schema
readDataSourceProviderMetaSchema Schema
readDataSourceImpl func(context.Context, ReadDataSourceRequest, *ReadDataSourceResponse)
readDataSourceCalledDataSourceType string
}

func (t *testServeProvider) GetSchema(_ context.Context) (schema.Schema, []*tfprotov6.Diagnostic) {
return schema.Schema{
func (t *testServeProvider) GetSchema(_ context.Context) (Schema, []*tfprotov6.Diagnostic) {
return Schema{
Version: 1,
DeprecationMessage: "Deprecated in favor of other_resource",
Attributes: map[string]schema.Attribute{
Attributes: map[string]Attribute{
"required": {
Type: types.StringType,
Required: true,
Expand Down Expand Up @@ -143,7 +141,7 @@ func (t *testServeProvider) GetSchema(_ context.Context) (schema.Schema, []*tfpr
// TODO: add sets when we support them
// TODO: add tuples when we support them
"single-nested-attributes": {
Attributes: schema.SingleNestedAttributes(map[string]schema.Attribute{
Attributes: SingleNestedAttributes(map[string]Attribute{
"foo": {
Type: types.StringType,
Optional: true,
Expand All @@ -157,7 +155,7 @@ func (t *testServeProvider) GetSchema(_ context.Context) (schema.Schema, []*tfpr
Optional: true,
},
"list-nested-attributes": {
Attributes: schema.ListNestedAttributes(map[string]schema.Attribute{
Attributes: ListNestedAttributes(map[string]Attribute{
"foo": {
Type: types.StringType,
Optional: true,
Expand All @@ -167,11 +165,11 @@ func (t *testServeProvider) GetSchema(_ context.Context) (schema.Schema, []*tfpr
Type: types.NumberType,
Required: true,
},
}, schema.ListNestedAttributesOptions{}),
}, ListNestedAttributesOptions{}),
Optional: true,
},
"map-nested-attributes": {
Attributes: schema.MapNestedAttributes(map[string]schema.Attribute{
Attributes: MapNestedAttributes(map[string]Attribute{
"foo": {
Type: types.StringType,
Optional: true,
Expand All @@ -181,7 +179,7 @@ func (t *testServeProvider) GetSchema(_ context.Context) (schema.Schema, []*tfpr
Type: types.NumberType,
Required: true,
},
}, schema.MapNestedAttributesOptions{}),
}, MapNestedAttributesOptions{}),
Optional: true,
},
},
Expand Down Expand Up @@ -429,10 +427,10 @@ type testServeProviderWithMetaSchema struct {
*testServeProvider
}

func (t *testServeProviderWithMetaSchema) GetMetaSchema(context.Context) (schema.Schema, []*tfprotov6.Diagnostic) {
return schema.Schema{
func (t *testServeProviderWithMetaSchema) GetMetaSchema(context.Context) (Schema, []*tfprotov6.Diagnostic) {
return Schema{
Version: 2,
Attributes: map[string]schema.Attribute{
Attributes: map[string]Attribute{
"foo": {
Type: types.StringType,
Required: true,
Expand Down
8 changes: 3 additions & 5 deletions tfsdk/serve_resource_one_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@ import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/schema"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

type testServeResourceTypeOne struct{}

func (rt testServeResourceTypeOne) GetSchema(_ context.Context) (schema.Schema, []*tfprotov6.Diagnostic) {
return schema.Schema{
func (rt testServeResourceTypeOne) GetSchema(_ context.Context) (Schema, []*tfprotov6.Diagnostic) {
return Schema{
Version: 1,
Attributes: map[string]schema.Attribute{
Attributes: map[string]Attribute{
"name": {
Required: true,
Type: types.StringType,
Expand Down
Loading

0 comments on commit bcb5ce1

Please sign in to comment.