github.com/renier/terraform@v0.7.8-0.20161024133817-eb8a9ef5471a/builtin/providers/aws/resource_aws_redshift_subnet_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 TestAccAWSRedshiftSubnetGroup_basic(t *testing.T) {
    15  	var v redshift.ClusterSubnetGroup
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckRedshiftSubnetGroupDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccRedshiftSubnetGroupConfig,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckRedshiftSubnetGroupExists("aws_redshift_subnet_group.foo", &v),
    26  					resource.TestCheckResourceAttr(
    27  						"aws_redshift_subnet_group.foo", "subnet_ids.#", "2"),
    28  					resource.TestCheckResourceAttr(
    29  						"aws_redshift_subnet_group.foo", "description", "Managed by Terraform"),
    30  				),
    31  			},
    32  		},
    33  	})
    34  }
    35  
    36  func TestAccAWSRedshiftSubnetGroup_updateDescription(t *testing.T) {
    37  	var v redshift.ClusterSubnetGroup
    38  
    39  	resource.Test(t, resource.TestCase{
    40  		PreCheck:     func() { testAccPreCheck(t) },
    41  		Providers:    testAccProviders,
    42  		CheckDestroy: testAccCheckRedshiftSubnetGroupDestroy,
    43  		Steps: []resource.TestStep{
    44  			resource.TestStep{
    45  				Config: testAccRedshiftSubnetGroupConfig,
    46  				Check: resource.ComposeTestCheckFunc(
    47  					testAccCheckRedshiftSubnetGroupExists("aws_redshift_subnet_group.foo", &v),
    48  					resource.TestCheckResourceAttr(
    49  						"aws_redshift_subnet_group.foo", "description", "foo description"),
    50  				),
    51  			},
    52  
    53  			resource.TestStep{
    54  				Config: testAccRedshiftSubnetGroup_updateDescription,
    55  				Check: resource.ComposeTestCheckFunc(
    56  					testAccCheckRedshiftSubnetGroupExists("aws_redshift_subnet_group.foo", &v),
    57  					resource.TestCheckResourceAttr(
    58  						"aws_redshift_subnet_group.foo", "description", "foo description updated"),
    59  				),
    60  			},
    61  		},
    62  	})
    63  }
    64  
    65  func TestAccAWSRedshiftSubnetGroup_updateSubnetIds(t *testing.T) {
    66  	var v redshift.ClusterSubnetGroup
    67  
    68  	resource.Test(t, resource.TestCase{
    69  		PreCheck:     func() { testAccPreCheck(t) },
    70  		Providers:    testAccProviders,
    71  		CheckDestroy: testAccCheckRedshiftSubnetGroupDestroy,
    72  		Steps: []resource.TestStep{
    73  			resource.TestStep{
    74  				Config: testAccRedshiftSubnetGroupConfig,
    75  				Check: resource.ComposeTestCheckFunc(
    76  					testAccCheckRedshiftSubnetGroupExists("aws_redshift_subnet_group.foo", &v),
    77  					resource.TestCheckResourceAttr(
    78  						"aws_redshift_subnet_group.foo", "subnet_ids.#", "2"),
    79  				),
    80  			},
    81  
    82  			resource.TestStep{
    83  				Config: testAccRedshiftSubnetGroupConfig_updateSubnetIds,
    84  				Check: resource.ComposeTestCheckFunc(
    85  					testAccCheckRedshiftSubnetGroupExists("aws_redshift_subnet_group.foo", &v),
    86  					resource.TestCheckResourceAttr(
    87  						"aws_redshift_subnet_group.foo", "subnet_ids.#", "3"),
    88  				),
    89  			},
    90  		},
    91  	})
    92  }
    93  
    94  func TestResourceAWSRedshiftSubnetGroupNameValidation(t *testing.T) {
    95  	cases := []struct {
    96  		Value    string
    97  		ErrCount int
    98  	}{
    99  		{
   100  			Value:    "default",
   101  			ErrCount: 1,
   102  		},
   103  		{
   104  			Value:    "testing123%%",
   105  			ErrCount: 1,
   106  		},
   107  		{
   108  			Value:    "TestingSG",
   109  			ErrCount: 1,
   110  		},
   111  		{
   112  			Value:    "testing_123",
   113  			ErrCount: 1,
   114  		},
   115  		{
   116  			Value:    "testing.123",
   117  			ErrCount: 1,
   118  		},
   119  		{
   120  			Value:    randomString(256),
   121  			ErrCount: 1,
   122  		},
   123  	}
   124  
   125  	for _, tc := range cases {
   126  		_, errors := validateRedshiftSubnetGroupName(tc.Value, "aws_redshift_subnet_group_name")
   127  
   128  		if len(errors) != tc.ErrCount {
   129  			t.Fatalf("Expected the Redshift Subnet Group Name to trigger a validation error")
   130  		}
   131  	}
   132  }
   133  
   134  func testAccCheckRedshiftSubnetGroupDestroy(s *terraform.State) error {
   135  	conn := testAccProvider.Meta().(*AWSClient).redshiftconn
   136  
   137  	for _, rs := range s.RootModule().Resources {
   138  		if rs.Type != "aws_redshift_subnet_group" {
   139  			continue
   140  		}
   141  
   142  		resp, err := conn.DescribeClusterSubnetGroups(
   143  			&redshift.DescribeClusterSubnetGroupsInput{
   144  				ClusterSubnetGroupName: aws.String(rs.Primary.ID)})
   145  		if err == nil {
   146  			if len(resp.ClusterSubnetGroups) > 0 {
   147  				return fmt.Errorf("still exist.")
   148  			}
   149  
   150  			return nil
   151  		}
   152  
   153  		redshiftErr, ok := err.(awserr.Error)
   154  		if !ok {
   155  			return err
   156  		}
   157  		if redshiftErr.Code() != "ClusterSubnetGroupNotFoundFault" {
   158  			return err
   159  		}
   160  	}
   161  
   162  	return nil
   163  }
   164  
   165  func testAccCheckRedshiftSubnetGroupExists(n string, v *redshift.ClusterSubnetGroup) resource.TestCheckFunc {
   166  	return func(s *terraform.State) error {
   167  		rs, ok := s.RootModule().Resources[n]
   168  		if !ok {
   169  			return fmt.Errorf("Not found: %s", n)
   170  		}
   171  
   172  		if rs.Primary.ID == "" {
   173  			return fmt.Errorf("No ID is set")
   174  		}
   175  
   176  		conn := testAccProvider.Meta().(*AWSClient).redshiftconn
   177  		resp, err := conn.DescribeClusterSubnetGroups(
   178  			&redshift.DescribeClusterSubnetGroupsInput{ClusterSubnetGroupName: aws.String(rs.Primary.ID)})
   179  		if err != nil {
   180  			return err
   181  		}
   182  		if len(resp.ClusterSubnetGroups) == 0 {
   183  			return fmt.Errorf("ClusterSubnetGroup not found")
   184  		}
   185  
   186  		*v = *resp.ClusterSubnetGroups[0]
   187  
   188  		return nil
   189  	}
   190  }
   191  
   192  const testAccRedshiftSubnetGroupConfig = `
   193  resource "aws_vpc" "foo" {
   194  	cidr_block = "10.1.0.0/16"
   195  }
   196  
   197  resource "aws_subnet" "foo" {
   198  	cidr_block = "10.1.1.0/24"
   199  	availability_zone = "us-west-2a"
   200  	vpc_id = "${aws_vpc.foo.id}"
   201  	tags {
   202  		Name = "tf-dbsubnet-test-1"
   203  	}
   204  }
   205  
   206  resource "aws_subnet" "bar" {
   207  	cidr_block = "10.1.2.0/24"
   208  	availability_zone = "us-west-2b"
   209  	vpc_id = "${aws_vpc.foo.id}"
   210  	tags {
   211  		Name = "tf-dbsubnet-test-2"
   212  	}
   213  }
   214  
   215  resource "aws_redshift_subnet_group" "foo" {
   216  	name = "foo"
   217  	description = "foo description"
   218  	subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"]
   219  }
   220  `
   221  
   222  const testAccRedshiftSubnetGroup_updateDescription = `
   223  resource "aws_vpc" "foo" {
   224  	cidr_block = "10.1.0.0/16"
   225  }
   226  
   227  resource "aws_subnet" "foo" {
   228  	cidr_block = "10.1.1.0/24"
   229  	availability_zone = "us-west-2a"
   230  	vpc_id = "${aws_vpc.foo.id}"
   231  	tags {
   232  		Name = "tf-dbsubnet-test-1"
   233  	}
   234  }
   235  
   236  resource "aws_subnet" "bar" {
   237  	cidr_block = "10.1.2.0/24"
   238  	availability_zone = "us-west-2b"
   239  	vpc_id = "${aws_vpc.foo.id}"
   240  	tags {
   241  		Name = "tf-dbsubnet-test-2"
   242  	}
   243  }
   244  
   245  resource "aws_redshift_subnet_group" "foo" {
   246  	name = "foo"
   247  	description = "foo description updated"
   248  	subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"]
   249  }
   250  `
   251  
   252  const testAccRedshiftSubnetGroupConfig_updateSubnetIds = `
   253  resource "aws_vpc" "foo" {
   254  	cidr_block = "10.1.0.0/16"
   255  }
   256  
   257  resource "aws_subnet" "foo" {
   258  	cidr_block = "10.1.1.0/24"
   259  	availability_zone = "us-west-2a"
   260  	vpc_id = "${aws_vpc.foo.id}"
   261  	tags {
   262  		Name = "tf-dbsubnet-test-1"
   263  	}
   264  }
   265  
   266  resource "aws_subnet" "bar" {
   267  	cidr_block = "10.1.2.0/24"
   268  	availability_zone = "us-west-2b"
   269  	vpc_id = "${aws_vpc.foo.id}"
   270  	tags {
   271  		Name = "tf-dbsubnet-test-2"
   272  	}
   273  }
   274  
   275  resource "aws_subnet" "foobar" {
   276  	cidr_block = "10.1.3.0/24"
   277  	availability_zone = "us-west-2c"
   278  	vpc_id = "${aws_vpc.foo.id}"
   279  	tags {
   280  		Name = "tf-dbsubnet-test-3"
   281  	}
   282  }
   283  
   284  resource "aws_redshift_subnet_group" "foo" {
   285  	name = "foo"
   286  	subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}", "${aws_subnet.foobar.id}"]
   287  }
   288  `