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

     1  package dme
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  	"github.com/soniah/dnsmadeeasy"
    10  )
    11  
    12  func resourceDMERecord() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceDMERecordCreate,
    15  		Read:   resourceDMERecordRead,
    16  		Update: resourceDMERecordUpdate,
    17  		Delete: resourceDMERecordDelete,
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			// Use recordid for TF ID.
    21  			"domainid": &schema.Schema{
    22  				Type:     schema.TypeString,
    23  				Required: true,
    24  			},
    25  			"name": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Required: true,
    28  			},
    29  			"type": &schema.Schema{
    30  				Type:     schema.TypeString,
    31  				Required: true,
    32  			},
    33  			"value": &schema.Schema{
    34  				Type:     schema.TypeString,
    35  				Required: true,
    36  			},
    37  			"ttl": &schema.Schema{
    38  				Type:     schema.TypeInt,
    39  				Optional: true,
    40  			},
    41  			"mxLevel": &schema.Schema{
    42  				Type:     schema.TypeInt,
    43  				Optional: true,
    44  			},
    45  			"weight": &schema.Schema{
    46  				Type:     schema.TypeInt,
    47  				Optional: true,
    48  			},
    49  			"priority": &schema.Schema{
    50  				Type:     schema.TypeInt,
    51  				Optional: true,
    52  			},
    53  			"port": &schema.Schema{
    54  				Type:     schema.TypeInt,
    55  				Optional: true,
    56  			},
    57  			"keywords": &schema.Schema{
    58  				Type:     schema.TypeString,
    59  				Optional: true,
    60  			},
    61  			"title": &schema.Schema{
    62  				Type:     schema.TypeString,
    63  				Optional: true,
    64  			},
    65  			"hardLink": &schema.Schema{
    66  				Type:     schema.TypeBool,
    67  				Optional: true,
    68  			},
    69  			"redirectType": &schema.Schema{
    70  				Type:     schema.TypeString,
    71  				Optional: true,
    72  			},
    73  			"description": &schema.Schema{
    74  				Type:     schema.TypeString,
    75  				Optional: true,
    76  			},
    77  		},
    78  	}
    79  }
    80  
    81  func resourceDMERecordCreate(d *schema.ResourceData, meta interface{}) error {
    82  	client := meta.(*dnsmadeeasy.Client)
    83  
    84  	domainid := d.Get("domainid").(string)
    85  	log.Printf("[INFO] Creating record for domainid: %s", domainid)
    86  
    87  	cr := make(map[string]interface{})
    88  	if err := getAll(d, cr); err != nil {
    89  		return err
    90  	}
    91  	log.Printf("[DEBUG] record create configuration: %#v", cr)
    92  
    93  	result, err := client.CreateRecord(domainid, cr)
    94  	if err != nil {
    95  		return fmt.Errorf("Failed to create record: %s", err)
    96  	}
    97  
    98  	d.SetId(result)
    99  	log.Printf("[INFO] record ID: %s", d.Id())
   100  
   101  	return resourceDMERecordRead(d, meta)
   102  }
   103  
   104  func resourceDMERecordRead(d *schema.ResourceData, meta interface{}) error {
   105  	client := meta.(*dnsmadeeasy.Client)
   106  
   107  	domainid := d.Get("domainid").(string)
   108  	recordid := d.Id()
   109  	log.Printf("[INFO] Reading record for domainid: %s recordid: %s", domainid, recordid)
   110  
   111  	rec, err := client.ReadRecord(domainid, recordid)
   112  	if err != nil {
   113  		if strings.Contains(err.Error(), "Unable to find") {
   114  			d.SetId("")
   115  			return nil
   116  		}
   117  
   118  		return fmt.Errorf("Couldn't find record: %s", err)
   119  	}
   120  
   121  	return setAll(d, rec)
   122  }
   123  
   124  func resourceDMERecordUpdate(d *schema.ResourceData, meta interface{}) error {
   125  	client := meta.(*dnsmadeeasy.Client)
   126  
   127  	domainid := d.Get("domainid").(string)
   128  	recordid := d.Id()
   129  
   130  	cr := make(map[string]interface{})
   131  	if err := getAll(d, cr); err != nil {
   132  		return err
   133  	}
   134  	log.Printf("[DEBUG] record update configuration: %+#v", cr)
   135  
   136  	if _, err := client.UpdateRecord(domainid, recordid, cr); err != nil {
   137  		return fmt.Errorf("Error updating record: %s", err)
   138  	}
   139  
   140  	return resourceDMERecordRead(d, meta)
   141  }
   142  
   143  func resourceDMERecordDelete(d *schema.ResourceData, meta interface{}) error {
   144  	client := meta.(*dnsmadeeasy.Client)
   145  
   146  	domainid := d.Get("domainid").(string)
   147  	recordid := d.Id()
   148  	log.Printf("[INFO] Deleting record for domainid: %s recordid: %s", domainid, recordid)
   149  
   150  	if err := client.DeleteRecord(domainid, recordid); err != nil {
   151  		return fmt.Errorf("Error deleting record: %s", err)
   152  	}
   153  
   154  	return nil
   155  }
   156  
   157  func getAll(d *schema.ResourceData, cr map[string]interface{}) error {
   158  
   159  	if attr, ok := d.GetOk("name"); ok {
   160  		cr["name"] = attr.(string)
   161  	}
   162  	if attr, ok := d.GetOk("type"); ok {
   163  		cr["type"] = attr.(string)
   164  	}
   165  	if attr, ok := d.GetOk("ttl"); ok {
   166  		cr["ttl"] = int64(attr.(int))
   167  	}
   168  	if attr, ok := d.GetOk("value"); ok {
   169  		cr["value"] = attr.(string)
   170  	}
   171  
   172  	switch strings.ToUpper(d.Get("type").(string)) {
   173  	case "A", "CNAME", "ANAME", "TXT", "SPF", "NS", "PTR", "AAAA":
   174  		// all done
   175  	case "MX":
   176  		if attr, ok := d.GetOk("mxLevel"); ok {
   177  			cr["mxLevel"] = int64(attr.(int))
   178  		}
   179  	case "SRV":
   180  		if attr, ok := d.GetOk("priority"); ok {
   181  			cr["priority"] = int64(attr.(int))
   182  		}
   183  		if attr, ok := d.GetOk("weight"); ok {
   184  			cr["weight"] = int64(attr.(int))
   185  		}
   186  		if attr, ok := d.GetOk("port"); ok {
   187  			cr["port"] = int64(attr.(int))
   188  		}
   189  	case "HTTPRED":
   190  		if attr, ok := d.GetOk("hardLink"); ok && attr.(bool) {
   191  			cr["hardLink"] = "true"
   192  		}
   193  		if attr, ok := d.GetOk("redirectType"); ok {
   194  			cr["redirectType"] = attr.(string)
   195  		}
   196  		if attr, ok := d.GetOk("title"); ok {
   197  			cr["title"] = attr.(string)
   198  		}
   199  		if attr, ok := d.GetOk("keywords"); ok {
   200  			cr["keywords"] = attr.(string)
   201  		}
   202  		if attr, ok := d.GetOk("description"); ok {
   203  			cr["description"] = attr.(string)
   204  		}
   205  	default:
   206  		return fmt.Errorf("getAll: type not found")
   207  	}
   208  	return nil
   209  }
   210  
   211  func setAll(d *schema.ResourceData, rec *dnsmadeeasy.Record) error {
   212  	d.Set("type", rec.Type)
   213  	d.Set("name", rec.Name)
   214  	d.Set("ttl", rec.TTL)
   215  	d.Set("value", rec.Value)
   216  
   217  	switch rec.Type {
   218  	case "A", "CNAME", "ANAME", "TXT", "SPF", "NS", "PTR":
   219  		// all done
   220  	case "AAAA":
   221  		// overwrite value set above - DME ipv6 is lower case
   222  		d.Set("value", strings.ToLower(rec.Value))
   223  	case "MX":
   224  		d.Set("mxLevel", rec.MXLevel)
   225  	case "SRV":
   226  		d.Set("priority", rec.Priority)
   227  		d.Set("weight", rec.Weight)
   228  		d.Set("port", rec.Port)
   229  	case "HTTPRED":
   230  		d.Set("hardLink", rec.HardLink)
   231  		d.Set("redirectType", rec.RedirectType)
   232  		d.Set("title", rec.Title)
   233  		d.Set("keywords", rec.Keywords)
   234  		d.Set("description", rec.Description)
   235  	default:
   236  		return fmt.Errorf("setAll: type not found")
   237  	}
   238  	return nil
   239  }