From 184c70a11e11788facbc38f26a18567e75b48ece Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Mon, 12 Sep 2022 17:37:49 -0400 Subject: [PATCH 1/3] provider: Remove DataSourceType and ResourceType types, Provider GetDataSources and GetResources methods Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/441 Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/442 Providers should use the `Provider` interface `DataSources` and `Resources` methods after migrating `DataSourceType` and `ResourceType` implementations into their respective `datasource.DataSource` and `resource.Resource`. --- .changelog/pending.txt | 15 + datasource/data_source.go | 31 +- internal/fwserver/server.go | 198 +----- .../fwserver/server_getproviderschema_test.go | 312 +--------- .../server_upgraderesourcestate_test.go | 4 +- .../server_validatedatasourceconfig_test.go | 42 +- .../server_validateresourceconfig_test.go | 42 +- .../server_applyresourcechange_test.go | 564 ++++++++---------- .../server_getproviderschema_test.go | 355 +---------- .../server_importresourcestate_test.go | 60 +- .../server_planresourcechange_test.go | 252 ++++---- .../server_readdatasource_test.go | 77 ++- .../proto5server/server_readresource_test.go | 119 ++-- .../server_upgraderesourcestate_test.go | 45 +- .../server_validatedatasourceconfig_test.go | 21 +- .../server_validateresourcetypeconfig_test.go | 21 +- .../server_applyresourcechange_test.go | 564 ++++++++---------- .../server_getproviderschema_test.go | 355 +---------- .../server_importresourcestate_test.go | 60 +- .../server_planresourcechange_test.go | 252 ++++---- .../server_readdatasource_test.go | 77 ++- .../proto6server/server_readresource_test.go | 119 ++-- .../server_upgraderesourcestate_test.go | 45 +- .../server_validatedataresourceconfig_test.go | 21 +- .../server_validateresourceconfig_test.go | 21 +- internal/testing/testprovider/datasource.go | 24 +- .../testing/testprovider/datasourcetype.go | 37 -- .../testprovider/datasourcewithgetschema.go | 29 - .../datasourcewithgetschema_temp.go | 137 ----- .../testprovider/datasourcewithmetadata.go | 27 - internal/testing/testprovider/provider.go | 4 - .../providerwithgetdatasources.go | 31 - .../testprovider/providerwithgetresources.go | 31 - internal/testing/testprovider/resource.go | 30 +- internal/testing/testprovider/resourcetype.go | 37 -- .../testprovider/resourcewithgetschema.go | 29 - .../resourcewithgetschema_temp.go | 281 --------- .../testprovider/resourcewithmetadata.go | 27 - provider/data_source_type.go | 24 - provider/provider.go | 82 +-- provider/resource_type.go | 24 - resource/resource.go | 31 +- .../plugin/framework/data-sources/index.mdx | 4 +- .../docs/plugin/framework/resources/index.mdx | 4 +- 44 files changed, 1344 insertions(+), 3221 deletions(-) create mode 100644 .changelog/pending.txt delete mode 100644 internal/testing/testprovider/datasourcetype.go delete mode 100644 internal/testing/testprovider/datasourcewithgetschema.go delete mode 100644 internal/testing/testprovider/datasourcewithgetschema_temp.go delete mode 100644 internal/testing/testprovider/datasourcewithmetadata.go delete mode 100644 internal/testing/testprovider/providerwithgetdatasources.go delete mode 100644 internal/testing/testprovider/providerwithgetresources.go delete mode 100644 internal/testing/testprovider/resourcetype.go delete mode 100644 internal/testing/testprovider/resourcewithgetschema.go delete mode 100644 internal/testing/testprovider/resourcewithgetschema_temp.go delete mode 100644 internal/testing/testprovider/resourcewithmetadata.go delete mode 100644 provider/data_source_type.go delete mode 100644 provider/resource_type.go diff --git a/.changelog/pending.txt b/.changelog/pending.txt new file mode 100644 index 000000000..582f0c1f5 --- /dev/null +++ b/.changelog/pending.txt @@ -0,0 +1,15 @@ +```release-note:breaking-change +datasource: The `DataSource` interface now requires the `GetSchema` and `Metadata` methods. +``` + +```release-note:breaking-change +provider: The `Provider` interface `GetDataSources` and `GetResources` methods have been removed. Use the `DataSources` and `Resources` methods instead. +``` + +```release-note:breaking-change +provider: The `DataSourceType` and `ResourceType` types have been removed. Use the `GetSchema`, `Metadata`, and optionally the `Configure` methods on `datasource.DataSource` and `resource.Resource` implementations instead. +``` + +```release-note:breaking-change +resource: The `Resource` interface now requires the `GetSchema` and `Metadata` methods. +``` diff --git a/datasource/data_source.go b/datasource/data_source.go index 0d74ab11e..46e417754 100644 --- a/datasource/data_source.go +++ b/datasource/data_source.go @@ -16,6 +16,13 @@ import ( // - Validation: Schema-based via tfsdk.Attribute or entire configuration // via DataSourceWithConfigValidators or DataSourceWithValidateConfig. type DataSource interface { + // Metadata should return the full name of the data source, such as + // examplecloud_thing. + Metadata(context.Context, MetadataRequest, *MetadataResponse) + + // GetSchema returns the schema for this data source. + GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) + // Read is called when the provider must read data source values in // order to update state. Config values should be read from the // ReadRequest and new state values set on the ReadResponse. @@ -53,30 +60,6 @@ type DataSourceWithConfigValidators interface { ConfigValidators(context.Context) []ConfigValidator } -// DataSourceWithGetSchema is an interface type that extends DataSource to -// return its schema definition. -// -// This method will be required in the DataSource interface in a future -// release. -type DataSourceWithGetSchema interface { - // GetSchema returns the schema for this data source. - GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) -} - -// DataSourceWithMetadata is an interface type that extends DataSource to -// return metadata, such as its data source type name. For example, if the -// provider is named examplecloud and the data source reads a thing, this -// should return examplecloud_thing. -// -// This method will be required in the DataSource interface a future release. -type DataSourceWithMetadata interface { - DataSource - - // Metadata should return the full name of the data source, such as - // examplecloud_thing. - Metadata(context.Context, MetadataRequest, *MetadataResponse) -} - // DataSourceWithValidateConfig is an interface type that extends DataSource to include imperative validation. // // Declaring validation using this methodology simplifies one-off diff --git a/internal/fwserver/server.go b/internal/fwserver/server.go index 85e73bcd5..8a97cac61 100644 --- a/internal/fwserver/server.go +++ b/internal/fwserver/server.go @@ -48,12 +48,6 @@ type Server struct { // Provider.DataSources() method. dataSourceFuncs map[string]func() datasource.DataSource - // dataSourceTypes is the cached DataSourceTypes for RPCs that need to - // access data sources. If not found, it will be fetched from the - // Provider.GetDataSources() method. - //nolint:staticcheck // Internal implementation - dataSourceTypes map[string]provider.DataSourceType - // dataSourceTypesDiags is the cached Diagnostics obtained while populating // dataSourceTypes. This is to ensure any warnings or errors are also // returned appropriately when fetching dataSourceTypes. @@ -114,12 +108,6 @@ type Server struct { // Provider.Resources() method. resourceFuncs map[string]func() resource.Resource - // resourceTypes is the cached ResourceTypes for RPCs that need to - // access resources. If not found, it will be fetched from the - // Provider.GetResources() method. - //nolint:staticcheck // Internal implementation - resourceTypes map[string]provider.ResourceType - // resourceTypesDiags is the cached Diagnostics obtained while populating // resourceTypes. This is to ensure any warnings or errors are also // returned appropriately when fetching resourceTypes. @@ -136,14 +124,6 @@ func (s *Server) DataSource(ctx context.Context, typeName string) (datasource.Da dataSourceFunc, ok := dataSourceFuncs[typeName] - if ok { - return dataSourceFunc(), diags - } - - dataSourceTypes, diags := s.DataSourceTypes(ctx) - - dataSourceType, ok := dataSourceTypes[typeName] - if !ok { diags.AddError( "Data Source Type Not Found", @@ -153,11 +133,7 @@ func (s *Server) DataSource(ctx context.Context, typeName string) (datasource.Da return nil, diags } - logging.FrameworkDebug(ctx, "Calling provider defined DataSourceType NewDataSource") - dataSource, diags := dataSourceType.NewDataSource(ctx, s.Provider) - logging.FrameworkDebug(ctx, "Called provider defined DataSourceType NewDataSource") - - return dataSource, diags + return dataSourceFunc(), diags } // DataSourceFuncs returns a map of DataSource functions. The results are cached @@ -173,36 +149,19 @@ func (s *Server) DataSourceFuncs(ctx context.Context) (map[string]func() datasou s.dataSourceFuncs = make(map[string]func() datasource.DataSource) - providerWithDataSources, ok := s.Provider.(provider.ProviderWithDataSources) - - if !ok { - return s.dataSourceFuncs, nil - } - logging.FrameworkDebug(ctx, "Calling provider defined Provider DataSources") - dataSourceFuncsSlice := providerWithDataSources.DataSources(ctx) + dataSourceFuncsSlice := s.Provider.DataSources(ctx) logging.FrameworkDebug(ctx, "Called provider defined Provider DataSources") for _, dataSourceFunc := range dataSourceFuncsSlice { dataSource := dataSourceFunc() - dataSourceWithMetadata, ok := dataSource.(datasource.DataSourceWithMetadata) - - if !ok { - s.dataSourceTypesDiags.AddError( - "Data Source Type Name Missing", - fmt.Sprintf("The %T DataSource in the provider DataSources method results is missing the Metadata method. ", dataSource)+ - "This is always an issue with the provider and should be reported to the provider developers.", - ) - continue - } - dataSourceTypeNameReq := datasource.MetadataRequest{ ProviderTypeName: s.providerTypeName, } dataSourceTypeNameResp := datasource.MetadataResponse{} - dataSourceWithMetadata.Metadata(ctx, dataSourceTypeNameReq, &dataSourceTypeNameResp) + dataSource.Metadata(ctx, dataSourceTypeNameReq, &dataSourceTypeNameResp) if dataSourceTypeNameResp.TypeName == "" { s.dataSourceTypesDiags.AddError( @@ -271,7 +230,7 @@ func (s *Server) DataSourceSchemas(ctx context.Context) (map[string]fwschema.Sch for dataSourceTypeName, dataSourceFunc := range dataSourceFuncs { dataSource := dataSourceFunc() - dataSourceWithGetSchema, ok := dataSource.(datasource.DataSourceWithGetSchema) + dataSourceWithGetSchema, ok := dataSource.(datasource.DataSource) if !ok { s.dataSourceSchemasDiags.AddError( @@ -295,65 +254,9 @@ func (s *Server) DataSourceSchemas(ctx context.Context) (map[string]fwschema.Sch s.dataSourceSchemas[dataSourceTypeName] = schema } - if len(s.dataSourceSchemas) > 0 || s.dataSourceSchemasDiags.HasError() { - return s.dataSourceSchemas, s.dataSourceSchemasDiags - } - - dataSourceTypes, diags := s.DataSourceTypes(ctx) - - s.dataSourceSchemasDiags = diags - - if s.dataSourceSchemasDiags.HasError() { - return s.dataSourceSchemas, s.dataSourceSchemasDiags - } - - for dataSourceTypeName, dataSourceType := range dataSourceTypes { - logging.FrameworkTrace(ctx, "Found data source type", map[string]interface{}{logging.KeyDataSourceType: dataSourceTypeName}) - - logging.FrameworkDebug(ctx, "Calling provider defined DataSourceType GetSchema", map[string]interface{}{logging.KeyDataSourceType: dataSourceTypeName}) - schema, diags := dataSourceType.GetSchema(ctx) - logging.FrameworkDebug(ctx, "Called provider defined DataSourceType GetSchema", map[string]interface{}{logging.KeyDataSourceType: dataSourceTypeName}) - - s.dataSourceSchemasDiags.Append(diags...) - - if s.dataSourceSchemasDiags.HasError() { - return s.dataSourceSchemas, s.dataSourceSchemasDiags - } - - s.dataSourceSchemas[dataSourceTypeName] = &schema - } - return s.dataSourceSchemas, s.dataSourceSchemasDiags } -// DataSourceTypes returns the map of DataSourceTypes. The results are cached -// on first use. -// -//nolint:staticcheck // Internal implementation -func (s *Server) DataSourceTypes(ctx context.Context) (map[string]provider.DataSourceType, diag.Diagnostics) { - logging.FrameworkTrace(ctx, "Checking DataSourceTypes lock") - s.dataSourceTypesMutex.Lock() - defer s.dataSourceTypesMutex.Unlock() - - if s.dataSourceTypes != nil { - return s.dataSourceTypes, s.dataSourceTypesDiags - } - - s.dataSourceTypes = make(map[string]provider.DataSourceType) - - providerWithGetDataSources, ok := s.Provider.(provider.ProviderWithGetDataSources) //nolint:staticcheck // Internal usage - - if !ok { - return s.dataSourceTypes, nil - } - - logging.FrameworkDebug(ctx, "Calling provider defined Provider GetDataSources") - s.dataSourceTypes, s.dataSourceTypesDiags = providerWithGetDataSources.GetDataSources(ctx) //nolint:staticcheck // Internal usage - logging.FrameworkDebug(ctx, "Called provider defined Provider GetDataSources") - - return s.dataSourceTypes, s.dataSourceTypesDiags -} - // ProviderSchema returns the Schema associated with the Provider. The Schema // and Diagnostics are cached on first use. func (s *Server) ProviderSchema(ctx context.Context) (fwschema.Schema, diag.Diagnostics) { @@ -410,14 +313,6 @@ func (s *Server) Resource(ctx context.Context, typeName string) (resource.Resour resourceFunc, ok := resourceFuncs[typeName] - if ok { - return resourceFunc(), diags - } - - resourceTypes, diags := s.ResourceTypes(ctx) - - resourceType, ok := resourceTypes[typeName] - if !ok { diags.AddError( "Resource Type Not Found", @@ -427,11 +322,7 @@ func (s *Server) Resource(ctx context.Context, typeName string) (resource.Resour return nil, diags } - logging.FrameworkDebug(ctx, "Calling provider defined ResourceType NewResource") - resource, diags := resourceType.NewResource(ctx, s.Provider) - logging.FrameworkDebug(ctx, "Called provider defined ResourceType NewResource") - - return resource, diags + return resourceFunc(), diags } // ResourceFuncs returns a map of Resource functions. The results are cached @@ -447,36 +338,19 @@ func (s *Server) ResourceFuncs(ctx context.Context) (map[string]func() resource. s.resourceFuncs = make(map[string]func() resource.Resource) - providerWithResources, ok := s.Provider.(provider.ProviderWithResources) - - if !ok { - return s.resourceFuncs, nil - } - logging.FrameworkDebug(ctx, "Calling provider defined Provider Resources") - resourceFuncsSlice := providerWithResources.Resources(ctx) + resourceFuncsSlice := s.Provider.Resources(ctx) logging.FrameworkDebug(ctx, "Called provider defined Provider Resources") for _, resourceFunc := range resourceFuncsSlice { res := resourceFunc() - resourceWithMetadata, ok := res.(resource.ResourceWithMetadata) - - if !ok { - s.resourceTypesDiags.AddError( - "Resource Type Name Missing", - fmt.Sprintf("The %T Resource in the provider Resources method results is missing the Metadata method. ", res)+ - "This is always an issue with the provider and should be reported to the provider developers.", - ) - continue - } - resourceTypeNameReq := resource.MetadataRequest{ ProviderTypeName: s.providerTypeName, } resourceTypeNameResp := resource.MetadataResponse{} - resourceWithMetadata.Metadata(ctx, resourceTypeNameReq, &resourceTypeNameResp) + res.Metadata(ctx, resourceTypeNameReq, &resourceTypeNameResp) if resourceTypeNameResp.TypeName == "" { s.resourceTypesDiags.AddError( @@ -545,7 +419,7 @@ func (s *Server) ResourceSchemas(ctx context.Context) (map[string]fwschema.Schem for resourceTypeName, resourceFunc := range resourceFuncs { res := resourceFunc() - resourceWithGetSchema, ok := res.(resource.ResourceWithGetSchema) + resourceWithGetSchema, ok := res.(resource.Resource) if !ok { s.resourceSchemasDiags.AddError( @@ -569,61 +443,5 @@ func (s *Server) ResourceSchemas(ctx context.Context) (map[string]fwschema.Schem s.resourceSchemas[resourceTypeName] = schema } - if len(s.resourceSchemas) > 0 || s.resourceSchemasDiags.HasError() { - return s.resourceSchemas, s.resourceSchemasDiags - } - - resourceTypes, diags := s.ResourceTypes(ctx) - - s.resourceSchemasDiags = diags - - if s.resourceSchemasDiags.HasError() { - return s.resourceSchemas, s.resourceSchemasDiags - } - - for resourceTypeName, resourceType := range resourceTypes { - logging.FrameworkTrace(ctx, "Found resource type", map[string]interface{}{logging.KeyResourceType: resourceTypeName}) - - logging.FrameworkDebug(ctx, "Calling provider defined ResourceType GetSchema", map[string]interface{}{logging.KeyResourceType: resourceTypeName}) - schema, diags := resourceType.GetSchema(ctx) - logging.FrameworkDebug(ctx, "Called provider defined ResourceType GetSchema", map[string]interface{}{logging.KeyResourceType: resourceTypeName}) - - s.resourceSchemasDiags.Append(diags...) - - if s.resourceSchemasDiags.HasError() { - return s.resourceSchemas, s.resourceSchemasDiags - } - - s.resourceSchemas[resourceTypeName] = &schema - } - return s.resourceSchemas, s.resourceSchemasDiags } - -// ResourceTypes returns the map of ResourceTypes. The results are cached -// on first use. -// -//nolint:staticcheck // Internal implementation -func (s *Server) ResourceTypes(ctx context.Context) (map[string]provider.ResourceType, diag.Diagnostics) { - logging.FrameworkTrace(ctx, "Checking ResourceTypes lock") - s.resourceTypesMutex.Lock() - defer s.resourceTypesMutex.Unlock() - - if s.resourceTypes != nil { - return s.resourceTypes, s.resourceTypesDiags - } - - s.resourceTypes = make(map[string]provider.ResourceType) - - providerWithGetResources, ok := s.Provider.(provider.ProviderWithGetResources) //nolint:staticcheck // Internal usage - - if !ok { - return s.resourceTypes, nil - } - - logging.FrameworkDebug(ctx, "Calling provider defined Provider GetResources") - s.resourceTypes, s.resourceTypesDiags = providerWithGetResources.GetResources(ctx) //nolint:staticcheck // Internal usage - logging.FrameworkDebug(ctx, "Called provider defined Provider GetResources") - - return s.resourceTypes, s.resourceTypesDiags -} diff --git a/internal/fwserver/server_getproviderschema_test.go b/internal/fwserver/server_getproviderschema_test.go index 89b89a14e..0a3e02ed2 100644 --- a/internal/fwserver/server_getproviderschema_test.go +++ b/internal/fwserver/server_getproviderschema_test.go @@ -37,13 +37,13 @@ func TestServerGetProviderSchema(t *testing.T) { }, }, }, - "datasourceschemas-DataSources": { + "datasourceschemas": { server: &fwserver.Server{ Provider: &testprovider.Provider{ DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { return []func() datasource.DataSource{ func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchemaAndMetadata{ + return &testprovider.DataSource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{ Attributes: map[string]tfsdk.Attribute{ @@ -60,7 +60,7 @@ func TestServerGetProviderSchema(t *testing.T) { } }, func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchemaAndMetadata{ + return &testprovider.DataSource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{ Attributes: map[string]tfsdk.Attribute{ @@ -107,13 +107,13 @@ func TestServerGetProviderSchema(t *testing.T) { }, }, }, - "datasourceschemas-DataSources-duplicate-type-name": { + "datasourceschemas-duplicate-type-name": { server: &fwserver.Server{ Provider: &testprovider.Provider{ DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { return []func() datasource.DataSource{ func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchemaAndMetadata{ + return &testprovider.DataSource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{ Attributes: map[string]tfsdk.Attribute{ @@ -130,7 +130,7 @@ func TestServerGetProviderSchema(t *testing.T) { } }, func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchemaAndMetadata{ + return &testprovider.DataSource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{ Attributes: map[string]tfsdk.Attribute{ @@ -168,13 +168,13 @@ func TestServerGetProviderSchema(t *testing.T) { }, }, }, - "datasourceschemas-DataSources-empty-type-name": { + "datasourceschemas-empty-type-name": { server: &fwserver.Server{ Provider: &testprovider.Provider{ DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { return []func() datasource.DataSource{ func() datasource.DataSource { - return &testprovider.DataSourceWithMetadata{ + return &testprovider.DataSource{ MetadataMethod: func(_ context.Context, _ datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = "" }, @@ -190,7 +190,7 @@ func TestServerGetProviderSchema(t *testing.T) { Diagnostics: diag.Diagnostics{ diag.NewErrorDiagnostic( "Data Source Type Name Missing", - "The *testprovider.DataSourceWithMetadata DataSource returned an empty string from the Metadata method. "+ + "The *testprovider.DataSource DataSource returned an empty string from the Metadata method. "+ "This is always an issue with the provider and should be reported to the provider developers.", ), }, @@ -201,80 +201,7 @@ func TestServerGetProviderSchema(t *testing.T) { }, }, }, - "datasourceschemas-DataSources-missing-schema": { - server: &fwserver.Server{ - Provider: &testprovider.Provider{ - DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { - return []func() datasource.DataSource{ - func() datasource.DataSource { - return &testprovider.DataSourceWithMetadata{ - MetadataMethod: func(_ context.Context, _ datasource.MetadataRequest, resp *datasource.MetadataResponse) { - resp.TypeName = "test_data_source" - }, - } - }, - } - }, - }, - }, - request: &fwserver.GetProviderSchemaRequest{}, - expectedResponse: &fwserver.GetProviderSchemaResponse{ - DataSourceSchemas: nil, - Diagnostics: diag.Diagnostics{ - diag.NewErrorDiagnostic( - "Data Source Schema Missing", - "The *testprovider.DataSourceWithMetadata DataSource in the provider is missing the GetSchema method. "+ - "This is always an issue with the provider and should be reported to the provider developers.", - ), - }, - Provider: &tfsdk.Schema{}, - ResourceSchemas: map[string]fwschema.Schema{}, - ServerCapabilities: &fwserver.ServerCapabilities{ - PlanDestroy: true, - }, - }, - }, - "datasourceschemas-DataSources-missing-type-name": { - server: &fwserver.Server{ - Provider: &testprovider.Provider{ - DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { - return []func() datasource.DataSource{ - func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchema{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return tfsdk.Schema{ - Attributes: map[string]tfsdk.Attribute{ - "test1": { - Required: true, - Type: types.StringType, - }, - }, - }, nil - }, - } - }, - } - }, - }, - }, - request: &fwserver.GetProviderSchemaRequest{}, - expectedResponse: &fwserver.GetProviderSchemaResponse{ - DataSourceSchemas: nil, - Diagnostics: diag.Diagnostics{ - diag.NewErrorDiagnostic( - "Data Source Type Name Missing", - "The *testprovider.DataSourceWithGetSchema DataSource in the provider DataSources method results is missing the Metadata method. "+ - "This is always an issue with the provider and should be reported to the provider developers.", - ), - }, - Provider: &tfsdk.Schema{}, - ResourceSchemas: map[string]fwschema.Schema{}, - ServerCapabilities: &fwserver.ServerCapabilities{ - PlanDestroy: true, - }, - }, - }, - "datasourceschemas-DataSources-provider-type-name": { + "datasourceschemas-provider-type-name": { server: &fwserver.Server{ Provider: &testprovider.ProviderWithMetadata{ MetadataMethod: func(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) { @@ -284,7 +211,7 @@ func TestServerGetProviderSchema(t *testing.T) { DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { return []func() datasource.DataSource{ func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchemaAndMetadata{ + return &testprovider.DataSource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{ Attributes: map[string]tfsdk.Attribute{ @@ -324,67 +251,6 @@ func TestServerGetProviderSchema(t *testing.T) { }, }, }, - "datasourceschemas-GetDataSources": { - server: &fwserver.Server{ - Provider: &testprovider.ProviderWithGetDataSources{ - //nolint:staticcheck // Internal implementation - GetDataSourcesMethod: func(_ context.Context) (map[string]provider.DataSourceType, diag.Diagnostics) { - return map[string]provider.DataSourceType{ - "test_data_source1": &testprovider.DataSourceType{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return tfsdk.Schema{ - Attributes: map[string]tfsdk.Attribute{ - "test1": { - Required: true, - Type: types.StringType, - }, - }, - }, nil - }, - }, - "test_data_source2": &testprovider.DataSourceType{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return tfsdk.Schema{ - Attributes: map[string]tfsdk.Attribute{ - "test2": { - Required: true, - Type: types.StringType, - }, - }, - }, nil - }, - }, - }, nil - }, - }, - }, - request: &fwserver.GetProviderSchemaRequest{}, - expectedResponse: &fwserver.GetProviderSchemaResponse{ - DataSourceSchemas: map[string]fwschema.Schema{ - "test_data_source1": &tfsdk.Schema{ - Attributes: map[string]tfsdk.Attribute{ - "test1": { - Required: true, - Type: types.StringType, - }, - }, - }, - "test_data_source2": &tfsdk.Schema{ - Attributes: map[string]tfsdk.Attribute{ - "test2": { - Required: true, - Type: types.StringType, - }, - }, - }, - }, - Provider: &tfsdk.Schema{}, - ResourceSchemas: map[string]fwschema.Schema{}, - ServerCapabilities: &fwserver.ServerCapabilities{ - PlanDestroy: true, - }, - }, - }, "provider": { server: &fwserver.Server{ Provider: &testprovider.Provider{ @@ -451,74 +317,13 @@ func TestServerGetProviderSchema(t *testing.T) { }, }, }, - "resourceschemas-GetResources": { - server: &fwserver.Server{ - Provider: &testprovider.ProviderWithGetResources{ - //nolint:staticcheck // Internal implementation - GetResourcesMethod: func(_ context.Context) (map[string]provider.ResourceType, diag.Diagnostics) { - return map[string]provider.ResourceType{ - "test_resource1": &testprovider.ResourceType{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return tfsdk.Schema{ - Attributes: map[string]tfsdk.Attribute{ - "test1": { - Required: true, - Type: types.StringType, - }, - }, - }, nil - }, - }, - "test_resource2": &testprovider.ResourceType{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return tfsdk.Schema{ - Attributes: map[string]tfsdk.Attribute{ - "test2": { - Required: true, - Type: types.StringType, - }, - }, - }, nil - }, - }, - }, nil - }, - }, - }, - request: &fwserver.GetProviderSchemaRequest{}, - expectedResponse: &fwserver.GetProviderSchemaResponse{ - DataSourceSchemas: map[string]fwschema.Schema{}, - Provider: &tfsdk.Schema{}, - ResourceSchemas: map[string]fwschema.Schema{ - "test_resource1": &tfsdk.Schema{ - Attributes: map[string]tfsdk.Attribute{ - "test1": { - Required: true, - Type: types.StringType, - }, - }, - }, - "test_resource2": &tfsdk.Schema{ - Attributes: map[string]tfsdk.Attribute{ - "test2": { - Required: true, - Type: types.StringType, - }, - }, - }, - }, - ServerCapabilities: &fwserver.ServerCapabilities{ - PlanDestroy: true, - }, - }, - }, - "resourceschemas-Resources": { + "resourceschemas": { server: &fwserver.Server{ Provider: &testprovider.Provider{ ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{ Attributes: map[string]tfsdk.Attribute{ @@ -535,7 +340,7 @@ func TestServerGetProviderSchema(t *testing.T) { } }, func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{ Attributes: map[string]tfsdk.Attribute{ @@ -582,13 +387,13 @@ func TestServerGetProviderSchema(t *testing.T) { }, }, }, - "resourceschemas-Resources-duplicate-type-name": { + "resourceschemas-duplicate-type-name": { server: &fwserver.Server{ Provider: &testprovider.Provider{ ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{ Attributes: map[string]tfsdk.Attribute{ @@ -605,7 +410,7 @@ func TestServerGetProviderSchema(t *testing.T) { } }, func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{ Attributes: map[string]tfsdk.Attribute{ @@ -643,13 +448,13 @@ func TestServerGetProviderSchema(t *testing.T) { }, }, }, - "resourceschemas-Resources-empty-type-name": { + "resourceschemas-empty-type-name": { server: &fwserver.Server{ Provider: &testprovider.Provider{ ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithMetadata{ + return &testprovider.Resource{ MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "" }, @@ -665,80 +470,7 @@ func TestServerGetProviderSchema(t *testing.T) { Diagnostics: diag.Diagnostics{ diag.NewErrorDiagnostic( "Resource Type Name Missing", - "The *testprovider.ResourceWithMetadata Resource returned an empty string from the Metadata method. "+ - "This is always an issue with the provider and should be reported to the provider developers.", - ), - }, - Provider: &tfsdk.Schema{}, - ResourceSchemas: nil, - ServerCapabilities: &fwserver.ServerCapabilities{ - PlanDestroy: true, - }, - }, - }, - "resourceschemas-Resources-missing-schema": { - server: &fwserver.Server{ - Provider: &testprovider.Provider{ - ResourcesMethod: func(_ context.Context) []func() resource.Resource { - return []func() resource.Resource{ - func() resource.Resource { - return &testprovider.ResourceWithMetadata{ - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_data_source" - }, - } - }, - } - }, - }, - }, - request: &fwserver.GetProviderSchemaRequest{}, - expectedResponse: &fwserver.GetProviderSchemaResponse{ - DataSourceSchemas: nil, - Diagnostics: diag.Diagnostics{ - diag.NewErrorDiagnostic( - "Resource Schema Missing", - "The *testprovider.ResourceWithMetadata Resource in the provider is missing the GetSchema method. "+ - "This is always an issue with the provider and should be reported to the provider developers.", - ), - }, - Provider: &tfsdk.Schema{}, - ResourceSchemas: nil, - ServerCapabilities: &fwserver.ServerCapabilities{ - PlanDestroy: true, - }, - }, - }, - "resourceschemas-Resources-missing-type-name": { - server: &fwserver.Server{ - Provider: &testprovider.Provider{ - ResourcesMethod: func(_ context.Context) []func() resource.Resource { - return []func() resource.Resource{ - func() resource.Resource { - return &testprovider.ResourceWithGetSchema{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return tfsdk.Schema{ - Attributes: map[string]tfsdk.Attribute{ - "test1": { - Required: true, - Type: types.StringType, - }, - }, - }, nil - }, - } - }, - } - }, - }, - }, - request: &fwserver.GetProviderSchemaRequest{}, - expectedResponse: &fwserver.GetProviderSchemaResponse{ - DataSourceSchemas: nil, - Diagnostics: diag.Diagnostics{ - diag.NewErrorDiagnostic( - "Resource Type Name Missing", - "The *testprovider.ResourceWithGetSchema Resource in the provider Resources method results is missing the Metadata method. "+ + "The *testprovider.Resource Resource returned an empty string from the Metadata method. "+ "This is always an issue with the provider and should be reported to the provider developers.", ), }, @@ -749,7 +481,7 @@ func TestServerGetProviderSchema(t *testing.T) { }, }, }, - "resourceschemas-Resources-provider-type-name": { + "resourceschemas-provider-type-name": { server: &fwserver.Server{ Provider: &testprovider.ProviderWithMetadata{ MetadataMethod: func(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) { @@ -759,7 +491,7 @@ func TestServerGetProviderSchema(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{ Attributes: map[string]tfsdk.Attribute{ diff --git a/internal/fwserver/server_upgraderesourcestate_test.go b/internal/fwserver/server_upgraderesourcestate_test.go index da22756ad..9225f7243 100644 --- a/internal/fwserver/server_upgraderesourcestate_test.go +++ b/internal/fwserver/server_upgraderesourcestate_test.go @@ -645,7 +645,7 @@ func TestServerUpgradeResourceState(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return schema, nil }, @@ -687,7 +687,7 @@ func TestServerUpgradeResourceState(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return schema, nil }, diff --git a/internal/fwserver/server_validatedatasourceconfig_test.go b/internal/fwserver/server_validatedatasourceconfig_test.go index 7b914ede2..af33e9f88 100644 --- a/internal/fwserver/server_validatedatasourceconfig_test.go +++ b/internal/fwserver/server_validatedatasourceconfig_test.go @@ -111,7 +111,7 @@ func TestServerValidateDataSourceConfig(t *testing.T) { }, request: &fwserver.ValidateDataSourceConfigRequest{ Config: &testConfig, - DataSource: &testprovider.DataSourceWithGetSchema{ + DataSource: &testprovider.DataSource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, @@ -125,7 +125,7 @@ func TestServerValidateDataSourceConfig(t *testing.T) { }, request: &fwserver.ValidateDataSourceConfigRequest{ Config: &testConfigAttributeValidator, - DataSource: &testprovider.DataSourceWithGetSchema{ + DataSource: &testprovider.DataSource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchemaAttributeValidator, nil }, @@ -139,7 +139,7 @@ func TestServerValidateDataSourceConfig(t *testing.T) { }, request: &fwserver.ValidateDataSourceConfigRequest{ Config: &testConfigAttributeValidatorError, - DataSource: &testprovider.DataSourceWithGetSchema{ + DataSource: &testprovider.DataSource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchemaAttributeValidatorError, nil }, @@ -161,11 +161,12 @@ func TestServerValidateDataSourceConfig(t *testing.T) { }, request: &fwserver.ValidateDataSourceConfigRequest{ Config: &testConfig, - DataSource: &testprovider.DataSourceWithConfigValidatorsAndGetSchemaAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil + DataSource: &testprovider.DataSourceWithConfigValidators{ + DataSource: &testprovider.DataSource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, }, - DataSource: &testprovider.DataSource{}, ConfigValidatorsMethod: func(ctx context.Context) []datasource.ConfigValidator { return []datasource.ConfigValidator{ &testprovider.DataSourceConfigValidator{ @@ -195,11 +196,12 @@ func TestServerValidateDataSourceConfig(t *testing.T) { }, request: &fwserver.ValidateDataSourceConfigRequest{ Config: &testConfig, - DataSource: &testprovider.DataSourceWithConfigValidatorsAndGetSchemaAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil + DataSource: &testprovider.DataSourceWithConfigValidators{ + DataSource: &testprovider.DataSource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, }, - DataSource: &testprovider.DataSource{}, ConfigValidatorsMethod: func(ctx context.Context) []datasource.ConfigValidator { return []datasource.ConfigValidator{ &testprovider.DataSourceConfigValidator{ @@ -225,11 +227,12 @@ func TestServerValidateDataSourceConfig(t *testing.T) { }, request: &fwserver.ValidateDataSourceConfigRequest{ Config: &testConfig, - DataSource: &testprovider.DataSourceWithGetSchemaAndMetadataAndValidateConfig{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil + DataSource: &testprovider.DataSourceWithValidateConfig{ + DataSource: &testprovider.DataSource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, }, - DataSource: &testprovider.DataSource{}, ValidateConfigMethod: func(ctx context.Context, req datasource.ValidateConfigRequest, resp *datasource.ValidateConfigResponse) { var got types.String @@ -253,11 +256,12 @@ func TestServerValidateDataSourceConfig(t *testing.T) { }, request: &fwserver.ValidateDataSourceConfigRequest{ Config: &testConfig, - DataSource: &testprovider.DataSourceWithGetSchemaAndMetadataAndValidateConfig{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil + DataSource: &testprovider.DataSourceWithValidateConfig{ + DataSource: &testprovider.DataSource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, }, - DataSource: &testprovider.DataSource{}, ValidateConfigMethod: func(ctx context.Context, req datasource.ValidateConfigRequest, resp *datasource.ValidateConfigResponse) { resp.Diagnostics.AddWarning("warning summary", "warning detail") resp.Diagnostics.AddError("error summary", "error detail") diff --git a/internal/fwserver/server_validateresourceconfig_test.go b/internal/fwserver/server_validateresourceconfig_test.go index 1ab356023..fd4d32bb9 100644 --- a/internal/fwserver/server_validateresourceconfig_test.go +++ b/internal/fwserver/server_validateresourceconfig_test.go @@ -111,7 +111,7 @@ func TestServerValidateResourceConfig(t *testing.T) { }, request: &fwserver.ValidateResourceConfigRequest{ Config: &testConfig, - Resource: &testprovider.ResourceWithGetSchema{ + Resource: &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, @@ -125,7 +125,7 @@ func TestServerValidateResourceConfig(t *testing.T) { }, request: &fwserver.ValidateResourceConfigRequest{ Config: &testConfigAttributeValidator, - Resource: &testprovider.ResourceWithGetSchema{ + Resource: &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchemaAttributeValidator, nil }, @@ -139,7 +139,7 @@ func TestServerValidateResourceConfig(t *testing.T) { }, request: &fwserver.ValidateResourceConfigRequest{ Config: &testConfigAttributeValidatorError, - Resource: &testprovider.ResourceWithGetSchema{ + Resource: &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchemaAttributeValidatorError, nil }, @@ -161,11 +161,12 @@ func TestServerValidateResourceConfig(t *testing.T) { }, request: &fwserver.ValidateResourceConfigRequest{ Config: &testConfig, - Resource: &testprovider.ResourceWithConfigValidatorsAndGetSchemaAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil + Resource: &testprovider.ResourceWithConfigValidators{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, }, - Resource: &testprovider.Resource{}, ConfigValidatorsMethod: func(ctx context.Context) []resource.ConfigValidator { return []resource.ConfigValidator{ &testprovider.ResourceConfigValidator{ @@ -195,11 +196,12 @@ func TestServerValidateResourceConfig(t *testing.T) { }, request: &fwserver.ValidateResourceConfigRequest{ Config: &testConfig, - Resource: &testprovider.ResourceWithConfigValidatorsAndGetSchemaAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil + Resource: &testprovider.ResourceWithConfigValidators{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, }, - Resource: &testprovider.Resource{}, ConfigValidatorsMethod: func(ctx context.Context) []resource.ConfigValidator { return []resource.ConfigValidator{ &testprovider.ResourceConfigValidator{ @@ -225,11 +227,12 @@ func TestServerValidateResourceConfig(t *testing.T) { }, request: &fwserver.ValidateResourceConfigRequest{ Config: &testConfig, - Resource: &testprovider.ResourceWithGetSchemaAndMetadataAndValidateConfig{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil + Resource: &testprovider.ResourceWithValidateConfig{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, }, - Resource: &testprovider.Resource{}, ValidateConfigMethod: func(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) { var got types.String @@ -253,11 +256,12 @@ func TestServerValidateResourceConfig(t *testing.T) { }, request: &fwserver.ValidateResourceConfigRequest{ Config: &testConfig, - Resource: &testprovider.ResourceWithGetSchemaAndMetadataAndValidateConfig{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil + Resource: &testprovider.ResourceWithValidateConfig{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, }, - Resource: &testprovider.Resource{}, ValidateConfigMethod: func(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) { resp.Diagnostics.AddWarning("warning summary", "warning detail") resp.Diagnostics.AddError("error summary", "error detail") diff --git a/internal/proto5server/server_applyresourcechange_test.go b/internal/proto5server/server_applyresourcechange_test.go index f3a2bf7b1..7450d4025 100644 --- a/internal/proto5server/server_applyresourcechange_test.go +++ b/internal/proto5server/server_applyresourcechange_test.go @@ -84,32 +84,30 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { - var data testSchemaData + CreateMethod: func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var data testSchemaData - resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) - if data.TestRequired.Value != "test-config-value" { - resp.Diagnostics.AddError("Unexpected req.Config Value", "Got: "+data.TestRequired.Value) - } + if data.TestRequired.Value != "test-config-value" { + resp.Diagnostics.AddError("Unexpected req.Config Value", "Got: "+data.TestRequired.Value) + } - // Prevent missing resource state error diagnostic - resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Delete") - }, - UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Update") - }, + // Prevent missing resource state error diagnostic + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Delete") + }, + UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Update") }, } }, @@ -144,32 +142,30 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { - var data testSchemaData + CreateMethod: func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var data testSchemaData - resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) - if data.TestComputed.Value != "test-plannedstate-value" { - resp.Diagnostics.AddError("Unexpected req.Plan Value", "Got: "+data.TestComputed.Value) - } + if data.TestComputed.Value != "test-plannedstate-value" { + resp.Diagnostics.AddError("Unexpected req.Plan Value", "Got: "+data.TestComputed.Value) + } - // Prevent missing resource state error diagnostic - resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Delete") - }, - UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Update") - }, + // Prevent missing resource state error diagnostic + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Delete") + }, + UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Update") }, } }, @@ -205,35 +201,33 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { - var metadata testProviderMetaData + CreateMethod: func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var metadata testProviderMetaData - resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &metadata)...) + resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &metadata)...) - if metadata.TestProviderMetaAttribute.Value != "test-provider-meta-value" { - resp.Diagnostics.AddError("Unexpected req.ProviderMeta Value", "Got: "+metadata.TestProviderMetaAttribute.Value) - } + if metadata.TestProviderMetaAttribute.Value != "test-provider-meta-value" { + resp.Diagnostics.AddError("Unexpected req.ProviderMeta Value", "Got: "+metadata.TestProviderMetaAttribute.Value) + } - // Prevent missing resource state error diagnostic - var data testSchemaData + // Prevent missing resource state error diagnostic + var data testSchemaData - resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) - resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Delete") - }, - UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Update") - }, + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Delete") + }, + UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Update") }, } }, @@ -273,24 +267,22 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { - resp.Diagnostics.AddWarning("warning summary", "warning detail") - resp.Diagnostics.AddError("error summary", "error detail") - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Delete") - }, - UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Update") - }, + CreateMethod: func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + resp.Diagnostics.AddWarning("warning summary", "warning detail") + resp.Diagnostics.AddError("error summary", "error detail") + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Delete") + }, + UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Update") }, } }, @@ -334,26 +326,24 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { - var data testSchemaData + CreateMethod: func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var data testSchemaData - resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) - resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Delete") - }, - UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Update") - }, + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Delete") + }, + UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Update") }, } }, @@ -388,23 +378,21 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { - // Intentionally missing resp.State.Set() - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Delete") - }, - UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Update") - }, + CreateMethod: func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + // Intentionally missing resp.State.Set() + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Delete") + }, + UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Update") }, } }, @@ -446,30 +434,28 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { - var data testSchemaData + CreateMethod: func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var data testSchemaData - // Prevent missing resource state error diagnostic - resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) + // Prevent missing resource state error diagnostic + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) - diags := resp.Private.SetKey(ctx, "providerKey", []byte(`{"key": "value"}`)) + diags := resp.Private.SetKey(ctx, "providerKey", []byte(`{"key": "value"}`)) - resp.Diagnostics.Append(diags...) - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Delete") - }, - UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Update") - }, + resp.Diagnostics.Append(diags...) + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Delete") + }, + UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Update") }, } }, @@ -498,29 +484,27 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Create") - }, - DeleteMethod: func(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { - var data testSchemaData + CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Create") + }, + DeleteMethod: func(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + var data testSchemaData - resp.Diagnostics.Append(req.State.Get(ctx, &data)...) + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) - if data.TestRequired.Value != "test-priorstate-value" { - resp.Diagnostics.AddError("Unexpected req.State Value", "Got: "+data.TestRequired.Value) - } - }, - UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Update") - }, + if data.TestRequired.Value != "test-priorstate-value" { + resp.Diagnostics.AddError("Unexpected req.State Value", "Got: "+data.TestRequired.Value) + } + }, + UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Update") }, } }, @@ -549,29 +533,27 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Create") - }, - DeleteMethod: func(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { - var data testProviderMetaData + CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Create") + }, + DeleteMethod: func(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + var data testProviderMetaData - resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &data)...) + resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &data)...) - if data.TestProviderMetaAttribute.Value != "test-provider-meta-value" { - resp.Diagnostics.AddError("Unexpected req.ProviderMeta Value", "Got: "+data.TestProviderMetaAttribute.Value) - } - }, - UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Update") - }, + if data.TestProviderMetaAttribute.Value != "test-provider-meta-value" { + resp.Diagnostics.AddError("Unexpected req.ProviderMeta Value", "Got: "+data.TestProviderMetaAttribute.Value) + } + }, + UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Update") }, } }, @@ -604,33 +586,31 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Create") - }, - DeleteMethod: func(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { - expected := `{"key": "value"}` - got, diags := req.Private.GetKey(ctx, "providerKey") + CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Create") + }, + DeleteMethod: func(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + expected := `{"key": "value"}` + got, diags := req.Private.GetKey(ctx, "providerKey") - resp.Diagnostics.Append(diags...) + resp.Diagnostics.Append(diags...) - if string(got) != expected { - resp.Diagnostics.AddError( - "Unexpected req.Private Value", - fmt.Sprintf("expected %q, got %q", expected, got), - ) - } - }, - UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Update") - }, + if string(got) != expected { + resp.Diagnostics.AddError( + "Unexpected req.Private Value", + fmt.Sprintf("expected %q, got %q", expected, got), + ) + } + }, + UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Update") }, } }, @@ -660,24 +640,22 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Create") - }, - DeleteMethod: func(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddWarning("warning summary", "warning detail") - resp.Diagnostics.AddError("error summary", "error detail") - }, - UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Update") - }, + CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Create") + }, + DeleteMethod: func(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddWarning("warning summary", "warning detail") + resp.Diagnostics.AddError("error summary", "error detail") + }, + UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Update") }, } }, @@ -720,23 +698,21 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Create") - }, - DeleteMethod: func(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { - // Intentionally empty, should call resp.State.RemoveResource() automatically. - }, - UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Update") - }, + CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Create") + }, + DeleteMethod: func(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + // Intentionally empty, should call resp.State.RemoveResource() automatically. + }, + UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Update") }, } }, @@ -764,30 +740,28 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") + CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") - }, - UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { - var data testSchemaData + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") + }, + UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var data testSchemaData - resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) - if data.TestRequired.Value != "test-new-value" { - resp.Diagnostics.AddError("Unexpected req.Config Value", "Got: "+data.TestRequired.Value) - } - }, + if data.TestRequired.Value != "test-new-value" { + resp.Diagnostics.AddError("Unexpected req.Config Value", "Got: "+data.TestRequired.Value) + } }, } }, @@ -826,30 +800,28 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") + CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") - }, - UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { - var data testSchemaData + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") + }, + UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var data testSchemaData - resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) - if data.TestComputed.Value != "test-plannedstate-value" { - resp.Diagnostics.AddError("Unexpected req.Plan Value", "Got: "+data.TestComputed.Value) - } - }, + if data.TestComputed.Value != "test-plannedstate-value" { + resp.Diagnostics.AddError("Unexpected req.Plan Value", "Got: "+data.TestComputed.Value) + } }, } }, @@ -888,29 +860,27 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") - }, - UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { - var data testSchemaData + CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") + }, + UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var data testSchemaData - resp.Diagnostics.Append(req.State.Get(ctx, &data)...) + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) - if data.TestRequired.Value != "test-old-value" { - resp.Diagnostics.AddError("Unexpected req.State Value", "Got: "+data.TestRequired.Value) - } - }, + if data.TestRequired.Value != "test-old-value" { + resp.Diagnostics.AddError("Unexpected req.State Value", "Got: "+data.TestRequired.Value) + } }, } }, @@ -950,29 +920,27 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") - }, - UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { - var data testProviderMetaData + CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") + }, + UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var data testProviderMetaData - resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &data)...) + resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &data)...) - if data.TestProviderMetaAttribute.Value != "test-provider-meta-value" { - resp.Diagnostics.AddError("Unexpected req.ProviderMeta Value", "Got: "+data.TestProviderMetaAttribute.Value) - } - }, + if data.TestProviderMetaAttribute.Value != "test-provider-meta-value" { + resp.Diagnostics.AddError("Unexpected req.ProviderMeta Value", "Got: "+data.TestProviderMetaAttribute.Value) + } }, } }, @@ -1017,33 +985,31 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") - }, - UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { - expected := `{"providerKey": "provider value"}` - got, diags := req.Private.GetKey(ctx, "providerKey") + CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") + }, + UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + expected := `{"providerKey": "provider value"}` + got, diags := req.Private.GetKey(ctx, "providerKey") - resp.Diagnostics.Append(diags...) + resp.Diagnostics.Append(diags...) - if string(got) != expected { - resp.Diagnostics.AddError( - "Unexpected req.Private Value", - fmt.Sprintf("expected %q, got %q", expected, got), - ) - } - }, + if string(got) != expected { + resp.Diagnostics.AddError( + "Unexpected req.Private Value", + fmt.Sprintf("expected %q, got %q", expected, got), + ) + } }, } }, @@ -1091,24 +1057,22 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") - }, - UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { - resp.Diagnostics.AddWarning("warning summary", "warning detail") - resp.Diagnostics.AddError("error summary", "error detail") - }, + CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") + }, + UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + resp.Diagnostics.AddWarning("warning summary", "warning detail") + resp.Diagnostics.AddError("error summary", "error detail") }, } }, @@ -1158,26 +1122,24 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") - }, - UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { - var data testSchemaData + CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") + }, + UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var data testSchemaData - resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) - resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) - }, + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) }, } }, @@ -1215,23 +1177,21 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") - }, - UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { - resp.State.RemoveResource(ctx) - }, + CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") + }, + UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + resp.State.RemoveResource(ctx) }, } }, @@ -1274,25 +1234,23 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") - }, - UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { - diags := resp.Private.SetKey(ctx, "providerKey", []byte(`{"providerKey": "provider value"}`)) + CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") + }, + UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + diags := resp.Private.SetKey(ctx, "providerKey", []byte(`{"providerKey": "provider value"}`)) - resp.Diagnostics.Append(diags...) - }, + resp.Diagnostics.Append(diags...) }, } }, diff --git a/internal/proto5server/server_getproviderschema_test.go b/internal/proto5server/server_getproviderschema_test.go index cfd8c72c5..c77e5693f 100644 --- a/internal/proto5server/server_getproviderschema_test.go +++ b/internal/proto5server/server_getproviderschema_test.go @@ -11,7 +11,6 @@ import ( "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/resource" "github.com/hashicorp/terraform-plugin-framework/tfsdk" "github.com/hashicorp/terraform-plugin-framework/types" @@ -29,14 +28,14 @@ func TestServerGetProviderSchema(t *testing.T) { expectedError error expectedResponse *tfprotov5.GetProviderSchemaResponse }{ - "datasourceschemas-DataSources": { + "datasourceschemas": { server: &Server{ FrameworkServer: fwserver.Server{ Provider: &testprovider.Provider{ DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { return []func() datasource.DataSource{ func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchemaAndMetadata{ + return &testprovider.DataSource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{ Attributes: map[string]tfsdk.Attribute{ @@ -53,7 +52,7 @@ func TestServerGetProviderSchema(t *testing.T) { } }, func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchemaAndMetadata{ + return &testprovider.DataSource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{ Attributes: map[string]tfsdk.Attribute{ @@ -109,14 +108,14 @@ func TestServerGetProviderSchema(t *testing.T) { }, }, }, - "datasourceschemas-DataSources-duplicate-type-name": { + "datasourceschemas-duplicate-type-name": { server: &Server{ FrameworkServer: fwserver.Server{ Provider: &testprovider.Provider{ DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { return []func() datasource.DataSource{ func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchemaAndMetadata{ + return &testprovider.DataSource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{ Attributes: map[string]tfsdk.Attribute{ @@ -133,7 +132,7 @@ func TestServerGetProviderSchema(t *testing.T) { } }, func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchemaAndMetadata{ + return &testprovider.DataSource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{ Attributes: map[string]tfsdk.Attribute{ @@ -175,14 +174,14 @@ func TestServerGetProviderSchema(t *testing.T) { }, }, }, - "datasourceschemas-DataSources-empty-type-name": { + "datasourceschemas-empty-type-name": { server: &Server{ FrameworkServer: fwserver.Server{ Provider: &testprovider.Provider{ DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { return []func() datasource.DataSource{ func() datasource.DataSource { - return &testprovider.DataSourceWithMetadata{ + return &testprovider.DataSource{ MetadataMethod: func(_ context.Context, _ datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = "" }, @@ -200,7 +199,7 @@ func TestServerGetProviderSchema(t *testing.T) { { Severity: tfprotov5.DiagnosticSeverityError, Summary: "Data Source Type Name Missing", - Detail: "The *testprovider.DataSourceWithMetadata DataSource returned an empty string from the Metadata method. " + + Detail: "The *testprovider.DataSource DataSource returned an empty string from the Metadata method. " + "This is always an issue with the provider and should be reported to the provider developers.", }, }, @@ -213,160 +212,6 @@ func TestServerGetProviderSchema(t *testing.T) { }, }, }, - "datasourceschemas-DataSources-missing-schema": { - server: &Server{ - FrameworkServer: fwserver.Server{ - Provider: &testprovider.Provider{ - DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { - return []func() datasource.DataSource{ - func() datasource.DataSource { - return &testprovider.DataSourceWithMetadata{ - MetadataMethod: func(_ context.Context, _ datasource.MetadataRequest, resp *datasource.MetadataResponse) { - resp.TypeName = "test_data_source" - }, - } - }, - } - }, - }, - }, - }, - request: &tfprotov5.GetProviderSchemaRequest{}, - expectedResponse: &tfprotov5.GetProviderSchemaResponse{ - DataSourceSchemas: map[string]*tfprotov5.Schema{}, - Diagnostics: []*tfprotov5.Diagnostic{ - { - Severity: tfprotov5.DiagnosticSeverityError, - Summary: "Data Source Schema Missing", - Detail: "The *testprovider.DataSourceWithMetadata DataSource in the provider is missing the GetSchema method. " + - "This is always an issue with the provider and should be reported to the provider developers.", - }, - }, - Provider: &tfprotov5.Schema{ - Block: &tfprotov5.SchemaBlock{}, - }, - ResourceSchemas: map[string]*tfprotov5.Schema{}, - ServerCapabilities: &tfprotov5.ServerCapabilities{ - PlanDestroy: true, - }, - }, - }, - "datasourceschemas-DataSources-missing-type-name": { - server: &Server{ - FrameworkServer: fwserver.Server{ - Provider: &testprovider.Provider{ - DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { - return []func() datasource.DataSource{ - func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchema{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return tfsdk.Schema{ - Attributes: map[string]tfsdk.Attribute{ - "test1": { - Required: true, - Type: types.StringType, - }, - }, - }, nil - }, - } - }, - } - }, - }, - }, - }, - request: &tfprotov5.GetProviderSchemaRequest{}, - expectedResponse: &tfprotov5.GetProviderSchemaResponse{ - DataSourceSchemas: map[string]*tfprotov5.Schema{}, - Diagnostics: []*tfprotov5.Diagnostic{ - { - Severity: tfprotov5.DiagnosticSeverityError, - Summary: "Data Source Type Name Missing", - Detail: "The *testprovider.DataSourceWithGetSchema DataSource in the provider DataSources method results is missing the Metadata method. " + - "This is always an issue with the provider and should be reported to the provider developers.", - }, - }, - Provider: &tfprotov5.Schema{ - Block: &tfprotov5.SchemaBlock{}, - }, - ResourceSchemas: map[string]*tfprotov5.Schema{}, - ServerCapabilities: &tfprotov5.ServerCapabilities{ - PlanDestroy: true, - }, - }, - }, - "datasourceschemas-GetDataSources": { - server: &Server{ - FrameworkServer: fwserver.Server{ - Provider: &testprovider.ProviderWithGetDataSources{ - //nolint:staticcheck // Internal implementation - GetDataSourcesMethod: func(_ context.Context) (map[string]provider.DataSourceType, diag.Diagnostics) { - return map[string]provider.DataSourceType{ - "test_data_source1": &testprovider.DataSourceType{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return tfsdk.Schema{ - Attributes: map[string]tfsdk.Attribute{ - "test1": { - Required: true, - Type: types.StringType, - }, - }, - }, nil - }, - }, - "test_data_source2": &testprovider.DataSourceType{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return tfsdk.Schema{ - Attributes: map[string]tfsdk.Attribute{ - "test2": { - Required: true, - Type: types.StringType, - }, - }, - }, nil - }, - }, - }, nil - }, - }, - }, - }, - request: &tfprotov5.GetProviderSchemaRequest{}, - expectedResponse: &tfprotov5.GetProviderSchemaResponse{ - DataSourceSchemas: map[string]*tfprotov5.Schema{ - "test_data_source1": { - Block: &tfprotov5.SchemaBlock{ - Attributes: []*tfprotov5.SchemaAttribute{ - { - Name: "test1", - Required: true, - Type: tftypes.String, - }, - }, - }, - }, - "test_data_source2": { - Block: &tfprotov5.SchemaBlock{ - Attributes: []*tfprotov5.SchemaAttribute{ - { - Name: "test2", - Required: true, - Type: tftypes.String, - }, - }, - }, - }, - }, - Provider: &tfprotov5.Schema{ - Block: &tfprotov5.SchemaBlock{}, - }, - ResourceSchemas: map[string]*tfprotov5.Schema{}, - ServerCapabilities: &tfprotov5.ServerCapabilities{ - PlanDestroy: true, - }, - }, - }, "provider": { server: &Server{ FrameworkServer: fwserver.Server{ @@ -445,14 +290,14 @@ func TestServerGetProviderSchema(t *testing.T) { }, }, }, - "resourceschemas-Resources": { + "resourceschemas": { server: &Server{ FrameworkServer: fwserver.Server{ Provider: &testprovider.Provider{ ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{ Attributes: map[string]tfsdk.Attribute{ @@ -469,7 +314,7 @@ func TestServerGetProviderSchema(t *testing.T) { } }, func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{ Attributes: map[string]tfsdk.Attribute{ @@ -525,14 +370,14 @@ func TestServerGetProviderSchema(t *testing.T) { }, }, }, - "resourceschemas-Resources-duplicate-type-name": { + "resourceschemas-duplicate-type-name": { server: &Server{ FrameworkServer: fwserver.Server{ Provider: &testprovider.Provider{ ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{ Attributes: map[string]tfsdk.Attribute{ @@ -549,7 +394,7 @@ func TestServerGetProviderSchema(t *testing.T) { } }, func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{ Attributes: map[string]tfsdk.Attribute{ @@ -591,14 +436,14 @@ func TestServerGetProviderSchema(t *testing.T) { }, }, }, - "resourceschemas-Resources-empty-type-name": { + "resourceschemas-empty-type-name": { server: &Server{ FrameworkServer: fwserver.Server{ Provider: &testprovider.Provider{ ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithMetadata{ + return &testprovider.Resource{ MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "" }, @@ -616,90 +461,7 @@ func TestServerGetProviderSchema(t *testing.T) { { Severity: tfprotov5.DiagnosticSeverityError, Summary: "Resource Type Name Missing", - Detail: "The *testprovider.ResourceWithMetadata Resource returned an empty string from the Metadata method. " + - "This is always an issue with the provider and should be reported to the provider developers.", - }, - }, - Provider: &tfprotov5.Schema{ - Block: &tfprotov5.SchemaBlock{}, - }, - ResourceSchemas: map[string]*tfprotov5.Schema{}, - ServerCapabilities: &tfprotov5.ServerCapabilities{ - PlanDestroy: true, - }, - }, - }, - "resourceschemas-Resources-missing-schema": { - server: &Server{ - FrameworkServer: fwserver.Server{ - Provider: &testprovider.Provider{ - ResourcesMethod: func(_ context.Context) []func() resource.Resource { - return []func() resource.Resource{ - func() resource.Resource { - return &testprovider.ResourceWithMetadata{ - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" - }, - } - }, - } - }, - }, - }, - }, - request: &tfprotov5.GetProviderSchemaRequest{}, - expectedResponse: &tfprotov5.GetProviderSchemaResponse{ - DataSourceSchemas: map[string]*tfprotov5.Schema{}, - Diagnostics: []*tfprotov5.Diagnostic{ - { - Severity: tfprotov5.DiagnosticSeverityError, - Summary: "Resource Schema Missing", - Detail: "The *testprovider.ResourceWithMetadata Resource in the provider is missing the GetSchema method. " + - "This is always an issue with the provider and should be reported to the provider developers.", - }, - }, - Provider: &tfprotov5.Schema{ - Block: &tfprotov5.SchemaBlock{}, - }, - ResourceSchemas: map[string]*tfprotov5.Schema{}, - ServerCapabilities: &tfprotov5.ServerCapabilities{ - PlanDestroy: true, - }, - }, - }, - "resourceschemas-Resources-missing-type-name": { - server: &Server{ - FrameworkServer: fwserver.Server{ - Provider: &testprovider.Provider{ - ResourcesMethod: func(_ context.Context) []func() resource.Resource { - return []func() resource.Resource{ - func() resource.Resource { - return &testprovider.ResourceWithGetSchema{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return tfsdk.Schema{ - Attributes: map[string]tfsdk.Attribute{ - "test1": { - Required: true, - Type: types.StringType, - }, - }, - }, nil - }, - } - }, - } - }, - }, - }, - }, - request: &tfprotov5.GetProviderSchemaRequest{}, - expectedResponse: &tfprotov5.GetProviderSchemaResponse{ - DataSourceSchemas: map[string]*tfprotov5.Schema{}, - Diagnostics: []*tfprotov5.Diagnostic{ - { - Severity: tfprotov5.DiagnosticSeverityError, - Summary: "Resource Type Name Missing", - Detail: "The *testprovider.ResourceWithGetSchema Resource in the provider Resources method results is missing the Metadata method. " + + Detail: "The *testprovider.Resource Resource returned an empty string from the Metadata method. " + "This is always an issue with the provider and should be reported to the provider developers.", }, }, @@ -712,77 +474,6 @@ func TestServerGetProviderSchema(t *testing.T) { }, }, }, - "resourceschemas-GetResources": { - server: &Server{ - FrameworkServer: fwserver.Server{ - Provider: &testprovider.ProviderWithGetResources{ - //nolint:staticcheck // Internal implementation - GetResourcesMethod: func(_ context.Context) (map[string]provider.ResourceType, diag.Diagnostics) { - return map[string]provider.ResourceType{ - "test_resource1": &testprovider.ResourceType{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return tfsdk.Schema{ - Attributes: map[string]tfsdk.Attribute{ - "test1": { - Required: true, - Type: types.StringType, - }, - }, - }, nil - }, - }, - "test_resource2": &testprovider.ResourceType{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return tfsdk.Schema{ - Attributes: map[string]tfsdk.Attribute{ - "test2": { - Required: true, - Type: types.StringType, - }, - }, - }, nil - }, - }, - }, nil - }, - }, - }, - }, - request: &tfprotov5.GetProviderSchemaRequest{}, - expectedResponse: &tfprotov5.GetProviderSchemaResponse{ - DataSourceSchemas: map[string]*tfprotov5.Schema{}, - Provider: &tfprotov5.Schema{ - Block: &tfprotov5.SchemaBlock{}, - }, - ResourceSchemas: map[string]*tfprotov5.Schema{ - "test_resource1": { - Block: &tfprotov5.SchemaBlock{ - Attributes: []*tfprotov5.SchemaAttribute{ - { - Name: "test1", - Required: true, - Type: tftypes.String, - }, - }, - }, - }, - "test_resource2": { - Block: &tfprotov5.SchemaBlock{ - Attributes: []*tfprotov5.SchemaAttribute{ - { - Name: "test2", - Required: true, - Type: tftypes.String, - }, - }, - }, - }, - }, - ServerCapabilities: &tfprotov5.ServerCapabilities{ - PlanDestroy: true, - }, - }, - }, } for name, testCase := range testCases { @@ -866,11 +557,6 @@ func TestServerGetProviderSchema_logging(t *testing.T) { "@message": "Called provider defined Provider Resources", "@module": "sdk.framework", }, - { - "@level": "trace", - "@message": "Checking ResourceTypes lock", - "@module": "sdk.framework", - }, { "@level": "trace", "@message": "Checking DataSourceSchemas lock", @@ -891,11 +577,6 @@ func TestServerGetProviderSchema_logging(t *testing.T) { "@message": "Called provider defined Provider DataSources", "@module": "sdk.framework", }, - { - "@level": "trace", - "@message": "Checking DataSourceTypes lock", - "@module": "sdk.framework", - }, } if diff := cmp.Diff(entries, expectedEntries); diff != "" { diff --git a/internal/proto5server/server_importresourcestate_test.go b/internal/proto5server/server_importresourcestate_test.go index 63204f87c..cf08de133 100644 --- a/internal/proto5server/server_importresourcestate_test.go +++ b/internal/proto5server/server_importresourcestate_test.go @@ -65,14 +65,15 @@ func TestServerImportResourceState(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndImportStateAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil + return &testprovider.ResourceWithImportState{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" - }, - Resource: &testprovider.Resource{}, ImportStateMethod: func(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { if req.ID != "test-id" { resp.Diagnostics.AddError("unexpected req.ID value: %s", req.ID) @@ -107,14 +108,15 @@ func TestServerImportResourceState(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndImportStateAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithImportState{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, - Resource: &testprovider.Resource{}, ImportStateMethod: func(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { resp.Diagnostics.AddWarning("warning summary", "warning detail") resp.Diagnostics.AddError("error summary", "error detail") @@ -152,14 +154,15 @@ func TestServerImportResourceState(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndImportStateAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil + return &testprovider.ResourceWithImportState{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" - }, - Resource: &testprovider.Resource{}, ImportStateMethod: func(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) }, @@ -190,14 +193,15 @@ func TestServerImportResourceState(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndImportStateAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithImportState{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, - Resource: &testprovider.Resource{}, ImportStateMethod: func(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { diags := resp.Private.SetKey(ctx, "providerKey", []byte(`{"key": "value"}`)) diff --git a/internal/proto5server/server_planresourcechange_test.go b/internal/proto5server/server_planresourcechange_test.go index 29f7e88b1..d206871c0 100644 --- a/internal/proto5server/server_planresourcechange_test.go +++ b/internal/proto5server/server_planresourcechange_test.go @@ -82,12 +82,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { var data testSchemaData @@ -131,12 +133,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { var data testSchemaData @@ -181,12 +185,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { var data testProviderMetaData @@ -235,12 +241,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { resp.Diagnostics.AddWarning("warning summary", "warning detail") @@ -291,12 +299,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { var data testSchemaData @@ -340,12 +350,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { // This is a strange thing to signal on creation, @@ -394,12 +406,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { var data testSchemaData @@ -437,12 +451,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { var data testProviderMetaData @@ -484,12 +500,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { resp.Diagnostics.AddWarning("warning summary", "warning detail") @@ -533,12 +551,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { // This is invalid logic to run during deletion. @@ -582,12 +602,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { // This is a strange thing to signal on destroy, @@ -629,12 +651,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { var data testSchemaData @@ -681,12 +705,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { var data testSchemaData @@ -733,12 +759,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { var data testSchemaData @@ -786,12 +814,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { var data testProviderMetaData @@ -843,12 +873,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { resp.Diagnostics.AddWarning("warning summary", "warning detail") @@ -902,12 +934,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { var data testSchemaData @@ -954,12 +988,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { resp.RequiresReplace = path.Paths{ diff --git a/internal/proto5server/server_readdatasource_test.go b/internal/proto5server/server_readdatasource_test.go index ff3a46158..d8a0b1148 100644 --- a/internal/proto5server/server_readdatasource_test.go +++ b/internal/proto5server/server_readdatasource_test.go @@ -63,14 +63,13 @@ func TestServerReadDataSource(t *testing.T) { DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { return []func() datasource.DataSource{ func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchemaAndMetadata{ + return &testprovider.DataSource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{}, nil }, MetadataMethod: func(_ context.Context, _ datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = "test_data_source" }, - DataSource: &testprovider.DataSource{}, } }, } @@ -93,26 +92,24 @@ func TestServerReadDataSource(t *testing.T) { DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { return []func() datasource.DataSource{ func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchemaAndMetadata{ + return &testprovider.DataSource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = "test_data_source" }, - DataSource: &testprovider.DataSource{ - ReadMethod: func(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { - var config struct { - TestComputed types.String `tfsdk:"test_computed"` - TestRequired types.String `tfsdk:"test_required"` - } + ReadMethod: func(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var config struct { + TestComputed types.String `tfsdk:"test_computed"` + TestRequired types.String `tfsdk:"test_required"` + } - resp.Diagnostics.Append(req.Config.Get(ctx, &config)...) + resp.Diagnostics.Append(req.Config.Get(ctx, &config)...) - if config.TestRequired.Value != "test-config-value" { - resp.Diagnostics.AddError("unexpected req.Config value: %s", config.TestRequired.Value) - } - }, + if config.TestRequired.Value != "test-config-value" { + resp.Diagnostics.AddError("unexpected req.Config value: %s", config.TestRequired.Value) + } }, } }, @@ -137,26 +134,24 @@ func TestServerReadDataSource(t *testing.T) { DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { return []func() datasource.DataSource{ func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchemaAndMetadata{ + return &testprovider.DataSource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{}, nil }, MetadataMethod: func(_ context.Context, _ datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = "test_data_source" }, - DataSource: &testprovider.DataSource{ - ReadMethod: func(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { - var config struct { - TestComputed types.String `tfsdk:"test_computed"` - TestRequired types.String `tfsdk:"test_required"` - } + ReadMethod: func(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var config struct { + TestComputed types.String `tfsdk:"test_computed"` + TestRequired types.String `tfsdk:"test_required"` + } - resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &config)...) + resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &config)...) - if config.TestRequired.Value != "test-config-value" { - resp.Diagnostics.AddError("unexpected req.ProviderMeta value: %s", config.TestRequired.Value) - } - }, + if config.TestRequired.Value != "test-config-value" { + resp.Diagnostics.AddError("unexpected req.ProviderMeta value: %s", config.TestRequired.Value) + } }, } }, @@ -185,18 +180,16 @@ func TestServerReadDataSource(t *testing.T) { DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { return []func() datasource.DataSource{ func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchemaAndMetadata{ + return &testprovider.DataSource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = "test_data_source" }, - DataSource: &testprovider.DataSource{ - ReadMethod: func(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { - resp.Diagnostics.AddWarning("warning summary", "warning detail") - resp.Diagnostics.AddError("error summary", "error detail") - }, + ReadMethod: func(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + resp.Diagnostics.AddWarning("warning summary", "warning detail") + resp.Diagnostics.AddError("error summary", "error detail") }, } }, @@ -232,26 +225,24 @@ func TestServerReadDataSource(t *testing.T) { DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { return []func() datasource.DataSource{ func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchemaAndMetadata{ + return &testprovider.DataSource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = "test_data_source" }, - DataSource: &testprovider.DataSource{ - ReadMethod: func(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { - var data struct { - TestComputed types.String `tfsdk:"test_computed"` - TestRequired types.String `tfsdk:"test_required"` - } + ReadMethod: func(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var data struct { + TestComputed types.String `tfsdk:"test_computed"` + TestRequired types.String `tfsdk:"test_required"` + } - resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) - data.TestComputed = types.String{Value: "test-state-value"} + data.TestComputed = types.String{Value: "test-state-value"} - resp.Diagnostics.Append(resp.State.Set(ctx, data)...) - }, + resp.Diagnostics.Append(resp.State.Set(ctx, data)...) }, } }, diff --git a/internal/proto5server/server_readresource_test.go b/internal/proto5server/server_readresource_test.go index 15c932e13..a36d3c274 100644 --- a/internal/proto5server/server_readresource_test.go +++ b/internal/proto5server/server_readresource_test.go @@ -68,14 +68,13 @@ func TestServerReadResource(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{}, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{}, } }, } @@ -98,26 +97,24 @@ func TestServerReadResource(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - ReadMethod: func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { - var data struct { - TestComputed types.String `tfsdk:"test_computed"` - TestRequired types.String `tfsdk:"test_required"` - } + ReadMethod: func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + var data struct { + TestComputed types.String `tfsdk:"test_computed"` + TestRequired types.String `tfsdk:"test_required"` + } - resp.Diagnostics.Append(req.State.Get(ctx, &data)...) + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) - if data.TestRequired.Value != "test-currentstate-value" { - resp.Diagnostics.AddError("unexpected req.State value: %s", data.TestRequired.Value) - } - }, + if data.TestRequired.Value != "test-currentstate-value" { + resp.Diagnostics.AddError("unexpected req.State value: %s", data.TestRequired.Value) + } }, } }, @@ -142,26 +139,24 @@ func TestServerReadResource(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{}, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - ReadMethod: func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { - var data struct { - TestComputed types.String `tfsdk:"test_computed"` - TestRequired types.String `tfsdk:"test_required"` - } + ReadMethod: func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + var data struct { + TestComputed types.String `tfsdk:"test_computed"` + TestRequired types.String `tfsdk:"test_required"` + } - resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &data)...) + resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &data)...) - if data.TestRequired.Value != "test-currentstate-value" { - resp.Diagnostics.AddError("unexpected req.ProviderMeta value: %s", data.TestRequired.Value) - } - }, + if data.TestRequired.Value != "test-currentstate-value" { + resp.Diagnostics.AddError("unexpected req.ProviderMeta value: %s", data.TestRequired.Value) + } }, } }, @@ -190,27 +185,25 @@ func TestServerReadResource(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{}, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - ReadMethod: func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { - expected := `{"pKeyOne": {"k0": "zero", "k1": 1}}` - got, diags := req.Private.GetKey(ctx, "providerKey") + ReadMethod: func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + expected := `{"pKeyOne": {"k0": "zero", "k1": 1}}` + got, diags := req.Private.GetKey(ctx, "providerKey") - resp.Diagnostics.Append(diags...) + resp.Diagnostics.Append(diags...) - if string(got) != expected { - resp.Diagnostics.AddError( - "Unexpected req.Private Value", - fmt.Sprintf("expected %q, got %q", expected, got), - ) - } - }, + if string(got) != expected { + resp.Diagnostics.AddError( + "Unexpected req.Private Value", + fmt.Sprintf("expected %q, got %q", expected, got), + ) + } }, } }, @@ -242,18 +235,16 @@ func TestServerReadResource(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - ReadMethod: func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { - resp.Diagnostics.AddWarning("warning summary", "warning detail") - resp.Diagnostics.AddError("error summary", "error detail") - }, + ReadMethod: func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + resp.Diagnostics.AddWarning("warning summary", "warning detail") + resp.Diagnostics.AddError("error summary", "error detail") }, } }, @@ -289,26 +280,24 @@ func TestServerReadResource(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - ReadMethod: func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { - var data struct { - TestComputed types.String `tfsdk:"test_computed"` - TestRequired types.String `tfsdk:"test_required"` - } + ReadMethod: func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + var data struct { + TestComputed types.String `tfsdk:"test_computed"` + TestRequired types.String `tfsdk:"test_required"` + } - resp.Diagnostics.Append(req.State.Get(ctx, &data)...) + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) - data.TestComputed = types.String{Value: "test-newstate-value"} + data.TestComputed = types.String{Value: "test-newstate-value"} - resp.Diagnostics.Append(resp.State.Set(ctx, data)...) - }, + resp.Diagnostics.Append(resp.State.Set(ctx, data)...) }, } }, @@ -332,17 +321,15 @@ func TestServerReadResource(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - ReadMethod: func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { - resp.State.RemoveResource(ctx) - }, + ReadMethod: func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + resp.State.RemoveResource(ctx) }, } }, @@ -366,19 +353,17 @@ func TestServerReadResource(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{}, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - ReadMethod: func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { - diags := resp.Private.SetKey(ctx, "providerKey", []byte(`{"key": "value"}`)) + ReadMethod: func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + diags := resp.Private.SetKey(ctx, "providerKey", []byte(`{"key": "value"}`)) - resp.Diagnostics.Append(diags...) - }, + resp.Diagnostics.Append(diags...) }, } }, diff --git a/internal/proto5server/server_upgraderesourcestate_test.go b/internal/proto5server/server_upgraderesourcestate_test.go index ea61a4ba0..ec46b94e5 100644 --- a/internal/proto5server/server_upgraderesourcestate_test.go +++ b/internal/proto5server/server_upgraderesourcestate_test.go @@ -60,14 +60,15 @@ func TestServerUpgradeResourceState(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadataAndUpgradeState{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return schema, nil + return &testprovider.ResourceWithUpgradeState{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return schema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" - }, - Resource: &testprovider.Resource{}, UpgradeStateMethod: func(ctx context.Context) map[int64]resource.StateUpgrader { return map[int64]resource.StateUpgrader{ 0: { @@ -160,14 +161,15 @@ func TestServerUpgradeResourceState(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadataAndUpgradeState{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return schema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithUpgradeState{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return schema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, - Resource: &testprovider.Resource{}, UpgradeStateMethod: func(ctx context.Context) map[int64]resource.StateUpgrader { return map[int64]resource.StateUpgrader{ 0: { @@ -215,14 +217,15 @@ func TestServerUpgradeResourceState(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadataAndUpgradeState{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return schema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithUpgradeState{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return schema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, - Resource: &testprovider.Resource{}, UpgradeStateMethod: func(ctx context.Context) map[int64]resource.StateUpgrader { return map[int64]resource.StateUpgrader{ 0: { diff --git a/internal/proto5server/server_validatedatasourceconfig_test.go b/internal/proto5server/server_validatedatasourceconfig_test.go index e83d953af..976a79fe7 100644 --- a/internal/proto5server/server_validatedatasourceconfig_test.go +++ b/internal/proto5server/server_validatedatasourceconfig_test.go @@ -56,14 +56,13 @@ func TestServerValidateDataSourceConfig(t *testing.T) { DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { return []func() datasource.DataSource{ func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchemaAndMetadata{ + return &testprovider.DataSource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{}, nil }, MetadataMethod: func(_ context.Context, _ datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = "test_data_source" }, - DataSource: &testprovider.DataSource{}, } }, } @@ -83,14 +82,13 @@ func TestServerValidateDataSourceConfig(t *testing.T) { DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { return []func() datasource.DataSource{ func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchemaAndMetadata{ + return &testprovider.DataSource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = "test_data_source" }, - DataSource: &testprovider.DataSource{}, } }, } @@ -111,14 +109,15 @@ func TestServerValidateDataSourceConfig(t *testing.T) { DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { return []func() datasource.DataSource{ func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchemaAndMetadataAndValidateConfig{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ datasource.MetadataRequest, resp *datasource.MetadataResponse) { - resp.TypeName = "test_data_source" + return &testprovider.DataSourceWithValidateConfig{ + DataSource: &testprovider.DataSource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = "test_data_source" + }, }, - DataSource: &testprovider.DataSource{}, ValidateConfigMethod: func(ctx context.Context, req datasource.ValidateConfigRequest, resp *datasource.ValidateConfigResponse) { resp.Diagnostics.AddWarning("warning summary", "warning detail") resp.Diagnostics.AddError("error summary", "error detail") diff --git a/internal/proto5server/server_validateresourcetypeconfig_test.go b/internal/proto5server/server_validateresourcetypeconfig_test.go index f11059271..2a3a93f14 100644 --- a/internal/proto5server/server_validateresourcetypeconfig_test.go +++ b/internal/proto5server/server_validateresourcetypeconfig_test.go @@ -56,14 +56,13 @@ func TestServerValidateResourceTypeConfig(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{}, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{}, } }, } @@ -83,14 +82,13 @@ func TestServerValidateResourceTypeConfig(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{}, } }, } @@ -111,14 +109,15 @@ func TestServerValidateResourceTypeConfig(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadataAndValidateConfig{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithValidateConfig{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, - Resource: &testprovider.Resource{}, ValidateConfigMethod: func(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) { resp.Diagnostics.AddWarning("warning summary", "warning detail") resp.Diagnostics.AddError("error summary", "error detail") diff --git a/internal/proto6server/server_applyresourcechange_test.go b/internal/proto6server/server_applyresourcechange_test.go index 51389409b..d99bb524a 100644 --- a/internal/proto6server/server_applyresourcechange_test.go +++ b/internal/proto6server/server_applyresourcechange_test.go @@ -84,32 +84,30 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { - var data testSchemaData + CreateMethod: func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var data testSchemaData - resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) - if data.TestRequired.Value != "test-config-value" { - resp.Diagnostics.AddError("Unexpected req.Config Value", "Got: "+data.TestRequired.Value) - } + if data.TestRequired.Value != "test-config-value" { + resp.Diagnostics.AddError("Unexpected req.Config Value", "Got: "+data.TestRequired.Value) + } - // Prevent missing resource state error diagnostic - resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Delete") - }, - UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Update") - }, + // Prevent missing resource state error diagnostic + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Delete") + }, + UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Update") }, } }, @@ -144,32 +142,30 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { - var data testSchemaData + CreateMethod: func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var data testSchemaData - resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) - if data.TestComputed.Value != "test-plannedstate-value" { - resp.Diagnostics.AddError("Unexpected req.Plan Value", "Got: "+data.TestComputed.Value) - } + if data.TestComputed.Value != "test-plannedstate-value" { + resp.Diagnostics.AddError("Unexpected req.Plan Value", "Got: "+data.TestComputed.Value) + } - // Prevent missing resource state error diagnostic - resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Delete") - }, - UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Update") - }, + // Prevent missing resource state error diagnostic + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Delete") + }, + UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Update") }, } }, @@ -205,35 +201,33 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { - var metadata testProviderMetaData + CreateMethod: func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var metadata testProviderMetaData - resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &metadata)...) + resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &metadata)...) - if metadata.TestProviderMetaAttribute.Value != "test-provider-meta-value" { - resp.Diagnostics.AddError("Unexpected req.ProviderMeta Value", "Got: "+metadata.TestProviderMetaAttribute.Value) - } + if metadata.TestProviderMetaAttribute.Value != "test-provider-meta-value" { + resp.Diagnostics.AddError("Unexpected req.ProviderMeta Value", "Got: "+metadata.TestProviderMetaAttribute.Value) + } - // Prevent missing resource state error diagnostic - var data testSchemaData + // Prevent missing resource state error diagnostic + var data testSchemaData - resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) - resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Delete") - }, - UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Update") - }, + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Delete") + }, + UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Update") }, } }, @@ -273,24 +267,22 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { - resp.Diagnostics.AddWarning("warning summary", "warning detail") - resp.Diagnostics.AddError("error summary", "error detail") - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Delete") - }, - UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Update") - }, + CreateMethod: func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + resp.Diagnostics.AddWarning("warning summary", "warning detail") + resp.Diagnostics.AddError("error summary", "error detail") + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Delete") + }, + UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Update") }, } }, @@ -334,26 +326,24 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { - var data testSchemaData + CreateMethod: func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var data testSchemaData - resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) - resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Delete") - }, - UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Update") - }, + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Delete") + }, + UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Update") }, } }, @@ -388,23 +378,21 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { - // Intentionally missing resp.State.Set() - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Delete") - }, - UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Update") - }, + CreateMethod: func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + // Intentionally missing resp.State.Set() + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Delete") + }, + UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Update") }, } }, @@ -446,30 +434,28 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { - var data testSchemaData + CreateMethod: func(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var data testSchemaData - // Prevent missing resource state error diagnostic - resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) + // Prevent missing resource state error diagnostic + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) - diags := resp.Private.SetKey(ctx, "providerKey", []byte(`{"key": "value"}`)) + diags := resp.Private.SetKey(ctx, "providerKey", []byte(`{"key": "value"}`)) - resp.Diagnostics.Append(diags...) - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Delete") - }, - UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Update") - }, + resp.Diagnostics.Append(diags...) + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Delete") + }, + UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Create, Got: Update") }, } }, @@ -498,29 +484,27 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Create") - }, - DeleteMethod: func(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { - var data testSchemaData + CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Create") + }, + DeleteMethod: func(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + var data testSchemaData - resp.Diagnostics.Append(req.State.Get(ctx, &data)...) + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) - if data.TestRequired.Value != "test-priorstate-value" { - resp.Diagnostics.AddError("Unexpected req.State Value", "Got: "+data.TestRequired.Value) - } - }, - UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Update") - }, + if data.TestRequired.Value != "test-priorstate-value" { + resp.Diagnostics.AddError("Unexpected req.State Value", "Got: "+data.TestRequired.Value) + } + }, + UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Update") }, } }, @@ -549,29 +533,27 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Create") - }, - DeleteMethod: func(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { - var data testProviderMetaData + CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Create") + }, + DeleteMethod: func(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + var data testProviderMetaData - resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &data)...) + resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &data)...) - if data.TestProviderMetaAttribute.Value != "test-provider-meta-value" { - resp.Diagnostics.AddError("Unexpected req.ProviderMeta Value", "Got: "+data.TestProviderMetaAttribute.Value) - } - }, - UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Update") - }, + if data.TestProviderMetaAttribute.Value != "test-provider-meta-value" { + resp.Diagnostics.AddError("Unexpected req.ProviderMeta Value", "Got: "+data.TestProviderMetaAttribute.Value) + } + }, + UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Update") }, } }, @@ -604,33 +586,31 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Create") - }, - DeleteMethod: func(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { - expected := `{"key": "value"}` - got, diags := req.Private.GetKey(ctx, "providerKey") + CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Create") + }, + DeleteMethod: func(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + expected := `{"key": "value"}` + got, diags := req.Private.GetKey(ctx, "providerKey") - resp.Diagnostics.Append(diags...) + resp.Diagnostics.Append(diags...) - if string(got) != expected { - resp.Diagnostics.AddError( - "Unexpected req.Private Value", - fmt.Sprintf("expected %q, got %q", expected, got), - ) - } - }, - UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Update") - }, + if string(got) != expected { + resp.Diagnostics.AddError( + "Unexpected req.Private Value", + fmt.Sprintf("expected %q, got %q", expected, got), + ) + } + }, + UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Update") }, } }, @@ -660,24 +640,22 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Create") - }, - DeleteMethod: func(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddWarning("warning summary", "warning detail") - resp.Diagnostics.AddError("error summary", "error detail") - }, - UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Update") - }, + CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Create") + }, + DeleteMethod: func(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddWarning("warning summary", "warning detail") + resp.Diagnostics.AddError("error summary", "error detail") + }, + UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Update") }, } }, @@ -720,23 +698,21 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Create") - }, - DeleteMethod: func(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { - // Intentionally empty, should call resp.State.RemoveResource() automatically. - }, - UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Update") - }, + CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Create") + }, + DeleteMethod: func(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + // Intentionally empty, should call resp.State.RemoveResource() automatically. + }, + UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Delete, Got: Update") }, } }, @@ -764,30 +740,28 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") + CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") - }, - UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { - var data testSchemaData + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") + }, + UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var data testSchemaData - resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) - if data.TestRequired.Value != "test-new-value" { - resp.Diagnostics.AddError("Unexpected req.Config Value", "Got: "+data.TestRequired.Value) - } - }, + if data.TestRequired.Value != "test-new-value" { + resp.Diagnostics.AddError("Unexpected req.Config Value", "Got: "+data.TestRequired.Value) + } }, } }, @@ -826,30 +800,28 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") + CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") - }, - UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { - var data testSchemaData + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") + }, + UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var data testSchemaData - resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) - if data.TestComputed.Value != "test-plannedstate-value" { - resp.Diagnostics.AddError("Unexpected req.Plan Value", "Got: "+data.TestComputed.Value) - } - }, + if data.TestComputed.Value != "test-plannedstate-value" { + resp.Diagnostics.AddError("Unexpected req.Plan Value", "Got: "+data.TestComputed.Value) + } }, } }, @@ -888,29 +860,27 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") - }, - UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { - var data testSchemaData + CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") + }, + UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var data testSchemaData - resp.Diagnostics.Append(req.State.Get(ctx, &data)...) + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) - if data.TestRequired.Value != "test-old-value" { - resp.Diagnostics.AddError("Unexpected req.State Value", "Got: "+data.TestRequired.Value) - } - }, + if data.TestRequired.Value != "test-old-value" { + resp.Diagnostics.AddError("Unexpected req.State Value", "Got: "+data.TestRequired.Value) + } }, } }, @@ -950,29 +920,27 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") - }, - UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { - var data testProviderMetaData + CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") + }, + UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var data testProviderMetaData - resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &data)...) + resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &data)...) - if data.TestProviderMetaAttribute.Value != "test-provider-meta-value" { - resp.Diagnostics.AddError("Unexpected req.ProviderMeta Value", "Got: "+data.TestProviderMetaAttribute.Value) - } - }, + if data.TestProviderMetaAttribute.Value != "test-provider-meta-value" { + resp.Diagnostics.AddError("Unexpected req.ProviderMeta Value", "Got: "+data.TestProviderMetaAttribute.Value) + } }, } }, @@ -1017,33 +985,31 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") - }, - UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { - expected := `{"providerKey": "provider value"}` - got, diags := req.Private.GetKey(ctx, "providerKey") + CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") + }, + UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + expected := `{"providerKey": "provider value"}` + got, diags := req.Private.GetKey(ctx, "providerKey") - resp.Diagnostics.Append(diags...) + resp.Diagnostics.Append(diags...) - if string(got) != expected { - resp.Diagnostics.AddError( - "Unexpected req.Private Value", - fmt.Sprintf("expected %q, got %q", expected, got), - ) - } - }, + if string(got) != expected { + resp.Diagnostics.AddError( + "Unexpected req.Private Value", + fmt.Sprintf("expected %q, got %q", expected, got), + ) + } }, } }, @@ -1091,24 +1057,22 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") - }, - UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { - resp.Diagnostics.AddWarning("warning summary", "warning detail") - resp.Diagnostics.AddError("error summary", "error detail") - }, + CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") + }, + UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + resp.Diagnostics.AddWarning("warning summary", "warning detail") + resp.Diagnostics.AddError("error summary", "error detail") }, } }, @@ -1158,26 +1122,24 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") - }, - UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { - var data testSchemaData + CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") + }, + UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var data testSchemaData - resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) - resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) - }, + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) }, } }, @@ -1215,23 +1177,21 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") - }, - UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { - resp.State.RemoveResource(ctx) - }, + CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") + }, + UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + resp.State.RemoveResource(ctx) }, } }, @@ -1274,25 +1234,23 @@ func TestServerApplyResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") - }, - DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { - resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") - }, - UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { - diags := resp.Private.SetKey(ctx, "providerKey", []byte(`{"providerKey": "provider value"}`)) + CreateMethod: func(_ context.Context, _ resource.CreateRequest, resp *resource.CreateResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Create") + }, + DeleteMethod: func(_ context.Context, _ resource.DeleteRequest, resp *resource.DeleteResponse) { + resp.Diagnostics.AddError("Unexpected Method Call", "Expected: Update, Got: Delete") + }, + UpdateMethod: func(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + diags := resp.Private.SetKey(ctx, "providerKey", []byte(`{"providerKey": "provider value"}`)) - resp.Diagnostics.Append(diags...) - }, + resp.Diagnostics.Append(diags...) }, } }, diff --git a/internal/proto6server/server_getproviderschema_test.go b/internal/proto6server/server_getproviderschema_test.go index bc9816fe6..69e62ab15 100644 --- a/internal/proto6server/server_getproviderschema_test.go +++ b/internal/proto6server/server_getproviderschema_test.go @@ -11,7 +11,6 @@ import ( "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/resource" "github.com/hashicorp/terraform-plugin-framework/tfsdk" "github.com/hashicorp/terraform-plugin-framework/types" @@ -29,14 +28,14 @@ func TestServerGetProviderSchema(t *testing.T) { expectedError error expectedResponse *tfprotov6.GetProviderSchemaResponse }{ - "datasourceschemas-DataSources": { + "datasourceschemas": { server: &Server{ FrameworkServer: fwserver.Server{ Provider: &testprovider.Provider{ DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { return []func() datasource.DataSource{ func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchemaAndMetadata{ + return &testprovider.DataSource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{ Attributes: map[string]tfsdk.Attribute{ @@ -53,7 +52,7 @@ func TestServerGetProviderSchema(t *testing.T) { } }, func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchemaAndMetadata{ + return &testprovider.DataSource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{ Attributes: map[string]tfsdk.Attribute{ @@ -109,14 +108,14 @@ func TestServerGetProviderSchema(t *testing.T) { }, }, }, - "datasourceschemas-DataSources-duplicate-type-name": { + "datasourceschemas-duplicate-type-name": { server: &Server{ FrameworkServer: fwserver.Server{ Provider: &testprovider.Provider{ DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { return []func() datasource.DataSource{ func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchemaAndMetadata{ + return &testprovider.DataSource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{ Attributes: map[string]tfsdk.Attribute{ @@ -133,7 +132,7 @@ func TestServerGetProviderSchema(t *testing.T) { } }, func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchemaAndMetadata{ + return &testprovider.DataSource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{ Attributes: map[string]tfsdk.Attribute{ @@ -175,14 +174,14 @@ func TestServerGetProviderSchema(t *testing.T) { }, }, }, - "datasourceschemas-DataSources-empty-type-name": { + "datasourceschemas-empty-type-name": { server: &Server{ FrameworkServer: fwserver.Server{ Provider: &testprovider.Provider{ DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { return []func() datasource.DataSource{ func() datasource.DataSource { - return &testprovider.DataSourceWithMetadata{ + return &testprovider.DataSource{ MetadataMethod: func(_ context.Context, _ datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = "" }, @@ -200,7 +199,7 @@ func TestServerGetProviderSchema(t *testing.T) { { Severity: tfprotov6.DiagnosticSeverityError, Summary: "Data Source Type Name Missing", - Detail: "The *testprovider.DataSourceWithMetadata DataSource returned an empty string from the Metadata method. " + + Detail: "The *testprovider.DataSource DataSource returned an empty string from the Metadata method. " + "This is always an issue with the provider and should be reported to the provider developers.", }, }, @@ -213,160 +212,6 @@ func TestServerGetProviderSchema(t *testing.T) { }, }, }, - "datasourceschemas-DataSources-missing-schema": { - server: &Server{ - FrameworkServer: fwserver.Server{ - Provider: &testprovider.Provider{ - DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { - return []func() datasource.DataSource{ - func() datasource.DataSource { - return &testprovider.DataSourceWithMetadata{ - MetadataMethod: func(_ context.Context, _ datasource.MetadataRequest, resp *datasource.MetadataResponse) { - resp.TypeName = "test_data_source" - }, - } - }, - } - }, - }, - }, - }, - request: &tfprotov6.GetProviderSchemaRequest{}, - expectedResponse: &tfprotov6.GetProviderSchemaResponse{ - DataSourceSchemas: map[string]*tfprotov6.Schema{}, - Diagnostics: []*tfprotov6.Diagnostic{ - { - Severity: tfprotov6.DiagnosticSeverityError, - Summary: "Data Source Schema Missing", - Detail: "The *testprovider.DataSourceWithMetadata DataSource in the provider is missing the GetSchema method. " + - "This is always an issue with the provider and should be reported to the provider developers.", - }, - }, - Provider: &tfprotov6.Schema{ - Block: &tfprotov6.SchemaBlock{}, - }, - ResourceSchemas: map[string]*tfprotov6.Schema{}, - ServerCapabilities: &tfprotov6.ServerCapabilities{ - PlanDestroy: true, - }, - }, - }, - "datasourceschemas-DataSources-missing-type-name": { - server: &Server{ - FrameworkServer: fwserver.Server{ - Provider: &testprovider.Provider{ - DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { - return []func() datasource.DataSource{ - func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchema{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return tfsdk.Schema{ - Attributes: map[string]tfsdk.Attribute{ - "test1": { - Required: true, - Type: types.StringType, - }, - }, - }, nil - }, - } - }, - } - }, - }, - }, - }, - request: &tfprotov6.GetProviderSchemaRequest{}, - expectedResponse: &tfprotov6.GetProviderSchemaResponse{ - DataSourceSchemas: map[string]*tfprotov6.Schema{}, - Diagnostics: []*tfprotov6.Diagnostic{ - { - Severity: tfprotov6.DiagnosticSeverityError, - Summary: "Data Source Type Name Missing", - Detail: "The *testprovider.DataSourceWithGetSchema DataSource in the provider DataSources method results is missing the Metadata method. " + - "This is always an issue with the provider and should be reported to the provider developers.", - }, - }, - Provider: &tfprotov6.Schema{ - Block: &tfprotov6.SchemaBlock{}, - }, - ResourceSchemas: map[string]*tfprotov6.Schema{}, - ServerCapabilities: &tfprotov6.ServerCapabilities{ - PlanDestroy: true, - }, - }, - }, - "datasourceschemas-GetDataSources": { - server: &Server{ - FrameworkServer: fwserver.Server{ - Provider: &testprovider.ProviderWithGetDataSources{ - //nolint:staticcheck // Internal implementation - GetDataSourcesMethod: func(_ context.Context) (map[string]provider.DataSourceType, diag.Diagnostics) { - return map[string]provider.DataSourceType{ - "test_data_source1": &testprovider.DataSourceType{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return tfsdk.Schema{ - Attributes: map[string]tfsdk.Attribute{ - "test1": { - Required: true, - Type: types.StringType, - }, - }, - }, nil - }, - }, - "test_data_source2": &testprovider.DataSourceType{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return tfsdk.Schema{ - Attributes: map[string]tfsdk.Attribute{ - "test2": { - Required: true, - Type: types.StringType, - }, - }, - }, nil - }, - }, - }, nil - }, - }, - }, - }, - request: &tfprotov6.GetProviderSchemaRequest{}, - expectedResponse: &tfprotov6.GetProviderSchemaResponse{ - DataSourceSchemas: map[string]*tfprotov6.Schema{ - "test_data_source1": { - Block: &tfprotov6.SchemaBlock{ - Attributes: []*tfprotov6.SchemaAttribute{ - { - Name: "test1", - Required: true, - Type: tftypes.String, - }, - }, - }, - }, - "test_data_source2": { - Block: &tfprotov6.SchemaBlock{ - Attributes: []*tfprotov6.SchemaAttribute{ - { - Name: "test2", - Required: true, - Type: tftypes.String, - }, - }, - }, - }, - }, - Provider: &tfprotov6.Schema{ - Block: &tfprotov6.SchemaBlock{}, - }, - ResourceSchemas: map[string]*tfprotov6.Schema{}, - ServerCapabilities: &tfprotov6.ServerCapabilities{ - PlanDestroy: true, - }, - }, - }, "provider": { server: &Server{ FrameworkServer: fwserver.Server{ @@ -445,14 +290,14 @@ func TestServerGetProviderSchema(t *testing.T) { }, }, }, - "resourceschemas-Resources": { + "resourceschemas": { server: &Server{ FrameworkServer: fwserver.Server{ Provider: &testprovider.Provider{ ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{ Attributes: map[string]tfsdk.Attribute{ @@ -469,7 +314,7 @@ func TestServerGetProviderSchema(t *testing.T) { } }, func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{ Attributes: map[string]tfsdk.Attribute{ @@ -525,14 +370,14 @@ func TestServerGetProviderSchema(t *testing.T) { }, }, }, - "resourceschemas-Resources-duplicate-type-name": { + "resourceschemas-duplicate-type-name": { server: &Server{ FrameworkServer: fwserver.Server{ Provider: &testprovider.Provider{ ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{ Attributes: map[string]tfsdk.Attribute{ @@ -549,7 +394,7 @@ func TestServerGetProviderSchema(t *testing.T) { } }, func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{ Attributes: map[string]tfsdk.Attribute{ @@ -591,14 +436,14 @@ func TestServerGetProviderSchema(t *testing.T) { }, }, }, - "resourceschemas-Resources-empty-type-name": { + "resourceschemas-empty-type-name": { server: &Server{ FrameworkServer: fwserver.Server{ Provider: &testprovider.Provider{ ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithMetadata{ + return &testprovider.Resource{ MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "" }, @@ -616,90 +461,7 @@ func TestServerGetProviderSchema(t *testing.T) { { Severity: tfprotov6.DiagnosticSeverityError, Summary: "Resource Type Name Missing", - Detail: "The *testprovider.ResourceWithMetadata Resource returned an empty string from the Metadata method. " + - "This is always an issue with the provider and should be reported to the provider developers.", - }, - }, - Provider: &tfprotov6.Schema{ - Block: &tfprotov6.SchemaBlock{}, - }, - ResourceSchemas: map[string]*tfprotov6.Schema{}, - ServerCapabilities: &tfprotov6.ServerCapabilities{ - PlanDestroy: true, - }, - }, - }, - "resourceschemas-Resources-missing-schema": { - server: &Server{ - FrameworkServer: fwserver.Server{ - Provider: &testprovider.Provider{ - ResourcesMethod: func(_ context.Context) []func() resource.Resource { - return []func() resource.Resource{ - func() resource.Resource { - return &testprovider.ResourceWithMetadata{ - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" - }, - } - }, - } - }, - }, - }, - }, - request: &tfprotov6.GetProviderSchemaRequest{}, - expectedResponse: &tfprotov6.GetProviderSchemaResponse{ - DataSourceSchemas: map[string]*tfprotov6.Schema{}, - Diagnostics: []*tfprotov6.Diagnostic{ - { - Severity: tfprotov6.DiagnosticSeverityError, - Summary: "Resource Schema Missing", - Detail: "The *testprovider.ResourceWithMetadata Resource in the provider is missing the GetSchema method. " + - "This is always an issue with the provider and should be reported to the provider developers.", - }, - }, - Provider: &tfprotov6.Schema{ - Block: &tfprotov6.SchemaBlock{}, - }, - ResourceSchemas: map[string]*tfprotov6.Schema{}, - ServerCapabilities: &tfprotov6.ServerCapabilities{ - PlanDestroy: true, - }, - }, - }, - "resourceschemas-Resources-missing-type-name": { - server: &Server{ - FrameworkServer: fwserver.Server{ - Provider: &testprovider.Provider{ - ResourcesMethod: func(_ context.Context) []func() resource.Resource { - return []func() resource.Resource{ - func() resource.Resource { - return &testprovider.ResourceWithGetSchema{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return tfsdk.Schema{ - Attributes: map[string]tfsdk.Attribute{ - "test1": { - Required: true, - Type: types.StringType, - }, - }, - }, nil - }, - } - }, - } - }, - }, - }, - }, - request: &tfprotov6.GetProviderSchemaRequest{}, - expectedResponse: &tfprotov6.GetProviderSchemaResponse{ - DataSourceSchemas: map[string]*tfprotov6.Schema{}, - Diagnostics: []*tfprotov6.Diagnostic{ - { - Severity: tfprotov6.DiagnosticSeverityError, - Summary: "Resource Type Name Missing", - Detail: "The *testprovider.ResourceWithGetSchema Resource in the provider Resources method results is missing the Metadata method. " + + Detail: "The *testprovider.Resource Resource returned an empty string from the Metadata method. " + "This is always an issue with the provider and should be reported to the provider developers.", }, }, @@ -712,77 +474,6 @@ func TestServerGetProviderSchema(t *testing.T) { }, }, }, - "resourceschemas-GetResources": { - server: &Server{ - FrameworkServer: fwserver.Server{ - Provider: &testprovider.ProviderWithGetResources{ - //nolint:staticcheck // Internal implementation - GetResourcesMethod: func(_ context.Context) (map[string]provider.ResourceType, diag.Diagnostics) { - return map[string]provider.ResourceType{ - "test_resource1": &testprovider.ResourceType{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return tfsdk.Schema{ - Attributes: map[string]tfsdk.Attribute{ - "test1": { - Required: true, - Type: types.StringType, - }, - }, - }, nil - }, - }, - "test_resource2": &testprovider.ResourceType{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return tfsdk.Schema{ - Attributes: map[string]tfsdk.Attribute{ - "test2": { - Required: true, - Type: types.StringType, - }, - }, - }, nil - }, - }, - }, nil - }, - }, - }, - }, - request: &tfprotov6.GetProviderSchemaRequest{}, - expectedResponse: &tfprotov6.GetProviderSchemaResponse{ - DataSourceSchemas: map[string]*tfprotov6.Schema{}, - Provider: &tfprotov6.Schema{ - Block: &tfprotov6.SchemaBlock{}, - }, - ResourceSchemas: map[string]*tfprotov6.Schema{ - "test_resource1": { - Block: &tfprotov6.SchemaBlock{ - Attributes: []*tfprotov6.SchemaAttribute{ - { - Name: "test1", - Required: true, - Type: tftypes.String, - }, - }, - }, - }, - "test_resource2": { - Block: &tfprotov6.SchemaBlock{ - Attributes: []*tfprotov6.SchemaAttribute{ - { - Name: "test2", - Required: true, - Type: tftypes.String, - }, - }, - }, - }, - }, - ServerCapabilities: &tfprotov6.ServerCapabilities{ - PlanDestroy: true, - }, - }, - }, } for name, testCase := range testCases { @@ -866,11 +557,6 @@ func TestServerGetProviderSchema_logging(t *testing.T) { "@message": "Called provider defined Provider Resources", "@module": "sdk.framework", }, - { - "@level": "trace", - "@message": "Checking ResourceTypes lock", - "@module": "sdk.framework", - }, { "@level": "trace", "@message": "Checking DataSourceSchemas lock", @@ -891,11 +577,6 @@ func TestServerGetProviderSchema_logging(t *testing.T) { "@message": "Called provider defined Provider DataSources", "@module": "sdk.framework", }, - { - "@level": "trace", - "@message": "Checking DataSourceTypes lock", - "@module": "sdk.framework", - }, } if diff := cmp.Diff(entries, expectedEntries); diff != "" { diff --git a/internal/proto6server/server_importresourcestate_test.go b/internal/proto6server/server_importresourcestate_test.go index cb8807f37..4c6dea05e 100644 --- a/internal/proto6server/server_importresourcestate_test.go +++ b/internal/proto6server/server_importresourcestate_test.go @@ -65,14 +65,15 @@ func TestServerImportResourceState(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndImportStateAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil + return &testprovider.ResourceWithImportState{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" - }, - Resource: &testprovider.Resource{}, ImportStateMethod: func(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { if req.ID != "test-id" { resp.Diagnostics.AddError("unexpected req.ID value: %s", req.ID) @@ -107,14 +108,15 @@ func TestServerImportResourceState(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndImportStateAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithImportState{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, - Resource: &testprovider.Resource{}, ImportStateMethod: func(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { resp.Diagnostics.AddWarning("warning summary", "warning detail") resp.Diagnostics.AddError("error summary", "error detail") @@ -152,14 +154,15 @@ func TestServerImportResourceState(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndImportStateAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil + return &testprovider.ResourceWithImportState{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" - }, - Resource: &testprovider.Resource{}, ImportStateMethod: func(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) }, @@ -190,14 +193,15 @@ func TestServerImportResourceState(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndImportStateAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithImportState{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, - Resource: &testprovider.Resource{}, ImportStateMethod: func(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { diags := resp.Private.SetKey(ctx, "providerKey", []byte(`{"key": "value"}`)) diff --git a/internal/proto6server/server_planresourcechange_test.go b/internal/proto6server/server_planresourcechange_test.go index 0ff05ea36..36ba4d5e5 100644 --- a/internal/proto6server/server_planresourcechange_test.go +++ b/internal/proto6server/server_planresourcechange_test.go @@ -82,12 +82,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { var data testSchemaData @@ -131,12 +133,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { var data testSchemaData @@ -181,12 +185,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { var data testProviderMetaData @@ -235,12 +241,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { resp.Diagnostics.AddWarning("warning summary", "warning detail") @@ -291,12 +299,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { var data testSchemaData @@ -340,12 +350,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { // This is a strange thing to signal on creation, @@ -394,12 +406,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { var data testSchemaData @@ -437,12 +451,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { var data testProviderMetaData @@ -484,12 +500,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { resp.Diagnostics.AddWarning("warning summary", "warning detail") @@ -533,12 +551,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { // This is invalid logic to run during deletion. @@ -582,12 +602,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { // This is a strange thing to signal on destroy, @@ -629,12 +651,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { var data testSchemaData @@ -681,12 +705,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { var data testSchemaData @@ -733,12 +759,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { var data testSchemaData @@ -786,12 +814,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { var data testProviderMetaData @@ -843,12 +873,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { resp.Diagnostics.AddWarning("warning summary", "warning detail") @@ -902,12 +934,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { var data testSchemaData @@ -954,12 +988,14 @@ func TestServerPlanResourceChange(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndModifyPlanAndMetadata{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithModifyPlan{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { resp.RequiresReplace = path.Paths{ diff --git a/internal/proto6server/server_readdatasource_test.go b/internal/proto6server/server_readdatasource_test.go index 1743a42b0..89b0cc07f 100644 --- a/internal/proto6server/server_readdatasource_test.go +++ b/internal/proto6server/server_readdatasource_test.go @@ -63,14 +63,13 @@ func TestServerReadDataSource(t *testing.T) { DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { return []func() datasource.DataSource{ func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchemaAndMetadata{ + return &testprovider.DataSource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{}, nil }, MetadataMethod: func(_ context.Context, _ datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = "test_data_source" }, - DataSource: &testprovider.DataSource{}, } }, } @@ -93,26 +92,24 @@ func TestServerReadDataSource(t *testing.T) { DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { return []func() datasource.DataSource{ func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchemaAndMetadata{ + return &testprovider.DataSource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = "test_data_source" }, - DataSource: &testprovider.DataSource{ - ReadMethod: func(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { - var config struct { - TestComputed types.String `tfsdk:"test_computed"` - TestRequired types.String `tfsdk:"test_required"` - } + ReadMethod: func(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var config struct { + TestComputed types.String `tfsdk:"test_computed"` + TestRequired types.String `tfsdk:"test_required"` + } - resp.Diagnostics.Append(req.Config.Get(ctx, &config)...) + resp.Diagnostics.Append(req.Config.Get(ctx, &config)...) - if config.TestRequired.Value != "test-config-value" { - resp.Diagnostics.AddError("unexpected req.Config value: %s", config.TestRequired.Value) - } - }, + if config.TestRequired.Value != "test-config-value" { + resp.Diagnostics.AddError("unexpected req.Config value: %s", config.TestRequired.Value) + } }, } }, @@ -137,26 +134,24 @@ func TestServerReadDataSource(t *testing.T) { DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { return []func() datasource.DataSource{ func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchemaAndMetadata{ + return &testprovider.DataSource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{}, nil }, MetadataMethod: func(_ context.Context, _ datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = "test_data_source" }, - DataSource: &testprovider.DataSource{ - ReadMethod: func(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { - var config struct { - TestComputed types.String `tfsdk:"test_computed"` - TestRequired types.String `tfsdk:"test_required"` - } + ReadMethod: func(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var config struct { + TestComputed types.String `tfsdk:"test_computed"` + TestRequired types.String `tfsdk:"test_required"` + } - resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &config)...) + resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &config)...) - if config.TestRequired.Value != "test-config-value" { - resp.Diagnostics.AddError("unexpected req.ProviderMeta value: %s", config.TestRequired.Value) - } - }, + if config.TestRequired.Value != "test-config-value" { + resp.Diagnostics.AddError("unexpected req.ProviderMeta value: %s", config.TestRequired.Value) + } }, } }, @@ -185,18 +180,16 @@ func TestServerReadDataSource(t *testing.T) { DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { return []func() datasource.DataSource{ func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchemaAndMetadata{ + return &testprovider.DataSource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = "test_data_source" }, - DataSource: &testprovider.DataSource{ - ReadMethod: func(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { - resp.Diagnostics.AddWarning("warning summary", "warning detail") - resp.Diagnostics.AddError("error summary", "error detail") - }, + ReadMethod: func(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + resp.Diagnostics.AddWarning("warning summary", "warning detail") + resp.Diagnostics.AddError("error summary", "error detail") }, } }, @@ -232,26 +225,24 @@ func TestServerReadDataSource(t *testing.T) { DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { return []func() datasource.DataSource{ func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchemaAndMetadata{ + return &testprovider.DataSource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = "test_data_source" }, - DataSource: &testprovider.DataSource{ - ReadMethod: func(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { - var data struct { - TestComputed types.String `tfsdk:"test_computed"` - TestRequired types.String `tfsdk:"test_required"` - } + ReadMethod: func(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var data struct { + TestComputed types.String `tfsdk:"test_computed"` + TestRequired types.String `tfsdk:"test_required"` + } - resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) - data.TestComputed = types.String{Value: "test-state-value"} + data.TestComputed = types.String{Value: "test-state-value"} - resp.Diagnostics.Append(resp.State.Set(ctx, data)...) - }, + resp.Diagnostics.Append(resp.State.Set(ctx, data)...) }, } }, diff --git a/internal/proto6server/server_readresource_test.go b/internal/proto6server/server_readresource_test.go index d3f611f45..0ff4b63b8 100644 --- a/internal/proto6server/server_readresource_test.go +++ b/internal/proto6server/server_readresource_test.go @@ -68,14 +68,13 @@ func TestServerReadResource(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{}, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{}, } }, } @@ -98,26 +97,24 @@ func TestServerReadResource(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - ReadMethod: func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { - var data struct { - TestComputed types.String `tfsdk:"test_computed"` - TestRequired types.String `tfsdk:"test_required"` - } + ReadMethod: func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + var data struct { + TestComputed types.String `tfsdk:"test_computed"` + TestRequired types.String `tfsdk:"test_required"` + } - resp.Diagnostics.Append(req.State.Get(ctx, &data)...) + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) - if data.TestRequired.Value != "test-currentstate-value" { - resp.Diagnostics.AddError("unexpected req.State value: %s", data.TestRequired.Value) - } - }, + if data.TestRequired.Value != "test-currentstate-value" { + resp.Diagnostics.AddError("unexpected req.State value: %s", data.TestRequired.Value) + } }, } }, @@ -142,26 +139,24 @@ func TestServerReadResource(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{}, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - ReadMethod: func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { - var data struct { - TestComputed types.String `tfsdk:"test_computed"` - TestRequired types.String `tfsdk:"test_required"` - } + ReadMethod: func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + var data struct { + TestComputed types.String `tfsdk:"test_computed"` + TestRequired types.String `tfsdk:"test_required"` + } - resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &data)...) + resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &data)...) - if data.TestRequired.Value != "test-currentstate-value" { - resp.Diagnostics.AddError("unexpected req.ProviderMeta value: %s", data.TestRequired.Value) - } - }, + if data.TestRequired.Value != "test-currentstate-value" { + resp.Diagnostics.AddError("unexpected req.ProviderMeta value: %s", data.TestRequired.Value) + } }, } }, @@ -190,27 +185,25 @@ func TestServerReadResource(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{}, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - ReadMethod: func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { - expected := `{"pKeyOne": {"k0": "zero", "k1": 1}}` - got, diags := req.Private.GetKey(ctx, "providerKey") + ReadMethod: func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + expected := `{"pKeyOne": {"k0": "zero", "k1": 1}}` + got, diags := req.Private.GetKey(ctx, "providerKey") - resp.Diagnostics.Append(diags...) + resp.Diagnostics.Append(diags...) - if string(got) != expected { - resp.Diagnostics.AddError( - "Unexpected req.Private Value", - fmt.Sprintf("expected %q, got %q", expected, got), - ) - } - }, + if string(got) != expected { + resp.Diagnostics.AddError( + "Unexpected req.Private Value", + fmt.Sprintf("expected %q, got %q", expected, got), + ) + } }, } }, @@ -242,18 +235,16 @@ func TestServerReadResource(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - ReadMethod: func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { - resp.Diagnostics.AddWarning("warning summary", "warning detail") - resp.Diagnostics.AddError("error summary", "error detail") - }, + ReadMethod: func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + resp.Diagnostics.AddWarning("warning summary", "warning detail") + resp.Diagnostics.AddError("error summary", "error detail") }, } }, @@ -289,26 +280,24 @@ func TestServerReadResource(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - ReadMethod: func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { - var data struct { - TestComputed types.String `tfsdk:"test_computed"` - TestRequired types.String `tfsdk:"test_required"` - } + ReadMethod: func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + var data struct { + TestComputed types.String `tfsdk:"test_computed"` + TestRequired types.String `tfsdk:"test_required"` + } - resp.Diagnostics.Append(req.State.Get(ctx, &data)...) + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) - data.TestComputed = types.String{Value: "test-newstate-value"} + data.TestComputed = types.String{Value: "test-newstate-value"} - resp.Diagnostics.Append(resp.State.Set(ctx, data)...) - }, + resp.Diagnostics.Append(resp.State.Set(ctx, data)...) }, } }, @@ -332,17 +321,15 @@ func TestServerReadResource(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - ReadMethod: func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { - resp.State.RemoveResource(ctx) - }, + ReadMethod: func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + resp.State.RemoveResource(ctx) }, } }, @@ -366,19 +353,17 @@ func TestServerReadResource(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{}, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{ - ReadMethod: func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { - diags := resp.Private.SetKey(ctx, "providerKey", []byte(`{"key": "value"}`)) + ReadMethod: func(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + diags := resp.Private.SetKey(ctx, "providerKey", []byte(`{"key": "value"}`)) - resp.Diagnostics.Append(diags...) - }, + resp.Diagnostics.Append(diags...) }, } }, diff --git a/internal/proto6server/server_upgraderesourcestate_test.go b/internal/proto6server/server_upgraderesourcestate_test.go index dfd595df6..832613914 100644 --- a/internal/proto6server/server_upgraderesourcestate_test.go +++ b/internal/proto6server/server_upgraderesourcestate_test.go @@ -60,14 +60,15 @@ func TestServerUpgradeResourceState(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadataAndUpgradeState{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return schema, nil + return &testprovider.ResourceWithUpgradeState{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return schema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" - }, - Resource: &testprovider.Resource{}, UpgradeStateMethod: func(ctx context.Context) map[int64]resource.StateUpgrader { return map[int64]resource.StateUpgrader{ 0: { @@ -160,14 +161,15 @@ func TestServerUpgradeResourceState(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadataAndUpgradeState{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return schema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithUpgradeState{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return schema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, - Resource: &testprovider.Resource{}, UpgradeStateMethod: func(ctx context.Context) map[int64]resource.StateUpgrader { return map[int64]resource.StateUpgrader{ 0: { @@ -215,14 +217,15 @@ func TestServerUpgradeResourceState(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadataAndUpgradeState{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return schema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithUpgradeState{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return schema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, - Resource: &testprovider.Resource{}, UpgradeStateMethod: func(ctx context.Context) map[int64]resource.StateUpgrader { return map[int64]resource.StateUpgrader{ 0: { diff --git a/internal/proto6server/server_validatedataresourceconfig_test.go b/internal/proto6server/server_validatedataresourceconfig_test.go index 43b1f151d..25df92129 100644 --- a/internal/proto6server/server_validatedataresourceconfig_test.go +++ b/internal/proto6server/server_validatedataresourceconfig_test.go @@ -56,14 +56,13 @@ func TestServerValidateDataResourceConfig(t *testing.T) { DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { return []func() datasource.DataSource{ func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchemaAndMetadata{ + return &testprovider.DataSource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{}, nil }, MetadataMethod: func(_ context.Context, _ datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = "test_data_source" }, - DataSource: &testprovider.DataSource{}, } }, } @@ -83,14 +82,13 @@ func TestServerValidateDataResourceConfig(t *testing.T) { DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { return []func() datasource.DataSource{ func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchemaAndMetadata{ + return &testprovider.DataSource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = "test_data_source" }, - DataSource: &testprovider.DataSource{}, } }, } @@ -111,14 +109,15 @@ func TestServerValidateDataResourceConfig(t *testing.T) { DataSourcesMethod: func(_ context.Context) []func() datasource.DataSource { return []func() datasource.DataSource{ func() datasource.DataSource { - return &testprovider.DataSourceWithGetSchemaAndMetadataAndValidateConfig{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ datasource.MetadataRequest, resp *datasource.MetadataResponse) { - resp.TypeName = "test_data_source" + return &testprovider.DataSourceWithValidateConfig{ + DataSource: &testprovider.DataSource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = "test_data_source" + }, }, - DataSource: &testprovider.DataSource{}, ValidateConfigMethod: func(ctx context.Context, req datasource.ValidateConfigRequest, resp *datasource.ValidateConfigResponse) { resp.Diagnostics.AddWarning("warning summary", "warning detail") resp.Diagnostics.AddError("error summary", "error detail") diff --git a/internal/proto6server/server_validateresourceconfig_test.go b/internal/proto6server/server_validateresourceconfig_test.go index 8c9fcf24b..6cd980cdd 100644 --- a/internal/proto6server/server_validateresourceconfig_test.go +++ b/internal/proto6server/server_validateresourceconfig_test.go @@ -56,14 +56,13 @@ func TestServerValidateResourceConfig(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return tfsdk.Schema{}, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{}, } }, } @@ -83,14 +82,13 @@ func TestServerValidateResourceConfig(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadata{ + return &testprovider.Resource{ GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { return testSchema, nil }, MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = "test_resource" }, - Resource: &testprovider.Resource{}, } }, } @@ -111,14 +109,15 @@ func TestServerValidateResourceConfig(t *testing.T) { ResourcesMethod: func(_ context.Context) []func() resource.Resource { return []func() resource.Resource{ func() resource.Resource { - return &testprovider.ResourceWithGetSchemaAndMetadataAndValidateConfig{ - GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return testSchema, nil - }, - MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "test_resource" + return &testprovider.ResourceWithValidateConfig{ + Resource: &testprovider.Resource{ + GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { + return testSchema, nil + }, + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = "test_resource" + }, }, - Resource: &testprovider.Resource{}, ValidateConfigMethod: func(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) { resp.Diagnostics.AddWarning("warning summary", "warning detail") resp.Diagnostics.AddError("error summary", "error detail") diff --git a/internal/testing/testprovider/datasource.go b/internal/testing/testprovider/datasource.go index e126cd442..a2519612a 100644 --- a/internal/testing/testprovider/datasource.go +++ b/internal/testing/testprovider/datasource.go @@ -4,6 +4,8 @@ import ( "context" "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/diag" + "github.com/hashicorp/terraform-plugin-framework/tfsdk" ) var _ datasource.DataSource = &DataSource{} @@ -11,7 +13,27 @@ var _ datasource.DataSource = &DataSource{} // Declarative datasource.DataSource for unit testing. type DataSource struct { // DataSource interface methods - ReadMethod func(context.Context, datasource.ReadRequest, *datasource.ReadResponse) + MetadataMethod func(context.Context, datasource.MetadataRequest, *datasource.MetadataResponse) + GetSchemaMethod func(context.Context) (tfsdk.Schema, diag.Diagnostics) + ReadMethod func(context.Context, datasource.ReadRequest, *datasource.ReadResponse) +} + +// Metadata satisfies the datasource.DataSource interface. +func (d *DataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + if d.MetadataMethod == nil { + return + } + + d.MetadataMethod(ctx, req, resp) +} + +// GetSchema satisfies the datasource.DataSource interface. +func (d *DataSource) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) { + if d.GetSchemaMethod == nil { + return tfsdk.Schema{}, nil + } + + return d.GetSchemaMethod(ctx) } // Read satisfies the datasource.DataSource interface. diff --git a/internal/testing/testprovider/datasourcetype.go b/internal/testing/testprovider/datasourcetype.go deleted file mode 100644 index c0232d0a4..000000000 --- a/internal/testing/testprovider/datasourcetype.go +++ /dev/null @@ -1,37 +0,0 @@ -package testprovider - -import ( - "context" - - "github.com/hashicorp/terraform-plugin-framework/datasource" - "github.com/hashicorp/terraform-plugin-framework/diag" - "github.com/hashicorp/terraform-plugin-framework/provider" - "github.com/hashicorp/terraform-plugin-framework/tfsdk" -) - -var _ provider.DataSourceType = &DataSourceType{} //nolint:staticcheck // Internal implementation - -// Declarative provider.DataSourceType for unit testing. -type DataSourceType struct { - // DataSourceType interface methods - GetSchemaMethod func(context.Context) (tfsdk.Schema, diag.Diagnostics) - NewDataSourceMethod func(context.Context, provider.Provider) (datasource.DataSource, diag.Diagnostics) -} - -// GetSchema satisfies the provider.DataSourceType interface. -func (t *DataSourceType) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) { - if t.GetSchemaMethod == nil { - return tfsdk.Schema{}, nil - } - - return t.GetSchemaMethod(ctx) -} - -// NewDataSource satisfies the provider.DataSourceType interface. -func (t *DataSourceType) NewDataSource(ctx context.Context, p provider.Provider) (datasource.DataSource, diag.Diagnostics) { - if t.NewDataSourceMethod == nil { - return nil, nil - } - - return t.NewDataSourceMethod(ctx, p) -} diff --git a/internal/testing/testprovider/datasourcewithgetschema.go b/internal/testing/testprovider/datasourcewithgetschema.go deleted file mode 100644 index ce06cf30a..000000000 --- a/internal/testing/testprovider/datasourcewithgetschema.go +++ /dev/null @@ -1,29 +0,0 @@ -package testprovider - -import ( - "context" - - "github.com/hashicorp/terraform-plugin-framework/datasource" - "github.com/hashicorp/terraform-plugin-framework/diag" - "github.com/hashicorp/terraform-plugin-framework/tfsdk" -) - -var _ datasource.DataSource = &DataSourceWithGetSchema{} -var _ datasource.DataSourceWithGetSchema = &DataSourceWithGetSchema{} - -// Declarative datasource.DataSourceWithGetSchema for unit testing. -type DataSourceWithGetSchema struct { - *DataSource - - // DataSourceWithGetSchema interface methods - GetSchemaMethod func(context.Context) (tfsdk.Schema, diag.Diagnostics) -} - -// GetSchema satisfies the datasource.DataSourceWithGetSchema interface. -func (d *DataSourceWithGetSchema) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) { - if d.GetSchemaMethod == nil { - return tfsdk.Schema{}, nil - } - - return d.GetSchemaMethod(ctx) -} diff --git a/internal/testing/testprovider/datasourcewithgetschema_temp.go b/internal/testing/testprovider/datasourcewithgetschema_temp.go deleted file mode 100644 index b9b1a05bf..000000000 --- a/internal/testing/testprovider/datasourcewithgetschema_temp.go +++ /dev/null @@ -1,137 +0,0 @@ -package testprovider - -import ( - "context" - - "github.com/hashicorp/terraform-plugin-framework/datasource" - "github.com/hashicorp/terraform-plugin-framework/diag" - "github.com/hashicorp/terraform-plugin-framework/tfsdk" -) - -// This file contains temporary types until GetSchema and Metadata are required -// in DataSource. - -var _ datasource.DataSource = &DataSourceWithConfigValidatorsAndGetSchemaAndMetadata{} -var _ datasource.DataSourceWithConfigValidators = &DataSourceWithConfigValidatorsAndGetSchemaAndMetadata{} -var _ datasource.DataSourceWithGetSchema = &DataSourceWithConfigValidatorsAndGetSchemaAndMetadata{} -var _ datasource.DataSourceWithMetadata = &DataSourceWithConfigValidatorsAndGetSchemaAndMetadata{} - -// Declarative datasource.DataSourceWithGetSchema for unit testing. -type DataSourceWithConfigValidatorsAndGetSchemaAndMetadata struct { - *DataSource - - // DataSourceWithConfigValidators interface methods - ConfigValidatorsMethod func(context.Context) []datasource.ConfigValidator - - // DataSourceWithGetSchema interface methods - GetSchemaMethod func(context.Context) (tfsdk.Schema, diag.Diagnostics) - - // DataSourceWithMetadata interface methods - MetadataMethod func(context.Context, datasource.MetadataRequest, *datasource.MetadataResponse) -} - -// ConfigValidators satisfies the datasource.DataSourceWithConfigValidators interface. -func (d *DataSourceWithConfigValidatorsAndGetSchemaAndMetadata) ConfigValidators(ctx context.Context) []datasource.ConfigValidator { - if d.ConfigValidatorsMethod == nil { - return nil - } - - return d.ConfigValidatorsMethod(ctx) -} - -// GetSchema satisfies the datasource.DataSourceWithGetSchema interface. -func (d *DataSourceWithConfigValidatorsAndGetSchemaAndMetadata) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) { - if d.GetSchemaMethod == nil { - return tfsdk.Schema{}, nil - } - - return d.GetSchemaMethod(ctx) -} - -// Metadata satisfies the datasource.DataSourceWithMetadata interface. -func (d *DataSourceWithConfigValidatorsAndGetSchemaAndMetadata) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { - if d.MetadataMethod == nil { - return - } - - d.MetadataMethod(ctx, req, resp) -} - -var _ datasource.DataSource = &DataSourceWithGetSchemaAndMetadata{} -var _ datasource.DataSourceWithGetSchema = &DataSourceWithGetSchemaAndMetadata{} -var _ datasource.DataSourceWithMetadata = &DataSourceWithGetSchemaAndMetadata{} - -// Declarative datasource.DataSourceWithGetSchema for unit testing. -type DataSourceWithGetSchemaAndMetadata struct { - *DataSource - - // DataSourceWithGetSchema interface methods - GetSchemaMethod func(context.Context) (tfsdk.Schema, diag.Diagnostics) - - // DataSourceWithMetadata interface methods - MetadataMethod func(context.Context, datasource.MetadataRequest, *datasource.MetadataResponse) -} - -// GetSchema satisfies the datasource.DataSourceWithGetSchema interface. -func (d *DataSourceWithGetSchemaAndMetadata) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) { - if d.GetSchemaMethod == nil { - return tfsdk.Schema{}, nil - } - - return d.GetSchemaMethod(ctx) -} - -// Metadata satisfies the datasource.DataSourceWithMetadata interface. -func (d *DataSourceWithGetSchemaAndMetadata) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { - if d.MetadataMethod == nil { - return - } - - d.MetadataMethod(ctx, req, resp) -} - -var _ datasource.DataSource = &DataSourceWithGetSchemaAndMetadataAndValidateConfig{} -var _ datasource.DataSourceWithGetSchema = &DataSourceWithGetSchemaAndMetadataAndValidateConfig{} -var _ datasource.DataSourceWithMetadata = &DataSourceWithGetSchemaAndMetadataAndValidateConfig{} -var _ datasource.DataSourceWithValidateConfig = &DataSourceWithGetSchemaAndMetadataAndValidateConfig{} - -// Declarative datasource.DataSourceWithGetSchema for unit testing. -type DataSourceWithGetSchemaAndMetadataAndValidateConfig struct { - *DataSource - - // DataSourceWithGetSchema interface methods - GetSchemaMethod func(context.Context) (tfsdk.Schema, diag.Diagnostics) - - // DataSourceWithMetadata interface methods - MetadataMethod func(context.Context, datasource.MetadataRequest, *datasource.MetadataResponse) - - // DataSourceWithValidateConfig interface methods - ValidateConfigMethod func(context.Context, datasource.ValidateConfigRequest, *datasource.ValidateConfigResponse) -} - -// GetSchema satisfies the datasource.DataSourceWithGetSchema interface. -func (d *DataSourceWithGetSchemaAndMetadataAndValidateConfig) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) { - if d.GetSchemaMethod == nil { - return tfsdk.Schema{}, nil - } - - return d.GetSchemaMethod(ctx) -} - -// Metadata satisfies the datasource.DataSourceWithMetadata interface. -func (d *DataSourceWithGetSchemaAndMetadataAndValidateConfig) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { - if d.MetadataMethod == nil { - return - } - - d.MetadataMethod(ctx, req, resp) -} - -// ValidateConfig satisfies the datasource.DataSourceWithValidateConfig interface. -func (d *DataSourceWithGetSchemaAndMetadataAndValidateConfig) ValidateConfig(ctx context.Context, req datasource.ValidateConfigRequest, resp *datasource.ValidateConfigResponse) { - if d.ValidateConfigMethod == nil { - return - } - - d.ValidateConfigMethod(ctx, req, resp) -} diff --git a/internal/testing/testprovider/datasourcewithmetadata.go b/internal/testing/testprovider/datasourcewithmetadata.go deleted file mode 100644 index c30f4493b..000000000 --- a/internal/testing/testprovider/datasourcewithmetadata.go +++ /dev/null @@ -1,27 +0,0 @@ -package testprovider - -import ( - "context" - - "github.com/hashicorp/terraform-plugin-framework/datasource" -) - -var _ datasource.DataSource = &DataSourceWithMetadata{} -var _ datasource.DataSourceWithMetadata = &DataSourceWithMetadata{} - -// Declarative datasource.DataSourceWithMetadata for unit testing. -type DataSourceWithMetadata struct { - *DataSource - - // DataSourceWithMetadata interface methods - MetadataMethod func(context.Context, datasource.MetadataRequest, *datasource.MetadataResponse) -} - -// Metadata satisfies the datasource.DataSourceWithMetadata interface. -func (d *DataSourceWithMetadata) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { - if d.MetadataMethod == nil { - return - } - - d.MetadataMethod(ctx, req, resp) -} diff --git a/internal/testing/testprovider/provider.go b/internal/testing/testprovider/provider.go index 09f52f96e..27f5215e0 100644 --- a/internal/testing/testprovider/provider.go +++ b/internal/testing/testprovider/provider.go @@ -12,10 +12,6 @@ import ( var _ provider.Provider = &Provider{} -// Temporarily implement the new interfaces to ease testing transition -var _ provider.ProviderWithDataSources = &Provider{} -var _ provider.ProviderWithResources = &Provider{} - // Declarative provider.Provider for unit testing. type Provider struct { // Provider interface methods diff --git a/internal/testing/testprovider/providerwithgetdatasources.go b/internal/testing/testprovider/providerwithgetdatasources.go deleted file mode 100644 index e43a94acf..000000000 --- a/internal/testing/testprovider/providerwithgetdatasources.go +++ /dev/null @@ -1,31 +0,0 @@ -package testprovider - -import ( - "context" - - "github.com/hashicorp/terraform-plugin-framework/diag" - "github.com/hashicorp/terraform-plugin-framework/provider" -) - -var _ provider.Provider = &ProviderWithGetDataSources{} -var _ provider.ProviderWithGetDataSources = &ProviderWithGetDataSources{} //nolint:staticcheck // Internal usage - -// Declarative provider.ProviderWithGetDataSources for unit testing. -type ProviderWithGetDataSources struct { - *Provider - - // ProviderWithGetDataSources interface methods - //nolint:staticcheck // Internal implementation - GetDataSourcesMethod func(context.Context) (map[string]provider.DataSourceType, diag.Diagnostics) -} - -// GetDataSources satisfies the provider.ProviderWithGetDataSources interface. -// -//nolint:staticcheck // Internal implementation -func (p *ProviderWithGetDataSources) GetDataSources(ctx context.Context) (map[string]provider.DataSourceType, diag.Diagnostics) { - if p.GetDataSourcesMethod == nil { - return nil, nil - } - - return p.GetDataSourcesMethod(ctx) -} diff --git a/internal/testing/testprovider/providerwithgetresources.go b/internal/testing/testprovider/providerwithgetresources.go deleted file mode 100644 index 2bbfbc2c0..000000000 --- a/internal/testing/testprovider/providerwithgetresources.go +++ /dev/null @@ -1,31 +0,0 @@ -package testprovider - -import ( - "context" - - "github.com/hashicorp/terraform-plugin-framework/diag" - "github.com/hashicorp/terraform-plugin-framework/provider" -) - -var _ provider.Provider = &ProviderWithGetResources{} -var _ provider.ProviderWithGetResources = &ProviderWithGetResources{} //nolint:staticcheck // Internal usage - -// Declarative provider.ProviderWithGetResources for unit testing. -type ProviderWithGetResources struct { - *Provider - - // ProviderWithGetResources interface methods - //nolint:staticcheck // Internal implementation - GetResourcesMethod func(context.Context) (map[string]provider.ResourceType, diag.Diagnostics) -} - -// GetResources satisfies the provider.ProviderWithGetResources interface. -// -//nolint:staticcheck // Internal implementation -func (p *ProviderWithGetResources) GetResources(ctx context.Context) (map[string]provider.ResourceType, diag.Diagnostics) { - if p.GetResourcesMethod == nil { - return nil, nil - } - - return p.GetResourcesMethod(ctx) -} diff --git a/internal/testing/testprovider/resource.go b/internal/testing/testprovider/resource.go index 194e6666a..b4c45adc6 100644 --- a/internal/testing/testprovider/resource.go +++ b/internal/testing/testprovider/resource.go @@ -3,7 +3,9 @@ package testprovider import ( "context" + "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/resource" + "github.com/hashicorp/terraform-plugin-framework/tfsdk" ) var _ resource.Resource = &Resource{} @@ -11,10 +13,30 @@ var _ resource.Resource = &Resource{} // Declarative resource.Resource for unit testing. type Resource struct { // Resource interface methods - CreateMethod func(context.Context, resource.CreateRequest, *resource.CreateResponse) - DeleteMethod func(context.Context, resource.DeleteRequest, *resource.DeleteResponse) - ReadMethod func(context.Context, resource.ReadRequest, *resource.ReadResponse) - UpdateMethod func(context.Context, resource.UpdateRequest, *resource.UpdateResponse) + MetadataMethod func(context.Context, resource.MetadataRequest, *resource.MetadataResponse) + GetSchemaMethod func(context.Context) (tfsdk.Schema, diag.Diagnostics) + CreateMethod func(context.Context, resource.CreateRequest, *resource.CreateResponse) + DeleteMethod func(context.Context, resource.DeleteRequest, *resource.DeleteResponse) + ReadMethod func(context.Context, resource.ReadRequest, *resource.ReadResponse) + UpdateMethod func(context.Context, resource.UpdateRequest, *resource.UpdateResponse) +} + +// Metadata satisfies the resource.Resource interface. +func (r *Resource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { + if r.MetadataMethod == nil { + return + } + + r.MetadataMethod(ctx, req, resp) +} + +// GetSchema satisfies the resource.Resource interface. +func (r *Resource) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) { + if r.GetSchemaMethod == nil { + return tfsdk.Schema{}, nil + } + + return r.GetSchemaMethod(ctx) } // Create satisfies the resource.Resource interface. diff --git a/internal/testing/testprovider/resourcetype.go b/internal/testing/testprovider/resourcetype.go deleted file mode 100644 index 0e477621f..000000000 --- a/internal/testing/testprovider/resourcetype.go +++ /dev/null @@ -1,37 +0,0 @@ -package testprovider - -import ( - "context" - - "github.com/hashicorp/terraform-plugin-framework/diag" - "github.com/hashicorp/terraform-plugin-framework/provider" - "github.com/hashicorp/terraform-plugin-framework/resource" - "github.com/hashicorp/terraform-plugin-framework/tfsdk" -) - -var _ provider.ResourceType = &ResourceType{} //nolint:staticcheck // Internal implementation - -// Declarative provider.ResourceType for unit testing. -type ResourceType struct { - // ResourceType interface methods - GetSchemaMethod func(context.Context) (tfsdk.Schema, diag.Diagnostics) - NewResourceMethod func(context.Context, provider.Provider) (resource.Resource, diag.Diagnostics) -} - -// GetSchema satisfies the provider.ResourceType interface. -func (t *ResourceType) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) { - if t.GetSchemaMethod == nil { - return tfsdk.Schema{}, nil - } - - return t.GetSchemaMethod(ctx) -} - -// NewResource satisfies the provider.ResourceType interface. -func (t *ResourceType) NewResource(ctx context.Context, p provider.Provider) (resource.Resource, diag.Diagnostics) { - if t.NewResourceMethod == nil { - return nil, nil - } - - return t.NewResourceMethod(ctx, p) -} diff --git a/internal/testing/testprovider/resourcewithgetschema.go b/internal/testing/testprovider/resourcewithgetschema.go deleted file mode 100644 index 930fe57cf..000000000 --- a/internal/testing/testprovider/resourcewithgetschema.go +++ /dev/null @@ -1,29 +0,0 @@ -package testprovider - -import ( - "context" - - "github.com/hashicorp/terraform-plugin-framework/diag" - "github.com/hashicorp/terraform-plugin-framework/resource" - "github.com/hashicorp/terraform-plugin-framework/tfsdk" -) - -var _ resource.Resource = &ResourceWithGetSchema{} -var _ resource.ResourceWithGetSchema = &ResourceWithGetSchema{} - -// Declarative resource.ResourceWithGetSchema for unit testing. -type ResourceWithGetSchema struct { - *Resource - - // ResourceWithGetSchema interface methods - GetSchemaMethod func(context.Context) (tfsdk.Schema, diag.Diagnostics) -} - -// GetSchema satisfies the resource.ResourceWithGetSchema interface. -func (r *ResourceWithGetSchema) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) { - if r.GetSchemaMethod == nil { - return tfsdk.Schema{}, nil - } - - return r.GetSchemaMethod(ctx) -} diff --git a/internal/testing/testprovider/resourcewithgetschema_temp.go b/internal/testing/testprovider/resourcewithgetschema_temp.go deleted file mode 100644 index ac38d7c2b..000000000 --- a/internal/testing/testprovider/resourcewithgetschema_temp.go +++ /dev/null @@ -1,281 +0,0 @@ -package testprovider - -import ( - "context" - - "github.com/hashicorp/terraform-plugin-framework/diag" - "github.com/hashicorp/terraform-plugin-framework/resource" - "github.com/hashicorp/terraform-plugin-framework/tfsdk" -) - -// This file contains temporary types until GetSchema and Metadata are required -// in Resource. - -var _ resource.Resource = &ResourceWithConfigValidatorsAndGetSchemaAndMetadata{} -var _ resource.ResourceWithConfigValidators = &ResourceWithConfigValidatorsAndGetSchemaAndMetadata{} -var _ resource.ResourceWithGetSchema = &ResourceWithConfigValidatorsAndGetSchemaAndMetadata{} -var _ resource.ResourceWithMetadata = &ResourceWithConfigValidatorsAndGetSchemaAndMetadata{} - -// Declarative resource.ResourceWithGetSchema for unit testing. This type is -// temporary until GetSchema and Metadata are required in Resource. -type ResourceWithConfigValidatorsAndGetSchemaAndMetadata struct { - *Resource - - // ResourceWithConfigValidators interface methods - ConfigValidatorsMethod func(context.Context) []resource.ConfigValidator - - // ResourceWithGetSchema interface methods - GetSchemaMethod func(context.Context) (tfsdk.Schema, diag.Diagnostics) - - // ResourceWithMetadata interface methods - MetadataMethod func(context.Context, resource.MetadataRequest, *resource.MetadataResponse) -} - -// ConfigValidators satisfies the resource.ResourceWithConfigValidators interface. -func (r *ResourceWithConfigValidatorsAndGetSchemaAndMetadata) ConfigValidators(ctx context.Context) []resource.ConfigValidator { - if r.ConfigValidatorsMethod == nil { - return nil - } - - return r.ConfigValidatorsMethod(ctx) -} - -// GetSchema satisfies the resource.ResourceWithGetSchema interface. -func (r *ResourceWithConfigValidatorsAndGetSchemaAndMetadata) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) { - if r.GetSchemaMethod == nil { - return tfsdk.Schema{}, nil - } - - return r.GetSchemaMethod(ctx) -} - -// Metadata satisfies the resource.ResourceWithMetadata interface. -func (r *ResourceWithConfigValidatorsAndGetSchemaAndMetadata) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { - if r.MetadataMethod == nil { - return - } - - r.MetadataMethod(ctx, req, resp) -} - -var _ resource.Resource = &ResourceWithGetSchemaAndMetadata{} -var _ resource.ResourceWithGetSchema = &ResourceWithGetSchemaAndMetadata{} -var _ resource.ResourceWithMetadata = &ResourceWithGetSchemaAndMetadata{} - -// Declarative resource.ResourceWithGetSchema for unit testing. This type is -// temporary until GetSchema and Metadata are required in Resource. -type ResourceWithGetSchemaAndMetadata struct { - *Resource - - // ResourceWithGetSchema interface methods - GetSchemaMethod func(context.Context) (tfsdk.Schema, diag.Diagnostics) - - // ResourceWithMetadata interface methods - MetadataMethod func(context.Context, resource.MetadataRequest, *resource.MetadataResponse) -} - -// GetSchema satisfies the resource.ResourceWithGetSchema interface. -func (r *ResourceWithGetSchemaAndMetadata) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) { - if r.GetSchemaMethod == nil { - return tfsdk.Schema{}, nil - } - - return r.GetSchemaMethod(ctx) -} - -// Metadata satisfies the resource.ResourceWithMetadata interface. -func (r *ResourceWithGetSchemaAndMetadata) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { - if r.MetadataMethod == nil { - return - } - - r.MetadataMethod(ctx, req, resp) -} - -var _ resource.Resource = &ResourceWithGetSchemaAndImportStateAndMetadata{} -var _ resource.ResourceWithGetSchema = &ResourceWithGetSchemaAndImportStateAndMetadata{} -var _ resource.ResourceWithImportState = &ResourceWithGetSchemaAndImportStateAndMetadata{} -var _ resource.ResourceWithMetadata = &ResourceWithGetSchemaAndImportStateAndMetadata{} - -// Declarative resource.ResourceWithGetSchema for unit testing. This type is -// temporary until GetSchema and Metadata are required in Resource. -type ResourceWithGetSchemaAndImportStateAndMetadata struct { - *Resource - - // ResourceWithGetSchema interface methods - GetSchemaMethod func(context.Context) (tfsdk.Schema, diag.Diagnostics) - - // ResourceWithImportState interface methods - ImportStateMethod func(context.Context, resource.ImportStateRequest, *resource.ImportStateResponse) - - // ResourceWithMetadata interface methods - MetadataMethod func(context.Context, resource.MetadataRequest, *resource.MetadataResponse) -} - -// GetSchema satisfies the resource.ResourceWithGetSchema interface. -func (r *ResourceWithGetSchemaAndImportStateAndMetadata) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) { - if r.GetSchemaMethod == nil { - return tfsdk.Schema{}, nil - } - - return r.GetSchemaMethod(ctx) -} - -// ImportState satisfies the resource.ResourceWithImportState interface. -func (r *ResourceWithGetSchemaAndImportStateAndMetadata) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { - if r.ImportStateMethod == nil { - return - } - - r.ImportStateMethod(ctx, req, resp) -} - -// Metadata satisfies the resource.ResourceWithMetadata interface. -func (r *ResourceWithGetSchemaAndImportStateAndMetadata) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { - if r.MetadataMethod == nil { - return - } - - r.MetadataMethod(ctx, req, resp) -} - -var _ resource.Resource = &ResourceWithGetSchemaAndModifyPlanAndMetadata{} -var _ resource.ResourceWithGetSchema = &ResourceWithGetSchemaAndModifyPlanAndMetadata{} -var _ resource.ResourceWithModifyPlan = &ResourceWithGetSchemaAndModifyPlanAndMetadata{} -var _ resource.ResourceWithMetadata = &ResourceWithGetSchemaAndModifyPlanAndMetadata{} - -// Declarative resource.ResourceWithGetSchema for unit testing. This type is -// temporary until GetSchema and Metadata are required in Resource. -type ResourceWithGetSchemaAndModifyPlanAndMetadata struct { - *Resource - - // ResourceWithGetSchema interface methods - GetSchemaMethod func(context.Context) (tfsdk.Schema, diag.Diagnostics) - - // ResourceWithModifyPlan interface methods - ModifyPlanMethod func(context.Context, resource.ModifyPlanRequest, *resource.ModifyPlanResponse) - - // ResourceWithMetadata interface methods - MetadataMethod func(context.Context, resource.MetadataRequest, *resource.MetadataResponse) -} - -// GetSchema satisfies the resource.ResourceWithGetSchema interface. -func (r *ResourceWithGetSchemaAndModifyPlanAndMetadata) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) { - if r.GetSchemaMethod == nil { - return tfsdk.Schema{}, nil - } - - return r.GetSchemaMethod(ctx) -} - -// ModifyPlan satisfies the resource.ResourceWithModifyPlan interface. -func (r *ResourceWithGetSchemaAndModifyPlanAndMetadata) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { - if r.ModifyPlanMethod == nil { - return - } - - r.ModifyPlanMethod(ctx, req, resp) -} - -// Metadata satisfies the resource.ResourceWithMetadata interface. -func (r *ResourceWithGetSchemaAndModifyPlanAndMetadata) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { - if r.MetadataMethod == nil { - return - } - - r.MetadataMethod(ctx, req, resp) -} - -var _ resource.Resource = &ResourceWithGetSchemaAndMetadataAndUpgradeState{} -var _ resource.ResourceWithGetSchema = &ResourceWithGetSchemaAndMetadataAndUpgradeState{} -var _ resource.ResourceWithMetadata = &ResourceWithGetSchemaAndMetadataAndUpgradeState{} -var _ resource.ResourceWithUpgradeState = &ResourceWithGetSchemaAndMetadataAndUpgradeState{} - -// Declarative resource.ResourceWithGetSchema for unit testing. This type is -// temporary until GetSchema and Metadata are required in Resource. -type ResourceWithGetSchemaAndMetadataAndUpgradeState struct { - *Resource - - // ResourceWithGetSchema interface methods - GetSchemaMethod func(context.Context) (tfsdk.Schema, diag.Diagnostics) - - // ResourceWithMetadata interface methods - MetadataMethod func(context.Context, resource.MetadataRequest, *resource.MetadataResponse) - - // ResourceWithUpgradeState interface methods - UpgradeStateMethod func(context.Context) map[int64]resource.StateUpgrader -} - -// GetSchema satisfies the resource.ResourceWithGetSchema interface. -func (r *ResourceWithGetSchemaAndMetadataAndUpgradeState) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) { - if r.GetSchemaMethod == nil { - return tfsdk.Schema{}, nil - } - - return r.GetSchemaMethod(ctx) -} - -// Metadata satisfies the resource.ResourceWithMetadata interface. -func (r *ResourceWithGetSchemaAndMetadataAndUpgradeState) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { - if r.MetadataMethod == nil { - return - } - - r.MetadataMethod(ctx, req, resp) -} - -// UpgradeState satisfies the resource.ResourceWithUpgradeState interface. -func (r *ResourceWithGetSchemaAndMetadataAndUpgradeState) UpgradeState(ctx context.Context) map[int64]resource.StateUpgrader { - if r.UpgradeStateMethod == nil { - return nil - } - - return r.UpgradeStateMethod(ctx) -} - -var _ resource.Resource = &ResourceWithGetSchemaAndMetadataAndValidateConfig{} -var _ resource.ResourceWithGetSchema = &ResourceWithGetSchemaAndMetadataAndValidateConfig{} -var _ resource.ResourceWithMetadata = &ResourceWithGetSchemaAndMetadataAndValidateConfig{} -var _ resource.ResourceWithValidateConfig = &ResourceWithGetSchemaAndMetadataAndValidateConfig{} - -// Declarative resource.ResourceWithGetSchema for unit testing. This type is -// temporary until GetSchema and Metadata are required in Resource. -type ResourceWithGetSchemaAndMetadataAndValidateConfig struct { - *Resource - - // ResourceWithGetSchema interface methods - GetSchemaMethod func(context.Context) (tfsdk.Schema, diag.Diagnostics) - - // ResourceWithMetadata interface methods - MetadataMethod func(context.Context, resource.MetadataRequest, *resource.MetadataResponse) - - // ResourceWithValidateConfig interface methods - ValidateConfigMethod func(context.Context, resource.ValidateConfigRequest, *resource.ValidateConfigResponse) -} - -// GetSchema satisfies the resource.ResourceWithGetSchema interface. -func (r *ResourceWithGetSchemaAndMetadataAndValidateConfig) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) { - if r.GetSchemaMethod == nil { - return tfsdk.Schema{}, nil - } - - return r.GetSchemaMethod(ctx) -} - -// Metadata satisfies the resource.ResourceWithMetadata interface. -func (r *ResourceWithGetSchemaAndMetadataAndValidateConfig) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { - if r.MetadataMethod == nil { - return - } - - r.MetadataMethod(ctx, req, resp) -} - -// ValidateConfig satisfies the resource.ResourceWithValidateConfig interface. -func (r *ResourceWithGetSchemaAndMetadataAndValidateConfig) ValidateConfig(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) { - if r.ValidateConfigMethod == nil { - return - } - - r.ValidateConfigMethod(ctx, req, resp) -} diff --git a/internal/testing/testprovider/resourcewithmetadata.go b/internal/testing/testprovider/resourcewithmetadata.go deleted file mode 100644 index 83dcd5a3a..000000000 --- a/internal/testing/testprovider/resourcewithmetadata.go +++ /dev/null @@ -1,27 +0,0 @@ -package testprovider - -import ( - "context" - - "github.com/hashicorp/terraform-plugin-framework/resource" -) - -var _ resource.Resource = &ResourceWithMetadata{} -var _ resource.ResourceWithMetadata = &ResourceWithMetadata{} - -// Declarative resource.ResourceWithMetadata for unit testing. -type ResourceWithMetadata struct { - *Resource - - // ResourceWithMetadata interface methods - MetadataMethod func(context.Context, resource.MetadataRequest, *resource.MetadataResponse) -} - -// Metadata satisfies the resource.ResourceWithMetadata interface. -func (r *ResourceWithMetadata) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { - if r.MetadataMethod == nil { - return - } - - r.MetadataMethod(ctx, req, resp) -} diff --git a/provider/data_source_type.go b/provider/data_source_type.go deleted file mode 100644 index 9ee76f986..000000000 --- a/provider/data_source_type.go +++ /dev/null @@ -1,24 +0,0 @@ -package provider - -import ( - "context" - - "github.com/hashicorp/terraform-plugin-framework/datasource" - "github.com/hashicorp/terraform-plugin-framework/diag" - "github.com/hashicorp/terraform-plugin-framework/tfsdk" -) - -// A DataSourceType is a type of data source. For each type of data source this -// provider supports, it should define a type implementing DataSourceType and -// return an instance of it in the map returned by Provider.GetDataSources. -// -// Deprecated: Migrate to datasource.DataSource implementation Configure, -// GetSchema, and Metadata methods. Migrate the provider.Provider -// implementation from the GetDataSources method to the DataSources method. -type DataSourceType interface { - // GetSchema returns the schema for this data source. - GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) - - // NewDataSource instantiates a new DataSource of this DataSourceType. - NewDataSource(context.Context, Provider) (datasource.DataSource, diag.Diagnostics) -} diff --git a/provider/provider.go b/provider/provider.go index 11d7f1a64..f93f29fc1 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -33,6 +33,20 @@ type Provider interface { // API client, which should be stored on the struct implementing the // Provider interface. Configure(context.Context, ConfigureRequest, *ConfigureResponse) + + // DataSources returns a slice of functions to instantiate each DataSource + // implementation. + // + // The data source type name is determined by the DataSource implementing + // the Metadata method. All data sources must have unique names. + DataSources(context.Context) []func() datasource.DataSource + + // Resources returns a slice of functions to instantiate each Resource + // implementation. + // + // The resource type name is determined by the Resource implementing + // the Metadata method. All resources must have unique names. + Resources(context.Context) []func() resource.Resource } // ProviderWithConfigValidators is an interface type that extends Provider to include declarative validations. @@ -50,61 +64,6 @@ type ProviderWithConfigValidators interface { ConfigValidators(context.Context) []ConfigValidator } -// ProviderWithDataSources is an interface type that extends Provider to -// include data source implementations. -type ProviderWithDataSources interface { - Provider - - // DataSources returns a slice of functions to instantiate each DataSource - // implementation. - // - // The data source type name is determined by the DataSource implementing - // the Metadata method. All data sources must have unique names. - DataSources(context.Context) []func() datasource.DataSource -} - -// ProviderWithGetDataSources is an interface type that extends Provider to -// include the previously required GetDataSources method. -// -// Deprecated: This will be removed in a future release. Use the DataSources -// method instead. -type ProviderWithGetDataSources interface { - Provider - - // GetDataSources returns a mapping of data source name to types - // implementations. - // - // Conventionally, data source names should each include a prefix of the - // provider name and an underscore. For example, a provider named - // "examplecloud" with data sources "thing" and "widget" should use - // "examplecloud_thing" and "examplecloud_widget" as data source names. - // - // Deprecated: This will be removed in a future release. Use the - // DataSources method instead. - GetDataSources(context.Context) (map[string]DataSourceType, diag.Diagnostics) -} - -// ProviderWithGetResources is an interface type that extends Provider to -// include the previously required GetResources method. -// -// Deprecated: This will be removed in a future release. Use the Resources -// method instead. -type ProviderWithGetResources interface { - Provider - - // GetResources returns a mapping of resource names to type - // implementations. - // - // Conventionally, resource names should each include a prefix of the - // provider name and an underscore. For example, a provider named - // "examplecloud" with resources "thing" and "widget" should use - // "examplecloud_thing" and "examplecloud_widget" as resource names. - // - // Deprecated: This will be removed in a future release. Use the Resources - // method instead. - GetResources(context.Context) (map[string]ResourceType, diag.Diagnostics) -} - // ProviderWithMetadata is an interface type that extends Provider to // return its type name, such as examplecloud, and other // metadata, such as version. @@ -131,19 +90,6 @@ type ProviderWithMetaSchema interface { GetMetaSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) } -// ProviderWithResources is an interface type that extends Provider to -// include data source implementations. -type ProviderWithResources interface { - Provider - - // Resources returns a slice of functions to instantiate each Resource - // implementation. - // - // The resource type name is determined by the Resource implementing - // the Metadata method. All resources must have unique names. - Resources(context.Context) []func() resource.Resource -} - // ProviderWithValidateConfig is an interface type that extends Provider to include imperative validation. // // Declaring validation using this methodology simplifies one-off diff --git a/provider/resource_type.go b/provider/resource_type.go deleted file mode 100644 index 93c61e5d0..000000000 --- a/provider/resource_type.go +++ /dev/null @@ -1,24 +0,0 @@ -package provider - -import ( - "context" - - "github.com/hashicorp/terraform-plugin-framework/diag" - "github.com/hashicorp/terraform-plugin-framework/resource" - "github.com/hashicorp/terraform-plugin-framework/tfsdk" -) - -// A ResourceType is a type of resource. For each type of resource this provider -// supports, it should define a type implementing ResourceType and return an -// instance of it in the map returned by Provider.GetResources. -// -// Deprecated: Migrate to resource.Resource implementation Configure, -// GetSchema, and Metadata methods. Migrate the provider.Provider -// implementation from the GetResources method to the Resources method. -type ResourceType interface { - // GetSchema returns the schema for this resource. - GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) - - // NewResource instantiates a new Resource of this ResourceType. - NewResource(context.Context, Provider) (resource.Resource, diag.Diagnostics) -} diff --git a/resource/resource.go b/resource/resource.go index c8aea6ddd..63ced26a0 100644 --- a/resource/resource.go +++ b/resource/resource.go @@ -23,6 +23,13 @@ import ( // Although not required, it is conventional for resources to implement the // ResourceWithImportState interface. type Resource interface { + // Metadata should return the full name of the resource, such as + // examplecloud_thing. + Metadata(context.Context, MetadataRequest, *MetadataResponse) + + // GetSchema returns the schema for this resource. + GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) + // Create is called when the provider must create a new resource. Config // and planned state values should be read from the // CreateRequest and new state values set on the CreateResponse. @@ -78,16 +85,6 @@ type ResourceWithConfigValidators interface { ConfigValidators(context.Context) []ConfigValidator } -// ResourceWithGetSchema is an interface type that extends Resource to -// return its schema definition. -// -// This method will be required in the Resource interface in a future -// release. -type ResourceWithGetSchema interface { - // GetSchema returns the schema for this data source. - GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) -} - // Optional interface on top of Resource that enables provider control over // the ImportResourceState RPC. This RPC is called by Terraform when the // `terraform import` command is executed. Afterwards, the ReadResource RPC @@ -130,20 +127,6 @@ type ResourceWithModifyPlan interface { ModifyPlan(context.Context, ModifyPlanRequest, *ModifyPlanResponse) } -// ResourceWithMetadata is an interface type that extends Resource to -// return metadata, such as its resource type name. For example, if the -// provider is named examplecloud and the resource manages a thing, this -// should return examplecloud_thing. -// -// This method will be required in the Resource interface a future release. -type ResourceWithMetadata interface { - Resource - - // Metadata should return the full name of the resource, such as - // examplecloud_thing. - Metadata(context.Context, MetadataRequest, *MetadataResponse) -} - // Optional interface on top of Resource that enables provider control over // the UpgradeResourceState RPC. This RPC is automatically called by Terraform // when the current Schema type Version field is greater than the stored state. diff --git a/website/docs/plugin/framework/data-sources/index.mdx b/website/docs/plugin/framework/data-sources/index.mdx index ea3a88bc7..2f0978e0f 100644 --- a/website/docs/plugin/framework/data-sources/index.mdx +++ b/website/docs/plugin/framework/data-sources/index.mdx @@ -67,7 +67,7 @@ func (d *ThingDataSource) Read(ctx context.Context, req datasource.ReadRequest, ### Metadata Method -The [`datasource.DataSourceWithMetadata` interface `Metadata` method](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/datasource#DataSourceWithMetadata.Metadata) defines the data source name as it would appear in Terraform configurations. This name should include the provider type prefix, an underscore, then the data source specific name. For example, a provider named `examplecloud` and a data source that reads "thing" resources would be named `examplecloud_thing`. Ensure the [Add Data Source To Provider](#add-data-source-to-provider) documentation is followed so the data source becomes part of the provider implementation, and therefore available to practitioners. +The [`datasource.DataSource` interface `Metadata` method](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/datasource#DataSource.Metadata) defines the data source name as it would appear in Terraform configurations. This name should include the provider type prefix, an underscore, then the data source specific name. For example, a provider named `examplecloud` and a data source that reads "thing" resources would be named `examplecloud_thing`. Ensure the [Add Data Source To Provider](#add-data-source-to-provider) documentation is followed so the data source becomes part of the provider implementation, and therefore available to practitioners. In this example, the data source name in an `examplecloud` provider that reads "thing" resources is hardcoded to `examplecloud_thing`: @@ -96,7 +96,7 @@ func (d *ThingDataSource) Metadata(ctx context.Context, req datasource.MetadataR ### GetSchema Method -The [`datasource.DataSourceWithGetSchema` interface `GetSchema` method](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/datasource#DataSourceWithGetSchema.GetSchema) defines a [schema](/plugin/framework/schemas) describing what data is available in the data source's configuration and state. +The [`datasource.DataSource` interface `GetSchema` method](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/datasource#DataSource.GetSchema) defines a [schema](/plugin/framework/schemas) describing what data is available in the data source's configuration and state. ### Read Method diff --git a/website/docs/plugin/framework/resources/index.mdx b/website/docs/plugin/framework/resources/index.mdx index 75e6b9a14..7950cdb16 100644 --- a/website/docs/plugin/framework/resources/index.mdx +++ b/website/docs/plugin/framework/resources/index.mdx @@ -111,7 +111,7 @@ func (r *ThingResource) Read(ctx context.Context, req resource.ReadRequest, resp ### Metadata Method -The [`resource.ResourceWithMetadata` interface `Metadata` method](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/resource#ResourceWithMetadata.Metadata) defines the resource name as it would appear in Terraform configurations. This name should include the provider type prefix, an underscore, then the resource specific name. For example, a provider named `examplecloud` and a resource that reads "thing" resources would be named `examplecloud_thing`. Ensure the [Add Data Source To Provider](#add-data-source-to-provider) documentation is followed so the resource becomes part of the provider implementation, and therefore available to practitioners. +The [`resource.Resource` interface `Metadata` method](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/resource#Resource.Metadata) defines the resource name as it would appear in Terraform configurations. This name should include the provider type prefix, an underscore, then the resource specific name. For example, a provider named `examplecloud` and a resource that reads "thing" resources would be named `examplecloud_thing`. Ensure the [Add Data Source To Provider](#add-data-source-to-provider) documentation is followed so the resource becomes part of the provider implementation, and therefore available to practitioners. In this example, the resource name in an `examplecloud` provider that reads "thing" resources is hardcoded to `examplecloud_thing`: @@ -140,7 +140,7 @@ func (d *ThingDataSource) Metadata(ctx context.Context, req resource.MetadataReq ### GetSchema Method -The [`resource.ResourceWithGetSchema` interface `GetSchema` method](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/resource#ResourceWithGetSchema.GetSchema) defines a [schema](/plugin/framework/schemas) describing what data is available in the resource's configuration, plan, and state. +The [`resource.Resource` interface `GetSchema` method](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/resource#Resource.GetSchema) defines a [schema](/plugin/framework/schemas) describing what data is available in the resource's configuration, plan, and state. ### Create From f610319e8539e5197f23059347eacf69727d03f8 Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Mon, 12 Sep 2022 17:40:38 -0400 Subject: [PATCH 2/3] Update CHANGELOG for #478 --- .changelog/{pending.txt => 478.txt} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .changelog/{pending.txt => 478.txt} (100%) diff --git a/.changelog/pending.txt b/.changelog/478.txt similarity index 100% rename from .changelog/pending.txt rename to .changelog/478.txt From 62de8068933a35ee236b27be85f6f5899fdad8b7 Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Mon, 12 Sep 2022 17:42:21 -0400 Subject: [PATCH 3/3] internal/fwserver: Remove unnecessary type assertions --- internal/fwserver/server.go | 26 ++------------------------ 1 file changed, 2 insertions(+), 24 deletions(-) diff --git a/internal/fwserver/server.go b/internal/fwserver/server.go index 8a97cac61..af06952f6 100644 --- a/internal/fwserver/server.go +++ b/internal/fwserver/server.go @@ -230,19 +230,8 @@ func (s *Server) DataSourceSchemas(ctx context.Context) (map[string]fwschema.Sch for dataSourceTypeName, dataSourceFunc := range dataSourceFuncs { dataSource := dataSourceFunc() - dataSourceWithGetSchema, ok := dataSource.(datasource.DataSource) - - if !ok { - s.dataSourceSchemasDiags.AddError( - "Data Source Schema Missing", - fmt.Sprintf("The %T DataSource in the provider is missing the GetSchema method. ", dataSource)+ - "This is always an issue with the provider and should be reported to the provider developers.", - ) - continue - } - logging.FrameworkDebug(ctx, "Calling provider defined DataSource GetSchema", map[string]interface{}{logging.KeyDataSourceType: dataSourceTypeName}) - schema, diags := dataSourceWithGetSchema.GetSchema(ctx) + schema, diags := dataSource.GetSchema(ctx) logging.FrameworkDebug(ctx, "Called provider defined DataSource GetSchema", map[string]interface{}{logging.KeyDataSourceType: dataSourceTypeName}) s.dataSourceSchemasDiags.Append(diags...) @@ -419,19 +408,8 @@ func (s *Server) ResourceSchemas(ctx context.Context) (map[string]fwschema.Schem for resourceTypeName, resourceFunc := range resourceFuncs { res := resourceFunc() - resourceWithGetSchema, ok := res.(resource.Resource) - - if !ok { - s.resourceSchemasDiags.AddError( - "Resource Schema Missing", - fmt.Sprintf("The %T Resource in the provider is missing the GetSchema method. ", res)+ - "This is always an issue with the provider and should be reported to the provider developers.", - ) - continue - } - logging.FrameworkDebug(ctx, "Calling provider defined Resource GetSchema", map[string]interface{}{logging.KeyResourceType: resourceTypeName}) - schema, diags := resourceWithGetSchema.GetSchema(ctx) + schema, diags := res.GetSchema(ctx) logging.FrameworkDebug(ctx, "Called provider defined Resource GetSchema", map[string]interface{}{logging.KeyResourceType: resourceTypeName}) s.resourceSchemasDiags.Append(diags...)