github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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 var ct *waf.GetChangeTokenInput 100 101 resp, err := conn.GetChangeToken(ct) 102 if err != nil { 103 return fmt.Errorf("Error getting change token: %s", err) 104 } 105 106 req := &waf.UpdateXssMatchSetInput{ 107 ChangeToken: resp.ChangeToken, 108 XssMatchSetId: v.XssMatchSetId, 109 } 110 111 for _, xssMatchTuple := range v.XssMatchTuples { 112 xssMatchTupleUpdate := &waf.XssMatchSetUpdate{ 113 Action: aws.String("DELETE"), 114 XssMatchTuple: &waf.XssMatchTuple{ 115 FieldToMatch: xssMatchTuple.FieldToMatch, 116 TextTransformation: xssMatchTuple.TextTransformation, 117 }, 118 } 119 req.Updates = append(req.Updates, xssMatchTupleUpdate) 120 } 121 _, err = conn.UpdateXssMatchSet(req) 122 if err != nil { 123 return errwrap.Wrapf("[ERROR] Error updating XssMatchSet: {{err}}", err) 124 } 125 126 resp, err = conn.GetChangeToken(ct) 127 if err != nil { 128 return errwrap.Wrapf("[ERROR] Error getting change token: {{err}}", err) 129 } 130 131 opts := &waf.DeleteXssMatchSetInput{ 132 ChangeToken: resp.ChangeToken, 133 XssMatchSetId: v.XssMatchSetId, 134 } 135 if _, err := conn.DeleteXssMatchSet(opts); err != nil { 136 return err 137 } 138 return nil 139 } 140 } 141 142 func testAccCheckAWSWafXssMatchSetExists(n string, v *waf.XssMatchSet) resource.TestCheckFunc { 143 return func(s *terraform.State) error { 144 rs, ok := s.RootModule().Resources[n] 145 if !ok { 146 return fmt.Errorf("Not found: %s", n) 147 } 148 149 if rs.Primary.ID == "" { 150 return fmt.Errorf("No WAF XssMatchSet ID is set") 151 } 152 153 conn := testAccProvider.Meta().(*AWSClient).wafconn 154 resp, err := conn.GetXssMatchSet(&waf.GetXssMatchSetInput{ 155 XssMatchSetId: aws.String(rs.Primary.ID), 156 }) 157 158 if err != nil { 159 return err 160 } 161 162 if *resp.XssMatchSet.XssMatchSetId == rs.Primary.ID { 163 *v = *resp.XssMatchSet 164 return nil 165 } 166 167 return fmt.Errorf("WAF XssMatchSet (%s) not found", rs.Primary.ID) 168 } 169 } 170 171 func testAccCheckAWSWafXssMatchSetDestroy(s *terraform.State) error { 172 for _, rs := range s.RootModule().Resources { 173 if rs.Type != "aws_waf_byte_match_set" { 174 continue 175 } 176 177 conn := testAccProvider.Meta().(*AWSClient).wafconn 178 resp, err := conn.GetXssMatchSet( 179 &waf.GetXssMatchSetInput{ 180 XssMatchSetId: aws.String(rs.Primary.ID), 181 }) 182 183 if err == nil { 184 if *resp.XssMatchSet.XssMatchSetId == rs.Primary.ID { 185 return fmt.Errorf("WAF XssMatchSet %s still exists", rs.Primary.ID) 186 } 187 } 188 189 // Return nil if the XssMatchSet is already destroyed 190 if awsErr, ok := err.(awserr.Error); ok { 191 if awsErr.Code() == "WAFNonexistentItemException" { 192 return nil 193 } 194 } 195 196 return err 197 } 198 199 return nil 200 } 201 202 func testAccAWSWafXssMatchSetConfig(name string) string { 203 return fmt.Sprintf(` 204 resource "aws_waf_xss_match_set" "xss_match_set" { 205 name = "%s" 206 xss_match_tuples { 207 text_transformation = "NONE" 208 field_to_match { 209 type = "URI" 210 } 211 } 212 213 xss_match_tuples { 214 text_transformation = "NONE" 215 field_to_match { 216 type = "QUERY_STRING" 217 } 218 } 219 }`, name) 220 } 221 222 func testAccAWSWafXssMatchSetConfigChangeName(name string) string { 223 return fmt.Sprintf(` 224 resource "aws_waf_xss_match_set" "xss_match_set" { 225 name = "%s" 226 xss_match_tuples { 227 text_transformation = "NONE" 228 field_to_match { 229 type = "URI" 230 } 231 } 232 233 xss_match_tuples { 234 text_transformation = "NONE" 235 field_to_match { 236 type = "QUERY_STRING" 237 } 238 } 239 }`, name) 240 }