Skip to content

Commit 95eda84

Browse files
authored
Merge pull request #30533 from hashicorp/td-transparent-tagging-phase3c-e
Tech debt: Reduce `tags` boilerplate code - Plugin SDK resources `e*` (Phase 3c)
2 parents 0cc2d9c + f57a4ca commit 95eda84

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+929
-2116
lines changed

.changelog/30533.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
resource/aws_elasticache_user_group: Change `user_group_id` to [ForceNew](https://developer.hashicorp.com/terraform/plugin/sdkv2/schemas/schema-behaviors#forcenew)
3+
```

internal/provider/fwprovider/intercept.go

+10-13
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,11 @@ func (r tagsInterceptor) create(ctx context.Context, request resource.CreateRequ
295295
return ctx, diags
296296
}
297297

298+
inContext, ok := conns.FromContext(ctx)
299+
if !ok {
300+
return ctx, diags
301+
}
302+
298303
tagsInContext, ok := tftags.FromContext(ctx)
299304
if !ok {
300305
return ctx, diags
@@ -312,14 +317,14 @@ func (r tagsInterceptor) create(ctx context.Context, request resource.CreateRequ
312317
// Merge the resource's configured tags with any provider configured default_tags.
313318
tags := tagsInContext.DefaultConfig.MergeTags(tftags.New(ctx, planTags))
314319
// Remove system tags.
315-
tags = tags.IgnoreAWS()
320+
tags = tags.IgnoreSystem(inContext.ServicePackageName)
316321

317322
tagsInContext.TagsIn = types.Some(tags)
318323
case After:
319324
// Set values for unknowns.
320325
// Remove any provider configured ignore_tags and system tags from those passed to the service API.
321326
// Computed tags_all include any provider configured default_tags.
322-
stateTagsAll := flex.FlattenFrameworkStringValueMapLegacy(ctx, tagsInContext.TagsIn.MustUnwrap().IgnoreAWS().IgnoreConfig(tagsInContext.IgnoreConfig).Map())
327+
stateTagsAll := flex.FlattenFrameworkStringValueMapLegacy(ctx, tagsInContext.TagsIn.MustUnwrap().IgnoreSystem(inContext.ServicePackageName).IgnoreConfig(tagsInContext.IgnoreConfig).Map())
323328
diags.Append(response.State.SetAttribute(ctx, path.Root(names.AttrTagsAll), &stateTagsAll)...)
324329

325330
if diags.HasError() {
@@ -336,25 +341,21 @@ func (r tagsInterceptor) read(ctx context.Context, request resource.ReadRequest,
336341
}
337342

338343
inContext, ok := conns.FromContext(ctx)
339-
340344
if !ok {
341345
return ctx, diags
342346
}
343347

344348
sp, ok := meta.ServicePackages[inContext.ServicePackageName]
345-
346349
if !ok {
347350
return ctx, diags
348351
}
349352

350353
serviceName, err := names.HumanFriendly(inContext.ServicePackageName)
351-
352354
if err != nil {
353355
serviceName = "<service>"
354356
}
355357

356358
resourceName := inContext.ResourceName
357-
358359
if resourceName == "" {
359360
resourceName = "<thing>"
360361
}
@@ -409,7 +410,7 @@ func (r tagsInterceptor) read(ctx context.Context, request resource.ReadRequest,
409410
stateTags := tftags.Null
410411
// Remove any provider configured ignore_tags and system tags from those returned from the service API.
411412
// The resource's configured tags do not include any provider configured default_tags.
412-
if v := apiTags.IgnoreAWS().IgnoreConfig(tagsInContext.IgnoreConfig).RemoveDefaultConfig(tagsInContext.DefaultConfig).Map(); len(v) > 0 {
413+
if v := apiTags.IgnoreSystem(inContext.ServicePackageName).IgnoreConfig(tagsInContext.IgnoreConfig).RemoveDefaultConfig(tagsInContext.DefaultConfig).Map(); len(v) > 0 {
413414
stateTags = flex.FlattenFrameworkStringValueMapLegacy(ctx, v)
414415
}
415416
diags.Append(response.State.SetAttribute(ctx, path.Root(names.AttrTags), &stateTags)...)
@@ -419,7 +420,7 @@ func (r tagsInterceptor) read(ctx context.Context, request resource.ReadRequest,
419420
}
420421

421422
// Computed tags_all do.
422-
stateTagsAll := flex.FlattenFrameworkStringValueMapLegacy(ctx, apiTags.IgnoreAWS().IgnoreConfig(tagsInContext.IgnoreConfig).Map())
423+
stateTagsAll := flex.FlattenFrameworkStringValueMapLegacy(ctx, apiTags.IgnoreSystem(inContext.ServicePackageName).IgnoreConfig(tagsInContext.IgnoreConfig).Map())
423424
diags.Append(response.State.SetAttribute(ctx, path.Root(names.AttrTagsAll), &stateTagsAll)...)
424425

425426
if diags.HasError() {
@@ -436,25 +437,21 @@ func (r tagsInterceptor) update(ctx context.Context, request resource.UpdateRequ
436437
}
437438

438439
inContext, ok := conns.FromContext(ctx)
439-
440440
if !ok {
441441
return ctx, diags
442442
}
443443

444444
sp, ok := meta.ServicePackages[inContext.ServicePackageName]
445-
446445
if !ok {
447446
return ctx, diags
448447
}
449448

450449
serviceName, err := names.HumanFriendly(inContext.ServicePackageName)
451-
452450
if err != nil {
453451
serviceName = "<service>"
454452
}
455453

456454
resourceName := inContext.ResourceName
457-
458455
if resourceName == "" {
459456
resourceName = "<thing>"
460457
}
@@ -476,7 +473,7 @@ func (r tagsInterceptor) update(ctx context.Context, request resource.UpdateRequ
476473
// Merge the resource's configured tags with any provider configured default_tags.
477474
tags := tagsInContext.DefaultConfig.MergeTags(tftags.New(ctx, planTags))
478475
// Remove system tags.
479-
tags = tags.IgnoreAWS()
476+
tags = tags.IgnoreSystem(inContext.ServicePackageName)
480477

481478
tagsInContext.TagsIn = types.Some(tags)
482479

internal/provider/intercept.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -187,25 +187,21 @@ func (r tagsInterceptor) run(ctx context.Context, d *schema.ResourceData, meta a
187187
}
188188

189189
inContext, ok := conns.FromContext(ctx)
190-
191190
if !ok {
192191
return ctx, diags
193192
}
194193

195194
sp, ok := meta.(*conns.AWSClient).ServicePackages[inContext.ServicePackageName]
196-
197195
if !ok {
198196
return ctx, diags
199197
}
200198

201199
serviceName, err := names.HumanFriendly(inContext.ServicePackageName)
202-
203200
if err != nil {
204201
serviceName = "<service>"
205202
}
206203

207204
resourceName := inContext.ResourceName
208-
209205
if resourceName == "" {
210206
resourceName = "<thing>"
211207
}
@@ -222,7 +218,7 @@ func (r tagsInterceptor) run(ctx context.Context, d *schema.ResourceData, meta a
222218
// Merge the resource's configured tags with any provider configured default_tags.
223219
tags := tagsInContext.DefaultConfig.MergeTags(tftags.New(ctx, d.Get(names.AttrTags).(map[string]interface{})))
224220
// Remove system tags.
225-
tags = tags.IgnoreAWS()
221+
tags = tags.IgnoreSystem(inContext.ServicePackageName)
226222

227223
tagsInContext.TagsIn = types.Some(tags)
228224

@@ -321,7 +317,7 @@ func (r tagsInterceptor) run(ctx context.Context, d *schema.ResourceData, meta a
321317
}
322318

323319
// Remove any provider configured ignore_tags and system tags from those returned from the service API.
324-
tags := tagsInContext.TagsOut.UnwrapOrDefault().IgnoreAWS().IgnoreConfig(tagsInContext.IgnoreConfig)
320+
tags := tagsInContext.TagsOut.UnwrapOrDefault().IgnoreSystem(inContext.ServicePackageName).IgnoreConfig(tagsInContext.IgnoreConfig)
325321

326322
// The resource's configured tags do not include any provider configured default_tags.
327323
if err := d.Set(names.AttrTags, tags.RemoveDefaultConfig(tagsInContext.DefaultConfig).Map()); err != nil {

internal/service/ecr/repository.go

+6-51
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import (
2323
"github.com/hashicorp/terraform-provider-aws/names"
2424
)
2525

26-
// @SDKResource("aws_ecr_repository")
26+
// @SDKResource("aws_ecr_repository", name="Repository")
27+
// @Tags(identifierAttribute="arn")
2728
func ResourceRepository() *schema.Resource {
2829
return &schema.Resource{
2930
CreateWithoutTimeout: resourceRepositoryCreate,
@@ -106,8 +107,8 @@ func ResourceRepository() *schema.Resource {
106107
Type: schema.TypeString,
107108
Computed: true,
108109
},
109-
"tags": tftags.TagsSchema(),
110-
"tags_all": tftags.TagsSchemaComputed(),
110+
names.AttrTags: tftags.TagsSchema(),
111+
names.AttrTagsAll: tftags.TagsSchemaComputed(),
111112
},
112113
}
113114
}
@@ -119,14 +120,13 @@ const (
119120
func resourceRepositoryCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
120121
var diags diag.Diagnostics
121122
conn := meta.(*conns.AWSClient).ECRConn()
122-
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
123-
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))
124123

125124
name := d.Get("name").(string)
126125
input := &ecr.CreateRepositoryInput{
127126
EncryptionConfiguration: expandRepositoryEncryptionConfiguration(d.Get("encryption_configuration").([]interface{})),
128127
ImageTagMutability: aws.String(d.Get("image_tag_mutability").(string)),
129128
RepositoryName: aws.String(name),
129+
Tags: GetTagsIn(ctx),
130130
}
131131

132132
if v, ok := d.GetOk("image_scanning_configuration"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
@@ -136,10 +136,6 @@ func resourceRepositoryCreate(ctx context.Context, d *schema.ResourceData, meta
136136
}
137137
}
138138

139-
if len(tags) > 0 {
140-
input.Tags = Tags(tags.IgnoreAWS())
141-
}
142-
143139
output, err := conn.CreateRepositoryWithContext(ctx, input)
144140

145141
// Some partitions (i.e., ISO) may not support tag-on-create
@@ -157,7 +153,7 @@ func resourceRepositoryCreate(ctx context.Context, d *schema.ResourceData, meta
157153
d.SetId(aws.StringValue(output.Repository.RepositoryName))
158154

159155
// Some partitions (i.e., ISO) may not support tag-on-create, attempt tag after create
160-
if input.Tags == nil && len(tags) > 0 && meta.(*conns.AWSClient).Partition != endpoints.AwsPartitionID {
156+
if tags := KeyValueTags(ctx, GetTagsIn(ctx)); input.Tags == nil && len(tags) > 0 && meta.(*conns.AWSClient).Partition != endpoints.AwsPartitionID {
161157
err := UpdateTags(ctx, conn, aws.StringValue(output.Repository.RepositoryArn), nil, tags)
162158

163159
// If default tags only, log and continue. Otherwise, error.
@@ -177,8 +173,6 @@ func resourceRepositoryCreate(ctx context.Context, d *schema.ResourceData, meta
177173
func resourceRepositoryRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
178174
var diags diag.Diagnostics
179175
conn := meta.(*conns.AWSClient).ECRConn()
180-
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
181-
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig
182176

183177
outputRaw, err := tfresource.RetryWhenNewResourceNotFound(ctx, propagationTimeout, func() (interface{}, error) {
184178
return FindRepositoryByName(ctx, conn, d.Id())
@@ -210,29 +204,6 @@ func resourceRepositoryRead(ctx context.Context, d *schema.ResourceData, meta in
210204
d.Set("registry_id", repository.RegistryId)
211205
d.Set("repository_url", repository.RepositoryUri)
212206

213-
tags, err := ListTags(ctx, conn, arn)
214-
215-
// Some partitions (i.e., ISO) may not support tagging, giving error
216-
if meta.(*conns.AWSClient).Partition != endpoints.AwsPartitionID && verify.ErrorISOUnsupported(conn.PartitionID, err) {
217-
log.Printf("[WARN] failed listing tags for ECR Repository (%s): %s", d.Id(), err)
218-
return diags
219-
}
220-
221-
if err != nil {
222-
return sdkdiag.AppendErrorf(diags, "listing tags for ECR Repository (%s): %s", d.Id(), err)
223-
}
224-
225-
tags = tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig)
226-
227-
//lintignore:AWSR002
228-
if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
229-
return sdkdiag.AppendErrorf(diags, "setting tags: %s", err)
230-
}
231-
232-
if err := d.Set("tags_all", tags.Map()); err != nil {
233-
return sdkdiag.AppendErrorf(diags, "setting tags_all: %s", err)
234-
}
235-
236207
return diags
237208
}
238209

@@ -273,22 +244,6 @@ func resourceRepositoryUpdate(ctx context.Context, d *schema.ResourceData, meta
273244
}
274245
}
275246

276-
if d.HasChange("tags_all") {
277-
o, n := d.GetChange("tags_all")
278-
279-
err := UpdateTags(ctx, conn, d.Get("arn").(string), o, n)
280-
281-
// Some partitions may not support tagging, giving error
282-
if meta.(*conns.AWSClient).Partition != endpoints.AwsPartitionID && verify.ErrorISOUnsupported(conn.PartitionID, err) {
283-
log.Printf("[WARN] failed updating tags for ECR Repository (%s): %s", d.Id(), err)
284-
return append(diags, resourceRepositoryRead(ctx, d, meta)...)
285-
}
286-
287-
if err != nil {
288-
return sdkdiag.AppendErrorf(diags, "updating ECR Repository (%s) tags: %s", d.Id(), err)
289-
}
290-
}
291-
292247
return append(diags, resourceRepositoryRead(ctx, d, meta)...)
293248
}
294249

internal/service/ecr/service_package_gen.go

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/service/ecrpublic/repository.go

+6-37
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ import (
2121
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
2222
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
2323
"github.com/hashicorp/terraform-provider-aws/internal/verify"
24+
"github.com/hashicorp/terraform-provider-aws/names"
2425
)
2526

26-
// @SDKResource("aws_ecrpublic_repository")
27+
// @SDKResource("aws_ecrpublic_repository", name="Repository")
28+
// @Tags(identifierAttribute="arn")
2729
func ResourceRepository() *schema.Resource {
2830
return &schema.Resource{
2931
CreateWithoutTimeout: resourceRepositoryCreate,
@@ -113,32 +115,25 @@ func ResourceRepository() *schema.Resource {
113115
Type: schema.TypeString,
114116
Computed: true,
115117
},
116-
"tags": tftags.TagsSchema(),
117-
"tags_all": tftags.TagsSchemaComputed(),
118+
names.AttrTags: tftags.TagsSchema(),
119+
names.AttrTagsAll: tftags.TagsSchemaComputed(),
118120
},
119121
}
120122
}
121123

122124
func resourceRepositoryCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
123125
var diags diag.Diagnostics
124126
conn := meta.(*conns.AWSClient).ECRPublicConn()
125-
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
126-
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))
127127

128128
input := ecrpublic.CreateRepositoryInput{
129129
RepositoryName: aws.String(d.Get("repository_name").(string)),
130+
Tags: GetTagsIn(ctx),
130131
}
131132

132133
if v, ok := d.GetOk("catalog_data"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
133134
input.CatalogData = expandRepositoryCatalogData(v.([]interface{})[0].(map[string]interface{}))
134135
}
135136

136-
if len(tags) > 0 {
137-
input.Tags = Tags(tags.IgnoreAWS())
138-
}
139-
140-
log.Printf("[DEBUG] Creating ECR Public repository: %#v", input)
141-
142137
out, err := conn.CreateRepositoryWithContext(ctx, &input)
143138
if err != nil {
144139
return sdkdiag.AppendErrorf(diags, "creating ECR Public repository: %s", err)
@@ -160,8 +155,6 @@ func resourceRepositoryCreate(ctx context.Context, d *schema.ResourceData, meta
160155
func resourceRepositoryRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
161156
var diags diag.Diagnostics
162157
conn := meta.(*conns.AWSClient).ECRPublicConn()
163-
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
164-
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig
165158

166159
log.Printf("[DEBUG] Reading ECR Public repository %s", d.Id())
167160
var out *ecrpublic.DescribeRepositoriesOutput
@@ -237,19 +230,6 @@ func resourceRepositoryRead(ctx context.Context, d *schema.ResourceData, meta in
237230
d.Set("catalog_data", nil)
238231
}
239232

240-
tags, err := ListTags(ctx, conn, aws.StringValue(repository.RepositoryArn))
241-
242-
tags = tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig)
243-
244-
//lintignore:AWSR002
245-
if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
246-
return sdkdiag.AppendErrorf(diags, "setting tags: %s", err)
247-
}
248-
249-
if err := d.Set("tags_all", tags.Map()); err != nil {
250-
return sdkdiag.AppendErrorf(diags, "setting tags_all: %s", err)
251-
}
252-
253233
return diags
254234
}
255235

@@ -310,7 +290,6 @@ func resourceRepositoryDelete(ctx context.Context, d *schema.ResourceData, meta
310290

311291
func resourceRepositoryUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
312292
var diags diag.Diagnostics
313-
arn := d.Get("arn").(string)
314293
conn := meta.(*conns.AWSClient).ECRPublicConn()
315294

316295
if d.HasChange("catalog_data") {
@@ -319,16 +298,6 @@ func resourceRepositoryUpdate(ctx context.Context, d *schema.ResourceData, meta
319298
}
320299
}
321300

322-
if d.HasChange("tags_all") {
323-
o, n := d.GetChange("tags_all")
324-
325-
err := UpdateTags(ctx, conn, arn, o, n)
326-
327-
if err != nil {
328-
return sdkdiag.AppendErrorf(diags, "updating tags for ECR Public Repository (%s): %s", d.Id(), err)
329-
}
330-
}
331-
332301
return append(diags, resourceRepositoryRead(ctx, d, meta)...)
333302
}
334303

0 commit comments

Comments
 (0)