github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/aws/resource_aws_db_parameter_group_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  	"github.com/mitchellh/goamz/rds"
    10  )
    11  
    12  func TestAccAWSDBParameterGroup(t *testing.T) {
    13  	var v rds.DBParameterGroup
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckAWSDBParameterGroupDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccAWSDBParameterGroupConfig,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckAWSDBParameterGroupExists("aws_db_parameter_group.bar", &v),
    24  					testAccCheckAWSDBParameterGroupAttributes(&v),
    25  					resource.TestCheckResourceAttr(
    26  						"aws_db_parameter_group.bar", "name", "parameter-group-test-terraform"),
    27  					resource.TestCheckResourceAttr(
    28  						"aws_db_parameter_group.bar", "family", "mysql5.6"),
    29  					resource.TestCheckResourceAttr(
    30  						"aws_db_parameter_group.bar", "description", "Test parameter group for terraform"),
    31  					resource.TestCheckResourceAttr(
    32  						"aws_db_parameter_group.bar", "parameter.1708034931.name", "character_set_results"),
    33  					resource.TestCheckResourceAttr(
    34  						"aws_db_parameter_group.bar", "parameter.1708034931.value", "utf8"),
    35  					resource.TestCheckResourceAttr(
    36  						"aws_db_parameter_group.bar", "parameter.2421266705.name", "character_set_server"),
    37  					resource.TestCheckResourceAttr(
    38  						"aws_db_parameter_group.bar", "parameter.2421266705.value", "utf8"),
    39  					resource.TestCheckResourceAttr(
    40  						"aws_db_parameter_group.bar", "parameter.2478663599.name", "character_set_client"),
    41  					resource.TestCheckResourceAttr(
    42  						"aws_db_parameter_group.bar", "parameter.2478663599.value", "utf8"),
    43  				),
    44  			},
    45  			resource.TestStep{
    46  				Config: testAccAWSDBParameterGroupAddParametersConfig,
    47  				Check: resource.ComposeTestCheckFunc(
    48  					testAccCheckAWSDBParameterGroupExists("aws_db_parameter_group.bar", &v),
    49  					testAccCheckAWSDBParameterGroupAttributes(&v),
    50  					resource.TestCheckResourceAttr(
    51  						"aws_db_parameter_group.bar", "name", "parameter-group-test-terraform"),
    52  					resource.TestCheckResourceAttr(
    53  						"aws_db_parameter_group.bar", "family", "mysql5.6"),
    54  					resource.TestCheckResourceAttr(
    55  						"aws_db_parameter_group.bar", "description", "Test parameter group for terraform"),
    56  					resource.TestCheckResourceAttr(
    57  						"aws_db_parameter_group.bar", "parameter.1706463059.name", "collation_connection"),
    58  					resource.TestCheckResourceAttr(
    59  						"aws_db_parameter_group.bar", "parameter.1706463059.value", "utf8_unicode_ci"),
    60  					resource.TestCheckResourceAttr(
    61  						"aws_db_parameter_group.bar", "parameter.1708034931.name", "character_set_results"),
    62  					resource.TestCheckResourceAttr(
    63  						"aws_db_parameter_group.bar", "parameter.1708034931.value", "utf8"),
    64  					resource.TestCheckResourceAttr(
    65  						"aws_db_parameter_group.bar", "parameter.2421266705.name", "character_set_server"),
    66  					resource.TestCheckResourceAttr(
    67  						"aws_db_parameter_group.bar", "parameter.2421266705.value", "utf8"),
    68  					resource.TestCheckResourceAttr(
    69  						"aws_db_parameter_group.bar", "parameter.2475805061.name", "collation_server"),
    70  					resource.TestCheckResourceAttr(
    71  						"aws_db_parameter_group.bar", "parameter.2475805061.value", "utf8_unicode_ci"),
    72  					resource.TestCheckResourceAttr(
    73  						"aws_db_parameter_group.bar", "parameter.2478663599.name", "character_set_client"),
    74  					resource.TestCheckResourceAttr(
    75  						"aws_db_parameter_group.bar", "parameter.2478663599.value", "utf8"),
    76  				),
    77  			},
    78  		},
    79  	})
    80  }
    81  
    82  func TestAccAWSDBParameterGroupOnly(t *testing.T) {
    83  	var v rds.DBParameterGroup
    84  
    85  	resource.Test(t, resource.TestCase{
    86  		PreCheck:     func() { testAccPreCheck(t) },
    87  		Providers:    testAccProviders,
    88  		CheckDestroy: testAccCheckAWSDBParameterGroupDestroy,
    89  		Steps: []resource.TestStep{
    90  			resource.TestStep{
    91  				Config: testAccAWSDBParameterGroupOnlyConfig,
    92  				Check: resource.ComposeTestCheckFunc(
    93  					testAccCheckAWSDBParameterGroupExists("aws_db_parameter_group.bar", &v),
    94  					testAccCheckAWSDBParameterGroupAttributes(&v),
    95  					resource.TestCheckResourceAttr(
    96  						"aws_db_parameter_group.bar", "name", "parameter-group-test-terraform"),
    97  					resource.TestCheckResourceAttr(
    98  						"aws_db_parameter_group.bar", "family", "mysql5.6"),
    99  					resource.TestCheckResourceAttr(
   100  						"aws_db_parameter_group.bar", "description", "Test parameter group for terraform"),
   101  				),
   102  			},
   103  		},
   104  	})
   105  }
   106  
   107  func testAccCheckAWSDBParameterGroupDestroy(s *terraform.State) error {
   108  	conn := testAccProvider.Meta().(*AWSClient).rdsconn
   109  
   110  	for _, rs := range s.RootModule().Resources {
   111  		if rs.Type != "aws_db_parameter_group" {
   112  			continue
   113  		}
   114  
   115  		// Try to find the Group
   116  		resp, err := conn.DescribeDBParameterGroups(
   117  			&rds.DescribeDBParameterGroups{
   118  				DBParameterGroupName: rs.Primary.ID,
   119  			})
   120  
   121  		if err == nil {
   122  			if len(resp.DBParameterGroups) != 0 &&
   123  				resp.DBParameterGroups[0].DBParameterGroupName == rs.Primary.ID {
   124  				return fmt.Errorf("DB Parameter Group still exists")
   125  			}
   126  		}
   127  
   128  		// Verify the error
   129  		newerr, ok := err.(*rds.Error)
   130  		if !ok {
   131  			return err
   132  		}
   133  		if newerr.Code != "InvalidDBParameterGroup.NotFound" {
   134  			return err
   135  		}
   136  	}
   137  
   138  	return nil
   139  }
   140  
   141  func testAccCheckAWSDBParameterGroupAttributes(v *rds.DBParameterGroup) resource.TestCheckFunc {
   142  	return func(s *terraform.State) error {
   143  
   144  		if v.DBParameterGroupName != "parameter-group-test-terraform" {
   145  			return fmt.Errorf("bad name: %#v", v.DBParameterGroupName)
   146  		}
   147  
   148  		if v.DBParameterGroupFamily != "mysql5.6" {
   149  			return fmt.Errorf("bad family: %#v", v.DBParameterGroupFamily)
   150  		}
   151  
   152  		if v.Description != "Test parameter group for terraform" {
   153  			return fmt.Errorf("bad description: %#v", v.Description)
   154  		}
   155  
   156  		return nil
   157  	}
   158  }
   159  
   160  func testAccCheckAWSDBParameterGroupExists(n string, v *rds.DBParameterGroup) resource.TestCheckFunc {
   161  	return func(s *terraform.State) error {
   162  		rs, ok := s.RootModule().Resources[n]
   163  		if !ok {
   164  			return fmt.Errorf("Not found: %s", n)
   165  		}
   166  
   167  		if rs.Primary.ID == "" {
   168  			return fmt.Errorf("No DB Parameter Group ID is set")
   169  		}
   170  
   171  		conn := testAccProvider.Meta().(*AWSClient).rdsconn
   172  
   173  		opts := rds.DescribeDBParameterGroups{
   174  			DBParameterGroupName: rs.Primary.ID,
   175  		}
   176  
   177  		resp, err := conn.DescribeDBParameterGroups(&opts)
   178  
   179  		if err != nil {
   180  			return err
   181  		}
   182  
   183  		if len(resp.DBParameterGroups) != 1 ||
   184  			resp.DBParameterGroups[0].DBParameterGroupName != rs.Primary.ID {
   185  			return fmt.Errorf("DB Parameter Group not found")
   186  		}
   187  
   188  		*v = resp.DBParameterGroups[0]
   189  
   190  		return nil
   191  	}
   192  }
   193  
   194  const testAccAWSDBParameterGroupConfig = `
   195  resource "aws_db_parameter_group" "bar" {
   196  	name = "parameter-group-test-terraform"
   197  	family = "mysql5.6"
   198  	description = "Test parameter group for terraform"
   199  	parameter {
   200  	  name = "character_set_server"
   201  	  value = "utf8"
   202  	}
   203  	parameter {
   204  	  name = "character_set_client"
   205  	  value = "utf8"
   206  	}
   207  	parameter{
   208  	  name = "character_set_results"
   209  	  value = "utf8"
   210  	}
   211  }
   212  `
   213  
   214  const testAccAWSDBParameterGroupAddParametersConfig = `
   215  resource "aws_db_parameter_group" "bar" {
   216  	name = "parameter-group-test-terraform"
   217  	family = "mysql5.6"
   218  	description = "Test parameter group for terraform"
   219  	parameter {
   220  	  name = "character_set_server"
   221  	  value = "utf8"
   222  	}
   223  	parameter {
   224  	  name = "character_set_client"
   225  	  value = "utf8"
   226  	}
   227  	parameter{
   228  	  name = "character_set_results"
   229  	  value = "utf8"
   230  	}
   231  	parameter {
   232  	  name = "collation_server"
   233  	  value = "utf8_unicode_ci"
   234  	}
   235  	parameter {
   236  	  name = "collation_connection"
   237  	  value = "utf8_unicode_ci"
   238  	}
   239  }
   240  `
   241  
   242  const testAccAWSDBParameterGroupOnlyConfig = `
   243  resource "aws_db_parameter_group" "bar" {
   244  	name = "parameter-group-test-terraform"
   245  	family = "mysql5.6"
   246  	description = "Test parameter group for terraform"
   247  }
   248  `