github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_db_subnet_group_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  
    12  	"github.com/aws/aws-sdk-go/aws"
    13  	"github.com/aws/aws-sdk-go/aws/awserr"
    14  	"github.com/aws/aws-sdk-go/service/rds"
    15  )
    16  
    17  func TestAccAWSDBSubnetGroup_basic(t *testing.T) {
    18  	var v rds.DBSubnetGroup
    19  
    20  	testCheck := func(*terraform.State) error {
    21  		return nil
    22  	}
    23  
    24  	rName := fmt.Sprintf("tf-test-%d", acctest.RandInt())
    25  
    26  	resource.Test(t, resource.TestCase{
    27  		PreCheck:     func() { testAccPreCheck(t) },
    28  		Providers:    testAccProviders,
    29  		CheckDestroy: testAccCheckDBSubnetGroupDestroy,
    30  		Steps: []resource.TestStep{
    31  			resource.TestStep{
    32  				Config: testAccDBSubnetGroupConfig(rName),
    33  				Check: resource.ComposeTestCheckFunc(
    34  					testAccCheckDBSubnetGroupExists(
    35  						"aws_db_subnet_group.foo", &v),
    36  					resource.TestCheckResourceAttr(
    37  						"aws_db_subnet_group.foo", "name", rName),
    38  					resource.TestCheckResourceAttr(
    39  						"aws_db_subnet_group.foo", "description", "Managed by Terraform"),
    40  					testCheck,
    41  				),
    42  			},
    43  		},
    44  	})
    45  }
    46  
    47  func TestAccAWSDBSubnetGroup_namePrefix(t *testing.T) {
    48  	var v rds.DBSubnetGroup
    49  
    50  	resource.Test(t, resource.TestCase{
    51  		PreCheck:     func() { testAccPreCheck(t) },
    52  		Providers:    testAccProviders,
    53  		CheckDestroy: testAccCheckDBSubnetGroupDestroy,
    54  		Steps: []resource.TestStep{
    55  			resource.TestStep{
    56  				Config: testAccDBSubnetGroupConfig_namePrefix,
    57  				Check: resource.ComposeTestCheckFunc(
    58  					testAccCheckDBSubnetGroupExists(
    59  						"aws_db_subnet_group.test", &v),
    60  					resource.TestMatchResourceAttr(
    61  						"aws_db_subnet_group.test", "name", regexp.MustCompile("^tf_test-")),
    62  				),
    63  			},
    64  		},
    65  	})
    66  }
    67  
    68  func TestAccAWSDBSubnetGroup_generatedName(t *testing.T) {
    69  	var v rds.DBSubnetGroup
    70  
    71  	resource.Test(t, resource.TestCase{
    72  		PreCheck:     func() { testAccPreCheck(t) },
    73  		Providers:    testAccProviders,
    74  		CheckDestroy: testAccCheckDBSubnetGroupDestroy,
    75  		Steps: []resource.TestStep{
    76  			resource.TestStep{
    77  				Config: testAccDBSubnetGroupConfig_generatedName,
    78  				Check: resource.ComposeTestCheckFunc(
    79  					testAccCheckDBSubnetGroupExists(
    80  						"aws_db_subnet_group.test", &v),
    81  				),
    82  			},
    83  		},
    84  	})
    85  }
    86  
    87  // Regression test for https://github.com/hashicorp/terraform/issues/2603 and
    88  // https://github.com/hashicorp/terraform/issues/2664
    89  func TestAccAWSDBSubnetGroup_withUndocumentedCharacters(t *testing.T) {
    90  	var v rds.DBSubnetGroup
    91  
    92  	testCheck := func(*terraform.State) error {
    93  		return nil
    94  	}
    95  
    96  	resource.Test(t, resource.TestCase{
    97  		PreCheck:     func() { testAccPreCheck(t) },
    98  		Providers:    testAccProviders,
    99  		CheckDestroy: testAccCheckDBSubnetGroupDestroy,
   100  		Steps: []resource.TestStep{
   101  			resource.TestStep{
   102  				Config: testAccDBSubnetGroupConfig_withUnderscoresAndPeriodsAndSpaces,
   103  				Check: resource.ComposeTestCheckFunc(
   104  					testAccCheckDBSubnetGroupExists(
   105  						"aws_db_subnet_group.underscores", &v),
   106  					testAccCheckDBSubnetGroupExists(
   107  						"aws_db_subnet_group.periods", &v),
   108  					testAccCheckDBSubnetGroupExists(
   109  						"aws_db_subnet_group.spaces", &v),
   110  					testCheck,
   111  				),
   112  			},
   113  		},
   114  	})
   115  }
   116  
   117  func TestAccAWSDBSubnetGroup_updateDescription(t *testing.T) {
   118  	var v rds.DBSubnetGroup
   119  
   120  	rName := fmt.Sprintf("tf-test-%d", acctest.RandInt())
   121  	resource.Test(t, resource.TestCase{
   122  		PreCheck:     func() { testAccPreCheck(t) },
   123  		Providers:    testAccProviders,
   124  		CheckDestroy: testAccCheckDBSubnetGroupDestroy,
   125  		Steps: []resource.TestStep{
   126  			resource.TestStep{
   127  				Config: testAccDBSubnetGroupConfig(rName),
   128  				Check: resource.ComposeTestCheckFunc(
   129  					testAccCheckDBSubnetGroupExists(
   130  						"aws_db_subnet_group.foo", &v),
   131  					resource.TestCheckResourceAttr(
   132  						"aws_db_subnet_group.foo", "description", "Managed by Terraform"),
   133  				),
   134  			},
   135  
   136  			resource.TestStep{
   137  				Config: testAccDBSubnetGroupConfig_updatedDescription(rName),
   138  				Check: resource.ComposeTestCheckFunc(
   139  					testAccCheckDBSubnetGroupExists(
   140  						"aws_db_subnet_group.foo", &v),
   141  					resource.TestCheckResourceAttr(
   142  						"aws_db_subnet_group.foo", "description", "foo description updated"),
   143  				),
   144  			},
   145  		},
   146  	})
   147  }
   148  
   149  func testAccCheckDBSubnetGroupDestroy(s *terraform.State) error {
   150  	conn := testAccProvider.Meta().(*AWSClient).rdsconn
   151  
   152  	for _, rs := range s.RootModule().Resources {
   153  		if rs.Type != "aws_db_subnet_group" {
   154  			continue
   155  		}
   156  
   157  		// Try to find the resource
   158  		resp, err := conn.DescribeDBSubnetGroups(
   159  			&rds.DescribeDBSubnetGroupsInput{DBSubnetGroupName: aws.String(rs.Primary.ID)})
   160  		if err == nil {
   161  			if len(resp.DBSubnetGroups) > 0 {
   162  				return fmt.Errorf("still exist.")
   163  			}
   164  
   165  			return nil
   166  		}
   167  
   168  		// Verify the error is what we want
   169  		rdserr, ok := err.(awserr.Error)
   170  		if !ok {
   171  			return err
   172  		}
   173  		if rdserr.Code() != "DBSubnetGroupNotFoundFault" {
   174  			return err
   175  		}
   176  	}
   177  
   178  	return nil
   179  }
   180  
   181  func testAccCheckDBSubnetGroupExists(n string, v *rds.DBSubnetGroup) resource.TestCheckFunc {
   182  	return func(s *terraform.State) error {
   183  		rs, ok := s.RootModule().Resources[n]
   184  		if !ok {
   185  			return fmt.Errorf("Not found: %s", n)
   186  		}
   187  
   188  		if rs.Primary.ID == "" {
   189  			return fmt.Errorf("No ID is set")
   190  		}
   191  
   192  		conn := testAccProvider.Meta().(*AWSClient).rdsconn
   193  		resp, err := conn.DescribeDBSubnetGroups(
   194  			&rds.DescribeDBSubnetGroupsInput{DBSubnetGroupName: aws.String(rs.Primary.ID)})
   195  		if err != nil {
   196  			return err
   197  		}
   198  		if len(resp.DBSubnetGroups) == 0 {
   199  			return fmt.Errorf("DbSubnetGroup not found")
   200  		}
   201  
   202  		*v = *resp.DBSubnetGroups[0]
   203  
   204  		return nil
   205  	}
   206  }
   207  
   208  func testAccDBSubnetGroupConfig(rName string) string {
   209  	return fmt.Sprintf(`
   210  resource "aws_vpc" "foo" {
   211  	cidr_block = "10.1.0.0/16"
   212  }
   213  
   214  resource "aws_subnet" "foo" {
   215  	cidr_block = "10.1.1.0/24"
   216  	availability_zone = "us-west-2a"
   217  	vpc_id = "${aws_vpc.foo.id}"
   218  	tags {
   219  		Name = "tf-dbsubnet-test-1"
   220  	}
   221  }
   222  
   223  resource "aws_subnet" "bar" {
   224  	cidr_block = "10.1.2.0/24"
   225  	availability_zone = "us-west-2b"
   226  	vpc_id = "${aws_vpc.foo.id}"
   227  	tags {
   228  		Name = "tf-dbsubnet-test-2"
   229  	}
   230  }
   231  
   232  resource "aws_db_subnet_group" "foo" {
   233  	name = "%s"
   234  	subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"]
   235  	tags {
   236  		Name = "tf-dbsubnet-group-test"
   237  	}
   238  }`, rName)
   239  }
   240  
   241  func testAccDBSubnetGroupConfig_updatedDescription(rName string) string {
   242  	return fmt.Sprintf(`
   243  resource "aws_vpc" "foo" {
   244  	cidr_block = "10.1.0.0/16"
   245  }
   246  
   247  resource "aws_subnet" "foo" {
   248  	cidr_block = "10.1.1.0/24"
   249  	availability_zone = "us-west-2a"
   250  	vpc_id = "${aws_vpc.foo.id}"
   251  	tags {
   252  		Name = "tf-dbsubnet-test-1"
   253  	}
   254  }
   255  
   256  resource "aws_subnet" "bar" {
   257  	cidr_block = "10.1.2.0/24"
   258  	availability_zone = "us-west-2b"
   259  	vpc_id = "${aws_vpc.foo.id}"
   260  	tags {
   261  		Name = "tf-dbsubnet-test-2"
   262  	}
   263  }
   264  
   265  resource "aws_db_subnet_group" "foo" {
   266  	name = "%s"
   267  	description = "foo description updated"
   268  	subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"]
   269  	tags {
   270  		Name = "tf-dbsubnet-group-test"
   271  	}
   272  }`, rName)
   273  }
   274  
   275  const testAccDBSubnetGroupConfig_namePrefix = `
   276  resource "aws_vpc" "test" {
   277  	cidr_block = "10.1.0.0/16"
   278  }
   279  
   280  resource "aws_subnet" "a" {
   281  	vpc_id = "${aws_vpc.test.id}"
   282  	cidr_block = "10.1.1.0/24"
   283  	availability_zone = "us-west-2a"
   284  }
   285  
   286  resource "aws_subnet" "b" {
   287  	vpc_id = "${aws_vpc.test.id}"
   288  	cidr_block = "10.1.2.0/24"
   289  	availability_zone = "us-west-2b"
   290  }
   291  
   292  resource "aws_db_subnet_group" "test" {
   293  	name_prefix = "tf_test-"
   294  	subnet_ids = ["${aws_subnet.a.id}", "${aws_subnet.b.id}"]
   295  }`
   296  
   297  const testAccDBSubnetGroupConfig_generatedName = `
   298  resource "aws_vpc" "test" {
   299  	cidr_block = "10.1.0.0/16"
   300  }
   301  
   302  resource "aws_subnet" "a" {
   303  	vpc_id = "${aws_vpc.test.id}"
   304  	cidr_block = "10.1.1.0/24"
   305  	availability_zone = "us-west-2a"
   306  }
   307  
   308  resource "aws_subnet" "b" {
   309  	vpc_id = "${aws_vpc.test.id}"
   310  	cidr_block = "10.1.2.0/24"
   311  	availability_zone = "us-west-2b"
   312  }
   313  
   314  resource "aws_db_subnet_group" "test" {
   315  	subnet_ids = ["${aws_subnet.a.id}", "${aws_subnet.b.id}"]
   316  }`
   317  
   318  const testAccDBSubnetGroupConfig_withUnderscoresAndPeriodsAndSpaces = `
   319  resource "aws_vpc" "main" {
   320      cidr_block = "192.168.0.0/16"
   321  }
   322  
   323  resource "aws_subnet" "frontend" {
   324      vpc_id = "${aws_vpc.main.id}"
   325      availability_zone = "us-west-2b"
   326      cidr_block = "192.168.1.0/24"
   327  }
   328  
   329  resource "aws_subnet" "backend" {
   330      vpc_id = "${aws_vpc.main.id}"
   331      availability_zone = "us-west-2c"
   332      cidr_block = "192.168.2.0/24"
   333  }
   334  
   335  resource "aws_db_subnet_group" "underscores" {
   336      name = "with_underscores"
   337      description = "Our main group of subnets"
   338      subnet_ids = ["${aws_subnet.frontend.id}", "${aws_subnet.backend.id}"]
   339  }
   340  
   341  resource "aws_db_subnet_group" "periods" {
   342      name = "with.periods"
   343      description = "Our main group of subnets"
   344      subnet_ids = ["${aws_subnet.frontend.id}", "${aws_subnet.backend.id}"]
   345  }
   346  
   347  resource "aws_db_subnet_group" "spaces" {
   348      name = "with spaces"
   349      description = "Our main group of subnets"
   350      subnet_ids = ["${aws_subnet.frontend.id}", "${aws_subnet.backend.id}"]
   351  }
   352  `