github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/resource_aws_rds_cluster_parameter_group_test.go (about)

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