Skip to content

Commit

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

Tech debt: Reduce `tags` boilerplate code - Plugin SDK resources `b*` (Phase 3c)
  • Loading branch information
ewbankkit authored Apr 5, 2023
2 parents c718518 + 1b77c1e commit 68c9f42
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 256 deletions.
41 changes: 6 additions & 35 deletions internal/service/backup/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,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_backup_framework")
// @SDKResource("aws_backup_framework", name="Framework")
// @Tags(identifierAttribute="arn")
func ResourceFramework() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceFrameworkCreate,
Expand Down Expand Up @@ -125,8 +127,8 @@ func ResourceFramework() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
},
CustomizeDiff: verify.SetTagsDiff,
}
Expand All @@ -135,26 +137,19 @@ func ResourceFramework() *schema.Resource {
func resourceFrameworkCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).BackupConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))

name := d.Get("name").(string)

input := &backup.CreateFrameworkInput{
IdempotencyToken: aws.String(id.UniqueId()),
FrameworkControls: expandFrameworkControls(ctx, d.Get("control").(*schema.Set).List()),
FrameworkName: aws.String(name),
FrameworkTags: GetTagsIn(ctx),
}

if v, ok := d.GetOk("description"); ok {
input.FrameworkDescription = aws.String(v.(string))
}

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

log.Printf("[DEBUG] Creating Backup Framework: %#v", input)
resp, err := conn.CreateFrameworkWithContext(ctx, input)
if err != nil {
return sdkdiag.AppendErrorf(diags, "creating Backup Framework: %s", err)
Expand All @@ -174,8 +169,6 @@ func resourceFrameworkCreate(ctx context.Context, d *schema.ResourceData, meta i
func resourceFrameworkRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).BackupConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

resp, err := conn.DescribeFrameworkWithContext(ctx, &backup.DescribeFrameworkInput{
FrameworkName: aws.String(d.Id()),
Expand Down Expand Up @@ -204,21 +197,6 @@ func resourceFrameworkRead(ctx context.Context, d *schema.ResourceData, meta int
return sdkdiag.AppendErrorf(diags, "setting control: %s", err)
}

tags, err := ListTags(ctx, conn, d.Get("arn").(string))
if err != nil {
return sdkdiag.AppendErrorf(diags, "listing tags for Backup Framework (%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)
}

return diags
}

Expand Down Expand Up @@ -249,13 +227,6 @@ func resourceFrameworkUpdate(ctx context.Context, d *schema.ResourceData, meta i
}
}

if d.HasChange("tags_all") {
o, n := d.GetChange("tags_all")
if err := UpdateTags(ctx, conn, d.Get("arn").(string), o, n); err != nil {
return sdkdiag.AppendErrorf(diags, "updating tags for Backup Framework (%s): %s", d.Id(), err)
}
}

return append(diags, resourceFrameworkRead(ctx, d, meta)...)
}

Expand Down
37 changes: 6 additions & 31 deletions internal/service/backup/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,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_backup_plan")
// @SDKResource("aws_backup_plan", name="Plan")
// @Tags(identifierAttribute="arn")
func ResourcePlan() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourcePlanCreate,
Expand Down Expand Up @@ -161,8 +163,8 @@ func ResourcePlan() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
},

CustomizeDiff: verify.SetTagsDiff,
Expand All @@ -172,19 +174,16 @@ func ResourcePlan() *schema.Resource {
func resourcePlanCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).BackupConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))

input := &backup.CreateBackupPlanInput{
BackupPlan: &backup.PlanInput{
BackupPlanName: aws.String(d.Get("name").(string)),
Rules: expandPlanRules(ctx, d.Get("rule").(*schema.Set)),
AdvancedBackupSettings: expandPlanAdvancedSettings(d.Get("advanced_backup_setting").(*schema.Set)),
},
BackupPlanTags: Tags(tags.IgnoreAWS()),
BackupPlanTags: GetTagsIn(ctx),
}

log.Printf("[DEBUG] Creating Backup Plan: %#v", input)
resp, err := conn.CreateBackupPlanWithContext(ctx, input)
if err != nil {
return sdkdiag.AppendErrorf(diags, "creating Backup Plan: %s", err)
Expand All @@ -198,8 +197,6 @@ func resourcePlanCreate(ctx context.Context, d *schema.ResourceData, meta interf
func resourcePlanRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).BackupConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

resp, err := conn.GetBackupPlanWithContext(ctx, &backup.GetBackupPlanInput{
BackupPlanId: aws.String(d.Id()),
Expand Down Expand Up @@ -227,21 +224,6 @@ func resourcePlanRead(ctx context.Context, d *schema.ResourceData, meta interfac
return sdkdiag.AppendErrorf(diags, "setting advanced_backup_setting: %s", err)
}

tags, err := ListTags(ctx, conn, d.Get("arn").(string))
if err != nil {
return sdkdiag.AppendErrorf(diags, "listing tags for Backup Plan (%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)
}

return diags
}

Expand All @@ -266,13 +248,6 @@ func resourcePlanUpdate(ctx context.Context, d *schema.ResourceData, meta interf
}
}

if d.HasChange("tags_all") {
o, n := d.GetChange("tags_all")
if err := UpdateTags(ctx, conn, d.Get("arn").(string), o, n); err != nil {
return sdkdiag.AppendErrorf(diags, "updating tags for Backup Plan (%s): %s", d.Id(), err)
}
}

return append(diags, resourcePlanRead(ctx, d, meta)...)
}

Expand Down
43 changes: 6 additions & 37 deletions internal/service/backup/report_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,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_backup_report_plan")
// @SDKResource("aws_backup_report_plan", name="Report Plan")
// @Tags(identifierAttribute="arn")
func ResourceReportPlan() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceReportPlanCreate,
Expand Down Expand Up @@ -109,8 +111,8 @@ func ResourceReportPlan() *schema.Resource {
},
},
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
},

CustomizeDiff: verify.SetTagsDiff,
Expand All @@ -120,26 +122,20 @@ func ResourceReportPlan() *schema.Resource {
func resourceReportPlanCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).BackupConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))

name := d.Get("name").(string)
input := &backup.CreateReportPlanInput{
IdempotencyToken: aws.String(id.UniqueId()),
ReportDeliveryChannel: expandReportDeliveryChannel(d.Get("report_delivery_channel").([]interface{})),
ReportPlanName: aws.String(name),
ReportPlanTags: GetTagsIn(ctx),
ReportSetting: expandReportSetting(d.Get("report_setting").([]interface{})),
}

if v, ok := d.GetOk("description"); ok {
input.ReportPlanDescription = aws.String(v.(string))
}

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

log.Printf("[DEBUG] Creating Backup Report Plan: %s", input)
output, err := conn.CreateReportPlanWithContext(ctx, input)

if err != nil {
Expand All @@ -159,8 +155,6 @@ func resourceReportPlanCreate(ctx context.Context, d *schema.ResourceData, meta
func resourceReportPlanRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).BackupConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

reportPlan, err := FindReportPlanByName(ctx, conn, d.Id())

Expand Down Expand Up @@ -188,23 +182,6 @@ func resourceReportPlanRead(ctx context.Context, d *schema.ResourceData, meta in
return sdkdiag.AppendErrorf(diags, "setting report_setting: %s", err)
}

tags, err := ListTags(ctx, conn, d.Get("arn").(string))

if err != nil {
return sdkdiag.AppendErrorf(diags, "listing tags for Backup Report Plan (%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)
}

return diags
}

Expand Down Expand Up @@ -233,14 +210,6 @@ func resourceReportPlanUpdate(ctx context.Context, d *schema.ResourceData, meta
}
}

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

if err := UpdateTags(ctx, conn, d.Get("arn").(string), o, n); err != nil {
return sdkdiag.AppendErrorf(diags, "updating tags for Backup Report Plan (%s): %s", d.Id(), err)
}
}

return append(diags, resourceReportPlanRead(ctx, d, meta)...)
}

Expand Down
16 changes: 16 additions & 0 deletions internal/service/backup/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 68c9f42

Please sign in to comment.