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
import (
"fmt"
"log"
"os"
"github.com/rubyist/go-dnsimple"
"github.com/pearkes/dnsimple"
)
type Config struct {
@ -14,7 +15,7 @@ type Config struct {
// 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,
// we need to override the values passed in here.
@ -25,7 +26,11 @@ func (c *Config) Client() (*dnsimple.DNSimpleClient, error) {
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)

View File

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

View File

@ -3,12 +3,11 @@ package dnsimple
import (
"fmt"
"os"
"strconv"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/rubyist/go-dnsimple"
"github.com/pearkes/dnsimple"
)
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 {
client := testAccProvider.client
@ -45,12 +83,7 @@ func testAccCheckDNSimpleRecordDestroy(s *terraform.State) error {
continue
}
intId, err := strconv.Atoi(rs.ID)
if err != nil {
return err
}
_, err = client.RetrieveRecord(rs.Attributes["domain"], intId)
_, err := client.RetrieveRecord(rs.Attributes["domain"], rs.ID)
if err == nil {
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 {
return func(s *terraform.State) error {
rs, ok := s.Resources[n]
@ -85,18 +129,13 @@ func testAccCheckDNSimpleRecordExists(n string, record *dnsimple.Record) resourc
client := testAccProvider.client
intId, err := strconv.Atoi(rs.ID)
if err != nil {
return err
}
foundRecord, err := client.RetrieveRecord(rs.Attributes["domain"], intId)
foundRecord, err := client.RetrieveRecord(rs.Attributes["domain"], rs.ID)
if err != nil {
return err
}
if strconv.Itoa(foundRecord.Id) != rs.ID {
if foundRecord.StringId() != rs.ID {
return fmt.Errorf("Record not found")
}
@ -115,3 +154,13 @@ resource "dnsimple_record" "foobar" {
type = "A"
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/terraform"
"github.com/rubyist/go-dnsimple"
"github.com/pearkes/dnsimple"
)
type ResourceProvider struct {
Config Config
client *dnsimple.DNSimpleClient
client *dnsimple.Client
}
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 == "" {
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.")
}
}