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/config"
|
|
|
|
"github.com/hashicorp/terraform/helper/diff"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/goamz/route53"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resource_aws_r53_zone_validation() *config.Validator {
|
|
|
|
return &config.Validator{
|
|
|
|
Required: []string{
|
|
|
|
"name",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_r53_zone_create(
|
2014-09-17 02:44:42 +02:00
|
|
|
s *terraform.InstanceState,
|
2014-09-18 01:33:24 +02:00
|
|
|
d *terraform.InstanceDiff,
|
2014-09-17 02:44:42 +02:00
|
|
|
meta interface{}) (*terraform.InstanceState, error) {
|
2014-07-22 23:55:19 +02:00
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
r53 := p.route53
|
|
|
|
|
|
|
|
// Merge the diff into the state so that we have all the attributes
|
|
|
|
// properly.
|
|
|
|
rs := s.MergeDiff(d)
|
|
|
|
|
|
|
|
req := &route53.CreateHostedZoneRequest{
|
|
|
|
Name: rs.Attributes["name"],
|
|
|
|
Comment: "Managed by Terraform",
|
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] Creating Route53 hosted zone: %s", req.Name)
|
|
|
|
resp, err := r53.CreateHostedZone(req)
|
|
|
|
if err != nil {
|
|
|
|
return rs, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the zone_id
|
|
|
|
zone := route53.CleanZoneID(resp.HostedZone.ID)
|
|
|
|
rs.ID = zone
|
|
|
|
rs.Attributes["zone_id"] = zone
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
return resource_aws_r53_wait(r53, resp.ChangeInfo.ID)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, err = wait.WaitForState()
|
|
|
|
if err != nil {
|
|
|
|
return rs, err
|
|
|
|
}
|
|
|
|
return rs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// resource_aws_r53_wait checks the status of a change
|
|
|
|
func resource_aws_r53_wait(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
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_r53_zone_destroy(
|
2014-09-17 02:44:42 +02:00
|
|
|
s *terraform.InstanceState,
|
2014-07-22 23:55:19 +02:00
|
|
|
meta interface{}) error {
|
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
r53 := p.route53
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Deleting Route53 hosted zone: %s (ID: %s)",
|
|
|
|
s.Attributes["name"], s.Attributes["zone_id"])
|
|
|
|
_, err := r53.DeleteHostedZone(s.Attributes["zone_id"])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_r53_zone_refresh(
|
2014-09-17 02:44:42 +02:00
|
|
|
s *terraform.InstanceState,
|
|
|
|
meta interface{}) (*terraform.InstanceState, error) {
|
2014-07-22 23:55:19 +02:00
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
r53 := p.route53
|
|
|
|
|
|
|
|
_, err := r53.GetHostedZone(s.Attributes["zone_id"])
|
|
|
|
if err != nil {
|
2014-07-27 02:56:04 +02:00
|
|
|
// Handle a deleted zone
|
|
|
|
if strings.Contains(err.Error(), "404") {
|
|
|
|
s.ID = ""
|
|
|
|
return s, nil
|
|
|
|
}
|
2014-07-22 23:55:19 +02:00
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_r53_zone_diff(
|
2014-09-17 02:44:42 +02:00
|
|
|
s *terraform.InstanceState,
|
2014-07-22 23:55:19 +02:00
|
|
|
c *terraform.ResourceConfig,
|
2014-09-18 01:33:24 +02:00
|
|
|
meta interface{}) (*terraform.InstanceDiff, error) {
|
2014-07-22 23:55:19 +02:00
|
|
|
|
|
|
|
b := &diff.ResourceBuilder{
|
|
|
|
Attrs: map[string]diff.AttrType{
|
|
|
|
"name": diff.AttrTypeCreate,
|
|
|
|
},
|
|
|
|
|
|
|
|
ComputedAttrs: []string{
|
|
|
|
"zone_id",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return b.Diff(s, c)
|
|
|
|
}
|