terraform/builtin/providers/aws/resource_aws_route53_zone.go

107 lines
2.4 KiB
Go
Raw Normal View History

package aws
import (
"log"
"strings"
"time"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/mitchellh/goamz/route53"
)
func resourceAwsRoute53Zone() *schema.Resource {
return &schema.Resource{
Create: resourceAwsRoute53ZoneCreate,
Read: resourceAwsRoute53ZoneRead,
Delete: resourceAwsRoute53ZoneDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"zone_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAwsRoute53ZoneCreate(d *schema.ResourceData, meta interface{}) error {
r53 := meta.(*AWSClient).route53
req := &route53.CreateHostedZoneRequest{
Name: d.Get("name").(string),
Comment: "Managed by Terraform",
}
log.Printf("[DEBUG] Creating Route53 hosted zone: %s", req.Name)
resp, err := r53.CreateHostedZone(req)
if err != nil {
return err
}
// Store the zone_id
zone := route53.CleanZoneID(resp.HostedZone.ID)
d.Set("zone_id", zone)
d.SetId(zone)
// Wait until we are done initializing
wait := resource.StateChangeConf{
Delay: 30 * time.Second,
2014-07-23 00:44:59 +02:00
Pending: []string{"PENDING"},
Target: "INSYNC",
Timeout: 10 * time.Minute,
MinTimeout: 2 * time.Second,
Refresh: func() (result interface{}, state string, err error) {
return resourceAwsRoute53Wait(r53, resp.ChangeInfo.ID)
},
}
_, err = wait.WaitForState()
if err != nil {
return err
}
return nil
}
func resourceAwsRoute53ZoneRead(d *schema.ResourceData, meta interface{}) error {
r53 := meta.(*AWSClient).route53
_, err := r53.GetHostedZone(d.Id())
if err != nil {
// Handle a deleted zone
if strings.Contains(err.Error(), "404") {
d.SetId("")
return nil
}
return err
}
return nil
}
func resourceAwsRoute53ZoneDelete(d *schema.ResourceData, meta interface{}) error {
r53 := meta.(*AWSClient).route53
log.Printf("[DEBUG] Deleting Route53 hosted zone: %s (ID: %s)",
d.Get("name").(string), d.Id())
_, err := r53.DeleteHostedZone(d.Id())
if err != nil {
return err
}
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
}