107 lines
2.4 KiB
Go
107 lines
2.4 KiB
Go
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,
|
|
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
|
|
}
|