Skip to content

Commit

Permalink
internal/proto6server: Always use data source and resource caches, re…
Browse files Browse the repository at this point in the history
…move intermediate Provider field

Reference: #215
Reference: #299

This completes the migration to the cached data source and resource schema/type handling in the new `internal/fwserver` package. This also removes the previous `Provider` field so that implementation detail is now wholly within the `internal/fwserver` to remove any confusion.
  • Loading branch information
bflad committed May 19, 2022
1 parent 004e1b0 commit 1a9057b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 79 deletions.
76 changes: 18 additions & 58 deletions internal/proto6server/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Server struct {
FrameworkServer fwserver.Server

// Provider will be migrated to FrameworkServer over time.
Provider tfsdk.Provider
//Provider tfsdk.Provider
contextCancels []context.CancelFunc
contextCancelsMu sync.Mutex
}
Expand All @@ -47,46 +47,6 @@ func (s *Server) cancelRegisteredContexts(_ context.Context) {
s.contextCancels = nil
}

func (s *Server) getResourceType(ctx context.Context, typ string) (tfsdk.ResourceType, diag.Diagnostics) {
// TODO: Cache GetResources call in GetProviderSchema and reference cache instead
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/299
logging.FrameworkDebug(ctx, "Calling provider defined Provider GetResources")
resourceTypes, diags := s.Provider.GetResources(ctx)
logging.FrameworkDebug(ctx, "Called provider defined Provider GetResources")
if diags.HasError() {
return nil, diags
}
resourceType, ok := resourceTypes[typ]
if !ok {
diags.AddError(
"Resource not found",
fmt.Sprintf("No resource named %q is configured on the provider", typ),
)
return nil, diags
}
return resourceType, diags
}

func (s *Server) getDataSourceType(ctx context.Context, typ string) (tfsdk.DataSourceType, diag.Diagnostics) {
// TODO: Cache GetDataSources call in GetProviderSchema and reference cache instead
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/299
logging.FrameworkDebug(ctx, "Calling provider defined Provider GetDataSources")
dataSourceTypes, diags := s.Provider.GetDataSources(ctx)
logging.FrameworkDebug(ctx, "Called provider defined Provider GetDataSources")
if diags.HasError() {
return nil, diags
}
dataSourceType, ok := dataSourceTypes[typ]
if !ok {
diags.AddError(
"Data source not found",
fmt.Sprintf("No data source named %q is configured on the provider", typ),
)
return nil, diags
}
return dataSourceType, diags
}

func (s *Server) GetProviderSchema(ctx context.Context, proto6Req *tfprotov6.GetProviderSchemaRequest) (*tfprotov6.GetProviderSchemaResponse, error) {
ctx = s.registerContext(ctx)
ctx = logging.InitContext(ctx)
Expand Down Expand Up @@ -223,7 +183,7 @@ func (s *Server) upgradeResourceState(ctx context.Context, req *tfprotov6.Upgrad
return
}

resourceType, diags := s.getResourceType(ctx, req.TypeName)
resourceType, diags := s.FrameworkServer.ResourceType(ctx, req.TypeName)

resp.Diagnostics.Append(diags...)

Expand Down Expand Up @@ -302,7 +262,7 @@ func (s *Server) upgradeResourceState(ctx context.Context, req *tfprotov6.Upgrad
}

logging.FrameworkDebug(ctx, "Calling provider defined ResourceType NewResource")
resource, diags := resourceType.NewResource(ctx, s.Provider)
resource, diags := resourceType.NewResource(ctx, s.FrameworkServer.Provider)
logging.FrameworkDebug(ctx, "Called provider defined ResourceType NewResource")

resp.Diagnostics.Append(diags...)
Expand Down Expand Up @@ -450,7 +410,7 @@ func (s *Server) ReadResource(ctx context.Context, req *tfprotov6.ReadResourceRe
}

func (s *Server) readResource(ctx context.Context, req *tfprotov6.ReadResourceRequest, resp *readResourceResponse) {
resourceType, diags := s.getResourceType(ctx, req.TypeName)
resourceType, diags := s.FrameworkServer.ResourceType(ctx, req.TypeName)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
Expand All @@ -463,7 +423,7 @@ func (s *Server) readResource(ctx context.Context, req *tfprotov6.ReadResourceRe
return
}
logging.FrameworkDebug(ctx, "Calling provider defined ResourceType NewResource")
resource, diags := resourceType.NewResource(ctx, s.Provider)
resource, diags := resourceType.NewResource(ctx, s.FrameworkServer.Provider)
logging.FrameworkDebug(ctx, "Called provider defined ResourceType NewResource")
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand All @@ -483,7 +443,7 @@ func (s *Server) readResource(ctx context.Context, req *tfprotov6.ReadResourceRe
Schema: resourceSchema,
},
}
if pm, ok := s.Provider.(tfsdk.ProviderWithProviderMeta); ok {
if pm, ok := s.FrameworkServer.Provider.(tfsdk.ProviderWithProviderMeta); ok {
logging.FrameworkTrace(ctx, "Provider implements ProviderWithProviderMeta")
logging.FrameworkDebug(ctx, "Calling provider defined Provider GetMetaSchema")
pmSchema, diags := pm.GetMetaSchema(ctx)
Expand Down Expand Up @@ -599,7 +559,7 @@ func (s *Server) PlanResourceChange(ctx context.Context, req *tfprotov6.PlanReso
func (s *Server) planResourceChange(ctx context.Context, req *tfprotov6.PlanResourceChangeRequest, resp *planResourceChangeResponse) {
// get the type of resource, so we can get its schema and create an
// instance
resourceType, diags := s.getResourceType(ctx, req.TypeName)
resourceType, diags := s.FrameworkServer.ResourceType(ctx, req.TypeName)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
Expand Down Expand Up @@ -647,7 +607,7 @@ func (s *Server) planResourceChange(ctx context.Context, req *tfprotov6.PlanReso
// create the resource instance, so we can call its methods and handle
// the request
logging.FrameworkDebug(ctx, "Calling provider defined ResourceType NewResource")
resource, diags := resourceType.NewResource(ctx, s.Provider)
resource, diags := resourceType.NewResource(ctx, s.FrameworkServer.Provider)
logging.FrameworkDebug(ctx, "Called provider defined ResourceType NewResource")
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand Down Expand Up @@ -740,7 +700,7 @@ func (s *Server) planResourceChange(ctx context.Context, req *tfprotov6.PlanReso
Raw: plan,
},
}
if pm, ok := s.Provider.(tfsdk.ProviderWithProviderMeta); ok {
if pm, ok := s.FrameworkServer.Provider.(tfsdk.ProviderWithProviderMeta); ok {
logging.FrameworkTrace(ctx, "Provider implements ProviderWithProviderMeta")
logging.FrameworkDebug(ctx, "Calling provider defined Provider GetMetaSchema")
pmSchema, diags := pm.GetMetaSchema(ctx)
Expand Down Expand Up @@ -811,7 +771,7 @@ func (s *Server) planResourceChange(ctx context.Context, req *tfprotov6.PlanReso
Raw: plan,
},
}
if pm, ok := s.Provider.(tfsdk.ProviderWithProviderMeta); ok {
if pm, ok := s.FrameworkServer.Provider.(tfsdk.ProviderWithProviderMeta); ok {
logging.FrameworkTrace(ctx, "Provider implements ProviderWithProviderMeta")
logging.FrameworkDebug(ctx, "Calling provider defined Provider GetMetaSchema")
pmSchema, diags := pm.GetMetaSchema(ctx)
Expand Down Expand Up @@ -928,7 +888,7 @@ func (s *Server) ApplyResourceChange(ctx context.Context, req *tfprotov6.ApplyRe
func (s *Server) applyResourceChange(ctx context.Context, req *tfprotov6.ApplyResourceChangeRequest, resp *applyResourceChangeResponse) {
// get the type of resource, so we can get its schema and create an
// instance
resourceType, diags := s.getResourceType(ctx, req.TypeName)
resourceType, diags := s.FrameworkServer.ResourceType(ctx, req.TypeName)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
Expand All @@ -947,7 +907,7 @@ func (s *Server) applyResourceChange(ctx context.Context, req *tfprotov6.ApplyRe
// create the resource instance, so we can call its methods and handle
// the request
logging.FrameworkDebug(ctx, "Calling provider defined ResourceType NewResource")
resource, diags := resourceType.NewResource(ctx, s.Provider)
resource, diags := resourceType.NewResource(ctx, s.FrameworkServer.Provider)
logging.FrameworkDebug(ctx, "Called provider defined ResourceType NewResource")
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand Down Expand Up @@ -1020,7 +980,7 @@ func (s *Server) applyResourceChange(ctx context.Context, req *tfprotov6.ApplyRe
Raw: plan,
},
}
if pm, ok := s.Provider.(tfsdk.ProviderWithProviderMeta); ok {
if pm, ok := s.FrameworkServer.Provider.(tfsdk.ProviderWithProviderMeta); ok {
logging.FrameworkTrace(ctx, "Provider implements ProviderWithProviderMeta")
logging.FrameworkDebug(ctx, "Calling provider defined Provider GetMetaSchema")
pmSchema, diags := pm.GetMetaSchema(ctx)
Expand Down Expand Up @@ -1082,7 +1042,7 @@ func (s *Server) applyResourceChange(ctx context.Context, req *tfprotov6.ApplyRe
Raw: priorState,
},
}
if pm, ok := s.Provider.(tfsdk.ProviderWithProviderMeta); ok {
if pm, ok := s.FrameworkServer.Provider.(tfsdk.ProviderWithProviderMeta); ok {
logging.FrameworkTrace(ctx, "Provider implements ProviderWithProviderMeta")
logging.FrameworkDebug(ctx, "Calling provider defined Provider GetMetaSchema")
pmSchema, diags := pm.GetMetaSchema(ctx)
Expand Down Expand Up @@ -1136,7 +1096,7 @@ func (s *Server) applyResourceChange(ctx context.Context, req *tfprotov6.ApplyRe
Raw: priorState,
},
}
if pm, ok := s.Provider.(tfsdk.ProviderWithProviderMeta); ok {
if pm, ok := s.FrameworkServer.Provider.(tfsdk.ProviderWithProviderMeta); ok {
logging.FrameworkTrace(ctx, "Provider implements ProviderWithProviderMeta")
logging.FrameworkDebug(ctx, "Calling provider defined Provider GetMetaSchema")
pmSchema, diags := pm.GetMetaSchema(ctx)
Expand Down Expand Up @@ -1255,7 +1215,7 @@ func (s *Server) ReadDataSource(ctx context.Context, req *tfprotov6.ReadDataSour
}

func (s *Server) readDataSource(ctx context.Context, req *tfprotov6.ReadDataSourceRequest, resp *readDataSourceResponse) {
dataSourceType, diags := s.getDataSourceType(ctx, req.TypeName)
dataSourceType, diags := s.FrameworkServer.DataSourceType(ctx, req.TypeName)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
Expand All @@ -1268,7 +1228,7 @@ func (s *Server) readDataSource(ctx context.Context, req *tfprotov6.ReadDataSour
return
}
logging.FrameworkDebug(ctx, "Calling provider defined DataSourceType NewDataSource")
dataSource, diags := dataSourceType.NewDataSource(ctx, s.Provider)
dataSource, diags := dataSourceType.NewDataSource(ctx, s.FrameworkServer.Provider)
logging.FrameworkDebug(ctx, "Called provider defined DataSourceType NewDataSource")
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand All @@ -1288,7 +1248,7 @@ func (s *Server) readDataSource(ctx context.Context, req *tfprotov6.ReadDataSour
Schema: dataSourceSchema,
},
}
if pm, ok := s.Provider.(tfsdk.ProviderWithProviderMeta); ok {
if pm, ok := s.FrameworkServer.Provider.(tfsdk.ProviderWithProviderMeta); ok {
logging.FrameworkTrace(ctx, "Provider implements ProviderWithProviderMeta")
logging.FrameworkDebug(ctx, "Calling provider defined Provider GetMetaSchema")
pmSchema, diags := pm.GetMetaSchema(ctx)
Expand Down
4 changes: 2 additions & 2 deletions internal/proto6server/serve_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (r importResourceStateResponse) toTfprotov6(ctx context.Context) *tfprotov6
}

func (s *Server) importResourceState(ctx context.Context, req *tfprotov6.ImportResourceStateRequest, resp *importResourceStateResponse) {
resourceType, diags := s.getResourceType(ctx, req.TypeName)
resourceType, diags := s.FrameworkServer.ResourceType(ctx, req.TypeName)
resp.Diagnostics.Append(diags...)

if resp.Diagnostics.HasError() {
Expand All @@ -84,7 +84,7 @@ func (s *Server) importResourceState(ctx context.Context, req *tfprotov6.ImportR
}

logging.FrameworkDebug(ctx, "Calling provider defined ResourceType NewResource")
resource, diags := resourceType.NewResource(ctx, s.Provider)
resource, diags := resourceType.NewResource(ctx, s.FrameworkServer.Provider)
logging.FrameworkDebug(ctx, "Called provider defined ResourceType NewResource")
resp.Diagnostics.Append(diags...)

Expand Down
5 changes: 4 additions & 1 deletion internal/proto6server/serve_import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"
Expand Down Expand Up @@ -164,7 +165,9 @@ func TestServerImportResourceState(t *testing.T) {
importStateFunc: tc.impl,
}
testServer := &Server{
Provider: s,
FrameworkServer: fwserver.Server{
Provider: s,
},
}

got, err := testServer.ImportResourceState(context.Background(), tc.req)
Expand Down
40 changes: 25 additions & 15 deletions internal/proto6server/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1549,8 +1549,8 @@ func TestServerUpgradeResourceState(t *testing.T) {
Diagnostics: []*tfprotov6.Diagnostic{
{
Severity: tfprotov6.DiagnosticSeverityError,
Summary: "Resource not found",
Detail: "No resource named \"\" is configured on the provider",
Summary: "Resource Type Not Found",
Detail: "No resource type named \"\" was found in the provider.",
},
},
},
Expand Down Expand Up @@ -1605,8 +1605,8 @@ func TestServerUpgradeResourceState(t *testing.T) {
Diagnostics: []*tfprotov6.Diagnostic{
{
Severity: tfprotov6.DiagnosticSeverityError,
Summary: "Resource not found",
Detail: "No resource named \"unknown\" is configured on the provider",
Summary: "Resource Type Not Found",
Detail: "No resource type named \"unknown\" was found in the provider.",
},
},
},
Expand Down Expand Up @@ -1799,7 +1799,9 @@ func TestServerUpgradeResourceState(t *testing.T) {
ctx := context.Background()
testProvider := &testServeProvider{}
testServer := &Server{
Provider: testProvider,
FrameworkServer: fwserver.Server{
Provider: testProvider,
},
}

got, err := testServer.UpgradeResourceState(ctx, testCase.request)
Expand Down Expand Up @@ -2220,12 +2222,14 @@ func TestServerReadResource(t *testing.T) {
readResourceImpl: tc.impl,
}
testServer := &Server{
Provider: s,
FrameworkServer: fwserver.Server{
Provider: s,
},
}
var pmSchema tfsdk.Schema
if tc.providerMeta.Type() != nil {
sWithMeta := &testServeProviderWithMetaSchema{s}
testServer.Provider = sWithMeta
testServer.FrameworkServer.Provider = sWithMeta
schema, diags := sWithMeta.GetMetaSchema(context.Background())
if len(diags) > 0 {
t.Errorf("Unexpected diags: %+v", diags)
Expand All @@ -2234,7 +2238,7 @@ func TestServerReadResource(t *testing.T) {
pmSchema = schema
}

rt, diags := testServer.getResourceType(context.Background(), tc.resource)
rt, diags := testServer.FrameworkServer.ResourceType(context.Background(), tc.resource)
if len(diags) > 0 {
t.Errorf("Unexpected diags: %+v", diags)
return
Expand Down Expand Up @@ -4296,7 +4300,9 @@ func TestServerPlanResourceChange(t *testing.T) {
modifyPlanFunc: tc.modifyPlanFunc,
}
testServer := &Server{
Provider: s,
FrameworkServer: fwserver.Server{
Provider: s,
},
}

priorStateDV, err := tfprotov6.NewDynamicValue(tc.resourceType, tc.priorState)
Expand Down Expand Up @@ -5856,12 +5862,14 @@ func TestServerApplyResourceChange(t *testing.T) {
deleteFunc: tc.destroy,
}
testServer := &Server{
Provider: s,
FrameworkServer: fwserver.Server{
Provider: s,
},
}
var pmSchema tfsdk.Schema
if tc.providerMeta.Type() != nil {
sWithMeta := &testServeProviderWithMetaSchema{s}
testServer.Provider = sWithMeta
testServer.FrameworkServer.Provider = sWithMeta
schema, diags := sWithMeta.GetMetaSchema(context.Background())
if len(diags) > 0 {
t.Errorf("Unexpected diags: %+v", diags)
Expand All @@ -5870,7 +5878,7 @@ func TestServerApplyResourceChange(t *testing.T) {
pmSchema = schema
}

rt, diags := testServer.getResourceType(context.Background(), tc.resource)
rt, diags := testServer.FrameworkServer.ResourceType(context.Background(), tc.resource)
if len(diags) > 0 {
t.Errorf("Unexpected diags: %+v", diags)
return
Expand Down Expand Up @@ -6382,12 +6390,14 @@ func TestServerReadDataSource(t *testing.T) {
readDataSourceImpl: tc.impl,
}
testServer := &Server{
Provider: s,
FrameworkServer: fwserver.Server{
Provider: s,
},
}
var pmSchema tfsdk.Schema
if tc.providerMeta.Type() != nil {
sWithMeta := &testServeProviderWithMetaSchema{s}
testServer.Provider = sWithMeta
testServer.FrameworkServer.Provider = sWithMeta
schema, diags := sWithMeta.GetMetaSchema(context.Background())
if len(diags) > 0 {
t.Errorf("Unexpected diags: %+v", diags)
Expand All @@ -6396,7 +6406,7 @@ func TestServerReadDataSource(t *testing.T) {
pmSchema = schema
}

rt, diags := testServer.getDataSourceType(context.Background(), tc.dataSource)
rt, diags := testServer.FrameworkServer.DataSourceType(context.Background(), tc.dataSource)
if len(diags) > 0 {
t.Errorf("Unexpected diags: %+v", diags)
return
Expand Down
3 changes: 0 additions & 3 deletions providerserver/providerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ func NewProtocol6(p tfsdk.Provider) func() tfprotov6.ProviderServer {
FrameworkServer: fwserver.Server{
Provider: p,
},
Provider: p,
}
}
}
Expand All @@ -37,7 +36,6 @@ func NewProtocol6WithError(p tfsdk.Provider) func() (tfprotov6.ProviderServer, e
FrameworkServer: fwserver.Server{
Provider: p,
},
Provider: p,
}, nil
}
}
Expand Down Expand Up @@ -65,7 +63,6 @@ func Serve(ctx context.Context, providerFunc func() tfsdk.Provider, opts ServeOp
FrameworkServer: fwserver.Server{
Provider: provider,
},
Provider: provider,
}
},
tf6serverOpts...,
Expand Down

0 comments on commit 1a9057b

Please sign in to comment.