Skip to content

Commit e2cfbd1

Browse files
authored
Merge pull request #43495 from hashicorp/td-resource-identity-custom-import
Resource Identity: Passes Identity Spec to SDKv2 custom imports
2 parents a4b5a20 + 3c211ae commit e2cfbd1

22 files changed

+260
-83
lines changed

internal/generate/servicepackage/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ type ResourceDatum struct {
181181
SingletonIdentity bool
182182
MutableIdentity bool
183183
WrappedImport bool
184+
CustomImport bool
184185
goImports []goImport
185186
IdentityDuplicateAttrs []string
186187
ImportIDHandler string
@@ -412,6 +413,9 @@ func (v *visitor) processFuncDecl(funcDecl *ast.FuncDecl) {
412413
}
413414
}
414415

416+
case "CustomImport":
417+
d.CustomImport = true
418+
415419
case "ArnIdentity":
416420
d.ARNIdentity = true
417421
d.WrappedImport = true
@@ -644,7 +648,7 @@ func (v *visitor) processFuncDecl(funcDecl *ast.FuncDecl) {
644648
v.sdkResources[typeName] = d
645649
}
646650

647-
case "IdentityAttribute", "ArnIdentity", "ImportIDHandler", "MutableIdentity", "SingletonIdentity", "Region", "Tags", "WrappedImport", "V60SDKv2Fix", "IdentityFix":
651+
case "IdentityAttribute", "ArnIdentity", "ImportIDHandler", "MutableIdentity", "SingletonIdentity", "Region", "Tags", "WrappedImport", "V60SDKv2Fix", "IdentityFix", "CustomImport":
648652
// Handled above.
649653
case "ArnFormat", "IdAttrFormat", "NoImport", "Testing":
650654
// Ignored.

internal/generate/servicepackage/service_package_gen.go.gtpl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,11 @@ func (p *servicePackage) FrameworkResources(ctx context.Context) []*inttypes.Ser
224224
{{- end }}
225225
{{- if $value.WrappedImport }}
226226
Import: inttypes.FrameworkImport{
227-
WrappedImport: true,
227+
{{- if $value.CustomImport }}
228+
CustomImport: true,
229+
{{- else }}
230+
WrappedImport: true,
231+
{{- end }}
228232
{{- if ne $value.ImportIDHandler "" }}
229233
ImportID: {{ $value.ImportIDHandler }}{},
230234
{{- end }}
@@ -424,7 +428,11 @@ func (p *servicePackage) SDKResources(ctx context.Context) []*inttypes.ServicePa
424428
{{- end }}
425429
{{- if $value.WrappedImport }}
426430
Import: inttypes.SDKv2Import{
427-
WrappedImport: true,
431+
{{- if $value.CustomImport }}
432+
CustomImport: true,
433+
{{- else }}
434+
WrappedImport: true,
435+
{{- end }}
428436
{{- if ne $value.ImportIDHandler "" }}
429437
ImportID: {{ $value.ImportIDHandler }}{},
430438
{{- end }}

internal/provider/sdkv2/identity_interceptor.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,10 @@ func newResourceIdentity(v inttypes.Identity) *schema.ResourceIdentity {
9696

9797
func newParameterizedIdentityImporter(identitySpec inttypes.Identity, importSpec *inttypes.SDKv2Import) *schema.ResourceImporter {
9898
if identitySpec.IsSingleParameter {
99-
attr := identitySpec.Attributes[len(identitySpec.Attributes)-1]
10099
if identitySpec.IsGlobalResource {
101100
return &schema.ResourceImporter{
102101
StateContext: func(ctx context.Context, rd *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
103-
if err := importer.GlobalSingleParameterized(ctx, rd, attr, meta.(importer.AWSClient)); err != nil {
102+
if err := importer.GlobalSingleParameterized(ctx, rd, identitySpec, meta.(importer.AWSClient)); err != nil {
104103
return nil, err
105104
}
106105

@@ -110,7 +109,7 @@ func newParameterizedIdentityImporter(identitySpec inttypes.Identity, importSpec
110109
} else {
111110
return &schema.ResourceImporter{
112111
StateContext: func(ctx context.Context, rd *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
113-
if err := importer.RegionalSingleParameterized(ctx, rd, attr, meta.(importer.AWSClient)); err != nil {
112+
if err := importer.RegionalSingleParameterized(ctx, rd, identitySpec, meta.(importer.AWSClient)); err != nil {
114113
return nil, err
115114
}
116115

@@ -122,7 +121,7 @@ func newParameterizedIdentityImporter(identitySpec inttypes.Identity, importSpec
122121
if identitySpec.IsGlobalResource {
123122
return &schema.ResourceImporter{
124123
StateContext: func(ctx context.Context, rd *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
125-
if err := importer.GlobalMultipleParameterized(ctx, rd, identitySpec.Attributes, importSpec, meta.(importer.AWSClient)); err != nil {
124+
if err := importer.GlobalMultipleParameterized(ctx, rd, identitySpec, importSpec, meta.(importer.AWSClient)); err != nil {
126125
return nil, err
127126
}
128127

@@ -132,7 +131,7 @@ func newParameterizedIdentityImporter(identitySpec inttypes.Identity, importSpec
132131
} else {
133132
return &schema.ResourceImporter{
134133
StateContext: func(ctx context.Context, rd *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
135-
if err := importer.RegionalMultipleParameterized(ctx, rd, identitySpec.Attributes, importSpec, meta.(importer.AWSClient)); err != nil {
134+
if err := importer.RegionalMultipleParameterized(ctx, rd, identitySpec, importSpec, meta.(importer.AWSClient)); err != nil {
136135
return nil, err
137136
}
138137

@@ -147,7 +146,7 @@ func arnIdentityResourceImporter(identity inttypes.Identity) *schema.ResourceImp
147146
if identity.IsGlobalResource {
148147
return &schema.ResourceImporter{
149148
StateContext: func(ctx context.Context, rd *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
150-
if err := importer.GlobalARN(ctx, rd, identity.IdentityAttribute, identity.IdentityDuplicateAttrs); err != nil {
149+
if err := importer.GlobalARN(ctx, rd, identity); err != nil {
151150
return nil, err
152151
}
153152

@@ -157,7 +156,7 @@ func arnIdentityResourceImporter(identity inttypes.Identity) *schema.ResourceImp
157156
} else {
158157
return &schema.ResourceImporter{
159158
StateContext: func(ctx context.Context, rd *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
160-
if err := importer.RegionalARN(ctx, rd, identity.IdentityAttribute, identity.IdentityDuplicateAttrs); err != nil {
159+
if err := importer.RegionalARN(ctx, rd, identity); err != nil {
161160
return nil, err
162161
}
163162

@@ -191,3 +190,15 @@ func singletonIdentityResourceImporter(identity inttypes.Identity) *schema.Resou
191190
}
192191
}
193192
}
193+
194+
func customResourceImporter(r *schema.Resource, identity *inttypes.Identity, importSpec *inttypes.SDKv2Import) {
195+
importF := r.Importer.StateContext
196+
197+
r.Importer = &schema.ResourceImporter{
198+
StateContext: func(ctx context.Context, rd *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
199+
ctx = importer.Context(ctx, identity, importSpec)
200+
201+
return importF(ctx, rd, meta)
202+
},
203+
}
204+
}

internal/provider/sdkv2/importer/arn.go

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@ import (
99

1010
"github.com/aws/aws-sdk-go-v2/aws/arn"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
inttypes "github.com/hashicorp/terraform-provider-aws/internal/types"
1213
"github.com/hashicorp/terraform-provider-aws/names"
1314
)
1415

15-
func RegionalARN(_ context.Context, rd *schema.ResourceData, attrName string, duplicateAttrs []string) error {
16+
func RegionalARN(_ context.Context, rd *schema.ResourceData, identitySpec inttypes.Identity) error {
17+
attr := identitySpec.Attributes[0]
18+
1619
if rd.Id() != "" {
1720
arnARN, err := arn.Parse(rd.Id())
1821
if err != nil {
1922
return fmt.Errorf("could not parse import ID %q as ARN: %s", rd.Id(), err)
2023
}
21-
rd.Set(attrName, rd.Id())
22-
for _, attr := range duplicateAttrs {
24+
rd.Set(attr.ResourceAttributeName(), rd.Id())
25+
for _, attr := range identitySpec.IdentityDuplicateAttrs {
2326
setAttribute(rd, attr, rd.Id())
2427
}
2528

@@ -39,25 +42,25 @@ func RegionalARN(_ context.Context, rd *schema.ResourceData, attrName string, du
3942
return err
4043
}
4144

42-
arnRaw, ok := identity.GetOk(attrName)
45+
arnRaw, ok := identity.GetOk(attr.Name())
4346
if !ok {
44-
return fmt.Errorf("identity attribute %q is required", attrName)
47+
return fmt.Errorf("identity attribute %q is required", attr.Name())
4548
}
4649

4750
arnVal, ok := arnRaw.(string)
4851
if !ok {
49-
return fmt.Errorf("identity attribute %q: expected string, got %T", attrName, arnRaw)
52+
return fmt.Errorf("identity attribute %q: expected string, got %T", attr.Name(), arnRaw)
5053
}
5154

5255
arnARN, err := arn.Parse(arnVal)
5356
if err != nil {
54-
return fmt.Errorf("identity attribute %q: could not parse %q as ARN: %s", attrName, arnVal, err)
57+
return fmt.Errorf("identity attribute %q: could not parse %q as ARN: %s", attr.Name(), arnVal, err)
5558
}
5659

5760
rd.Set(names.AttrRegion, arnARN.Region)
5861

59-
rd.Set(attrName, arnVal)
60-
for _, attr := range duplicateAttrs {
62+
rd.Set(attr.ResourceAttributeName(), arnVal)
63+
for _, attr := range identitySpec.IdentityDuplicateAttrs {
6164
setAttribute(rd, attr, arnVal)
6265
}
6366

@@ -84,14 +87,16 @@ func RegionalARNValue(_ context.Context, rd *schema.ResourceData, attrName strin
8487
return nil
8588
}
8689

87-
func GlobalARN(_ context.Context, rd *schema.ResourceData, attrName string, duplicateAttrs []string) error {
90+
func GlobalARN(_ context.Context, rd *schema.ResourceData, identitySpec inttypes.Identity) error {
91+
attr := identitySpec.Attributes[0]
92+
8893
if rd.Id() != "" {
8994
_, err := arn.Parse(rd.Id())
9095
if err != nil {
9196
return fmt.Errorf("could not parse import ID %q as ARN: %s", rd.Id(), err)
9297
}
93-
rd.Set(attrName, rd.Id())
94-
for _, attr := range duplicateAttrs {
98+
rd.Set(attr.ResourceAttributeName(), rd.Id())
99+
for _, attr := range identitySpec.IdentityDuplicateAttrs {
95100
setAttribute(rd, attr, rd.Id())
96101
}
97102

@@ -103,23 +108,23 @@ func GlobalARN(_ context.Context, rd *schema.ResourceData, attrName string, dupl
103108
return err
104109
}
105110

106-
arnRaw, ok := identity.GetOk(attrName)
111+
arnRaw, ok := identity.GetOk(attr.Name())
107112
if !ok {
108-
return fmt.Errorf("identity attribute %q is required", attrName)
113+
return fmt.Errorf("identity attribute %q is required", attr.Name())
109114
}
110115

111116
arnVal, ok := arnRaw.(string)
112117
if !ok {
113-
return fmt.Errorf("identity attribute %q: expected string, got %T", attrName, arnRaw)
118+
return fmt.Errorf("identity attribute %q: expected string, got %T", attr.Name(), arnRaw)
114119
}
115120

116121
_, err = arn.Parse(arnVal)
117122
if err != nil {
118-
return fmt.Errorf("identity attribute %q: could not parse %q as ARN: %s", attrName, arnVal, err)
123+
return fmt.Errorf("identity attribute %q: could not parse %q as ARN: %s", attr.Name(), arnVal, err)
119124
}
120125

121-
rd.Set(attrName, arnVal)
122-
for _, attr := range duplicateAttrs {
126+
rd.Set(attr.ResourceAttributeName(), arnVal)
127+
for _, attr := range identitySpec.IdentityDuplicateAttrs {
123128
setAttribute(rd, attr, arnVal)
124129
}
125130

0 commit comments

Comments
 (0)