github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_waf_xss_match_set_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 10 "github.com/aws/aws-sdk-go/aws" 11 "github.com/aws/aws-sdk-go/aws/awserr" 12 "github.com/aws/aws-sdk-go/service/waf" 13 "github.com/hashicorp/errwrap" 14 "github.com/hashicorp/terraform/helper/acctest" 15 ) 16 17 func TestAccAWSWafXssMatchSet_basic(t *testing.T) { 18 var v waf.XssMatchSet 19 xssMatchSet := fmt.Sprintf("xssMatchSet-%s", acctest.RandString(5)) 20 21 resource.Test(t, resource.TestCase{ 22 PreCheck: func() { testAccPreCheck(t) }, 23 Providers: testAccProviders, 24 CheckDestroy: testAccCheckAWSWafXssMatchSetDestroy, 25 Steps: []resource.TestStep{ 26 resource.TestStep{ 27 Config: testAccAWSWafXssMatchSetConfig(xssMatchSet), 28 Check: resource.ComposeTestCheckFunc( 29 testAccCheckAWSWafXssMatchSetExists("aws_waf_xss_match_set.xss_match_set", &v), 30 resource.TestCheckResourceAttr( 31 "aws_waf_xss_match_set.xss_match_set", "name", xssMatchSet), 32 resource.TestCheckResourceAttr( 33 "aws_waf_xss_match_set.xss_match_set", "xss_match_tuples.#", "2"), 34 ), 35 }, 36 }, 37 }) 38 } 39 40 func TestAccAWSWafXssMatchSet_changeNameForceNew(t *testing.T) { 41 var before, after waf.XssMatchSet 42 xssMatchSet := fmt.Sprintf("xssMatchSet-%s", acctest.RandString(5)) 43 xssMatchSetNewName := fmt.Sprintf("xssMatchSetNewName-%s", acctest.RandString(5)) 44 45 resource.Test(t, resource.TestCase{ 46 PreCheck: func() { testAccPreCheck(t) }, 47 Providers: testAccProviders, 48 CheckDestroy: testAccCheckAWSWafXssMatchSetDestroy, 49 Steps: []resource.TestStep{ 50 { 51 Config: testAccAWSWafXssMatchSetConfig(xssMatchSet), 52 Check: resource.ComposeTestCheckFunc( 53 testAccCheckAWSWafXssMatchSetExists("aws_waf_xss_match_set.xss_match_set", &before), 54 resource.TestCheckResourceAttr( 55 "aws_waf_xss_match_set.xss_match_set", "name", xssMatchSet), 56 resource.TestCheckResourceAttr( 57 "aws_waf_xss_match_set.xss_match_set", "xss_match_tuples.#", "2"), 58 ), 59 }, 60 { 61 Config: testAccAWSWafXssMatchSetConfigChangeName(xssMatchSetNewName), 62 Check: resource.ComposeTestCheckFunc( 63 testAccCheckAWSWafXssMatchSetExists("aws_waf_xss_match_set.xss_match_set", &after), 64 resource.TestCheckResourceAttr( 65 "aws_waf_xss_match_set.xss_match_set", "name", xssMatchSetNewName), 66 resource.TestCheckResourceAttr( 67 "aws_waf_xss_match_set.xss_match_set", "xss_match_tuples.#", "2"), 68 ), 69 }, 70 }, 71 }) 72 } 73 74 func TestAccAWSWafXssMatchSet_disappears(t *testing.T) { 75 var v waf.XssMatchSet 76 xssMatchSet := fmt.Sprintf("xssMatchSet-%s", acctest.RandString(5)) 77 78 resource.Test(t, resource.TestCase{ 79 PreCheck: func() { testAccPreCheck(t) }, 80 Providers: testAccProviders, 81 CheckDestroy: testAccCheckAWSWafXssMatchSetDestroy, 82 Steps: []resource.TestStep{ 83 { 84 Config: testAccAWSWafXssMatchSetConfig(xssMatchSet), 85 Check: resource.ComposeTestCheckFunc( 86 testAccCheckAWSWafXssMatchSetExists("aws_waf_xss_match_set.xss_match_set", &v), 87 testAccCheckAWSWafXssMatchSetDisappears(&v), 88 ), 89 ExpectNonEmptyPlan: true, 90 }, 91 }, 92 }) 93 } 94 95 func testAccCheckAWSWafXssMatchSetDisappears(v *waf.XssMatchSet) resource.TestCheckFunc { 96 return func(s *terraform.State) error { 97 conn := testAccProvider.Meta().(*AWSClient).wafconn 98 99 wr := newWafRetryer(conn, "global") 100 _, err := wr.RetryWithToken(func(token *string) (interface{}, error) { 101 req := &waf.UpdateXssMatchSetInput{ 102 ChangeToken: token, 103 XssMatchSetId: v.XssMatchSetId, 104 } 105 106 for _, xssMatchTuple := range v.XssMatchTuples { 107 xssMatchTupleUpdate := &waf.XssMatchSetUpdate{ 108 Action: aws.String("DELETE"), 109 XssMatchTuple: &waf.XssMatchTuple{ 110 FieldToMatch: xssMatchTuple.FieldToMatch, 111 TextTransformation: xssMatchTuple.TextTransformation, 112 }, 113 } 114 req.Updates = append(req.Updates, xssMatchTupleUpdate) 115 } 116 return conn.UpdateXssMatchSet(req) 117 }) 118 if err != nil { 119 return errwrap.Wrapf("[ERROR] Error updating XssMatchSet: {{err}}", err) 120 } 121 122 _, err = wr.RetryWithToken(func(token *string) (interface{}, error) { 123 opts := &waf.DeleteXssMatchSetInput{ 124 ChangeToken: token, 125 XssMatchSetId: v.XssMatchSetId, 126 } 127 return conn.DeleteXssMatchSet(opts) 128 }) 129 if err != nil { 130 return errwrap.Wrapf("[ERROR] Error deleting XssMatchSet: {{err}}", err) 131 } 132 return nil 133 } 134 } 135 136 func testAccCheckAWSWafXssMatchSetExists(n string, v *waf.XssMatchSet) resource.TestCheckFunc { 137 return func(s *terraform.State) error { 138 rs, ok := s.RootModule().Resources[n] 139 if !ok { 140 return fmt.Errorf("Not found: %s", n) 141 } 142 143 if rs.Primary.ID == "" { 144 return fmt.Errorf("No WAF XssMatchSet ID is set") 145 } 146 147 conn := testAccProvider.Meta().(*AWSClient).wafconn 148 resp, err := conn.GetXssMatchSet(&waf.GetXssMatchSetInput{ 149 XssMatchSetId: aws.String(rs.Primary.ID), 150 }) 151 152 if err != nil { 153 return err 154 } 155 156 if *resp.XssMatchSet.XssMatchSetId == rs.Primary.ID { 157 *v = *resp.XssMatchSet 158 return nil 159 } 160 161 return fmt.Errorf("WAF XssMatchSet (%s) not found", rs.Primary.ID) 162 } 163 } 164 165 func testAccCheckAWSWafXssMatchSetDestroy(s *terraform.State) error { 166 for _, rs := range s.RootModule().Resources { 167 if rs.Type != "aws_waf_byte_match_set" { 168 continue 169 } 170 171 conn := testAccProvider.Meta().(*AWSClient).wafconn 172 resp, err := conn.GetXssMatchSet( 173 &waf.GetXssMatchSetInput{ 174 XssMatchSetId: aws.String(rs.Primary.ID), 175 }) 176 177 if err == nil { 178 if *resp.XssMatchSet.XssMatchSetId == rs.Primary.ID { 179 return fmt.Errorf("WAF XssMatchSet %s still exists", rs.Primary.ID) 180 } 181 } 182 183 // Return nil if the XssMatchSet is already destroyed 184 if awsErr, ok := err.(awserr.Error); ok { 185 if awsErr.Code() == "WAFNonexistentItemException" { 186 return nil 187 } 188 } 189 190 return err 191 } 192 193 return nil 194 } 195 196 func testAccAWSWafXssMatchSetConfig(name string) string { 197 return fmt.Sprintf(` 198 resource "aws_waf_xss_match_set" "xss_match_set" { 199 name = "%s" 200 xss_match_tuples { 201 text_transformation = "NONE" 202 field_to_match { 203 type = "URI" 204 } 205 } 206 207 xss_match_tuples { 208 text_transformation = "NONE" 209 field_to_match { 210 type = "QUERY_STRING" 211 } 212 } 213 }`, name) 214 } 215 216 func testAccAWSWafXssMatchSetConfigChangeName(name string) string { 217 return fmt.Sprintf(` 218 resource "aws_waf_xss_match_set" "xss_match_set" { 219 name = "%s" 220 xss_match_tuples { 221 text_transformation = "NONE" 222 field_to_match { 223 type = "URI" 224 } 225 } 226 227 xss_match_tuples { 228 text_transformation = "NONE" 229 field_to_match { 230 type = "QUERY_STRING" 231 } 232 } 233 }`, name) 234 }