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