cleanup of zone_association resource

This commit is contained in:
John Engelman 2015-05-08 10:59:31 -05:00
parent 9da89974fc
commit 3507c0618b
3 changed files with 46 additions and 25 deletions

View File

@ -1,8 +1,12 @@
package aws package aws
import ( import (
"fmt"
"log" "log"
"strings"
"time"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/awslabs/aws-sdk-go/aws" "github.com/awslabs/aws-sdk-go/aws"
@ -32,10 +36,6 @@ func resourceAwsRoute53ZoneAssociation() *schema.Resource {
Optional: true, Optional: true,
}, },
"association_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
}, },
} }
} }
@ -62,16 +62,35 @@ func resourceAwsRoute53ZoneAssociationCreate(d *schema.ResourceData, meta interf
} }
// Store association id // Store association id
association_id := cleanChangeID(*resp.ChangeInfo.ID) d.SetId(fmt.Sprintf("%s:%s", *req.HostedZoneID, *req.VPC.VPCID))
d.Set("association_id", association_id) d.Set("vpc_region", req.VPC.VPCRegion)
d.SetId(association_id)
// Wait until we are done initializing
wait := resource.StateChangeConf{
Delay: 30 * time.Second,
Pending: []string{"PENDING"},
Target: "INSYNC",
Timeout: 10 * time.Minute,
MinTimeout: 2 * time.Second,
Refresh: func() (result interface{}, state string, err error) {
changeRequest := &route53.GetChangeInput{
ID: aws.String(cleanChangeID(*resp.ChangeInfo.ID)),
}
return resourceAwsGoRoute53Wait(r53, changeRequest)
},
}
_, err = wait.WaitForState()
if err != nil {
return err
}
return resourceAwsRoute53ZoneAssociationUpdate(d, meta) return resourceAwsRoute53ZoneAssociationUpdate(d, meta)
} }
func resourceAwsRoute53ZoneAssociationRead(d *schema.ResourceData, meta interface{}) error { func resourceAwsRoute53ZoneAssociationRead(d *schema.ResourceData, meta interface{}) error {
r53 := meta.(*AWSClient).r53conn r53 := meta.(*AWSClient).r53conn
zone, err := r53.GetHostedZone(&route53.GetHostedZoneInput{ID: aws.String(d.Id())}) zone_id, vpc_id := resourceAwsRoute53ZoneAssociationParseId(d.Id())
zone, err := r53.GetHostedZone(&route53.GetHostedZoneInput{ID: aws.String(zone_id)})
if err != nil { if err != nil {
// Handle a deleted zone // Handle a deleted zone
if r53err, ok := err.(aws.APIError); ok && r53err.Code == "NoSuchHostedZone" { if r53err, ok := err.(aws.APIError); ok && r53err.Code == "NoSuchHostedZone" {
@ -81,10 +100,8 @@ func resourceAwsRoute53ZoneAssociationRead(d *schema.ResourceData, meta interfac
return err return err
} }
vpc_id := d.Get("vpc_id") for _, vpc := range zone.VPCs {
if vpc_id == *vpc.VPCID {
for i := range zone.VPCs {
if vpc_id == *zone.VPCs[i].VPCID {
// association is there, return // association is there, return
return nil return nil
} }
@ -101,21 +118,18 @@ func resourceAwsRoute53ZoneAssociationUpdate(d *schema.ResourceData, meta interf
func resourceAwsRoute53ZoneAssociationDelete(d *schema.ResourceData, meta interface{}) error { func resourceAwsRoute53ZoneAssociationDelete(d *schema.ResourceData, meta interface{}) error {
r53 := meta.(*AWSClient).r53conn r53 := meta.(*AWSClient).r53conn
zone_id, vpc_id := resourceAwsRoute53ZoneAssociationParseId(d.Id())
log.Printf("[DEBUG] Deleting Route53 Private Zone (%s) association (ID: %s)", log.Printf("[DEBUG] Deleting Route53 Private Zone (%s) association (VPC: %s)",
d.Get("zone_id").(string), d.Id()) zone_id, vpc_id)
req := &route53.DisassociateVPCFromHostedZoneInput{ req := &route53.DisassociateVPCFromHostedZoneInput{
HostedZoneID: aws.String(d.Get("zone_id").(string)), HostedZoneID: aws.String(zone_id),
VPC: &route53.VPC{ VPC: &route53.VPC{
VPCID: aws.String(d.Get("vpc_id").(string)), VPCID: aws.String(vpc_id),
VPCRegion: aws.String(meta.(*AWSClient).region), VPCRegion: aws.String(d.Get("vpc_region").(string)),
}, },
Comment: aws.String("Managed by Terraform"), Comment: aws.String("Managed by Terraform"),
} }
if w := d.Get("vpc_region"); w != "" {
req.VPC.VPCRegion = aws.String(w.(string))
}
_, err := r53.DisassociateVPCFromHostedZone(req) _, err := r53.DisassociateVPCFromHostedZone(req)
if err != nil { if err != nil {
@ -124,3 +138,10 @@ func resourceAwsRoute53ZoneAssociationDelete(d *schema.ResourceData, meta interf
return nil return nil
} }
func resourceAwsRoute53ZoneAssociationParseId(id string) (zone_id, vpc_id string) {
parts := strings.SplitN(id, ":", 2)
zone_id = parts[0]
vpc_id = parts[1]
return
}

View File

@ -62,8 +62,8 @@ func testAccCheckRoute53ZoneAssociationExists(n string, zone *route53.HostedZone
} }
exists := false exists := false
for i := range resp.VPCs { for _, vpc := range resp.VPCs {
if rs.Primary.Meta["vpc_id"] == *resp.VPCs[i].VPCID { if rs.Primary.Meta["vpc_id"] == *vpc.VPCID {
exists = true exists = true
} }
} }

View File

@ -96,7 +96,7 @@ func TestAccRoute53PrivateZone(t *testing.T) {
Config: testAccRoute53PrivateZoneConfig, Config: testAccRoute53PrivateZoneConfig,
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckRoute53ZoneExists("aws_route53_zone.main", &zone), testAccCheckRoute53ZoneExists("aws_route53_zone.main", &zone),
testAccCheckRoute53ZoneAssociationExists("aws_vpc.main", &zone), testAccCheckRoute53ZoneAssociatesWithVpc("aws_vpc.main", &zone),
), ),
}, },
}, },
@ -155,7 +155,7 @@ func testAccCheckRoute53ZoneExists(n string, zone *route53.GetHostedZoneOutput)
} }
} }
func testAccCheckRoute53ZoneAssociationExists(n string, zone *route53.GetHostedZoneOutput) resource.TestCheckFunc { func testAccCheckRoute53ZoneAssociatesWithVpc(n string, zone *route53.GetHostedZoneOutput) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n] rs, ok := s.RootModule().Resources[n]
if !ok { if !ok {