github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_rds_cluster_parameter_group_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"regexp"
     7  	"testing"
     8  	"time"
     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/rds"
    13  	"github.com/hashicorp/terraform/helper/acctest"
    14  	"github.com/hashicorp/terraform/helper/resource"
    15  	"github.com/hashicorp/terraform/terraform"
    16  )
    17  
    18  func TestAccAWSDBClusterParameterGroup_basic(t *testing.T) {
    19  	var v rds.DBClusterParameterGroup
    20  
    21  	parameterGroupName := fmt.Sprintf("cluster-parameter-group-test-terraform-%d", acctest.RandInt())
    22  
    23  	resource.Test(t, resource.TestCase{
    24  		PreCheck:     func() { testAccPreCheck(t) },
    25  		Providers:    testAccProviders,
    26  		CheckDestroy: testAccCheckAWSDBClusterParameterGroupDestroy,
    27  		Steps: []resource.TestStep{
    28  			{
    29  				Config: testAccAWSDBClusterParameterGroupConfig(parameterGroupName),
    30  				Check: resource.ComposeTestCheckFunc(
    31  					testAccCheckAWSDBClusterParameterGroupExists("aws_rds_cluster_parameter_group.bar", &v),
    32  					testAccCheckAWSDBClusterParameterGroupAttributes(&v, parameterGroupName),
    33  					resource.TestCheckResourceAttr(
    34  						"aws_rds_cluster_parameter_group.bar", "name", parameterGroupName),
    35  					resource.TestCheckResourceAttr(
    36  						"aws_rds_cluster_parameter_group.bar", "family", "aurora5.6"),
    37  					resource.TestCheckResourceAttr(
    38  						"aws_rds_cluster_parameter_group.bar", "description", "Test cluster parameter group for terraform"),
    39  					resource.TestCheckResourceAttr(
    40  						"aws_rds_cluster_parameter_group.bar", "parameter.1708034931.name", "character_set_results"),
    41  					resource.TestCheckResourceAttr(
    42  						"aws_rds_cluster_parameter_group.bar", "parameter.1708034931.value", "utf8"),
    43  					resource.TestCheckResourceAttr(
    44  						"aws_rds_cluster_parameter_group.bar", "parameter.2421266705.name", "character_set_server"),
    45  					resource.TestCheckResourceAttr(
    46  						"aws_rds_cluster_parameter_group.bar", "parameter.2421266705.value", "utf8"),
    47  					resource.TestCheckResourceAttr(
    48  						"aws_rds_cluster_parameter_group.bar", "parameter.2478663599.name", "character_set_client"),
    49  					resource.TestCheckResourceAttr(
    50  						"aws_rds_cluster_parameter_group.bar", "parameter.2478663599.value", "utf8"),
    51  					resource.TestCheckResourceAttr(
    52  						"aws_rds_cluster_parameter_group.bar", "tags.%", "1"),
    53  				),
    54  			},
    55  			{
    56  				Config: testAccAWSDBClusterParameterGroupAddParametersConfig(parameterGroupName),
    57  				Check: resource.ComposeTestCheckFunc(
    58  					testAccCheckAWSDBClusterParameterGroupExists("aws_rds_cluster_parameter_group.bar", &v),
    59  					testAccCheckAWSDBClusterParameterGroupAttributes(&v, parameterGroupName),
    60  					resource.TestCheckResourceAttr(
    61  						"aws_rds_cluster_parameter_group.bar", "name", parameterGroupName),
    62  					resource.TestCheckResourceAttr(
    63  						"aws_rds_cluster_parameter_group.bar", "family", "aurora5.6"),
    64  					resource.TestCheckResourceAttr(
    65  						"aws_rds_cluster_parameter_group.bar", "description", "Test cluster parameter group for terraform"),
    66  					resource.TestCheckResourceAttr(
    67  						"aws_rds_cluster_parameter_group.bar", "parameter.1706463059.name", "collation_connection"),
    68  					resource.TestCheckResourceAttr(
    69  						"aws_rds_cluster_parameter_group.bar", "parameter.1706463059.value", "utf8_unicode_ci"),
    70  					resource.TestCheckResourceAttr(
    71  						"aws_rds_cluster_parameter_group.bar", "parameter.1708034931.name", "character_set_results"),
    72  					resource.TestCheckResourceAttr(
    73  						"aws_rds_cluster_parameter_group.bar", "parameter.1708034931.value", "utf8"),
    74  					resource.TestCheckResourceAttr(
    75  						"aws_rds_cluster_parameter_group.bar", "parameter.2421266705.name", "character_set_server"),
    76  					resource.TestCheckResourceAttr(
    77  						"aws_rds_cluster_parameter_group.bar", "parameter.2421266705.value", "utf8"),
    78  					resource.TestCheckResourceAttr(
    79  						"aws_rds_cluster_parameter_group.bar", "parameter.2475805061.name", "collation_server"),
    80  					resource.TestCheckResourceAttr(
    81  						"aws_rds_cluster_parameter_group.bar", "parameter.2475805061.value", "utf8_unicode_ci"),
    82  					resource.TestCheckResourceAttr(
    83  						"aws_rds_cluster_parameter_group.bar", "parameter.2478663599.name", "character_set_client"),
    84  					resource.TestCheckResourceAttr(
    85  						"aws_rds_cluster_parameter_group.bar", "parameter.2478663599.value", "utf8"),
    86  					resource.TestCheckResourceAttr(
    87  						"aws_rds_cluster_parameter_group.bar", "tags.%", "2"),
    88  				),
    89  			},
    90  		},
    91  	})
    92  }
    93  
    94  func TestAccAWSDBClusterParameterGroup_namePrefix(t *testing.T) {
    95  	var v rds.DBClusterParameterGroup
    96  
    97  	resource.Test(t, resource.TestCase{
    98  		PreCheck:     func() { testAccPreCheck(t) },
    99  		Providers:    testAccProviders,
   100  		CheckDestroy: testAccCheckAWSDBClusterParameterGroupDestroy,
   101  		Steps: []resource.TestStep{
   102  			{
   103  				Config: testAccAWSDBClusterParameterGroupConfig_namePrefix,
   104  				Check: resource.ComposeTestCheckFunc(
   105  					testAccCheckAWSDBClusterParameterGroupExists("aws_rds_cluster_parameter_group.test", &v),
   106  					resource.TestMatchResourceAttr(
   107  						"aws_rds_cluster_parameter_group.test", "name", regexp.MustCompile("^tf-test-")),
   108  				),
   109  			},
   110  		},
   111  	})
   112  }
   113  
   114  func TestAccAWSDBClusterParameterGroup_generatedName(t *testing.T) {
   115  	var v rds.DBClusterParameterGroup
   116  
   117  	resource.Test(t, resource.TestCase{
   118  		PreCheck:     func() { testAccPreCheck(t) },
   119  		Providers:    testAccProviders,
   120  		CheckDestroy: testAccCheckAWSDBClusterParameterGroupDestroy,
   121  		Steps: []resource.TestStep{
   122  			{
   123  				Config: testAccAWSDBClusterParameterGroupConfig_generatedName,
   124  				Check: resource.ComposeTestCheckFunc(
   125  					testAccCheckAWSDBClusterParameterGroupExists("aws_rds_cluster_parameter_group.test", &v),
   126  				),
   127  			},
   128  		},
   129  	})
   130  }
   131  
   132  func TestAccAWSDBClusterParameterGroup_disappears(t *testing.T) {
   133  	var v rds.DBClusterParameterGroup
   134  
   135  	parameterGroupName := fmt.Sprintf("cluster-parameter-group-test-terraform-%d", acctest.RandInt())
   136  
   137  	resource.Test(t, resource.TestCase{
   138  		PreCheck:     func() { testAccPreCheck(t) },
   139  		Providers:    testAccProviders,
   140  		CheckDestroy: testAccCheckAWSDBClusterParameterGroupDestroy,
   141  		Steps: []resource.TestStep{
   142  			{
   143  				Config: testAccAWSDBClusterParameterGroupConfig(parameterGroupName),
   144  				Check: resource.ComposeTestCheckFunc(
   145  					testAccCheckAWSDBClusterParameterGroupExists("aws_rds_cluster_parameter_group.bar", &v),
   146  					testAccAWSDBClusterParameterGroupDisappears(&v),
   147  				),
   148  				ExpectNonEmptyPlan: true,
   149  			},
   150  		},
   151  	})
   152  }
   153  
   154  func TestAccAWSDBClusterParameterGroupOnly(t *testing.T) {
   155  	var v rds.DBClusterParameterGroup
   156  
   157  	parameterGroupName := fmt.Sprintf("cluster-parameter-group-test-tf-%d", acctest.RandInt())
   158  
   159  	resource.Test(t, resource.TestCase{
   160  		PreCheck:     func() { testAccPreCheck(t) },
   161  		Providers:    testAccProviders,
   162  		CheckDestroy: testAccCheckAWSDBClusterParameterGroupDestroy,
   163  		Steps: []resource.TestStep{
   164  			{
   165  				Config: testAccAWSDBClusterParameterGroupOnlyConfig(parameterGroupName),
   166  				Check: resource.ComposeTestCheckFunc(
   167  					testAccCheckAWSDBClusterParameterGroupExists("aws_rds_cluster_parameter_group.bar", &v),
   168  					testAccCheckAWSDBClusterParameterGroupAttributes(&v, parameterGroupName),
   169  					resource.TestCheckResourceAttr(
   170  						"aws_rds_cluster_parameter_group.bar", "name", parameterGroupName),
   171  					resource.TestCheckResourceAttr(
   172  						"aws_rds_cluster_parameter_group.bar", "family", "aurora5.6"),
   173  					resource.TestCheckResourceAttr(
   174  						"aws_rds_cluster_parameter_group.bar", "description", "Managed by Terraform"),
   175  				),
   176  			},
   177  		},
   178  	})
   179  }
   180  
   181  func TestResourceAWSDBClusterParameterGroupName_validation(t *testing.T) {
   182  	cases := []struct {
   183  		Value    string
   184  		ErrCount int
   185  	}{
   186  		{
   187  			Value:    "tEsting123",
   188  			ErrCount: 1,
   189  		},
   190  		{
   191  			Value:    "testing123!",
   192  			ErrCount: 1,
   193  		},
   194  		{
   195  			Value:    "1testing123",
   196  			ErrCount: 1,
   197  		},
   198  		{
   199  			Value:    "testing--123",
   200  			ErrCount: 1,
   201  		},
   202  		{
   203  			Value:    "testing123-",
   204  			ErrCount: 1,
   205  		},
   206  		{
   207  			Value:    randomString(256),
   208  			ErrCount: 1,
   209  		},
   210  	}
   211  
   212  	for _, tc := range cases {
   213  		_, errors := validateDbParamGroupName(tc.Value, "aws_rds_cluster_parameter_group_name")
   214  
   215  		if len(errors) != tc.ErrCount {
   216  			t.Fatal("Expected the DB Cluster Parameter Group Name to trigger a validation error")
   217  		}
   218  	}
   219  }
   220  
   221  func testAccCheckAWSDBClusterParameterGroupDestroy(s *terraform.State) error {
   222  	conn := testAccProvider.Meta().(*AWSClient).rdsconn
   223  
   224  	for _, rs := range s.RootModule().Resources {
   225  		if rs.Type != "aws_rds_cluster_parameter_group" {
   226  			continue
   227  		}
   228  
   229  		// Try to find the Group
   230  		resp, err := conn.DescribeDBClusterParameterGroups(
   231  			&rds.DescribeDBClusterParameterGroupsInput{
   232  				DBClusterParameterGroupName: aws.String(rs.Primary.ID),
   233  			})
   234  
   235  		if err == nil {
   236  			if len(resp.DBClusterParameterGroups) != 0 &&
   237  				*resp.DBClusterParameterGroups[0].DBClusterParameterGroupName == rs.Primary.ID {
   238  				return errors.New("DB Cluster Parameter Group still exists")
   239  			}
   240  		}
   241  
   242  		// Verify the error
   243  		newerr, ok := err.(awserr.Error)
   244  		if !ok {
   245  			return err
   246  		}
   247  		if newerr.Code() != "DBParameterGroupNotFound" {
   248  			return err
   249  		}
   250  	}
   251  
   252  	return nil
   253  }
   254  
   255  func testAccCheckAWSDBClusterParameterGroupAttributes(v *rds.DBClusterParameterGroup, name string) resource.TestCheckFunc {
   256  	return func(s *terraform.State) error {
   257  
   258  		if *v.DBClusterParameterGroupName != name {
   259  			return fmt.Errorf("bad name: %#v expected: %v", *v.DBClusterParameterGroupName, name)
   260  		}
   261  
   262  		if *v.DBParameterGroupFamily != "aurora5.6" {
   263  			return fmt.Errorf("bad family: %#v", *v.DBParameterGroupFamily)
   264  		}
   265  
   266  		return nil
   267  	}
   268  }
   269  
   270  func testAccAWSDBClusterParameterGroupDisappears(v *rds.DBClusterParameterGroup) resource.TestCheckFunc {
   271  	return func(s *terraform.State) error {
   272  		conn := testAccProvider.Meta().(*AWSClient).rdsconn
   273  		opts := &rds.DeleteDBClusterParameterGroupInput{
   274  			DBClusterParameterGroupName: v.DBClusterParameterGroupName,
   275  		}
   276  		if _, err := conn.DeleteDBClusterParameterGroup(opts); err != nil {
   277  			return err
   278  		}
   279  		return resource.Retry(40*time.Minute, func() *resource.RetryError {
   280  			opts := &rds.DescribeDBClusterParameterGroupsInput{
   281  				DBClusterParameterGroupName: v.DBClusterParameterGroupName,
   282  			}
   283  			_, err := conn.DescribeDBClusterParameterGroups(opts)
   284  			if err != nil {
   285  				dbparamgrouperr, ok := err.(awserr.Error)
   286  				if ok && dbparamgrouperr.Code() == "DBParameterGroupNotFound" {
   287  					return nil
   288  				}
   289  				return resource.NonRetryableError(
   290  					fmt.Errorf("Error retrieving DB Cluster Parameter Groups: %s", err))
   291  			}
   292  			return resource.RetryableError(fmt.Errorf(
   293  				"Waiting for cluster parameter group to be deleted: %v", v.DBClusterParameterGroupName))
   294  		})
   295  	}
   296  }
   297  
   298  func testAccCheckAWSDBClusterParameterGroupExists(n string, v *rds.DBClusterParameterGroup) resource.TestCheckFunc {
   299  	return func(s *terraform.State) error {
   300  		rs, ok := s.RootModule().Resources[n]
   301  		if !ok {
   302  			return fmt.Errorf("Not found: %s", n)
   303  		}
   304  
   305  		if rs.Primary.ID == "" {
   306  			return errors.New("No DB Cluster Parameter Group ID is set")
   307  		}
   308  
   309  		conn := testAccProvider.Meta().(*AWSClient).rdsconn
   310  
   311  		opts := rds.DescribeDBClusterParameterGroupsInput{
   312  			DBClusterParameterGroupName: aws.String(rs.Primary.ID),
   313  		}
   314  
   315  		resp, err := conn.DescribeDBClusterParameterGroups(&opts)
   316  
   317  		if err != nil {
   318  			return err
   319  		}
   320  
   321  		if len(resp.DBClusterParameterGroups) != 1 ||
   322  			*resp.DBClusterParameterGroups[0].DBClusterParameterGroupName != rs.Primary.ID {
   323  			return errors.New("DB Cluster Parameter Group not found")
   324  		}
   325  
   326  		*v = *resp.DBClusterParameterGroups[0]
   327  
   328  		return nil
   329  	}
   330  }
   331  
   332  func testAccAWSDBClusterParameterGroupConfig(name string) string {
   333  	return fmt.Sprintf(`
   334  resource "aws_rds_cluster_parameter_group" "bar" {
   335    name        = "%s"
   336    family      = "aurora5.6"
   337    description = "Test cluster parameter group for terraform"
   338  
   339    parameter {
   340      name  = "character_set_server"
   341      value = "utf8"
   342    }
   343  
   344    parameter {
   345      name  = "character_set_client"
   346      value = "utf8"
   347    }
   348  
   349    parameter {
   350      name  = "character_set_results"
   351      value = "utf8"
   352    }
   353  
   354    tags {
   355      foo = "bar"
   356    }
   357  }
   358  `, name)
   359  }
   360  
   361  func testAccAWSDBClusterParameterGroupAddParametersConfig(name string) string {
   362  	return fmt.Sprintf(`
   363  resource "aws_rds_cluster_parameter_group" "bar" {
   364    name        = "%s"
   365    family      = "aurora5.6"
   366    description = "Test cluster parameter group for terraform"
   367  
   368    parameter {
   369      name  = "character_set_server"
   370      value = "utf8"
   371    }
   372  
   373    parameter {
   374      name  = "character_set_client"
   375      value = "utf8"
   376    }
   377  
   378    parameter {
   379      name  = "character_set_results"
   380      value = "utf8"
   381    }
   382  
   383    parameter {
   384      name  = "collation_server"
   385      value = "utf8_unicode_ci"
   386    }
   387  
   388    parameter {
   389      name  = "collation_connection"
   390      value = "utf8_unicode_ci"
   391    }
   392  
   393    tags {
   394      foo = "bar"
   395      baz = "foo"
   396    }
   397  }
   398  `, name)
   399  }
   400  
   401  func testAccAWSDBClusterParameterGroupOnlyConfig(name string) string {
   402  	return fmt.Sprintf(`resource "aws_rds_cluster_parameter_group" "bar" {
   403    name        = "%s"
   404    family      = "aurora5.6"
   405  }`, name)
   406  }
   407  
   408  const testAccAWSDBClusterParameterGroupConfig_namePrefix = `
   409  resource "aws_rds_cluster_parameter_group" "test" {
   410    name_prefix = "tf-test-"
   411    family = "aurora5.6"
   412  }
   413  `
   414  const testAccAWSDBClusterParameterGroupConfig_generatedName = `
   415  resource "aws_rds_cluster_parameter_group" "test" {
   416    family = "aurora5.6"
   417  }
   418  `