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