Skip to content

Commit

Permalink
Scaffold the AWS DB Option Group resource
Browse files Browse the repository at this point in the history
Change the AWS DB Instance to now include the DB Option Group param. Adds a test to prove that it works

Add acceptance tests for the AWS DB Option Group work. This ensures that Options can be added and updated

Documentation for the AWS DB Option resource
  • Loading branch information
stack72 committed Apr 27, 2016
1 parent e0f1283 commit 0844013
Show file tree
Hide file tree
Showing 8 changed files with 764 additions and 2 deletions.
2 changes: 1 addition & 1 deletion builtin/providers/aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func Provider() terraform.ResourceProvider {
"aws_codecommit_repository": resourceAwsCodeCommitRepository(),
"aws_customer_gateway": resourceAwsCustomerGateway(),
"aws_db_instance": resourceAwsDbInstance(),
"aws_db_option_group": resourceAwsDbOptionGroup(),
"aws_db_parameter_group": resourceAwsDbParameterGroup(),
"aws_db_security_group": resourceAwsDbSecurityGroup(),
"aws_db_subnet_group": resourceAwsDbSubnetGroup(),
Expand Down Expand Up @@ -254,7 +255,6 @@ func Provider() terraform.ResourceProvider {
"aws_vpn_connection_route": resourceAwsVpnConnectionRoute(),
"aws_vpn_gateway": resourceAwsVpnGateway(),
},

ConfigureFunc: providerConfigure,
}
}
Expand Down
23 changes: 23 additions & 0 deletions builtin/providers/aws/resource_aws_db_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,12 @@ func resourceAwsDbInstance() *schema.Resource {
Default: 0,
},

"option_group_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},

"tags": tagsSchema(),
},
}
Expand Down Expand Up @@ -345,6 +351,10 @@ func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error
opts.MonitoringInterval = aws.Int64(int64(attr.(int)))
}

if attr, ok := d.GetOk("option_group_name"); ok {
opts.OptionGroupName = aws.String(attr.(string))
}

log.Printf("[DEBUG] DB Instance Replica create configuration: %#v", opts)
_, err := conn.CreateDBInstanceReadReplica(&opts)
if err != nil {
Expand Down Expand Up @@ -546,6 +556,10 @@ func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error
opts.MonitoringInterval = aws.Int64(int64(attr.(int)))
}

if attr, ok := d.GetOk("option_group_name"); ok {
opts.OptionGroupName = aws.String(attr.(string))
}

log.Printf("[DEBUG] DB Instance create configuration: %#v", opts)
var err error
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
Expand Down Expand Up @@ -638,6 +652,9 @@ func resourceAwsDbInstanceRead(d *schema.ResourceData, meta interface{}) error {

d.Set("status", v.DBInstanceStatus)
d.Set("storage_encrypted", v.StorageEncrypted)
if v.OptionGroupMemberships != nil {
d.Set("option_group_name", v.OptionGroupMemberships[0].OptionGroupName)
}

if v.MonitoringInterval != nil {
d.Set("monitoring_interval", v.MonitoringInterval)
Expand Down Expand Up @@ -874,6 +891,12 @@ func resourceAwsDbInstanceUpdate(d *schema.ResourceData, meta interface{}) error
requestUpdate = true
}

if d.HasChange("option_group_name") {
d.SetPartial("option_group_name")
req.OptionGroupName = aws.String(d.Get("option_group_name").(string))
requestUpdate = true
}

log.Printf("[DEBUG] Send DB Instance Modification request: %#v", requestUpdate)
if requestUpdate {
log.Printf("[DEBUG] DB Instance Modification request: %#v", req)
Expand Down
48 changes: 47 additions & 1 deletion builtin/providers/aws/resource_aws_db_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,27 @@ func TestAccAWSDBInstance_basic(t *testing.T) {
})
}

func TestAccAWSDBInstance_optionGroup(t *testing.T) {
var v rds.DBInstance

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSDBInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSDBInstanceConfigWithOptionGroup,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBInstanceExists("aws_db_instance.bar", &v),
testAccCheckAWSDBInstanceAttributes(&v),
resource.TestCheckResourceAttr(
"aws_db_instance.bar", "option_group_name", "option-group-test-terraform"),
),
},
},
})
}

func TestAccAWSDBInstanceReplica(t *testing.T) {
var s, r rds.DBInstance

Expand Down Expand Up @@ -160,7 +181,7 @@ func testAccCheckAWSDBInstanceDestroy(s *terraform.State) error {
if !ok {
return err
}
if newerr.Code() != "InvalidDBInstance.NotFound" {
if newerr.Code() != "DBInstanceNotFound" {
return err
}
}
Expand Down Expand Up @@ -383,6 +404,31 @@ resource "aws_db_instance" "bar" {
parameter_group_name = "default.mysql5.6"
}`

var testAccAWSDBInstanceConfigWithOptionGroup = fmt.Sprintf(`
resource "aws_db_option_group" "bar" {
option_group_name = "option-group-test-terraform"
option_group_description = "Test option group for terraform"
engine_name = "mysql"
major_engine_version = "5.6"
}
resource "aws_db_instance" "bar" {
identifier = "foobarbaz-test-terraform-%d"
allocated_storage = 10
engine = "MySQL"
instance_class = "db.m1.small"
name = "baz"
password = "barbarbarbar"
username = "foo"
backup_retention_period = 0
parameter_group_name = "default.mysql5.6"
option_group_name = "${aws_db_option_group.bar.option_group_name}"
}`, acctest.RandInt())

func testAccReplicaInstanceConfig(val int) string {
return fmt.Sprintf(`
resource "aws_db_instance" "bar" {
Expand Down
Loading

0 comments on commit 0844013

Please sign in to comment.