From 1b4fd28ef8deab33c22a83ce56550920e0d92204 Mon Sep 17 00:00:00 2001 From: Clint Shryock Date: Tue, 17 Feb 2015 15:10:28 -0600 Subject: [PATCH] provider/aws: Finish converting Route53 to aws-sdk-go This migrates Route53 Record to the new sdk and removes goamz/route53. --- builtin/providers/aws/config.go | 11 +- .../aws/resource_aws_route53_record.go | 108 ++++++++++++------ .../aws/resource_aws_route53_record_test.go | 38 +++--- .../aws/resource_aws_route53_zone.go | 40 +++---- .../aws/resource_aws_route53_zone_test.go | 12 +- 5 files changed, 120 insertions(+), 89 deletions(-) diff --git a/builtin/providers/aws/config.go b/builtin/providers/aws/config.go index 10f5903d0011..631fb79b924e 100644 --- a/builtin/providers/aws/config.go +++ b/builtin/providers/aws/config.go @@ -12,11 +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" - awsr53 "github.com/awslabs/aws-sdk-go/gen/route53" + "github.com/awslabs/aws-sdk-go/gen/route53" ) type Config struct { @@ -31,8 +30,7 @@ type AWSClient struct { autoscalingconn *autoscaling.AutoScaling s3conn *s3.S3 rdsconn *rds.Rds - route53 *route53.Route53 - awsr53Conn *awsr53.Route53 + r53conn *route53.Route53 } // Client configures and returns a fully initailized AWSClient @@ -66,15 +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) - - log.Println("[INFO] Initializing aws-go Route53 connection") 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.awsr53Conn = awsr53.New(creds, "us-east-1", nil) + client.r53conn = route53.New(creds, "us-east-1", nil) } if len(errs) > 0 { diff --git a/builtin/providers/aws/resource_aws_route53_record.go b/builtin/providers/aws/resource_aws_route53_record.go index 8250e1ecf22f..e0f05621266a 100644 --- a/builtin/providers/aws/resource_aws_route53_record.go +++ b/builtin/providers/aws/resource_aws_route53_record.go @@ -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,34 @@ 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 } + +// CleanZoneID is used to remove the leading /hostedzone/ +func CleanZoneID(ID string) string { + if strings.HasPrefix(ID, "/hostedzone/") { + ID = strings.TrimPrefix(ID, "/hostedzone/") + } + return ID +} + +func FQDN(name string) string { + n := len(name) + if n == 0 || name[n-1] == '.' { + return name + } else { + return name + "." + } +} diff --git a/builtin/providers/aws/resource_aws_route53_record_test.go b/builtin/providers/aws/resource_aws_route53_record_test.go index ee5272fdfa0d..1eeffd915a7f 100644 --- a/builtin/providers/aws/resource_aws_route53_record_test.go +++ b/builtin/providers/aws/resource_aws_route53_record_test.go @@ -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) diff --git a/builtin/providers/aws/resource_aws_route53_zone.go b/builtin/providers/aws/resource_aws_route53_zone.go index f05bb917c256..a477b9592a26 100644 --- a/builtin/providers/aws/resource_aws_route53_zone.go +++ b/builtin/providers/aws/resource_aws_route53_zone.go @@ -7,10 +7,9 @@ import ( "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" - "github.com/mitchellh/goamz/route53" - awsGo "github.com/awslabs/aws-sdk-go/aws" - awsr53 "github.com/awslabs/aws-sdk-go/gen/route53" + "github.com/awslabs/aws-sdk-go/aws" + "github.com/awslabs/aws-sdk-go/gen/route53" ) func resourceAwsRoute53Zone() *schema.Resource { @@ -35,13 +34,13 @@ func resourceAwsRoute53Zone() *schema.Resource { } func resourceAwsRoute53ZoneCreate(d *schema.ResourceData, meta interface{}) error { - r53 := meta.(*AWSClient).awsr53Conn + r53 := meta.(*AWSClient).r53conn - comment := &awsr53.HostedZoneConfig{Comment: awsGo.String("Managed by Terraform")} - req := &awsr53.CreateHostedZoneRequest{ - Name: awsGo.String(d.Get("name").(string)), + comment := &route53.HostedZoneConfig{Comment: aws.String("Managed by Terraform")} + req := &route53.CreateHostedZoneRequest{ + Name: aws.String(d.Get("name").(string)), HostedZoneConfig: comment, - CallerReference: awsGo.String(time.Now().Format(time.RFC3339Nano)), + CallerReference: aws.String(time.Now().Format(time.RFC3339Nano)), } log.Printf("[DEBUG] Creating Route53 hosted zone: %s", req.Name) resp, err := r53.CreateHostedZone(req) @@ -50,7 +49,7 @@ func resourceAwsRoute53ZoneCreate(d *schema.ResourceData, meta interface{}) erro } // Store the zone_id - zone := route53.CleanZoneID(*resp.HostedZone.ID) + zone := CleanZoneID(*resp.HostedZone.ID) d.Set("zone_id", zone) d.SetId(zone) @@ -62,8 +61,8 @@ func resourceAwsRoute53ZoneCreate(d *schema.ResourceData, meta interface{}) erro Timeout: 10 * time.Minute, MinTimeout: 2 * time.Second, Refresh: func() (result interface{}, state string, err error) { - changeRequest := &awsr53.GetChangeRequest{ - ID: awsGo.String(CleanChangeID(*resp.ChangeInfo.ID)), + changeRequest := &route53.GetChangeRequest{ + ID: aws.String(CleanChangeID(*resp.ChangeInfo.ID)), } return resourceAwsGoRoute53Wait(r53, changeRequest) }, @@ -76,8 +75,8 @@ func resourceAwsRoute53ZoneCreate(d *schema.ResourceData, meta interface{}) erro } func resourceAwsRoute53ZoneRead(d *schema.ResourceData, meta interface{}) error { - r53 := meta.(*AWSClient).awsr53Conn - _, err := r53.GetHostedZone(&awsr53.GetHostedZoneRequest{ID: awsGo.String(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") { @@ -91,11 +90,11 @@ func resourceAwsRoute53ZoneRead(d *schema.ResourceData, meta interface{}) error } func resourceAwsRoute53ZoneDelete(d *schema.ResourceData, meta interface{}) error { - r53 := meta.(*AWSClient).awsr53Conn + r53 := meta.(*AWSClient).r53conn log.Printf("[DEBUG] Deleting Route53 hosted zone: %s (ID: %s)", d.Get("name").(string), d.Id()) - _, err := r53.DeleteHostedZone(&awsr53.DeleteHostedZoneRequest{ID: awsGo.String(d.Id())}) + _, err := r53.DeleteHostedZone(&route53.DeleteHostedZoneRequest{ID: aws.String(d.Id())}) if err != nil { return err } @@ -103,16 +102,7 @@ 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) { - status, err := r53.GetChange(ref) - if err != nil { - return nil, "UNKNOWN", err - } - return true, status, nil -} - -func resourceAwsGoRoute53Wait(r53 *awsr53.Route53, ref *awsr53.GetChangeRequest) (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 { diff --git a/builtin/providers/aws/resource_aws_route53_zone_test.go b/builtin/providers/aws/resource_aws_route53_zone_test.go index 3a65b8f0987d..659b6c05a379 100644 --- a/builtin/providers/aws/resource_aws_route53_zone_test.go +++ b/builtin/providers/aws/resource_aws_route53_zone_test.go @@ -7,8 +7,8 @@ import ( "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" - awsGo "github.com/awslabs/aws-sdk-go/aws" - awsr53 "github.com/awslabs/aws-sdk-go/gen/route53" + "github.com/awslabs/aws-sdk-go/aws" + "github.com/awslabs/aws-sdk-go/gen/route53" ) func TestAccRoute53Zone(t *testing.T) { @@ -28,13 +28,13 @@ func TestAccRoute53Zone(t *testing.T) { } func testAccCheckRoute53ZoneDestroy(s *terraform.State) error { - conn := testAccProvider.Meta().(*AWSClient).awsr53Conn + conn := testAccProvider.Meta().(*AWSClient).r53conn for _, rs := range s.RootModule().Resources { if rs.Type != "aws_route53_zone" { continue } - _, err := conn.GetHostedZone(&awsr53.GetHostedZoneRequest{ID: awsGo.String(rs.Primary.ID)}) + _, err := conn.GetHostedZone(&route53.GetHostedZoneRequest{ID: aws.String(rs.Primary.ID)}) if err == nil { return fmt.Errorf("Hosted zone still exists") } @@ -53,8 +53,8 @@ func testAccCheckRoute53ZoneExists(n string) resource.TestCheckFunc { return fmt.Errorf("No hosted zone ID is set") } - conn := testAccProvider.Meta().(*AWSClient).awsr53Conn - _, err := conn.GetHostedZone(&awsr53.GetHostedZoneRequest{ID: awsGo.String(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) }