github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/cloudflare/resource_cloudflare_record.go (about)

     1  package cloudflare
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/cloudflare/cloudflare-go"
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  )
    10  
    11  func resourceCloudFlareRecord() *schema.Resource {
    12  	return &schema.Resource{
    13  		Create: resourceCloudFlareRecordCreate,
    14  		Read:   resourceCloudFlareRecordRead,
    15  		Update: resourceCloudFlareRecordUpdate,
    16  		Delete: resourceCloudFlareRecordDelete,
    17  
    18  		SchemaVersion: 1,
    19  		MigrateState:  resourceCloudFlareRecordMigrateState,
    20  		Schema: map[string]*schema.Schema{
    21  			"domain": &schema.Schema{
    22  				Type:     schema.TypeString,
    23  				Required: true,
    24  			},
    25  
    26  			"name": &schema.Schema{
    27  				Type:     schema.TypeString,
    28  				Required: true,
    29  			},
    30  
    31  			"hostname": &schema.Schema{
    32  				Type:     schema.TypeString,
    33  				Computed: true,
    34  			},
    35  
    36  			"type": &schema.Schema{
    37  				Type:     schema.TypeString,
    38  				Required: true,
    39  				ForceNew: true,
    40  			},
    41  
    42  			"value": &schema.Schema{
    43  				Type:     schema.TypeString,
    44  				Required: true,
    45  			},
    46  
    47  			"ttl": &schema.Schema{
    48  				Type:     schema.TypeInt,
    49  				Optional: true,
    50  				Computed: true,
    51  			},
    52  
    53  			"priority": &schema.Schema{
    54  				Type:     schema.TypeInt,
    55  				Optional: true,
    56  			},
    57  
    58  			"proxied": &schema.Schema{
    59  				Default:  false,
    60  				Optional: true,
    61  				Type:     schema.TypeBool,
    62  			},
    63  
    64  			"zone_id": &schema.Schema{
    65  				Type:     schema.TypeString,
    66  				Computed: true,
    67  			},
    68  		},
    69  	}
    70  }
    71  
    72  func resourceCloudFlareRecordCreate(d *schema.ResourceData, meta interface{}) error {
    73  	client := meta.(*cloudflare.API)
    74  
    75  	newRecord := cloudflare.DNSRecord{
    76  		Type:     d.Get("type").(string),
    77  		Name:     d.Get("name").(string),
    78  		Content:  d.Get("value").(string),
    79  		Proxied:  d.Get("proxied").(bool),
    80  		ZoneName: d.Get("domain").(string),
    81  	}
    82  
    83  	if priority, ok := d.GetOk("priority"); ok {
    84  		newRecord.Priority = priority.(int)
    85  	}
    86  
    87  	if ttl, ok := d.GetOk("ttl"); ok {
    88  		newRecord.TTL = ttl.(int)
    89  	}
    90  
    91  	zoneId, err := client.ZoneIDByName(newRecord.ZoneName)
    92  	if err != nil {
    93  		return fmt.Errorf("Error finding zone %q: %s", newRecord.ZoneName, err)
    94  	}
    95  
    96  	d.Set("zone_id", zoneId)
    97  	newRecord.ZoneID = zoneId
    98  
    99  	log.Printf("[DEBUG] CloudFlare Record create configuration: %#v", newRecord)
   100  
   101  	r, err := client.CreateDNSRecord(zoneId, newRecord)
   102  	if err != nil {
   103  		return fmt.Errorf("Failed to create record: %s", err)
   104  	}
   105  
   106  	// In the Event that the API returns an empty DNS Record, we verify that the
   107  	// ID returned is not the default ""
   108  	if r.Result.ID == "" {
   109  		return fmt.Errorf("Failed to find record in Creat response; Record was empty")
   110  	}
   111  
   112  	d.SetId(r.Result.ID)
   113  
   114  	log.Printf("[INFO] CloudFlare Record ID: %s", d.Id())
   115  
   116  	return resourceCloudFlareRecordRead(d, meta)
   117  }
   118  
   119  func resourceCloudFlareRecordRead(d *schema.ResourceData, meta interface{}) error {
   120  	client := meta.(*cloudflare.API)
   121  	domain := d.Get("domain").(string)
   122  
   123  	zoneId, err := client.ZoneIDByName(domain)
   124  	if err != nil {
   125  		return fmt.Errorf("Error finding zone %q: %s", domain, err)
   126  	}
   127  
   128  	record, err := client.DNSRecord(zoneId, d.Id())
   129  	if err != nil {
   130  		return err
   131  	}
   132  
   133  	d.SetId(record.ID)
   134  	d.Set("hostname", record.Name)
   135  	d.Set("type", record.Type)
   136  	d.Set("value", record.Content)
   137  	d.Set("ttl", record.TTL)
   138  	d.Set("priority", record.Priority)
   139  	d.Set("proxied", record.Proxied)
   140  	d.Set("zone_id", zoneId)
   141  
   142  	return nil
   143  }
   144  
   145  func resourceCloudFlareRecordUpdate(d *schema.ResourceData, meta interface{}) error {
   146  	client := meta.(*cloudflare.API)
   147  
   148  	updateRecord := cloudflare.DNSRecord{
   149  		ID:       d.Id(),
   150  		Type:     d.Get("type").(string),
   151  		Name:     d.Get("name").(string),
   152  		Content:  d.Get("value").(string),
   153  		ZoneName: d.Get("domain").(string),
   154  		Proxied:  false,
   155  	}
   156  
   157  	if priority, ok := d.GetOk("priority"); ok {
   158  		updateRecord.Priority = priority.(int)
   159  	}
   160  
   161  	if proxied, ok := d.GetOk("proxied"); ok {
   162  		updateRecord.Proxied = proxied.(bool)
   163  	}
   164  
   165  	if ttl, ok := d.GetOk("ttl"); ok {
   166  		updateRecord.TTL = ttl.(int)
   167  	}
   168  
   169  	zoneId, err := client.ZoneIDByName(updateRecord.ZoneName)
   170  	if err != nil {
   171  		return fmt.Errorf("Error finding zone %q: %s", updateRecord.ZoneName, err)
   172  	}
   173  
   174  	updateRecord.ZoneID = zoneId
   175  
   176  	log.Printf("[DEBUG] CloudFlare Record update configuration: %#v", updateRecord)
   177  	err = client.UpdateDNSRecord(zoneId, d.Id(), updateRecord)
   178  	if err != nil {
   179  		return fmt.Errorf("Failed to update CloudFlare Record: %s", err)
   180  	}
   181  
   182  	return resourceCloudFlareRecordRead(d, meta)
   183  }
   184  
   185  func resourceCloudFlareRecordDelete(d *schema.ResourceData, meta interface{}) error {
   186  	client := meta.(*cloudflare.API)
   187  	domain := d.Get("domain").(string)
   188  
   189  	zoneId, err := client.ZoneIDByName(domain)
   190  	if err != nil {
   191  		return fmt.Errorf("Error finding zone %q: %s", domain, err)
   192  	}
   193  
   194  	log.Printf("[INFO] Deleting CloudFlare Record: %s, %s", domain, d.Id())
   195  
   196  	err = client.DeleteDNSRecord(zoneId, d.Id())
   197  	if err != nil {
   198  		return fmt.Errorf("Error deleting CloudFlare Record: %s", err)
   199  	}
   200  
   201  	return nil
   202  }