Skip to content

Commit 85038b5

Browse files
committed
Adds customInherentRegionResourceImporter for SDKv2
1 parent 63f0c0d commit 85038b5

File tree

3 files changed

+51
-16
lines changed

3 files changed

+51
-16
lines changed

internal/provider/sdkv2/identity_interceptor.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,19 @@ func singletonIdentityResourceImporter(identity inttypes.Identity) *schema.Resou
251251
}
252252
}
253253

254+
func customInherentRegionResourceImporter(identity inttypes.Identity) *schema.ResourceImporter {
255+
// Not supported for Global resources. This is validated in validateResourceSchemas().
256+
return &schema.ResourceImporter{
257+
StateContext: func(ctx context.Context, rd *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
258+
if err := importer.RegionalInherentRegion(ctx, rd, identity); err != nil {
259+
return nil, err
260+
}
261+
262+
return []*schema.ResourceData{rd}, nil
263+
},
264+
}
265+
}
266+
254267
func customResourceImporter(r *schema.Resource, identity *inttypes.Identity, importSpec *inttypes.SDKv2Import) {
255268
importF := r.Importer.StateContext
256269

internal/provider/sdkv2/provider.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,8 @@ func (p *sdkProvider) initialize(ctx context.Context) (map[string]conns.ServiceP
733733
r.Importer = arnIdentityResourceImporter(resource.Identity)
734734
} else if resource.Identity.IsSingleton {
735735
r.Importer = singletonIdentityResourceImporter(resource.Identity)
736+
} else if resource.Identity.IsCustomInherentRegion {
737+
r.Importer = customInherentRegionResourceImporter(resource.Identity)
736738
} else {
737739
r.Importer = newParameterizedIdentityImporter(resource.Identity, &resource.Import)
738740
}
@@ -834,6 +836,13 @@ func (p *sdkProvider) validateResourceSchemas(ctx context.Context) error {
834836
continue
835837
}
836838
}
839+
840+
if resource.Identity.IsCustomInherentRegion {
841+
if resource.Identity.IsGlobalResource {
842+
errs = append(errs, fmt.Errorf("`IsCustomInherentRegion` is not supported for Global resources: %s resource", typeName))
843+
continue
844+
}
845+
}
837846
}
838847
}
839848

internal/types/service_package.go

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,20 @@ type ServicePackageSDKListResource struct {
130130
}
131131

132132
type Identity struct {
133-
IsGlobalResource bool // All
134-
IsSingleton bool // Singleton
135-
IsARN bool // ARN
136-
IsGlobalARNFormat bool // ARN
137-
IdentityAttribute string // ARN
138-
IDAttrShadowsAttr string
139-
Attributes []IdentityAttribute
140-
IdentityDuplicateAttrs []string
141-
IsSingleParameter bool
142-
IsMutable bool
143-
IsSetOnUpdate bool
144-
customInherentRegion bool
145-
version int64
133+
IsGlobalResource bool // All
134+
IsSingleton bool // Singleton
135+
IsARN bool // ARN
136+
IsGlobalARNFormat bool // ARN
137+
IdentityAttribute string // ARN
138+
IDAttrShadowsAttr string
139+
Attributes []IdentityAttribute
140+
IdentityDuplicateAttrs []string
141+
IsSingleParameter bool
142+
IsMutable bool
143+
IsSetOnUpdate bool
144+
IsCustomInherentRegion bool
145+
customInherentRegionParser RegionalCustomInherentRegionIdentityFunc
146+
version int64
146147
}
147148

148149
func (i Identity) HasInherentRegion() bool {
@@ -155,7 +156,7 @@ func (i Identity) HasInherentRegion() bool {
155156
if i.IsARN && !i.IsGlobalARNFormat {
156157
return true
157158
}
158-
if i.customInherentRegion {
159+
if i.IsCustomInherentRegion {
159160
return true
160161
}
161162
return false
@@ -165,6 +166,10 @@ func (i Identity) Version() int64 {
165166
return i.version
166167
}
167168

169+
func (i Identity) CustomInherentRegionParser() RegionalCustomInherentRegionIdentityFunc {
170+
return i.customInherentRegionParser
171+
}
172+
168173
func RegionalParameterizedIdentity(attributes []IdentityAttribute, opts ...IdentityOptsFunc) Identity {
169174
baseAttributes := []IdentityAttribute{
170175
StringIdentityAttribute("account_id", false),
@@ -255,14 +260,15 @@ func arnIdentity(isGlobalResource bool, name string, opts []IdentityOptsFunc) Id
255260
return identity
256261
}
257262

258-
func RegionalCustomInherentRegionIdentity(name string, opts ...IdentityOptsFunc) Identity {
263+
func RegionalCustomInherentRegionIdentity(name string, parser RegionalCustomInherentRegionIdentityFunc, opts ...IdentityOptsFunc) Identity {
259264
identity := Identity{
260265
IsGlobalResource: false,
261266
IdentityAttribute: name,
262267
Attributes: []IdentityAttribute{
263268
StringIdentityAttribute(name, true),
264269
},
265-
customInherentRegion: true,
270+
IsCustomInherentRegion: true,
271+
customInherentRegionParser: parser,
266272
}
267273

268274
for _, opt := range opts {
@@ -272,6 +278,13 @@ func RegionalCustomInherentRegionIdentity(name string, opts ...IdentityOptsFunc)
272278
return identity
273279
}
274280

281+
type BaseIdentity struct {
282+
AccountID string
283+
Region string
284+
}
285+
286+
type RegionalCustomInherentRegionIdentityFunc func(value string) (BaseIdentity, error)
287+
275288
func RegionalResourceWithGlobalARNFormat(opts ...IdentityOptsFunc) Identity {
276289
return RegionalResourceWithGlobalARNFormatNamed(names.AttrARN, opts...)
277290
}

0 commit comments

Comments
 (0)