github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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  		Importer: &schema.ResourceImporter{
    21  			State: schema.ImportStatePassthrough,
    22  		},
    23  
    24  		Schema: map[string]*schema.Schema{
    25  			"reference_name": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Optional: true,
    28  				ForceNew: true,
    29  			},
    30  
    31  			"name_servers": &schema.Schema{
    32  				Type:     schema.TypeList,
    33  				Elem:     &schema.Schema{Type: schema.TypeString},
    34  				Computed: true,
    35  			},
    36  		},
    37  	}
    38  }
    39  
    40  func resourceAwsRoute53DelegationSetCreate(d *schema.ResourceData, meta interface{}) error {
    41  	r53 := meta.(*AWSClient).r53conn
    42  
    43  	callerRef := resource.UniqueId()
    44  	if v, ok := d.GetOk("reference_name"); ok {
    45  		callerRef = strings.Join([]string{
    46  			v.(string), "-", callerRef,
    47  		}, "")
    48  	}
    49  	input := &route53.CreateReusableDelegationSetInput{
    50  		CallerReference: aws.String(callerRef),
    51  	}
    52  
    53  	log.Printf("[DEBUG] Creating Route53 reusable delegation set: %#v", input)
    54  	out, err := r53.CreateReusableDelegationSet(input)
    55  	if err != nil {
    56  		return err
    57  	}
    58  	log.Printf("[DEBUG] Route53 reusable delegation set created: %#v", out)
    59  
    60  	set := out.DelegationSet
    61  	d.SetId(cleanDelegationSetId(*set.Id))
    62  	d.Set("name_servers", expandNameServers(set.NameServers))
    63  	return nil
    64  }
    65  
    66  func resourceAwsRoute53DelegationSetRead(d *schema.ResourceData, meta interface{}) error {
    67  	r53 := meta.(*AWSClient).r53conn
    68  
    69  	input := &route53.GetReusableDelegationSetInput{
    70  		Id: aws.String(cleanDelegationSetId(d.Id())),
    71  	}
    72  	log.Printf("[DEBUG] Reading Route53 reusable delegation set: %#v", input)
    73  	out, err := r53.GetReusableDelegationSet(input)
    74  	if err != nil {
    75  		return err
    76  	}
    77  	log.Printf("[DEBUG] Route53 reusable delegation set received: %#v", out)
    78  
    79  	set := out.DelegationSet
    80  
    81  	d.SetId(cleanDelegationSetId(*set.Id))
    82  	d.Set("name_servers", expandNameServers(set.NameServers))
    83  
    84  	return nil
    85  }
    86  
    87  func resourceAwsRoute53DelegationSetDelete(d *schema.ResourceData, meta interface{}) error {
    88  	r53 := meta.(*AWSClient).r53conn
    89  
    90  	input := &route53.DeleteReusableDelegationSetInput{
    91  		Id: aws.String(cleanDelegationSetId(d.Id())),
    92  	}
    93  	log.Printf("[DEBUG] Deleting Route53 reusable delegation set: %#v", input)
    94  	_, err := r53.DeleteReusableDelegationSet(input)
    95  	return err
    96  }
    97  
    98  func expandNameServers(name_servers []*string) []string {
    99  	log.Printf("[DEBUG] Processing %d name servers: %#v...", len(name_servers), name_servers)
   100  	ns := make([]string, len(name_servers))
   101  	for i, server := range name_servers {
   102  		ns[i] = *server
   103  	}
   104  	sort.Strings(ns)
   105  	log.Printf("[DEBUG] Returning processed name servers: %#v", ns)
   106  	return ns
   107  }
   108  
   109  func cleanDelegationSetId(id string) string {
   110  	return strings.TrimPrefix(id, "/delegationset/")
   111  }