github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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  		wr := newWafRetryer(conn, "global")
   100  		_, err := wr.RetryWithToken(func(token *string) (interface{}, error) {
   101  			req := &waf.UpdateByteMatchSetInput{
   102  				ChangeToken:    token,
   103  				ByteMatchSetId: v.ByteMatchSetId,
   104  			}
   105  
   106  			for _, ByteMatchTuple := range v.ByteMatchTuples {
   107  				ByteMatchUpdate := &waf.ByteMatchSetUpdate{
   108  					Action: aws.String("DELETE"),
   109  					ByteMatchTuple: &waf.ByteMatchTuple{
   110  						FieldToMatch:         ByteMatchTuple.FieldToMatch,
   111  						PositionalConstraint: ByteMatchTuple.PositionalConstraint,
   112  						TargetString:         ByteMatchTuple.TargetString,
   113  						TextTransformation:   ByteMatchTuple.TextTransformation,
   114  					},
   115  				}
   116  				req.Updates = append(req.Updates, ByteMatchUpdate)
   117  			}
   118  
   119  			return conn.UpdateByteMatchSet(req)
   120  		})
   121  		if err != nil {
   122  			return errwrap.Wrapf("[ERROR] Error updating ByteMatchSet: {{err}}", err)
   123  		}
   124  
   125  		_, err = wr.RetryWithToken(func(token *string) (interface{}, error) {
   126  			opts := &waf.DeleteByteMatchSetInput{
   127  				ChangeToken:    token,
   128  				ByteMatchSetId: v.ByteMatchSetId,
   129  			}
   130  			return conn.DeleteByteMatchSet(opts)
   131  		})
   132  		if err != nil {
   133  			return errwrap.Wrapf("[ERROR] Error deleting ByteMatchSet: {{err}}", err)
   134  		}
   135  
   136  		return nil
   137  	}
   138  }
   139  
   140  func testAccCheckAWSWafByteMatchSetExists(n string, v *waf.ByteMatchSet) resource.TestCheckFunc {
   141  	return func(s *terraform.State) error {
   142  		rs, ok := s.RootModule().Resources[n]
   143  		if !ok {
   144  			return fmt.Errorf("Not found: %s", n)
   145  		}
   146  
   147  		if rs.Primary.ID == "" {
   148  			return fmt.Errorf("No WAF ByteMatchSet ID is set")
   149  		}
   150  
   151  		conn := testAccProvider.Meta().(*AWSClient).wafconn
   152  		resp, err := conn.GetByteMatchSet(&waf.GetByteMatchSetInput{
   153  			ByteMatchSetId: aws.String(rs.Primary.ID),
   154  		})
   155  
   156  		if err != nil {
   157  			return err
   158  		}
   159  
   160  		if *resp.ByteMatchSet.ByteMatchSetId == rs.Primary.ID {
   161  			*v = *resp.ByteMatchSet
   162  			return nil
   163  		}
   164  
   165  		return fmt.Errorf("WAF ByteMatchSet (%s) not found", rs.Primary.ID)
   166  	}
   167  }
   168  
   169  func testAccCheckAWSWafByteMatchSetDestroy(s *terraform.State) error {
   170  	for _, rs := range s.RootModule().Resources {
   171  		if rs.Type != "aws_waf_byte_match_set" {
   172  			continue
   173  		}
   174  
   175  		conn := testAccProvider.Meta().(*AWSClient).wafconn
   176  		resp, err := conn.GetByteMatchSet(
   177  			&waf.GetByteMatchSetInput{
   178  				ByteMatchSetId: aws.String(rs.Primary.ID),
   179  			})
   180  
   181  		if err == nil {
   182  			if *resp.ByteMatchSet.ByteMatchSetId == rs.Primary.ID {
   183  				return fmt.Errorf("WAF ByteMatchSet %s still exists", rs.Primary.ID)
   184  			}
   185  		}
   186  
   187  		// Return nil if the ByteMatchSet is already destroyed
   188  		if awsErr, ok := err.(awserr.Error); ok {
   189  			if awsErr.Code() == "WAFNonexistentItemException" {
   190  				return nil
   191  			}
   192  		}
   193  
   194  		return err
   195  	}
   196  
   197  	return nil
   198  }
   199  
   200  func testAccAWSWafByteMatchSetConfig(name string) string {
   201  	return fmt.Sprintf(`
   202  resource "aws_waf_byte_match_set" "byte_set" {
   203    name = "%s"
   204    byte_match_tuples {
   205      text_transformation = "NONE"
   206      target_string = "badrefer1"
   207      positional_constraint = "CONTAINS"
   208      field_to_match {
   209        type = "HEADER"
   210        data = "referer"
   211      }
   212    }
   213  
   214    byte_match_tuples {
   215      text_transformation = "NONE"
   216      target_string = "badrefer2"
   217      positional_constraint = "CONTAINS"
   218      field_to_match {
   219        type = "HEADER"
   220        data = "referer"
   221      }
   222    }
   223  }`, name)
   224  }
   225  
   226  func testAccAWSWafByteMatchSetConfigChangeName(name string) string {
   227  	return fmt.Sprintf(`
   228  resource "aws_waf_byte_match_set" "byte_set" {
   229    name = "%s"
   230    byte_match_tuples {
   231      text_transformation = "NONE"
   232      target_string = "badrefer1"
   233      positional_constraint = "CONTAINS"
   234      field_to_match {
   235        type = "HEADER"
   236        data = "referer"
   237      }
   238    }
   239  
   240    byte_match_tuples {
   241      text_transformation = "NONE"
   242      target_string = "badrefer2"
   243      positional_constraint = "CONTAINS"
   244      field_to_match {
   245        type = "HEADER"
   246        data = "referer"
   247      }
   248    }
   249  }`, name)
   250  }