-
Notifications
You must be signed in to change notification settings - Fork 9.1k
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
Add Tag Support for aws_waf_rate_based_rule resource #10479
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
31f9449
Merge pull request #1 from terraform-providers/master
DrFaust92 d4284c7
Merge pull request #2 from terraform-providers/master
DrFaust92 aca775e
Merge pull request #3 from terraform-providers/master
DrFaust92 d6fc247
tag support for resource_aws_waf_rate_based_rule
DrFaust92 d9d70a8
tag support for resource_aws_waf_rate_based_rule
DrFaust92 fb35a7d
Update aws/resource_aws_waf_rate_based_rule.go
DrFaust92 b4cd043
add predicate create on create
DrFaust92 b642aa8
add predicate create on create
DrFaust92 b8265d0
update keyvaluetags lib for waf list function
DrFaust92 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,12 @@ import ( | |
"log" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/arn" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/service/waf" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/validation" | ||
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags" | ||
) | ||
|
||
func resourceAwsWafRateBasedRule() *schema.Resource { | ||
|
@@ -64,12 +66,18 @@ func resourceAwsWafRateBasedRule() *schema.Resource { | |
Required: true, | ||
ValidateFunc: validation.IntAtLeast(100), | ||
}, | ||
"tags": tagsSchema(), | ||
"arn": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsWafRateBasedRuleCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).wafconn | ||
tags := keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().WafTags() | ||
|
||
wr := newWafRetryer(conn) | ||
out, err := wr.RetryWithToken(func(token *string) (interface{}, error) { | ||
|
@@ -81,14 +89,28 @@ func resourceAwsWafRateBasedRuleCreate(d *schema.ResourceData, meta interface{}) | |
RateLimit: aws.Int64(int64(d.Get("rate_limit").(int))), | ||
} | ||
|
||
if len(tags) > 0 { | ||
params.Tags = tags | ||
} | ||
|
||
return conn.CreateRateBasedRule(params) | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
resp := out.(*waf.CreateRateBasedRuleOutput) | ||
d.SetId(*resp.Rule.RuleId) | ||
return resourceAwsWafRateBasedRuleUpdate(d, meta) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❤️ |
||
|
||
newPredicates := d.Get("predicates").(*schema.Set).List() | ||
if len(newPredicates) > 0 { | ||
noPredicates := []interface{}{} | ||
err := updateWafRateBasedRuleResource(*resp.Rule.RuleId, noPredicates, newPredicates, d.Get("rate_limit"), conn) | ||
if err != nil { | ||
return fmt.Errorf("Error Updating WAF Rate Based Rule: %s", err) | ||
} | ||
} | ||
|
||
return resourceAwsWafRateBasedRuleRead(d, meta) | ||
} | ||
|
||
func resourceAwsWafRateBasedRuleRead(d *schema.ResourceData, meta interface{}) error { | ||
|
@@ -100,7 +122,7 @@ func resourceAwsWafRateBasedRuleRead(d *schema.ResourceData, meta interface{}) e | |
|
||
resp, err := conn.GetRateBasedRule(params) | ||
if err != nil { | ||
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "WAFNonexistentItemException" { | ||
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == waf.ErrCodeNonexistentItemException { | ||
log.Printf("[WARN] WAF Rate Based Rule (%s) not found, removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
|
@@ -120,6 +142,22 @@ func resourceAwsWafRateBasedRuleRead(d *schema.ResourceData, meta interface{}) e | |
predicates = append(predicates, predicate) | ||
} | ||
|
||
arn := arn.ARN{ | ||
Partition: meta.(*AWSClient).partition, | ||
Service: "waf", | ||
AccountID: meta.(*AWSClient).accountid, | ||
Resource: fmt.Sprintf("ratebasedrule/%s", d.Id()), | ||
}.String() | ||
d.Set("arn", arn) | ||
|
||
tagList, err := keyvaluetags.WafListTags(conn, arn) | ||
if err != nil { | ||
return fmt.Errorf("Failed to get WAF Rated Based Rule parameter tags for %s: %s", d.Get("name"), err) | ||
} | ||
if err := d.Set("tags", tagList.IgnoreAws().Map()); err != nil { | ||
return fmt.Errorf("error setting tags: %s", err) | ||
} | ||
|
||
d.Set("predicates", predicates) | ||
d.Set("name", resp.Rule.Name) | ||
d.Set("metric_name", resp.Rule.MetricName) | ||
|
@@ -143,6 +181,14 @@ func resourceAwsWafRateBasedRuleUpdate(d *schema.ResourceData, meta interface{}) | |
} | ||
} | ||
|
||
if d.HasChange("tags") { | ||
o, n := d.GetChange("tags") | ||
|
||
if err := keyvaluetags.WafUpdateTags(conn, d.Get("arn").(string), o, n); err != nil { | ||
return fmt.Errorf("error updating tags: %s", err) | ||
} | ||
} | ||
|
||
return resourceAwsWafRateBasedRuleRead(d, meta) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice 👍