Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider: Deprecate DataSourceType, ResourceType, Provider type GetDataSources, and Provider type GetResources #472

Merged
merged 12 commits into from Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 43 additions & 0 deletions .changelog/472.txt
@@ -0,0 +1,43 @@
```release-note:enhancement
datasource: The `DataSource` type `GetSchema` and `Metadata` methods will be required in the next version.
```

```release-note:note
provider: The `DataSourceType` type has been deprecated in preference of moving the `GetSchema` method to the `datasource.DataSource` type and optionally implementing the `NewResource` method logic to a new `Configure` method. The `DataSourceType` type will be removed in the next version.
```

```release-note:note
provider: The `Provider` type `GetDataSources` method has been deprecated in preference of the `DataSources` method. All `datasource.DataSource` types must implement the `Metadata` method after migrating. Support for the `GetDataSources` method will be removed in the next version.
```

```release-note:note
provider: The `Provider` type `GetResources` method has been deprecated in preference of the `Resources` method. All `resource.Resource` types must implement the `Metadata` method after migrating. Support for the `GetResources` method will be removed in the next version.
```

```release-note:note
provider: The `ResourceType` type has been deprecated in preference of moving the `GetSchema` method to the `resource.Resource` type and optionally implementing the `NewResource` method logic to a new `Configure` method. The `ResourceType` type will be removed in the next version.
```

```release-note:enhancement
bflad marked this conversation as resolved.
Show resolved Hide resolved
resource: The `Resource` type `GetSchema` and `Metadata` methods will be required in the next version.
```

```release-note:enhancement
datasource: Added `DataSource` type `Configure`, `GetSchema`, and `Metadata` method support
```

```release-note:enhancement
provider: Added `ConfigureResponse` type `DataSourceData` field, which will set the `datasource.ConfigureRequest.ProviderData` field
```

```release-note:enhancement
provider: Added `ConfigureResponse` type `ResourceData` field, which will set the `resource.ConfigureRequest.ProviderData` field
```

```release-note:enhancement
provider: Added `Provider` type `Metadata` method support, which the `MetadataResponse.TypeName` field will set the `datasource.MetadataRequest.ProviderTypeName` and `resource.MetadataRequest.ProviderTypeName` fields
```

```release-note:enhancement
resource: Added `Resource` type `Configure`, `GetSchema`, and `Metadata` method support
```
31 changes: 31 additions & 0 deletions datasource/configure.go
@@ -0,0 +1,31 @@
package datasource

import (
"github.com/hashicorp/terraform-plugin-framework/diag"
)

// ConfigureRequest represents a request for the provider to configure a data
// source, i.e., set provider-level data or clients. An instance of this
// request struct is supplied as an argument to the DataSource type Configure
// method.
type ConfigureRequest struct {
// ProviderData is the data set in the
// [provider.ConfigureResponse.DataSourceData] field. This data is
// provider-specifc and therefore can contain any necessary remote system
// clients, custom provider data, or anything else pertinent to the
// functionality of the DataSource.
//
// This data is only set after the ConfigureProvider RPC has been called
// by Terraform.
ProviderData any
}

// ConfigureResponse represents a response to a ConfigureRequest. An
// instance of this response struct is supplied as an argument to the
// DataSource type Configure method.
type ConfigureResponse struct {
// Diagnostics report errors or warnings related to configuring of the
// Datasource. An empty slice indicates a successful operation with no
// warnings or errors generated.
Diagnostics diag.Diagnostics
}
48 changes: 47 additions & 1 deletion datasource/data_source.go
@@ -1,12 +1,18 @@
package datasource

import "context"
import (
"context"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
)

// DataSource represents an instance of a data source type. This is the core
// interface that all data sources must implement.
//
// Data sources can optionally implement these additional concepts:
//
// - Configure: Include provider-level data or clients.
// - Validation: Schema-based via tfsdk.Attribute or entire configuration
// via DataSourceWithConfigValidators or DataSourceWithValidateConfig.
type DataSource interface {
Expand All @@ -16,6 +22,22 @@ type DataSource interface {
Read(context.Context, ReadRequest, *ReadResponse)
}

// DataSourceWithConfigure is an interface type that extends DataSource to
// include a method which the framework will automatically call so provider
// developers have the opportunity to setup any necessary provider-level data
// or clients in the DataSource type.
//
// This method is intended to replace the provider.DataSourceType type
// NewDataSource method in a future release.
type DataSourceWithConfigure interface {
DataSource

// Configure enables provider-level data or clients to be set in the
// provider-defined DataSource type. It is separately executed for each
// ReadDataSource RPC.
Configure(context.Context, ConfigureRequest, *ConfigureResponse)
}

// DataSourceWithConfigValidators is an interface type that extends DataSource to include declarative validations.
//
// Declaring validation using this methodology simplifies implmentation of
Expand All @@ -31,6 +53,30 @@ 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
Expand Down
10 changes: 4 additions & 6 deletions datasource/doc.go
Expand Up @@ -5,14 +5,12 @@
// to offer practitioners a read-only source of information, which is saved
// into the Terraform state and can be referenced by other parts of a
// configuration. Data sources are defined by a data source type/name, such as
// "example_thing", a schema representing the structure and data types of
// "examplecloud_thing", a schema representing the structure and data types of
// configuration and state, and read logic.
//
// The main starting point for implementations in this package is the
// DataSource type which represents an instance of a data source type that has
// its own configuration, read logic, and state. A DataSource is instantiated
// from a provider.DataSourceType type NewDataSource method, which also defines
// the data source schema. The provider.DataSourceType types are referenced by
// a provider.Provider type GetDataSources method, which enables the data
// source for practitioner and testing usage.
// its own configuration, read logic, and state. The DataSource implementations
// are referenced by a [provider.Provider] type DataSources method, which
// enables the data source for practitioner and testing usage.
package datasource
21 changes: 21 additions & 0 deletions datasource/metadata.go
@@ -0,0 +1,21 @@
package datasource

// MetadataRequest represents a request for the DataSource to return metadata,
// such as its type name. An instance of this request struct is supplied as an
// argument to the DataSource type Metadata method.
type MetadataRequest struct {
// ProviderTypeName is the string returned from
// [provider.MetadataResponse.TypeName], if the Provider type implements
// the Metadata method. This string should prefix the DataSource type name
// with an underscore in the response.
ProviderTypeName string
}

// MetadataResponse represents a response to a MetadataRequest. An
// instance of this response struct is supplied as an argument to the
// DataSource type Metadata method.
type MetadataResponse struct {
// TypeName should be the full data source type, including the provider
// type prefix and an underscore. For example, examplecloud_thing.
TypeName string
}
6 changes: 3 additions & 3 deletions internal/fromproto5/applyresourcechange.go
Expand Up @@ -9,12 +9,12 @@ import (
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
"github.com/hashicorp/terraform-plugin-framework/internal/privatestate"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/resource"
)

// ApplyResourceChangeRequest returns the *fwserver.ApplyResourceChangeRequest
// equivalent of a *tfprotov5.ApplyResourceChangeRequest.
func ApplyResourceChangeRequest(ctx context.Context, proto5 *tfprotov5.ApplyResourceChangeRequest, resourceType provider.ResourceType, resourceSchema fwschema.Schema, providerMetaSchema fwschema.Schema) (*fwserver.ApplyResourceChangeRequest, diag.Diagnostics) {
func ApplyResourceChangeRequest(ctx context.Context, proto5 *tfprotov5.ApplyResourceChangeRequest, resource resource.Resource, resourceSchema fwschema.Schema, providerMetaSchema fwschema.Schema) (*fwserver.ApplyResourceChangeRequest, diag.Diagnostics) {
if proto5 == nil {
return nil, nil
}
Expand All @@ -37,7 +37,7 @@ func ApplyResourceChangeRequest(ctx context.Context, proto5 *tfprotov5.ApplyReso

fw := &fwserver.ApplyResourceChangeRequest{
ResourceSchema: resourceSchema,
ResourceType: resourceType,
Resource: resource,
}

config, configDiags := Config(ctx, proto5.Config, resourceSchema)
Expand Down
6 changes: 3 additions & 3 deletions internal/fromproto5/applyresourcechange_test.go
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
"github.com/hashicorp/terraform-plugin-framework/internal/privatestate"
"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"
)
Expand Down Expand Up @@ -57,7 +57,7 @@ func TestApplyResourceChangeRequest(t *testing.T) {
testCases := map[string]struct {
input *tfprotov5.ApplyResourceChangeRequest
resourceSchema fwschema.Schema
resourceType provider.ResourceType
resource resource.Resource
providerMetaSchema fwschema.Schema
expected *fwserver.ApplyResourceChangeRequest
expectedDiagnostics diag.Diagnostics
Expand Down Expand Up @@ -253,7 +253,7 @@ func TestApplyResourceChangeRequest(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()

got, diags := fromproto5.ApplyResourceChangeRequest(context.Background(), testCase.input, testCase.resourceType, testCase.resourceSchema, testCase.providerMetaSchema)
got, diags := fromproto5.ApplyResourceChangeRequest(context.Background(), testCase.input, testCase.resource, testCase.resourceSchema, testCase.providerMetaSchema)

if diff := cmp.Diff(got, testCase.expected, cmp.AllowUnexported(privatestate.ProviderData{})); diff != "" {
t.Errorf("unexpected difference: %s", diff)
Expand Down
10 changes: 5 additions & 5 deletions internal/fromproto5/importresourcestate.go
Expand Up @@ -6,15 +6,15 @@ import (
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

// ImportResourceStateRequest returns the *fwserver.ImportResourceStateRequest
// equivalent of a *tfprotov5.ImportResourceStateRequest.
func ImportResourceStateRequest(ctx context.Context, proto5 *tfprotov5.ImportResourceStateRequest, resourceType provider.ResourceType, resourceSchema fwschema.Schema) (*fwserver.ImportResourceStateRequest, diag.Diagnostics) {
func ImportResourceStateRequest(ctx context.Context, proto5 *tfprotov5.ImportResourceStateRequest, resource resource.Resource, resourceSchema fwschema.Schema) (*fwserver.ImportResourceStateRequest, diag.Diagnostics) {
if proto5 == nil {
return nil, nil
}
Expand All @@ -40,9 +40,9 @@ func ImportResourceStateRequest(ctx context.Context, proto5 *tfprotov5.ImportRes
Raw: tftypes.NewValue(resourceSchema.Type().TerraformType(ctx), nil),
Schema: tfsdkSchema(resourceSchema),
},
ID: proto5.ID,
ResourceType: resourceType,
TypeName: proto5.TypeName,
ID: proto5.ID,
Resource: resource,
TypeName: proto5.TypeName,
}

return fw, diags
Expand Down
6 changes: 3 additions & 3 deletions internal/fromproto5/importresourcestate_test.go
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/internal/fromproto5"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
"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"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
Expand All @@ -36,7 +36,7 @@ func TestImportResourceStateRequest(t *testing.T) {
testCases := map[string]struct {
input *tfprotov5.ImportResourceStateRequest
resourceSchema fwschema.Schema
resourceType provider.ResourceType
resource resource.Resource
expected *fwserver.ImportResourceStateRequest
expectedDiagnostics diag.Diagnostics
}{
Expand Down Expand Up @@ -92,7 +92,7 @@ func TestImportResourceStateRequest(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()

got, diags := fromproto5.ImportResourceStateRequest(context.Background(), testCase.input, testCase.resourceType, testCase.resourceSchema)
got, diags := fromproto5.ImportResourceStateRequest(context.Background(), testCase.input, testCase.resource, testCase.resourceSchema)

if diff := cmp.Diff(got, testCase.expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
Expand Down
6 changes: 3 additions & 3 deletions internal/fromproto5/planresourcechange.go
Expand Up @@ -9,12 +9,12 @@ import (
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
"github.com/hashicorp/terraform-plugin-framework/internal/privatestate"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/resource"
)

// PlanResourceChangeRequest returns the *fwserver.PlanResourceChangeRequest
// equivalent of a *tfprotov5.PlanResourceChangeRequest.
func PlanResourceChangeRequest(ctx context.Context, proto5 *tfprotov5.PlanResourceChangeRequest, resourceType provider.ResourceType, resourceSchema fwschema.Schema, providerMetaSchema fwschema.Schema) (*fwserver.PlanResourceChangeRequest, diag.Diagnostics) {
func PlanResourceChangeRequest(ctx context.Context, proto5 *tfprotov5.PlanResourceChangeRequest, resource resource.Resource, resourceSchema fwschema.Schema, providerMetaSchema fwschema.Schema) (*fwserver.PlanResourceChangeRequest, diag.Diagnostics) {
if proto5 == nil {
return nil, nil
}
Expand All @@ -37,7 +37,7 @@ func PlanResourceChangeRequest(ctx context.Context, proto5 *tfprotov5.PlanResour

fw := &fwserver.PlanResourceChangeRequest{
ResourceSchema: resourceSchema,
ResourceType: resourceType,
Resource: resource,
}

config, configDiags := Config(ctx, proto5.Config, resourceSchema)
Expand Down
6 changes: 3 additions & 3 deletions internal/fromproto5/planresourcechange_test.go
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
"github.com/hashicorp/terraform-plugin-framework/internal/privatestate"
"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"
)
Expand Down Expand Up @@ -55,7 +55,7 @@ func TestPlanResourceChangeRequest(t *testing.T) {
testCases := map[string]struct {
input *tfprotov5.PlanResourceChangeRequest
resourceSchema fwschema.Schema
resourceType provider.ResourceType
resource resource.Resource
providerMetaSchema fwschema.Schema
expected *fwserver.PlanResourceChangeRequest
expectedDiagnostics diag.Diagnostics
Expand Down Expand Up @@ -223,7 +223,7 @@ func TestPlanResourceChangeRequest(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()

got, diags := fromproto5.PlanResourceChangeRequest(context.Background(), testCase.input, testCase.resourceType, testCase.resourceSchema, testCase.providerMetaSchema)
got, diags := fromproto5.PlanResourceChangeRequest(context.Background(), testCase.input, testCase.resource, testCase.resourceSchema, testCase.providerMetaSchema)

if diff := cmp.Diff(got, testCase.expected, cmp.AllowUnexported(privatestate.ProviderData{})); diff != "" {
t.Errorf("unexpected difference: %s", diff)
Expand Down
6 changes: 3 additions & 3 deletions internal/fromproto5/readdatasource.go
Expand Up @@ -3,16 +3,16 @@ package fromproto5
import (
"context"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
)

// ReadDataSourceRequest returns the *fwserver.ReadDataSourceRequest
// equivalent of a *tfprotov5.ReadDataSourceRequest.
func ReadDataSourceRequest(ctx context.Context, proto5 *tfprotov5.ReadDataSourceRequest, dataSourceType provider.DataSourceType, dataSourceSchema fwschema.Schema, providerMetaSchema fwschema.Schema) (*fwserver.ReadDataSourceRequest, diag.Diagnostics) {
func ReadDataSourceRequest(ctx context.Context, proto5 *tfprotov5.ReadDataSourceRequest, dataSource datasource.DataSource, dataSourceSchema fwschema.Schema, providerMetaSchema fwschema.Schema) (*fwserver.ReadDataSourceRequest, diag.Diagnostics) {
if proto5 == nil {
return nil, nil
}
Expand All @@ -34,8 +34,8 @@ func ReadDataSourceRequest(ctx context.Context, proto5 *tfprotov5.ReadDataSource
}

fw := &fwserver.ReadDataSourceRequest{
DataSource: dataSource,
DataSourceSchema: dataSourceSchema,
DataSourceType: dataSourceType,
}

config, configDiags := Config(ctx, proto5.Config, dataSourceSchema)
Expand Down