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

     1  package ultradns
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strconv"
     7  	"strings"
     8  
     9  	"github.com/Ensighten/udnssdk"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  type rRSetResource struct {
    14  	OwnerName string
    15  	RRType    string
    16  	RData     []string
    17  	TTL       int
    18  	Profile   *udnssdk.StringProfile
    19  	Zone      string
    20  }
    21  
    22  func newRRSetResource(d *schema.ResourceData) (rRSetResource, error) {
    23  	r := rRSetResource{}
    24  
    25  	if attr, ok := d.GetOk("name"); ok {
    26  		r.OwnerName = attr.(string)
    27  	}
    28  
    29  	if attr, ok := d.GetOk("type"); ok {
    30  		r.RRType = attr.(string)
    31  	}
    32  
    33  	if attr, ok := d.GetOk("zone"); ok {
    34  		r.Zone = attr.(string)
    35  	}
    36  
    37  	if attr, ok := d.GetOk("rdata"); ok {
    38  		rdata := attr.([]interface{})
    39  		r.RData = make([]string, len(rdata))
    40  		for i, j := range rdata {
    41  			r.RData[i] = j.(string)
    42  		}
    43  	}
    44  
    45  	if attr, ok := d.GetOk("ttl"); ok {
    46  		r.TTL, _ = strconv.Atoi(attr.(string))
    47  	}
    48  
    49  	return r, nil
    50  }
    51  
    52  func (r rRSetResource) RRSetKey() udnssdk.RRSetKey {
    53  	return udnssdk.RRSetKey{
    54  		Zone: r.Zone,
    55  		Type: r.RRType,
    56  		Name: r.OwnerName,
    57  	}
    58  }
    59  
    60  func (r rRSetResource) RRSet() udnssdk.RRSet {
    61  	return udnssdk.RRSet{
    62  		OwnerName: r.OwnerName,
    63  		RRType:    r.RRType,
    64  		RData:     r.RData,
    65  		TTL:       r.TTL,
    66  	}
    67  }
    68  
    69  func (r rRSetResource) ID() string {
    70  	return fmt.Sprintf("%s.%s", r.OwnerName, r.Zone)
    71  }
    72  
    73  func populateResourceDataFromRRSet(r udnssdk.RRSet, d *schema.ResourceData) error {
    74  	zone := d.Get("zone")
    75  	// ttl
    76  	d.Set("ttl", r.TTL)
    77  	// rdata
    78  	err := d.Set("rdata", r.RData)
    79  	if err != nil {
    80  		return fmt.Errorf("ultradns_record.rdata set failed: %#v", err)
    81  	}
    82  	// hostname
    83  	if r.OwnerName == "" {
    84  		d.Set("hostname", zone)
    85  	} else {
    86  		if strings.HasSuffix(r.OwnerName, ".") {
    87  			d.Set("hostname", r.OwnerName)
    88  		} else {
    89  			d.Set("hostname", fmt.Sprintf("%s.%s", r.OwnerName, zone))
    90  		}
    91  	}
    92  	return nil
    93  }
    94  
    95  func resourceUltraDNSRecord() *schema.Resource {
    96  	return &schema.Resource{
    97  		Create: resourceUltraDNSRecordCreate,
    98  		Read:   resourceUltraDNSRecordRead,
    99  		Update: resourceUltraDNSRecordUpdate,
   100  		Delete: resourceUltraDNSRecordDelete,
   101  
   102  		Schema: map[string]*schema.Schema{
   103  			// Required
   104  			"zone": &schema.Schema{
   105  				Type:     schema.TypeString,
   106  				Required: true,
   107  				ForceNew: true,
   108  			},
   109  			"name": &schema.Schema{
   110  				Type:     schema.TypeString,
   111  				Required: true,
   112  				ForceNew: true,
   113  			},
   114  			"type": &schema.Schema{
   115  				Type:     schema.TypeString,
   116  				Required: true,
   117  				ForceNew: true,
   118  			},
   119  			"rdata": &schema.Schema{
   120  				Type:     schema.TypeList,
   121  				Required: true,
   122  				Elem:     &schema.Schema{Type: schema.TypeString},
   123  			},
   124  			// Optional
   125  			"ttl": &schema.Schema{
   126  				Type:     schema.TypeString,
   127  				Optional: true,
   128  				Default:  "3600",
   129  			},
   130  			// Computed
   131  			"hostname": &schema.Schema{
   132  				Type:     schema.TypeString,
   133  				Computed: true,
   134  			},
   135  		},
   136  	}
   137  }
   138  
   139  func resourceUltraDNSRecordCreate(d *schema.ResourceData, meta interface{}) error {
   140  	client := meta.(*udnssdk.Client)
   141  
   142  	r, err := newRRSetResource(d)
   143  	if err != nil {
   144  		return err
   145  	}
   146  
   147  	log.Printf("[INFO] ultradns_record create: %#v", r.RRSet())
   148  	_, err = client.RRSets.Create(r.RRSetKey(), r.RRSet())
   149  	if err != nil {
   150  		return fmt.Errorf("Failed to create UltraDNS RRSet: %s", err)
   151  	}
   152  
   153  	d.SetId(r.ID())
   154  	log.Printf("[INFO] ultradns_record.id: %s", d.Id())
   155  
   156  	return resourceUltraDNSRecordRead(d, meta)
   157  }
   158  
   159  func resourceUltraDNSRecordRead(d *schema.ResourceData, meta interface{}) error {
   160  	client := meta.(*udnssdk.Client)
   161  
   162  	r, err := newRRSetResource(d)
   163  	if err != nil {
   164  		return err
   165  	}
   166  
   167  	rrsets, err := client.RRSets.Select(r.RRSetKey())
   168  	if err != nil {
   169  		uderr, ok := err.(*udnssdk.ErrorResponseList)
   170  		if ok {
   171  			for _, r := range uderr.Responses {
   172  				// 70002 means Records Not Found
   173  				if r.ErrorCode == 70002 {
   174  					d.SetId("")
   175  					return nil
   176  				}
   177  				return fmt.Errorf("ultradns_record not found: %s", err)
   178  			}
   179  		}
   180  		return fmt.Errorf("ultradns_record not found: %s", err)
   181  	}
   182  	rec := rrsets[0]
   183  	return populateResourceDataFromRRSet(rec, d)
   184  }
   185  
   186  func resourceUltraDNSRecordUpdate(d *schema.ResourceData, meta interface{}) error {
   187  	client := meta.(*udnssdk.Client)
   188  
   189  	r, err := newRRSetResource(d)
   190  	if err != nil {
   191  		return err
   192  	}
   193  
   194  	log.Printf("[INFO] ultradns_record update: %#v", r.RRSet())
   195  	_, err = client.RRSets.Update(r.RRSetKey(), r.RRSet())
   196  	if err != nil {
   197  		return fmt.Errorf("ultradns_record update failed: %s", err)
   198  	}
   199  
   200  	return resourceUltraDNSRecordRead(d, meta)
   201  }
   202  
   203  func resourceUltraDNSRecordDelete(d *schema.ResourceData, meta interface{}) error {
   204  	client := meta.(*udnssdk.Client)
   205  
   206  	r, err := newRRSetResource(d)
   207  	if err != nil {
   208  		return err
   209  	}
   210  
   211  	log.Printf("[INFO] ultradns_record delete: %#v", r.RRSet())
   212  	_, err = client.RRSets.Delete(r.RRSetKey())
   213  	if err != nil {
   214  		return fmt.Errorf("ultradns_record delete failed: %s", err)
   215  	}
   216  
   217  	return nil
   218  }