Skip to content

Commit

Permalink
Merge pull request #14962 from terraform-providers/td-waf-change-tupl…
Browse files Browse the repository at this point in the history
…es-tests

tech-debt/waf_xss_match_set: update test configuration params, disappears methods, and enumerated vals
  • Loading branch information
anGie44 authored Sep 2, 2020
2 parents 393eb26 + 7ed2b1d commit 8bc472a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 145 deletions.
43 changes: 15 additions & 28 deletions aws/resource_aws_waf_xss_match_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,32 +47,17 @@ func resourceAwsWafXssMatchSet() *schema.Resource {
Optional: true,
},
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
waf.MatchFieldTypeUri,
waf.MatchFieldTypeSingleQueryArg,
waf.MatchFieldTypeQueryString,
waf.MatchFieldTypeMethod,
waf.MatchFieldTypeHeader,
waf.MatchFieldTypeBody,
waf.MatchFieldTypeAllQueryArgs,
}, false),
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(waf.MatchFieldType_Values(), false),
},
},
},
},
"text_transformation": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
waf.TextTransformationUrlDecode,
waf.TextTransformationNone,
waf.TextTransformationHtmlEntityDecode,
waf.TextTransformationCompressWhiteSpace,
waf.TextTransformationCmdLine,
waf.TextTransformationLowercase,
}, false),
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(waf.TextTransformation_Values(), false),
},
},
},
Expand Down Expand Up @@ -100,12 +85,12 @@ func resourceAwsWafXssMatchSetCreate(d *schema.ResourceData, meta interface{}) e
}
resp := out.(*waf.CreateXssMatchSetOutput)

d.SetId(*resp.XssMatchSet.XssMatchSetId)
d.SetId(aws.StringValue(resp.XssMatchSet.XssMatchSetId))

if v, ok := d.GetOk("xss_match_tuples"); ok && v.(*schema.Set).Len() > 0 {
err := updateXssMatchSetResource(d.Id(), nil, v.(*schema.Set).List(), conn)
if err != nil {
return fmt.Errorf("Error setting WAF XSS Match Set tuples: %s", err)
return fmt.Errorf("Error setting WAF XSS Match Set tuples: %w", err)
}
}
return resourceAwsWafXssMatchSetRead(d, meta)
Expand All @@ -130,7 +115,9 @@ func resourceAwsWafXssMatchSetRead(d *schema.ResourceData, meta interface{}) err
}

d.Set("name", resp.XssMatchSet.Name)
d.Set("xss_match_tuples", flattenWafXssMatchTuples(resp.XssMatchSet.XssMatchTuples))
if err := d.Set("xss_match_tuples", flattenWafXssMatchTuples(resp.XssMatchSet.XssMatchTuples)); err != nil {
return fmt.Errorf("error setting xss_match_tuples: %w", err)
}

arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Expand All @@ -152,7 +139,7 @@ func resourceAwsWafXssMatchSetUpdate(d *schema.ResourceData, meta interface{}) e

err := updateXssMatchSetResource(d.Id(), oldT, newT, conn)
if err != nil {
return fmt.Errorf("Error updating WAF XSS Match Set: %s", err)
return fmt.Errorf("Error updating WAF XSS Match Set: %w", err)
}
}

Expand All @@ -166,7 +153,7 @@ func resourceAwsWafXssMatchSetDelete(d *schema.ResourceData, meta interface{}) e
if len(oldTuples) > 0 {
err := updateXssMatchSetResource(d.Id(), oldTuples, nil, conn)
if err != nil {
return fmt.Errorf("Error removing WAF XSS Match Set tuples: %s", err)
return fmt.Errorf("Error removing WAF XSS Match Set tuples: %w", err)
}
}

Expand All @@ -180,7 +167,7 @@ func resourceAwsWafXssMatchSetDelete(d *schema.ResourceData, meta interface{}) e
return conn.DeleteXssMatchSet(req)
})
if err != nil {
return fmt.Errorf("Error deleting WAF XSS Match Set: %s", err)
return fmt.Errorf("Error deleting WAF XSS Match Set: %w", err)
}

return nil
Expand All @@ -199,7 +186,7 @@ func updateXssMatchSetResource(id string, oldT, newT []interface{}, conn *waf.WA
return conn.UpdateXssMatchSet(req)
})
if err != nil {
return fmt.Errorf("Error updating WAF XSS Match Set: %s", err)
return fmt.Errorf("Error updating WAF XSS Match Set: %w", err)
}

return nil
Expand Down
46 changes: 2 additions & 44 deletions aws/resource_aws_waf_xss_match_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestAccAWSWafXssMatchSet_disappears(t *testing.T) {
Config: testAccAWSWafXssMatchSetConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSWafXssMatchSetExists(resourceName, &v),
testAccCheckAWSWafXssMatchSetDisappears(&v),
testAccCheckResourceDisappears(testAccProvider, resourceAwsWafXssMatchSet(), resourceName),
),
ExpectNonEmptyPlan: true,
},
Expand Down Expand Up @@ -149,7 +149,7 @@ func TestAccAWSWafXssMatchSet_changeTuples(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "xss_match_tuples.#", "2"),
tfawsresource.TestCheckTypeSetElemNestedAttrs(resourceName, "xss_match_tuples.*", map[string]string{
"field_to_match.#": "1",
"field_to_match.0.data": "GET",
"field_to_match.0.data": "",
"field_to_match.0.type": "METHOD",
"text_transformation": "HTML_ENTITY_DECODE",
}),
Expand Down Expand Up @@ -197,47 +197,6 @@ func TestAccAWSWafXssMatchSet_noTuples(t *testing.T) {
})
}

func testAccCheckAWSWafXssMatchSetDisappears(v *waf.XssMatchSet) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).wafconn

wr := newWafRetryer(conn)
_, err := wr.RetryWithToken(func(token *string) (interface{}, error) {
req := &waf.UpdateXssMatchSetInput{
ChangeToken: token,
XssMatchSetId: v.XssMatchSetId,
}

for _, xssMatchTuple := range v.XssMatchTuples {
xssMatchTupleUpdate := &waf.XssMatchSetUpdate{
Action: aws.String(waf.ChangeActionDelete),
XssMatchTuple: &waf.XssMatchTuple{
FieldToMatch: xssMatchTuple.FieldToMatch,
TextTransformation: xssMatchTuple.TextTransformation,
},
}
req.Updates = append(req.Updates, xssMatchTupleUpdate)
}
return conn.UpdateXssMatchSet(req)
})
if err != nil {
return fmt.Errorf("Error updating XssMatchSet: %s", err)
}

_, err = wr.RetryWithToken(func(token *string) (interface{}, error) {
opts := &waf.DeleteXssMatchSetInput{
ChangeToken: token,
XssMatchSetId: v.XssMatchSetId,
}
return conn.DeleteXssMatchSet(opts)
})
if err != nil {
return fmt.Errorf("Error deleting WAF XSS Match Set: %s", err)
}
return nil
}
}

func testAccCheckAWSWafXssMatchSetExists(n string, v *waf.XssMatchSet) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -362,7 +321,6 @@ resource "aws_waf_xss_match_set" "test" {
field_to_match {
type = "METHOD"
data = "GET"
}
}
}
Expand Down
43 changes: 15 additions & 28 deletions aws/resource_aws_wafregional_xss_match_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,17 @@ func resourceAwsWafRegionalXssMatchSet() *schema.Resource {
Optional: true,
},
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
wafregional.MatchFieldTypeUri,
wafregional.MatchFieldTypeSingleQueryArg,
wafregional.MatchFieldTypeQueryString,
wafregional.MatchFieldTypeMethod,
wafregional.MatchFieldTypeHeader,
wafregional.MatchFieldTypeBody,
wafregional.MatchFieldTypeAllQueryArgs,
}, false),
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(wafregional.MatchFieldType_Values(), false),
},
},
},
},
"text_transformation": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
wafregional.TextTransformationUrlDecode,
wafregional.TextTransformationNone,
wafregional.TextTransformationHtmlEntityDecode,
wafregional.TextTransformationCompressWhiteSpace,
wafregional.TextTransformationCmdLine,
wafregional.TextTransformationLowercase,
}, false),
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(wafregional.TextTransformation_Values(), false),
},
},
},
Expand All @@ -93,7 +78,7 @@ func resourceAwsWafRegionalXssMatchSetCreate(d *schema.ResourceData, meta interf
return conn.CreateXssMatchSet(params)
})
if err != nil {
return fmt.Errorf("Failed creating regional WAF XSS Match Set: %s", err)
return fmt.Errorf("Failed creating regional WAF XSS Match Set: %w", err)
}
resp := out.(*waf.CreateXssMatchSetOutput)

Expand All @@ -102,7 +87,7 @@ func resourceAwsWafRegionalXssMatchSetCreate(d *schema.ResourceData, meta interf
if v, ok := d.Get("xss_match_tuple").(*schema.Set); ok && v.Len() > 0 {
err := updateXssMatchSetResourceWR(d.Id(), nil, v.List(), conn, region)
if err != nil {
return fmt.Errorf("Failed updating regional WAF XSS Match Set: %s", err)
return fmt.Errorf("Failed updating regional WAF XSS Match Set: %w", err)
}
}

Expand All @@ -129,7 +114,9 @@ func resourceAwsWafRegionalXssMatchSetRead(d *schema.ResourceData, meta interfac

set := resp.XssMatchSet

d.Set("xss_match_tuple", flattenWafXssMatchTuples(set.XssMatchTuples))
if err := d.Set("xss_match_tuple", flattenWafXssMatchTuples(set.XssMatchTuples)); err != nil {
return fmt.Errorf("error setting xss_match_tuple: %w", err)
}
d.Set("name", set.Name)

return nil
Expand All @@ -145,7 +132,7 @@ func resourceAwsWafRegionalXssMatchSetUpdate(d *schema.ResourceData, meta interf

err := updateXssMatchSetResourceWR(d.Id(), oldT, newT, conn, region)
if err != nil {
return fmt.Errorf("Failed updating regional WAF XSS Match Set: %s", err)
return fmt.Errorf("Failed updating regional WAF XSS Match Set: %w", err)
}
}

Expand All @@ -162,7 +149,7 @@ func resourceAwsWafRegionalXssMatchSetDelete(d *schema.ResourceData, meta interf
noTuples := []interface{}{}
err := updateXssMatchSetResourceWR(d.Id(), oldTuples, noTuples, conn, region)
if err != nil {
return fmt.Errorf("Error updating regional WAF XSS Match Set: %s", err)
return fmt.Errorf("Error updating regional WAF XSS Match Set: %w", err)
}
}
}
Expand All @@ -177,7 +164,7 @@ func resourceAwsWafRegionalXssMatchSetDelete(d *schema.ResourceData, meta interf
return conn.DeleteXssMatchSet(req)
})
if err != nil {
return fmt.Errorf("Failed deleting regional WAF XSS Match Set: %s", err)
return fmt.Errorf("Failed deleting regional WAF XSS Match Set: %w", err)
}

return nil
Expand All @@ -196,7 +183,7 @@ func updateXssMatchSetResourceWR(id string, oldT, newT []interface{}, conn *wafr
return conn.UpdateXssMatchSet(req)
})
if err != nil {
return fmt.Errorf("Failed updating regional WAF XSS Match Set: %s", err)
return fmt.Errorf("Failed updating regional WAF XSS Match Set: %w", err)
}

return nil
Expand Down
47 changes: 2 additions & 45 deletions aws/resource_aws_wafregional_xss_match_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestAccAWSWafRegionalXssMatchSet_disappears(t *testing.T) {
Config: testAccAWSWafRegionalXssMatchSetConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSWafRegionalXssMatchSetExists(resourceName, &v),
testAccCheckAWSWafRegionalXssMatchSetDisappears(&v),
testAccCheckResourceDisappears(testAccProvider, resourceAwsWafRegionalXssMatchSet(), resourceName),
),
ExpectNonEmptyPlan: true,
},
Expand Down Expand Up @@ -153,7 +153,7 @@ func TestAccAWSWafRegionalXssMatchSet_changeTuples(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "xss_match_tuple.#", "2"),
tfawsresource.TestCheckTypeSetElemNestedAttrs(resourceName, "xss_match_tuple.*", map[string]string{
"field_to_match.#": "1",
"field_to_match.0.data": "GET",
"field_to_match.0.data": "",
"field_to_match.0.type": "METHOD",
"text_transformation": "HTML_ENTITY_DECODE",
}),
Expand Down Expand Up @@ -196,48 +196,6 @@ func TestAccAWSWafRegionalXssMatchSet_noTuples(t *testing.T) {
})
}

func testAccCheckAWSWafRegionalXssMatchSetDisappears(v *waf.XssMatchSet) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).wafregionalconn
region := testAccProvider.Meta().(*AWSClient).region

wr := newWafRegionalRetryer(conn, region)
_, err := wr.RetryWithToken(func(token *string) (interface{}, error) {
req := &waf.UpdateXssMatchSetInput{
ChangeToken: token,
XssMatchSetId: v.XssMatchSetId,
}

for _, xssMatchTuple := range v.XssMatchTuples {
xssMatchTupleUpdate := &waf.XssMatchSetUpdate{
Action: aws.String(wafregional.ChangeActionDelete),
XssMatchTuple: &waf.XssMatchTuple{
FieldToMatch: xssMatchTuple.FieldToMatch,
TextTransformation: xssMatchTuple.TextTransformation,
},
}
req.Updates = append(req.Updates, xssMatchTupleUpdate)
}
return conn.UpdateXssMatchSet(req)
})
if err != nil {
return fmt.Errorf("Error updating regional WAF XSS Match Set: %s", err)
}

_, err = wr.RetryWithToken(func(token *string) (interface{}, error) {
opts := &waf.DeleteXssMatchSetInput{
ChangeToken: token,
XssMatchSetId: v.XssMatchSetId,
}
return conn.DeleteXssMatchSet(opts)
})
if err != nil {
return fmt.Errorf("Error deleting regional WAF XSS Match Set: %s", err)
}
return nil
}
}

func testAccCheckAWSWafRegionalXssMatchSetExists(n string, v *waf.XssMatchSet) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -362,7 +320,6 @@ resource "aws_wafregional_xss_match_set" "test" {
field_to_match {
type = "METHOD"
data = "GET"
}
}
}
Expand Down

0 comments on commit 8bc472a

Please sign in to comment.