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