github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/builtin/providers/aws/resource_aws_redshift_cluster_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     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/redshift"
    12  	"github.com/hashicorp/terraform/helper/resource"
    13  	"github.com/hashicorp/terraform/terraform"
    14  )
    15  
    16  func TestAccAWSRedshiftCluster_basic(t *testing.T) {
    17  	var v redshift.Cluster
    18  
    19  	ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
    20  	config := fmt.Sprintf(testAccAWSRedshiftClusterConfig_basic, ri)
    21  
    22  	resource.Test(t, resource.TestCase{
    23  		PreCheck:     func() { testAccPreCheck(t) },
    24  		Providers:    testAccProviders,
    25  		CheckDestroy: testAccCheckAWSRedshiftClusterDestroy,
    26  		Steps: []resource.TestStep{
    27  			resource.TestStep{
    28  				Config: config,
    29  				Check: resource.ComposeTestCheckFunc(
    30  					testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v),
    31  					resource.TestCheckResourceAttr(
    32  						"aws_redshift_cluster.default", "cluster_type", "single-node"),
    33  					resource.TestCheckResourceAttr(
    34  						"aws_redshift_cluster.default", "publicly_accessible", "true"),
    35  				),
    36  			},
    37  		},
    38  	})
    39  }
    40  
    41  func TestAccAWSRedshiftCluster_publiclyAccessible(t *testing.T) {
    42  	var v redshift.Cluster
    43  
    44  	ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
    45  	preConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_notPubliclyAccessible, ri)
    46  	postConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_updatePubliclyAccessible, ri)
    47  
    48  	resource.Test(t, resource.TestCase{
    49  		PreCheck:     func() { testAccPreCheck(t) },
    50  		Providers:    testAccProviders,
    51  		CheckDestroy: testAccCheckAWSRedshiftClusterDestroy,
    52  		Steps: []resource.TestStep{
    53  			resource.TestStep{
    54  				Config: preConfig,
    55  				Check: resource.ComposeTestCheckFunc(
    56  					testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v),
    57  					resource.TestCheckResourceAttr(
    58  						"aws_redshift_cluster.default", "publicly_accessible", "false"),
    59  				),
    60  			},
    61  
    62  			resource.TestStep{
    63  				Config: postConfig,
    64  				Check: resource.ComposeTestCheckFunc(
    65  					testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v),
    66  					resource.TestCheckResourceAttr(
    67  						"aws_redshift_cluster.default", "publicly_accessible", "true"),
    68  				),
    69  			},
    70  		},
    71  	})
    72  }
    73  
    74  func testAccCheckAWSRedshiftClusterDestroy(s *terraform.State) error {
    75  	for _, rs := range s.RootModule().Resources {
    76  		if rs.Type != "aws_redshift_cluster" {
    77  			continue
    78  		}
    79  
    80  		// Try to find the Group
    81  		conn := testAccProvider.Meta().(*AWSClient).redshiftconn
    82  		var err error
    83  		resp, err := conn.DescribeClusters(
    84  			&redshift.DescribeClustersInput{
    85  				ClusterIdentifier: aws.String(rs.Primary.ID),
    86  			})
    87  
    88  		if err == nil {
    89  			if len(resp.Clusters) != 0 &&
    90  				*resp.Clusters[0].ClusterIdentifier == rs.Primary.ID {
    91  				return fmt.Errorf("Redshift Cluster %s still exists", rs.Primary.ID)
    92  			}
    93  		}
    94  
    95  		// Return nil if the cluster is already destroyed
    96  		if awsErr, ok := err.(awserr.Error); ok {
    97  			if awsErr.Code() == "ClusterNotFound" {
    98  				return nil
    99  			}
   100  		}
   101  
   102  		return err
   103  	}
   104  
   105  	return nil
   106  }
   107  
   108  func testAccCheckAWSRedshiftClusterExists(n string, v *redshift.Cluster) resource.TestCheckFunc {
   109  	return func(s *terraform.State) error {
   110  		rs, ok := s.RootModule().Resources[n]
   111  		if !ok {
   112  			return fmt.Errorf("Not found: %s", n)
   113  		}
   114  
   115  		if rs.Primary.ID == "" {
   116  			return fmt.Errorf("No Redshift Cluster Instance ID is set")
   117  		}
   118  
   119  		conn := testAccProvider.Meta().(*AWSClient).redshiftconn
   120  		resp, err := conn.DescribeClusters(&redshift.DescribeClustersInput{
   121  			ClusterIdentifier: aws.String(rs.Primary.ID),
   122  		})
   123  
   124  		if err != nil {
   125  			return err
   126  		}
   127  
   128  		for _, c := range resp.Clusters {
   129  			if *c.ClusterIdentifier == rs.Primary.ID {
   130  				*v = *c
   131  				return nil
   132  			}
   133  		}
   134  
   135  		return fmt.Errorf("Redshift Cluster (%s) not found", rs.Primary.ID)
   136  	}
   137  }
   138  
   139  func TestResourceAWSRedshiftClusterIdentifierValidation(t *testing.T) {
   140  	cases := []struct {
   141  		Value    string
   142  		ErrCount int
   143  	}{
   144  		{
   145  			Value:    "tEsting",
   146  			ErrCount: 1,
   147  		},
   148  		{
   149  			Value:    "1testing",
   150  			ErrCount: 1,
   151  		},
   152  		{
   153  			Value:    "testing--123",
   154  			ErrCount: 1,
   155  		},
   156  		{
   157  			Value:    "testing!",
   158  			ErrCount: 1,
   159  		},
   160  		{
   161  			Value:    "testing-",
   162  			ErrCount: 1,
   163  		},
   164  	}
   165  
   166  	for _, tc := range cases {
   167  		_, errors := validateRedshiftClusterIdentifier(tc.Value, "aws_redshift_cluster_identifier")
   168  
   169  		if len(errors) != tc.ErrCount {
   170  			t.Fatalf("Expected the Redshift Cluster cluster_identifier to trigger a validation error")
   171  		}
   172  	}
   173  }
   174  
   175  func TestResourceAWSRedshiftClusterDbNameValidation(t *testing.T) {
   176  	cases := []struct {
   177  		Value    string
   178  		ErrCount int
   179  	}{
   180  		{
   181  			Value:    "tEsting",
   182  			ErrCount: 1,
   183  		},
   184  		{
   185  			Value:    "testing1",
   186  			ErrCount: 1,
   187  		},
   188  		{
   189  			Value:    "testing-",
   190  			ErrCount: 1,
   191  		},
   192  		{
   193  			Value:    "",
   194  			ErrCount: 2,
   195  		},
   196  		{
   197  			Value:    randomString(65),
   198  			ErrCount: 1,
   199  		},
   200  	}
   201  
   202  	for _, tc := range cases {
   203  		_, errors := validateRedshiftClusterDbName(tc.Value, "aws_redshift_cluster_database_name")
   204  
   205  		if len(errors) != tc.ErrCount {
   206  			t.Fatalf("Expected the Redshift Cluster database_name to trigger a validation error")
   207  		}
   208  	}
   209  }
   210  
   211  func TestResourceAWSRedshiftClusterFinalSnapshotIdentifierValidation(t *testing.T) {
   212  	cases := []struct {
   213  		Value    string
   214  		ErrCount int
   215  	}{
   216  		{
   217  			Value:    "testing--123",
   218  			ErrCount: 1,
   219  		},
   220  		{
   221  			Value:    "testing-",
   222  			ErrCount: 1,
   223  		},
   224  		{
   225  			Value:    "Testingq123!",
   226  			ErrCount: 1,
   227  		},
   228  		{
   229  			Value:    randomString(256),
   230  			ErrCount: 1,
   231  		},
   232  	}
   233  
   234  	for _, tc := range cases {
   235  		_, errors := validateRedshiftClusterFinalSnapshotIdentifier(tc.Value, "aws_redshift_cluster_final_snapshot_identifier")
   236  
   237  		if len(errors) != tc.ErrCount {
   238  			t.Fatalf("Expected the Redshift Cluster final_snapshot_identifier to trigger a validation error")
   239  		}
   240  	}
   241  }
   242  
   243  func TestResourceAWSRedshiftClusterMasterUsernameValidation(t *testing.T) {
   244  	cases := []struct {
   245  		Value    string
   246  		ErrCount int
   247  	}{
   248  		{
   249  			Value:    "1Testing",
   250  			ErrCount: 1,
   251  		},
   252  		{
   253  			Value:    "Testing!!",
   254  			ErrCount: 1,
   255  		},
   256  		{
   257  			Value:    randomString(129),
   258  			ErrCount: 1,
   259  		},
   260  		{
   261  			Value:    "testing_testing123",
   262  			ErrCount: 0,
   263  		},
   264  	}
   265  
   266  	for _, tc := range cases {
   267  		_, errors := validateRedshiftClusterMasterUsername(tc.Value, "aws_redshift_cluster_master_username")
   268  
   269  		if len(errors) != tc.ErrCount {
   270  			t.Fatalf("Expected the Redshift Cluster master_username to trigger a validation error")
   271  		}
   272  	}
   273  }
   274  
   275  var testAccAWSRedshiftClusterConfig_basic = `
   276  provider "aws" {
   277  	region = "us-west-2"
   278  }
   279  
   280  resource "aws_redshift_cluster" "default" {
   281    cluster_identifier = "tf-redshift-cluster-%d"
   282    availability_zone = "us-west-2a"
   283    database_name = "mydb"
   284    master_username = "foo_test"
   285    master_password = "Mustbe8characters"
   286    node_type = "dc1.large"
   287    automated_snapshot_retention_period = 7
   288    allow_version_upgrade = false
   289  }`
   290  
   291  var testAccAWSRedshiftClusterConfig_notPubliclyAccessible = `
   292  provider "aws" {
   293  	region = "us-west-2"
   294  }
   295  
   296  resource "aws_vpc" "foo" {
   297  	cidr_block = "10.1.0.0/16"
   298  }
   299  
   300  resource "aws_internet_gateway" "foo" {
   301  	vpc_id = "${aws_vpc.foo.id}"
   302  	tags {
   303  		foo = "bar"
   304  	}
   305  }
   306  
   307  resource "aws_subnet" "foo" {
   308  	cidr_block = "10.1.1.0/24"
   309  	availability_zone = "us-west-2a"
   310  	vpc_id = "${aws_vpc.foo.id}"
   311  	tags {
   312  		Name = "tf-dbsubnet-test-1"
   313  	}
   314  }
   315  
   316  resource "aws_subnet" "bar" {
   317  	cidr_block = "10.1.2.0/24"
   318  	availability_zone = "us-west-2b"
   319  	vpc_id = "${aws_vpc.foo.id}"
   320  	tags {
   321  		Name = "tf-dbsubnet-test-2"
   322  	}
   323  }
   324  
   325  resource "aws_subnet" "foobar" {
   326  	cidr_block = "10.1.3.0/24"
   327  	availability_zone = "us-west-2c"
   328  	vpc_id = "${aws_vpc.foo.id}"
   329  	tags {
   330  		Name = "tf-dbsubnet-test-3"
   331  	}
   332  }
   333  
   334  resource "aws_redshift_subnet_group" "foo" {
   335  	name = "foo"
   336  	description = "foo description"
   337  	subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}", "${aws_subnet.foobar.id}"]
   338  }
   339  
   340  resource "aws_redshift_cluster" "default" {
   341    cluster_identifier = "tf-redshift-cluster-%d"
   342    availability_zone = "us-west-2a"
   343    database_name = "mydb"
   344    master_username = "foo"
   345    master_password = "Mustbe8characters"
   346    node_type = "dc1.large"
   347    automated_snapshot_retention_period = 7
   348    allow_version_upgrade = false
   349    cluster_subnet_group_name = "${aws_redshift_subnet_group.foo.name}"
   350    publicly_accessible = false
   351  }`
   352  
   353  var testAccAWSRedshiftClusterConfig_updatePubliclyAccessible = `
   354  provider "aws" {
   355  	region = "us-west-2"
   356  }
   357  
   358  resource "aws_vpc" "foo" {
   359  	cidr_block = "10.1.0.0/16"
   360  }
   361  
   362  resource "aws_internet_gateway" "foo" {
   363  	vpc_id = "${aws_vpc.foo.id}"
   364  	tags {
   365  		foo = "bar"
   366  	}
   367  }
   368  
   369  resource "aws_subnet" "foo" {
   370  	cidr_block = "10.1.1.0/24"
   371  	availability_zone = "us-west-2a"
   372  	vpc_id = "${aws_vpc.foo.id}"
   373  	tags {
   374  		Name = "tf-dbsubnet-test-1"
   375  	}
   376  }
   377  
   378  resource "aws_subnet" "bar" {
   379  	cidr_block = "10.1.2.0/24"
   380  	availability_zone = "us-west-2b"
   381  	vpc_id = "${aws_vpc.foo.id}"
   382  	tags {
   383  		Name = "tf-dbsubnet-test-2"
   384  	}
   385  }
   386  
   387  resource "aws_subnet" "foobar" {
   388  	cidr_block = "10.1.3.0/24"
   389  	availability_zone = "us-west-2c"
   390  	vpc_id = "${aws_vpc.foo.id}"
   391  	tags {
   392  		Name = "tf-dbsubnet-test-3"
   393  	}
   394  }
   395  
   396  resource "aws_redshift_subnet_group" "foo" {
   397  	name = "foo"
   398  	description = "foo description"
   399  	subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}", "${aws_subnet.foobar.id}"]
   400  }
   401  
   402  resource "aws_redshift_cluster" "default" {
   403    cluster_identifier = "tf-redshift-cluster-%d"
   404    availability_zone = "us-west-2a"
   405    database_name = "mydb"
   406    master_username = "foo"
   407    master_password = "Mustbe8characters"
   408    node_type = "dc1.large"
   409    automated_snapshot_retention_period = 7
   410    allow_version_upgrade = false
   411    cluster_subnet_group_name = "${aws_redshift_subnet_group.foo.name}"
   412    publicly_accessible = true
   413  }`