github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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  			"gtdLocation": &schema.Schema{
    78  				Type:     schema.TypeString,
    79  				Optional: true,
    80  			},
    81  		},
    82  	}
    83  }
    84  
    85  func resourceDMERecordCreate(d *schema.ResourceData, meta interface{}) error {
    86  	client := meta.(*dnsmadeeasy.Client)
    87  
    88  	domainid := d.Get("domainid").(string)
    89  	log.Printf("[INFO] Creating record for domainid: %s", domainid)
    90  
    91  	cr := make(map[string]interface{})
    92  	if err := getAll(d, cr); err != nil {
    93  		return err
    94  	}
    95  	log.Printf("[DEBUG] record create configuration: %#v", cr)
    96  
    97  	result, err := client.CreateRecord(domainid, cr)
    98  	if err != nil {
    99  		return fmt.Errorf("Failed to create record: %s", err)
   100  	}
   101  
   102  	d.SetId(result)
   103  	log.Printf("[INFO] record ID: %s", d.Id())
   104  
   105  	return resourceDMERecordRead(d, meta)
   106  }
   107  
   108  func resourceDMERecordRead(d *schema.ResourceData, meta interface{}) error {
   109  	client := meta.(*dnsmadeeasy.Client)
   110  
   111  	domainid := d.Get("domainid").(string)
   112  	recordid := d.Id()
   113  	log.Printf("[INFO] Reading record for domainid: %s recordid: %s", domainid, recordid)
   114  
   115  	rec, err := client.ReadRecord(domainid, recordid)
   116  	if err != nil {
   117  		if strings.Contains(err.Error(), "Unable to find") {
   118  			d.SetId("")
   119  			return nil
   120  		}
   121  
   122  		return fmt.Errorf("Couldn't find record: %s", err)
   123  	}
   124  
   125  	return setAll(d, rec)
   126  }
   127  
   128  func resourceDMERecordUpdate(d *schema.ResourceData, meta interface{}) error {
   129  	client := meta.(*dnsmadeeasy.Client)
   130  
   131  	domainid := d.Get("domainid").(string)
   132  	recordid := d.Id()
   133  
   134  	cr := make(map[string]interface{})
   135  	if err := getAll(d, cr); err != nil {
   136  		return err
   137  	}
   138  	log.Printf("[DEBUG] record update configuration: %+#v", cr)
   139  
   140  	if _, err := client.UpdateRecord(domainid, recordid, cr); err != nil {
   141  		return fmt.Errorf("Error updating record: %s", err)
   142  	}
   143  
   144  	return resourceDMERecordRead(d, meta)
   145  }
   146  
   147  func resourceDMERecordDelete(d *schema.ResourceData, meta interface{}) error {
   148  	client := meta.(*dnsmadeeasy.Client)
   149  
   150  	domainid := d.Get("domainid").(string)
   151  	recordid := d.Id()
   152  	log.Printf("[INFO] Deleting record for domainid: %s recordid: %s", domainid, recordid)
   153  
   154  	if err := client.DeleteRecord(domainid, recordid); err != nil {
   155  		return fmt.Errorf("Error deleting record: %s", err)
   156  	}
   157  
   158  	return nil
   159  }
   160  
   161  func getAll(d *schema.ResourceData, cr map[string]interface{}) error {
   162  
   163  	if attr, ok := d.GetOk("name"); ok {
   164  		cr["name"] = attr.(string)
   165  	}
   166  	if attr, ok := d.GetOk("type"); ok {
   167  		cr["type"] = attr.(string)
   168  	}
   169  	if attr, ok := d.GetOk("ttl"); ok {
   170  		cr["ttl"] = int64(attr.(int))
   171  	}
   172  	if attr, ok := d.GetOk("value"); ok {
   173  		cr["value"] = attr.(string)
   174  	}
   175  	if attr, ok := d.GetOk("gtdLocation"); ok {
   176  		cr["gtdLocation"] = attr.(string)
   177  	}
   178  
   179  	switch strings.ToUpper(d.Get("type").(string)) {
   180  	case "A", "CNAME", "ANAME", "TXT", "SPF", "NS", "PTR", "AAAA":
   181  		// all done
   182  	case "MX":
   183  		if attr, ok := d.GetOk("mxLevel"); ok {
   184  			cr["mxLevel"] = int64(attr.(int))
   185  		}
   186  	case "SRV":
   187  		if attr, ok := d.GetOk("priority"); ok {
   188  			cr["priority"] = int64(attr.(int))
   189  		}
   190  		if attr, ok := d.GetOk("weight"); ok {
   191  			cr["weight"] = int64(attr.(int))
   192  		}
   193  		if attr, ok := d.GetOk("port"); ok {
   194  			cr["port"] = int64(attr.(int))
   195  		}
   196  	case "HTTPRED":
   197  		if attr, ok := d.GetOk("hardLink"); ok && attr.(bool) {
   198  			cr["hardLink"] = "true"
   199  		}
   200  		if attr, ok := d.GetOk("redirectType"); ok {
   201  			cr["redirectType"] = attr.(string)
   202  		}
   203  		if attr, ok := d.GetOk("title"); ok {
   204  			cr["title"] = attr.(string)
   205  		}
   206  		if attr, ok := d.GetOk("keywords"); ok {
   207  			cr["keywords"] = attr.(string)
   208  		}
   209  		if attr, ok := d.GetOk("description"); ok {
   210  			cr["description"] = attr.(string)
   211  		}
   212  	default:
   213  		return fmt.Errorf("getAll: type not found")
   214  	}
   215  	return nil
   216  }
   217  
   218  func setAll(d *schema.ResourceData, rec *dnsmadeeasy.Record) error {
   219  	d.Set("type", rec.Type)
   220  	d.Set("name", rec.Name)
   221  	d.Set("ttl", rec.TTL)
   222  	d.Set("value", rec.Value)
   223  	// only set gtdLocation if it is given as this is optional.
   224  	if rec.GtdLocation != "" {
   225  		d.Set("gtdLocation", rec.GtdLocation)
   226  	}
   227  
   228  	switch rec.Type {
   229  	case "A", "CNAME", "ANAME", "TXT", "SPF", "NS", "PTR":
   230  		// all done
   231  	case "AAAA":
   232  		// overwrite value set above - DME ipv6 is lower case
   233  		d.Set("value", strings.ToLower(rec.Value))
   234  	case "MX":
   235  		d.Set("mxLevel", rec.MXLevel)
   236  	case "SRV":
   237  		d.Set("priority", rec.Priority)
   238  		d.Set("weight", rec.Weight)
   239  		d.Set("port", rec.Port)
   240  	case "HTTPRED":
   241  		d.Set("hardLink", rec.HardLink)
   242  		d.Set("redirectType", rec.RedirectType)
   243  		d.Set("title", rec.Title)
   244  		d.Set("keywords", rec.Keywords)
   245  		d.Set("description", rec.Description)
   246  	default:
   247  		return fmt.Errorf("setAll: type not found")
   248  	}
   249  	return nil
   250  }