-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
resource/aws_wafregional_size_constraint_set #3796
Merged
appilon
merged 4 commits into
hashicorp:master
from
appilon:resource_wafregional_size_constraint_set
Mar 22, 2018
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
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 |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/waf" | ||
"github.com/aws/aws-sdk-go/service/wafregional" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceAwsWafRegionalSizeConstraintSet() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsWafRegionalSizeConstraintSetCreate, | ||
Read: resourceAwsWafRegionalSizeConstraintSetRead, | ||
Update: resourceAwsWafRegionalSizeConstraintSetUpdate, | ||
Delete: resourceAwsWafRegionalSizeConstraintSetDelete, | ||
|
||
Schema: wafSizeConstraintSetSchema(), | ||
} | ||
} | ||
|
||
func resourceAwsWafRegionalSizeConstraintSetCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).wafregionalconn | ||
region := meta.(*AWSClient).region | ||
|
||
name := d.Get("name").(string) | ||
|
||
log.Printf("[INFO] Creating WAF Regional SizeConstraintSet: %s", name) | ||
|
||
wr := newWafRegionalRetryer(conn, region) | ||
out, err := wr.RetryWithToken(func(token *string) (interface{}, error) { | ||
params := &waf.CreateSizeConstraintSetInput{ | ||
ChangeToken: token, | ||
Name: aws.String(name), | ||
} | ||
|
||
return conn.CreateSizeConstraintSet(params) | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("[ERROR] Error creating WAF Regional SizeConstraintSet: %s", err) | ||
} | ||
resp := out.(*waf.CreateSizeConstraintSetOutput) | ||
|
||
d.SetId(*resp.SizeConstraintSet.SizeConstraintSetId) | ||
|
||
return resourceAwsWafRegionalSizeConstraintSetUpdate(d, meta) | ||
} | ||
|
||
func resourceAwsWafRegionalSizeConstraintSetRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).wafregionalconn | ||
|
||
log.Printf("[INFO] Reading WAF Regional SizeConstraintSet: %s", d.Get("name").(string)) | ||
params := &waf.GetSizeConstraintSetInput{ | ||
SizeConstraintSetId: aws.String(d.Id()), | ||
} | ||
|
||
resp, err := conn.GetSizeConstraintSet(params) | ||
if err != nil { | ||
if isAWSErr(err, wafregional.ErrCodeWAFNonexistentItemException, "") { | ||
log.Printf("[WARN] WAF Regional SizeConstraintSet (%s) not found, removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
d.Set("name", resp.SizeConstraintSet.Name) | ||
d.Set("size_constraints", flattenWafSizeConstraints(resp.SizeConstraintSet.SizeConstraints)) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsWafRegionalSizeConstraintSetUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*AWSClient) | ||
|
||
if d.HasChange("size_constraints") { | ||
o, n := d.GetChange("size_constraints") | ||
oldConstraints, newConstraints := o.(*schema.Set).List(), n.(*schema.Set).List() | ||
|
||
if err := updateRegionalSizeConstraintSetResource(d.Id(), oldConstraints, newConstraints, client.wafregionalconn, client.region); err != nil { | ||
return fmt.Errorf("[ERROR] Error updating WAF Regional SizeConstraintSet: %s", err) | ||
} | ||
} | ||
|
||
return resourceAwsWafRegionalSizeConstraintSetRead(d, meta) | ||
} | ||
|
||
func resourceAwsWafRegionalSizeConstraintSetDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).wafregionalconn | ||
region := meta.(*AWSClient).region | ||
|
||
oldConstraints := d.Get("size_constraints").(*schema.Set).List() | ||
|
||
if len(oldConstraints) > 0 { | ||
noConstraints := []interface{}{} | ||
if err := updateRegionalSizeConstraintSetResource(d.Id(), oldConstraints, noConstraints, conn, region); err != nil { | ||
return fmt.Errorf("[ERROR] Error deleting WAF Regional SizeConstraintSet: %s", err) | ||
} | ||
} | ||
|
||
wr := newWafRegionalRetryer(conn, region) | ||
_, err := wr.RetryWithToken(func(token *string) (interface{}, error) { | ||
req := &waf.DeleteSizeConstraintSetInput{ | ||
ChangeToken: token, | ||
SizeConstraintSetId: aws.String(d.Id()), | ||
} | ||
return conn.DeleteSizeConstraintSet(req) | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("[ERROR] Error deleting WAF Regional SizeConstraintSet: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func updateRegionalSizeConstraintSetResource(id string, oldConstraints, newConstraints []interface{}, conn *wafregional.WAFRegional, region string) error { | ||
wr := newWafRegionalRetryer(conn, region) | ||
_, err := wr.RetryWithToken(func(token *string) (interface{}, error) { | ||
req := &waf.UpdateSizeConstraintSetInput{ | ||
ChangeToken: token, | ||
SizeConstraintSetId: aws.String(id), | ||
Updates: diffWafSizeConstraints(oldConstraints, newConstraints), | ||
} | ||
|
||
log.Printf("[INFO] Updating WAF Regional SizeConstraintSet: %s", req) | ||
return conn.UpdateSizeConstraintSet(req) | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("[ERROR] Error updating WAF Regional SizeConstraintSet: %s", err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.
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.
So the conditional should IMO stay here, it should be just changed to
if rs.Type != "aws_waf_size_constraint_set" {
. Otherwise this would cause confusing errors if there was more than 1 resource in the state after deletion and we'd use the ID of that resource for looking up Size Constraint Set (even though it may be a completely different resource).