dnsimple: fix for new library

This commit is contained in:
Jack Pearkes 2014-07-24 10:21:39 -04:00
parent a76252e9d8
commit 2f4ea10349
5 changed files with 135 additions and 54 deletions

View File

@ -1,10 +1,11 @@
package dnsimple package dnsimple
import ( import (
"fmt"
"log" "log"
"os" "os"
"github.com/rubyist/go-dnsimple" "github.com/pearkes/dnsimple"
) )
type Config struct { type Config struct {
@ -14,7 +15,7 @@ type Config struct {
// Client() returns a new client for accessing heroku. // Client() returns a new client for accessing heroku.
// //
func (c *Config) Client() (*dnsimple.DNSimpleClient, error) { func (c *Config) Client() (*dnsimple.Client, error) {
// If we have env vars set (like in the acc) tests, // If we have env vars set (like in the acc) tests,
// we need to override the values passed in here. // we need to override the values passed in here.
@ -25,7 +26,11 @@ func (c *Config) Client() (*dnsimple.DNSimpleClient, error) {
c.Token = v c.Token = v
} }
client := dnsimple.NewClient(c.Token, c.Email) client, err := dnsimple.NewClient(c.Email, c.Token)
if err != nil {
return nil, fmt.Errorf("Error setting up client: %s", err)
}
log.Printf("[INFO] DNSimple Client configured for user: %s", client.Email) log.Printf("[INFO] DNSimple Client configured for user: %s", client.Email)

View File

@ -3,12 +3,11 @@ package dnsimple
import ( import (
"fmt" "fmt"
"log" "log"
"strconv"
"github.com/hashicorp/terraform/helper/config" "github.com/hashicorp/terraform/helper/config"
"github.com/hashicorp/terraform/helper/diff" "github.com/hashicorp/terraform/helper/diff"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/rubyist/go-dnsimple" "github.com/pearkes/dnsimple"
) )
func resource_dnsimple_record_create( func resource_dnsimple_record_create(
@ -24,42 +23,79 @@ func resource_dnsimple_record_create(
var err error var err error
newRecord := dnsimple.Record{ newRecord := dnsimple.ChangeRecord{
Name: rs.Attributes["name"], Name: rs.Attributes["name"],
Content: rs.Attributes["value"], Value: rs.Attributes["value"],
RecordType: rs.Attributes["type"], Type: rs.Attributes["type"],
} }
if attr, ok := rs.Attributes["ttl"]; ok { if attr, ok := rs.Attributes["ttl"]; ok {
newRecord.TTL, err = strconv.Atoi(attr) newRecord.Ttl = attr
if err != nil {
return nil, err
}
} }
log.Printf("[DEBUG] record create configuration: %#v", newRecord) log.Printf("[DEBUG] record create configuration: %#v", newRecord)
rec, err := client.CreateRecord(rs.Attributes["domain"], newRecord) recId, err := client.CreateRecord(rs.Attributes["domain"], &newRecord)
if err != nil { if err != nil {
return nil, fmt.Errorf("Failed to create record: %s", err) return nil, fmt.Errorf("Failed to create record: %s", err)
} }
rs.ID = strconv.Itoa(rec.Id) rs.ID = recId
log.Printf("[INFO] record ID: %s", rs.ID) log.Printf("[INFO] record ID: %s", rs.ID)
return resource_dnsimple_record_update_state(rs, &rec) record, err := resource_dnsimple_record_retrieve(s.Attributes["domain"], s.ID, client)
if err != nil {
return nil, fmt.Errorf("Couldn't find record: %s", err)
}
return resource_dnsimple_record_update_state(rs, record)
} }
func resource_dnsimple_record_update( func resource_dnsimple_record_update(
s *terraform.ResourceState, s *terraform.ResourceState,
d *terraform.ResourceDiff, d *terraform.ResourceDiff,
meta interface{}) (*terraform.ResourceState, error) { meta interface{}) (*terraform.ResourceState, error) {
p := meta.(*ResourceProvider)
client := p.client
rs := s.MergeDiff(d)
panic("Cannot update record") updateRecord := dnsimple.ChangeRecord{}
return nil, nil record, err := resource_dnsimple_record_retrieve(s.Attributes["domain"], s.ID, client)
if err != nil {
return nil, fmt.Errorf("Couldn't find record: %s", err)
}
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
}
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)
if err != nil {
return nil, fmt.Errorf("Failed to update record: %s", err)
}
record, err = resource_dnsimple_record_retrieve(s.Attributes["domain"], s.ID, client)
if err != nil {
return nil, fmt.Errorf("Couldn't find record: %s", err)
}
return resource_dnsimple_record_update_state(rs, record)
} }
func resource_dnsimple_record_destroy( func resource_dnsimple_record_destroy(
@ -70,12 +106,8 @@ func resource_dnsimple_record_destroy(
log.Printf("[INFO] Deleting record: %s", s.ID) log.Printf("[INFO] Deleting record: %s", s.ID)
rec, err := resource_dnsimple_record_retrieve(s.Attributes["domain"], s.ID, client) err := client.DestroyRecord(s.ID)
if err != nil {
return err
}
err = rec.Delete(client)
if err != nil { if err != nil {
return fmt.Errorf("Error deleting record: %s", err) return fmt.Errorf("Error deleting record: %s", err)
} }
@ -105,9 +137,9 @@ func resource_dnsimple_record_diff(
b := &diff.ResourceBuilder{ b := &diff.ResourceBuilder{
Attrs: map[string]diff.AttrType{ Attrs: map[string]diff.AttrType{
"domain": diff.AttrTypeCreate, "domain": diff.AttrTypeCreate,
"name": diff.AttrTypeCreate, "name": diff.AttrTypeUpdate,
"value": diff.AttrTypeUpdate, "value": diff.AttrTypeUpdate,
"ttl": diff.AttrTypeCreate, "ttl": diff.AttrTypeUpdate,
"type": diff.AttrTypeUpdate, "type": diff.AttrTypeUpdate,
}, },
@ -127,20 +159,15 @@ func resource_dnsimple_record_update_state(
s.Attributes["name"] = rec.Name s.Attributes["name"] = rec.Name
s.Attributes["value"] = rec.Content s.Attributes["value"] = rec.Content
s.Attributes["type"] = rec.RecordType s.Attributes["type"] = rec.RecordType
s.Attributes["ttl"] = strconv.Itoa(rec.TTL) s.Attributes["ttl"] = rec.StringTtl()
s.Attributes["priority"] = strconv.Itoa(rec.Priority) s.Attributes["priority"] = rec.StringPrio()
s.Attributes["domain_id"] = strconv.Itoa(rec.DomainId) s.Attributes["domain_id"] = rec.StringDomainId()
return s, nil return s, nil
} }
func resource_dnsimple_record_retrieve(domain string, id string, client *dnsimple.DNSimpleClient) (*dnsimple.Record, error) { func resource_dnsimple_record_retrieve(domain string, id string, client *dnsimple.Client) (*dnsimple.Record, error) {
intId, err := strconv.Atoi(id) record, err := client.RetrieveRecord(domain, id)
if err != nil {
return nil, err
}
record, err := client.RetrieveRecord(domain, intId)
if err != nil { if err != nil {
return nil, fmt.Errorf("Error retrieving record: %s", err) return nil, fmt.Errorf("Error retrieving record: %s", err)
} }

View File

@ -3,12 +3,11 @@ package dnsimple
import ( import (
"fmt" "fmt"
"os" "os"
"strconv"
"testing" "testing"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/rubyist/go-dnsimple" "github.com/pearkes/dnsimple"
) )
func TestAccDNSimpleRecord_Basic(t *testing.T) { func TestAccDNSimpleRecord_Basic(t *testing.T) {
@ -37,6 +36,45 @@ func TestAccDNSimpleRecord_Basic(t *testing.T) {
}) })
} }
func TestAccDNSimpleRecord_Updated(t *testing.T) {
var record dnsimple.Record
domain := os.Getenv("DNSIMPLE_DOMAIN")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDNSimpleRecordDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: fmt.Sprintf(testAccCheckDNSimpleRecordConfig_basic, domain),
Check: resource.ComposeTestCheckFunc(
testAccCheckDNSimpleRecordExists("dnsimple_record.foobar", &record),
testAccCheckDNSimpleRecordAttributes(&record),
resource.TestCheckResourceAttr(
"dnsimple_record.foobar", "name", "terraform"),
resource.TestCheckResourceAttr(
"dnsimple_record.foobar", "domain", domain),
resource.TestCheckResourceAttr(
"dnsimple_record.foobar", "value", "192.168.0.10"),
),
},
resource.TestStep{
Config: fmt.Sprintf(testAccCheckDNSimpleRecordConfig_new_value, domain),
Check: resource.ComposeTestCheckFunc(
testAccCheckDNSimpleRecordExists("dnsimple_record.foobar", &record),
testAccCheckDNSimpleRecordAttributesUpdated(&record),
resource.TestCheckResourceAttr(
"dnsimple_record.foobar", "name", "terraform"),
resource.TestCheckResourceAttr(
"dnsimple_record.foobar", "domain", domain),
resource.TestCheckResourceAttr(
"dnsimple_record.foobar", "value", "192.168.0.11"),
),
},
},
})
}
func testAccCheckDNSimpleRecordDestroy(s *terraform.State) error { func testAccCheckDNSimpleRecordDestroy(s *terraform.State) error {
client := testAccProvider.client client := testAccProvider.client
@ -45,12 +83,7 @@ func testAccCheckDNSimpleRecordDestroy(s *terraform.State) error {
continue continue
} }
intId, err := strconv.Atoi(rs.ID) _, err := client.RetrieveRecord(rs.Attributes["domain"], rs.ID)
if err != nil {
return err
}
_, err = client.RetrieveRecord(rs.Attributes["domain"], intId)
if err == nil { if err == nil {
return fmt.Errorf("Record still exists") return fmt.Errorf("Record still exists")
@ -71,6 +104,17 @@ func testAccCheckDNSimpleRecordAttributes(record *dnsimple.Record) resource.Test
} }
} }
func testAccCheckDNSimpleRecordAttributesUpdated(record *dnsimple.Record) resource.TestCheckFunc {
return func(s *terraform.State) error {
if record.Content != "192.168.0.11" {
return fmt.Errorf("Bad content: %s", record.Content)
}
return nil
}
}
func testAccCheckDNSimpleRecordExists(n string, record *dnsimple.Record) resource.TestCheckFunc { func testAccCheckDNSimpleRecordExists(n string, record *dnsimple.Record) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
rs, ok := s.Resources[n] rs, ok := s.Resources[n]
@ -85,18 +129,13 @@ func testAccCheckDNSimpleRecordExists(n string, record *dnsimple.Record) resourc
client := testAccProvider.client client := testAccProvider.client
intId, err := strconv.Atoi(rs.ID) foundRecord, err := client.RetrieveRecord(rs.Attributes["domain"], rs.ID)
if err != nil {
return err
}
foundRecord, err := client.RetrieveRecord(rs.Attributes["domain"], intId)
if err != nil { if err != nil {
return err return err
} }
if strconv.Itoa(foundRecord.Id) != rs.ID { if foundRecord.StringId() != rs.ID {
return fmt.Errorf("Record not found") return fmt.Errorf("Record not found")
} }
@ -115,3 +154,13 @@ resource "dnsimple_record" "foobar" {
type = "A" type = "A"
ttl = 3600 ttl = 3600
}` }`
const testAccCheckDNSimpleRecordConfig_new_value = `
resource "dnsimple_record" "foobar" {
domain = "%s"
name = "terraform"
value = "192.168.0.11"
type = "A"
ttl = 3600
}`

View File

@ -5,13 +5,13 @@ import (
"github.com/hashicorp/terraform/helper/config" "github.com/hashicorp/terraform/helper/config"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/rubyist/go-dnsimple" "github.com/pearkes/dnsimple"
) )
type ResourceProvider struct { type ResourceProvider struct {
Config Config Config Config
client *dnsimple.DNSimpleClient client *dnsimple.Client
} }
func (p *ResourceProvider) Validate(c *terraform.ResourceConfig) ([]string, []error) { func (p *ResourceProvider) Validate(c *terraform.ResourceConfig) ([]string, []error) {

View File

@ -75,6 +75,6 @@ func testAccPreCheck(t *testing.T) {
} }
if v := os.Getenv("DNSIMPLE_DOMAIN"); v == "" { if v := os.Getenv("DNSIMPLE_DOMAIN"); v == "" {
t.Fatal("DNSIMPLE_DOMAIN must be set for acceptance tests. The domain is used to create and destroy record against.") t.Fatal("DNSIMPLE_DOMAIN must be set for acceptance tests. The domain is used to ` and destroy record against.")
} }
} }