2014-07-22 22:26:48 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strconv"
|
2014-07-31 21:18:33 +02:00
|
|
|
"strings"
|
2014-07-22 22:26:48 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/flatmap"
|
|
|
|
"github.com/hashicorp/terraform/helper/config"
|
|
|
|
"github.com/hashicorp/terraform/helper/diff"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/goamz/rds"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resource_aws_db_instance_create(
|
2014-09-17 02:44:42 +02:00
|
|
|
s *terraform.InstanceState,
|
2014-09-18 01:33:24 +02:00
|
|
|
d *terraform.InstanceDiff,
|
2014-09-17 02:44:42 +02:00
|
|
|
meta interface{}) (*terraform.InstanceState, error) {
|
2014-07-22 22:26:48 +02:00
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
conn := p.rdsconn
|
|
|
|
|
|
|
|
// Merge the diff into the state so that we have all the attributes
|
|
|
|
// properly.
|
|
|
|
rs := s.MergeDiff(d)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var attr string
|
|
|
|
|
|
|
|
opts := rds.CreateDBInstance{}
|
|
|
|
|
|
|
|
if attr = rs.Attributes["allocated_storage"]; attr != "" {
|
|
|
|
opts.AllocatedStorage, err = strconv.Atoi(attr)
|
|
|
|
opts.SetAllocatedStorage = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr = rs.Attributes["backup_retention_period"]; attr != "" {
|
|
|
|
opts.BackupRetentionPeriod, err = strconv.Atoi(attr)
|
|
|
|
opts.SetBackupRetentionPeriod = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr = rs.Attributes["iops"]; attr != "" {
|
|
|
|
opts.Iops, err = strconv.Atoi(attr)
|
|
|
|
opts.SetIops = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr = rs.Attributes["port"]; attr != "" {
|
|
|
|
opts.Port, err = strconv.Atoi(attr)
|
|
|
|
opts.SetPort = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr = rs.Attributes["availability_zone"]; attr != "" {
|
|
|
|
opts.AvailabilityZone = attr
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr = rs.Attributes["instance_class"]; attr != "" {
|
|
|
|
opts.DBInstanceClass = attr
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr = rs.Attributes["maintenance_window"]; attr != "" {
|
|
|
|
opts.PreferredMaintenanceWindow = attr
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr = rs.Attributes["backup_window"]; attr != "" {
|
|
|
|
opts.PreferredBackupWindow = attr
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr = rs.Attributes["multi_az"]; attr == "true" {
|
|
|
|
opts.MultiAZ = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr = rs.Attributes["publicly_accessible"]; attr == "true" {
|
|
|
|
opts.PubliclyAccessible = true
|
|
|
|
}
|
|
|
|
|
2014-09-18 22:19:24 +02:00
|
|
|
if attr = rs.Attributes["db_subnet_group_name"]; attr != "" {
|
2014-09-12 17:59:31 +02:00
|
|
|
opts.DBSubnetGroupName = attr
|
|
|
|
}
|
|
|
|
|
2014-10-22 23:22:30 +02:00
|
|
|
if attr = rs.Attributes["parameter_group_name"]; attr != "" {
|
|
|
|
opts.DBParameterGroupName = attr
|
|
|
|
}
|
|
|
|
|
2014-07-22 22:26:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error parsing configuration: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := rs.Attributes["vpc_security_group_ids.#"]; ok {
|
|
|
|
opts.VpcSecurityGroupIds = expandStringList(flatmap.Expand(
|
|
|
|
rs.Attributes, "vpc_security_group_ids").([]interface{}))
|
|
|
|
}
|
|
|
|
|
2014-07-23 04:22:52 +02:00
|
|
|
if _, ok := rs.Attributes["security_group_names.#"]; ok {
|
|
|
|
opts.DBSecurityGroupNames = expandStringList(flatmap.Expand(
|
|
|
|
rs.Attributes, "security_group_names").([]interface{}))
|
|
|
|
}
|
|
|
|
|
2014-07-22 22:26:48 +02:00
|
|
|
opts.DBInstanceIdentifier = rs.Attributes["identifier"]
|
|
|
|
opts.DBName = rs.Attributes["name"]
|
|
|
|
opts.MasterUsername = rs.Attributes["username"]
|
|
|
|
opts.MasterUserPassword = rs.Attributes["password"]
|
|
|
|
opts.EngineVersion = rs.Attributes["engine_version"]
|
2014-07-30 17:44:02 +02:00
|
|
|
opts.Engine = rs.Attributes["engine"]
|
2014-07-22 22:26:48 +02:00
|
|
|
|
|
|
|
log.Printf("[DEBUG] DB Instance create configuration: %#v", opts)
|
|
|
|
_, err = conn.CreateDBInstance(&opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error creating DB Instance: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rs.ID = rs.Attributes["identifier"]
|
|
|
|
|
|
|
|
log.Printf("[INFO] DB Instance ID: %s", rs.ID)
|
|
|
|
|
|
|
|
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",
|
|
|
|
Refresh: DBInstanceStateRefreshFunc(rs.ID, conn),
|
2014-10-23 23:27:38 +02:00
|
|
|
Timeout: 20 * 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 {
|
|
|
|
return rs, err
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := resource_aws_db_instance_retrieve(rs.ID, conn)
|
|
|
|
if err != nil {
|
|
|
|
return rs, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resource_aws_db_instance_update_state(rs, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_db_instance_update(
|
2014-09-17 02:44:42 +02:00
|
|
|
s *terraform.InstanceState,
|
2014-09-18 01:33:24 +02:00
|
|
|
d *terraform.InstanceDiff,
|
2014-09-17 02:44:42 +02:00
|
|
|
meta interface{}) (*terraform.InstanceState, error) {
|
2014-07-22 22:26:48 +02:00
|
|
|
panic("Cannot update DB")
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_db_instance_destroy(
|
2014-09-17 02:44:42 +02:00
|
|
|
s *terraform.InstanceState,
|
2014-07-22 22:26:48 +02:00
|
|
|
meta interface{}) error {
|
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
conn := p.rdsconn
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] DB Instance destroy: %v", s.ID)
|
|
|
|
|
|
|
|
opts := rds.DeleteDBInstance{DBInstanceIdentifier: s.ID}
|
|
|
|
|
|
|
|
if s.Attributes["skip_final_snapshot"] == "true" {
|
|
|
|
opts.SkipFinalSnapshot = true
|
2014-07-31 21:18:33 +02:00
|
|
|
} else {
|
|
|
|
opts.FinalDBSnapshotIdentifier = s.Attributes["final_snapshot_identifier"]
|
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: "",
|
|
|
|
Refresh: DBInstanceStateRefreshFunc(s.ID, conn),
|
2014-10-23 23:27:38 +02:00
|
|
|
Timeout: 20 * 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
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_db_instance_refresh(
|
2014-09-17 02:44:42 +02:00
|
|
|
s *terraform.InstanceState,
|
|
|
|
meta interface{}) (*terraform.InstanceState, error) {
|
2014-07-22 22:26:48 +02:00
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
conn := p.rdsconn
|
|
|
|
|
|
|
|
v, err := resource_aws_db_instance_retrieve(s.ID, conn)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
2014-09-18 22:25:30 +02:00
|
|
|
if v == nil {
|
|
|
|
s.ID = ""
|
|
|
|
return s, nil
|
|
|
|
}
|
2014-07-22 22:26:48 +02:00
|
|
|
|
|
|
|
return resource_aws_db_instance_update_state(s, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_db_instance_diff(
|
2014-09-17 02:44:42 +02:00
|
|
|
s *terraform.InstanceState,
|
2014-07-22 22:26:48 +02:00
|
|
|
c *terraform.ResourceConfig,
|
2014-09-18 01:33:24 +02:00
|
|
|
meta interface{}) (*terraform.InstanceDiff, error) {
|
2014-07-22 22:26:48 +02:00
|
|
|
|
|
|
|
b := &diff.ResourceBuilder{
|
|
|
|
Attrs: map[string]diff.AttrType{
|
2014-07-31 21:18:33 +02:00
|
|
|
"allocated_storage": diff.AttrTypeCreate,
|
|
|
|
"availability_zone": diff.AttrTypeCreate,
|
|
|
|
"backup_retention_period": diff.AttrTypeCreate,
|
|
|
|
"backup_window": diff.AttrTypeCreate,
|
|
|
|
"engine": diff.AttrTypeCreate,
|
|
|
|
"engine_version": diff.AttrTypeCreate,
|
|
|
|
"identifier": diff.AttrTypeCreate,
|
|
|
|
"instance_class": diff.AttrTypeCreate,
|
|
|
|
"iops": diff.AttrTypeCreate,
|
|
|
|
"maintenance_window": diff.AttrTypeCreate,
|
|
|
|
"multi_az": diff.AttrTypeCreate,
|
|
|
|
"name": diff.AttrTypeCreate,
|
2014-09-18 22:21:08 +02:00
|
|
|
"password": diff.AttrTypeCreate,
|
2014-07-31 21:18:33 +02:00
|
|
|
"port": diff.AttrTypeCreate,
|
|
|
|
"publicly_accessible": diff.AttrTypeCreate,
|
|
|
|
"username": diff.AttrTypeCreate,
|
|
|
|
"vpc_security_group_ids": diff.AttrTypeCreate,
|
|
|
|
"security_group_names": diff.AttrTypeCreate,
|
2014-09-18 22:19:24 +02:00
|
|
|
"db_subnet_group_name": diff.AttrTypeCreate,
|
2014-10-22 23:22:30 +02:00
|
|
|
"parameter_group_name": diff.AttrTypeCreate,
|
2014-07-31 21:18:33 +02:00
|
|
|
"skip_final_snapshot": diff.AttrTypeUpdate,
|
|
|
|
"final_snapshot_identifier": diff.AttrTypeUpdate,
|
2014-07-22 22:26:48 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
ComputedAttrs: []string{
|
|
|
|
"address",
|
|
|
|
"availability_zone",
|
|
|
|
"backup_retention_period",
|
|
|
|
"backup_window",
|
|
|
|
"engine_version",
|
|
|
|
"maintenance_window",
|
|
|
|
"endpoint",
|
|
|
|
"status",
|
|
|
|
"multi_az",
|
|
|
|
"port",
|
|
|
|
"address",
|
2014-07-31 15:39:19 +02:00
|
|
|
"password",
|
2014-07-22 22:26:48 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-07-31 21:18:33 +02:00
|
|
|
return b.Diff(s, c)
|
2014-07-22 22:26:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_db_instance_update_state(
|
2014-09-17 02:44:42 +02:00
|
|
|
s *terraform.InstanceState,
|
|
|
|
v *rds.DBInstance) (*terraform.InstanceState, error) {
|
2014-07-22 22:26:48 +02:00
|
|
|
|
|
|
|
s.Attributes["address"] = v.Address
|
|
|
|
s.Attributes["allocated_storage"] = strconv.Itoa(v.AllocatedStorage)
|
|
|
|
s.Attributes["availability_zone"] = v.AvailabilityZone
|
|
|
|
s.Attributes["backup_retention_period"] = strconv.Itoa(v.BackupRetentionPeriod)
|
|
|
|
s.Attributes["backup_window"] = v.PreferredBackupWindow
|
2014-08-01 23:13:26 +02:00
|
|
|
s.Attributes["endpoint"] = fmt.Sprintf("%s:%s", s.Attributes["address"], strconv.Itoa(v.Port))
|
2014-07-22 22:26:48 +02:00
|
|
|
s.Attributes["engine"] = v.Engine
|
|
|
|
s.Attributes["engine_version"] = v.EngineVersion
|
|
|
|
s.Attributes["instance_class"] = v.DBInstanceClass
|
|
|
|
s.Attributes["maintenance_window"] = v.PreferredMaintenanceWindow
|
|
|
|
s.Attributes["multi_az"] = strconv.FormatBool(v.MultiAZ)
|
|
|
|
s.Attributes["name"] = v.DBName
|
|
|
|
s.Attributes["port"] = strconv.Itoa(v.Port)
|
|
|
|
s.Attributes["status"] = v.DBInstanceStatus
|
|
|
|
s.Attributes["username"] = v.MasterUsername
|
2014-09-18 22:19:24 +02:00
|
|
|
s.Attributes["db_subnet_group_name"] = v.DBSubnetGroup.Name
|
2014-10-22 23:22:30 +02:00
|
|
|
s.Attributes["parameter_group_name"] = v.DBParameterGroupName
|
2014-07-22 22:26:48 +02:00
|
|
|
|
|
|
|
// Flatten our group values
|
|
|
|
toFlatten := make(map[string]interface{})
|
|
|
|
|
2014-07-23 04:22:52 +02:00
|
|
|
if len(v.DBSecurityGroupNames) > 0 && v.DBSecurityGroupNames[0] != "" {
|
|
|
|
toFlatten["security_group_names"] = v.DBSecurityGroupNames
|
|
|
|
}
|
2014-07-22 22:26:48 +02:00
|
|
|
if len(v.VpcSecurityGroupIds) > 0 && v.VpcSecurityGroupIds[0] != "" {
|
|
|
|
toFlatten["vpc_security_group_ids"] = v.VpcSecurityGroupIds
|
|
|
|
}
|
|
|
|
for k, v := range flatmap.Flatten(toFlatten) {
|
|
|
|
s.Attributes[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_db_instance_retrieve(id string, conn *rds.Rds) (*rds.DBInstance, error) {
|
|
|
|
opts := rds.DescribeDBInstances{
|
|
|
|
DBInstanceIdentifier: id,
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] DB Instance describe configuration: %#v", opts)
|
|
|
|
|
|
|
|
resp, err := conn.DescribeDBInstances(&opts)
|
|
|
|
|
|
|
|
if err != nil {
|
2014-09-18 22:25:30 +02:00
|
|
|
if strings.Contains(err.Error(), "DBInstanceNotFound") {
|
|
|
|
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 ||
|
|
|
|
resp.DBInstances[0].DBInstanceIdentifier != id {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_db_instance_validation() *config.Validator {
|
|
|
|
return &config.Validator{
|
|
|
|
Required: []string{
|
|
|
|
"allocated_storage",
|
|
|
|
"engine",
|
|
|
|
"engine_version",
|
|
|
|
"identifier",
|
|
|
|
"instance_class",
|
|
|
|
"name",
|
|
|
|
"password",
|
|
|
|
"username",
|
|
|
|
},
|
|
|
|
Optional: []string{
|
|
|
|
"availability_zone",
|
|
|
|
"backup_retention_period",
|
|
|
|
"backup_window",
|
|
|
|
"iops",
|
|
|
|
"maintenance_window",
|
|
|
|
"multi_az",
|
|
|
|
"port",
|
|
|
|
"publicly_accessible",
|
2014-07-23 04:22:52 +02:00
|
|
|
"vpc_security_group_ids.*",
|
2014-07-22 22:26:48 +02:00
|
|
|
"skip_final_snapshot",
|
2014-07-23 04:22:52 +02:00
|
|
|
"security_group_names.*",
|
2014-09-18 22:19:24 +02:00
|
|
|
"db_subnet_group_name",
|
2014-10-22 23:22:30 +02:00
|
|
|
"parameter_group_name",
|
2014-10-23 23:07:31 +02:00
|
|
|
"skip_final_snapshot",
|
|
|
|
"final_snapshot_identifier",
|
2014-07-22 22:26:48 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func DBInstanceStateRefreshFunc(id string, conn *rds.Rds) resource.StateRefreshFunc {
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
v, err := resource_aws_db_instance_retrieve(id, conn)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-07-22 22:26:48 +02:00
|
|
|
return v, v.DBInstanceStatus, nil
|
|
|
|
}
|
|
|
|
}
|