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

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  
    10  	"github.com/aws/aws-sdk-go/aws"
    11  	"github.com/aws/aws-sdk-go/aws/awserr"
    12  	"github.com/aws/aws-sdk-go/service/rds"
    13  )
    14  
    15  func TestAccAWSDBSubnetGroup_basic(t *testing.T) {
    16  	var v rds.DBSubnetGroup
    17  
    18  	testCheck := func(*terraform.State) error {
    19  		return nil
    20  	}
    21  
    22  	resource.Test(t, resource.TestCase{
    23  		PreCheck:     func() { testAccPreCheck(t) },
    24  		Providers:    testAccProviders,
    25  		CheckDestroy: testAccCheckDBSubnetGroupDestroy,
    26  		Steps: []resource.TestStep{
    27  			resource.TestStep{
    28  				Config: testAccDBSubnetGroupConfig,
    29  				Check: resource.ComposeTestCheckFunc(
    30  					testAccCheckDBSubnetGroupExists(
    31  						"aws_db_subnet_group.foo", &v),
    32  					testCheck,
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  // Regression test for https://github.com/hashicorp/terraform/issues/2603 and
    40  // https://github.com/hashicorp/terraform/issues/2664
    41  func TestAccAWSDBSubnetGroup_withUndocumentedCharacters(t *testing.T) {
    42  	var v rds.DBSubnetGroup
    43  
    44  	testCheck := func(*terraform.State) error {
    45  		return nil
    46  	}
    47  
    48  	resource.Test(t, resource.TestCase{
    49  		PreCheck:     func() { testAccPreCheck(t) },
    50  		Providers:    testAccProviders,
    51  		CheckDestroy: testAccCheckDBSubnetGroupDestroy,
    52  		Steps: []resource.TestStep{
    53  			resource.TestStep{
    54  				Config: testAccDBSubnetGroupConfig_withUnderscoresAndPeriodsAndSpaces,
    55  				Check: resource.ComposeTestCheckFunc(
    56  					testAccCheckDBSubnetGroupExists(
    57  						"aws_db_subnet_group.underscores", &v),
    58  					testAccCheckDBSubnetGroupExists(
    59  						"aws_db_subnet_group.periods", &v),
    60  					testAccCheckDBSubnetGroupExists(
    61  						"aws_db_subnet_group.spaces", &v),
    62  					testCheck,
    63  				),
    64  			},
    65  		},
    66  	})
    67  }
    68  
    69  func TestAccAWSDBSubnetGroup_updateDescription(t *testing.T) {
    70  	var v rds.DBSubnetGroup
    71  
    72  	resource.Test(t, resource.TestCase{
    73  		PreCheck:     func() { testAccPreCheck(t) },
    74  		Providers:    testAccProviders,
    75  		CheckDestroy: testAccCheckDBSubnetGroupDestroy,
    76  		Steps: []resource.TestStep{
    77  			resource.TestStep{
    78  				Config: testAccDBSubnetGroupConfig,
    79  				Check: resource.ComposeTestCheckFunc(
    80  					testAccCheckDBSubnetGroupExists(
    81  						"aws_db_subnet_group.foo", &v),
    82  					resource.TestCheckResourceAttr(
    83  						"aws_db_subnet_group.foo", "description", "foo description"),
    84  				),
    85  			},
    86  
    87  			resource.TestStep{
    88  				Config: testAccDBSubnetGroupConfig_updatedDescription,
    89  				Check: resource.ComposeTestCheckFunc(
    90  					testAccCheckDBSubnetGroupExists(
    91  						"aws_db_subnet_group.foo", &v),
    92  					resource.TestCheckResourceAttr(
    93  						"aws_db_subnet_group.foo", "description", "foo description updated"),
    94  				),
    95  			},
    96  		},
    97  	})
    98  }
    99  
   100  func TestResourceAWSDBSubnetGroupNameValidation(t *testing.T) {
   101  	cases := []struct {
   102  		Value    string
   103  		ErrCount int
   104  	}{
   105  		{
   106  			Value:    "tEsting",
   107  			ErrCount: 1,
   108  		},
   109  		{
   110  			Value:    "testing?",
   111  			ErrCount: 1,
   112  		},
   113  		{
   114  			Value:    "default",
   115  			ErrCount: 1,
   116  		},
   117  		{
   118  			Value:    randomString(300),
   119  			ErrCount: 1,
   120  		},
   121  	}
   122  
   123  	for _, tc := range cases {
   124  		_, errors := validateSubnetGroupName(tc.Value, "aws_db_subnet_group")
   125  
   126  		if len(errors) != tc.ErrCount {
   127  			t.Fatalf("Expected the DB Subnet Group name to trigger a validation error")
   128  		}
   129  	}
   130  }
   131  
   132  func testAccCheckDBSubnetGroupDestroy(s *terraform.State) error {
   133  	conn := testAccProvider.Meta().(*AWSClient).rdsconn
   134  
   135  	for _, rs := range s.RootModule().Resources {
   136  		if rs.Type != "aws_db_subnet_group" {
   137  			continue
   138  		}
   139  
   140  		// Try to find the resource
   141  		resp, err := conn.DescribeDBSubnetGroups(
   142  			&rds.DescribeDBSubnetGroupsInput{DBSubnetGroupName: aws.String(rs.Primary.ID)})
   143  		if err == nil {
   144  			if len(resp.DBSubnetGroups) > 0 {
   145  				return fmt.Errorf("still exist.")
   146  			}
   147  
   148  			return nil
   149  		}
   150  
   151  		// Verify the error is what we want
   152  		rdserr, ok := err.(awserr.Error)
   153  		if !ok {
   154  			return err
   155  		}
   156  		if rdserr.Code() != "DBSubnetGroupNotFoundFault" {
   157  			return err
   158  		}
   159  	}
   160  
   161  	return nil
   162  }
   163  
   164  func testAccCheckDBSubnetGroupExists(n string, v *rds.DBSubnetGroup) resource.TestCheckFunc {
   165  	return func(s *terraform.State) error {
   166  		rs, ok := s.RootModule().Resources[n]
   167  		if !ok {
   168  			return fmt.Errorf("Not found: %s", n)
   169  		}
   170  
   171  		if rs.Primary.ID == "" {
   172  			return fmt.Errorf("No ID is set")
   173  		}
   174  
   175  		conn := testAccProvider.Meta().(*AWSClient).rdsconn
   176  		resp, err := conn.DescribeDBSubnetGroups(
   177  			&rds.DescribeDBSubnetGroupsInput{DBSubnetGroupName: aws.String(rs.Primary.ID)})
   178  		if err != nil {
   179  			return err
   180  		}
   181  		if len(resp.DBSubnetGroups) == 0 {
   182  			return fmt.Errorf("DbSubnetGroup not found")
   183  		}
   184  
   185  		*v = *resp.DBSubnetGroups[0]
   186  
   187  		return nil
   188  	}
   189  }
   190  
   191  const testAccDBSubnetGroupConfig = `
   192  resource "aws_vpc" "foo" {
   193  	cidr_block = "10.1.0.0/16"
   194  }
   195  
   196  resource "aws_subnet" "foo" {
   197  	cidr_block = "10.1.1.0/24"
   198  	availability_zone = "us-west-2a"
   199  	vpc_id = "${aws_vpc.foo.id}"
   200  	tags {
   201  		Name = "tf-dbsubnet-test-1"
   202  	}
   203  }
   204  
   205  resource "aws_subnet" "bar" {
   206  	cidr_block = "10.1.2.0/24"
   207  	availability_zone = "us-west-2b"
   208  	vpc_id = "${aws_vpc.foo.id}"
   209  	tags {
   210  		Name = "tf-dbsubnet-test-2"
   211  	}
   212  }
   213  
   214  resource "aws_db_subnet_group" "foo" {
   215  	name = "foo"
   216  	description = "foo description"
   217  	subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"]
   218  	tags {
   219  		Name = "tf-dbsubnet-group-test"
   220  	}
   221  }
   222  `
   223  
   224  const testAccDBSubnetGroupConfig_updatedDescription = `
   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_db_subnet_group" "foo" {
   248  	name = "foo"
   249  	description = "foo description updated"
   250  	subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"]
   251  	tags {
   252  		Name = "tf-dbsubnet-group-test"
   253  	}
   254  }
   255  `
   256  
   257  const testAccDBSubnetGroupConfig_withUnderscoresAndPeriodsAndSpaces = `
   258  resource "aws_vpc" "main" {
   259      cidr_block = "192.168.0.0/16"
   260  }
   261  
   262  resource "aws_subnet" "frontend" {
   263      vpc_id = "${aws_vpc.main.id}"
   264      availability_zone = "us-west-2b"
   265      cidr_block = "192.168.1.0/24"
   266  }
   267  
   268  resource "aws_subnet" "backend" {
   269      vpc_id = "${aws_vpc.main.id}"
   270      availability_zone = "us-west-2c"
   271      cidr_block = "192.168.2.0/24"
   272  }
   273  
   274  resource "aws_db_subnet_group" "underscores" {
   275      name = "with_underscores"
   276      description = "Our main group of subnets"
   277      subnet_ids = ["${aws_subnet.frontend.id}", "${aws_subnet.backend.id}"]
   278  }
   279  
   280  resource "aws_db_subnet_group" "periods" {
   281      name = "with.periods"
   282      description = "Our main group of subnets"
   283      subnet_ids = ["${aws_subnet.frontend.id}", "${aws_subnet.backend.id}"]
   284  }
   285  
   286  resource "aws_db_subnet_group" "spaces" {
   287      name = "with spaces"
   288      description = "Our main group of subnets"
   289      subnet_ids = ["${aws_subnet.frontend.id}", "${aws_subnet.backend.id}"]
   290  }
   291  `