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

     1  package dnsimple
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  	"github.com/pearkes/dnsimple"
     9  )
    10  
    11  func resourceDNSimpleRecord() *schema.Resource {
    12  	return &schema.Resource{
    13  		Create: resourceDNSimpleRecordCreate,
    14  		Read:   resourceDNSimpleRecordRead,
    15  		Update: resourceDNSimpleRecordUpdate,
    16  		Delete: resourceDNSimpleRecordDelete,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"domain": &schema.Schema{
    20  				Type:     schema.TypeString,
    21  				Required: true,
    22  				ForceNew: true,
    23  			},
    24  
    25  			"domain_id": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Computed: true,
    28  			},
    29  
    30  			"name": &schema.Schema{
    31  				Type:     schema.TypeString,
    32  				Required: true,
    33  			},
    34  
    35  			"hostname": &schema.Schema{
    36  				Type:     schema.TypeString,
    37  				Computed: true,
    38  			},
    39  
    40  			"type": &schema.Schema{
    41  				Type:     schema.TypeString,
    42  				Required: true,
    43  				ForceNew: true,
    44  			},
    45  
    46  			"value": &schema.Schema{
    47  				Type:     schema.TypeString,
    48  				Required: true,
    49  			},
    50  
    51  			"ttl": &schema.Schema{
    52  				Type:     schema.TypeString,
    53  				Optional: true,
    54  				Default:  "3600",
    55  			},
    56  
    57  			"priority": &schema.Schema{
    58  				Type:     schema.TypeString,
    59  				Computed: true,
    60  			},
    61  		},
    62  	}
    63  }
    64  
    65  func resourceDNSimpleRecordCreate(d *schema.ResourceData, meta interface{}) error {
    66  	client := meta.(*dnsimple.Client)
    67  
    68  	// Create the new record
    69  	newRecord := &dnsimple.ChangeRecord{
    70  		Name:  d.Get("name").(string),
    71  		Type:  d.Get("type").(string),
    72  		Value: d.Get("value").(string),
    73  	}
    74  
    75  	if ttl, ok := d.GetOk("ttl"); ok {
    76  		newRecord.Ttl = ttl.(string)
    77  	}
    78  
    79  	log.Printf("[DEBUG] DNSimple Record create configuration: %#v", newRecord)
    80  
    81  	recId, err := client.CreateRecord(d.Get("domain").(string), newRecord)
    82  
    83  	if err != nil {
    84  		return fmt.Errorf("Failed to create DNSimple Record: %s", err)
    85  	}
    86  
    87  	d.SetId(recId)
    88  	log.Printf("[INFO] record ID: %s", d.Id())
    89  
    90  	return resourceDNSimpleRecordRead(d, meta)
    91  }
    92  
    93  func resourceDNSimpleRecordRead(d *schema.ResourceData, meta interface{}) error {
    94  	client := meta.(*dnsimple.Client)
    95  
    96  	rec, err := client.RetrieveRecord(d.Get("domain").(string), d.Id())
    97  	if err != nil {
    98  		return fmt.Errorf("Couldn't find DNSimple Record: %s", err)
    99  	}
   100  
   101  	d.Set("domain_id", rec.StringDomainId())
   102  	d.Set("name", rec.Name)
   103  	d.Set("type", rec.RecordType)
   104  	d.Set("value", rec.Content)
   105  	d.Set("ttl", rec.StringTtl())
   106  	d.Set("priority", rec.StringPrio())
   107  
   108  	if rec.Name == "" {
   109  		d.Set("hostname", d.Get("domain").(string))
   110  	} else {
   111  		d.Set("hostname", fmt.Sprintf("%s.%s", rec.Name, d.Get("domain").(string)))
   112  	}
   113  
   114  	return nil
   115  }
   116  
   117  func resourceDNSimpleRecordUpdate(d *schema.ResourceData, meta interface{}) error {
   118  	client := meta.(*dnsimple.Client)
   119  
   120  	updateRecord := &dnsimple.ChangeRecord{}
   121  
   122  	if attr, ok := d.GetOk("name"); ok {
   123  		updateRecord.Name = attr.(string)
   124  	}
   125  
   126  	if attr, ok := d.GetOk("type"); ok {
   127  		updateRecord.Type = attr.(string)
   128  	}
   129  
   130  	if attr, ok := d.GetOk("value"); ok {
   131  		updateRecord.Value = attr.(string)
   132  	}
   133  
   134  	if attr, ok := d.GetOk("ttl"); ok {
   135  		updateRecord.Ttl = attr.(string)
   136  	}
   137  
   138  	log.Printf("[DEBUG] DNSimple Record update configuration: %#v", updateRecord)
   139  
   140  	_, err := client.UpdateRecord(d.Get("domain").(string), d.Id(), updateRecord)
   141  	if err != nil {
   142  		return fmt.Errorf("Failed to update DNSimple Record: %s", err)
   143  	}
   144  
   145  	return resourceDNSimpleRecordRead(d, meta)
   146  }
   147  
   148  func resourceDNSimpleRecordDelete(d *schema.ResourceData, meta interface{}) error {
   149  	client := meta.(*dnsimple.Client)
   150  
   151  	log.Printf("[INFO] Deleting DNSimple Record: %s, %s", d.Get("domain").(string), d.Id())
   152  
   153  	err := client.DestroyRecord(d.Get("domain").(string), d.Id())
   154  
   155  	if err != nil {
   156  		return fmt.Errorf("Error deleting DNSimple Record: %s", err)
   157  	}
   158  
   159  	return nil
   160  }