github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_redshift_parameter_group_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/redshift"
    10  	"github.com/hashicorp/terraform/helper/acctest"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAWSRedshiftParameterGroup_withParameters(t *testing.T) {
    16  	var v redshift.ClusterParameterGroup
    17  	rInt := acctest.RandInt()
    18  
    19  	resource.Test(t, resource.TestCase{
    20  		PreCheck:     func() { testAccPreCheck(t) },
    21  		Providers:    testAccProviders,
    22  		CheckDestroy: testAccCheckAWSRedshiftParameterGroupDestroy,
    23  		Steps: []resource.TestStep{
    24  			{
    25  				Config: testAccAWSRedshiftParameterGroupConfig(rInt),
    26  				Check: resource.ComposeTestCheckFunc(
    27  					testAccCheckAWSRedshiftParameterGroupExists("aws_redshift_parameter_group.bar", &v),
    28  					resource.TestCheckResourceAttr(
    29  						"aws_redshift_parameter_group.bar", "name", fmt.Sprintf("test-terraform-%d", rInt)),
    30  					resource.TestCheckResourceAttr(
    31  						"aws_redshift_parameter_group.bar", "family", "redshift-1.0"),
    32  					resource.TestCheckResourceAttr(
    33  						"aws_redshift_parameter_group.bar", "description", "Managed by Terraform"),
    34  					resource.TestCheckResourceAttr(
    35  						"aws_redshift_parameter_group.bar", "parameter.490804664.name", "require_ssl"),
    36  					resource.TestCheckResourceAttr(
    37  						"aws_redshift_parameter_group.bar", "parameter.490804664.value", "true"),
    38  					resource.TestCheckResourceAttr(
    39  						"aws_redshift_parameter_group.bar", "parameter.2036118857.name", "query_group"),
    40  					resource.TestCheckResourceAttr(
    41  						"aws_redshift_parameter_group.bar", "parameter.2036118857.value", "example"),
    42  					resource.TestCheckResourceAttr(
    43  						"aws_redshift_parameter_group.bar", "parameter.484080973.name", "enable_user_activity_logging"),
    44  					resource.TestCheckResourceAttr(
    45  						"aws_redshift_parameter_group.bar", "parameter.484080973.value", "true"),
    46  				),
    47  			},
    48  		},
    49  	})
    50  }
    51  
    52  func TestAccAWSRedshiftParameterGroup_withoutParameters(t *testing.T) {
    53  	var v redshift.ClusterParameterGroup
    54  	rInt := acctest.RandInt()
    55  
    56  	resource.Test(t, resource.TestCase{
    57  		PreCheck:     func() { testAccPreCheck(t) },
    58  		Providers:    testAccProviders,
    59  		CheckDestroy: testAccCheckAWSRedshiftParameterGroupDestroy,
    60  		Steps: []resource.TestStep{
    61  			{
    62  				Config: testAccAWSRedshiftParameterGroupOnlyConfig(rInt),
    63  				Check: resource.ComposeTestCheckFunc(
    64  					testAccCheckAWSRedshiftParameterGroupExists("aws_redshift_parameter_group.bar", &v),
    65  					resource.TestCheckResourceAttr(
    66  						"aws_redshift_parameter_group.bar", "name", fmt.Sprintf("test-terraform-%d", rInt)),
    67  					resource.TestCheckResourceAttr(
    68  						"aws_redshift_parameter_group.bar", "family", "redshift-1.0"),
    69  					resource.TestCheckResourceAttr(
    70  						"aws_redshift_parameter_group.bar", "description", "Test parameter group for terraform"),
    71  				),
    72  			},
    73  		},
    74  	})
    75  }
    76  
    77  func TestResourceAWSRedshiftParameterGroupNameValidation(t *testing.T) {
    78  	cases := []struct {
    79  		Value    string
    80  		ErrCount int
    81  	}{
    82  		{
    83  			Value:    "tEsting123",
    84  			ErrCount: 1,
    85  		},
    86  		{
    87  			Value:    "testing123!",
    88  			ErrCount: 1,
    89  		},
    90  		{
    91  			Value:    "1testing123",
    92  			ErrCount: 1,
    93  		},
    94  		{
    95  			Value:    "testing--123",
    96  			ErrCount: 1,
    97  		},
    98  		{
    99  			Value:    "testing123-",
   100  			ErrCount: 1,
   101  		},
   102  		{
   103  			Value:    randomString(256),
   104  			ErrCount: 1,
   105  		},
   106  	}
   107  
   108  	for _, tc := range cases {
   109  		_, errors := validateRedshiftParamGroupName(tc.Value, "aws_redshift_parameter_group_name")
   110  
   111  		if len(errors) != tc.ErrCount {
   112  			t.Fatalf("Expected the Redshift Parameter Group Name to trigger a validation error")
   113  		}
   114  	}
   115  }
   116  
   117  func testAccCheckAWSRedshiftParameterGroupDestroy(s *terraform.State) error {
   118  	conn := testAccProvider.Meta().(*AWSClient).redshiftconn
   119  
   120  	for _, rs := range s.RootModule().Resources {
   121  		if rs.Type != "aws_redshift_parameter_group" {
   122  			continue
   123  		}
   124  
   125  		// Try to find the Group
   126  		resp, err := conn.DescribeClusterParameterGroups(
   127  			&redshift.DescribeClusterParameterGroupsInput{
   128  				ParameterGroupName: aws.String(rs.Primary.ID),
   129  			})
   130  
   131  		if err == nil {
   132  			if len(resp.ParameterGroups) != 0 &&
   133  				*resp.ParameterGroups[0].ParameterGroupName == rs.Primary.ID {
   134  				return fmt.Errorf("Redshift Parameter Group still exists")
   135  			}
   136  		}
   137  
   138  		// Verify the error
   139  		newerr, ok := err.(awserr.Error)
   140  		if !ok {
   141  			return err
   142  		}
   143  		if newerr.Code() != "ClusterParameterGroupNotFound" {
   144  			return err
   145  		}
   146  	}
   147  
   148  	return nil
   149  }
   150  
   151  func testAccCheckAWSRedshiftParameterGroupExists(n string, v *redshift.ClusterParameterGroup) resource.TestCheckFunc {
   152  	return func(s *terraform.State) error {
   153  		rs, ok := s.RootModule().Resources[n]
   154  		if !ok {
   155  			return fmt.Errorf("Not found: %s", n)
   156  		}
   157  
   158  		if rs.Primary.ID == "" {
   159  			return fmt.Errorf("No Redshift Parameter Group ID is set")
   160  		}
   161  
   162  		conn := testAccProvider.Meta().(*AWSClient).redshiftconn
   163  
   164  		opts := redshift.DescribeClusterParameterGroupsInput{
   165  			ParameterGroupName: aws.String(rs.Primary.ID),
   166  		}
   167  
   168  		resp, err := conn.DescribeClusterParameterGroups(&opts)
   169  
   170  		if err != nil {
   171  			return err
   172  		}
   173  
   174  		if len(resp.ParameterGroups) != 1 ||
   175  			*resp.ParameterGroups[0].ParameterGroupName != rs.Primary.ID {
   176  			return fmt.Errorf("Redshift Parameter Group not found")
   177  		}
   178  
   179  		*v = *resp.ParameterGroups[0]
   180  
   181  		return nil
   182  	}
   183  }
   184  
   185  func testAccAWSRedshiftParameterGroupOnlyConfig(rInt int) string {
   186  	return fmt.Sprintf(`
   187  	resource "aws_redshift_parameter_group" "bar" {
   188  		name = "test-terraform-%d"
   189  		family = "redshift-1.0"
   190  		description = "Test parameter group for terraform"
   191  	}`, rInt)
   192  }
   193  
   194  func testAccAWSRedshiftParameterGroupConfig(rInt int) string {
   195  	return fmt.Sprintf(`
   196  	resource "aws_redshift_parameter_group" "bar" {
   197  		name = "test-terraform-%d"
   198  		family = "redshift-1.0"
   199  		parameter {
   200  			name = "require_ssl"
   201  			value = "true"
   202  		}
   203  		parameter {
   204  			name = "query_group"
   205  			value = "example"
   206  		}
   207  		parameter{
   208  			name = "enable_user_activity_logging"
   209  			value = "true"
   210  		}
   211  	}`, rInt)
   212  }