github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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": {
    22  				Type:     schema.TypeString,
    23  				Required: true,
    24  			},
    25  
    26  			"name": {
    27  				Type:     schema.TypeString,
    28  				Required: true,
    29  			},
    30  
    31  			"hostname": {
    32  				Type:     schema.TypeString,
    33  				Computed: true,
    34  			},
    35  
    36  			"type": {
    37  				Type:     schema.TypeString,
    38  				Required: true,
    39  				ForceNew: true,
    40  			},
    41  
    42  			"value": {
    43  				Type:     schema.TypeString,
    44  				Required: true,
    45  			},
    46  
    47  			"ttl": {
    48  				Type:     schema.TypeInt,
    49  				Optional: true,
    50  				Computed: true,
    51  			},
    52  
    53  			"priority": {
    54  				Type:     schema.TypeInt,
    55  				Optional: true,
    56  			},
    57  
    58  			"proxied": {
    59  				Default:  false,
    60  				Optional: true,
    61  				Type:     schema.TypeBool,
    62  			},
    63  
    64  			"zone_id": {
    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  	// Validate value based on type
    92  	if err := validateRecordName(newRecord.Type, newRecord.Content); err != nil {
    93  		return fmt.Errorf("Error validating record name %q: %s", newRecord.Name, err)
    94  	}
    95  
    96  	// Validate type
    97  	if err := validateRecordType(newRecord.Type, newRecord.Proxied); err != nil {
    98  		return fmt.Errorf("Error validating record type %q: %s", newRecord.Type, err)
    99  	}
   100  
   101  	zoneId, err := client.ZoneIDByName(newRecord.ZoneName)
   102  	if err != nil {
   103  		return fmt.Errorf("Error finding zone %q: %s", newRecord.ZoneName, err)
   104  	}
   105  
   106  	d.Set("zone_id", zoneId)
   107  	newRecord.ZoneID = zoneId
   108  
   109  	log.Printf("[DEBUG] CloudFlare Record create configuration: %#v", newRecord)
   110  
   111  	r, err := client.CreateDNSRecord(zoneId, newRecord)
   112  	if err != nil {
   113  		return fmt.Errorf("Failed to create record: %s", err)
   114  	}
   115  
   116  	// In the Event that the API returns an empty DNS Record, we verify that the
   117  	// ID returned is not the default ""
   118  	if r.Result.ID == "" {
   119  		return fmt.Errorf("Failed to find record in Creat response; Record was empty")
   120  	}
   121  
   122  	d.SetId(r.Result.ID)
   123  
   124  	log.Printf("[INFO] CloudFlare Record ID: %s", d.Id())
   125  
   126  	return resourceCloudFlareRecordRead(d, meta)
   127  }
   128  
   129  func resourceCloudFlareRecordRead(d *schema.ResourceData, meta interface{}) error {
   130  	client := meta.(*cloudflare.API)
   131  	domain := d.Get("domain").(string)
   132  
   133  	zoneId, err := client.ZoneIDByName(domain)
   134  	if err != nil {
   135  		return fmt.Errorf("Error finding zone %q: %s", domain, err)
   136  	}
   137  
   138  	record, err := client.DNSRecord(zoneId, d.Id())
   139  	if err != nil {
   140  		return err
   141  	}
   142  
   143  	d.SetId(record.ID)
   144  	d.Set("hostname", record.Name)
   145  	d.Set("type", record.Type)
   146  	d.Set("value", record.Content)
   147  	d.Set("ttl", record.TTL)
   148  	d.Set("priority", record.Priority)
   149  	d.Set("proxied", record.Proxied)
   150  	d.Set("zone_id", zoneId)
   151  
   152  	return nil
   153  }
   154  
   155  func resourceCloudFlareRecordUpdate(d *schema.ResourceData, meta interface{}) error {
   156  	client := meta.(*cloudflare.API)
   157  
   158  	updateRecord := cloudflare.DNSRecord{
   159  		ID:       d.Id(),
   160  		Type:     d.Get("type").(string),
   161  		Name:     d.Get("name").(string),
   162  		Content:  d.Get("value").(string),
   163  		ZoneName: d.Get("domain").(string),
   164  		Proxied:  false,
   165  	}
   166  
   167  	if priority, ok := d.GetOk("priority"); ok {
   168  		updateRecord.Priority = priority.(int)
   169  	}
   170  
   171  	if proxied, ok := d.GetOk("proxied"); ok {
   172  		updateRecord.Proxied = proxied.(bool)
   173  	}
   174  
   175  	if ttl, ok := d.GetOk("ttl"); ok {
   176  		updateRecord.TTL = ttl.(int)
   177  	}
   178  
   179  	zoneId, err := client.ZoneIDByName(updateRecord.ZoneName)
   180  	if err != nil {
   181  		return fmt.Errorf("Error finding zone %q: %s", updateRecord.ZoneName, err)
   182  	}
   183  
   184  	updateRecord.ZoneID = zoneId
   185  
   186  	log.Printf("[DEBUG] CloudFlare Record update configuration: %#v", updateRecord)
   187  	err = client.UpdateDNSRecord(zoneId, d.Id(), updateRecord)
   188  	if err != nil {
   189  		return fmt.Errorf("Failed to update CloudFlare Record: %s", err)
   190  	}
   191  
   192  	return resourceCloudFlareRecordRead(d, meta)
   193  }
   194  
   195  func resourceCloudFlareRecordDelete(d *schema.ResourceData, meta interface{}) error {
   196  	client := meta.(*cloudflare.API)
   197  	domain := d.Get("domain").(string)
   198  
   199  	zoneId, err := client.ZoneIDByName(domain)
   200  	if err != nil {
   201  		return fmt.Errorf("Error finding zone %q: %s", domain, err)
   202  	}
   203  
   204  	log.Printf("[INFO] Deleting CloudFlare Record: %s, %s", domain, d.Id())
   205  
   206  	err = client.DeleteDNSRecord(zoneId, d.Id())
   207  	if err != nil {
   208  		return fmt.Errorf("Error deleting CloudFlare Record: %s", err)
   209  	}
   210  
   211  	return nil
   212  }