Skip to content

Commit

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

Tech debt: Reduce `tags` boilerplate code - Plugin SDK resources `c*` (Phase 3c)
  • Loading branch information
ewbankkit authored Apr 5, 2023
2 parents 1afac8d + e7fdf66 commit b2ac360
Show file tree
Hide file tree
Showing 63 changed files with 769 additions and 1,707 deletions.
48 changes: 5 additions & 43 deletions internal/service/ce/anomaly_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import (
"github.com/hashicorp/terraform-provider-aws/names"
)

// @SDKResource("aws_ce_anomaly_monitor")
// @SDKResource("aws_ce_anomaly_monitor", name="Anomaly Monitor")
// @Tags(identifierAttribute="id")
func ResourceAnomalyMonitor() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceAnomalyMonitorCreate,
Expand Down Expand Up @@ -62,8 +63,8 @@ func ResourceAnomalyMonitor() *schema.Resource {
ForceNew: true,
ValidateFunc: validation.StringInSlice(costexplorer.MonitorType_Values(), false),
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
},

CustomizeDiff: verify.SetTagsDiff,
Expand All @@ -72,14 +73,13 @@ func ResourceAnomalyMonitor() *schema.Resource {

func resourceAnomalyMonitorCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).CEConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))

input := &costexplorer.CreateAnomalyMonitorInput{
AnomalyMonitor: &costexplorer.AnomalyMonitor{
MonitorName: aws.String(d.Get("name").(string)),
MonitorType: aws.String(d.Get("monitor_type").(string)),
},
ResourceTags: GetTagsIn(ctx),
}
switch d.Get("monitor_type").(string) {
case costexplorer.MonitorTypeDimensional:
Expand All @@ -102,10 +102,6 @@ func resourceAnomalyMonitorCreate(ctx context.Context, d *schema.ResourceData, m
}
}

if len(tags) > 0 {
input.ResourceTags = Tags(tags.IgnoreAWS())
}

resp, err := conn.CreateAnomalyMonitorWithContext(ctx, input)

if err != nil {
Expand All @@ -123,8 +119,6 @@ func resourceAnomalyMonitorCreate(ctx context.Context, d *schema.ResourceData, m

func resourceAnomalyMonitorRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).CEConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

monitor, err := FindAnomalyMonitorByARN(ctx, conn, d.Id())

Expand Down Expand Up @@ -157,22 +151,6 @@ func resourceAnomalyMonitorRead(ctx context.Context, d *schema.ResourceData, met
d.Set("name", monitor.MonitorName)
d.Set("monitor_type", monitor.MonitorType)

tags, err := ListTags(ctx, conn, aws.StringValue(monitor.MonitorArn))
tags = tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig)

if err != nil {
return create.DiagError(names.CE, create.ErrActionReading, ResNameAnomalyMonitor, d.Id(), err)
}

//lintignore:AWSR002
if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return create.DiagError(names.CE, create.ErrActionReading, ResNameAnomalyMonitor, d.Id(), err)
}

if err := d.Set("tags_all", tags.Map()); err != nil {
return create.DiagError(names.CE, create.ErrActionReading, ResNameAnomalyMonitor, d.Id(), err)
}

return nil
}

Expand All @@ -189,22 +167,6 @@ func resourceAnomalyMonitorUpdate(ctx context.Context, d *schema.ResourceData, m
requestUpdate = true
}

if d.HasChange("tags") {
o, n := d.GetChange("tags")

if err := UpdateTags(ctx, conn, d.Id(), o, n); err != nil {
return create.DiagError(names.CE, create.ErrActionUpdating, ResNameAnomalyMonitor, d.Id(), err)
}
}

if d.HasChange("tags_all") {
o, n := d.GetChange("tags_all")

if err := UpdateTags(ctx, conn, d.Id(), o, n); err != nil {
return create.DiagError(names.CE, create.ErrActionUpdating, ResNameAnomalyMonitor, d.Id(), err)
}
}

if requestUpdate {
_, err := conn.UpdateAnomalyMonitorWithContext(ctx, input)

Expand Down
48 changes: 5 additions & 43 deletions internal/service/ce/anomaly_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import (
"github.com/hashicorp/terraform-provider-aws/names"
)

// @SDKResource("aws_ce_anomaly_subscription")
// @SDKResource("aws_ce_anomaly_subscription", name="Anomaly Subscription")
// @Tags(identifierAttribute="id")
func ResourceAnomalySubscription() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceAnomalySubscriptionCreate,
Expand Down Expand Up @@ -91,8 +92,8 @@ func ResourceAnomalySubscription() *schema.Resource {
Optional: true,
Elem: schemaCostCategoryRule(),
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
},

CustomizeDiff: verify.SetTagsDiff,
Expand All @@ -101,8 +102,6 @@ func ResourceAnomalySubscription() *schema.Resource {

func resourceAnomalySubscriptionCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).CEConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))

input := &costexplorer.CreateAnomalySubscriptionInput{
AnomalySubscription: &costexplorer.AnomalySubscription{
Expand All @@ -111,6 +110,7 @@ func resourceAnomalySubscriptionCreate(ctx context.Context, d *schema.ResourceDa
MonitorArnList: aws.StringSlice(expandAnomalySubscriptionMonitorARNList(d.Get("monitor_arn_list").([]interface{}))),
Subscribers: expandAnomalySubscriptionSubscribers(d.Get("subscriber").(*schema.Set).List()),
},
ResourceTags: GetTagsIn(ctx),
}

if v, ok := d.GetOk("account_id"); ok {
Expand All @@ -125,10 +125,6 @@ func resourceAnomalySubscriptionCreate(ctx context.Context, d *schema.ResourceDa
input.AnomalySubscription.ThresholdExpression = expandCostExpression(v.([]interface{})[0].(map[string]interface{}))
}

if len(tags) > 0 {
input.ResourceTags = Tags(tags.IgnoreAWS())
}

resp, err := conn.CreateAnomalySubscriptionWithContext(ctx, input)

if err != nil {
Expand All @@ -146,8 +142,6 @@ func resourceAnomalySubscriptionCreate(ctx context.Context, d *schema.ResourceDa

func resourceAnomalySubscriptionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).CEConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

subscription, err := FindAnomalySubscriptionByARN(ctx, conn, d.Id())

Expand All @@ -173,22 +167,6 @@ func resourceAnomalySubscriptionRead(ctx context.Context, d *schema.ResourceData
return create.DiagError(names.CE, "setting threshold_expression", ResNameAnomalySubscription, d.Id(), err)
}

tags, err := ListTags(ctx, conn, aws.StringValue(subscription.SubscriptionArn))
tags = tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig)

if err != nil {
return create.DiagError(names.CE, create.ErrActionReading, ResNameAnomalySubscription, d.Id(), err)
}

//lintignore:AWSR002
if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return create.DiagError(names.CE, create.ErrActionUpdating, ResNameAnomalySubscription, d.Id(), err)
}

if err := d.Set("tags_all", tags.Map()); err != nil {
return create.DiagError(names.CE, create.ErrActionUpdating, ResNameAnomalySubscription, d.Id(), err)
}

return nil
}

Expand Down Expand Up @@ -227,22 +205,6 @@ func resourceAnomalySubscriptionUpdate(ctx context.Context, d *schema.ResourceDa
}
}

if d.HasChange("tags") {
o, n := d.GetChange("tags")

if err := UpdateTags(ctx, conn, d.Id(), o, n); err != nil {
return create.DiagError(names.CE, create.ErrActionUpdating, ResNameAnomalySubscription, d.Id(), err)
}
}

if d.HasChange("tags_all") {
o, n := d.GetChange("tags_all")

if err := UpdateTags(ctx, conn, d.Id(), o, n); err != nil {
return create.DiagError(names.CE, create.ErrActionUpdating, ResNameAnomalySubscription, d.Id(), err)
}
}

return resourceAnomalySubscriptionRead(ctx, d, meta)
}

Expand Down
45 changes: 8 additions & 37 deletions internal/service/ce/cost_category.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import (
"github.com/hashicorp/terraform-provider-aws/names"
)

// @SDKResource("aws_ce_cost_category")
// @SDKResource("aws_ce_cost_category", name="Cost Category")
// @Tags(identifierAttribute="id")
func ResourceCostCategory() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceCostCategoryCreate,
Expand Down Expand Up @@ -158,8 +159,8 @@ func ResourceCostCategory() *schema.Resource {
},
},
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
},
}
}
Expand Down Expand Up @@ -374,13 +375,12 @@ func schemaCostCategoryRuleExpression() *schema.Resource {

func resourceCostCategoryCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).CEConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))

input := &costexplorer.CreateCostCategoryDefinitionInput{
Name: aws.String(d.Get("name").(string)),
Rules: expandCostCategoryRules(d.Get("rule").(*schema.Set).List()),
RuleVersion: aws.String(d.Get("rule_version").(string)),
Name: aws.String(d.Get("name").(string)),
ResourceTags: GetTagsIn(ctx),
Rules: expandCostCategoryRules(d.Get("rule").(*schema.Set).List()),
RuleVersion: aws.String(d.Get("rule_version").(string)),
}

if v, ok := d.GetOk("default_value"); ok {
Expand All @@ -395,10 +395,6 @@ func resourceCostCategoryCreate(ctx context.Context, d *schema.ResourceData, met
input.EffectiveStart = aws.String(v.(string))
}

if len(tags) > 0 {
input.ResourceTags = Tags(tags.IgnoreAWS())
}

outputRaw, err := tfresource.RetryWhenAWSErrCodeEquals(ctx, d.Timeout(schema.TimeoutCreate),
func() (interface{}, error) {
return conn.CreateCostCategoryDefinitionWithContext(ctx, input)
Expand All @@ -416,8 +412,6 @@ func resourceCostCategoryCreate(ctx context.Context, d *schema.ResourceData, met

func resourceCostCategoryRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).CEConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

costCategory, err := FindCostCategoryByARN(ctx, conn, d.Id())

Expand All @@ -443,22 +437,6 @@ func resourceCostCategoryRead(ctx context.Context, d *schema.ResourceData, meta
return create.DiagError(names.CE, "setting split_charge_rule", ResNameCostCategory, d.Id(), err)
}

tags, err := ListTags(ctx, conn, d.Id())

if err != nil {
return create.DiagError(names.CE, "listing tags", ResNameCostCategory, d.Id(), err)
}

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

//lintignore:AWSR002
if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return create.DiagError(names.CE, "setting tags", ResNameCostCategory, d.Id(), err)
}
if err := d.Set("tags_all", tags.Map()); err != nil {
return create.DiagError(names.CE, "setting tags_all", ResNameCostCategory, d.Id(), err)
}

return nil
}

Expand Down Expand Up @@ -488,13 +466,6 @@ func resourceCostCategoryUpdate(ctx context.Context, d *schema.ResourceData, met
}
}

if d.HasChange("tags_all") {
o, n := d.GetChange("tags_all")
if err := UpdateTags(ctx, conn, d.Id(), o, n); err != nil {
return create.DiagError(names.CE, create.ErrActionUpdating, ResNameCostCategory, d.Id(), err)
}
}

return resourceCostCategoryRead(ctx, d, meta)
}

Expand Down
12 changes: 12 additions & 0 deletions internal/service/ce/service_package_gen.go

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

Loading

0 comments on commit b2ac360

Please sign in to comment.