2014-07-22 22:26:48 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2015-02-25 22:29:11 +01:00
|
|
|
"github.com/hashicorp/aws-sdk-go/aws"
|
|
|
|
"github.com/hashicorp/aws-sdk-go/gen/rds"
|
2015-02-23 22:22:52 +01:00
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/hashcode"
|
2014-07-22 22:26:48 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2014-11-21 17:58:34 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2014-07-22 22:26:48 +02:00
|
|
|
)
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsDbInstance() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsDbInstanceCreate,
|
|
|
|
Read: resourceAwsDbInstanceRead,
|
|
|
|
Delete: resourceAwsDbInstanceDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
2015-01-28 17:48:30 +01:00
|
|
|
Optional: true,
|
2014-11-21 17:58:34 +01:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"username": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"password": &schema.Schema{
|
2014-11-24 14:04:48 +01:00
|
|
|
Type: schema.TypeString,
|
2014-11-21 17:58:34 +01:00
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"engine": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"engine_version": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2015-02-24 19:34:08 +01:00
|
|
|
"storage_encrypted": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
"allocated_storage": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2015-01-30 19:01:10 +01:00
|
|
|
"storage_type": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
"identifier": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"instance_class": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2014-11-24 14:04:48 +01:00
|
|
|
|
|
|
|
"availability_zone": &schema.Schema{
|
2014-11-21 17:58:34 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2015-01-28 17:48:30 +01:00
|
|
|
Computed: true,
|
2014-11-21 17:58:34 +01:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2014-11-24 14:04:48 +01:00
|
|
|
"backup_retention_period": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
2014-11-21 17:58:34 +01:00
|
|
|
ForceNew: true,
|
2015-02-25 17:44:10 +01:00
|
|
|
Default: 1,
|
2014-11-21 17:58:34 +01:00
|
|
|
},
|
|
|
|
|
2014-11-24 14:04:48 +01:00
|
|
|
"backup_window": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2015-01-28 17:48:30 +01:00
|
|
|
Computed: true,
|
2014-11-24 14:04:48 +01:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"iops": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"maintenance_window": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2015-01-28 17:48:30 +01:00
|
|
|
Computed: true,
|
2014-11-24 14:04:48 +01:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"multi_az": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
2014-11-21 17:58:34 +01:00
|
|
|
Optional: true,
|
2015-01-28 17:48:30 +01:00
|
|
|
Computed: true,
|
2014-11-21 17:58:34 +01:00
|
|
|
ForceNew: true,
|
2014-11-24 14:04:48 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
"port": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
2015-01-28 17:48:30 +01:00
|
|
|
Computed: true,
|
2014-11-24 14:04:48 +01:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"publicly_accessible": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"vpc_security_group_ids": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
2014-11-21 17:58:34 +01:00
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Set: func(v interface{}) int {
|
|
|
|
return hashcode.String(v.(string))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2014-11-24 14:04:48 +01:00
|
|
|
"security_group_names": &schema.Schema{
|
2014-11-21 17:58:34 +01:00
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Set: func(v interface{}) int {
|
|
|
|
return hashcode.String(v.(string))
|
|
|
|
},
|
|
|
|
},
|
2014-11-24 14:04:48 +01:00
|
|
|
|
|
|
|
"final_snapshot_identifier": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"db_subnet_group_name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"parameter_group_name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2015-01-28 17:48:30 +01:00
|
|
|
Computed: true,
|
2014-11-24 14:04:48 +01:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"address": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"endpoint": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"status": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2014-11-21 17:58:34 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2014-07-22 22:26:48 +02:00
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error {
|
2015-02-27 17:50:21 +01:00
|
|
|
conn := meta.(*AWSClient).rdsconn
|
2015-02-23 22:22:52 +01:00
|
|
|
opts := rds.CreateDBInstanceMessage{
|
|
|
|
AllocatedStorage: aws.Integer(d.Get("allocated_storage").(int)),
|
|
|
|
DBInstanceClass: aws.String(d.Get("instance_class").(string)),
|
|
|
|
DBInstanceIdentifier: aws.String(d.Get("identifier").(string)),
|
|
|
|
DBName: aws.String(d.Get("name").(string)),
|
|
|
|
MasterUsername: aws.String(d.Get("username").(string)),
|
|
|
|
MasterUserPassword: aws.String(d.Get("password").(string)),
|
|
|
|
Engine: aws.String(d.Get("engine").(string)),
|
|
|
|
EngineVersion: aws.String(d.Get("engine_version").(string)),
|
2015-02-24 19:34:08 +01:00
|
|
|
StorageEncrypted: aws.Boolean(d.Get("storage_encrypted").(bool)),
|
2014-07-22 22:26:48 +02:00
|
|
|
}
|
|
|
|
|
2015-01-30 19:01:10 +01:00
|
|
|
if attr, ok := d.GetOk("storage_type"); ok {
|
2015-02-23 22:22:52 +01:00
|
|
|
opts.StorageType = aws.String(attr.(string))
|
2015-01-30 19:01:10 +01:00
|
|
|
}
|
|
|
|
|
2015-02-25 18:05:14 +01:00
|
|
|
attr := d.Get("backup_retention_period")
|
|
|
|
opts.BackupRetentionPeriod = aws.Integer(attr.(int))
|
2014-07-22 22:26:48 +02:00
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
if attr, ok := d.GetOk("iops"); ok {
|
2015-02-23 22:22:52 +01:00
|
|
|
opts.IOPS = aws.Integer(attr.(int))
|
2014-07-22 22:26:48 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
if attr, ok := d.GetOk("port"); ok {
|
2015-02-23 22:22:52 +01:00
|
|
|
opts.Port = aws.Integer(attr.(int))
|
2014-07-22 22:26:48 +02:00
|
|
|
}
|
|
|
|
|
2014-11-24 14:04:48 +01:00
|
|
|
if attr, ok := d.GetOk("multi_az"); ok {
|
2015-02-23 22:22:52 +01:00
|
|
|
opts.MultiAZ = aws.Boolean(attr.(bool))
|
2014-11-24 14:04:48 +01:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
if attr, ok := d.GetOk("availability_zone"); ok {
|
2015-02-23 22:22:52 +01:00
|
|
|
opts.AvailabilityZone = aws.String(attr.(string))
|
2014-07-22 22:26:48 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
if attr, ok := d.GetOk("maintenance_window"); ok {
|
2015-02-23 22:22:52 +01:00
|
|
|
opts.PreferredMaintenanceWindow = aws.String(attr.(string))
|
2014-07-22 22:26:48 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
if attr, ok := d.GetOk("backup_window"); ok {
|
2015-02-23 22:22:52 +01:00
|
|
|
opts.PreferredBackupWindow = aws.String(attr.(string))
|
2014-07-22 22:26:48 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
if attr, ok := d.GetOk("publicly_accessible"); ok {
|
2015-02-23 22:22:52 +01:00
|
|
|
opts.PubliclyAccessible = aws.Boolean(attr.(bool))
|
2014-07-22 22:26:48 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
if attr, ok := d.GetOk("db_subnet_group_name"); ok {
|
2015-02-23 22:22:52 +01:00
|
|
|
opts.DBSubnetGroupName = aws.String(attr.(string))
|
2014-07-22 22:26:48 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
if attr, ok := d.GetOk("parameter_group_name"); ok {
|
2015-02-23 22:22:52 +01:00
|
|
|
opts.DBParameterGroupName = aws.String(attr.(string))
|
2014-07-22 22:26:48 +02:00
|
|
|
}
|
|
|
|
|
2014-11-24 14:04:48 +01:00
|
|
|
if attr := d.Get("vpc_security_group_ids").(*schema.Set); attr.Len() > 0 {
|
|
|
|
var s []string
|
|
|
|
for _, v := range attr.List() {
|
|
|
|
s = append(s, v.(string))
|
|
|
|
}
|
2015-02-23 22:22:52 +01:00
|
|
|
opts.VPCSecurityGroupIDs = s
|
2014-07-22 22:26:48 +02:00
|
|
|
}
|
|
|
|
|
2014-11-24 14:04:48 +01:00
|
|
|
if attr := d.Get("security_group_names").(*schema.Set); attr.Len() > 0 {
|
|
|
|
var s []string
|
|
|
|
for _, v := range attr.List() {
|
|
|
|
s = append(s, v.(string))
|
|
|
|
}
|
2015-02-23 22:22:52 +01:00
|
|
|
opts.DBSecurityGroups = s
|
2014-07-23 04:22:52 +02:00
|
|
|
}
|
|
|
|
|
2014-07-22 22:26:48 +02:00
|
|
|
log.Printf("[DEBUG] DB Instance create configuration: %#v", opts)
|
2014-11-21 17:58:34 +01:00
|
|
|
_, err := conn.CreateDBInstance(&opts)
|
2014-07-22 22:26:48 +02:00
|
|
|
if err != nil {
|
2014-11-21 17:58:34 +01:00
|
|
|
return fmt.Errorf("Error creating DB Instance: %s", err)
|
2014-07-22 22:26:48 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
d.SetId(d.Get("identifier").(string))
|
2014-07-22 22:26:48 +02:00
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
log.Printf("[INFO] DB Instance ID: %s", d.Id())
|
2014-07-22 22:26:48 +02:00
|
|
|
|
|
|
|
log.Println(
|
|
|
|
"[INFO] Waiting for DB Instance to be available")
|
|
|
|
|
|
|
|
stateConf := &resource.StateChangeConf{
|
2014-07-30 17:58:00 +02:00
|
|
|
Pending: []string{"creating", "backing-up", "modifying"},
|
2014-07-23 00:45:17 +02:00
|
|
|
Target: "available",
|
2014-11-24 14:04:48 +01:00
|
|
|
Refresh: resourceAwsDbInstanceStateRefreshFunc(d, meta),
|
2015-02-27 18:38:07 +01:00
|
|
|
Timeout: 40 * time.Minute,
|
2014-07-23 00:45:17 +02:00
|
|
|
MinTimeout: 10 * time.Second,
|
|
|
|
Delay: 30 * time.Second, // Wait 30 secs before starting
|
2014-07-22 22:26:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait, catching any errors
|
|
|
|
_, err = stateConf.WaitForState()
|
|
|
|
if err != nil {
|
2014-11-21 17:58:34 +01:00
|
|
|
return err
|
2014-07-22 22:26:48 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
return resourceAwsDbInstanceRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsDbInstanceRead(d *schema.ResourceData, meta interface{}) error {
|
2014-11-24 14:04:48 +01:00
|
|
|
v, err := resourceAwsBbInstanceRetrieve(d, meta)
|
2014-11-21 17:58:34 +01:00
|
|
|
|
2014-11-24 14:04:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-11-21 17:58:34 +01:00
|
|
|
}
|
2014-11-24 14:04:48 +01:00
|
|
|
if v == nil {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
2014-11-21 17:58:34 +01:00
|
|
|
}
|
2014-11-24 14:04:48 +01:00
|
|
|
|
2015-04-01 14:33:08 +02:00
|
|
|
if v.DBName != nil {
|
|
|
|
d.Set("name", *v.DBName)
|
|
|
|
} else {
|
|
|
|
d.Set("name", "")
|
|
|
|
}
|
2015-02-23 22:22:52 +01:00
|
|
|
d.Set("username", *v.MasterUsername)
|
|
|
|
d.Set("engine", *v.Engine)
|
|
|
|
d.Set("engine_version", *v.EngineVersion)
|
|
|
|
d.Set("allocated_storage", *v.AllocatedStorage)
|
|
|
|
d.Set("storage_type", *v.StorageType)
|
|
|
|
d.Set("instance_class", *v.DBInstanceClass)
|
|
|
|
d.Set("availability_zone", *v.AvailabilityZone)
|
|
|
|
d.Set("backup_retention_period", *v.BackupRetentionPeriod)
|
|
|
|
d.Set("backup_window", *v.PreferredBackupWindow)
|
|
|
|
d.Set("maintenance_window", *v.PreferredMaintenanceWindow)
|
|
|
|
d.Set("multi_az", *v.MultiAZ)
|
|
|
|
d.Set("port", *v.Endpoint.Port)
|
|
|
|
d.Set("db_subnet_group_name", *v.DBSubnetGroup.DBSubnetGroupName)
|
|
|
|
|
|
|
|
if len(v.DBParameterGroups) > 0 {
|
|
|
|
d.Set("parameter_group_name", *v.DBParameterGroups[0].DBParameterGroupName)
|
|
|
|
}
|
|
|
|
|
2015-03-05 19:15:30 +01:00
|
|
|
d.Set("address", *v.Endpoint.Address)
|
2015-02-23 22:22:52 +01:00
|
|
|
d.Set("endpoint", fmt.Sprintf("%s:%d", *v.Endpoint.Address, *v.Endpoint.Port))
|
|
|
|
d.Set("status", *v.DBInstanceStatus)
|
2015-02-24 19:34:08 +01:00
|
|
|
d.Set("storage_encrypted", *v.StorageEncrypted)
|
2014-11-24 14:04:48 +01:00
|
|
|
|
|
|
|
// Create an empty schema.Set to hold all vpc security group ids
|
|
|
|
ids := &schema.Set{
|
|
|
|
F: func(v interface{}) int {
|
|
|
|
return hashcode.String(v.(string))
|
|
|
|
},
|
|
|
|
}
|
2015-02-23 22:22:52 +01:00
|
|
|
for _, v := range v.VPCSecurityGroups {
|
|
|
|
ids.Add(*v.VPCSecurityGroupID)
|
2014-07-22 22:26:48 +02:00
|
|
|
}
|
2014-11-24 14:04:48 +01:00
|
|
|
d.Set("vpc_security_group_ids", ids)
|
2014-07-22 22:26:48 +02:00
|
|
|
|
2014-11-24 14:04:48 +01:00
|
|
|
// Create an empty schema.Set to hold all security group names
|
|
|
|
sgn := &schema.Set{
|
|
|
|
F: func(v interface{}) int {
|
|
|
|
return hashcode.String(v.(string))
|
|
|
|
},
|
|
|
|
}
|
2015-02-23 22:22:52 +01:00
|
|
|
for _, v := range v.DBSecurityGroups {
|
|
|
|
sgn.Add(*v.DBSecurityGroupName)
|
2014-11-24 14:04:48 +01:00
|
|
|
}
|
|
|
|
d.Set("security_group_names", sgn)
|
|
|
|
|
|
|
|
return nil
|
2014-07-22 22:26:48 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsDbInstanceDelete(d *schema.ResourceData, meta interface{}) error {
|
2015-02-27 17:50:21 +01:00
|
|
|
conn := meta.(*AWSClient).rdsconn
|
2014-07-22 22:26:48 +02:00
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
log.Printf("[DEBUG] DB Instance destroy: %v", d.Id())
|
2014-07-22 22:26:48 +02:00
|
|
|
|
2015-02-23 22:22:52 +01:00
|
|
|
opts := rds.DeleteDBInstanceMessage{DBInstanceIdentifier: aws.String(d.Id())}
|
2014-07-22 22:26:48 +02:00
|
|
|
|
2015-01-28 17:48:30 +01:00
|
|
|
finalSnapshot := d.Get("final_snapshot_identifier").(string)
|
|
|
|
if finalSnapshot == "" {
|
2015-02-23 22:22:52 +01:00
|
|
|
opts.SkipFinalSnapshot = aws.Boolean(true)
|
2014-07-31 21:18:33 +02:00
|
|
|
} else {
|
2015-02-23 22:22:52 +01:00
|
|
|
opts.FinalDBSnapshotIdentifier = aws.String(finalSnapshot)
|
2014-07-22 22:26:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] DB Instance destroy configuration: %v", opts)
|
2014-10-11 20:02:11 +02:00
|
|
|
if _, err := conn.DeleteDBInstance(&opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-07-22 22:26:48 +02:00
|
|
|
|
2014-07-31 21:18:33 +02:00
|
|
|
log.Println(
|
|
|
|
"[INFO] Waiting for DB Instance to be destroyed")
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"creating", "backing-up",
|
|
|
|
"modifying", "deleting", "available"},
|
|
|
|
Target: "",
|
2014-11-24 14:04:48 +01:00
|
|
|
Refresh: resourceAwsDbInstanceStateRefreshFunc(d, meta),
|
2015-02-27 18:38:07 +01:00
|
|
|
Timeout: 40 * time.Minute,
|
2014-07-31 21:18:33 +02:00
|
|
|
MinTimeout: 10 * time.Second,
|
|
|
|
Delay: 30 * time.Second, // Wait 30 secs before starting
|
|
|
|
}
|
2014-10-11 20:02:11 +02:00
|
|
|
if _, err := stateConf.WaitForState(); err != nil {
|
2014-07-22 22:26:48 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-24 14:04:48 +01:00
|
|
|
func resourceAwsBbInstanceRetrieve(
|
|
|
|
d *schema.ResourceData, meta interface{}) (*rds.DBInstance, error) {
|
2015-02-27 17:50:21 +01:00
|
|
|
conn := meta.(*AWSClient).rdsconn
|
2014-07-22 22:26:48 +02:00
|
|
|
|
2015-02-23 22:22:52 +01:00
|
|
|
opts := rds.DescribeDBInstancesMessage{
|
|
|
|
DBInstanceIdentifier: aws.String(d.Id()),
|
2014-07-22 22:26:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] DB Instance describe configuration: %#v", opts)
|
|
|
|
|
|
|
|
resp, err := conn.DescribeDBInstances(&opts)
|
|
|
|
|
|
|
|
if err != nil {
|
2015-02-23 22:22:52 +01:00
|
|
|
dbinstanceerr, ok := err.(aws.APIError)
|
|
|
|
if ok && dbinstanceerr.Code == "DBInstanceNotFound" {
|
2014-09-18 22:25:30 +02:00
|
|
|
return nil, nil
|
|
|
|
}
|
2014-07-22 22:26:48 +02:00
|
|
|
return nil, fmt.Errorf("Error retrieving DB Instances: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.DBInstances) != 1 ||
|
2015-02-23 22:22:52 +01:00
|
|
|
*resp.DBInstances[0].DBInstanceIdentifier != d.Id() {
|
2014-07-22 22:26:48 +02:00
|
|
|
if err != nil {
|
2014-09-18 22:25:30 +02:00
|
|
|
return nil, nil
|
2014-07-22 22:26:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
v := resp.DBInstances[0]
|
|
|
|
|
|
|
|
return &v, nil
|
|
|
|
}
|
|
|
|
|
2014-11-24 14:04:48 +01:00
|
|
|
func resourceAwsDbInstanceStateRefreshFunc(
|
|
|
|
d *schema.ResourceData, meta interface{}) resource.StateRefreshFunc {
|
2014-07-22 22:26:48 +02:00
|
|
|
return func() (interface{}, string, error) {
|
2014-11-24 14:04:48 +01:00
|
|
|
v, err := resourceAwsBbInstanceRetrieve(d, meta)
|
2014-07-22 22:26:48 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error on retrieving DB Instance when waiting: %s", err)
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
2014-09-18 22:25:30 +02:00
|
|
|
if v == nil {
|
|
|
|
return nil, "", nil
|
|
|
|
}
|
|
|
|
|
2015-02-23 22:22:52 +01:00
|
|
|
return v, *v.DBInstanceStatus, nil
|
2014-07-22 22:26:48 +02:00
|
|
|
}
|
|
|
|
}
|