Skip to content

Commit

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

Tech debt: Reduce `tags` boilerplate code - Plugin Framework resources `w*` and `x*` (Phase 3c)
  • Loading branch information
ewbankkit authored Apr 3, 2023
2 parents 6a8270b + 6c8c77e commit de6d166
Show file tree
Hide file tree
Showing 22 changed files with 195 additions and 609 deletions.
42 changes: 6 additions & 36 deletions internal/service/waf/rate_based_rule.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_waf_rate_based_rule")
// @SDKResource("aws_waf_rate_based_rule", name="Rate Based Rule")
// @Tags(identifierAttribute="arn")
func ResourceRateBasedRule() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceRateBasedRuleCreate,
Expand Down Expand Up @@ -79,8 +81,8 @@ func ResourceRateBasedRule() *schema.Resource {
Required: true,
ValidateFunc: validation.IntAtLeast(100),
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
},

CustomizeDiff: verify.SetTagsDiff,
Expand All @@ -90,25 +92,20 @@ func ResourceRateBasedRule() *schema.Resource {
func resourceRateBasedRuleCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).WAFConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))

name := d.Get("name").(string)
input := &waf.CreateRateBasedRuleInput{
MetricName: aws.String(d.Get("metric_name").(string)),
Name: aws.String(name),
RateKey: aws.String(d.Get("rate_key").(string)),
RateLimit: aws.Int64(int64(d.Get("rate_limit").(int))),
Tags: GetTagsIn(ctx),
}

wr := NewRetryer(conn)
outputRaw, err := wr.RetryWithToken(ctx, func(token *string) (interface{}, error) {
input.ChangeToken = token

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

return conn.CreateRateBasedRuleWithContext(ctx, input)
})

Expand All @@ -135,8 +132,6 @@ func resourceRateBasedRuleCreate(ctx context.Context, d *schema.ResourceData, me
func resourceRateBasedRuleRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).WAFConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

rule, err := FindRateBasedRuleByID(ctx, conn, d.Id())

Expand Down Expand Up @@ -177,23 +172,6 @@ func resourceRateBasedRuleRead(ctx context.Context, d *schema.ResourceData, meta
return sdkdiag.AppendErrorf(diags, "setting predicates: %s", err)
}

tags, err := ListTags(ctx, conn, arn)

if err != nil {
return sdkdiag.AppendErrorf(diags, "listing tags for WAF Rate Based Rule (%s): %s", arn, 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 @@ -213,14 +191,6 @@ func resourceRateBasedRuleUpdate(ctx context.Context, d *schema.ResourceData, me
}
}

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: %s", err)
}
}

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

Expand Down
47 changes: 8 additions & 39 deletions internal/service/waf/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ 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"
)

const (
RuleDeleteTimeout = 5 * time.Minute
)

// @SDKResource("aws_waf_rule")
// @SDKResource("aws_waf_rule", name="Rule")
// @Tags(identifierAttribute="arn")
func ResourceRule() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceRuleCreate,
Expand Down Expand Up @@ -71,8 +73,8 @@ func ResourceRule() *schema.Resource {
},
},
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
"arn": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -86,22 +88,17 @@ func ResourceRule() *schema.Resource {
func resourceRuleCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).WAFConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))

wr := NewRetryer(conn)
out, err := wr.RetryWithToken(ctx, func(token *string) (interface{}, error) {
params := &waf.CreateRuleInput{
input := &waf.CreateRuleInput{
ChangeToken: token,
MetricName: aws.String(d.Get("metric_name").(string)),
Name: aws.String(d.Get("name").(string)),
Tags: GetTagsIn(ctx),
}

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

return conn.CreateRuleWithContext(ctx, params)
return conn.CreateRuleWithContext(ctx, input)
})

if err != nil {
Expand All @@ -126,8 +123,6 @@ func resourceRuleCreate(ctx context.Context, d *schema.ResourceData, meta interf
func resourceRuleRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).WAFConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

params := &waf.GetRuleInput{
RuleId: aws.String(d.Id()),
Expand Down Expand Up @@ -172,24 +167,6 @@ func resourceRuleRead(ctx context.Context, d *schema.ResourceData, meta interfac
Resource: fmt.Sprintf("rule/%s", d.Id()),
}.String()
d.Set("arn", arn)

tags, err := ListTags(ctx, conn, arn)

if err != nil {
return sdkdiag.AppendErrorf(diags, "listing tags for WAF Rule (%s): %s", arn, 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)
}

d.Set("predicates", predicates)
d.Set("name", resp.Rule.Name)
d.Set("metric_name", resp.Rule.MetricName)
Expand All @@ -211,14 +188,6 @@ func resourceRuleUpdate(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 WAF Rule (%s) tags: %s", d.Id(), err)
}
}

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

Expand Down
46 changes: 8 additions & 38 deletions internal/service/waf/rule_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,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_waf_rule_group")
// @SDKResource("aws_waf_rule_group", name="Rule Group")
// @Tags(identifierAttribute="arn")
func ResourceRuleGroup() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceRuleGroupCreate,
Expand Down Expand Up @@ -74,8 +76,8 @@ func ResourceRuleGroup() *schema.Resource {
},
},
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
"arn": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -89,22 +91,17 @@ func ResourceRuleGroup() *schema.Resource {
func resourceRuleGroupCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).WAFConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))

wr := NewRetryer(conn)
out, err := wr.RetryWithToken(ctx, func(token *string) (interface{}, error) {
params := &waf.CreateRuleGroupInput{
input := &waf.CreateRuleGroupInput{
ChangeToken: token,
MetricName: aws.String(d.Get("metric_name").(string)),
Name: aws.String(d.Get("name").(string)),
Tags: GetTagsIn(ctx),
}

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

return conn.CreateRuleGroupWithContext(ctx, params)
return conn.CreateRuleGroupWithContext(ctx, input)
})
if err != nil {
return sdkdiag.AppendErrorf(diags, "creating WAF Rule Group (%s): %s", d.Get("name").(string), err)
Expand All @@ -128,8 +125,6 @@ func resourceRuleGroupCreate(ctx context.Context, d *schema.ResourceData, meta i
func resourceRuleGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).WAFConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

params := &waf.GetRuleGroupInput{
RuleGroupId: aws.String(d.Id()),
Expand Down Expand Up @@ -160,23 +155,6 @@ func resourceRuleGroupRead(ctx context.Context, d *schema.ResourceData, meta int
Resource: fmt.Sprintf("rulegroup/%s", d.Id()),
}.String()
d.Set("arn", arn)

tags, err := ListTags(ctx, conn, arn)
if err != nil {
return sdkdiag.AppendErrorf(diags, "listing tags for WAF Rule Group (%s): %s", arn, 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)
}

d.Set("activated_rule", FlattenActivatedRules(rResp.ActivatedRules))
d.Set("name", resp.RuleGroup.Name)
d.Set("metric_name", resp.RuleGroup.MetricName)
Expand All @@ -198,14 +176,6 @@ func resourceRuleGroupUpdate(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: %s", err)
}
}

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

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

Please sign in to comment.