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

Add support resource import for aws wafregional size constraint set resource #9713

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
43 changes: 28 additions & 15 deletions aws/resource_aws_wafregional_size_constraint_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ func resourceAwsWafRegionalSizeConstraintSet() *schema.Resource {
Read: resourceAwsWafRegionalSizeConstraintSetRead,
Update: resourceAwsWafRegionalSizeConstraintSetUpdate,
Delete: resourceAwsWafRegionalSizeConstraintSetDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: wafSizeConstraintSetSchema(),
}
Expand Down Expand Up @@ -43,7 +46,7 @@ func resourceAwsWafRegionalSizeConstraintSetCreate(d *schema.ResourceData, meta
}
resp := out.(*waf.CreateSizeConstraintSetOutput)

d.SetId(*resp.SizeConstraintSet.SizeConstraintSetId)
d.SetId(aws.StringValue(resp.SizeConstraintSet.SizeConstraintSetId))

return resourceAwsWafRegionalSizeConstraintSetUpdate(d, meta)
}
Expand All @@ -57,13 +60,13 @@ func resourceAwsWafRegionalSizeConstraintSetRead(d *schema.ResourceData, meta in
}

resp, err := conn.GetSizeConstraintSet(params)
if isAWSErr(err, wafregional.ErrCodeWAFNonexistentItemException, "") {
log.Printf("[WARN] WAF Regional SizeConstraintSet (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
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
return fmt.Errorf("Error getting WAF Regional Size Constraint Set (%s): %s", d.Id(), err)
}

d.Set("name", resp.SizeConstraintSet.Name)
Expand All @@ -79,8 +82,14 @@ func resourceAwsWafRegionalSizeConstraintSetUpdate(d *schema.ResourceData, meta
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 updating WAF Regional SizeConstraintSet: %s", err)
err := updateRegionalSizeConstraintSetResource(d.Id(), oldConstraints, newConstraints, client.wafregionalconn, client.region)
if isAWSErr(err, wafregional.ErrCodeWAFNonexistentItemException, "") {
log.Printf("[WARN] WAF Regional SizeConstraintSet (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error updating WAF Regional SizeConstraintSet(%s): %s", d.Id(), err)
}
}

Expand All @@ -95,8 +104,12 @@ func resourceAwsWafRegionalSizeConstraintSetDelete(d *schema.ResourceData, meta

if len(oldConstraints) > 0 {
noConstraints := []interface{}{}
if err := updateRegionalSizeConstraintSetResource(d.Id(), oldConstraints, noConstraints, conn, region); err != nil {
return fmt.Errorf("Error deleting WAF Regional SizeConstraintSet: %s", err)
err := updateRegionalSizeConstraintSetResource(d.Id(), oldConstraints, noConstraints, conn, region)
if isAWSErr(err, wafregional.ErrCodeWAFNonexistentItemException, "") {
return nil
}
if err != nil {
return fmt.Errorf("Error deleting WAF Regional SizeConstraintSet(%s): %s", d.Id(), err)
}
}

Expand All @@ -108,6 +121,9 @@ func resourceAwsWafRegionalSizeConstraintSetDelete(d *schema.ResourceData, meta
}
return conn.DeleteSizeConstraintSet(req)
})
if isAWSErr(err, wafregional.ErrCodeWAFNonexistentItemException, "") {
return nil
}
if err != nil {
return fmt.Errorf("Error deleting WAF Regional SizeConstraintSet: %s", err)
}
Expand All @@ -127,9 +143,6 @@ func updateRegionalSizeConstraintSetResource(id string, oldConstraints, newConst
log.Printf("[INFO] Updating WAF Regional SizeConstraintSet: %s", req)
return conn.UpdateSizeConstraintSet(req)
})
if err != nil {
return fmt.Errorf("Error updating WAF Regional SizeConstraintSet: %s", err)
}

return nil
return err
}
99 changes: 62 additions & 37 deletions aws/resource_aws_wafregional_size_constraint_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
func TestAccAWSWafRegionalSizeConstraintSet_basic(t *testing.T) {
var constraints waf.SizeConstraintSet
sizeConstraintSet := fmt.Sprintf("sizeConstraintSet-%s", acctest.RandString(5))
resourceName := "aws_wafregional_size_constraint_set.size_constraint_set"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -25,25 +26,30 @@ func TestAccAWSWafRegionalSizeConstraintSet_basic(t *testing.T) {
{
Config: testAccAWSWafRegionalSizeConstraintSetConfig(sizeConstraintSet),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSWafRegionalSizeConstraintSetExists("aws_wafregional_size_constraint_set.size_constraint_set", &constraints),
testAccCheckAWSWafRegionalSizeConstraintSetExists(resourceName, &constraints),
resource.TestCheckResourceAttr(
"aws_wafregional_size_constraint_set.size_constraint_set", "name", sizeConstraintSet),
resourceName, "name", sizeConstraintSet),
resource.TestCheckResourceAttr(
"aws_wafregional_size_constraint_set.size_constraint_set", "size_constraints.#", "1"),
resourceName, "size_constraints.#", "1"),
resource.TestCheckResourceAttr(
"aws_wafregional_size_constraint_set.size_constraint_set", "size_constraints.2029852522.comparison_operator", "EQ"),
resourceName, "size_constraints.2029852522.comparison_operator", "EQ"),
resource.TestCheckResourceAttr(
"aws_wafregional_size_constraint_set.size_constraint_set", "size_constraints.2029852522.field_to_match.#", "1"),
resourceName, "size_constraints.2029852522.field_to_match.#", "1"),
resource.TestCheckResourceAttr(
"aws_wafregional_size_constraint_set.size_constraint_set", "size_constraints.2029852522.field_to_match.281401076.data", ""),
resourceName, "size_constraints.2029852522.field_to_match.281401076.data", ""),
resource.TestCheckResourceAttr(
"aws_wafregional_size_constraint_set.size_constraint_set", "size_constraints.2029852522.field_to_match.281401076.type", "BODY"),
resourceName, "size_constraints.2029852522.field_to_match.281401076.type", "BODY"),
resource.TestCheckResourceAttr(
"aws_wafregional_size_constraint_set.size_constraint_set", "size_constraints.2029852522.size", "4096"),
resourceName, "size_constraints.2029852522.size", "4096"),
resource.TestCheckResourceAttr(
"aws_wafregional_size_constraint_set.size_constraint_set", "size_constraints.2029852522.text_transformation", "NONE"),
resourceName, "size_constraints.2029852522.text_transformation", "NONE"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -52,6 +58,7 @@ func TestAccAWSWafRegionalSizeConstraintSet_changeNameForceNew(t *testing.T) {
var before, after waf.SizeConstraintSet
sizeConstraintSet := fmt.Sprintf("sizeConstraintSet-%s", acctest.RandString(5))
sizeConstraintSetNewName := fmt.Sprintf("sizeConstraintSet-%s", acctest.RandString(5))
resourceName := "aws_wafregional_size_constraint_set.size_constraint_set"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -61,30 +68,36 @@ func TestAccAWSWafRegionalSizeConstraintSet_changeNameForceNew(t *testing.T) {
{
Config: testAccAWSWafRegionalSizeConstraintSetConfig(sizeConstraintSet),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSWafRegionalSizeConstraintSetExists("aws_wafregional_size_constraint_set.size_constraint_set", &before),
testAccCheckAWSWafRegionalSizeConstraintSetExists(resourceName, &before),
resource.TestCheckResourceAttr(
"aws_wafregional_size_constraint_set.size_constraint_set", "name", sizeConstraintSet),
resourceName, "name", sizeConstraintSet),
resource.TestCheckResourceAttr(
"aws_wafregional_size_constraint_set.size_constraint_set", "size_constraints.#", "1"),
resourceName, "size_constraints.#", "1"),
),
},
{
Config: testAccAWSWafRegionalSizeConstraintSetConfigChangeName(sizeConstraintSetNewName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSWafRegionalSizeConstraintSetExists("aws_wafregional_size_constraint_set.size_constraint_set", &after),
testAccCheckAWSWafRegionalSizeConstraintSetExists(resourceName, &after),
resource.TestCheckResourceAttr(
"aws_wafregional_size_constraint_set.size_constraint_set", "name", sizeConstraintSetNewName),
resourceName, "name", sizeConstraintSetNewName),
resource.TestCheckResourceAttr(
"aws_wafregional_size_constraint_set.size_constraint_set", "size_constraints.#", "1"),
resourceName, "size_constraints.#", "1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAWSWafRegionalSizeConstraintSet_disappears(t *testing.T) {
var constraints waf.SizeConstraintSet
sizeConstraintSet := fmt.Sprintf("sizeConstraintSet-%s", acctest.RandString(5))
resourceName := "aws_wafregional_size_constraint_set.size_constraint_set"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -94,7 +107,7 @@ func TestAccAWSWafRegionalSizeConstraintSet_disappears(t *testing.T) {
{
Config: testAccAWSWafRegionalSizeConstraintSetConfig(sizeConstraintSet),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSWafRegionalSizeConstraintSetExists("aws_wafregional_size_constraint_set.size_constraint_set", &constraints),
testAccCheckAWSWafRegionalSizeConstraintSetExists(resourceName, &constraints),
testAccCheckAWSWafRegionalSizeConstraintSetDisappears(&constraints),
),
ExpectNonEmptyPlan: true,
Expand All @@ -106,6 +119,7 @@ func TestAccAWSWafRegionalSizeConstraintSet_disappears(t *testing.T) {
func TestAccAWSWafRegionalSizeConstraintSet_changeConstraints(t *testing.T) {
var before, after waf.SizeConstraintSet
setName := fmt.Sprintf("sizeConstraintSet-%s", acctest.RandString(5))
resourceName := "aws_wafregional_size_constraint_set.size_constraint_set"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -115,54 +129,60 @@ func TestAccAWSWafRegionalSizeConstraintSet_changeConstraints(t *testing.T) {
{
Config: testAccAWSWafRegionalSizeConstraintSetConfig(setName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSWafRegionalSizeConstraintSetExists("aws_wafregional_size_constraint_set.size_constraint_set", &before),
testAccCheckAWSWafRegionalSizeConstraintSetExists(resourceName, &before),
resource.TestCheckResourceAttr(
"aws_wafregional_size_constraint_set.size_constraint_set", "name", setName),
resourceName, "name", setName),
resource.TestCheckResourceAttr(
"aws_wafregional_size_constraint_set.size_constraint_set", "size_constraints.#", "1"),
resourceName, "size_constraints.#", "1"),
resource.TestCheckResourceAttr(
"aws_wafregional_size_constraint_set.size_constraint_set", "size_constraints.2029852522.comparison_operator", "EQ"),
resourceName, "size_constraints.2029852522.comparison_operator", "EQ"),
resource.TestCheckResourceAttr(
"aws_wafregional_size_constraint_set.size_constraint_set", "size_constraints.2029852522.field_to_match.#", "1"),
resourceName, "size_constraints.2029852522.field_to_match.#", "1"),
resource.TestCheckResourceAttr(
"aws_wafregional_size_constraint_set.size_constraint_set", "size_constraints.2029852522.field_to_match.281401076.data", ""),
resourceName, "size_constraints.2029852522.field_to_match.281401076.data", ""),
resource.TestCheckResourceAttr(
"aws_wafregional_size_constraint_set.size_constraint_set", "size_constraints.2029852522.field_to_match.281401076.type", "BODY"),
resourceName, "size_constraints.2029852522.field_to_match.281401076.type", "BODY"),
resource.TestCheckResourceAttr(
"aws_wafregional_size_constraint_set.size_constraint_set", "size_constraints.2029852522.size", "4096"),
resourceName, "size_constraints.2029852522.size", "4096"),
resource.TestCheckResourceAttr(
"aws_wafregional_size_constraint_set.size_constraint_set", "size_constraints.2029852522.text_transformation", "NONE"),
resourceName, "size_constraints.2029852522.text_transformation", "NONE"),
),
},
{
Config: testAccAWSWafRegionalSizeConstraintSetConfig_changeConstraints(setName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSWafRegionalSizeConstraintSetExists("aws_wafregional_size_constraint_set.size_constraint_set", &after),
testAccCheckAWSWafRegionalSizeConstraintSetExists(resourceName, &after),
resource.TestCheckResourceAttr(
"aws_wafregional_size_constraint_set.size_constraint_set", "name", setName),
resourceName, "name", setName),
resource.TestCheckResourceAttr(
"aws_wafregional_size_constraint_set.size_constraint_set", "size_constraints.#", "1"),
resourceName, "size_constraints.#", "1"),
resource.TestCheckResourceAttr(
"aws_wafregional_size_constraint_set.size_constraint_set", "size_constraints.3222308386.comparison_operator", "GE"),
resourceName, "size_constraints.3222308386.comparison_operator", "GE"),
resource.TestCheckResourceAttr(
"aws_wafregional_size_constraint_set.size_constraint_set", "size_constraints.3222308386.field_to_match.#", "1"),
resourceName, "size_constraints.3222308386.field_to_match.#", "1"),
resource.TestCheckResourceAttr(
"aws_wafregional_size_constraint_set.size_constraint_set", "size_constraints.3222308386.field_to_match.281401076.data", ""),
resourceName, "size_constraints.3222308386.field_to_match.281401076.data", ""),
resource.TestCheckResourceAttr(
"aws_wafregional_size_constraint_set.size_constraint_set", "size_constraints.3222308386.field_to_match.281401076.type", "BODY"),
resourceName, "size_constraints.3222308386.field_to_match.281401076.type", "BODY"),
resource.TestCheckResourceAttr(
"aws_wafregional_size_constraint_set.size_constraint_set", "size_constraints.3222308386.size", "1024"),
resourceName, "size_constraints.3222308386.size", "1024"),
resource.TestCheckResourceAttr(
"aws_wafregional_size_constraint_set.size_constraint_set", "size_constraints.3222308386.text_transformation", "NONE"),
resourceName, "size_constraints.3222308386.text_transformation", "NONE"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAWSWafRegionalSizeConstraintSet_noConstraints(t *testing.T) {
var constraints waf.SizeConstraintSet
setName := fmt.Sprintf("sizeConstraintSet-%s", acctest.RandString(5))
resourceName := "aws_wafregional_size_constraint_set.size_constraint_set"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -172,13 +192,18 @@ func TestAccAWSWafRegionalSizeConstraintSet_noConstraints(t *testing.T) {
{
Config: testAccAWSWafRegionalSizeConstraintSetConfig_noConstraints(setName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSWafRegionalSizeConstraintSetExists("aws_wafregional_size_constraint_set.size_constraint_set", &constraints),
testAccCheckAWSWafRegionalSizeConstraintSetExists(resourceName, &constraints),
resource.TestCheckResourceAttr(
"aws_wafregional_size_constraint_set.size_constraint_set", "name", setName),
resourceName, "name", setName),
resource.TestCheckResourceAttr(
"aws_wafregional_size_constraint_set.size_constraint_set", "size_constraints.#", "0"),
resourceName, "size_constraints.#", "0"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/wafregional_size_constraint_set.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,11 @@ The following arguments are supported:
In addition to all arguments above, the following attributes are exported:

* `id` - The ID of the WAF Size Constraint Set.

## Import

WAF Size Constraint Set can be imported using the id, e.g.

```
$ terraform import aws_wafregional_size_constraint_set.size_constraint_set a1b2c3d4-d5f6-7777-8888-9999aaaabbbbcccc
```