Merge pull request #971 from hashicorp/aws-go-route53

provider/aws: Convert Route 53 Zone resource to use awslabs/aws-sdk-go
This commit is contained in:
Clint 2015-02-19 15:20:41 -06:00
commit d99df916bc
5 changed files with 196 additions and 72 deletions

View File

@ -12,8 +12,10 @@ import (
"github.com/mitchellh/goamz/ec2"
"github.com/mitchellh/goamz/elb"
"github.com/mitchellh/goamz/rds"
"github.com/mitchellh/goamz/route53"
"github.com/mitchellh/goamz/s3"
awsGo "github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/gen/route53"
)
type Config struct {
@ -28,7 +30,7 @@ type AWSClient struct {
autoscalingconn *autoscaling.AutoScaling
s3conn *s3.S3
rdsconn *rds.Rds
route53 *route53.Route53
r53conn *route53.Route53
}
// Client configures and returns a fully initailized AWSClient
@ -62,7 +64,12 @@ func (c *Config) Client() (interface{}, error) {
log.Println("[INFO] Initializing RDS connection")
client.rdsconn = rds.New(auth, region)
log.Println("[INFO] Initializing Route53 connection")
client.route53 = route53.New(auth, region)
creds := awsGo.Creds(c.AccessKey, c.SecretKey, "")
// aws-sdk-go uses v4 for signing requests, which requires all global
// endpoints to use 'us-east-1'.
// See http://docs.aws.amazon.com/general/latest/gr/sigv4_changes.html
client.r53conn = route53.New(creds, "us-east-1", nil)
}
if len(errs) > 0 {

View File

@ -9,7 +9,9 @@ import (
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/mitchellh/goamz/route53"
"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/gen/route53"
)
func resourceAwsRoute53Record() *schema.Resource {
@ -57,11 +59,10 @@ func resourceAwsRoute53Record() *schema.Resource {
}
func resourceAwsRoute53RecordCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).route53
conn := meta.(*AWSClient).r53conn
zone := d.Get("zone_id").(string)
zoneRecord, err := conn.GetHostedZone(zone)
zoneRecord, err := conn.GetHostedZone(&route53.GetHostedZoneRequest{ID: aws.String(zone)})
if err != nil {
return err
}
@ -70,7 +71,7 @@ func resourceAwsRoute53RecordCreate(d *schema.ResourceData, meta interface{}) er
// If it does not, add the zone name to form a fully qualified name
// and keep AWS happy.
recordName := d.Get("name").(string)
zoneName := strings.Trim(zoneRecord.HostedZone.Name, ".")
zoneName := strings.Trim(*zoneRecord.HostedZone.Name, ".")
if !strings.HasSuffix(recordName, zoneName) {
d.Set("name", strings.Join([]string{recordName, zoneName}, "."))
}
@ -84,16 +85,21 @@ func resourceAwsRoute53RecordCreate(d *schema.ResourceData, meta interface{}) er
// 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.
req := &route53.ChangeResourceRecordSetsRequest{
Comment: "Managed by Terraform",
changeBatch := &route53.ChangeBatch{
Comment: aws.String("Managed by Terraform"),
Changes: []route53.Change{
route53.Change{
Action: "UPSERT",
Record: *rec,
Action: aws.String("UPSERT"),
ResourceRecordSet: rec,
},
},
}
req := &route53.ChangeResourceRecordSetsRequest{
HostedZoneID: aws.String(cleanZoneID(*zoneRecord.HostedZone.ID)),
ChangeBatch: changeBatch,
}
log.Printf("[DEBUG] Creating resource records for zone: %s, name: %s",
zone, d.Get("name").(string))
@ -103,7 +109,7 @@ func resourceAwsRoute53RecordCreate(d *schema.ResourceData, meta interface{}) er
Timeout: 5 * time.Minute,
MinTimeout: 1 * time.Second,
Refresh: func() (interface{}, string, error) {
resp, err := conn.ChangeResourceRecordSets(zone, req)
resp, err := conn.ChangeResourceRecordSets(req)
if err != nil {
if strings.Contains(err.Error(), "PriorRequestNotComplete") {
// There is some pending operation, so just retry
@ -114,7 +120,7 @@ func resourceAwsRoute53RecordCreate(d *schema.ResourceData, meta interface{}) er
return nil, "failure", err
}
return resp.ChangeInfo, "accepted", nil
return resp, "accepted", nil
},
}
@ -122,7 +128,7 @@ func resourceAwsRoute53RecordCreate(d *schema.ResourceData, meta interface{}) er
if err != nil {
return err
}
changeInfo := respRaw.(route53.ChangeInfo)
changeInfo := respRaw.(*route53.ChangeResourceRecordSetsResponse).ChangeInfo
// Generate an ID
d.SetId(fmt.Sprintf("%s_%s_%s", zone, d.Get("name").(string), d.Get("type").(string)))
@ -135,7 +141,10 @@ func resourceAwsRoute53RecordCreate(d *schema.ResourceData, meta interface{}) er
Timeout: 10 * time.Minute,
MinTimeout: 5 * time.Second,
Refresh: func() (result interface{}, state string, err error) {
return resourceAwsRoute53Wait(conn, changeInfo.ID)
changeRequest := &route53.GetChangeRequest{
ID: aws.String(cleanChangeID(*changeInfo.ID)),
}
return resourceAwsGoRoute53Wait(conn, changeRequest)
},
}
_, err = wait.WaitForState()
@ -147,31 +156,33 @@ func resourceAwsRoute53RecordCreate(d *schema.ResourceData, meta interface{}) er
}
func resourceAwsRoute53RecordRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).route53
conn := meta.(*AWSClient).r53conn
zone := d.Get("zone_id").(string)
lopts := &route53.ListOpts{
Name: d.Get("name").(string),
Type: d.Get("type").(string),
lopts := &route53.ListResourceRecordSetsRequest{
HostedZoneID: aws.String(cleanZoneID(zone)),
StartRecordName: aws.String(d.Get("name").(string)),
StartRecordType: aws.String(d.Get("type").(string)),
}
resp, err := conn.ListResourceRecordSets(zone, lopts)
resp, err := conn.ListResourceRecordSets(lopts)
if err != nil {
return err
}
// Scan for a matching record
found := false
for _, record := range resp.Records {
if route53.FQDN(record.Name) != route53.FQDN(lopts.Name) {
for _, record := range resp.ResourceRecordSets {
if FQDN(*record.Name) != FQDN(*lopts.StartRecordName) {
continue
}
if strings.ToUpper(record.Type) != strings.ToUpper(lopts.Type) {
if strings.ToUpper(*record.Type) != strings.ToUpper(*lopts.StartRecordType) {
continue
}
found = true
d.Set("records", record.Records)
d.Set("records", record.ResourceRecords)
d.Set("ttl", record.TTL)
break
@ -185,7 +196,11 @@ func resourceAwsRoute53RecordRead(d *schema.ResourceData, meta interface{}) erro
}
func resourceAwsRoute53RecordDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).route53
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))
// Get the records
rec, err := resourceAwsRoute53RecordBuildSet(d)
@ -194,18 +209,20 @@ func resourceAwsRoute53RecordDelete(d *schema.ResourceData, meta interface{}) er
}
// Create the new records
req := &route53.ChangeResourceRecordSetsRequest{
Comment: "Deleted by Terraform",
changeBatch := &route53.ChangeBatch{
Comment: aws.String("Deleted by Terraform"),
Changes: []route53.Change{
route53.Change{
Action: "DELETE",
Record: *rec,
Action: aws.String("DELETE"),
ResourceRecordSet: rec,
},
},
}
zone := d.Get("zone_id").(string)
log.Printf("[DEBUG] Deleting resource records for zone: %s, name: %s",
zone, d.Get("name").(string))
req := &route53.ChangeResourceRecordSetsRequest{
HostedZoneID: aws.String(cleanZoneID(zone)),
ChangeBatch: changeBatch,
}
wait := resource.StateChangeConf{
Pending: []string{"rejected"},
@ -213,7 +230,7 @@ func resourceAwsRoute53RecordDelete(d *schema.ResourceData, meta interface{}) er
Timeout: 5 * time.Minute,
MinTimeout: 1 * time.Second,
Refresh: func() (interface{}, string, error) {
_, err := conn.ChangeResourceRecordSets(zone, req)
_, err := conn.ChangeResourceRecordSets(req)
if err != nil {
if strings.Contains(err.Error(), "PriorRequestNotComplete") {
// There is some pending operation, so just retry
@ -242,17 +259,26 @@ func resourceAwsRoute53RecordDelete(d *schema.ResourceData, meta interface{}) er
func resourceAwsRoute53RecordBuildSet(d *schema.ResourceData) (*route53.ResourceRecordSet, error) {
recs := d.Get("records").(*schema.Set).List()
records := make([]string, 0, len(recs))
records := make([]route53.ResourceRecord, 0, len(recs))
for _, r := range recs {
records = append(records, r.(string))
records = append(records, route53.ResourceRecord{Value: aws.String(r.(string))})
}
rec := &route53.ResourceRecordSet{
Name: d.Get("name").(string),
Type: d.Get("type").(string),
TTL: d.Get("ttl").(int),
Records: records,
Name: aws.String(d.Get("name").(string)),
Type: aws.String(d.Get("type").(string)),
TTL: aws.Long(int64(d.Get("ttl").(int))),
ResourceRecords: records,
}
return rec, nil
}
func FQDN(name string) string {
n := len(name)
if n == 0 || name[n-1] == '.' {
return name
} else {
return name + "."
}
}

View File

@ -7,7 +7,9 @@ import (
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/goamz/route53"
"github.com/awslabs/aws-sdk-go/aws"
awsr53 "github.com/awslabs/aws-sdk-go/gen/route53"
)
func TestAccRoute53Record(t *testing.T) {
@ -43,7 +45,7 @@ func TestAccRoute53Record_generatesSuffix(t *testing.T) {
}
func testAccCheckRoute53RecordDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).route53
conn := testAccProvider.Meta().(*AWSClient).r53conn
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_route53_record" {
continue
@ -54,16 +56,21 @@ func testAccCheckRoute53RecordDestroy(s *terraform.State) error {
name := parts[1]
rType := parts[2]
lopts := &route53.ListOpts{Name: name, Type: rType}
resp, err := conn.ListResourceRecordSets(zone, lopts)
lopts := &awsr53.ListResourceRecordSetsRequest{
HostedZoneID: aws.String(cleanZoneID(zone)),
StartRecordName: aws.String(name),
StartRecordType: aws.String(rType),
}
resp, err := conn.ListResourceRecordSets(lopts)
if err != nil {
return err
}
if len(resp.Records) == 0 {
if len(resp.ResourceRecordSets) == 0 {
return nil
}
rec := resp.Records[0]
if route53.FQDN(rec.Name) == route53.FQDN(name) && rec.Type == rType {
rec := resp.ResourceRecordSets[0]
if FQDN(*rec.Name) == FQDN(name) && *rec.Type == rType {
return fmt.Errorf("Record still exists: %#v", rec)
}
}
@ -72,7 +79,7 @@ func testAccCheckRoute53RecordDestroy(s *terraform.State) error {
func testAccCheckRoute53RecordExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).route53
conn := testAccProvider.Meta().(*AWSClient).r53conn
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
@ -87,16 +94,21 @@ func testAccCheckRoute53RecordExists(n string) resource.TestCheckFunc {
name := parts[1]
rType := parts[2]
lopts := &route53.ListOpts{Name: name, Type: rType}
resp, err := conn.ListResourceRecordSets(zone, lopts)
lopts := &awsr53.ListResourceRecordSetsRequest{
HostedZoneID: aws.String(cleanZoneID(zone)),
StartRecordName: aws.String(name),
StartRecordType: aws.String(rType),
}
resp, err := conn.ListResourceRecordSets(lopts)
if err != nil {
return err
}
if len(resp.Records) == 0 {
if len(resp.ResourceRecordSets) == 0 {
return fmt.Errorf("Record does not exist")
}
rec := resp.Records[0]
if route53.FQDN(rec.Name) == route53.FQDN(name) && rec.Type == rType {
rec := resp.ResourceRecordSets[0]
if FQDN(*rec.Name) == FQDN(name) && *rec.Type == rType {
return nil
}
return fmt.Errorf("Record does not exist: %#v", rec)

View File

@ -7,7 +7,9 @@ import (
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/mitchellh/goamz/route53"
"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/gen/route53"
)
func resourceAwsRoute53Zone() *schema.Resource {
@ -32,20 +34,23 @@ func resourceAwsRoute53Zone() *schema.Resource {
}
func resourceAwsRoute53ZoneCreate(d *schema.ResourceData, meta interface{}) error {
r53 := meta.(*AWSClient).route53
r53 := meta.(*AWSClient).r53conn
comment := &route53.HostedZoneConfig{Comment: aws.String("Managed by Terraform")}
req := &route53.CreateHostedZoneRequest{
Name: d.Get("name").(string),
Comment: "Managed by Terraform",
Name: aws.String(d.Get("name").(string)),
HostedZoneConfig: comment,
CallerReference: aws.String(time.Now().Format(time.RFC3339Nano)),
}
log.Printf("[DEBUG] Creating Route53 hosted zone: %s", req.Name)
log.Printf("[DEBUG] Creating Route53 hosted zone: %s", *req.Name)
resp, err := r53.CreateHostedZone(req)
if err != nil {
return err
}
// Store the zone_id
zone := route53.CleanZoneID(resp.HostedZone.ID)
zone := cleanZoneID(*resp.HostedZone.ID)
d.Set("zone_id", zone)
d.SetId(zone)
@ -57,7 +62,10 @@ func resourceAwsRoute53ZoneCreate(d *schema.ResourceData, meta interface{}) erro
Timeout: 10 * time.Minute,
MinTimeout: 2 * time.Second,
Refresh: func() (result interface{}, state string, err error) {
return resourceAwsRoute53Wait(r53, resp.ChangeInfo.ID)
changeRequest := &route53.GetChangeRequest{
ID: aws.String(cleanChangeID(*resp.ChangeInfo.ID)),
}
return resourceAwsGoRoute53Wait(r53, changeRequest)
},
}
_, err = wait.WaitForState()
@ -68,9 +76,8 @@ func resourceAwsRoute53ZoneCreate(d *schema.ResourceData, meta interface{}) erro
}
func resourceAwsRoute53ZoneRead(d *schema.ResourceData, meta interface{}) error {
r53 := meta.(*AWSClient).route53
_, err := r53.GetHostedZone(d.Id())
r53 := meta.(*AWSClient).r53conn
_, err := r53.GetHostedZone(&route53.GetHostedZoneRequest{ID: aws.String(d.Id())})
if err != nil {
// Handle a deleted zone
if strings.Contains(err.Error(), "404") {
@ -84,11 +91,11 @@ func resourceAwsRoute53ZoneRead(d *schema.ResourceData, meta interface{}) error
}
func resourceAwsRoute53ZoneDelete(d *schema.ResourceData, meta interface{}) error {
r53 := meta.(*AWSClient).route53
r53 := meta.(*AWSClient).r53conn
log.Printf("[DEBUG] Deleting Route53 hosted zone: %s (ID: %s)",
d.Get("name").(string), d.Id())
_, err := r53.DeleteHostedZone(d.Id())
_, err := r53.DeleteHostedZone(&route53.DeleteHostedZoneRequest{ID: aws.String(d.Id())})
if err != nil {
return err
}
@ -96,11 +103,29 @@ func resourceAwsRoute53ZoneDelete(d *schema.ResourceData, meta interface{}) erro
return nil
}
// resourceAwsRoute53Wait checks the status of a change
func resourceAwsRoute53Wait(r53 *route53.Route53, ref string) (result interface{}, state string, err error) {
func resourceAwsGoRoute53Wait(r53 *route53.Route53, ref *route53.GetChangeRequest) (result interface{}, state string, err error) {
status, err := r53.GetChange(ref)
if err != nil {
return nil, "UNKNOWN", err
}
return true, status, nil
return true, *status.ChangeInfo.Status, nil
}
// cleanChangeID is used to remove the leading /change/
func cleanChangeID(ID string) string {
return cleanPrefix(ID, "/change/")
}
// cleanZoneID is used to remove the leading /hostedzone/
func cleanZoneID(ID string) string {
return cleanPrefix(ID, "/hostedzone/")
}
// cleanPrefix removes a string prefix from an ID
func cleanPrefix(ID, prefix string) string {
if strings.HasPrefix(ID, prefix) {
ID = strings.TrimPrefix(ID, prefix)
}
return ID
}

View File

@ -6,8 +6,62 @@ import (
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/gen/route53"
)
func TestCleanPrefix(t *testing.T) {
cases := []struct {
Input, Prefix, Output string
}{
{"/hostedzone/foo", "/hostedzone/", "foo"},
{"/change/foo", "/change/", "foo"},
{"/bar", "/test", "/bar"},
}
for _, tc := range cases {
actual := cleanPrefix(tc.Input, tc.Prefix)
if actual != tc.Output {
t.Fatalf("input: %s\noutput: %s", tc.Input, actual)
}
}
}
func TestCleanZoneID(t *testing.T) {
cases := []struct {
Input, Output string
}{
{"/hostedzone/foo", "foo"},
{"/change/foo", "/change/foo"},
{"/bar", "/bar"},
}
for _, tc := range cases {
actual := cleanZoneID(tc.Input)
if actual != tc.Output {
t.Fatalf("input: %s\noutput: %s", tc.Input, actual)
}
}
}
func TestCleanChangeID(t *testing.T) {
cases := []struct {
Input, Output string
}{
{"/hostedzone/foo", "/hostedzone/foo"},
{"/change/foo", "foo"},
{"/bar", "/bar"},
}
for _, tc := range cases {
actual := cleanChangeID(tc.Input)
if actual != tc.Output {
t.Fatalf("input: %s\noutput: %s", tc.Input, actual)
}
}
}
func TestAccRoute53Zone(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
@ -25,13 +79,13 @@ func TestAccRoute53Zone(t *testing.T) {
}
func testAccCheckRoute53ZoneDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).route53
conn := testAccProvider.Meta().(*AWSClient).r53conn
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_route53_zone" {
continue
}
_, err := conn.GetHostedZone(rs.Primary.ID)
_, err := conn.GetHostedZone(&route53.GetHostedZoneRequest{ID: aws.String(rs.Primary.ID)})
if err == nil {
return fmt.Errorf("Hosted zone still exists")
}
@ -50,8 +104,8 @@ func testAccCheckRoute53ZoneExists(n string) resource.TestCheckFunc {
return fmt.Errorf("No hosted zone ID is set")
}
conn := testAccProvider.Meta().(*AWSClient).route53
_, err := conn.GetHostedZone(rs.Primary.ID)
conn := testAccProvider.Meta().(*AWSClient).r53conn
_, err := conn.GetHostedZone(&route53.GetHostedZoneRequest{ID: aws.String(rs.Primary.ID)})
if err != nil {
return fmt.Errorf("Hosted zone err: %v", err)
}