github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/powerdns/resource_powerdns_record.go (about)

     1  package powerdns
     2  
     3  import (
     4  	"log"
     5  
     6  	"fmt"
     7  
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  )
    10  
    11  func resourcePDNSRecord() *schema.Resource {
    12  	return &schema.Resource{
    13  		Create: resourcePDNSRecordCreate,
    14  		Read:   resourcePDNSRecordRead,
    15  		Delete: resourcePDNSRecordDelete,
    16  		Exists: resourcePDNSRecordExists,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"zone": {
    20  				Type:     schema.TypeString,
    21  				Required: true,
    22  				ForceNew: true,
    23  			},
    24  
    25  			"name": {
    26  				Type:     schema.TypeString,
    27  				Required: true,
    28  				ForceNew: true,
    29  			},
    30  
    31  			"type": {
    32  				Type:     schema.TypeString,
    33  				Required: true,
    34  				ForceNew: true,
    35  			},
    36  
    37  			"ttl": {
    38  				Type:     schema.TypeInt,
    39  				Required: true,
    40  				ForceNew: true,
    41  			},
    42  
    43  			"records": {
    44  				Type:     schema.TypeSet,
    45  				Elem:     &schema.Schema{Type: schema.TypeString},
    46  				Required: true,
    47  				ForceNew: true,
    48  				Set:      schema.HashString,
    49  			},
    50  		},
    51  	}
    52  }
    53  
    54  func resourcePDNSRecordCreate(d *schema.ResourceData, meta interface{}) error {
    55  	client := meta.(*Client)
    56  
    57  	rrSet := ResourceRecordSet{
    58  		Name: d.Get("name").(string),
    59  		Type: d.Get("type").(string),
    60  		TTL:  d.Get("ttl").(int),
    61  	}
    62  
    63  	zone := d.Get("zone").(string)
    64  	ttl := d.Get("ttl").(int)
    65  	recs := d.Get("records").(*schema.Set).List()
    66  
    67  	if len(recs) > 0 {
    68  		records := make([]Record, 0, len(recs))
    69  		for _, recContent := range recs {
    70  			records = append(records, Record{Name: rrSet.Name, Type: rrSet.Type, TTL: ttl, Content: recContent.(string)})
    71  		}
    72  		rrSet.Records = records
    73  
    74  		log.Printf("[DEBUG] Creating PowerDNS Record: %#v", rrSet)
    75  
    76  		recId, err := client.ReplaceRecordSet(zone, rrSet)
    77  		if err != nil {
    78  			return fmt.Errorf("Failed to create PowerDNS Record: %s", err)
    79  		}
    80  
    81  		d.SetId(recId)
    82  		log.Printf("[INFO] Created PowerDNS Record with ID: %s", d.Id())
    83  
    84  	} else {
    85  		log.Printf("[DEBUG] Deleting empty PowerDNS Record: %#v", rrSet)
    86  		err := client.DeleteRecordSet(zone, rrSet.Name, rrSet.Type)
    87  		if err != nil {
    88  			return fmt.Errorf("Failed to delete PowerDNS Record: %s", err)
    89  		}
    90  
    91  		d.SetId(rrSet.Id())
    92  	}
    93  
    94  	return resourcePDNSRecordRead(d, meta)
    95  }
    96  
    97  func resourcePDNSRecordRead(d *schema.ResourceData, meta interface{}) error {
    98  	client := meta.(*Client)
    99  
   100  	log.Printf("[DEBUG] Reading PowerDNS Record: %s", d.Id())
   101  	records, err := client.ListRecordsByID(d.Get("zone").(string), d.Id())
   102  	if err != nil {
   103  		return fmt.Errorf("Couldn't fetch PowerDNS Record: %s", err)
   104  	}
   105  
   106  	recs := make([]string, 0, len(records))
   107  	for _, r := range records {
   108  		recs = append(recs, r.Content)
   109  	}
   110  	d.Set("records", recs)
   111  
   112  	if len(records) > 0 {
   113  		d.Set("ttl", records[0].TTL)
   114  	}
   115  
   116  	return nil
   117  }
   118  
   119  func resourcePDNSRecordDelete(d *schema.ResourceData, meta interface{}) error {
   120  	client := meta.(*Client)
   121  
   122  	log.Printf("[INFO] Deleting PowerDNS Record: %s", d.Id())
   123  	err := client.DeleteRecordSetByID(d.Get("zone").(string), d.Id())
   124  
   125  	if err != nil {
   126  		return fmt.Errorf("Error deleting PowerDNS Record: %s", err)
   127  	}
   128  
   129  	return nil
   130  }
   131  
   132  func resourcePDNSRecordExists(d *schema.ResourceData, meta interface{}) (bool, error) {
   133  	zone := d.Get("zone").(string)
   134  	name := d.Get("name").(string)
   135  	tpe := d.Get("type").(string)
   136  
   137  	log.Printf("[INFO] Checking existence of PowerDNS Record: %s, %s", name, tpe)
   138  
   139  	client := meta.(*Client)
   140  	exists, err := client.RecordExists(zone, name, tpe)
   141  
   142  	if err != nil {
   143  		return false, fmt.Errorf("Error checking PowerDNS Record: %s", err)
   144  	} else {
   145  		return exists, nil
   146  	}
   147  }