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"
|
2015-02-12 17:48:48 +01:00
|
|
|
|
2015-02-25 22:29:11 +01:00
|
|
|
"github.com/hashicorp/aws-sdk-go/aws"
|
|
|
|
"github.com/hashicorp/aws-sdk-go/gen/route53"
|
2014-07-23 05:08:39 +02:00
|
|
|
)
|
|
|
|
|
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 {
|
2015-02-12 17:48:48 +01:00
|
|
|
conn := meta.(*AWSClient).r53conn
|
2015-02-12 01:33:21 +01:00
|
|
|
zone := d.Get("zone_id").(string)
|
|
|
|
|
2015-02-12 17:48:48 +01:00
|
|
|
zoneRecord, err := conn.GetHostedZone(&route53.GetHostedZoneRequest{ID: aws.String(zone)})
|
2015-02-12 01:33:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-07-23 05:08:39 +02:00
|
|
|
// Get the record
|
2015-03-23 21:01:53 +01:00
|
|
|
rec, err := resourceAwsRoute53RecordBuildSet(d, *zoneRecord.HostedZone.Name)
|
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.
|
2015-02-12 17:48:48 +01:00
|
|
|
changeBatch := &route53.ChangeBatch{
|
|
|
|
Comment: aws.String("Managed by Terraform"),
|
2014-07-23 05:08:39 +02:00
|
|
|
Changes: []route53.Change{
|
|
|
|
route53.Change{
|
2015-02-12 17:48:48 +01:00
|
|
|
Action: aws.String("UPSERT"),
|
|
|
|
ResourceRecordSet: rec,
|
2014-07-23 05:08:39 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2015-02-12 01:33:21 +01:00
|
|
|
|
2015-02-12 17:48:48 +01:00
|
|
|
req := &route53.ChangeResourceRecordSetsRequest{
|
|
|
|
HostedZoneID: aws.String(cleanZoneID(*zoneRecord.HostedZone.ID)),
|
|
|
|
ChangeBatch: changeBatch,
|
|
|
|
}
|
|
|
|
|
2014-07-23 05:08:39 +02:00
|
|
|
log.Printf("[DEBUG] Creating resource records for zone: %s, name: %s",
|
2015-03-23 21:01:53 +01:00
|
|
|
zone, *rec.Name)
|
2014-11-21 17:58:34 +01:00
|
|
|
|
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) {
|
2015-02-12 17:48:48 +01:00
|
|
|
resp, err := conn.ChangeResourceRecordSets(req)
|
2014-08-22 21:51:11 +02:00
|
|
|
if err != nil {
|
2015-03-23 21:01:53 +01:00
|
|
|
if r53err, ok := err.(aws.APIError); ok {
|
|
|
|
if r53err.Code == "PriorRequestNotComplete" {
|
|
|
|
// There is some pending operation, so just retry
|
|
|
|
// in a bit.
|
|
|
|
return nil, "rejected", nil
|
|
|
|
}
|
2014-08-22 21:51:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, "failure", err
|
|
|
|
}
|
|
|
|
|
2015-02-12 17:48:48 +01:00
|
|
|
return resp, "accepted", nil
|
2014-08-22 21:51:11 +02:00
|
|
|
},
|
|
|
|
}
|
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
|
|
|
}
|
2015-02-12 17:48:48 +01:00
|
|
|
changeInfo := respRaw.(*route53.ChangeResourceRecordSetsResponse).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",
|
2015-03-09 20:06:27 +01:00
|
|
|
Timeout: 30 * time.Minute,
|
2014-07-23 05:08:39 +02:00
|
|
|
MinTimeout: 5 * time.Second,
|
|
|
|
Refresh: func() (result interface{}, state string, err error) {
|
2015-02-12 17:48:48 +01:00
|
|
|
changeRequest := &route53.GetChangeRequest{
|
|
|
|
ID: aws.String(cleanChangeID(*changeInfo.ID)),
|
|
|
|
}
|
|
|
|
return resourceAwsGoRoute53Wait(conn, changeRequest)
|
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 {
|
2015-02-12 17:48:48 +01:00
|
|
|
conn := meta.(*AWSClient).r53conn
|
2014-11-21 17:58:34 +01:00
|
|
|
|
|
|
|
zone := d.Get("zone_id").(string)
|
2015-03-23 21:01:53 +01:00
|
|
|
|
|
|
|
// get expanded name
|
|
|
|
zoneRecord, err := conn.GetHostedZone(&route53.GetHostedZoneRequest{ID: aws.String(zone)})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
en := expandRecordName(d.Get("name").(string), *zoneRecord.HostedZone.Name)
|
|
|
|
|
2015-02-12 17:48:48 +01:00
|
|
|
lopts := &route53.ListResourceRecordSetsRequest{
|
|
|
|
HostedZoneID: aws.String(cleanZoneID(zone)),
|
2015-03-23 21:01:53 +01:00
|
|
|
StartRecordName: aws.String(en),
|
2015-02-12 17:48:48 +01:00
|
|
|
StartRecordType: aws.String(d.Get("type").(string)),
|
2014-11-21 17:58:34 +01:00
|
|
|
}
|
2015-02-12 17:48:48 +01:00
|
|
|
|
|
|
|
resp, err := conn.ListResourceRecordSets(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
|
2015-02-12 17:48:48 +01:00
|
|
|
for _, record := range resp.ResourceRecordSets {
|
2015-03-17 20:57:45 +01:00
|
|
|
name := cleanRecordName(*record.Name)
|
|
|
|
if FQDN(name) != FQDN(*lopts.StartRecordName) {
|
2014-11-21 17:58:34 +01:00
|
|
|
continue
|
|
|
|
}
|
2015-02-12 17:48:48 +01:00
|
|
|
if strings.ToUpper(*record.Type) != strings.ToUpper(*lopts.StartRecordType) {
|
2014-11-21 17:58:34 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
found = true
|
|
|
|
|
2015-04-08 16:06:15 +02:00
|
|
|
err := d.Set("records", flattenResourceRecords(record.ResourceRecords))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[DEBUG] Error setting records for: %s, error: %#v", en, err)
|
|
|
|
}
|
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 {
|
2015-02-12 17:48:48 +01:00
|
|
|
conn := meta.(*AWSClient).r53conn
|
|
|
|
|
|
|
|
zone := d.Get("zone_id").(string)
|
|
|
|
log.Printf("[DEBUG] Deleting resource records for zone: %s, name: %s",
|
|
|
|
zone, d.Get("name").(string))
|
2015-03-23 21:01:53 +01:00
|
|
|
zoneRecord, err := conn.GetHostedZone(&route53.GetHostedZoneRequest{ID: aws.String(zone)})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-11-21 17:58:34 +01:00
|
|
|
// Get the records
|
2015-03-23 21:01:53 +01:00
|
|
|
rec, err := resourceAwsRoute53RecordBuildSet(d, *zoneRecord.HostedZone.Name)
|
2014-07-23 05:08:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the new records
|
2015-02-12 17:48:48 +01:00
|
|
|
changeBatch := &route53.ChangeBatch{
|
|
|
|
Comment: aws.String("Deleted by Terraform"),
|
2014-07-23 05:08:39 +02:00
|
|
|
Changes: []route53.Change{
|
|
|
|
route53.Change{
|
2015-02-12 17:48:48 +01:00
|
|
|
Action: aws.String("DELETE"),
|
|
|
|
ResourceRecordSet: rec,
|
2014-07-23 05:08:39 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2015-02-12 17:48:48 +01:00
|
|
|
|
|
|
|
req := &route53.ChangeResourceRecordSetsRequest{
|
|
|
|
HostedZoneID: aws.String(cleanZoneID(zone)),
|
|
|
|
ChangeBatch: changeBatch,
|
|
|
|
}
|
2014-11-21 17:58:34 +01:00
|
|
|
|
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) {
|
2015-02-12 17:48:48 +01:00
|
|
|
_, err := conn.ChangeResourceRecordSets(req)
|
2014-08-31 18:12:05 +02:00
|
|
|
if err != nil {
|
2015-03-17 20:57:45 +01:00
|
|
|
if r53err, ok := err.(aws.APIError); ok {
|
|
|
|
if r53err.Code == "PriorRequestNotComplete" {
|
|
|
|
// There is some pending operation, so just retry
|
|
|
|
// in a bit.
|
|
|
|
return 42, "rejected", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if r53err.Code == "InvalidChangeBatch" {
|
|
|
|
// This means that the record is already gone.
|
|
|
|
return 42, "accepted", nil
|
|
|
|
}
|
2014-10-08 06:55:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-03-23 21:01:53 +01:00
|
|
|
func resourceAwsRoute53RecordBuildSet(d *schema.ResourceData, zoneName string) (*route53.ResourceRecordSet, error) {
|
2014-12-16 00:59:47 +01:00
|
|
|
recs := d.Get("records").(*schema.Set).List()
|
2015-04-08 20:58:23 +02:00
|
|
|
|
|
|
|
records := expandResourceRecords(recs, d.Get("type").(string))
|
2014-07-23 05:08:39 +02:00
|
|
|
|
2015-03-23 21:01:53 +01:00
|
|
|
// get expanded name
|
|
|
|
en := expandRecordName(d.Get("name").(string), zoneName)
|
|
|
|
|
|
|
|
// Create the RecordSet request with the fully expanded name, e.g.
|
|
|
|
// sub.domain.com. Route 53 requires a fully qualified domain name, but does
|
|
|
|
// not require the trailing ".", which it will itself, so we don't call FQDN
|
|
|
|
// here.
|
2014-11-21 17:58:34 +01:00
|
|
|
rec := &route53.ResourceRecordSet{
|
2015-03-23 21:01:53 +01:00
|
|
|
Name: aws.String(en),
|
2015-02-12 17:48:48 +01:00
|
|
|
Type: aws.String(d.Get("type").(string)),
|
|
|
|
TTL: aws.Long(int64(d.Get("ttl").(int))),
|
|
|
|
ResourceRecords: 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
|
|
|
}
|
2015-02-12 17:48:48 +01:00
|
|
|
|
|
|
|
func FQDN(name string) string {
|
|
|
|
n := len(name)
|
|
|
|
if n == 0 || name[n-1] == '.' {
|
|
|
|
return name
|
|
|
|
} else {
|
|
|
|
return name + "."
|
|
|
|
}
|
|
|
|
}
|
2015-03-17 20:57:45 +01:00
|
|
|
|
|
|
|
// Route 53 stores the "*" wildcard indicator as ASCII 42 and returns the
|
|
|
|
// octal equivalent, "\\052". Here we look for that, and convert back to "*"
|
|
|
|
// as needed.
|
|
|
|
func cleanRecordName(name string) string {
|
|
|
|
str := name
|
|
|
|
if strings.HasPrefix(name, "\\052") {
|
|
|
|
str = strings.Replace(name, "\\052", "*", 1)
|
|
|
|
log.Printf("[DEBUG] Replacing octal \\052 for * in: %s", name)
|
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|
2015-03-23 21:01:53 +01:00
|
|
|
|
|
|
|
// Check if the current record name contains the zone suffix.
|
|
|
|
// If it does not, add the zone name to form a fully qualified name
|
|
|
|
// and keep AWS happy.
|
|
|
|
func expandRecordName(name, zone string) string {
|
|
|
|
rn := strings.TrimSuffix(name, ".")
|
|
|
|
zone = strings.TrimSuffix(zone, ".")
|
|
|
|
if !strings.HasSuffix(rn, zone) {
|
|
|
|
rn = strings.Join([]string{name, zone}, ".")
|
|
|
|
}
|
|
|
|
return rn
|
|
|
|
}
|