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

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  	"google.golang.org/api/dns/v1"
    10  )
    11  
    12  func resourceDnsRecordSet() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceDnsRecordSetCreate,
    15  		Read:   resourceDnsRecordSetRead,
    16  		Delete: resourceDnsRecordSetDelete,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"name": &schema.Schema{
    20  				Type:     schema.TypeString,
    21  				Required: true,
    22  				ForceNew: true,
    23  			},
    24  
    25  			"managed_zone": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Required: true,
    28  				ForceNew: true,
    29  			},
    30  
    31  			"type": &schema.Schema{
    32  				Type:     schema.TypeString,
    33  				Required: true,
    34  				ForceNew: true,
    35  			},
    36  
    37  			"ttl": &schema.Schema{
    38  				Type:     schema.TypeInt,
    39  				Required: true,
    40  				ForceNew: true,
    41  			},
    42  
    43  			"rrdatas": &schema.Schema{
    44  				Type:     schema.TypeList,
    45  				Required: true,
    46  				ForceNew: true,
    47  				Elem: &schema.Schema{
    48  					Type: schema.TypeString,
    49  				},
    50  			},
    51  		},
    52  	}
    53  }
    54  
    55  func resourceDnsRecordSetCreate(d *schema.ResourceData, meta interface{}) error {
    56  	config := meta.(*Config)
    57  
    58  	zone := d.Get("managed_zone").(string)
    59  
    60  	rrdatasCount := d.Get("rrdatas.#").(int)
    61  
    62  	// Build the change
    63  	chg := &dns.Change{
    64  		Additions: []*dns.ResourceRecordSet{
    65  			&dns.ResourceRecordSet{
    66  				Name:    d.Get("name").(string),
    67  				Type:    d.Get("type").(string),
    68  				Ttl:     int64(d.Get("ttl").(int)),
    69  				Rrdatas: make([]string, rrdatasCount),
    70  			},
    71  		},
    72  	}
    73  
    74  	for i := 0; i < rrdatasCount; i++ {
    75  		rrdata := fmt.Sprintf("rrdatas.%d", i)
    76  		chg.Additions[0].Rrdatas[i] = d.Get(rrdata).(string)
    77  	}
    78  
    79  	log.Printf("[DEBUG] DNS Record create request: %#v", chg)
    80  	chg, err := config.clientDns.Changes.Create(config.Project, zone, chg).Do()
    81  	if err != nil {
    82  		return fmt.Errorf("Error creating DNS RecordSet: %s", err)
    83  	}
    84  
    85  	d.SetId(chg.Id)
    86  
    87  	w := &DnsChangeWaiter{
    88  		Service:     config.clientDns,
    89  		Change:      chg,
    90  		Project:     config.Project,
    91  		ManagedZone: zone,
    92  	}
    93  	state := w.Conf()
    94  	state.Delay = 10 * time.Second
    95  	state.Timeout = 10 * time.Minute
    96  	state.MinTimeout = 2 * time.Second
    97  	_, err = state.WaitForState()
    98  	if err != nil {
    99  		return fmt.Errorf("Error waiting for Google DNS change: %s", err)
   100  	}
   101  
   102  	return resourceDnsRecordSetRead(d, meta)
   103  }
   104  
   105  func resourceDnsRecordSetRead(d *schema.ResourceData, meta interface{}) error {
   106  	config := meta.(*Config)
   107  
   108  	zone := d.Get("managed_zone").(string)
   109  
   110  	// name and type are effectively the 'key'
   111  	name := d.Get("name").(string)
   112  	dnsType := d.Get("type").(string)
   113  
   114  	resp, err := config.clientDns.ResourceRecordSets.List(
   115  		config.Project, zone).Name(name).Type(dnsType).Do()
   116  	if err != nil {
   117  		return fmt.Errorf("Error reading DNS RecordSet: %#v", err)
   118  	}
   119  	if len(resp.Rrsets) == 0 {
   120  		// The resource doesn't exist anymore
   121  		d.SetId("")
   122  		return nil
   123  	}
   124  
   125  	if len(resp.Rrsets) > 1 {
   126  		return fmt.Errorf("Only expected 1 record set, got %d", len(resp.Rrsets))
   127  	}
   128  
   129  	d.Set("ttl", resp.Rrsets[0].Ttl)
   130  	d.Set("rrdatas", resp.Rrsets[0].Rrdatas)
   131  
   132  	return nil
   133  }
   134  
   135  func resourceDnsRecordSetDelete(d *schema.ResourceData, meta interface{}) error {
   136  	config := meta.(*Config)
   137  
   138  	zone := d.Get("managed_zone").(string)
   139  
   140  	rrdatasCount := d.Get("rrdatas.#").(int)
   141  
   142  	// Build the change
   143  	chg := &dns.Change{
   144  		Deletions: []*dns.ResourceRecordSet{
   145  			&dns.ResourceRecordSet{
   146  				Name:    d.Get("name").(string),
   147  				Type:    d.Get("type").(string),
   148  				Ttl:     int64(d.Get("ttl").(int)),
   149  				Rrdatas: make([]string, rrdatasCount),
   150  			},
   151  		},
   152  	}
   153  
   154  	for i := 0; i < rrdatasCount; i++ {
   155  		rrdata := fmt.Sprintf("rrdatas.%d", i)
   156  		chg.Deletions[0].Rrdatas[i] = d.Get(rrdata).(string)
   157  	}
   158  	log.Printf("[DEBUG] DNS Record delete request: %#v", chg)
   159  	chg, err := config.clientDns.Changes.Create(config.Project, zone, chg).Do()
   160  	if err != nil {
   161  		return fmt.Errorf("Error deleting DNS RecordSet: %s", err)
   162  	}
   163  
   164  	w := &DnsChangeWaiter{
   165  		Service:     config.clientDns,
   166  		Change:      chg,
   167  		Project:     config.Project,
   168  		ManagedZone: zone,
   169  	}
   170  	state := w.Conf()
   171  	state.Delay = 10 * time.Second
   172  	state.Timeout = 10 * time.Minute
   173  	state.MinTimeout = 2 * time.Second
   174  	_, err = state.WaitForState()
   175  	if err != nil {
   176  		return fmt.Errorf("Error waiting for Google DNS change: %s", err)
   177  	}
   178  
   179  	d.SetId("")
   180  	return nil
   181  }