Skip to content

Commit

Permalink
ultradns: generalize config loading, update to new sdk interface
Browse files Browse the repository at this point in the history
  • Loading branch information
josephholsten committed Mar 18, 2016
1 parent 807cd60 commit 0fa82c0
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 93 deletions.
3 changes: 2 additions & 1 deletion builtin/providers/ultradns/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (
"github.com/Ensighten/udnssdk"
)

// Config collects the connection service-endpoint and credentials
type Config struct {
Username string
Password string
BaseURL string
}

// Client() returns a new client for accessing UltraDNS.
// Client returns a new client for accessing UltraDNS.
func (c *Config) Client() (*udnssdk.Client, error) {
client, err := udnssdk.NewClient(c.Username, c.Password, c.BaseURL)

Expand Down
206 changes: 121 additions & 85 deletions builtin/providers/ultradns/resource_ultradns_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,96 @@ package ultradns

import (
"fmt"
"github.com/Ensighten/udnssdk"
"github.com/hashicorp/terraform/helper/schema"
"log"
"strconv"
"strings"

"github.com/Ensighten/udnssdk"
"github.com/hashicorp/terraform/helper/schema"
)

type rRSetResource struct {
OwnerName string
RRType string
RData []string
TTL int
Profile *udnssdk.StringProfile
Zone string
}

func newRRSetResource(d *schema.ResourceData) (rRSetResource, error) {
r := rRSetResource{}

if attr, ok := d.GetOk("name"); ok {
r.OwnerName = attr.(string)
}

if attr, ok := d.GetOk("type"); ok {
r.RRType = attr.(string)
}

if attr, ok := d.GetOk("zone"); ok {
r.Zone = attr.(string)
}

if attr, ok := d.GetOk("rdata"); ok {
rdata := attr.([]interface{})
r.RData = make([]string, len(rdata))
for i, j := range rdata {
r.RData[i] = j.(string)
}
}

if attr, ok := d.GetOk("ttl"); ok {
r.TTL, _ = strconv.Atoi(attr.(string))
}

return r, nil
}

func (r rRSetResource) RRSetKey() udnssdk.RRSetKey {
return udnssdk.RRSetKey{
Zone: r.Zone,
Type: r.RRType,
Name: r.OwnerName,
}
}

func (r rRSetResource) RRSet() udnssdk.RRSet {
return udnssdk.RRSet{
OwnerName: r.OwnerName,
RRType: r.RRType,
RData: r.RData,
TTL: r.TTL,
}
}

func (r rRSetResource) ID() string {
return fmt.Sprintf("%s.%s", r.OwnerName, r.Zone)
}

func populateResourceDataFromRRSet(r udnssdk.RRSet, d *schema.ResourceData) error {
zone := d.Get("zone")
// ttl
d.Set("ttl", r.TTL)
// rdata
err := d.Set("rdata", r.RData)
if err != nil {
return fmt.Errorf("ultradns_record.rdata set failed: %#v", err)
}
// hostname
if r.OwnerName == "" {
d.Set("hostname", zone)
} else {
if strings.HasSuffix(r.OwnerName, ".") {
d.Set("hostname", r.OwnerName)
} else {
d.Set("hostname", fmt.Sprintf("%s.%s", r.OwnerName, zone))
}
}
return nil
}

func resourceUltraDNSRecord() *schema.Resource {
return &schema.Resource{
Create: resourceUltraDNSRecordCreate,
Expand All @@ -17,23 +100,17 @@ func resourceUltraDNSRecord() *schema.Resource {
Delete: resourceUltraDNSRecordDelete,

Schema: map[string]*schema.Schema{
// Required
"zone": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"hostname": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},

"type": &schema.Schema{
Type: schema.TypeString,
Required: true,
Expand All @@ -44,49 +121,50 @@ func resourceUltraDNSRecord() *schema.Resource {
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},

// Optional
"ttl": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "3600",
},
// Computed
"hostname": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceUltraDNSRecordCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*udnssdk.Client)
newRecord := &udnssdk.RRSet{
OwnerName: d.Get("name").(string),
RRType: d.Get("type").(string),
}
rdata := d.Get("rdata").([]interface{})
rdatas := make([]string, len(rdata))
for i, j := range rdata {
rdatas[i] = j.(string)
}
newRecord.RData = rdatas
ttl := d.Get("ttl").(string)
newRecord.TTL, _ = strconv.Atoi(ttl)

log.Printf("[DEBUG] UltraDNS RRSet create configuration: %#v", newRecord)
r, err := newRRSetResource(d)
if err != nil {
return err
}

_, err := client.RRSets.CreateRRSet(d.Get("zone").(string), *newRecord)
recId := fmt.Sprintf("%s.%s", d.Get("name").(string), d.Get("zone").(string))
log.Printf("[INFO] ultradns_record create: %#v", r.RRSet())
_, err = client.RRSets.Create(r.RRSetKey(), r.RRSet())
if err != nil {
return fmt.Errorf("Failed to create UltraDNS RRSet: %s", err)
}

d.SetId(recId)
log.Printf("[INFO] record ID: %s", d.Id())
d.SetId(r.ID())
log.Printf("[INFO] ultradns_record.id: %s", d.Id())

return resourceUltraDNSRecordRead(d, meta)
}

func resourceUltraDNSRecordRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*udnssdk.Client)

rrsets, _, err := client.RRSets.GetRRSets(d.Get("zone").(string), d.Get("name").(string), d.Get("type").(string))
r, err := newRRSetResource(d)
if err != nil {
return err
}

rrsets, err := client.RRSets.Select(r.RRSetKey())
if err != nil {
uderr, ok := err.(*udnssdk.ErrorResponseList)
if ok {
Expand All @@ -95,64 +173,28 @@ func resourceUltraDNSRecordRead(d *schema.ResourceData, meta interface{}) error
if r.ErrorCode == 70002 {
d.SetId("")
return nil
} else {
return fmt.Errorf("Couldn't find UltraDNS RRSet: %s", err)
}
return fmt.Errorf("ultradns_record not found: %s", err)
}
} else {
return fmt.Errorf("Couldn't find UltraDNS RRSet: %s", err)
}
return fmt.Errorf("ultradns_record not found: %s", err)
}
rec := rrsets[0]
err = d.Set("rdata", rec.RData)
if err != nil {
return fmt.Errorf("[DEBUG] Error setting records: %#v", err)
}
d.Set("ttl", rec.TTL)

if rec.OwnerName == "" {
d.Set("hostname", d.Get("zone").(string))
} else {
if strings.HasSuffix(rec.OwnerName, ".") {
d.Set("hostname", rec.OwnerName)
} else {
d.Set("hostname", fmt.Sprintf("%s.%s", rec.OwnerName, d.Get("zone").(string)))
}
}
return nil
return populateResourceDataFromRRSet(rec, d)
}

func resourceUltraDNSRecordUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*udnssdk.Client)

updateRecord := &udnssdk.RRSet{}

if attr, ok := d.GetOk("name"); ok {
updateRecord.OwnerName = attr.(string)
}

if attr, ok := d.GetOk("type"); ok {
updateRecord.RRType = attr.(string)
}

if attr, ok := d.GetOk("rdata"); ok {
rdata := attr.([]interface{})
rdatas := make([]string, len(rdata))
for i, j := range rdata {
rdatas[i] = j.(string)
}
updateRecord.RData = rdatas
}

if attr, ok := d.GetOk("ttl"); ok {
updateRecord.TTL, _ = strconv.Atoi(attr.(string))
r, err := newRRSetResource(d)
if err != nil {
return err
}

log.Printf("[DEBUG] UltraDNS RRSet update configuration: %#v", updateRecord)

_, err := client.RRSets.UpdateRRSet(d.Get("zone").(string), *updateRecord)
log.Printf("[INFO] ultradns_record update: %#v", r.RRSet())
_, err = client.RRSets.Update(r.RRSetKey(), r.RRSet())
if err != nil {
return fmt.Errorf("Failed to update UltraDNS RRSet: %s", err)
return fmt.Errorf("ultradns_record update failed: %s", err)
}

return resourceUltraDNSRecordRead(d, meta)
Expand All @@ -161,21 +203,15 @@ func resourceUltraDNSRecordUpdate(d *schema.ResourceData, meta interface{}) erro
func resourceUltraDNSRecordDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*udnssdk.Client)

log.Printf("[INFO] Deleting UltraDNS RRSet: %s, %s", d.Get("zone").(string), d.Id())
deleteRecord := &udnssdk.RRSet{}

if attr, ok := d.GetOk("name"); ok {
deleteRecord.OwnerName = attr.(string)
}

if attr, ok := d.GetOk("type"); ok {
deleteRecord.RRType = attr.(string)
r, err := newRRSetResource(d)
if err != nil {
return err
}

_, err := client.RRSets.DeleteRRSet(d.Get("zone").(string), *deleteRecord)

log.Printf("[INFO] ultradns_record delete: %#v", r.RRSet())
_, err = client.RRSets.Delete(r.RRSetKey())
if err != nil {
return fmt.Errorf("Error deleting UltraDNS RRSet: %s", err)
return fmt.Errorf("ultradns_record delete failed: %s", err)
}

return nil
Expand Down
26 changes: 19 additions & 7 deletions builtin/providers/ultradns/resource_ultradns_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestAccUltraDNSRecord_Basic(t *testing.T) {
CheckDestroy: testAccCheckUltraDNSRecordDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: fmt.Sprintf(testAccCheckUltraDNSRecordConfig_basic, domain),
Config: fmt.Sprintf(testAccCheckUltraDNSRecordConfigBasic, domain),
Check: resource.ComposeTestCheckFunc(
testAccCheckUltraDNSRecordExists("ultradns_record.foobar", &record),
testAccCheckUltraDNSRecordAttributes(&record),
Expand All @@ -46,7 +46,7 @@ func TestAccUltraDNSRecord_Updated(t *testing.T) {
CheckDestroy: testAccCheckUltraDNSRecordDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: fmt.Sprintf(testAccCheckUltraDNSRecordConfig_basic, domain),
Config: fmt.Sprintf(testAccCheckUltraDNSRecordConfigBasic, domain),
Check: resource.ComposeTestCheckFunc(
testAccCheckUltraDNSRecordExists("ultradns_record.foobar", &record),
testAccCheckUltraDNSRecordAttributes(&record),
Expand All @@ -59,7 +59,7 @@ func TestAccUltraDNSRecord_Updated(t *testing.T) {
),
},
resource.TestStep{
Config: fmt.Sprintf(testAccCheckUltraDNSRecordConfig_new_value, domain),
Config: fmt.Sprintf(testAccCheckUltraDNSRecordConfigNewValue, domain),
Check: resource.ComposeTestCheckFunc(
testAccCheckUltraDNSRecordExists("ultradns_record.foobar", &record),
testAccCheckUltraDNSRecordAttributesUpdated(&record),
Expand All @@ -83,7 +83,13 @@ func testAccCheckUltraDNSRecordDestroy(s *terraform.State) error {
continue
}

_, _, err := client.RRSets.GetRRSets(rs.Primary.Attributes["zone"], rs.Primary.Attributes["name"], rs.Primary.Attributes["type"])
k := udnssdk.RRSetKey{
Zone: rs.Primary.Attributes["zone"],
Name: rs.Primary.Attributes["name"],
Type: rs.Primary.Attributes["type"],
}

_, err := client.RRSets.Select(k)

if err == nil {
return fmt.Errorf("Record still exists")
Expand Down Expand Up @@ -128,7 +134,13 @@ func testAccCheckUltraDNSRecordExists(n string, record *udnssdk.RRSet) resource.
}

client := testAccProvider.Meta().(*udnssdk.Client)
foundRecord, _, err := client.RRSets.GetRRSets(rs.Primary.Attributes["zone"], rs.Primary.Attributes["name"], rs.Primary.Attributes["type"])
k := udnssdk.RRSetKey{
Zone: rs.Primary.Attributes["zone"],
Name: rs.Primary.Attributes["name"],
Type: rs.Primary.Attributes["type"],
}

foundRecord, err := client.RRSets.Select(k)

if err != nil {
return err
Expand All @@ -144,7 +156,7 @@ func testAccCheckUltraDNSRecordExists(n string, record *udnssdk.RRSet) resource.
}
}

const testAccCheckUltraDNSRecordConfig_basic = `
const testAccCheckUltraDNSRecordConfigBasic = `
resource "ultradns_record" "foobar" {
zone = "%s"
Expand All @@ -154,7 +166,7 @@ resource "ultradns_record" "foobar" {
ttl = 3600
}`

const testAccCheckUltraDNSRecordConfig_new_value = `
const testAccCheckUltraDNSRecordConfigNewValue = `
resource "ultradns_record" "foobar" {
zone = "%s"
Expand Down

0 comments on commit 0fa82c0

Please sign in to comment.