2014-07-23 05:08:39 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2014-12-16 00:40:43 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/hashcode"
|
2014-07-23 05:08:39 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2014-11-21 17:58:34 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2014-07-23 05:08:39 +02:00
|
|
|
"github.com/mitchellh/goamz/route53"
|
|
|
|
)
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsRoute53Record() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsRoute53RecordCreate,
|
|
|
|
Read: resourceAwsRoute53RecordRead,
|
|
|
|
Delete: resourceAwsRoute53RecordDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"type": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"zone_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"ttl": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"records": &schema.Schema{
|
2014-12-16 00:40:43 +01:00
|
|
|
Type: schema.TypeSet,
|
2014-11-21 17:58:34 +01:00
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
2014-12-16 00:40:43 +01:00
|
|
|
Set: func(v interface{}) int {
|
|
|
|
return hashcode.String(v.(string))
|
|
|
|
},
|
2014-11-21 17:58:34 +01:00
|
|
|
},
|
2014-07-23 05:08:39 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsRoute53RecordCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).route53
|
2014-07-23 05:08:39 +02:00
|
|
|
|
|
|
|
// Get the record
|
2014-11-21 17:58:34 +01:00
|
|
|
rec, err := resourceAwsRoute53RecordBuildSet(d)
|
2014-07-23 05:08:39 +02:00
|
|
|
if err != nil {
|
2014-11-21 17:58:34 +01:00
|
|
|
return err
|
2014-07-23 05:08:39 +02:00
|
|
|
}
|
|
|
|
|
2014-08-22 21:51:11 +02:00
|
|
|
// Create the new records. We abuse StateChangeConf for this to
|
|
|
|
// retry for us since Route53 sometimes returns errors about another
|
|
|
|
// operation happening at the same time.
|
2014-07-23 05:08:39 +02:00
|
|
|
req := &route53.ChangeResourceRecordSetsRequest{
|
|
|
|
Comment: "Managed by Terraform",
|
|
|
|
Changes: []route53.Change{
|
|
|
|
route53.Change{
|
|
|
|
Action: "UPSERT",
|
|
|
|
Record: *rec,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2014-11-21 17:58:34 +01:00
|
|
|
zone := d.Get("zone_id").(string)
|
2014-07-23 05:08:39 +02:00
|
|
|
log.Printf("[DEBUG] Creating resource records for zone: %s, name: %s",
|
2014-11-21 17:58:34 +01:00
|
|
|
zone, d.Get("name").(string))
|
|
|
|
|
2014-08-22 21:51:11 +02:00
|
|
|
wait := resource.StateChangeConf{
|
|
|
|
Pending: []string{"rejected"},
|
|
|
|
Target: "accepted",
|
|
|
|
Timeout: 5 * time.Minute,
|
|
|
|
MinTimeout: 1 * time.Second,
|
|
|
|
Refresh: func() (interface{}, string, error) {
|
|
|
|
resp, err := conn.ChangeResourceRecordSets(zone, req)
|
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), "PriorRequestNotComplete") {
|
|
|
|
// There is some pending operation, so just retry
|
|
|
|
// in a bit.
|
|
|
|
return nil, "rejected", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, "failure", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.ChangeInfo, "accepted", nil
|
|
|
|
},
|
|
|
|
}
|
2014-11-21 17:58:34 +01:00
|
|
|
|
2014-08-22 21:51:11 +02:00
|
|
|
respRaw, err := wait.WaitForState()
|
2014-07-23 05:08:39 +02:00
|
|
|
if err != nil {
|
2014-11-21 17:58:34 +01:00
|
|
|
return err
|
2014-07-23 05:08:39 +02:00
|
|
|
}
|
2014-08-22 21:51:11 +02:00
|
|
|
changeInfo := respRaw.(route53.ChangeInfo)
|
2014-07-23 05:08:39 +02:00
|
|
|
|
|
|
|
// Generate an ID
|
2014-11-21 17:58:34 +01:00
|
|
|
d.SetId(fmt.Sprintf("%s_%s_%s", zone, d.Get("name").(string), d.Get("type").(string)))
|
2014-07-23 05:08:39 +02:00
|
|
|
|
|
|
|
// Wait until we are done
|
2014-08-22 21:51:11 +02:00
|
|
|
wait = resource.StateChangeConf{
|
2014-07-23 05:08:39 +02:00
|
|
|
Delay: 30 * time.Second,
|
|
|
|
Pending: []string{"PENDING"},
|
|
|
|
Target: "INSYNC",
|
|
|
|
Timeout: 10 * time.Minute,
|
|
|
|
MinTimeout: 5 * time.Second,
|
|
|
|
Refresh: func() (result interface{}, state string, err error) {
|
2014-11-21 17:58:34 +01:00
|
|
|
return resourceAwsRoute53Wait(conn, changeInfo.ID)
|
2014-07-23 05:08:39 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
_, err = wait.WaitForState()
|
|
|
|
if err != nil {
|
2014-11-21 17:58:34 +01:00
|
|
|
return err
|
2014-07-23 05:08:39 +02:00
|
|
|
}
|
2014-11-21 17:58:34 +01:00
|
|
|
|
|
|
|
return nil
|
2014-07-23 05:08:39 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsRoute53RecordRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).route53
|
|
|
|
|
|
|
|
zone := d.Get("zone_id").(string)
|
|
|
|
lopts := &route53.ListOpts{
|
|
|
|
Name: d.Get("name").(string),
|
|
|
|
Type: d.Get("type").(string),
|
|
|
|
}
|
|
|
|
resp, err := conn.ListResourceRecordSets(zone, lopts)
|
2014-07-23 05:08:39 +02:00
|
|
|
if err != nil {
|
2014-11-21 17:58:34 +01:00
|
|
|
return err
|
2014-07-23 05:08:39 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
// Scan for a matching record
|
|
|
|
found := false
|
|
|
|
for _, record := range resp.Records {
|
|
|
|
if route53.FQDN(record.Name) != route53.FQDN(lopts.Name) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.ToUpper(record.Type) != strings.ToUpper(lopts.Type) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
found = true
|
|
|
|
|
2014-12-16 00:40:43 +01:00
|
|
|
// Create an empty schema.Set to hold all found records
|
|
|
|
records := &schema.Set{
|
|
|
|
F: func(v interface{}) int {
|
|
|
|
return hashcode.String(v.(string))
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rec := range record.Records {
|
|
|
|
records.Add(rec)
|
2014-11-21 17:58:34 +01:00
|
|
|
}
|
2014-12-16 00:40:43 +01:00
|
|
|
|
|
|
|
d.Set("records", records)
|
2014-11-21 17:58:34 +01:00
|
|
|
d.Set("ttl", record.TTL)
|
|
|
|
|
|
|
|
break
|
2014-07-23 05:08:39 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
if !found {
|
|
|
|
d.SetId("")
|
2014-07-23 05:08:39 +02:00
|
|
|
}
|
2014-11-21 17:58:34 +01:00
|
|
|
|
|
|
|
return nil
|
2014-07-23 05:08:39 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsRoute53RecordDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).route53
|
2014-07-23 05:08:39 +02:00
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
// Get the records
|
|
|
|
rec, err := resourceAwsRoute53RecordBuildSet(d)
|
2014-07-23 05:08:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the new records
|
|
|
|
req := &route53.ChangeResourceRecordSetsRequest{
|
|
|
|
Comment: "Deleted by Terraform",
|
|
|
|
Changes: []route53.Change{
|
|
|
|
route53.Change{
|
|
|
|
Action: "DELETE",
|
|
|
|
Record: *rec,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2014-11-21 17:58:34 +01:00
|
|
|
zone := d.Get("zone_id").(string)
|
2014-07-23 05:08:39 +02:00
|
|
|
log.Printf("[DEBUG] Deleting resource records for zone: %s, name: %s",
|
2014-11-21 17:58:34 +01:00
|
|
|
zone, d.Get("name").(string))
|
|
|
|
|
2014-08-31 18:12:05 +02:00
|
|
|
wait := resource.StateChangeConf{
|
|
|
|
Pending: []string{"rejected"},
|
|
|
|
Target: "accepted",
|
|
|
|
Timeout: 5 * time.Minute,
|
|
|
|
MinTimeout: 1 * time.Second,
|
|
|
|
Refresh: func() (interface{}, string, error) {
|
|
|
|
_, err := conn.ChangeResourceRecordSets(zone, req)
|
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), "PriorRequestNotComplete") {
|
|
|
|
// There is some pending operation, so just retry
|
|
|
|
// in a bit.
|
2014-10-08 06:55:40 +02:00
|
|
|
return 42, "rejected", nil
|
2014-08-31 18:12:05 +02:00
|
|
|
}
|
|
|
|
|
2014-10-08 06:55:40 +02:00
|
|
|
if strings.Contains(err.Error(), "InvalidChangeBatch") {
|
|
|
|
// This means that the record is already gone.
|
|
|
|
return 42, "accepted", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return 42, "failure", err
|
2014-08-31 18:12:05 +02:00
|
|
|
}
|
|
|
|
|
2014-10-08 06:55:40 +02:00
|
|
|
return 42, "accepted", nil
|
2014-08-31 18:12:05 +02:00
|
|
|
},
|
|
|
|
}
|
2014-11-21 17:58:34 +01:00
|
|
|
|
2014-08-31 18:12:05 +02:00
|
|
|
if _, err := wait.WaitForState(); err != nil {
|
2014-07-23 05:08:39 +02:00
|
|
|
return err
|
|
|
|
}
|
2014-08-31 18:12:05 +02:00
|
|
|
|
2014-07-23 05:08:39 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsRoute53RecordBuildSet(d *schema.ResourceData) (*route53.ResourceRecordSet, error) {
|
2014-12-16 00:59:47 +01:00
|
|
|
recs := d.Get("records").(*schema.Set).List()
|
|
|
|
records := make([]string, 0, len(recs))
|
|
|
|
|
|
|
|
for _, r := range recs {
|
|
|
|
records = append(records, r.(string))
|
2014-07-23 05:08:39 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
rec := &route53.ResourceRecordSet{
|
|
|
|
Name: d.Get("name").(string),
|
|
|
|
Type: d.Get("type").(string),
|
|
|
|
TTL: d.Get("ttl").(int),
|
|
|
|
Records: records,
|
2014-07-23 05:08:39 +02:00
|
|
|
}
|
2014-11-21 17:58:34 +01:00
|
|
|
return rec, nil
|
2014-07-23 05:08:39 +02:00
|
|
|
}
|