Skip to content

Commit

Permalink
Merge pull request #30510 from hashicorp/td-transparent-tagging-phase…
Browse files Browse the repository at this point in the history
…3c-m

Tech debt: Reduce `tags` boilerplate code - Plugin SDK resources `m*` (Phase 3c)
  • Loading branch information
ewbankkit authored Apr 6, 2023
2 parents 69a4b56 + 06d23f8 commit 9e19710
Show file tree
Hide file tree
Showing 28 changed files with 198 additions and 574 deletions.
27 changes: 7 additions & 20 deletions internal/service/macie2/classification_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/flex"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @SDKResource("aws_macie2_classification_job")
// @SDKResource("aws_macie2_classification_job", name="Classification Job")
// @Tags
func ResourceClassificationJob() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceClassificationJobCreate,
Expand Down Expand Up @@ -501,8 +503,8 @@ func ResourceClassificationJob() *schema.Resource {
},
},
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
names.AttrTags: tftags.TagsSchemaForceNew(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
"job_id": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -574,14 +576,12 @@ func resourceClassificationJobCustomizeDiff(_ context.Context, diff *schema.Reso
func resourceClassificationJobCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).Macie2Conn()

defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))

input := &macie2.CreateClassificationJobInput{
ClientToken: aws.String(id.UniqueId()),
Name: aws.String(create.Name(d.Get("name").(string), d.Get("name_prefix").(string))),
JobType: aws.String(d.Get("job_type").(string)),
S3JobDefinition: expandS3JobDefinition(d.Get("s3_job_definition").([]interface{})),
Tags: GetTagsIn(ctx),
}

if v, ok := d.GetOk("custom_data_identifier_ids"); ok {
Expand All @@ -599,11 +599,6 @@ func resourceClassificationJobCreate(ctx context.Context, d *schema.ResourceData
if v, ok := d.GetOk("initial_run"); ok {
input.InitialRun = aws.Bool(v.(bool))
}
if len(tags) > 0 {
input.Tags = Tags(tags.IgnoreAWS())
}

log.Printf("[DEBUG] Creating Macie ClassificationJob: %v", input)

var err error
var output *macie2.CreateClassificationJobOutput
Expand Down Expand Up @@ -636,8 +631,6 @@ func resourceClassificationJobCreate(ctx context.Context, d *schema.ResourceData
func resourceClassificationJobRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).Macie2Conn()

defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig
input := &macie2.DescribeClassificationJobInput{
JobId: aws.String(d.Id()),
}
Expand Down Expand Up @@ -671,15 +664,9 @@ func resourceClassificationJobRead(ctx context.Context, d *schema.ResourceData,
if err = d.Set("s3_job_definition", flattenS3JobDefinition(resp.S3JobDefinition)); err != nil {
return diag.FromErr(fmt.Errorf("error setting `%s` for Macie ClassificationJob (%s): %w", "s3_job_definition", d.Id(), err))
}
tags := KeyValueTags(ctx, resp.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig)

if err = d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return diag.FromErr(fmt.Errorf("error setting `%s` for Macie ClassificationJob (%s): %w", "tags", d.Id(), err))
}
SetTagsOut(ctx, resp.Tags)

if err = d.Set("tags_all", tags.Map()); err != nil {
return diag.FromErr(fmt.Errorf("error setting `%s` for Macie ClassificationJob (%s): %w", "tags_all", d.Id(), err))
}
d.Set("job_id", resp.JobId)
d.Set("job_arn", resp.JobArn)
status := aws.StringValue(resp.JobStatus)
Expand Down
26 changes: 7 additions & 19 deletions internal/service/macie2/custom_data_identifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/flex"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @SDKResource("aws_macie2_custom_data_identifier")
// @SDKResource("aws_macie2_custom_data_identifier", name="Custom Data Identifier")
// @Tags
func ResourceCustomDataIdentifier() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceCustomDataIdentifierCreate,
Expand Down Expand Up @@ -88,8 +90,8 @@ func ResourceCustomDataIdentifier() *schema.Resource {
ForceNew: true,
ValidateFunc: validation.IntBetween(1, 300),
},
"tags": tftags.TagsSchemaForceNew(),
"tags_all": tftags.TagsSchemaComputed(),
names.AttrTags: tftags.TagsSchemaForceNew(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
"created_at": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -105,11 +107,9 @@ func ResourceCustomDataIdentifier() *schema.Resource {
func resourceCustomDataIdentifierCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).Macie2Conn()

defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))

input := &macie2.CreateCustomDataIdentifierInput{
ClientToken: aws.String(id.UniqueId()),
Tags: GetTagsIn(ctx),
}

if v, ok := d.GetOk("regex"); ok {
Expand All @@ -128,9 +128,6 @@ func resourceCustomDataIdentifierCreate(ctx context.Context, d *schema.ResourceD
if v, ok := d.GetOk("maximum_match_distance"); ok {
input.MaximumMatchDistance = aws.Int64(int64(v.(int)))
}
if len(tags) > 0 {
input.Tags = Tags(tags.IgnoreAWS())
}

var err error
var output *macie2.CreateCustomDataIdentifierOutput
Expand Down Expand Up @@ -163,8 +160,6 @@ func resourceCustomDataIdentifierCreate(ctx context.Context, d *schema.ResourceD
func resourceCustomDataIdentifierRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).Macie2Conn()

defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig
input := &macie2.GetCustomDataIdentifierInput{
Id: aws.String(d.Id()),
}
Expand Down Expand Up @@ -193,15 +188,8 @@ func resourceCustomDataIdentifierRead(ctx context.Context, d *schema.ResourceDat
d.Set("name_prefix", create.NamePrefixFromName(aws.StringValue(resp.Name)))
d.Set("description", resp.Description)
d.Set("maximum_match_distance", resp.MaximumMatchDistance)
tags := KeyValueTags(ctx, resp.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig)

if err = d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return diag.FromErr(fmt.Errorf("error setting `%s` for Macie CustomDataIdentifier (%s): %w", "tags", d.Id(), err))
}

if err = d.Set("tags_all", tags.Map()); err != nil {
return diag.FromErr(fmt.Errorf("error setting `%s` for Macie CustomDataIdentifier (%s): %w", "tags_all", d.Id(), err))
}
SetTagsOut(ctx, resp.Tags)

if aws.BoolValue(resp.Deleted) {
log.Printf("[WARN] Macie CustomDataIdentifier (%s) is soft deleted, removing from state", d.Id())
Expand Down
26 changes: 7 additions & 19 deletions internal/service/macie2/findings_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ import (
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @SDKResource("aws_macie2_findings_filter")
// @SDKResource("aws_macie2_findings_filter", name="Findings Filter")
// @Tags
func ResourceFindingsFilter() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceFindingsFilterCreate,
Expand Down Expand Up @@ -119,8 +121,8 @@ func ResourceFindingsFilter() *schema.Resource {
Optional: true,
Computed: true,
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
names.AttrTags: tftags.TagsSchemaForceNew(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
"arn": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -132,13 +134,11 @@ func ResourceFindingsFilter() *schema.Resource {
func resourceFindingsFilterCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).Macie2Conn()

defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))

input := &macie2.CreateFindingsFilterInput{
ClientToken: aws.String(id.UniqueId()),
Name: aws.String(create.Name(d.Get("name").(string), d.Get("name_prefix").(string))),
Action: aws.String(d.Get("action").(string)),
Tags: GetTagsIn(ctx),
}

var err error
Expand All @@ -153,9 +153,6 @@ func resourceFindingsFilterCreate(ctx context.Context, d *schema.ResourceData, m
if v, ok := d.GetOk("position"); ok {
input.Position = aws.Int64(int64(v.(int)))
}
if len(tags) > 0 {
input.Tags = Tags(tags.IgnoreAWS())
}

var output *macie2.CreateFindingsFilterOutput
err = retry.RetryContext(ctx, 4*time.Minute, func() *retry.RetryError {
Expand Down Expand Up @@ -188,8 +185,6 @@ func resourceFindingsFilterCreate(ctx context.Context, d *schema.ResourceData, m
func resourceFindingsFilterRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).Macie2Conn()

defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig
input := &macie2.GetFindingsFilterInput{
Id: aws.String(d.Id()),
}
Expand All @@ -215,15 +210,8 @@ func resourceFindingsFilterRead(ctx context.Context, d *schema.ResourceData, met
d.Set("description", resp.Description)
d.Set("action", resp.Action)
d.Set("position", resp.Position)
tags := KeyValueTags(ctx, resp.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig)

if err = d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return diag.FromErr(fmt.Errorf("error setting `%s` for Macie FindingsFilter (%s): %w", "tags", d.Id(), err))
}

if err = d.Set("tags_all", tags.Map()); err != nil {
return diag.FromErr(fmt.Errorf("error setting `%s` for Macie FindingsFilter (%s): %w", "tags_all", d.Id(), err))
}
SetTagsOut(ctx, resp.Tags)

d.Set("arn", resp.Arn)

Expand Down
27 changes: 7 additions & 20 deletions internal/service/macie2/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @SDKResource("aws_macie2_member")
// @SDKResource("aws_macie2_member", name="Member")
// @Tags
func ResourceMember() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceMemberCreate,
Expand All @@ -39,8 +41,8 @@ func ResourceMember() *schema.Resource {
Required: true,
ForceNew: true,
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
names.AttrTags: tftags.TagsSchemaForceNew(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
"arn": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -95,19 +97,13 @@ func ResourceMember() *schema.Resource {
func resourceMemberCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).Macie2Conn()

defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))

accountId := d.Get("account_id").(string)
input := &macie2.CreateMemberInput{
Account: &macie2.AccountDetail{
AccountId: aws.String(accountId),
Email: aws.String(d.Get("email").(string)),
},
}

if len(tags) > 0 {
input.Tags = Tags(tags.IgnoreAWS())
Tags: GetTagsIn(ctx),
}

var err error
Expand Down Expand Up @@ -191,8 +187,6 @@ func resourceMemberCreate(ctx context.Context, d *schema.ResourceData, meta inte
func resourceMemberRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).Macie2Conn()

defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig
input := &macie2.GetMemberInput{
Id: aws.String(d.Id()),
}
Expand Down Expand Up @@ -220,15 +214,8 @@ func resourceMemberRead(ctx context.Context, d *schema.ResourceData, meta interf
d.Set("invited_at", aws.TimeValue(resp.InvitedAt).Format(time.RFC3339))
d.Set("updated_at", aws.TimeValue(resp.UpdatedAt).Format(time.RFC3339))
d.Set("arn", resp.Arn)
tags := KeyValueTags(ctx, resp.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig)

if err = d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return diag.FromErr(fmt.Errorf("error setting `%s` for Macie Member (%s): %w", "tags", d.Id(), err))
}

if err = d.Set("tags_all", tags.Map()); err != nil {
return diag.FromErr(fmt.Errorf("error setting `%s` for Macie Member (%s): %w", "tags_all", d.Id(), err))
}
SetTagsOut(ctx, resp.Tags)

status := aws.StringValue(resp.RelationshipStatus)
log.Printf("[DEBUG] print resp.RelationshipStatus: %v", aws.StringValue(resp.RelationshipStatus))
Expand Down
8 changes: 8 additions & 0 deletions internal/service/macie2/service_package_gen.go

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

27 changes: 7 additions & 20 deletions internal/service/mediaconvert/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @SDKResource("aws_media_convert_queue")
// @SDKResource("aws_media_convert_queue", name="Queue")
// @Tags
func ResourceQueue() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceQueueCreate,
Expand Down Expand Up @@ -92,8 +94,8 @@ func ResourceQueue() *schema.Resource {
mediaconvert.QueueStatusPaused,
}, false),
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
},

CustomizeDiff: verify.SetTagsDiff,
Expand All @@ -107,14 +109,11 @@ func resourceQueueCreate(ctx context.Context, d *schema.ResourceData, meta inter
return sdkdiag.AppendErrorf(diags, "getting Media Convert Account Client: %s", err)
}

defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))

createOpts := &mediaconvert.CreateQueueInput{
Name: aws.String(d.Get("name").(string)),
Status: aws.String(d.Get("status").(string)),
PricingPlan: aws.String(d.Get("pricing_plan").(string)),
Tags: Tags(tags.IgnoreAWS()),
Tags: GetTagsIn(ctx),
}

if v, ok := d.GetOk("description"); ok {
Expand Down Expand Up @@ -142,9 +141,6 @@ func resourceQueueRead(ctx context.Context, d *schema.ResourceData, meta interfa
return sdkdiag.AppendErrorf(diags, "getting Media Convert Account Client: %s", err)
}

defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

getOpts := &mediaconvert.GetQueueInput{
Name: aws.String(d.Id()),
}
Expand Down Expand Up @@ -175,16 +171,7 @@ func resourceQueueRead(ctx context.Context, d *schema.ResourceData, meta interfa
return sdkdiag.AppendErrorf(diags, "listing tags for Media Convert Queue (%s): %s", d.Id(), err)
}

tags = tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig)

//lintignore:AWSR002
if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return sdkdiag.AppendErrorf(diags, "setting tags: %s", err)
}

if err := d.Set("tags_all", tags.Map()); err != nil {
return sdkdiag.AppendErrorf(diags, "setting tags_all: %s", err)
}
SetTagsOut(ctx, Tags(tags))

return diags
}
Expand Down
Loading

0 comments on commit 9e19710

Please sign in to comment.