terraform/builtin/providers/dnsimple/resource_dnsimple_record.go

197 lines
4.5 KiB
Go
Raw Normal View History

2014-07-23 23:43:28 +02:00
package dnsimple
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/config"
"github.com/hashicorp/terraform/helper/diff"
"github.com/hashicorp/terraform/terraform"
2014-07-24 16:21:39 +02:00
"github.com/pearkes/dnsimple"
2014-07-23 23:43:28 +02:00
)
func resource_dnsimple_record_create(
s *terraform.InstanceState,
2014-09-18 01:33:24 +02:00
d *terraform.InstanceDiff,
meta interface{}) (*terraform.InstanceState, error) {
2014-07-23 23:43:28 +02:00
p := meta.(*ResourceProvider)
client := p.client
// Merge the diff into the state so that we have all the attributes
// properly.
rs := s.MergeDiff(d)
var err error
2014-07-24 16:21:39 +02:00
newRecord := dnsimple.ChangeRecord{
Name: rs.Attributes["name"],
Value: rs.Attributes["value"],
Type: rs.Attributes["type"],
2014-07-23 23:43:28 +02:00
}
if attr, ok := rs.Attributes["ttl"]; ok {
2014-07-24 16:21:39 +02:00
newRecord.Ttl = attr
2014-07-23 23:43:28 +02:00
}
log.Printf("[DEBUG] record create configuration: %#v", newRecord)
2014-07-24 16:21:39 +02:00
recId, err := client.CreateRecord(rs.Attributes["domain"], &newRecord)
2014-07-23 23:43:28 +02:00
if err != nil {
return nil, fmt.Errorf("Failed to create record: %s", err)
}
2014-07-24 16:21:39 +02:00
rs.ID = recId
2014-07-23 23:43:28 +02:00
log.Printf("[INFO] record ID: %s", rs.ID)
record, err := resource_dnsimple_record_retrieve(rs.Attributes["domain"], rs.ID, client)
2014-07-24 16:21:39 +02:00
if err != nil {
return nil, fmt.Errorf("Couldn't find record: %s", err)
}
return resource_dnsimple_record_update_state(rs, record)
2014-07-23 23:43:28 +02:00
}
func resource_dnsimple_record_update(
s *terraform.InstanceState,
2014-09-18 01:33:24 +02:00
d *terraform.InstanceDiff,
meta interface{}) (*terraform.InstanceState, error) {
2014-07-24 16:21:39 +02:00
p := meta.(*ResourceProvider)
client := p.client
rs := s.MergeDiff(d)
updateRecord := dnsimple.ChangeRecord{}
2014-07-23 23:43:28 +02:00
2014-07-24 16:21:39 +02:00
if attr, ok := d.Attributes["name"]; ok {
updateRecord.Name = attr.New
}
if attr, ok := d.Attributes["value"]; ok {
updateRecord.Value = attr.New
}
if attr, ok := d.Attributes["type"]; ok {
updateRecord.Type = attr.New
}
2014-07-23 23:43:28 +02:00
2014-07-24 16:21:39 +02:00
if attr, ok := d.Attributes["ttl"]; ok {
updateRecord.Ttl = attr.New
}
log.Printf("[DEBUG] record update configuration: %#v", updateRecord)
_, err := client.UpdateRecord(rs.Attributes["domain"], rs.ID, &updateRecord)
2014-07-24 16:21:39 +02:00
if err != nil {
return rs, fmt.Errorf("Failed to update record: %s", err)
2014-07-24 16:21:39 +02:00
}
record, err := resource_dnsimple_record_retrieve(rs.Attributes["domain"], rs.ID, client)
2014-07-24 16:21:39 +02:00
if err != nil {
return rs, fmt.Errorf("Couldn't find record: %s", err)
2014-07-24 16:21:39 +02:00
}
return resource_dnsimple_record_update_state(rs, record)
2014-07-23 23:43:28 +02:00
}
func resource_dnsimple_record_destroy(
s *terraform.InstanceState,
2014-07-23 23:43:28 +02:00
meta interface{}) error {
p := meta.(*ResourceProvider)
client := p.client
log.Printf("[INFO] Deleting record: %s, %s", s.Attributes["domain"], s.ID)
2014-07-23 23:43:28 +02:00
err := client.DestroyRecord(s.Attributes["domain"], s.ID)
2014-07-23 23:43:28 +02:00
if err != nil {
return fmt.Errorf("Error deleting record: %s", err)
}
return nil
}
func resource_dnsimple_record_refresh(
s *terraform.InstanceState,
meta interface{}) (*terraform.InstanceState, error) {
2014-07-23 23:43:28 +02:00
p := meta.(*ResourceProvider)
client := p.client
rec, err := resource_dnsimple_record_retrieve(s.Attributes["domain"], s.ID, client)
2014-07-23 23:43:28 +02:00
if err != nil {
return nil, err
}
return resource_dnsimple_record_update_state(s, rec)
}
func resource_dnsimple_record_diff(
s *terraform.InstanceState,
2014-07-23 23:43:28 +02:00
c *terraform.ResourceConfig,
2014-09-18 01:33:24 +02:00
meta interface{}) (*terraform.InstanceDiff, error) {
2014-07-23 23:43:28 +02:00
b := &diff.ResourceBuilder{
Attrs: map[string]diff.AttrType{
"domain": diff.AttrTypeCreate,
2014-07-24 16:21:39 +02:00
"name": diff.AttrTypeUpdate,
2014-07-23 23:43:28 +02:00
"value": diff.AttrTypeUpdate,
2014-07-24 16:21:39 +02:00
"ttl": diff.AttrTypeUpdate,
2014-07-23 23:43:28 +02:00
"type": diff.AttrTypeUpdate,
},
ComputedAttrs: []string{
"priority",
"domain_id",
"ttl",
},
ComputedAttrsUpdate: []string{
"hostname",
2014-07-23 23:43:28 +02:00
},
}
return b.Diff(s, c)
}
func resource_dnsimple_record_update_state(
s *terraform.InstanceState,
rec *dnsimple.Record) (*terraform.InstanceState, error) {
2014-07-23 23:43:28 +02:00
s.Attributes["name"] = rec.Name
s.Attributes["value"] = rec.Content
s.Attributes["type"] = rec.RecordType
2014-07-24 16:21:39 +02:00
s.Attributes["ttl"] = rec.StringTtl()
s.Attributes["priority"] = rec.StringPrio()
s.Attributes["domain_id"] = rec.StringDomainId()
2014-07-23 23:43:28 +02:00
2014-07-25 00:50:18 +02:00
if rec.Name == "" {
s.Attributes["hostname"] = s.Attributes["domain"]
} else {
s.Attributes["hostname"] = fmt.Sprintf("%s.%s", rec.Name, s.Attributes["domain"])
}
2014-07-23 23:43:28 +02:00
return s, nil
}
2014-07-24 16:21:39 +02:00
func resource_dnsimple_record_retrieve(domain string, id string, client *dnsimple.Client) (*dnsimple.Record, error) {
record, err := client.RetrieveRecord(domain, id)
2014-07-23 23:43:28 +02:00
if err != nil {
return nil, err
2014-07-23 23:43:28 +02:00
}
return record, nil
2014-07-23 23:43:28 +02:00
}
func resource_dnsimple_record_validation() *config.Validator {
return &config.Validator{
Required: []string{
"domain",
"name",
"value",
"type",
},
Optional: []string{
"ttl",
},
}
}