github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/aws/resource_aws_route53_zone.go (about) 1 package aws 2 3 import ( 4 "log" 5 "strings" 6 "time" 7 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/helper/schema" 10 "github.com/mitchellh/goamz/route53" 11 ) 12 13 func resourceAwsRoute53Zone() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceAwsRoute53ZoneCreate, 16 Read: resourceAwsRoute53ZoneRead, 17 Delete: resourceAwsRoute53ZoneDelete, 18 19 Schema: map[string]*schema.Schema{ 20 "name": &schema.Schema{ 21 Type: schema.TypeString, 22 Required: true, 23 ForceNew: true, 24 }, 25 26 "zone_id": &schema.Schema{ 27 Type: schema.TypeString, 28 Computed: true, 29 }, 30 }, 31 } 32 } 33 34 func resourceAwsRoute53ZoneCreate(d *schema.ResourceData, meta interface{}) error { 35 r53 := meta.(*AWSClient).route53 36 37 req := &route53.CreateHostedZoneRequest{ 38 Name: d.Get("name").(string), 39 Comment: "Managed by Terraform", 40 } 41 log.Printf("[DEBUG] Creating Route53 hosted zone: %s", req.Name) 42 resp, err := r53.CreateHostedZone(req) 43 if err != nil { 44 return err 45 } 46 47 // Store the zone_id 48 zone := route53.CleanZoneID(resp.HostedZone.ID) 49 d.Set("zone_id", zone) 50 d.SetId(zone) 51 52 // Wait until we are done initializing 53 wait := resource.StateChangeConf{ 54 Delay: 30 * time.Second, 55 Pending: []string{"PENDING"}, 56 Target: "INSYNC", 57 Timeout: 10 * time.Minute, 58 MinTimeout: 2 * time.Second, 59 Refresh: func() (result interface{}, state string, err error) { 60 return resourceAwsRoute53Wait(r53, resp.ChangeInfo.ID) 61 }, 62 } 63 _, err = wait.WaitForState() 64 if err != nil { 65 return err 66 } 67 return nil 68 } 69 70 func resourceAwsRoute53ZoneRead(d *schema.ResourceData, meta interface{}) error { 71 r53 := meta.(*AWSClient).route53 72 73 _, err := r53.GetHostedZone(d.Id()) 74 if err != nil { 75 // Handle a deleted zone 76 if strings.Contains(err.Error(), "404") { 77 d.SetId("") 78 return nil 79 } 80 return err 81 } 82 83 return nil 84 } 85 86 func resourceAwsRoute53ZoneDelete(d *schema.ResourceData, meta interface{}) error { 87 r53 := meta.(*AWSClient).route53 88 89 log.Printf("[DEBUG] Deleting Route53 hosted zone: %s (ID: %s)", 90 d.Get("name").(string), d.Id()) 91 _, err := r53.DeleteHostedZone(d.Id()) 92 if err != nil { 93 return err 94 } 95 96 return nil 97 } 98 99 // resourceAwsRoute53Wait checks the status of a change 100 func resourceAwsRoute53Wait(r53 *route53.Route53, ref string) (result interface{}, state string, err error) { 101 status, err := r53.GetChange(ref) 102 if err != nil { 103 return nil, "UNKNOWN", err 104 } 105 return true, status, nil 106 }