github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_route53_delegation_set.go (about)

     1  package aws
     2  
     3  import (
     4  	"log"
     5  	"sort"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  
    11  	"github.com/aws/aws-sdk-go/aws"
    12  	"github.com/aws/aws-sdk-go/service/route53"
    13  )
    14  
    15  func resourceAwsRoute53DelegationSet() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceAwsRoute53DelegationSetCreate,
    18  		Read:   resourceAwsRoute53DelegationSetRead,
    19  		Delete: resourceAwsRoute53DelegationSetDelete,
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"reference_name": &schema.Schema{
    23  				Type:     schema.TypeString,
    24  				Optional: true,
    25  				ForceNew: true,
    26  			},
    27  
    28  			"name_servers": &schema.Schema{
    29  				Type:     schema.TypeList,
    30  				Elem:     &schema.Schema{Type: schema.TypeString},
    31  				Computed: true,
    32  			},
    33  		},
    34  	}
    35  }
    36  
    37  func resourceAwsRoute53DelegationSetCreate(d *schema.ResourceData, meta interface{}) error {
    38  	r53 := meta.(*AWSClient).r53conn
    39  
    40  	callerRef := resource.UniqueId()
    41  	if v, ok := d.GetOk("reference_name"); ok {
    42  		callerRef = strings.Join([]string{
    43  			v.(string), "-", callerRef,
    44  		}, "")
    45  	}
    46  	input := &route53.CreateReusableDelegationSetInput{
    47  		CallerReference: aws.String(callerRef),
    48  	}
    49  
    50  	log.Printf("[DEBUG] Creating Route53 reusable delegation set: %#v", input)
    51  	out, err := r53.CreateReusableDelegationSet(input)
    52  	if err != nil {
    53  		return err
    54  	}
    55  	log.Printf("[DEBUG] Route53 reusable delegation set created: %#v", out)
    56  
    57  	set := out.DelegationSet
    58  	d.SetId(cleanDelegationSetId(*set.Id))
    59  	d.Set("name_servers", expandNameServers(set.NameServers))
    60  	return nil
    61  }
    62  
    63  func resourceAwsRoute53DelegationSetRead(d *schema.ResourceData, meta interface{}) error {
    64  	r53 := meta.(*AWSClient).r53conn
    65  
    66  	input := &route53.GetReusableDelegationSetInput{
    67  		Id: aws.String(cleanDelegationSetId(d.Id())),
    68  	}
    69  	log.Printf("[DEBUG] Reading Route53 reusable delegation set: %#v", input)
    70  	out, err := r53.GetReusableDelegationSet(input)
    71  	if err != nil {
    72  		return err
    73  	}
    74  	log.Printf("[DEBUG] Route53 reusable delegation set received: %#v", out)
    75  
    76  	set := out.DelegationSet
    77  
    78  	d.SetId(cleanDelegationSetId(*set.Id))
    79  	d.Set("name_servers", expandNameServers(set.NameServers))
    80  
    81  	return nil
    82  }
    83  
    84  func resourceAwsRoute53DelegationSetDelete(d *schema.ResourceData, meta interface{}) error {
    85  	r53 := meta.(*AWSClient).r53conn
    86  
    87  	input := &route53.DeleteReusableDelegationSetInput{
    88  		Id: aws.String(cleanDelegationSetId(d.Id())),
    89  	}
    90  	log.Printf("[DEBUG] Deleting Route53 reusable delegation set: %#v", input)
    91  	_, err := r53.DeleteReusableDelegationSet(input)
    92  	return err
    93  }
    94  
    95  func expandNameServers(name_servers []*string) []string {
    96  	log.Printf("[DEBUG] Processing %d name servers: %#v...", len(name_servers), name_servers)
    97  	ns := make([]string, len(name_servers))
    98  	for i, server := range name_servers {
    99  		ns[i] = *server
   100  	}
   101  	sort.Strings(ns)
   102  	log.Printf("[DEBUG] Returning processed name servers: %#v", ns)
   103  	return ns
   104  }
   105  
   106  func cleanDelegationSetId(id string) string {
   107  	return strings.TrimPrefix(id, "/delegationset/")
   108  }