github.com/profects/terraform@v0.9.0-beta1.0.20170227135739-92d4809db30d/builtin/providers/aws/resource_aws_redshift_cluster_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"math/rand"
     7  	"regexp"
     8  	"strings"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/aws/aws-sdk-go/aws"
    13  	"github.com/aws/aws-sdk-go/aws/awserr"
    14  	"github.com/aws/aws-sdk-go/service/redshift"
    15  	"github.com/hashicorp/terraform/helper/acctest"
    16  	"github.com/hashicorp/terraform/helper/resource"
    17  	"github.com/hashicorp/terraform/terraform"
    18  )
    19  
    20  func TestValidateRedshiftClusterDbName(t *testing.T) {
    21  	validNames := []string{
    22  		"testdbname",
    23  		"test_dbname",
    24  		"testdbname123",
    25  		"TestDBname",
    26  		"testdbname$hashicorp",
    27  		"_dbname",
    28  	}
    29  	for _, v := range validNames {
    30  		_, errors := validateRedshiftClusterDbName(v, "name")
    31  		if len(errors) != 0 {
    32  			t.Fatalf("%q should be a valid Redshift DBName: %q", v, errors)
    33  		}
    34  	}
    35  
    36  	invalidNames := []string{
    37  		"!",
    38  		"/",
    39  		" ",
    40  		":",
    41  		";",
    42  		"test name",
    43  		"/slash-at-the-beginning",
    44  		"slash-at-the-end/",
    45  		"",
    46  		randomString(100),
    47  	}
    48  	for _, v := range invalidNames {
    49  		_, errors := validateRedshiftClusterDbName(v, "name")
    50  		if len(errors) == 0 {
    51  			t.Fatalf("%q should be an invalid Redshift DBName", v)
    52  		}
    53  	}
    54  }
    55  
    56  func TestAccAWSRedshiftCluster_basic(t *testing.T) {
    57  	var v redshift.Cluster
    58  
    59  	ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
    60  	config := fmt.Sprintf(testAccAWSRedshiftClusterConfig_basic, ri)
    61  
    62  	resource.Test(t, resource.TestCase{
    63  		PreCheck:     func() { testAccPreCheck(t) },
    64  		Providers:    testAccProviders,
    65  		CheckDestroy: testAccCheckAWSRedshiftClusterDestroy,
    66  		Steps: []resource.TestStep{
    67  			{
    68  				Config: config,
    69  				Check: resource.ComposeTestCheckFunc(
    70  					testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v),
    71  					resource.TestCheckResourceAttr(
    72  						"aws_redshift_cluster.default", "cluster_type", "single-node"),
    73  					resource.TestCheckResourceAttr(
    74  						"aws_redshift_cluster.default", "publicly_accessible", "true"),
    75  				),
    76  			},
    77  		},
    78  	})
    79  }
    80  
    81  func TestAccAWSRedshiftCluster_withFinalSnapshot(t *testing.T) {
    82  	var v redshift.Cluster
    83  
    84  	rInt := acctest.RandInt()
    85  
    86  	resource.Test(t, resource.TestCase{
    87  		PreCheck:     func() { testAccPreCheck(t) },
    88  		Providers:    testAccProviders,
    89  		CheckDestroy: testAccCheckAWSRedshiftClusterSnapshot(rInt),
    90  		Steps: []resource.TestStep{
    91  			{
    92  				Config: testAccAWSRedshiftClusterConfigWithFinalSnapshot(rInt),
    93  				Check: resource.ComposeTestCheckFunc(
    94  					testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v),
    95  				),
    96  			},
    97  		},
    98  	})
    99  }
   100  
   101  func TestAccAWSRedshiftCluster_kmsKey(t *testing.T) {
   102  	var v redshift.Cluster
   103  
   104  	ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
   105  	config := fmt.Sprintf(testAccAWSRedshiftClusterConfig_kmsKey, ri, ri)
   106  	keyRegex := regexp.MustCompile("^arn:aws:([a-zA-Z0-9\\-])+:([a-z]{2}-[a-z]+-\\d{1})?:(\\d{12})?:(.*)$")
   107  
   108  	resource.Test(t, resource.TestCase{
   109  		PreCheck:     func() { testAccPreCheck(t) },
   110  		Providers:    testAccProviders,
   111  		CheckDestroy: testAccCheckAWSRedshiftClusterDestroy,
   112  		Steps: []resource.TestStep{
   113  			{
   114  				Config: config,
   115  				Check: resource.ComposeTestCheckFunc(
   116  					testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v),
   117  					resource.TestCheckResourceAttr(
   118  						"aws_redshift_cluster.default", "cluster_type", "single-node"),
   119  					resource.TestCheckResourceAttr(
   120  						"aws_redshift_cluster.default", "publicly_accessible", "true"),
   121  					resource.TestMatchResourceAttr("aws_redshift_cluster.default", "kms_key_id", keyRegex),
   122  				),
   123  			},
   124  		},
   125  	})
   126  }
   127  
   128  func TestAccAWSRedshiftCluster_enhancedVpcRoutingEnabled(t *testing.T) {
   129  	var v redshift.Cluster
   130  
   131  	ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
   132  	preConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_enhancedVpcRoutingEnabled, ri)
   133  	postConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_enhancedVpcRoutingDisabled, ri)
   134  
   135  	resource.Test(t, resource.TestCase{
   136  		PreCheck:     func() { testAccPreCheck(t) },
   137  		Providers:    testAccProviders,
   138  		CheckDestroy: testAccCheckAWSRedshiftClusterDestroy,
   139  		Steps: []resource.TestStep{
   140  			{
   141  				Config: preConfig,
   142  				Check: resource.ComposeTestCheckFunc(
   143  					testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v),
   144  					resource.TestCheckResourceAttr(
   145  						"aws_redshift_cluster.default", "enhanced_vpc_routing", "true"),
   146  				),
   147  			},
   148  			{
   149  				Config: postConfig,
   150  				Check: resource.ComposeTestCheckFunc(
   151  					testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v),
   152  					resource.TestCheckResourceAttr(
   153  						"aws_redshift_cluster.default", "enhanced_vpc_routing", "false"),
   154  				),
   155  			},
   156  		},
   157  	})
   158  }
   159  
   160  func TestAccAWSRedshiftCluster_loggingEnabled(t *testing.T) {
   161  	var v redshift.Cluster
   162  	rInt := acctest.RandInt()
   163  
   164  	resource.Test(t, resource.TestCase{
   165  		PreCheck:     func() { testAccPreCheck(t) },
   166  		Providers:    testAccProviders,
   167  		CheckDestroy: testAccCheckAWSRedshiftClusterDestroy,
   168  		Steps: []resource.TestStep{
   169  			{
   170  				Config: testAccAWSRedshiftClusterConfig_loggingEnabled(rInt),
   171  				Check: resource.ComposeTestCheckFunc(
   172  					testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v),
   173  					resource.TestCheckResourceAttr(
   174  						"aws_redshift_cluster.default", "enable_logging", "true"),
   175  					resource.TestCheckResourceAttr(
   176  						"aws_redshift_cluster.default", "bucket_name", fmt.Sprintf("tf-redshift-logging-%d", rInt)),
   177  				),
   178  			},
   179  
   180  			{
   181  				Config: testAccAWSRedshiftClusterConfig_loggingDisabled(rInt),
   182  				Check: resource.ComposeTestCheckFunc(
   183  					testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v),
   184  					resource.TestCheckResourceAttr(
   185  						"aws_redshift_cluster.default", "enable_logging", "false"),
   186  				),
   187  			},
   188  		},
   189  	})
   190  }
   191  
   192  func TestAccAWSRedshiftCluster_iamRoles(t *testing.T) {
   193  	var v redshift.Cluster
   194  
   195  	ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
   196  	preConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_iamRoles, ri, ri, ri)
   197  	postConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_updateIamRoles, ri, ri, ri)
   198  
   199  	resource.Test(t, resource.TestCase{
   200  		PreCheck:     func() { testAccPreCheck(t) },
   201  		Providers:    testAccProviders,
   202  		CheckDestroy: testAccCheckAWSRedshiftClusterDestroy,
   203  		Steps: []resource.TestStep{
   204  			{
   205  				Config: preConfig,
   206  				Check: resource.ComposeTestCheckFunc(
   207  					testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v),
   208  					resource.TestCheckResourceAttr(
   209  						"aws_redshift_cluster.default", "iam_roles.#", "2"),
   210  				),
   211  			},
   212  
   213  			{
   214  				Config: postConfig,
   215  				Check: resource.ComposeTestCheckFunc(
   216  					testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v),
   217  					resource.TestCheckResourceAttr(
   218  						"aws_redshift_cluster.default", "iam_roles.#", "1"),
   219  				),
   220  			},
   221  		},
   222  	})
   223  }
   224  
   225  func TestAccAWSRedshiftCluster_publiclyAccessible(t *testing.T) {
   226  	var v redshift.Cluster
   227  	rInt := acctest.RandInt()
   228  
   229  	resource.Test(t, resource.TestCase{
   230  		PreCheck:     func() { testAccPreCheck(t) },
   231  		Providers:    testAccProviders,
   232  		CheckDestroy: testAccCheckAWSRedshiftClusterDestroy,
   233  		Steps: []resource.TestStep{
   234  			{
   235  				Config: testAccAWSRedshiftClusterConfig_notPubliclyAccessible(rInt),
   236  				Check: resource.ComposeTestCheckFunc(
   237  					testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v),
   238  					resource.TestCheckResourceAttr(
   239  						"aws_redshift_cluster.default", "publicly_accessible", "false"),
   240  				),
   241  			},
   242  
   243  			{
   244  				Config: testAccAWSRedshiftClusterConfig_updatePubliclyAccessible(rInt),
   245  				Check: resource.ComposeTestCheckFunc(
   246  					testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v),
   247  					resource.TestCheckResourceAttr(
   248  						"aws_redshift_cluster.default", "publicly_accessible", "true"),
   249  				),
   250  			},
   251  		},
   252  	})
   253  }
   254  
   255  func TestAccAWSRedshiftCluster_updateNodeCount(t *testing.T) {
   256  	var v redshift.Cluster
   257  
   258  	ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
   259  	preConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_basic, ri)
   260  	postConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_updateNodeCount, ri)
   261  
   262  	resource.Test(t, resource.TestCase{
   263  		PreCheck:     func() { testAccPreCheck(t) },
   264  		Providers:    testAccProviders,
   265  		CheckDestroy: testAccCheckAWSRedshiftClusterDestroy,
   266  		Steps: []resource.TestStep{
   267  			{
   268  				Config: preConfig,
   269  				Check: resource.ComposeTestCheckFunc(
   270  					testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v),
   271  					resource.TestCheckResourceAttr(
   272  						"aws_redshift_cluster.default", "number_of_nodes", "1"),
   273  				),
   274  			},
   275  
   276  			{
   277  				Config: postConfig,
   278  				Check: resource.ComposeTestCheckFunc(
   279  					testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v),
   280  					resource.TestCheckResourceAttr(
   281  						"aws_redshift_cluster.default", "number_of_nodes", "2"),
   282  				),
   283  			},
   284  		},
   285  	})
   286  }
   287  
   288  func TestAccAWSRedshiftCluster_tags(t *testing.T) {
   289  	var v redshift.Cluster
   290  
   291  	ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
   292  	preConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_tags, ri)
   293  	postConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_updatedTags, ri)
   294  
   295  	resource.Test(t, resource.TestCase{
   296  		PreCheck:     func() { testAccPreCheck(t) },
   297  		Providers:    testAccProviders,
   298  		CheckDestroy: testAccCheckAWSRedshiftClusterDestroy,
   299  		Steps: []resource.TestStep{
   300  			{
   301  				Config: preConfig,
   302  				Check: resource.ComposeTestCheckFunc(
   303  					testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v),
   304  					resource.TestCheckResourceAttr(
   305  						"aws_redshift_cluster.default", "tags.%", "3"),
   306  					resource.TestCheckResourceAttr("aws_redshift_cluster.default", "tags.environment", "Production"),
   307  				),
   308  			},
   309  
   310  			{
   311  				Config: postConfig,
   312  				Check: resource.ComposeTestCheckFunc(
   313  					testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v),
   314  					resource.TestCheckResourceAttr(
   315  						"aws_redshift_cluster.default", "tags.%", "1"),
   316  					resource.TestCheckResourceAttr("aws_redshift_cluster.default", "tags.environment", "Production"),
   317  				),
   318  			},
   319  		},
   320  	})
   321  }
   322  
   323  func testAccCheckAWSRedshiftClusterDestroy(s *terraform.State) error {
   324  	for _, rs := range s.RootModule().Resources {
   325  		if rs.Type != "aws_redshift_cluster" {
   326  			continue
   327  		}
   328  
   329  		// Try to find the Group
   330  		conn := testAccProvider.Meta().(*AWSClient).redshiftconn
   331  		var err error
   332  		resp, err := conn.DescribeClusters(
   333  			&redshift.DescribeClustersInput{
   334  				ClusterIdentifier: aws.String(rs.Primary.ID),
   335  			})
   336  
   337  		if err == nil {
   338  			if len(resp.Clusters) != 0 &&
   339  				*resp.Clusters[0].ClusterIdentifier == rs.Primary.ID {
   340  				return fmt.Errorf("Redshift Cluster %s still exists", rs.Primary.ID)
   341  			}
   342  		}
   343  
   344  		// Return nil if the cluster is already destroyed
   345  		if awsErr, ok := err.(awserr.Error); ok {
   346  			if awsErr.Code() == "ClusterNotFound" {
   347  				return nil
   348  			}
   349  		}
   350  
   351  		return err
   352  	}
   353  
   354  	return nil
   355  }
   356  
   357  func testAccCheckAWSRedshiftClusterSnapshot(rInt int) resource.TestCheckFunc {
   358  	return func(s *terraform.State) error {
   359  		for _, rs := range s.RootModule().Resources {
   360  			if rs.Type != "aws_redshift_cluster" {
   361  				continue
   362  			}
   363  
   364  			var err error
   365  
   366  			// Try and delete the snapshot before we check for the cluster not found
   367  			conn := testAccProvider.Meta().(*AWSClient).redshiftconn
   368  
   369  			snapshot_identifier := fmt.Sprintf("tf-acctest-snapshot-%d", rInt)
   370  			arn, err := buildRedshiftARN(snapshot_identifier, testAccProvider.Meta().(*AWSClient).partition, testAccProvider.Meta().(*AWSClient).accountid, testAccProvider.Meta().(*AWSClient).region)
   371  			tagsARN := strings.Replace(arn, ":cluster:", ":snapshot:", 1)
   372  			if err != nil {
   373  				return fmt.Errorf("Error building ARN for tags check with ARN (%s): %s", tagsARN, err)
   374  			}
   375  
   376  			log.Printf("[INFO] Deleting the Snapshot %s", snapshot_identifier)
   377  			_, snapDeleteErr := conn.DeleteClusterSnapshot(
   378  				&redshift.DeleteClusterSnapshotInput{
   379  					SnapshotIdentifier: aws.String(snapshot_identifier),
   380  				})
   381  			if snapDeleteErr != nil {
   382  				return err
   383  			}
   384  
   385  			//lastly check that the Cluster is missing
   386  			resp, err := conn.DescribeClusters(
   387  				&redshift.DescribeClustersInput{
   388  					ClusterIdentifier: aws.String(rs.Primary.ID),
   389  				})
   390  
   391  			if err == nil {
   392  				if len(resp.Clusters) != 0 &&
   393  					*resp.Clusters[0].ClusterIdentifier == rs.Primary.ID {
   394  					return fmt.Errorf("Redshift Cluster %s still exists", rs.Primary.ID)
   395  				}
   396  			}
   397  
   398  			// Return nil if the cluster is already destroyed
   399  			if awsErr, ok := err.(awserr.Error); ok {
   400  				if awsErr.Code() == "ClusterNotFound" {
   401  					return nil
   402  				}
   403  
   404  				return err
   405  			}
   406  
   407  		}
   408  
   409  		return nil
   410  	}
   411  }
   412  
   413  func testAccCheckAWSRedshiftClusterExists(n string, v *redshift.Cluster) resource.TestCheckFunc {
   414  	return func(s *terraform.State) error {
   415  		rs, ok := s.RootModule().Resources[n]
   416  		if !ok {
   417  			return fmt.Errorf("Not found: %s", n)
   418  		}
   419  
   420  		if rs.Primary.ID == "" {
   421  			return fmt.Errorf("No Redshift Cluster Instance ID is set")
   422  		}
   423  
   424  		conn := testAccProvider.Meta().(*AWSClient).redshiftconn
   425  		resp, err := conn.DescribeClusters(&redshift.DescribeClustersInput{
   426  			ClusterIdentifier: aws.String(rs.Primary.ID),
   427  		})
   428  
   429  		if err != nil {
   430  			return err
   431  		}
   432  
   433  		for _, c := range resp.Clusters {
   434  			if *c.ClusterIdentifier == rs.Primary.ID {
   435  				*v = *c
   436  				return nil
   437  			}
   438  		}
   439  
   440  		return fmt.Errorf("Redshift Cluster (%s) not found", rs.Primary.ID)
   441  	}
   442  }
   443  
   444  func TestResourceAWSRedshiftClusterIdentifierValidation(t *testing.T) {
   445  	cases := []struct {
   446  		Value    string
   447  		ErrCount int
   448  	}{
   449  		{
   450  			Value:    "tEsting",
   451  			ErrCount: 1,
   452  		},
   453  		{
   454  			Value:    "1testing",
   455  			ErrCount: 1,
   456  		},
   457  		{
   458  			Value:    "testing--123",
   459  			ErrCount: 1,
   460  		},
   461  		{
   462  			Value:    "testing!",
   463  			ErrCount: 1,
   464  		},
   465  		{
   466  			Value:    "testing-",
   467  			ErrCount: 1,
   468  		},
   469  	}
   470  
   471  	for _, tc := range cases {
   472  		_, errors := validateRedshiftClusterIdentifier(tc.Value, "aws_redshift_cluster_identifier")
   473  
   474  		if len(errors) != tc.ErrCount {
   475  			t.Fatalf("Expected the Redshift Cluster cluster_identifier to trigger a validation error")
   476  		}
   477  	}
   478  }
   479  
   480  func TestResourceAWSRedshiftClusterFinalSnapshotIdentifierValidation(t *testing.T) {
   481  	cases := []struct {
   482  		Value    string
   483  		ErrCount int
   484  	}{
   485  		{
   486  			Value:    "testing--123",
   487  			ErrCount: 1,
   488  		},
   489  		{
   490  			Value:    "testing-",
   491  			ErrCount: 1,
   492  		},
   493  		{
   494  			Value:    "Testingq123!",
   495  			ErrCount: 1,
   496  		},
   497  		{
   498  			Value:    randomString(256),
   499  			ErrCount: 1,
   500  		},
   501  	}
   502  
   503  	for _, tc := range cases {
   504  		_, errors := validateRedshiftClusterFinalSnapshotIdentifier(tc.Value, "aws_redshift_cluster_final_snapshot_identifier")
   505  
   506  		if len(errors) != tc.ErrCount {
   507  			t.Fatalf("Expected the Redshift Cluster final_snapshot_identifier to trigger a validation error")
   508  		}
   509  	}
   510  }
   511  
   512  func TestResourceAWSRedshiftClusterMasterUsernameValidation(t *testing.T) {
   513  	cases := []struct {
   514  		Value    string
   515  		ErrCount int
   516  	}{
   517  		{
   518  			Value:    "1Testing",
   519  			ErrCount: 1,
   520  		},
   521  		{
   522  			Value:    "Testing!!",
   523  			ErrCount: 1,
   524  		},
   525  		{
   526  			Value:    randomString(129),
   527  			ErrCount: 1,
   528  		},
   529  		{
   530  			Value:    "testing_testing123",
   531  			ErrCount: 0,
   532  		},
   533  	}
   534  
   535  	for _, tc := range cases {
   536  		_, errors := validateRedshiftClusterMasterUsername(tc.Value, "aws_redshift_cluster_master_username")
   537  
   538  		if len(errors) != tc.ErrCount {
   539  			t.Fatalf("Expected the Redshift Cluster master_username to trigger a validation error")
   540  		}
   541  	}
   542  }
   543  
   544  func TestResourceAWSRedshiftClusterMasterPasswordValidation(t *testing.T) {
   545  	cases := []struct {
   546  		Value    string
   547  		ErrCount int
   548  	}{
   549  		{
   550  			Value:    "1TESTING",
   551  			ErrCount: 1,
   552  		},
   553  		{
   554  			Value:    "1testing",
   555  			ErrCount: 1,
   556  		},
   557  		{
   558  			Value:    "TestTest",
   559  			ErrCount: 1,
   560  		},
   561  		{
   562  			Value:    "T3st",
   563  			ErrCount: 1,
   564  		},
   565  		{
   566  			Value:    "1Testing",
   567  			ErrCount: 0,
   568  		},
   569  	}
   570  
   571  	for _, tc := range cases {
   572  		_, errors := validateRedshiftClusterMasterPassword(tc.Value, "aws_redshift_cluster_master_password")
   573  
   574  		if len(errors) != tc.ErrCount {
   575  			t.Fatalf("Expected the Redshift Cluster master_password to trigger a validation error")
   576  		}
   577  	}
   578  }
   579  
   580  var testAccAWSRedshiftClusterConfig_updateNodeCount = `
   581  resource "aws_redshift_cluster" "default" {
   582    cluster_identifier = "tf-redshift-cluster-%d"
   583    availability_zone = "us-west-2a"
   584    database_name = "mydb"
   585    master_username = "foo_test"
   586    master_password = "Mustbe8characters"
   587    node_type = "dc1.large"
   588    automated_snapshot_retention_period = 0
   589    allow_version_upgrade = false
   590    number_of_nodes = 2
   591    skip_final_snapshot = true
   592  }
   593  `
   594  
   595  var testAccAWSRedshiftClusterConfig_basic = `
   596  resource "aws_redshift_cluster" "default" {
   597    cluster_identifier = "tf-redshift-cluster-%d"
   598    availability_zone = "us-west-2a"
   599    database_name = "mydb"
   600    master_username = "foo_test"
   601    master_password = "Mustbe8characters"
   602    node_type = "dc1.large"
   603    automated_snapshot_retention_period = 0
   604    allow_version_upgrade = false
   605    skip_final_snapshot = true
   606  }`
   607  
   608  func testAccAWSRedshiftClusterConfigWithFinalSnapshot(rInt int) string {
   609  	return fmt.Sprintf(`
   610  resource "aws_redshift_cluster" "default" {
   611    cluster_identifier = "tf-redshift-cluster-%d"
   612    availability_zone = "us-west-2a"
   613    database_name = "mydb"
   614    master_username = "foo_test"
   615    master_password = "Mustbe8characters"
   616    node_type = "dc1.large"
   617    automated_snapshot_retention_period = 0
   618    allow_version_upgrade = false
   619    skip_final_snapshot = false
   620    final_snapshot_identifier = "tf-acctest-snapshot-%d"
   621  }`, rInt, rInt)
   622  }
   623  
   624  var testAccAWSRedshiftClusterConfig_kmsKey = `
   625  resource "aws_kms_key" "foo" {
   626    description = "Terraform acc test %d"
   627    policy = <<POLICY
   628  {
   629    "Version": "2012-10-17",
   630    "Id": "kms-tf-1",
   631    "Statement": [
   632      {
   633        "Sid": "Enable IAM User Permissions",
   634        "Effect": "Allow",
   635        "Principal": {
   636          "AWS": "*"
   637        },
   638        "Action": "kms:*",
   639        "Resource": "*"
   640      }
   641    ]
   642  }
   643  POLICY
   644  }
   645  
   646  resource "aws_redshift_cluster" "default" {
   647    cluster_identifier = "tf-redshift-cluster-%d"
   648    availability_zone = "us-west-2a"
   649    database_name = "mydb"
   650    master_username = "foo_test"
   651    master_password = "Mustbe8characters"
   652    node_type = "dc1.large"
   653    automated_snapshot_retention_period = 0
   654    allow_version_upgrade = false
   655    kms_key_id = "${aws_kms_key.foo.arn}"
   656    encrypted = true
   657    skip_final_snapshot = true
   658  }`
   659  
   660  var testAccAWSRedshiftClusterConfig_enhancedVpcRoutingEnabled = `
   661  resource "aws_redshift_cluster" "default" {
   662    cluster_identifier = "tf-redshift-cluster-%d"
   663    availability_zone = "us-west-2a"
   664    database_name = "mydb"
   665    master_username = "foo_test"
   666    master_password = "Mustbe8characters"
   667    node_type = "dc1.large"
   668    automated_snapshot_retention_period = 0
   669    allow_version_upgrade = false
   670    enhanced_vpc_routing = true
   671    skip_final_snapshot = true
   672  }
   673  `
   674  
   675  var testAccAWSRedshiftClusterConfig_enhancedVpcRoutingDisabled = `
   676  resource "aws_redshift_cluster" "default" {
   677    cluster_identifier = "tf-redshift-cluster-%d"
   678    availability_zone = "us-west-2a"
   679    database_name = "mydb"
   680    master_username = "foo_test"
   681    master_password = "Mustbe8characters"
   682    node_type = "dc1.large"
   683    automated_snapshot_retention_period = 0
   684    allow_version_upgrade = false
   685    enhanced_vpc_routing = false
   686    skip_final_snapshot = true
   687  }
   688  `
   689  
   690  func testAccAWSRedshiftClusterConfig_loggingDisabled(rInt int) string {
   691  	return fmt.Sprintf(`
   692  	resource "aws_redshift_cluster" "default" {
   693  		cluster_identifier = "tf-redshift-cluster-%d"
   694  		availability_zone = "us-west-2a"
   695  		database_name = "mydb"
   696  		master_username = "foo_test"
   697  		master_password = "Mustbe8characters"
   698  		node_type = "dc1.large"
   699  		automated_snapshot_retention_period = 0
   700  		allow_version_upgrade = false
   701  		enable_logging = false
   702  	}`, rInt)
   703  }
   704  
   705  func testAccAWSRedshiftClusterConfig_loggingEnabled(rInt int) string {
   706  	return fmt.Sprintf(`
   707   resource "aws_s3_bucket" "bucket" {
   708  	 bucket = "tf-redshift-logging-%d"
   709  	 force_destroy = true
   710  	 policy = <<EOF
   711  {
   712   "Version": "2008-10-17",
   713   "Statement": [
   714  	 {
   715  		 "Sid": "Stmt1376526643067",
   716  		 "Effect": "Allow",
   717  		 "Principal": {
   718  			 "AWS": "arn:aws:iam::902366379725:user/logs"
   719  		 },
   720  		 "Action": "s3:PutObject",
   721  		 "Resource": "arn:aws:s3:::tf-redshift-logging-%d/*"
   722  	 },
   723  	 {
   724  		 "Sid": "Stmt137652664067",
   725  		 "Effect": "Allow",
   726  		 "Principal": {
   727  			 "AWS": "arn:aws:iam::902366379725:user/logs"
   728  		 },
   729  		 "Action": "s3:GetBucketAcl",
   730  		 "Resource": "arn:aws:s3:::tf-redshift-logging-%d"
   731  	 }
   732   ]
   733  }
   734  EOF
   735   }
   736  
   737  
   738   resource "aws_redshift_cluster" "default" {
   739  	 cluster_identifier = "tf-redshift-cluster-%d"
   740  	 availability_zone = "us-west-2a"
   741  	 database_name = "mydb"
   742  	 master_username = "foo_test"
   743  	 master_password = "Mustbe8characters"
   744  	 node_type = "dc1.large"
   745  	 automated_snapshot_retention_period = 0
   746  	 allow_version_upgrade = false
   747  	 enable_logging = true
   748  	 bucket_name = "${aws_s3_bucket.bucket.bucket}"
   749  	 skip_final_snapshot = true
   750   }`, rInt, rInt, rInt, rInt)
   751  }
   752  
   753  var testAccAWSRedshiftClusterConfig_tags = `
   754  resource "aws_redshift_cluster" "default" {
   755    cluster_identifier = "tf-redshift-cluster-%d"
   756    availability_zone = "us-west-2a"
   757    database_name = "mydb"
   758    master_username = "foo"
   759    master_password = "Mustbe8characters"
   760    node_type = "dc1.large"
   761    automated_snapshot_retention_period = 7
   762    allow_version_upgrade = false
   763    skip_final_snapshot = true
   764    tags {
   765      environment = "Production"
   766      cluster = "reader"
   767      Type = "master"
   768    }
   769  }`
   770  
   771  var testAccAWSRedshiftClusterConfig_updatedTags = `
   772  resource "aws_redshift_cluster" "default" {
   773    cluster_identifier = "tf-redshift-cluster-%d"
   774    availability_zone = "us-west-2a"
   775    database_name = "mydb"
   776    master_username = "foo"
   777    master_password = "Mustbe8characters"
   778    node_type = "dc1.large"
   779    automated_snapshot_retention_period = 7
   780    allow_version_upgrade = false
   781    skip_final_snapshot = true
   782    tags {
   783      environment = "Production"
   784    }
   785  }`
   786  
   787  func testAccAWSRedshiftClusterConfig_notPubliclyAccessible(rInt int) string {
   788  	return fmt.Sprintf(`
   789  	resource "aws_vpc" "foo" {
   790  		cidr_block = "10.1.0.0/16"
   791  	}
   792  	resource "aws_internet_gateway" "foo" {
   793  		vpc_id = "${aws_vpc.foo.id}"
   794  		tags {
   795  			foo = "bar"
   796  		}
   797  	}
   798  	resource "aws_subnet" "foo" {
   799  		cidr_block = "10.1.1.0/24"
   800  		availability_zone = "us-west-2a"
   801  		vpc_id = "${aws_vpc.foo.id}"
   802  		tags {
   803  			Name = "tf-dbsubnet-test-1"
   804  		}
   805  	}
   806  	resource "aws_subnet" "bar" {
   807  		cidr_block = "10.1.2.0/24"
   808  		availability_zone = "us-west-2b"
   809  		vpc_id = "${aws_vpc.foo.id}"
   810  		tags {
   811  			Name = "tf-dbsubnet-test-2"
   812  		}
   813  	}
   814  	resource "aws_subnet" "foobar" {
   815  		cidr_block = "10.1.3.0/24"
   816  		availability_zone = "us-west-2c"
   817  		vpc_id = "${aws_vpc.foo.id}"
   818  		tags {
   819  			Name = "tf-dbsubnet-test-3"
   820  		}
   821  	}
   822  	resource "aws_redshift_subnet_group" "foo" {
   823  		name = "foo-%d"
   824  		description = "foo description"
   825  		subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}", "${aws_subnet.foobar.id}"]
   826  	}
   827  	resource "aws_redshift_cluster" "default" {
   828  		cluster_identifier = "tf-redshift-cluster-%d"
   829  		availability_zone = "us-west-2a"
   830  		database_name = "mydb"
   831  		master_username = "foo"
   832  		master_password = "Mustbe8characters"
   833  		node_type = "dc1.large"
   834  		automated_snapshot_retention_period = 0
   835  		allow_version_upgrade = false
   836  		cluster_subnet_group_name = "${aws_redshift_subnet_group.foo.name}"
   837  		publicly_accessible = false
   838  		skip_final_snapshot = true
   839  	}`, rInt, rInt)
   840  }
   841  
   842  func testAccAWSRedshiftClusterConfig_updatePubliclyAccessible(rInt int) string {
   843  	return fmt.Sprintf(`
   844  	resource "aws_vpc" "foo" {
   845  		cidr_block = "10.1.0.0/16"
   846  	}
   847  	resource "aws_internet_gateway" "foo" {
   848  		vpc_id = "${aws_vpc.foo.id}"
   849  		tags {
   850  			foo = "bar"
   851  		}
   852  	}
   853  	resource "aws_subnet" "foo" {
   854  		cidr_block = "10.1.1.0/24"
   855  		availability_zone = "us-west-2a"
   856  		vpc_id = "${aws_vpc.foo.id}"
   857  		tags {
   858  			Name = "tf-dbsubnet-test-1"
   859  		}
   860  	}
   861  	resource "aws_subnet" "bar" {
   862  		cidr_block = "10.1.2.0/24"
   863  		availability_zone = "us-west-2b"
   864  		vpc_id = "${aws_vpc.foo.id}"
   865  		tags {
   866  			Name = "tf-dbsubnet-test-2"
   867  		}
   868  	}
   869  	resource "aws_subnet" "foobar" {
   870  		cidr_block = "10.1.3.0/24"
   871  		availability_zone = "us-west-2c"
   872  		vpc_id = "${aws_vpc.foo.id}"
   873  		tags {
   874  			Name = "tf-dbsubnet-test-3"
   875  		}
   876  	}
   877  	resource "aws_redshift_subnet_group" "foo" {
   878  		name = "foo-%d"
   879  		description = "foo description"
   880  		subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}", "${aws_subnet.foobar.id}"]
   881  	}
   882  	resource "aws_redshift_cluster" "default" {
   883  		cluster_identifier = "tf-redshift-cluster-%d"
   884  		availability_zone = "us-west-2a"
   885  		database_name = "mydb"
   886  		master_username = "foo"
   887  		master_password = "Mustbe8characters"
   888  		node_type = "dc1.large"
   889  		automated_snapshot_retention_period = 0
   890  		allow_version_upgrade = false
   891  		cluster_subnet_group_name = "${aws_redshift_subnet_group.foo.name}"
   892  		publicly_accessible = true
   893  		skip_final_snapshot = true
   894  	}`, rInt, rInt)
   895  }
   896  
   897  var testAccAWSRedshiftClusterConfig_iamRoles = `
   898  resource "aws_iam_role" "ec2-role" {
   899  	name   = "test-role-ec2-%d"
   900  	path = "/"
   901   	assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}"
   902  }
   903  
   904  resource "aws_iam_role" "lambda-role" {
   905   	name   = "test-role-lambda-%d"
   906   	path = "/"
   907   	assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"lambda.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}"
   908  }
   909  
   910  resource "aws_redshift_cluster" "default" {
   911     cluster_identifier = "tf-redshift-cluster-%d"
   912     availability_zone = "us-west-2a"
   913     database_name = "mydb"
   914     master_username = "foo_test"
   915     master_password = "Mustbe8characters"
   916     node_type = "dc1.large"
   917     automated_snapshot_retention_period = 0
   918     allow_version_upgrade = false
   919     iam_roles = ["${aws_iam_role.ec2-role.arn}", "${aws_iam_role.lambda-role.arn}"]
   920     skip_final_snapshot = true
   921  }`
   922  
   923  var testAccAWSRedshiftClusterConfig_updateIamRoles = `
   924  resource "aws_iam_role" "ec2-role" {
   925   	name   = "test-role-ec2-%d"
   926   	path = "/"
   927   	assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}"
   928   }
   929  
   930   resource "aws_iam_role" "lambda-role" {
   931   	name   = "test-role-lambda-%d"
   932   	path = "/"
   933   	assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"lambda.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}"
   934   }
   935  
   936   resource "aws_redshift_cluster" "default" {
   937     cluster_identifier = "tf-redshift-cluster-%d"
   938     availability_zone = "us-west-2a"
   939     database_name = "mydb"
   940     master_username = "foo_test"
   941     master_password = "Mustbe8characters"
   942     node_type = "dc1.large"
   943     automated_snapshot_retention_period = 0
   944     allow_version_upgrade = false
   945     iam_roles = ["${aws_iam_role.ec2-role.arn}"]
   946     skip_final_snapshot = true
   947   }`