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 Jan 18, 2016
1 parent 556dd16 commit 9c7d0dc
Show file tree
Hide file tree
Showing 8 changed files with 731 additions and 3 deletions.
1 change: 1 addition & 0 deletions builtin/providers/aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,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
31 changes: 29 additions & 2 deletions builtin/providers/aws/resource_aws_db_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,12 @@ func resourceAwsDbInstance() *schema.Resource {
Optional: true,
},

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

"tags": tagsSchema(),
},
}
Expand Down Expand Up @@ -311,6 +317,10 @@ func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error
opts.DBSubnetGroupName = aws.String(attr.(string))
}

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 @@ -357,6 +367,10 @@ func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error
opts.Port = aws.Int64(int64(attr.(int)))
}

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

if attr, ok := d.GetOk("publicly_accessible"); ok {
opts.PubliclyAccessible = aws.Bool(attr.(bool))
}
Expand All @@ -365,8 +379,8 @@ func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error
opts.TdeCredentialArn = aws.String(attr.(string))
}

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

_, err := conn.RestoreDBInstanceFromDBSnapshot(&opts)
Expand Down Expand Up @@ -494,6 +508,10 @@ func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error
opts.PubliclyAccessible = aws.Bool(attr.(bool))
}

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 = conn.CreateDBInstance(&opts)
Expand Down Expand Up @@ -574,6 +592,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)
}

// list tags for resource
// set tags
Expand Down Expand Up @@ -786,6 +807,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
55 changes: 54 additions & 1 deletion builtin/providers/aws/resource_aws_db_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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 @@ -136,7 +157,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 @@ -332,6 +353,38 @@ resource "aws_db_instance" "bar" {
parameter_group_name = "default.mysql5.6"
}`, rand.New(rand.NewSource(time.Now().UnixNano())).Int())

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"
engine_version = "5.6.21"
instance_class = "db.m1.small"
name = "baz"
password = "barbarbarbar"
username = "foo"
# Maintenance Window is stored in lower case in the API, though not strictly
# documented. Terraform will downcase this to match (as opposed to throw a
# validation error).
maintenance_window = "Fri:09:00-Fri:09:30"
backup_retention_period = 0
parameter_group_name = "default.mysql5.6"
option_group_name = "${aws_db_option_group.bar.option_group_name}"
}`, rand.New(rand.NewSource(time.Now().UnixNano())).Int())

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

0 comments on commit 9c7d0dc

Please sign in to comment.