Skip to content

Commit 67050dd

Browse files
committed
internal/generic: New Go packages: hashicorp/terraform-plugin-framework#432.
1 parent 5b17afe commit 67050dd

File tree

5 files changed

+65
-59
lines changed

5 files changed

+65
-59
lines changed

internal/generic/data_source.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import (
77
)
88

99
// DataSourceTypeOptionsFunc is a type alias for a data source type functional option.
10-
type DataSourceTypeOptionsFunc func(*dataSourceType) error
10+
type DataSourceTypeOptionsFunc func(*genericDataSourceType) error
1111

1212
// dataSourceWithAttributeNameMap is a helper function to construct functional options
1313
// that set a data source type's attribute name maps.
1414
// If multiple dataSourceWithAttributeNameMap calls are made, the last call overrides
1515
// the previous calls' values.
1616
func dataSourceWithAttributeNameMap(v map[string]string) DataSourceTypeOptionsFunc {
17-
return func(o *dataSourceType) error {
17+
return func(o *genericDataSourceType) error {
1818
if _, ok := v["id"]; !ok {
1919
// Synthesize a mapping for the reserved top-level "id" attribute.
2020
v["id"] = "ID"
@@ -41,7 +41,7 @@ func dataSourceWithAttributeNameMap(v map[string]string) DataSourceTypeOptionsFu
4141
// If multiple dataSourceWithCloudFormationTypeName calls are made, the last call overrides
4242
// the previous calls' values.
4343
func dataSourceWithCloudFormationTypeName(v string) DataSourceTypeOptionsFunc {
44-
return func(o *dataSourceType) error {
44+
return func(o *genericDataSourceType) error {
4545
o.cfTypeName = v
4646

4747
return nil
@@ -53,7 +53,7 @@ func dataSourceWithCloudFormationTypeName(v string) DataSourceTypeOptionsFunc {
5353
// If multiple dataSourceWithTerraformSchema calls are made, the last call overrides
5454
// the previous calls' values.
5555
func dataSourceWithTerraformSchema(v tfsdk.Schema) DataSourceTypeOptionsFunc {
56-
return func(o *dataSourceType) error {
56+
return func(o *genericDataSourceType) error {
5757
o.tfSchema = v
5858

5959
return nil
@@ -65,7 +65,7 @@ func dataSourceWithTerraformSchema(v tfsdk.Schema) DataSourceTypeOptionsFunc {
6565
// If multiple dataSourceWithTerraformTypeName calls are made, the last call overrides
6666
// the previous calls' values.
6767
func dataSourceWithTerraformTypeName(v string) DataSourceTypeOptionsFunc {
68-
return func(o *dataSourceType) error {
68+
return func(o *genericDataSourceType) error {
6969
o.tfTypeName = v
7070

7171
return nil
@@ -106,8 +106,8 @@ func (opts DataSourceTypeOptions) WithTerraformTypeName(v string) DataSourceType
106106
return append(opts, dataSourceWithTerraformTypeName(v))
107107
}
108108

109-
// dataSourceType implements tfsdk.DataSourceType
110-
type dataSourceType struct {
109+
// genericDataSourceType implements provider.DataSourceType
110+
type genericDataSourceType struct {
111111
cfToTfNameMap map[string]string // Map of CloudFormation property name to Terraform attribute name
112112
cfTypeName string // CloudFormation type name for the resource type
113113
tfSchema tfsdk.Schema // Terraform schema for the data source type

internal/generic/plural_data_source.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,22 @@ import (
88
"github.com/aws/aws-sdk-go-v2/service/cloudcontrol"
99
cctypes "github.com/aws/aws-sdk-go-v2/service/cloudcontrol/types"
1010
"github.com/hashicorp/go-hclog"
11+
"github.com/hashicorp/terraform-plugin-framework/datasource"
1112
"github.com/hashicorp/terraform-plugin-framework/diag"
13+
"github.com/hashicorp/terraform-plugin-framework/provider"
1214
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
1315
"github.com/hashicorp/terraform-plugin-go/tftypes"
1416
"github.com/hashicorp/terraform-plugin-log/tflog"
1517
tfcloudcontrol "github.com/hashicorp/terraform-provider-awscc/internal/service/cloudcontrol"
1618
)
1719

1820
// pluralDataSourceType is a type alias for a data source type.
19-
type pluralDataSourceType dataSourceType
21+
type pluralDataSourceType genericDataSourceType
2022

2123
// NewPluralDataSourceType returns a new pluralDataSourceType from the specified variadic list of functional options.
2224
// It's public as it's called from generated code.
23-
func NewPluralDataSourceType(_ context.Context, optFns ...DataSourceTypeOptionsFunc) (tfsdk.DataSourceType, error) {
24-
dataSourceType := &dataSourceType{}
25+
func NewPluralDataSourceType(_ context.Context, optFns ...DataSourceTypeOptionsFunc) (provider.DataSourceType, error) {
26+
dataSourceType := &genericDataSourceType{}
2527

2628
for _, optFn := range optFns {
2729
err := optFn(dataSourceType)
@@ -48,24 +50,24 @@ func (pdt *pluralDataSourceType) GetSchema(ctx context.Context) (tfsdk.Schema, d
4850
return pdt.tfSchema, nil
4951
}
5052

51-
func (pdt *pluralDataSourceType) NewDataSource(ctx context.Context, provider tfsdk.Provider) (tfsdk.DataSource, diag.Diagnostics) {
53+
func (pdt *pluralDataSourceType) NewDataSource(ctx context.Context, provider provider.Provider) (datasource.DataSource, diag.Diagnostics) {
5254
return newGenericPluralDataSource(provider, pdt), nil
5355
}
5456

55-
// Implements tfsdk.DataSource
57+
// Implements datasource.DataSource
5658
type pluralDataSource struct {
5759
provider tfcloudcontrol.Provider
5860
dataSourceType *pluralDataSourceType
5961
}
6062

61-
func newGenericPluralDataSource(provider tfsdk.Provider, pluralDataSourceType *pluralDataSourceType) tfsdk.DataSource {
63+
func newGenericPluralDataSource(provider provider.Provider, pluralDataSourceType *pluralDataSourceType) datasource.DataSource {
6264
return &pluralDataSource{
6365
provider: provider.(tfcloudcontrol.Provider),
6466
dataSourceType: pluralDataSourceType,
6567
}
6668
}
6769

68-
func (pd *pluralDataSource) Read(ctx context.Context, _ tfsdk.ReadDataSourceRequest, response *tfsdk.ReadDataSourceResponse) {
70+
func (pd *pluralDataSource) Read(ctx context.Context, _ datasource.ReadRequest, response *datasource.ReadResponse) {
6971
ctx = pd.cfnTypeContext(ctx)
7072

7173
traceEntry(ctx, "PluralDataSource.Read")

internal/generic/resource.go

+39-37
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
hclog "github.com/hashicorp/go-hclog"
1414
"github.com/hashicorp/terraform-plugin-framework/diag"
1515
"github.com/hashicorp/terraform-plugin-framework/path"
16+
"github.com/hashicorp/terraform-plugin-framework/provider"
17+
"github.com/hashicorp/terraform-plugin-framework/resource"
1618
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
1719
"github.com/hashicorp/terraform-plugin-log/tflog"
1820
tfcloudcontrol "github.com/hashicorp/terraform-provider-awscc/internal/service/cloudcontrol"
@@ -22,14 +24,14 @@ import (
2224
)
2325

2426
// ResourceTypeOptionsFunc is a type alias for a resource type functional option.
25-
type ResourceTypeOptionsFunc func(*resourceType) error
27+
type ResourceTypeOptionsFunc func(*genericResourceType) error
2628

2729
// resourceWithAttributeNameMap is a helper function to construct functional options
2830
// that set a resource type's attribute name maps.
2931
// If multiple resourceWithAttributeNameMap calls are made, the last call overrides
3032
// the previous calls' values.
3133
func resourceWithAttributeNameMap(v map[string]string) ResourceTypeOptionsFunc {
32-
return func(o *resourceType) error {
34+
return func(o *genericResourceType) error {
3335
if _, ok := v["id"]; !ok {
3436
// Synthesize a mapping for the reserved top-level "id" attribute.
3537
v["id"] = "ID"
@@ -57,7 +59,7 @@ func resourceWithAttributeNameMap(v map[string]string) ResourceTypeOptionsFunc {
5759
// If multiple resourceWithCloudFormationTypeName calls are made, the last call overrides
5860
// the previous calls' values.
5961
func resourceWithCloudFormationTypeName(v string) ResourceTypeOptionsFunc {
60-
return func(o *resourceType) error {
62+
return func(o *genericResourceType) error {
6163
o.cfTypeName = v
6264

6365
return nil
@@ -69,7 +71,7 @@ func resourceWithCloudFormationTypeName(v string) ResourceTypeOptionsFunc {
6971
// If multiple resourceWithTerraformSchema calls are made, the last call overrides
7072
// the previous calls' values.
7173
func resourceWithTerraformSchema(v tfsdk.Schema) ResourceTypeOptionsFunc {
72-
return func(o *resourceType) error {
74+
return func(o *genericResourceType) error {
7375
o.tfSchema = v
7476

7577
return nil
@@ -81,7 +83,7 @@ func resourceWithTerraformSchema(v tfsdk.Schema) ResourceTypeOptionsFunc {
8183
// If multiple resourceWithTerraformTypeName calls are made, the last call overrides
8284
// the previous calls' values.
8385
func resourceWithTerraformTypeName(v string) ResourceTypeOptionsFunc {
84-
return func(o *resourceType) error {
86+
return func(o *genericResourceType) error {
8587
o.tfTypeName = v
8688

8789
return nil
@@ -93,7 +95,7 @@ func resourceWithTerraformTypeName(v string) ResourceTypeOptionsFunc {
9395
// If multiple resourceIsImmutableType calls are made, the last call overrides
9496
// the previous calls' values.
9597
func resourceIsImmutableType(v bool) ResourceTypeOptionsFunc {
96-
return func(o *resourceType) error {
98+
return func(o *genericResourceType) error {
9799
o.isImmutableType = v
98100

99101
return nil
@@ -105,7 +107,7 @@ func resourceIsImmutableType(v bool) ResourceTypeOptionsFunc {
105107
// If multiple resourceWithSyntheticIDAttribute calls are made, the last call overrides
106108
// the previous calls' values.
107109
func resourceWithSyntheticIDAttribute(v bool) ResourceTypeOptionsFunc {
108-
return func(o *resourceType) error {
110+
return func(o *genericResourceType) error {
109111
o.syntheticIDAttribute = v
110112

111113
return nil
@@ -117,7 +119,7 @@ func resourceWithSyntheticIDAttribute(v bool) ResourceTypeOptionsFunc {
117119
// If multiple resourceWithWriteOnlyPropertyPaths calls are made, the last call overrides
118120
// the previous calls' values.
119121
func resourceWithWriteOnlyPropertyPaths(v []string) ResourceTypeOptionsFunc {
120-
return func(o *resourceType) error {
122+
return func(o *genericResourceType) error {
121123
writeOnlyAttributePaths := make([]*path.Path, 0)
122124

123125
for _, writeOnlyPropertyPath := range v {
@@ -147,7 +149,7 @@ const (
147149
// If multiple resourceWithCreateTimeoutInMinutes calls are made, the last call overrides
148150
// the previous calls' values.
149151
func resourceWithCreateTimeoutInMinutes(v int) ResourceTypeOptionsFunc {
150-
return func(o *resourceType) error {
152+
return func(o *genericResourceType) error {
151153
if v > 0 {
152154
o.createTimeout = time.Duration(v) * time.Minute
153155
} else {
@@ -163,7 +165,7 @@ func resourceWithCreateTimeoutInMinutes(v int) ResourceTypeOptionsFunc {
163165
// If multiple resourceWithUpdateTimeoutInMinutes calls are made, the last call overrides
164166
// the previous calls' values.
165167
func resourceWithUpdateTimeoutInMinutes(v int) ResourceTypeOptionsFunc {
166-
return func(o *resourceType) error {
168+
return func(o *genericResourceType) error {
167169
if v > 0 {
168170
o.updateTimeout = time.Duration(v) * time.Minute
169171
} else {
@@ -179,7 +181,7 @@ func resourceWithUpdateTimeoutInMinutes(v int) ResourceTypeOptionsFunc {
179181
// If multiple resourceWithDeleteTimeoutInMinutes calls are made, the last call overrides
180182
// the previous calls' values.
181183
func resourceWithDeleteTimeoutInMinutes(v int) ResourceTypeOptionsFunc {
182-
return func(o *resourceType) error {
184+
return func(o *genericResourceType) error {
183185
if v > 0 {
184186
o.deleteTimeout = time.Duration(v) * time.Minute
185187
} else {
@@ -195,7 +197,7 @@ func resourceWithDeleteTimeoutInMinutes(v int) ResourceTypeOptionsFunc {
195197
// If multiple resourceWithRequiredAttributesValidators calls are made, the last call overrides
196198
// the previous calls' values.
197199
func resourceWithRequiredAttributesValidators(fs ...validate.RequiredAttributesFunc) ResourceTypeOptionsFunc {
198-
return func(o *resourceType) error {
200+
return func(o *genericResourceType) error {
199201
o.requiredAttributesValidators = fs
200202

201203
return nil
@@ -293,8 +295,8 @@ func (opts ResourceTypeOptions) WithRequiredAttributesValidators(v ...validate.R
293295
return append(opts, resourceWithRequiredAttributesValidators(v...))
294296
}
295297

296-
// resourceType implements tfsdk.ResourceType.
297-
type resourceType struct {
298+
// genericResourceType implements provider.ResourceType.
299+
type genericResourceType struct {
298300
cfTypeName string // CloudFormation type name for the resource type
299301
tfSchema tfsdk.Schema // Terraform schema for the resource type
300302
tfTypeName string // Terraform type name for resource type
@@ -311,8 +313,8 @@ type resourceType struct {
311313

312314
// NewResourceType returns a new ResourceType from the specified varidaic list of functional options.
313315
// It's public as it's called from generated code.
314-
func NewResourceType(_ context.Context, optFns ...ResourceTypeOptionsFunc) (tfsdk.ResourceType, error) {
315-
resourceType := &resourceType{}
316+
func NewResourceType(_ context.Context, optFns ...ResourceTypeOptionsFunc) (provider.ResourceType, error) {
317+
resourceType := &genericResourceType{}
316318

317319
for _, optFn := range optFns {
318320
err := optFn(resourceType)
@@ -332,16 +334,16 @@ func NewResourceType(_ context.Context, optFns ...ResourceTypeOptionsFunc) (tfsd
332334
return resourceType, nil
333335
}
334336

335-
func (rt *resourceType) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
337+
func (rt *genericResourceType) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
336338
return rt.tfSchema, nil
337339
}
338340

339-
func (rt *resourceType) NewResource(ctx context.Context, provider tfsdk.Provider) (tfsdk.Resource, diag.Diagnostics) {
341+
func (rt *genericResourceType) NewResource(ctx context.Context, provider provider.Provider) (resource.Resource, diag.Diagnostics) {
340342
return newGenericResource(provider, rt), nil
341343
}
342344

343345
// propertyPathToAttributePath returns the AttributePath for the specified JSON Pointer property path.
344-
func (rt *resourceType) propertyPathToAttributePath(propertyPath string) (*path.Path, error) {
346+
func (rt *genericResourceType) propertyPathToAttributePath(propertyPath string) (*path.Path, error) {
345347
segments := strings.Split(propertyPath, "/")
346348

347349
if got, expected := len(segments), 3; got < expected {
@@ -375,14 +377,14 @@ func (rt *resourceType) propertyPathToAttributePath(propertyPath string) (*path.
375377
return &attributePath, nil
376378
}
377379

378-
// Implements tfsdk.Resource.
379-
type resource struct {
380+
// Implements genericResource.Resource.
381+
type genericResource struct {
380382
provider tfcloudcontrol.Provider
381-
resourceType *resourceType
383+
resourceType *genericResourceType
382384
}
383385

384-
func newGenericResource(provider tfsdk.Provider, resourceType *resourceType) tfsdk.Resource {
385-
return &resource{
386+
func newGenericResource(provider provider.Provider, resourceType *genericResourceType) resource.Resource {
387+
return &genericResource{
386388
provider: provider.(tfcloudcontrol.Provider),
387389
resourceType: resourceType,
388390
}
@@ -394,7 +396,7 @@ var (
394396
idAttributePath = path.Root("id")
395397
)
396398

397-
func (r *resource) Create(ctx context.Context, request tfsdk.CreateResourceRequest, response *tfsdk.CreateResourceResponse) {
399+
func (r *genericResource) Create(ctx context.Context, request resource.CreateRequest, response *resource.CreateResponse) {
398400
ctx = r.cfnTypeContext(ctx)
399401

400402
traceEntry(ctx, "Resource.Create")
@@ -495,7 +497,7 @@ func (r *resource) Create(ctx context.Context, request tfsdk.CreateResourceReque
495497
traceExit(ctx, "Resource.Create")
496498
}
497499

498-
func (r *resource) Read(ctx context.Context, request tfsdk.ReadResourceRequest, response *tfsdk.ReadResourceResponse) {
500+
func (r *genericResource) Read(ctx context.Context, request resource.ReadRequest, response *resource.ReadResponse) {
499501
ctx = r.cfnTypeContext(ctx)
500502

501503
traceEntry(ctx, "Resource.Read")
@@ -581,7 +583,7 @@ func (r *resource) Read(ctx context.Context, request tfsdk.ReadResourceRequest,
581583
traceExit(ctx, "Resource.Read")
582584
}
583585

584-
func (r *resource) Update(ctx context.Context, request tfsdk.UpdateResourceRequest, response *tfsdk.UpdateResourceResponse) {
586+
func (r *genericResource) Update(ctx context.Context, request resource.UpdateRequest, response *resource.UpdateResponse) {
585587
ctx = r.cfnTypeContext(ctx)
586588

587589
traceEntry(ctx, "Resource.Update")
@@ -680,7 +682,7 @@ func (r *resource) Update(ctx context.Context, request tfsdk.UpdateResourceReque
680682
traceExit(ctx, "Resource.Update")
681683
}
682684

683-
func (r *resource) Delete(ctx context.Context, request tfsdk.DeleteResourceRequest, response *tfsdk.DeleteResourceResponse) {
685+
func (r *genericResource) Delete(ctx context.Context, request resource.DeleteRequest, response *resource.DeleteResponse) {
684686
ctx = r.cfnTypeContext(ctx)
685687

686688
traceEntry(ctx, "Resource.Delete")
@@ -708,7 +710,7 @@ func (r *resource) Delete(ctx context.Context, request tfsdk.DeleteResourceReque
708710
traceExit(ctx, "Resource.Delete")
709711
}
710712

711-
func (r *resource) ImportState(ctx context.Context, request tfsdk.ImportResourceStateRequest, response *tfsdk.ImportResourceStateResponse) {
713+
func (r *genericResource) ImportState(ctx context.Context, request resource.ImportStateRequest, response *resource.ImportStateResponse) {
712714
ctx = r.cfnTypeContext(ctx)
713715

714716
traceEntry(ctx, "Resource.ImportState")
@@ -717,14 +719,14 @@ func (r *resource) ImportState(ctx context.Context, request tfsdk.ImportResource
717719
"value": hclog.Fmt("%v", request.ID),
718720
})
719721

720-
tfsdk.ResourceImportStatePassthroughID(ctx, idAttributePath, request, response)
722+
resource.ImportStatePassthroughID(ctx, idAttributePath, request, response)
721723

722724
traceExit(ctx, "Resource.ImportState")
723725
}
724726

725727
// ConfigValidators returns a list of functions which will all be performed during validation.
726-
func (r *resource) ConfigValidators(context.Context) []tfsdk.ResourceConfigValidator {
727-
validators := make([]tfsdk.ResourceConfigValidator, 0)
728+
func (r *genericResource) ConfigValidators(context.Context) []resource.ConfigValidator {
729+
validators := make([]resource.ConfigValidator, 0)
728730

729731
if len(r.resourceType.requiredAttributesValidators) > 0 {
730732
validators = append(validators, validate.ResourceConfigRequiredAttributes(r.resourceType.requiredAttributesValidators...))
@@ -734,12 +736,12 @@ func (r *resource) ConfigValidators(context.Context) []tfsdk.ResourceConfigValid
734736
}
735737

736738
// describe returns the live state of the specified resource.
737-
func (r *resource) describe(ctx context.Context, conn *cloudcontrol.Client, id string) (*cctypes.ResourceDescription, error) {
739+
func (r *genericResource) describe(ctx context.Context, conn *cloudcontrol.Client, id string) (*cctypes.ResourceDescription, error) {
738740
return tfcloudcontrol.FindResourceByTypeNameAndID(ctx, conn, r.provider.RoleARN(ctx), r.resourceType.cfTypeName, id)
739741
}
740742

741743
// getId returns the resource's primary identifier value from State.
742-
func (r *resource) getId(ctx context.Context, state *tfsdk.State) (string, error) {
744+
func (r *genericResource) getId(ctx context.Context, state *tfsdk.State) (string, error) {
743745
var val string
744746
diags := state.GetAttribute(ctx, idAttributePath, &val)
745747

@@ -751,7 +753,7 @@ func (r *resource) getId(ctx context.Context, state *tfsdk.State) (string, error
751753
}
752754

753755
// setId sets the resource's primary identifier value in State.
754-
func (r *resource) setId(ctx context.Context, val string, state *tfsdk.State) error {
756+
func (r *genericResource) setId(ctx context.Context, val string, state *tfsdk.State) error {
755757
diags := state.SetAttribute(ctx, idAttributePath, val)
756758

757759
if diags.HasError() {
@@ -762,7 +764,7 @@ func (r *resource) setId(ctx context.Context, val string, state *tfsdk.State) er
762764
}
763765

764766
// populateUnknownValues populates and unknown values in State with values from the current resource description.
765-
func (r *resource) populateUnknownValues(ctx context.Context, id string, state *tfsdk.State) diag.Diagnostics {
767+
func (r *genericResource) populateUnknownValues(ctx context.Context, id string, state *tfsdk.State) diag.Diagnostics {
766768
var diags diag.Diagnostics
767769

768770
unknowns, err := UnknownValuePaths(ctx, state.Raw)
@@ -815,7 +817,7 @@ func (r *resource) populateUnknownValues(ctx context.Context, id string, state *
815817
}
816818

817819
// cfnTypeContext injects the CloudFormation type name into logger contexts.
818-
func (r *resource) cfnTypeContext(ctx context.Context) context.Context {
820+
func (r *genericResource) cfnTypeContext(ctx context.Context) context.Context {
819821
ctx = tflog.SetField(ctx, LoggingKeyCFNType, r.resourceType.cfTypeName)
820822

821823
return ctx

0 commit comments

Comments
 (0)