github.com/paulmey/terraform@v0.5.2-0.20150519145237-046e9b4c884d/builtin/providers/aws/resource_aws_route53_zone_association.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 "strings" 7 "time" 8 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/helper/schema" 11 12 "github.com/awslabs/aws-sdk-go/aws" 13 "github.com/awslabs/aws-sdk-go/service/route53" 14 ) 15 16 func resourceAwsRoute53ZoneAssociation() *schema.Resource { 17 return &schema.Resource{ 18 Create: resourceAwsRoute53ZoneAssociationCreate, 19 Read: resourceAwsRoute53ZoneAssociationRead, 20 Update: resourceAwsRoute53ZoneAssociationUpdate, 21 Delete: resourceAwsRoute53ZoneAssociationDelete, 22 23 Schema: map[string]*schema.Schema{ 24 "zone_id": &schema.Schema{ 25 Type: schema.TypeString, 26 Required: true, 27 }, 28 29 "vpc_id": &schema.Schema{ 30 Type: schema.TypeString, 31 Required: true, 32 }, 33 34 "vpc_region": &schema.Schema{ 35 Type: schema.TypeString, 36 Optional: true, 37 Computed: true, 38 }, 39 }, 40 } 41 } 42 43 func resourceAwsRoute53ZoneAssociationCreate(d *schema.ResourceData, meta interface{}) error { 44 r53 := meta.(*AWSClient).r53conn 45 46 req := &route53.AssociateVPCWithHostedZoneInput{ 47 HostedZoneID: aws.String(d.Get("zone_id").(string)), 48 VPC: &route53.VPC{ 49 VPCID: aws.String(d.Get("vpc_id").(string)), 50 VPCRegion: aws.String(meta.(*AWSClient).region), 51 }, 52 Comment: aws.String("Managed by Terraform"), 53 } 54 if w := d.Get("vpc_region"); w != "" { 55 req.VPC.VPCRegion = aws.String(w.(string)) 56 } 57 58 log.Printf("[DEBUG] Associating Route53 Private Zone %s with VPC %s with region %s", *req.HostedZoneID, *req.VPC.VPCID, *req.VPC.VPCRegion) 59 resp, err := r53.AssociateVPCWithHostedZone(req) 60 if err != nil { 61 return err 62 } 63 64 // Store association id 65 d.SetId(fmt.Sprintf("%s:%s", *req.HostedZoneID, *req.VPC.VPCID)) 66 d.Set("vpc_region", req.VPC.VPCRegion) 67 68 // Wait until we are done initializing 69 wait := resource.StateChangeConf{ 70 Delay: 30 * time.Second, 71 Pending: []string{"PENDING"}, 72 Target: "INSYNC", 73 Timeout: 10 * time.Minute, 74 MinTimeout: 2 * time.Second, 75 Refresh: func() (result interface{}, state string, err error) { 76 changeRequest := &route53.GetChangeInput{ 77 ID: aws.String(cleanChangeID(*resp.ChangeInfo.ID)), 78 } 79 return resourceAwsGoRoute53Wait(r53, changeRequest) 80 }, 81 } 82 _, err = wait.WaitForState() 83 if err != nil { 84 return err 85 } 86 87 return resourceAwsRoute53ZoneAssociationUpdate(d, meta) 88 } 89 90 func resourceAwsRoute53ZoneAssociationRead(d *schema.ResourceData, meta interface{}) error { 91 r53 := meta.(*AWSClient).r53conn 92 zone_id, vpc_id := resourceAwsRoute53ZoneAssociationParseId(d.Id()) 93 zone, err := r53.GetHostedZone(&route53.GetHostedZoneInput{ID: aws.String(zone_id)}) 94 if err != nil { 95 // Handle a deleted zone 96 if r53err, ok := err.(aws.APIError); ok && r53err.Code == "NoSuchHostedZone" { 97 d.SetId("") 98 return nil 99 } 100 return err 101 } 102 103 for _, vpc := range zone.VPCs { 104 if vpc_id == *vpc.VPCID { 105 // association is there, return 106 return nil 107 } 108 } 109 110 // no association found 111 d.SetId("") 112 return nil 113 } 114 115 func resourceAwsRoute53ZoneAssociationUpdate(d *schema.ResourceData, meta interface{}) error { 116 return resourceAwsRoute53ZoneAssociationRead(d, meta) 117 } 118 119 func resourceAwsRoute53ZoneAssociationDelete(d *schema.ResourceData, meta interface{}) error { 120 r53 := meta.(*AWSClient).r53conn 121 zone_id, vpc_id := resourceAwsRoute53ZoneAssociationParseId(d.Id()) 122 log.Printf("[DEBUG] Deleting Route53 Private Zone (%s) association (VPC: %s)", 123 zone_id, vpc_id) 124 125 req := &route53.DisassociateVPCFromHostedZoneInput{ 126 HostedZoneID: aws.String(zone_id), 127 VPC: &route53.VPC{ 128 VPCID: aws.String(vpc_id), 129 VPCRegion: aws.String(d.Get("vpc_region").(string)), 130 }, 131 Comment: aws.String("Managed by Terraform"), 132 } 133 134 _, err := r53.DisassociateVPCFromHostedZone(req) 135 if err != nil { 136 return err 137 } 138 139 return nil 140 } 141 142 func resourceAwsRoute53ZoneAssociationParseId(id string) (zone_id, vpc_id string) { 143 parts := strings.SplitN(id, ":", 2) 144 zone_id = parts[0] 145 vpc_id = parts[1] 146 return 147 }