github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/dyn/resource_dyn_record.go (about)

     1  package dyn
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"sync"
     7  
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  	"github.com/nesv/go-dynect/dynect"
    10  )
    11  
    12  var mutex = &sync.Mutex{}
    13  
    14  func resourceDynRecord() *schema.Resource {
    15  	return &schema.Resource{
    16  		Create: resourceDynRecordCreate,
    17  		Read:   resourceDynRecordRead,
    18  		Update: resourceDynRecordUpdate,
    19  		Delete: resourceDynRecordDelete,
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"zone": &schema.Schema{
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  				ForceNew: true,
    26  			},
    27  
    28  			"name": &schema.Schema{
    29  				Type:     schema.TypeString,
    30  				Required: true,
    31  				ForceNew: true,
    32  			},
    33  
    34  			"fqdn": &schema.Schema{
    35  				Type:     schema.TypeString,
    36  				Computed: true,
    37  			},
    38  
    39  			"type": &schema.Schema{
    40  				Type:     schema.TypeString,
    41  				Required: true,
    42  				ForceNew: true,
    43  			},
    44  
    45  			"value": &schema.Schema{
    46  				Type:     schema.TypeString,
    47  				Required: true,
    48  			},
    49  
    50  			"ttl": &schema.Schema{
    51  				Type:     schema.TypeString,
    52  				Optional: true,
    53  				Default:  "0", // 0 means use zone default
    54  			},
    55  		},
    56  	}
    57  }
    58  
    59  func resourceDynRecordCreate(d *schema.ResourceData, meta interface{}) error {
    60  	mutex.Lock()
    61  
    62  	client := meta.(*dynect.ConvenientClient)
    63  
    64  	record := &dynect.Record{
    65  		Name:  d.Get("name").(string),
    66  		Zone:  d.Get("zone").(string),
    67  		Type:  d.Get("type").(string),
    68  		TTL:   d.Get("ttl").(string),
    69  		Value: d.Get("value").(string),
    70  	}
    71  	log.Printf("[DEBUG] Dyn record create configuration: %#v", record)
    72  
    73  	// create the record
    74  	err := client.CreateRecord(record)
    75  	if err != nil {
    76  		mutex.Unlock()
    77  		return fmt.Errorf("Failed to create Dyn record: %s", err)
    78  	}
    79  
    80  	// publish the zone
    81  	err = client.PublishZone(record.Zone)
    82  	if err != nil {
    83  		mutex.Unlock()
    84  		return fmt.Errorf("Failed to publish Dyn zone: %s", err)
    85  	}
    86  
    87  	// get the record ID
    88  	err = client.GetRecordID(record)
    89  	if err != nil {
    90  		mutex.Unlock()
    91  		return fmt.Errorf("%s", err)
    92  	}
    93  	d.SetId(record.ID)
    94  
    95  	mutex.Unlock()
    96  	return resourceDynRecordRead(d, meta)
    97  }
    98  
    99  func resourceDynRecordRead(d *schema.ResourceData, meta interface{}) error {
   100  	mutex.Lock()
   101  	defer mutex.Unlock()
   102  
   103  	client := meta.(*dynect.ConvenientClient)
   104  
   105  	record := &dynect.Record{
   106  		ID:   d.Id(),
   107  		Name: d.Get("name").(string),
   108  		Zone: d.Get("zone").(string),
   109  		TTL:  d.Get("ttl").(string),
   110  		FQDN: d.Get("fqdn").(string),
   111  		Type: d.Get("type").(string),
   112  	}
   113  
   114  	err := client.GetRecord(record)
   115  	if err != nil {
   116  		return fmt.Errorf("Couldn't find Dyn record: %s", err)
   117  	}
   118  
   119  	d.Set("zone", record.Zone)
   120  	d.Set("fqdn", record.FQDN)
   121  	d.Set("name", record.Name)
   122  	d.Set("type", record.Type)
   123  	d.Set("ttl", record.TTL)
   124  	d.Set("value", record.Value)
   125  
   126  	return nil
   127  }
   128  
   129  func resourceDynRecordUpdate(d *schema.ResourceData, meta interface{}) error {
   130  	mutex.Lock()
   131  
   132  	client := meta.(*dynect.ConvenientClient)
   133  
   134  	record := &dynect.Record{
   135  		Name:  d.Get("name").(string),
   136  		Zone:  d.Get("zone").(string),
   137  		TTL:   d.Get("ttl").(string),
   138  		Type:  d.Get("type").(string),
   139  		Value: d.Get("value").(string),
   140  	}
   141  	log.Printf("[DEBUG] Dyn record update configuration: %#v", record)
   142  
   143  	// update the record
   144  	err := client.UpdateRecord(record)
   145  	if err != nil {
   146  		mutex.Unlock()
   147  		return fmt.Errorf("Failed to update Dyn record: %s", err)
   148  	}
   149  
   150  	// publish the zone
   151  	err = client.PublishZone(record.Zone)
   152  	if err != nil {
   153  		mutex.Unlock()
   154  		return fmt.Errorf("Failed to publish Dyn zone: %s", err)
   155  	}
   156  
   157  	// get the record ID
   158  	err = client.GetRecordID(record)
   159  	if err != nil {
   160  		mutex.Unlock()
   161  		return fmt.Errorf("%s", err)
   162  	}
   163  	d.SetId(record.ID)
   164  
   165  	mutex.Unlock()
   166  	return resourceDynRecordRead(d, meta)
   167  }
   168  
   169  func resourceDynRecordDelete(d *schema.ResourceData, meta interface{}) error {
   170  	mutex.Lock()
   171  	defer mutex.Unlock()
   172  
   173  	client := meta.(*dynect.ConvenientClient)
   174  
   175  	record := &dynect.Record{
   176  		ID:   d.Id(),
   177  		Name: d.Get("name").(string),
   178  		Zone: d.Get("zone").(string),
   179  		FQDN: d.Get("fqdn").(string),
   180  		Type: d.Get("type").(string),
   181  	}
   182  
   183  	log.Printf("[INFO] Deleting Dyn record: %s, %s", record.FQDN, record.ID)
   184  
   185  	// delete the record
   186  	err := client.DeleteRecord(record)
   187  	if err != nil {
   188  		return fmt.Errorf("Failed to delete Dyn record: %s", err)
   189  	}
   190  
   191  	// publish the zone
   192  	err = client.PublishZone(record.Zone)
   193  	if err != nil {
   194  		return fmt.Errorf("Failed to publish Dyn zone: %s", err)
   195  	}
   196  
   197  	return nil
   198  }