github.com/jsoriano/terraform@v0.6.7-0.20151026070445-8b70867fdd95/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_withUnderscoresAndPeriods,
    55  				Check: resource.ComposeTestCheckFunc(
    56  					testAccCheckDBSubnetGroupExists(
    57  						"aws_db_subnet_group.underscores", &v),
    58  					testAccCheckDBSubnetGroupExists(
    59  						"aws_db_subnet_group.periods", &v),
    60  					testCheck,
    61  				),
    62  			},
    63  		},
    64  	})
    65  }
    66  
    67  func testAccCheckDBSubnetGroupDestroy(s *terraform.State) error {
    68  	conn := testAccProvider.Meta().(*AWSClient).rdsconn
    69  
    70  	for _, rs := range s.RootModule().Resources {
    71  		if rs.Type != "aws_db_subnet_group" {
    72  			continue
    73  		}
    74  
    75  		// Try to find the resource
    76  		resp, err := conn.DescribeDBSubnetGroups(
    77  			&rds.DescribeDBSubnetGroupsInput{DBSubnetGroupName: aws.String(rs.Primary.ID)})
    78  		if err == nil {
    79  			if len(resp.DBSubnetGroups) > 0 {
    80  				return fmt.Errorf("still exist.")
    81  			}
    82  
    83  			return nil
    84  		}
    85  
    86  		// Verify the error is what we want
    87  		rdserr, ok := err.(awserr.Error)
    88  		if !ok {
    89  			return err
    90  		}
    91  		if rdserr.Code() != "DBSubnetGroupNotFoundFault" {
    92  			return err
    93  		}
    94  	}
    95  
    96  	return nil
    97  }
    98  
    99  func testAccCheckDBSubnetGroupExists(n string, v *rds.DBSubnetGroup) resource.TestCheckFunc {
   100  	return func(s *terraform.State) error {
   101  		rs, ok := s.RootModule().Resources[n]
   102  		if !ok {
   103  			return fmt.Errorf("Not found: %s", n)
   104  		}
   105  
   106  		if rs.Primary.ID == "" {
   107  			return fmt.Errorf("No ID is set")
   108  		}
   109  
   110  		conn := testAccProvider.Meta().(*AWSClient).rdsconn
   111  		resp, err := conn.DescribeDBSubnetGroups(
   112  			&rds.DescribeDBSubnetGroupsInput{DBSubnetGroupName: aws.String(rs.Primary.ID)})
   113  		if err != nil {
   114  			return err
   115  		}
   116  		if len(resp.DBSubnetGroups) == 0 {
   117  			return fmt.Errorf("DbSubnetGroup not found")
   118  		}
   119  
   120  		*v = *resp.DBSubnetGroups[0]
   121  
   122  		return nil
   123  	}
   124  }
   125  
   126  const testAccDBSubnetGroupConfig = `
   127  resource "aws_vpc" "foo" {
   128  	cidr_block = "10.1.0.0/16"
   129  }
   130  
   131  resource "aws_subnet" "foo" {
   132  	cidr_block = "10.1.1.0/24"
   133  	availability_zone = "us-west-2a"
   134  	vpc_id = "${aws_vpc.foo.id}"
   135  	tags {
   136  		Name = "tf-dbsubnet-test-1"
   137  	}
   138  }
   139  
   140  resource "aws_subnet" "bar" {
   141  	cidr_block = "10.1.2.0/24"
   142  	availability_zone = "us-west-2b"
   143  	vpc_id = "${aws_vpc.foo.id}"
   144  	tags {
   145  		Name = "tf-dbsubnet-test-2"
   146  	}
   147  }
   148  
   149  resource "aws_db_subnet_group" "foo" {
   150  	name = "FOO"
   151  	description = "foo description"
   152  	subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"]
   153  	tags {
   154  		Name = "tf-dbsubnet-group-test"
   155  	}
   156  }
   157  `
   158  
   159  const testAccDBSubnetGroupConfig_withUnderscoresAndPeriods = `
   160  resource "aws_vpc" "main" {
   161      cidr_block = "192.168.0.0/16"
   162  }
   163  
   164  resource "aws_subnet" "frontend" {
   165      vpc_id = "${aws_vpc.main.id}"
   166      availability_zone = "us-west-2b"
   167      cidr_block = "192.168.1.0/24"
   168  }
   169  
   170  resource "aws_subnet" "backend" {
   171      vpc_id = "${aws_vpc.main.id}"
   172      availability_zone = "us-west-2c"
   173      cidr_block = "192.168.2.0/24"
   174  }
   175  
   176  resource "aws_db_subnet_group" "underscores" {
   177      name = "with_underscores"
   178      description = "Our main group of subnets"
   179      subnet_ids = ["${aws_subnet.frontend.id}", "${aws_subnet.backend.id}"]
   180  }
   181  
   182  resource "aws_db_subnet_group" "periods" {
   183      name = "with.periods"
   184      description = "Our main group of subnets"
   185      subnet_ids = ["${aws_subnet.frontend.id}", "${aws_subnet.backend.id}"]
   186  }
   187  `