terraform/builtin/providers/aws/resource_aws_route53_zone.go

119 lines
2.7 KiB
Go

package aws
import (
"log"
"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(
s *terraform.ResourceState,
d *terraform.ResourceDiff,
meta interface{}) (*terraform.ResourceState, error) {
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{
Delay: 15 * time.Second,
Pending: []string{"PENDING"},
Target: "INSYNC",
Timeout: 10 * time.Minute,
MinTimeout: 3 * time.Second,
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(
s *terraform.ResourceState,
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(
s *terraform.ResourceState,
meta interface{}) (*terraform.ResourceState, error) {
p := meta.(*ResourceProvider)
r53 := p.route53
_, err := r53.GetHostedZone(s.Attributes["zone_id"])
if err != nil {
return s, err
}
return s, nil
}
func resource_aws_r53_zone_diff(
s *terraform.ResourceState,
c *terraform.ResourceConfig,
meta interface{}) (*terraform.ResourceDiff, error) {
b := &diff.ResourceBuilder{
Attrs: map[string]diff.AttrType{
"name": diff.AttrTypeCreate,
},
ComputedAttrs: []string{
"zone_id",
},
}
return b.Diff(s, c)
}