github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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", "foo description"),
    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 TestAccAWSRedshiftSubnetGroup_tags(t *testing.T) {
    95  	var v redshift.ClusterSubnetGroup
    96  
    97  	resource.Test(t, resource.TestCase{
    98  		PreCheck:     func() { testAccPreCheck(t) },
    99  		Providers:    testAccProviders,
   100  		CheckDestroy: testAccCheckRedshiftSubnetGroupDestroy,
   101  		Steps: []resource.TestStep{
   102  			{
   103  				Config: testAccRedshiftSubnetGroupConfigWithTags,
   104  				Check: resource.ComposeTestCheckFunc(
   105  					testAccCheckRedshiftSubnetGroupExists("aws_redshift_subnet_group.foo", &v),
   106  					resource.TestCheckResourceAttr(
   107  						"aws_redshift_subnet_group.foo", "tags.%", "1"),
   108  					resource.TestCheckResourceAttr("aws_redshift_subnet_group.foo", "tags.Name", "tf-redshift-subnetgroup"),
   109  				),
   110  			},
   111  			{
   112  				Config: testAccRedshiftSubnetGroupConfigWithTagsUpdated,
   113  				Check: resource.ComposeTestCheckFunc(
   114  					testAccCheckRedshiftSubnetGroupExists("aws_redshift_subnet_group.foo", &v),
   115  					resource.TestCheckResourceAttr(
   116  						"aws_redshift_subnet_group.foo", "tags.%", "3"),
   117  					resource.TestCheckResourceAttr("aws_redshift_subnet_group.foo", "tags.environment", "production"),
   118  					resource.TestCheckResourceAttr("aws_redshift_subnet_group.foo", "tags.Name", "tf-redshift-subnetgroup"),
   119  					resource.TestCheckResourceAttr("aws_redshift_subnet_group.foo", "tags.foo", "bar"),
   120  				),
   121  			},
   122  		},
   123  	})
   124  }
   125  
   126  func TestResourceAWSRedshiftSubnetGroupNameValidation(t *testing.T) {
   127  	cases := []struct {
   128  		Value    string
   129  		ErrCount int
   130  	}{
   131  		{
   132  			Value:    "default",
   133  			ErrCount: 1,
   134  		},
   135  		{
   136  			Value:    "testing123%%",
   137  			ErrCount: 1,
   138  		},
   139  		{
   140  			Value:    "TestingSG",
   141  			ErrCount: 1,
   142  		},
   143  		{
   144  			Value:    "testing_123",
   145  			ErrCount: 1,
   146  		},
   147  		{
   148  			Value:    "testing.123",
   149  			ErrCount: 1,
   150  		},
   151  		{
   152  			Value:    randomString(256),
   153  			ErrCount: 1,
   154  		},
   155  	}
   156  
   157  	for _, tc := range cases {
   158  		_, errors := validateRedshiftSubnetGroupName(tc.Value, "aws_redshift_subnet_group_name")
   159  
   160  		if len(errors) != tc.ErrCount {
   161  			t.Fatalf("Expected the Redshift Subnet Group Name to trigger a validation error")
   162  		}
   163  	}
   164  }
   165  
   166  func testAccCheckRedshiftSubnetGroupDestroy(s *terraform.State) error {
   167  	conn := testAccProvider.Meta().(*AWSClient).redshiftconn
   168  
   169  	for _, rs := range s.RootModule().Resources {
   170  		if rs.Type != "aws_redshift_subnet_group" {
   171  			continue
   172  		}
   173  
   174  		resp, err := conn.DescribeClusterSubnetGroups(
   175  			&redshift.DescribeClusterSubnetGroupsInput{
   176  				ClusterSubnetGroupName: aws.String(rs.Primary.ID)})
   177  		if err == nil {
   178  			if len(resp.ClusterSubnetGroups) > 0 {
   179  				return fmt.Errorf("still exist.")
   180  			}
   181  
   182  			return nil
   183  		}
   184  
   185  		redshiftErr, ok := err.(awserr.Error)
   186  		if !ok {
   187  			return err
   188  		}
   189  		if redshiftErr.Code() != "ClusterSubnetGroupNotFoundFault" {
   190  			return err
   191  		}
   192  	}
   193  
   194  	return nil
   195  }
   196  
   197  func testAccCheckRedshiftSubnetGroupExists(n string, v *redshift.ClusterSubnetGroup) resource.TestCheckFunc {
   198  	return func(s *terraform.State) error {
   199  		rs, ok := s.RootModule().Resources[n]
   200  		if !ok {
   201  			return fmt.Errorf("Not found: %s", n)
   202  		}
   203  
   204  		if rs.Primary.ID == "" {
   205  			return fmt.Errorf("No ID is set")
   206  		}
   207  
   208  		conn := testAccProvider.Meta().(*AWSClient).redshiftconn
   209  		resp, err := conn.DescribeClusterSubnetGroups(
   210  			&redshift.DescribeClusterSubnetGroupsInput{ClusterSubnetGroupName: aws.String(rs.Primary.ID)})
   211  		if err != nil {
   212  			return err
   213  		}
   214  		if len(resp.ClusterSubnetGroups) == 0 {
   215  			return fmt.Errorf("ClusterSubnetGroup not found")
   216  		}
   217  
   218  		*v = *resp.ClusterSubnetGroups[0]
   219  
   220  		return nil
   221  	}
   222  }
   223  
   224  const testAccRedshiftSubnetGroupConfig = `
   225  resource "aws_vpc" "foo" {
   226  	cidr_block = "10.1.0.0/16"
   227  }
   228  
   229  resource "aws_subnet" "foo" {
   230  	cidr_block = "10.1.1.0/24"
   231  	availability_zone = "us-west-2a"
   232  	vpc_id = "${aws_vpc.foo.id}"
   233  	tags {
   234  		Name = "tf-dbsubnet-test-1"
   235  	}
   236  }
   237  
   238  resource "aws_subnet" "bar" {
   239  	cidr_block = "10.1.2.0/24"
   240  	availability_zone = "us-west-2b"
   241  	vpc_id = "${aws_vpc.foo.id}"
   242  	tags {
   243  		Name = "tf-dbsubnet-test-2"
   244  	}
   245  }
   246  
   247  resource "aws_redshift_subnet_group" "foo" {
   248  	name = "foo"
   249  	description = "foo description"
   250  	subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"]
   251  }
   252  `
   253  
   254  const testAccRedshiftSubnetGroup_updateDescription = `
   255  resource "aws_vpc" "foo" {
   256  	cidr_block = "10.1.0.0/16"
   257  }
   258  
   259  resource "aws_subnet" "foo" {
   260  	cidr_block = "10.1.1.0/24"
   261  	availability_zone = "us-west-2a"
   262  	vpc_id = "${aws_vpc.foo.id}"
   263  	tags {
   264  		Name = "tf-dbsubnet-test-1"
   265  	}
   266  }
   267  
   268  resource "aws_subnet" "bar" {
   269  	cidr_block = "10.1.2.0/24"
   270  	availability_zone = "us-west-2b"
   271  	vpc_id = "${aws_vpc.foo.id}"
   272  	tags {
   273  		Name = "tf-dbsubnet-test-2"
   274  	}
   275  }
   276  
   277  resource "aws_redshift_subnet_group" "foo" {
   278  	name = "foo"
   279  	description = "foo description updated"
   280  	subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"]
   281  }
   282  `
   283  
   284  const testAccRedshiftSubnetGroupConfigWithTags = `
   285  resource "aws_vpc" "foo" {
   286  	cidr_block = "10.1.0.0/16"
   287  }
   288  
   289  resource "aws_subnet" "foo" {
   290  	cidr_block = "10.1.1.0/24"
   291  	availability_zone = "us-west-2a"
   292  	vpc_id = "${aws_vpc.foo.id}"
   293  	tags {
   294  		Name = "tf-dbsubnet-test-1"
   295  	}
   296  }
   297  
   298  resource "aws_subnet" "bar" {
   299  	cidr_block = "10.1.2.0/24"
   300  	availability_zone = "us-west-2b"
   301  	vpc_id = "${aws_vpc.foo.id}"
   302  	tags {
   303  		Name = "tf-dbsubnet-test-2"
   304  	}
   305  }
   306  
   307  resource "aws_redshift_subnet_group" "foo" {
   308  	name = "foo"
   309  	subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"]
   310  	tags {
   311  		Name = "tf-redshift-subnetgroup"
   312  	}
   313  }
   314  `
   315  
   316  const testAccRedshiftSubnetGroupConfigWithTagsUpdated = `
   317  resource "aws_vpc" "foo" {
   318  	cidr_block = "10.1.0.0/16"
   319  }
   320  
   321  resource "aws_subnet" "foo" {
   322  	cidr_block = "10.1.1.0/24"
   323  	availability_zone = "us-west-2a"
   324  	vpc_id = "${aws_vpc.foo.id}"
   325  	tags {
   326  		Name = "tf-dbsubnet-test-1"
   327  	}
   328  }
   329  
   330  resource "aws_subnet" "bar" {
   331  	cidr_block = "10.1.2.0/24"
   332  	availability_zone = "us-west-2b"
   333  	vpc_id = "${aws_vpc.foo.id}"
   334  	tags {
   335  		Name = "tf-dbsubnet-test-2"
   336  	}
   337  }
   338  
   339  resource "aws_redshift_subnet_group" "foo" {
   340  	name = "foo"
   341  	subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"]
   342  	tags {
   343  		Name = "tf-redshift-subnetgroup"
   344  		environment = "production"
   345  		foo = "bar"
   346  	}
   347  }
   348  `
   349  
   350  const testAccRedshiftSubnetGroupConfig_updateSubnetIds = `
   351  resource "aws_vpc" "foo" {
   352  	cidr_block = "10.1.0.0/16"
   353  }
   354  
   355  resource "aws_subnet" "foo" {
   356  	cidr_block = "10.1.1.0/24"
   357  	availability_zone = "us-west-2a"
   358  	vpc_id = "${aws_vpc.foo.id}"
   359  	tags {
   360  		Name = "tf-dbsubnet-test-1"
   361  	}
   362  }
   363  
   364  resource "aws_subnet" "bar" {
   365  	cidr_block = "10.1.2.0/24"
   366  	availability_zone = "us-west-2b"
   367  	vpc_id = "${aws_vpc.foo.id}"
   368  	tags {
   369  		Name = "tf-dbsubnet-test-2"
   370  	}
   371  }
   372  
   373  resource "aws_subnet" "foobar" {
   374  	cidr_block = "10.1.3.0/24"
   375  	availability_zone = "us-west-2c"
   376  	vpc_id = "${aws_vpc.foo.id}"
   377  	tags {
   378  		Name = "tf-dbsubnet-test-3"
   379  	}
   380  }
   381  
   382  resource "aws_redshift_subnet_group" "foo" {
   383  	name = "foo"
   384  	subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}", "${aws_subnet.foobar.id}"]
   385  }
   386  `