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