Skip to content

Commit

Permalink
feat(fw): custom data source, resource interface extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
jar-b committed Dec 1, 2022
1 parent 68fcd28 commit 9527228
Show file tree
Hide file tree
Showing 27 changed files with 106 additions and 83 deletions.
5 changes: 3 additions & 2 deletions internal/acctest/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/hashicorp/terraform-provider-aws/internal/errs/fwdiag"
"github.com/hashicorp/terraform-provider-aws/internal/experimental/intf"
)

// Terraform Plugin Framework variants of standard acceptance test helpers.

func DeleteFrameworkResource(factory func(context.Context) (fwresource.ResourceWithConfigure, error), is *terraform.InstanceState, meta interface{}) error {
func DeleteFrameworkResource(factory func(context.Context) (intf.FrameworkResource, error), is *terraform.InstanceState, meta interface{}) error {
ctx := context.Background()

resource, err := factory(ctx)
Expand Down Expand Up @@ -61,7 +62,7 @@ func DeleteFrameworkResource(factory func(context.Context) (fwresource.ResourceW
return nil
}

func CheckFrameworkResourceDisappears(provo *schema.Provider, factory func(context.Context) (fwresource.ResourceWithConfigure, error), n string) resource.TestCheckFunc {
func CheckFrameworkResourceDisappears(provo *schema.Provider, factory func(context.Context) (intf.FrameworkResource, error), n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
Expand Down
24 changes: 22 additions & 2 deletions internal/experimental/intf/service_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,30 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource"
)

// FrameworkDataSource is a custom interface for framework data sources in the AWS provider.
// This interface extends the base DataSource interface with the following:
// - datasource.DataSourceWithConfigure
// - datasource.DataSourceWithGetSchema
type FrameworkDataSource interface {
datasource.DataSourceWithConfigure
// TODO: migrate to datasource.DataSourceWithSchema (GetSchema is deprecated)
datasource.DataSourceWithGetSchema
}

// FrameworkResource is a custom interface for framework resources in the AWS provider.
// This interface extends the base Resource interface with the following:
// - resource.ResourceWithConfigure
// - resource.ResourceWithGetSchema
type FrameworkResource interface {
resource.ResourceWithConfigure
// TODO: migrate to resource.ResourceWithSchema (GetSchema is deprecated)
resource.ResourceWithGetSchema
}

// ServicePackageData is data about a service package.
type ServicePackageData interface {
Configure(context.Context, any) error
FrameworkDataSources(context.Context) []func(context.Context) (datasource.DataSourceWithConfigure, error)
FrameworkResources(context.Context) []func(context.Context) (resource.ResourceWithConfigure, error)
FrameworkDataSources(context.Context) []func(context.Context) (FrameworkDataSource, error)
FrameworkResources(context.Context) []func(context.Context) (FrameworkResource, error)
ServicePackageName() string
}
14 changes: 6 additions & 8 deletions internal/generate/servicepackagedata/file.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,33 @@ package {{ .PackageName }}
import (
"context"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-provider-aws/internal/experimental/intf"
)

var spd = &servicePackageData{}

func registerFrameworkDataSourceFactory(factory func(context.Context) (datasource.DataSourceWithConfigure, error)) {
func registerFrameworkDataSourceFactory(factory func(context.Context) (intf.FrameworkDataSource, error)) {
spd.frameworkDataSourceFactories = append(spd.frameworkDataSourceFactories, factory)
}

func registerFrameworkResourceFactory(factory func(context.Context) (resource.ResourceWithConfigure, error)) {
func registerFrameworkResourceFactory(factory func(context.Context) (intf.FrameworkResource, error)) {
spd.frameworkResourceFactories = append(spd.frameworkResourceFactories, factory)
}

type servicePackageData struct {
frameworkDataSourceFactories []func(context.Context) (datasource.DataSourceWithConfigure, error)
frameworkResourceFactories []func(context.Context) (resource.ResourceWithConfigure, error)
frameworkDataSourceFactories []func(context.Context) (intf.FrameworkDataSource, error)
frameworkResourceFactories []func(context.Context) (intf.FrameworkResource, error)
}

func (d *servicePackageData) Configure(ctx context.Context, meta any) error {
return nil
}

func (d *servicePackageData) FrameworkDataSources(ctx context.Context) []func(context.Context) (datasource.DataSourceWithConfigure, error) {
func (d *servicePackageData) FrameworkDataSources(ctx context.Context) []func(context.Context) (intf.FrameworkDataSource, error) {
return d.frameworkDataSourceFactories
}

func (d *servicePackageData) FrameworkResources(ctx context.Context) []func(context.Context) (resource.ResourceWithConfigure, error) {
func (d *servicePackageData) FrameworkResources(ctx context.Context) []func(context.Context) (intf.FrameworkResource, error) {
return d.frameworkResourceFactories
}

Expand Down
9 changes: 5 additions & 4 deletions internal/provider/fwprovider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/experimental/intf"
fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types"
"github.com/hashicorp/terraform-provider-aws/names"
)
Expand Down Expand Up @@ -391,11 +392,11 @@ func endpointsBlock() tfsdk.Block {

// wrappedDataSource wraps a data source, adding common functionality.
type wrappedDataSource struct {
inner datasource.DataSourceWithConfigure
inner intf.FrameworkDataSource
typeName string
}

func newWrappedDataSource(inner datasource.DataSourceWithConfigure) datasource.DataSourceWithConfigure {
func newWrappedDataSource(inner intf.FrameworkDataSource) intf.FrameworkDataSource {
return &wrappedDataSource{inner: inner, typeName: strings.TrimPrefix(reflect.TypeOf(inner).String(), "*")}
}

Expand All @@ -421,11 +422,11 @@ func (w *wrappedDataSource) Configure(ctx context.Context, request datasource.Co

// wrappedResource wraps a resource, adding common functionality.
type wrappedResource struct {
inner resource.ResourceWithConfigure
inner intf.FrameworkResource
typeName string
}

func newWrappedResource(inner resource.ResourceWithConfigure) resource.ResourceWithConfigure {
func newWrappedResource(inner intf.FrameworkResource) intf.FrameworkResource {
return &wrappedResource{inner: inner, typeName: strings.TrimPrefix(reflect.TypeOf(inner).String(), "*")}
}

Expand Down
14 changes: 6 additions & 8 deletions internal/service/ec2/service_package_data_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion internal/service/ec2/vpc_security_group_egress_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-provider-aws/internal/experimental/intf"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
)

Expand All @@ -14,7 +15,7 @@ func init() {
}

// newResourceSecurityGroupEgressRule instantiates a new Resource for the aws_vpc_security_group_egress_rule resource.
func newResourceSecurityGroupEgressRule(context.Context) (resource.ResourceWithConfigure, error) {
func newResourceSecurityGroupEgressRule(context.Context) (intf.FrameworkResource, error) {
return &resourceSecurityGroupEgressRule{}, nil
}

Expand Down
3 changes: 2 additions & 1 deletion internal/service/ec2/vpc_security_group_ingress_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-provider-aws/internal/experimental/intf"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
"github.com/hashicorp/terraform-provider-aws/internal/framework"
fwvalidators "github.com/hashicorp/terraform-provider-aws/internal/framework/validators"
Expand All @@ -29,7 +30,7 @@ func init() {
}

// newResourceSecurityGroupIngressRule instantiates a new Resource for the aws_vpc_security_group_ingress_rule resource.
func newResourceSecurityGroupIngressRule(context.Context) (resource.ResourceWithConfigure, error) {
func newResourceSecurityGroupIngressRule(context.Context) (intf.FrameworkResource, error) {
return &resourceSecurityGroupIngressRule{}, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-provider-aws/internal/experimental/intf"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
"github.com/hashicorp/terraform-provider-aws/internal/framework"
fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types"
Expand All @@ -22,7 +23,7 @@ func init() {
}

// newDataSourceAccelerator instantiates a new DataSource for the aws_globalaccelerator_accelerator data source.
func newDataSourceAccelerator(context.Context) (datasource.DataSourceWithConfigure, error) {
func newDataSourceAccelerator(context.Context) (intf.FrameworkDataSource, error) {
return &dataSourceAccelerator{}, nil
}

Expand Down
14 changes: 6 additions & 8 deletions internal/service/globalaccelerator/service_package_data_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion internal/service/medialive/multiplex_program.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
resourceHelper "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/create"
"github.com/hashicorp/terraform-provider-aws/internal/enum"
"github.com/hashicorp/terraform-provider-aws/internal/experimental/intf"
"github.com/hashicorp/terraform-provider-aws/internal/framework"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
Expand All @@ -27,7 +28,7 @@ func init() {
registerFrameworkResourceFactory(newResourceMultiplexProgram)
}

func newResourceMultiplexProgram(_ context.Context) (resource.ResourceWithConfigure, error) {
func newResourceMultiplexProgram(_ context.Context) (intf.FrameworkResource, error) {
return &multiplexProgram{}, nil
}

Expand Down
14 changes: 6 additions & 8 deletions internal/service/medialive/service_package_data_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion internal/service/meta/arn_data_source.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion internal/service/meta/billing_service_account_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-provider-aws/internal/experimental/intf"
"github.com/hashicorp/terraform-provider-aws/internal/framework"
)

Expand All @@ -16,7 +17,7 @@ func init() {
}

// newDataSourceBillingServiceAccount instantiates a new DataSource for the aws_billing_service_account data source.
func newDataSourceBillingServiceAccount(context.Context) (datasource.DataSourceWithConfigure, error) {
func newDataSourceBillingServiceAccount(context.Context) (intf.FrameworkDataSource, error) {
return &dataSourceBillingServiceAccount{}, nil
}

Expand Down
3 changes: 2 additions & 1 deletion internal/service/meta/default_tags_data_source.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion internal/service/meta/ip_ranges_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-provider-aws/internal/experimental/intf"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
"github.com/hashicorp/terraform-provider-aws/internal/framework"
tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices"
Expand All @@ -26,7 +27,7 @@ func init() {
}

// newDataSourceIPRanges instantiates a new DataSource for the aws_ip_ranges data source.
func newDataSourceIPRanges(context.Context) (datasource.DataSourceWithConfigure, error) {
func newDataSourceIPRanges(context.Context) (intf.FrameworkDataSource, error) {
return &dataSourceIPRanges{}, nil
}

Expand Down
Loading

0 comments on commit 9527228

Please sign in to comment.