2015-08-04 23:24:55 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2015-10-07 18:27:24 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/rds"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2015-08-04 23:24:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAwsRDSClusterInstance() *schema.Resource {
|
2015-10-07 18:27:24 +02:00
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsRDSClusterInstanceCreate,
|
|
|
|
Read: resourceAwsRDSClusterInstanceRead,
|
|
|
|
Update: resourceAwsRDSClusterInstanceUpdate,
|
|
|
|
Delete: resourceAwsRDSClusterInstanceDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"identifier": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
ValidateFunc: validateRdsId,
|
|
|
|
},
|
|
|
|
|
|
|
|
"db_subnet_group_name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"writer": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"cluster_identifier": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"endpoint": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"port": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"publicly_accessible": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: false,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"instance_class": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"tags": tagsSchema(),
|
|
|
|
},
|
|
|
|
}
|
2015-08-04 23:24:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsRDSClusterInstanceCreate(d *schema.ResourceData, meta interface{}) error {
|
2015-10-07 18:27:24 +02:00
|
|
|
conn := meta.(*AWSClient).rdsconn
|
|
|
|
tags := tagsFromMapRDS(d.Get("tags").(map[string]interface{}))
|
|
|
|
|
|
|
|
createOpts := &rds.CreateDBInstanceInput{
|
|
|
|
DBInstanceClass: aws.String(d.Get("instance_class").(string)),
|
|
|
|
DBClusterIdentifier: aws.String(d.Get("cluster_identifier").(string)),
|
|
|
|
Engine: aws.String("aurora"),
|
|
|
|
PubliclyAccessible: aws.Bool(d.Get("publicly_accessible").(bool)),
|
|
|
|
Tags: tags,
|
|
|
|
}
|
|
|
|
|
|
|
|
if v := d.Get("identifier").(string); v != "" {
|
|
|
|
createOpts.DBInstanceIdentifier = aws.String(v)
|
|
|
|
} else {
|
|
|
|
createOpts.DBInstanceIdentifier = aws.String(resource.UniqueId())
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr, ok := d.GetOk("db_subnet_group_name"); ok {
|
|
|
|
createOpts.DBSubnetGroupName = aws.String(attr.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Creating RDS DB Instance opts: %s", createOpts)
|
|
|
|
resp, err := conn.CreateDBInstance(createOpts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(*resp.DBInstance.DBInstanceIdentifier)
|
|
|
|
|
|
|
|
// reuse db_instance refresh func
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"creating", "backing-up", "modifying"},
|
2016-01-21 02:20:41 +01:00
|
|
|
Target: []string{"available"},
|
2015-10-07 18:27:24 +02:00
|
|
|
Refresh: resourceAwsDbInstanceStateRefreshFunc(d, meta),
|
|
|
|
Timeout: 40 * time.Minute,
|
|
|
|
MinTimeout: 10 * time.Second,
|
|
|
|
Delay: 10 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait, catching any errors
|
|
|
|
_, err = stateConf.WaitForState()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceAwsRDSClusterInstanceRead(d, meta)
|
2015-08-04 23:24:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsRDSClusterInstanceRead(d *schema.ResourceData, meta interface{}) error {
|
2015-10-07 18:27:24 +02:00
|
|
|
db, err := resourceAwsDbInstanceRetrieve(d, meta)
|
2016-03-18 17:12:31 +01:00
|
|
|
// Errors from this helper are always reportable
|
2015-10-07 18:27:24 +02:00
|
|
|
if err != nil {
|
2016-03-18 17:12:31 +01:00
|
|
|
return fmt.Errorf("[WARN] Error on retrieving RDS Cluster Instance (%s): %s", d.Id(), err)
|
|
|
|
}
|
|
|
|
// A nil response means "not found"
|
|
|
|
if db == nil {
|
|
|
|
log.Printf("[WARN] RDS Cluster Instance (%s): not found, removing from state.", d.Id())
|
2015-10-07 18:27:24 +02:00
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retreive DB Cluster information, to determine if this Instance is a writer
|
|
|
|
conn := meta.(*AWSClient).rdsconn
|
|
|
|
resp, err := conn.DescribeDBClusters(&rds.DescribeDBClustersInput{
|
|
|
|
DBClusterIdentifier: db.DBClusterIdentifier,
|
|
|
|
})
|
|
|
|
|
|
|
|
var dbc *rds.DBCluster
|
|
|
|
for _, c := range resp.DBClusters {
|
|
|
|
if *c.DBClusterIdentifier == *db.DBClusterIdentifier {
|
|
|
|
dbc = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if dbc == nil {
|
|
|
|
return fmt.Errorf("[WARN] Error finding RDS Cluster (%s) for Cluster Instance (%s): %s",
|
|
|
|
*db.DBClusterIdentifier, *db.DBInstanceIdentifier, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range dbc.DBClusterMembers {
|
|
|
|
if *db.DBInstanceIdentifier == *m.DBInstanceIdentifier {
|
|
|
|
if *m.IsClusterWriter == true {
|
|
|
|
d.Set("writer", true)
|
|
|
|
} else {
|
|
|
|
d.Set("writer", false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if db.Endpoint != nil {
|
|
|
|
d.Set("endpoint", db.Endpoint.Address)
|
|
|
|
d.Set("port", db.Endpoint.Port)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("publicly_accessible", db.PubliclyAccessible)
|
|
|
|
|
|
|
|
// Fetch and save tags
|
2016-02-18 18:33:21 +01:00
|
|
|
arn, err := buildRDSARN(d.Id(), meta)
|
2015-10-07 18:27:24 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("[DEBUG] Error building ARN for RDS Cluster Instance (%s), not setting Tags", *db.DBInstanceIdentifier)
|
|
|
|
} else {
|
|
|
|
if err := saveTagsRDS(conn, d, arn); err != nil {
|
|
|
|
log.Printf("[WARN] Failed to save tags for RDS Cluster Instance (%s): %s", *db.DBClusterIdentifier, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-08-04 23:24:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsRDSClusterInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
|
2015-10-07 18:27:24 +02:00
|
|
|
conn := meta.(*AWSClient).rdsconn
|
2015-08-04 23:24:55 +02:00
|
|
|
|
2016-02-18 18:33:21 +01:00
|
|
|
if arn, err := buildRDSARN(d.Id(), meta); err == nil {
|
2015-10-07 18:27:24 +02:00
|
|
|
if err := setTagsRDS(conn, d, arn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-08-04 23:24:55 +02:00
|
|
|
|
2015-10-07 18:27:24 +02:00
|
|
|
return resourceAwsRDSClusterInstanceRead(d, meta)
|
2015-08-04 23:24:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsRDSClusterInstanceDelete(d *schema.ResourceData, meta interface{}) error {
|
2015-10-07 18:27:24 +02:00
|
|
|
conn := meta.(*AWSClient).rdsconn
|
2015-08-04 23:24:55 +02:00
|
|
|
|
2015-10-07 18:27:24 +02:00
|
|
|
log.Printf("[DEBUG] RDS Cluster Instance destroy: %v", d.Id())
|
2015-08-04 23:24:55 +02:00
|
|
|
|
2015-10-07 18:27:24 +02:00
|
|
|
opts := rds.DeleteDBInstanceInput{DBInstanceIdentifier: aws.String(d.Id())}
|
2015-08-04 23:24:55 +02:00
|
|
|
|
2015-10-07 18:27:24 +02:00
|
|
|
log.Printf("[DEBUG] RDS Cluster Instance destroy configuration: %s", opts)
|
|
|
|
if _, err := conn.DeleteDBInstance(&opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-04 23:24:55 +02:00
|
|
|
|
2015-10-07 18:27:24 +02:00
|
|
|
// re-uses db_instance refresh func
|
|
|
|
log.Println("[INFO] Waiting for RDS Cluster Instance to be destroyed")
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"modifying", "deleting"},
|
2016-01-21 02:20:41 +01:00
|
|
|
Target: []string{},
|
2015-10-07 18:27:24 +02:00
|
|
|
Refresh: resourceAwsDbInstanceStateRefreshFunc(d, meta),
|
|
|
|
Timeout: 40 * time.Minute,
|
|
|
|
MinTimeout: 10 * time.Second,
|
|
|
|
}
|
2015-08-04 23:24:55 +02:00
|
|
|
|
2015-10-07 18:27:24 +02:00
|
|
|
if _, err := stateConf.WaitForState(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-04 23:24:55 +02:00
|
|
|
|
2015-10-07 18:27:24 +02:00
|
|
|
return nil
|
2015-08-04 23:24:55 +02:00
|
|
|
|
|
|
|
}
|