github.com/bradfeehan/terraform@v0.7.0-rc3.0.20170529055808-34b45c5ad841/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("aws_waf_byte_match_set.byte_set", "name", byteMatchSet),
    31  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.#", "2"),
    32  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.2174619346.field_to_match.#", "1"),
    33  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.2174619346.field_to_match.2991901334.data", "referer"),
    34  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.2174619346.field_to_match.2991901334.type", "HEADER"),
    35  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.2174619346.positional_constraint", "CONTAINS"),
    36  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.2174619346.target_string", "badrefer1"),
    37  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.2174619346.text_transformation", "NONE"),
    38  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.839525137.field_to_match.#", "1"),
    39  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.839525137.field_to_match.2991901334.data", "referer"),
    40  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.839525137.field_to_match.2991901334.type", "HEADER"),
    41  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.839525137.positional_constraint", "CONTAINS"),
    42  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.839525137.target_string", "badrefer2"),
    43  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.839525137.text_transformation", "NONE"),
    44  				),
    45  			},
    46  		},
    47  	})
    48  }
    49  
    50  func TestAccAWSWafByteMatchSet_changeNameForceNew(t *testing.T) {
    51  	var before, after waf.ByteMatchSet
    52  	byteMatchSet := fmt.Sprintf("byteMatchSet-%s", acctest.RandString(5))
    53  	byteMatchSetNewName := fmt.Sprintf("byteMatchSet-%s", acctest.RandString(5))
    54  
    55  	resource.Test(t, resource.TestCase{
    56  		PreCheck:     func() { testAccPreCheck(t) },
    57  		Providers:    testAccProviders,
    58  		CheckDestroy: testAccCheckAWSWafByteMatchSetDestroy,
    59  		Steps: []resource.TestStep{
    60  			{
    61  				Config: testAccAWSWafByteMatchSetConfig(byteMatchSet),
    62  				Check: resource.ComposeTestCheckFunc(
    63  					testAccCheckAWSWafByteMatchSetExists("aws_waf_byte_match_set.byte_set", &before),
    64  					resource.TestCheckResourceAttr(
    65  						"aws_waf_byte_match_set.byte_set", "name", byteMatchSet),
    66  					resource.TestCheckResourceAttr(
    67  						"aws_waf_byte_match_set.byte_set", "byte_match_tuples.#", "2"),
    68  				),
    69  			},
    70  			{
    71  				Config: testAccAWSWafByteMatchSetConfigChangeName(byteMatchSetNewName),
    72  				Check: resource.ComposeTestCheckFunc(
    73  					testAccCheckAWSWafByteMatchSetExists("aws_waf_byte_match_set.byte_set", &after),
    74  					resource.TestCheckResourceAttr(
    75  						"aws_waf_byte_match_set.byte_set", "name", byteMatchSetNewName),
    76  					resource.TestCheckResourceAttr(
    77  						"aws_waf_byte_match_set.byte_set", "byte_match_tuples.#", "2"),
    78  				),
    79  			},
    80  		},
    81  	})
    82  }
    83  
    84  func TestAccAWSWafByteMatchSet_changeTuples(t *testing.T) {
    85  	var before, after waf.ByteMatchSet
    86  	byteMatchSetName := fmt.Sprintf("byteMatchSet-%s", acctest.RandString(5))
    87  
    88  	resource.Test(t, resource.TestCase{
    89  		PreCheck:     func() { testAccPreCheck(t) },
    90  		Providers:    testAccProviders,
    91  		CheckDestroy: testAccCheckAWSWafByteMatchSetDestroy,
    92  		Steps: []resource.TestStep{
    93  			{
    94  				Config: testAccAWSWafByteMatchSetConfig(byteMatchSetName),
    95  				Check: resource.ComposeAggregateTestCheckFunc(
    96  					testAccCheckAWSWafByteMatchSetExists("aws_waf_byte_match_set.byte_set", &before),
    97  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "name", byteMatchSetName),
    98  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.#", "2"),
    99  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.2174619346.field_to_match.#", "1"),
   100  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.2174619346.field_to_match.2991901334.data", "referer"),
   101  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.2174619346.field_to_match.2991901334.type", "HEADER"),
   102  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.2174619346.positional_constraint", "CONTAINS"),
   103  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.2174619346.target_string", "badrefer1"),
   104  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.2174619346.text_transformation", "NONE"),
   105  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.839525137.field_to_match.#", "1"),
   106  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.839525137.field_to_match.2991901334.data", "referer"),
   107  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.839525137.field_to_match.2991901334.type", "HEADER"),
   108  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.839525137.positional_constraint", "CONTAINS"),
   109  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.839525137.target_string", "badrefer2"),
   110  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.839525137.text_transformation", "NONE"),
   111  				),
   112  			},
   113  			{
   114  				Config: testAccAWSWafByteMatchSetConfig_changeTuples(byteMatchSetName),
   115  				Check: resource.ComposeAggregateTestCheckFunc(
   116  					testAccCheckAWSWafByteMatchSetExists("aws_waf_byte_match_set.byte_set", &after),
   117  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "name", byteMatchSetName),
   118  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.#", "2"),
   119  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.2174619346.field_to_match.#", "1"),
   120  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.2174619346.field_to_match.2991901334.data", "referer"),
   121  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.2174619346.field_to_match.2991901334.type", "HEADER"),
   122  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.2174619346.positional_constraint", "CONTAINS"),
   123  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.2174619346.target_string", "badrefer1"),
   124  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.2174619346.text_transformation", "NONE"),
   125  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.4224486115.field_to_match.#", "1"),
   126  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.4224486115.field_to_match.4253810390.data", "GET"),
   127  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.4224486115.field_to_match.4253810390.type", "METHOD"),
   128  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.4224486115.positional_constraint", "CONTAINS_WORD"),
   129  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.4224486115.target_string", "blah"),
   130  					resource.TestCheckResourceAttr("aws_waf_byte_match_set.byte_set", "byte_match_tuples.4224486115.text_transformation", "URL_DECODE"),
   131  				),
   132  			},
   133  		},
   134  	})
   135  }
   136  
   137  func TestAccAWSWafByteMatchSet_noTuples(t *testing.T) {
   138  	var byteSet waf.ByteMatchSet
   139  	byteMatchSetName := fmt.Sprintf("byteMatchSet-%s", acctest.RandString(5))
   140  
   141  	resource.Test(t, resource.TestCase{
   142  		PreCheck:     func() { testAccPreCheck(t) },
   143  		Providers:    testAccProviders,
   144  		CheckDestroy: testAccCheckAWSWafByteMatchSetDestroy,
   145  		Steps: []resource.TestStep{
   146  			{
   147  				Config: testAccAWSWafByteMatchSetConfig_noTuples(byteMatchSetName),
   148  				Check: resource.ComposeAggregateTestCheckFunc(
   149  					testAccCheckAWSWafByteMatchSetExists("aws_waf_byte_match_set.byte_set", &byteSet),
   150  					resource.TestCheckResourceAttr(
   151  						"aws_waf_byte_match_set.byte_set", "name", byteMatchSetName),
   152  					resource.TestCheckResourceAttr(
   153  						"aws_waf_byte_match_set.byte_set", "byte_match_tuples.#", "0"),
   154  				),
   155  			},
   156  		},
   157  	})
   158  }
   159  
   160  func TestAccAWSWafByteMatchSet_disappears(t *testing.T) {
   161  	var v waf.ByteMatchSet
   162  	byteMatchSet := fmt.Sprintf("byteMatchSet-%s", acctest.RandString(5))
   163  
   164  	resource.Test(t, resource.TestCase{
   165  		PreCheck:     func() { testAccPreCheck(t) },
   166  		Providers:    testAccProviders,
   167  		CheckDestroy: testAccCheckAWSWafByteMatchSetDestroy,
   168  		Steps: []resource.TestStep{
   169  			{
   170  				Config: testAccAWSWafByteMatchSetConfig(byteMatchSet),
   171  				Check: resource.ComposeTestCheckFunc(
   172  					testAccCheckAWSWafByteMatchSetExists("aws_waf_byte_match_set.byte_set", &v),
   173  					testAccCheckAWSWafByteMatchSetDisappears(&v),
   174  				),
   175  				ExpectNonEmptyPlan: true,
   176  			},
   177  		},
   178  	})
   179  }
   180  
   181  func testAccCheckAWSWafByteMatchSetDisappears(v *waf.ByteMatchSet) resource.TestCheckFunc {
   182  	return func(s *terraform.State) error {
   183  		conn := testAccProvider.Meta().(*AWSClient).wafconn
   184  
   185  		wr := newWafRetryer(conn, "global")
   186  		_, err := wr.RetryWithToken(func(token *string) (interface{}, error) {
   187  			req := &waf.UpdateByteMatchSetInput{
   188  				ChangeToken:    token,
   189  				ByteMatchSetId: v.ByteMatchSetId,
   190  			}
   191  
   192  			for _, ByteMatchTuple := range v.ByteMatchTuples {
   193  				ByteMatchUpdate := &waf.ByteMatchSetUpdate{
   194  					Action: aws.String("DELETE"),
   195  					ByteMatchTuple: &waf.ByteMatchTuple{
   196  						FieldToMatch:         ByteMatchTuple.FieldToMatch,
   197  						PositionalConstraint: ByteMatchTuple.PositionalConstraint,
   198  						TargetString:         ByteMatchTuple.TargetString,
   199  						TextTransformation:   ByteMatchTuple.TextTransformation,
   200  					},
   201  				}
   202  				req.Updates = append(req.Updates, ByteMatchUpdate)
   203  			}
   204  
   205  			return conn.UpdateByteMatchSet(req)
   206  		})
   207  		if err != nil {
   208  			return errwrap.Wrapf("[ERROR] Error updating ByteMatchSet: {{err}}", err)
   209  		}
   210  
   211  		_, err = wr.RetryWithToken(func(token *string) (interface{}, error) {
   212  			opts := &waf.DeleteByteMatchSetInput{
   213  				ChangeToken:    token,
   214  				ByteMatchSetId: v.ByteMatchSetId,
   215  			}
   216  			return conn.DeleteByteMatchSet(opts)
   217  		})
   218  		if err != nil {
   219  			return errwrap.Wrapf("[ERROR] Error deleting ByteMatchSet: {{err}}", err)
   220  		}
   221  
   222  		return nil
   223  	}
   224  }
   225  
   226  func testAccCheckAWSWafByteMatchSetExists(n string, v *waf.ByteMatchSet) resource.TestCheckFunc {
   227  	return func(s *terraform.State) error {
   228  		rs, ok := s.RootModule().Resources[n]
   229  		if !ok {
   230  			return fmt.Errorf("Not found: %s", n)
   231  		}
   232  
   233  		if rs.Primary.ID == "" {
   234  			return fmt.Errorf("No WAF ByteMatchSet ID is set")
   235  		}
   236  
   237  		conn := testAccProvider.Meta().(*AWSClient).wafconn
   238  		resp, err := conn.GetByteMatchSet(&waf.GetByteMatchSetInput{
   239  			ByteMatchSetId: aws.String(rs.Primary.ID),
   240  		})
   241  
   242  		if err != nil {
   243  			return err
   244  		}
   245  
   246  		if *resp.ByteMatchSet.ByteMatchSetId == rs.Primary.ID {
   247  			*v = *resp.ByteMatchSet
   248  			return nil
   249  		}
   250  
   251  		return fmt.Errorf("WAF ByteMatchSet (%s) not found", rs.Primary.ID)
   252  	}
   253  }
   254  
   255  func testAccCheckAWSWafByteMatchSetDestroy(s *terraform.State) error {
   256  	for _, rs := range s.RootModule().Resources {
   257  		if rs.Type != "aws_waf_byte_match_set" {
   258  			continue
   259  		}
   260  
   261  		conn := testAccProvider.Meta().(*AWSClient).wafconn
   262  		resp, err := conn.GetByteMatchSet(
   263  			&waf.GetByteMatchSetInput{
   264  				ByteMatchSetId: aws.String(rs.Primary.ID),
   265  			})
   266  
   267  		if err == nil {
   268  			if *resp.ByteMatchSet.ByteMatchSetId == rs.Primary.ID {
   269  				return fmt.Errorf("WAF ByteMatchSet %s still exists", rs.Primary.ID)
   270  			}
   271  		}
   272  
   273  		// Return nil if the ByteMatchSet is already destroyed
   274  		if awsErr, ok := err.(awserr.Error); ok {
   275  			if awsErr.Code() == "WAFNonexistentItemException" {
   276  				return nil
   277  			}
   278  		}
   279  
   280  		return err
   281  	}
   282  
   283  	return nil
   284  }
   285  
   286  func testAccAWSWafByteMatchSetConfig(name string) string {
   287  	return fmt.Sprintf(`
   288  resource "aws_waf_byte_match_set" "byte_set" {
   289    name = "%s"
   290    byte_match_tuples {
   291      text_transformation = "NONE"
   292      target_string = "badrefer1"
   293      positional_constraint = "CONTAINS"
   294      field_to_match {
   295        type = "HEADER"
   296        data = "referer"
   297      }
   298    }
   299  
   300    byte_match_tuples {
   301      text_transformation = "NONE"
   302      target_string = "badrefer2"
   303      positional_constraint = "CONTAINS"
   304      field_to_match {
   305        type = "HEADER"
   306        data = "referer"
   307      }
   308    }
   309  }`, name)
   310  }
   311  
   312  func testAccAWSWafByteMatchSetConfigChangeName(name string) string {
   313  	return fmt.Sprintf(`
   314  resource "aws_waf_byte_match_set" "byte_set" {
   315    name = "%s"
   316    byte_match_tuples {
   317      text_transformation = "NONE"
   318      target_string = "badrefer1"
   319      positional_constraint = "CONTAINS"
   320      field_to_match {
   321        type = "HEADER"
   322        data = "referer"
   323      }
   324    }
   325  
   326    byte_match_tuples {
   327      text_transformation = "NONE"
   328      target_string = "badrefer2"
   329      positional_constraint = "CONTAINS"
   330      field_to_match {
   331        type = "HEADER"
   332        data = "referer"
   333      }
   334    }
   335  }`, name)
   336  }
   337  
   338  func testAccAWSWafByteMatchSetConfig_changeTuples(name string) string {
   339  	return fmt.Sprintf(`
   340  resource "aws_waf_byte_match_set" "byte_set" {
   341    name = "%s"
   342    byte_match_tuples {
   343      text_transformation = "NONE"
   344      target_string = "badrefer1"
   345      positional_constraint = "CONTAINS"
   346      field_to_match {
   347        type = "HEADER"
   348        data = "referer"
   349      }
   350    }
   351  
   352    byte_match_tuples {
   353      text_transformation = "URL_DECODE"
   354      target_string = "blah"
   355      positional_constraint = "CONTAINS_WORD"
   356      field_to_match {
   357        type = "METHOD"
   358        data = "GET"
   359      }
   360    }
   361  }`, name)
   362  }
   363  
   364  func testAccAWSWafByteMatchSetConfig_noTuples(name string) string {
   365  	return fmt.Sprintf(`
   366  resource "aws_waf_byte_match_set" "byte_set" {
   367    name = "%s"
   368  }`, name)
   369  }