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

New resource r/aws_wafv2_ip_set #12119

Merged
merged 19 commits into from
May 18, 2020
61 changes: 21 additions & 40 deletions aws/resource_aws_wafv2_ip_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/wafv2"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -63,6 +62,10 @@ func resourceAwsWafv2IPSet() *schema.Resource {
wafv2.IPAddressVersionIpv6,
}, false),
},
"lock_token": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -148,7 +151,7 @@ func resourceAwsWafv2IPSetRead(d *schema.ResourceData, meta interface{}) error {

resp, err := conn.GetIPSet(params)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == wafv2.ErrCodeWAFNonexistentItemException {
if isAWSErr(err, wafv2.ErrCodeWAFNonexistentItemException, "AWS WAF couldn’t perform the operation because your resource doesn’t exist") {
anGie44 marked this conversation as resolved.
Show resolved Hide resolved
anGie44 marked this conversation as resolved.
Show resolved Hide resolved
log.Printf("[WARN] WAFV2 IPSet (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
Expand All @@ -161,8 +164,9 @@ func resourceAwsWafv2IPSetRead(d *schema.ResourceData, meta interface{}) error {
d.Set("description", resp.IPSet.Description)
d.Set("ip_address_version", resp.IPSet.IPAddressVersion)
d.Set("arn", resp.IPSet.ARN)
d.Set("lock_token", resp.LockToken)

if err := d.Set("addresses", schema.NewSet(schema.HashString, flattenStringList(resp.IPSet.Addresses))); err != nil {
if err := d.Set("addresses", flattenStringSet(resp.IPSet.Addresses)); err != nil {
return fmt.Errorf("Error setting addresses: %s", err)
}

Expand All @@ -180,39 +184,27 @@ func resourceAwsWafv2IPSetRead(d *schema.ResourceData, meta interface{}) error {

func resourceAwsWafv2IPSetUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).wafv2conn
var resp *wafv2.GetIPSetOutput
params := &wafv2.GetIPSetInput{
Id: aws.String(d.Id()),
Name: aws.String(d.Get("name").(string)),
Scope: aws.String(d.Get("scope").(string)),
}

log.Printf("[INFO] Updating WAFV2 IPSet %s", d.Id())

err := resource.Retry(15*time.Minute, func() *resource.RetryError {
var err error
resp, err = conn.GetIPSet(params)
if err != nil {
return resource.NonRetryableError(fmt.Errorf("Error getting lock token: %s", err))
}

u := &wafv2.UpdateIPSetInput{
Id: aws.String(d.Id()),
Name: aws.String(d.Get("name").(string)),
Scope: aws.String(d.Get("scope").(string)),
Addresses: []*string{},
Description: aws.String(d.Get("description").(string)),
LockToken: resp.LockToken,
Id: aws.String(d.Id()),
Name: aws.String(d.Get("name").(string)),
Scope: aws.String(d.Get("scope").(string)),
Addresses: []*string{},
LockToken: aws.String(d.Get("lock_token").(string)),
}

if v, ok := d.GetOk("addresses"); ok && v.(*schema.Set).Len() > 0 {
u.Addresses = expandStringSet(d.Get("addresses").(*schema.Set))
}

if d.HasChange("description") {
if v, ok := d.GetOk("description"); ok && len(v.(string)) > 0 {
u.Description = aws.String(d.Get("description").(string))
}

_, err = conn.UpdateIPSet(u)
_, err := conn.UpdateIPSet(u)

if err != nil {
if isAWSErr(err, wafv2.ErrCodeWAFInternalErrorException, "AWS WAF couldn’t perform the operation because of a system problem") {
Expand All @@ -227,11 +219,11 @@ func resourceAwsWafv2IPSetUpdate(d *schema.ResourceData, meta interface{}) error
})

if isResourceTimeoutError(err) {
_, err = conn.DeleteIPSet(&wafv2.DeleteIPSetInput{
_, err = conn.UpdateIPSet(&wafv2.UpdateIPSetInput{
Id: aws.String(d.Id()),
Name: aws.String(d.Get("name").(string)),
Scope: aws.String(d.Get("scope").(string)),
LockToken: resp.LockToken,
LockToken: aws.String(d.Get("lock_token").(string)),
})
}

Expand All @@ -251,26 +243,15 @@ func resourceAwsWafv2IPSetUpdate(d *schema.ResourceData, meta interface{}) error

func resourceAwsWafv2IPSetDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).wafv2conn
var resp *wafv2.GetIPSetOutput
params := &wafv2.GetIPSetInput{
Id: aws.String(d.Id()),
Name: aws.String(d.Get("name").(string)),
Scope: aws.String(d.Get("scope").(string)),
}

log.Printf("[INFO] Deleting WAFV2 IPSet %s", d.Id())

err := resource.Retry(15*time.Minute, func() *resource.RetryError {
var err error
resp, err = conn.GetIPSet(params)
if err != nil {
return resource.NonRetryableError(fmt.Errorf("Error getting lock token: %s", err))
}

_, err = conn.DeleteIPSet(&wafv2.DeleteIPSetInput{
_, err := conn.DeleteIPSet(&wafv2.DeleteIPSetInput{
Id: aws.String(d.Id()),
Name: aws.String(d.Get("name").(string)),
Scope: aws.String(d.Get("scope").(string)),
LockToken: resp.LockToken,
LockToken: aws.String(d.Get("lock_token").(string)),
})

if err != nil {
Expand All @@ -290,7 +271,7 @@ func resourceAwsWafv2IPSetDelete(d *schema.ResourceData, meta interface{}) error
Id: aws.String(d.Id()),
Name: aws.String(d.Get("name").(string)),
Scope: aws.String(d.Get("scope").(string)),
LockToken: resp.LockToken,
LockToken: aws.String(d.Get("lock_token").(string)),
})
}

Expand Down
6 changes: 6 additions & 0 deletions aws/resource_aws_wafv2_ip_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ func TestAccAwsWafv2IPSet_minimal(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "addresses.#", "0"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: testAccAWSWafv2IPSetImportStateIdFunc(resourceName),
},
},
})
}
Expand Down