github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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_updateSubnetIds(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", "subnet_ids.#", "2"),
    50  				),
    51  			},
    52  
    53  			resource.TestStep{
    54  				Config: testAccRedshiftSubnetGroupConfig_updateSubnetIds,
    55  				Check: resource.ComposeTestCheckFunc(
    56  					testAccCheckRedshiftSubnetGroupExists("aws_redshift_subnet_group.foo", &v),
    57  					resource.TestCheckResourceAttr(
    58  						"aws_redshift_subnet_group.foo", "subnet_ids.#", "3"),
    59  				),
    60  			},
    61  		},
    62  	})
    63  }
    64  
    65  func TestResourceAWSRedshiftSubnetGroupNameValidation(t *testing.T) {
    66  	cases := []struct {
    67  		Value    string
    68  		ErrCount int
    69  	}{
    70  		{
    71  			Value:    "default",
    72  			ErrCount: 1,
    73  		},
    74  		{
    75  			Value:    "testing123%%",
    76  			ErrCount: 1,
    77  		},
    78  		{
    79  			Value:    "TestingSG",
    80  			ErrCount: 1,
    81  		},
    82  		{
    83  			Value:    randomString(256),
    84  			ErrCount: 1,
    85  		},
    86  	}
    87  
    88  	for _, tc := range cases {
    89  		_, errors := validateRedshiftSubnetGroupName(tc.Value, "aws_redshift_subnet_group_name")
    90  
    91  		if len(errors) != tc.ErrCount {
    92  			t.Fatalf("Expected the Redshift Subnet Group Name to trigger a validation error")
    93  		}
    94  	}
    95  }
    96  
    97  func testAccCheckRedshiftSubnetGroupDestroy(s *terraform.State) error {
    98  	conn := testAccProvider.Meta().(*AWSClient).redshiftconn
    99  
   100  	for _, rs := range s.RootModule().Resources {
   101  		if rs.Type != "aws_redshift_subnet_group" {
   102  			continue
   103  		}
   104  
   105  		resp, err := conn.DescribeClusterSubnetGroups(
   106  			&redshift.DescribeClusterSubnetGroupsInput{
   107  				ClusterSubnetGroupName: aws.String(rs.Primary.ID)})
   108  		if err == nil {
   109  			if len(resp.ClusterSubnetGroups) > 0 {
   110  				return fmt.Errorf("still exist.")
   111  			}
   112  
   113  			return nil
   114  		}
   115  
   116  		redshiftErr, ok := err.(awserr.Error)
   117  		if !ok {
   118  			return err
   119  		}
   120  		if redshiftErr.Code() != "ClusterSubnetGroupNotFoundFault" {
   121  			return err
   122  		}
   123  	}
   124  
   125  	return nil
   126  }
   127  
   128  func testAccCheckRedshiftSubnetGroupExists(n string, v *redshift.ClusterSubnetGroup) resource.TestCheckFunc {
   129  	return func(s *terraform.State) error {
   130  		rs, ok := s.RootModule().Resources[n]
   131  		if !ok {
   132  			return fmt.Errorf("Not found: %s", n)
   133  		}
   134  
   135  		if rs.Primary.ID == "" {
   136  			return fmt.Errorf("No ID is set")
   137  		}
   138  
   139  		conn := testAccProvider.Meta().(*AWSClient).redshiftconn
   140  		resp, err := conn.DescribeClusterSubnetGroups(
   141  			&redshift.DescribeClusterSubnetGroupsInput{ClusterSubnetGroupName: aws.String(rs.Primary.ID)})
   142  		if err != nil {
   143  			return err
   144  		}
   145  		if len(resp.ClusterSubnetGroups) == 0 {
   146  			return fmt.Errorf("ClusterSubnetGroup not found")
   147  		}
   148  
   149  		*v = *resp.ClusterSubnetGroups[0]
   150  
   151  		return nil
   152  	}
   153  }
   154  
   155  const testAccRedshiftSubnetGroupConfig = `
   156  resource "aws_vpc" "foo" {
   157  	cidr_block = "10.1.0.0/16"
   158  }
   159  
   160  resource "aws_subnet" "foo" {
   161  	cidr_block = "10.1.1.0/24"
   162  	availability_zone = "us-west-2a"
   163  	vpc_id = "${aws_vpc.foo.id}"
   164  	tags {
   165  		Name = "tf-dbsubnet-test-1"
   166  	}
   167  }
   168  
   169  resource "aws_subnet" "bar" {
   170  	cidr_block = "10.1.2.0/24"
   171  	availability_zone = "us-west-2b"
   172  	vpc_id = "${aws_vpc.foo.id}"
   173  	tags {
   174  		Name = "tf-dbsubnet-test-2"
   175  	}
   176  }
   177  
   178  resource "aws_redshift_subnet_group" "foo" {
   179  	name = "foo"
   180  	subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"]
   181  }
   182  `
   183  
   184  const testAccRedshiftSubnetGroupConfig_updateSubnetIds = `
   185  resource "aws_vpc" "foo" {
   186  	cidr_block = "10.1.0.0/16"
   187  }
   188  
   189  resource "aws_subnet" "foo" {
   190  	cidr_block = "10.1.1.0/24"
   191  	availability_zone = "us-west-2a"
   192  	vpc_id = "${aws_vpc.foo.id}"
   193  	tags {
   194  		Name = "tf-dbsubnet-test-1"
   195  	}
   196  }
   197  
   198  resource "aws_subnet" "bar" {
   199  	cidr_block = "10.1.2.0/24"
   200  	availability_zone = "us-west-2b"
   201  	vpc_id = "${aws_vpc.foo.id}"
   202  	tags {
   203  		Name = "tf-dbsubnet-test-2"
   204  	}
   205  }
   206  
   207  resource "aws_subnet" "foobar" {
   208  	cidr_block = "10.1.3.0/24"
   209  	availability_zone = "us-west-2c"
   210  	vpc_id = "${aws_vpc.foo.id}"
   211  	tags {
   212  		Name = "tf-dbsubnet-test-3"
   213  	}
   214  }
   215  
   216  resource "aws_redshift_subnet_group" "foo" {
   217  	name = "foo"
   218  	subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}", "${aws_subnet.foobar.id}"]
   219  }
   220  `