Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tech debt: Reduce tags boilerplate code - Plugin SDK resources w* and x* (Phase 3c) #30417

Merged
merged 5 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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