provider/aws: Support updating of aws_db_instance db_subnet_group_name (#10818)

Fixes: #10811

```
% make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSDBInstance_subnetGroup'
==> Checking that code complies with gofmt requirements...
go generate $(go list ./... | grep -v /terraform/vendor/)
2016/12/18 12:52:21 Generated command/internal_plugin_list.go
TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSDBInstance_subnetGroup -timeout 120m
=== RUN   TestAccAWSDBInstance_subnetGroup
--- PASS: TestAccAWSDBInstance_subnetGroup (992.05s)
PASS
ok  	github.com/hashicorp/terraform/builtin/providers/aws	992.073s
```
This commit is contained in:
Paul Stack 2016-12-18 14:35:24 +00:00 committed by GitHub
parent b54fd3ab9a
commit 1ff623a790
2 changed files with 163 additions and 2 deletions

View File

@ -216,7 +216,6 @@ func resourceAwsDbInstance() *schema.Resource {
"db_subnet_group_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
@ -936,6 +935,11 @@ func resourceAwsDbInstanceUpdate(d *schema.ResourceData, meta interface{}) error
req.DBPortNumber = aws.Int64(int64(d.Get("port").(int)))
requestUpdate = true
}
if d.HasChange("db_subnet_group_name") {
d.SetPartial("db_subnet_group_name")
req.DBSubnetGroupName = aws.String(d.Get("db_subnet_group_name").(string))
requestUpdate = true
}
log.Printf("[DEBUG] Send DB Instance Modification request: %t", requestUpdate)
if requestUpdate {
@ -949,7 +953,7 @@ func resourceAwsDbInstanceUpdate(d *schema.ResourceData, meta interface{}) error
stateConf := &resource.StateChangeConf{
Pending: []string{"creating", "backing-up", "modifying", "resetting-master-credentials",
"maintenance", "renaming", "rebooting", "upgrading", "configuring-enhanced-monitoring"},
"maintenance", "renaming", "rebooting", "upgrading", "configuring-enhanced-monitoring", "moving-to-vpc"},
Target: []string{"available"},
Refresh: resourceAwsDbInstanceStateRefreshFunc(d, meta),
Timeout: 80 * time.Minute,

View File

@ -78,6 +78,35 @@ func TestAccAWSDBInstance_kmsKey(t *testing.T) {
})
}
func TestAccAWSDBInstance_subnetGroup(t *testing.T) {
var v rds.DBInstance
rName := acctest.RandString(10)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSDBInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSDBInstanceConfigWithSubnetGroup(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBInstanceExists("aws_db_instance.bar", &v),
resource.TestCheckResourceAttr(
"aws_db_instance.bar", "db_subnet_group_name", "foo"),
),
},
{
Config: testAccAWSDBInstanceConfigWithSubnetGroupUpdated(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBInstanceExists("aws_db_instance.bar", &v),
resource.TestCheckResourceAttr(
"aws_db_instance.bar", "db_subnet_group_name", "bar"),
),
},
},
})
}
func TestAccAWSDBInstance_optionGroup(t *testing.T) {
var v rds.DBInstance
@ -766,3 +795,131 @@ resource "aws_db_instance" "bar" {
apply_immediately = true
}`, rName)
}
func testAccAWSDBInstanceConfigWithSubnetGroup(rName string) string {
return fmt.Sprintf(`
resource "aws_vpc" "foo" {
cidr_block = "10.1.0.0/16"
}
resource "aws_subnet" "foo" {
cidr_block = "10.1.1.0/24"
availability_zone = "us-west-2a"
vpc_id = "${aws_vpc.foo.id}"
tags {
Name = "tf-dbsubnet-test-1"
}
}
resource "aws_subnet" "bar" {
cidr_block = "10.1.2.0/24"
availability_zone = "us-west-2b"
vpc_id = "${aws_vpc.foo.id}"
tags {
Name = "tf-dbsubnet-test-2"
}
}
resource "aws_db_subnet_group" "foo" {
name = "foo"
subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"]
tags {
Name = "tf-dbsubnet-group-test"
}
}
resource "aws_db_instance" "bar" {
identifier = "mydb-rds-%s"
engine = "mysql"
engine_version = "5.6.23"
instance_class = "db.t2.micro"
name = "mydb"
username = "foo"
password = "barbarbar"
parameter_group_name = "default.mysql5.6"
db_subnet_group_name = "${aws_db_subnet_group.foo.name}"
port = 3305
allocated_storage = 10
apply_immediately = true
}`, rName)
}
func testAccAWSDBInstanceConfigWithSubnetGroupUpdated(rName string) string {
return fmt.Sprintf(`
resource "aws_vpc" "foo" {
cidr_block = "10.1.0.0/16"
}
resource "aws_vpc" "bar" {
cidr_block = "10.10.0.0/16"
}
resource "aws_subnet" "foo" {
cidr_block = "10.1.1.0/24"
availability_zone = "us-west-2a"
vpc_id = "${aws_vpc.foo.id}"
tags {
Name = "tf-dbsubnet-test-1"
}
}
resource "aws_subnet" "bar" {
cidr_block = "10.1.2.0/24"
availability_zone = "us-west-2b"
vpc_id = "${aws_vpc.foo.id}"
tags {
Name = "tf-dbsubnet-test-2"
}
}
resource "aws_subnet" "test" {
cidr_block = "10.10.3.0/24"
availability_zone = "us-west-2c"
vpc_id = "${aws_vpc.bar.id}"
tags {
Name = "tf-dbsubnet-test-3"
}
}
resource "aws_subnet" "another_test" {
cidr_block = "10.10.4.0/24"
availability_zone = "us-west-2a"
vpc_id = "${aws_vpc.bar.id}"
tags {
Name = "tf-dbsubnet-test-4"
}
}
resource "aws_db_subnet_group" "foo" {
name = "foo"
subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"]
tags {
Name = "tf-dbsubnet-group-test"
}
}
resource "aws_db_subnet_group" "bar" {
name = "bar"
subnet_ids = ["${aws_subnet.test.id}", "${aws_subnet.another_test.id}"]
tags {
Name = "tf-dbsubnet-group-test-updated"
}
}
resource "aws_db_instance" "bar" {
identifier = "mydb-rds-%s"
engine = "mysql"
engine_version = "5.6.23"
instance_class = "db.t2.micro"
name = "mydb"
username = "foo"
password = "barbarbar"
parameter_group_name = "default.mysql5.6"
db_subnet_group_name = "${aws_db_subnet_group.bar.name}"
port = 3305
allocated_storage = 10
apply_immediately = true
}`, rName)
}