github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_waf_byte_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 TestAccAWSWafByteMatchSet_basic(t *testing.T) { 18 var v waf.ByteMatchSet 19 byteMatchSet := fmt.Sprintf("byteMatchSet-%s", acctest.RandString(5)) 20 21 resource.Test(t, resource.TestCase{ 22 PreCheck: func() { testAccPreCheck(t) }, 23 Providers: testAccProviders, 24 CheckDestroy: testAccCheckAWSWafByteMatchSetDestroy, 25 Steps: []resource.TestStep{ 26 resource.TestStep{ 27 Config: testAccAWSWafByteMatchSetConfig(byteMatchSet), 28 Check: resource.ComposeTestCheckFunc( 29 testAccCheckAWSWafByteMatchSetExists("aws_waf_byte_match_set.byte_set", &v), 30 resource.TestCheckResourceAttr( 31 "aws_waf_byte_match_set.byte_set", "name", byteMatchSet), 32 resource.TestCheckResourceAttr( 33 "aws_waf_byte_match_set.byte_set", "byte_match_tuples.#", "2"), 34 ), 35 }, 36 }, 37 }) 38 } 39 40 func TestAccAWSWafByteMatchSet_changeNameForceNew(t *testing.T) { 41 var before, after waf.ByteMatchSet 42 byteMatchSet := fmt.Sprintf("byteMatchSet-%s", acctest.RandString(5)) 43 byteMatchSetNewName := fmt.Sprintf("byteMatchSet-%s", acctest.RandString(5)) 44 45 resource.Test(t, resource.TestCase{ 46 PreCheck: func() { testAccPreCheck(t) }, 47 Providers: testAccProviders, 48 CheckDestroy: testAccCheckAWSWafByteMatchSetDestroy, 49 Steps: []resource.TestStep{ 50 { 51 Config: testAccAWSWafByteMatchSetConfig(byteMatchSet), 52 Check: resource.ComposeTestCheckFunc( 53 testAccCheckAWSWafByteMatchSetExists("aws_waf_byte_match_set.byte_set", &before), 54 resource.TestCheckResourceAttr( 55 "aws_waf_byte_match_set.byte_set", "name", byteMatchSet), 56 resource.TestCheckResourceAttr( 57 "aws_waf_byte_match_set.byte_set", "byte_match_tuples.#", "2"), 58 ), 59 }, 60 { 61 Config: testAccAWSWafByteMatchSetConfigChangeName(byteMatchSetNewName), 62 Check: resource.ComposeTestCheckFunc( 63 testAccCheckAWSWafByteMatchSetExists("aws_waf_byte_match_set.byte_set", &after), 64 resource.TestCheckResourceAttr( 65 "aws_waf_byte_match_set.byte_set", "name", byteMatchSetNewName), 66 resource.TestCheckResourceAttr( 67 "aws_waf_byte_match_set.byte_set", "byte_match_tuples.#", "2"), 68 ), 69 }, 70 }, 71 }) 72 } 73 74 func TestAccAWSWafByteMatchSet_disappears(t *testing.T) { 75 var v waf.ByteMatchSet 76 byteMatchSet := fmt.Sprintf("byteMatchSet-%s", acctest.RandString(5)) 77 78 resource.Test(t, resource.TestCase{ 79 PreCheck: func() { testAccPreCheck(t) }, 80 Providers: testAccProviders, 81 CheckDestroy: testAccCheckAWSWafByteMatchSetDestroy, 82 Steps: []resource.TestStep{ 83 { 84 Config: testAccAWSWafByteMatchSetConfig(byteMatchSet), 85 Check: resource.ComposeTestCheckFunc( 86 testAccCheckAWSWafByteMatchSetExists("aws_waf_byte_match_set.byte_set", &v), 87 testAccCheckAWSWafByteMatchSetDisappears(&v), 88 ), 89 ExpectNonEmptyPlan: true, 90 }, 91 }, 92 }) 93 } 94 95 func testAccCheckAWSWafByteMatchSetDisappears(v *waf.ByteMatchSet) resource.TestCheckFunc { 96 return func(s *terraform.State) error { 97 conn := testAccProvider.Meta().(*AWSClient).wafconn 98 99 // ChangeToken 100 var ct *waf.GetChangeTokenInput 101 102 resp, err := conn.GetChangeToken(ct) 103 if err != nil { 104 return fmt.Errorf("Error getting change token: %s", err) 105 } 106 107 req := &waf.UpdateByteMatchSetInput{ 108 ChangeToken: resp.ChangeToken, 109 ByteMatchSetId: v.ByteMatchSetId, 110 } 111 112 for _, ByteMatchTuple := range v.ByteMatchTuples { 113 ByteMatchUpdate := &waf.ByteMatchSetUpdate{ 114 Action: aws.String("DELETE"), 115 ByteMatchTuple: &waf.ByteMatchTuple{ 116 FieldToMatch: ByteMatchTuple.FieldToMatch, 117 PositionalConstraint: ByteMatchTuple.PositionalConstraint, 118 TargetString: ByteMatchTuple.TargetString, 119 TextTransformation: ByteMatchTuple.TextTransformation, 120 }, 121 } 122 req.Updates = append(req.Updates, ByteMatchUpdate) 123 } 124 125 _, err = conn.UpdateByteMatchSet(req) 126 if err != nil { 127 return errwrap.Wrapf("[ERROR] Error updating ByteMatchSet: {{err}}", err) 128 } 129 130 resp, err = conn.GetChangeToken(ct) 131 if err != nil { 132 return errwrap.Wrapf("[ERROR] Error getting change token: {{err}}", err) 133 } 134 135 opts := &waf.DeleteByteMatchSetInput{ 136 ChangeToken: resp.ChangeToken, 137 ByteMatchSetId: v.ByteMatchSetId, 138 } 139 if _, err := conn.DeleteByteMatchSet(opts); err != nil { 140 return err 141 } 142 return nil 143 } 144 } 145 146 func testAccCheckAWSWafByteMatchSetExists(n string, v *waf.ByteMatchSet) resource.TestCheckFunc { 147 return func(s *terraform.State) error { 148 rs, ok := s.RootModule().Resources[n] 149 if !ok { 150 return fmt.Errorf("Not found: %s", n) 151 } 152 153 if rs.Primary.ID == "" { 154 return fmt.Errorf("No WAF ByteMatchSet ID is set") 155 } 156 157 conn := testAccProvider.Meta().(*AWSClient).wafconn 158 resp, err := conn.GetByteMatchSet(&waf.GetByteMatchSetInput{ 159 ByteMatchSetId: aws.String(rs.Primary.ID), 160 }) 161 162 if err != nil { 163 return err 164 } 165 166 if *resp.ByteMatchSet.ByteMatchSetId == rs.Primary.ID { 167 *v = *resp.ByteMatchSet 168 return nil 169 } 170 171 return fmt.Errorf("WAF ByteMatchSet (%s) not found", rs.Primary.ID) 172 } 173 } 174 175 func testAccCheckAWSWafByteMatchSetDestroy(s *terraform.State) error { 176 for _, rs := range s.RootModule().Resources { 177 if rs.Type != "aws_waf_byte_match_set" { 178 continue 179 } 180 181 conn := testAccProvider.Meta().(*AWSClient).wafconn 182 resp, err := conn.GetByteMatchSet( 183 &waf.GetByteMatchSetInput{ 184 ByteMatchSetId: aws.String(rs.Primary.ID), 185 }) 186 187 if err == nil { 188 if *resp.ByteMatchSet.ByteMatchSetId == rs.Primary.ID { 189 return fmt.Errorf("WAF ByteMatchSet %s still exists", rs.Primary.ID) 190 } 191 } 192 193 // Return nil if the ByteMatchSet is already destroyed 194 if awsErr, ok := err.(awserr.Error); ok { 195 if awsErr.Code() == "WAFNonexistentItemException" { 196 return nil 197 } 198 } 199 200 return err 201 } 202 203 return nil 204 } 205 206 func testAccAWSWafByteMatchSetConfig(name string) string { 207 return fmt.Sprintf(` 208 resource "aws_waf_byte_match_set" "byte_set" { 209 name = "%s" 210 byte_match_tuples { 211 text_transformation = "NONE" 212 target_string = "badrefer1" 213 positional_constraint = "CONTAINS" 214 field_to_match { 215 type = "HEADER" 216 data = "referer" 217 } 218 } 219 220 byte_match_tuples { 221 text_transformation = "NONE" 222 target_string = "badrefer2" 223 positional_constraint = "CONTAINS" 224 field_to_match { 225 type = "HEADER" 226 data = "referer" 227 } 228 } 229 }`, name) 230 } 231 232 func testAccAWSWafByteMatchSetConfigChangeName(name string) string { 233 return fmt.Sprintf(` 234 resource "aws_waf_byte_match_set" "byte_set" { 235 name = "%s" 236 byte_match_tuples { 237 text_transformation = "NONE" 238 target_string = "badrefer1" 239 positional_constraint = "CONTAINS" 240 field_to_match { 241 type = "HEADER" 242 data = "referer" 243 } 244 } 245 246 byte_match_tuples { 247 text_transformation = "NONE" 248 target_string = "badrefer2" 249 positional_constraint = "CONTAINS" 250 field_to_match { 251 type = "HEADER" 252 data = "referer" 253 } 254 } 255 }`, name) 256 }