Skip to content

Commit 2f4ea10

Browse files
committed
dnsimple: fix for new library
1 parent a76252e commit 2f4ea10

5 files changed

Lines changed: 135 additions & 54 deletions

File tree

builtin/providers/dnsimple/config.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package dnsimple
22

33
import (
4+
"fmt"
45
"log"
56
"os"
67

7-
"github.com/rubyist/go-dnsimple"
8+
"github.com/pearkes/dnsimple"
89
)
910

1011
type Config struct {
@@ -14,7 +15,7 @@ type Config struct {
1415

1516
// Client() returns a new client for accessing heroku.
1617
//
17-
func (c *Config) Client() (*dnsimple.DNSimpleClient, error) {
18+
func (c *Config) Client() (*dnsimple.Client, error) {
1819

1920
// If we have env vars set (like in the acc) tests,
2021
// we need to override the values passed in here.
@@ -25,7 +26,11 @@ func (c *Config) Client() (*dnsimple.DNSimpleClient, error) {
2526
c.Token = v
2627
}
2728

28-
client := dnsimple.NewClient(c.Token, c.Email)
29+
client, err := dnsimple.NewClient(c.Email, c.Token)
30+
31+
if err != nil {
32+
return nil, fmt.Errorf("Error setting up client: %s", err)
33+
}
2934

3035
log.Printf("[INFO] DNSimple Client configured for user: %s", client.Email)
3136

builtin/providers/dnsimple/resource_dnsimple_record.go

Lines changed: 60 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ package dnsimple
33
import (
44
"fmt"
55
"log"
6-
"strconv"
76

87
"github.com/hashicorp/terraform/helper/config"
98
"github.com/hashicorp/terraform/helper/diff"
109
"github.com/hashicorp/terraform/terraform"
11-
"github.com/rubyist/go-dnsimple"
10+
"github.com/pearkes/dnsimple"
1211
)
1312

1413
func resource_dnsimple_record_create(
@@ -24,42 +23,79 @@ func resource_dnsimple_record_create(
2423

2524
var err error
2625

27-
newRecord := dnsimple.Record{
28-
Name: rs.Attributes["name"],
29-
Content: rs.Attributes["value"],
30-
RecordType: rs.Attributes["type"],
26+
newRecord := dnsimple.ChangeRecord{
27+
Name: rs.Attributes["name"],
28+
Value: rs.Attributes["value"],
29+
Type: rs.Attributes["type"],
3130
}
3231

3332
if attr, ok := rs.Attributes["ttl"]; ok {
34-
newRecord.TTL, err = strconv.Atoi(attr)
35-
if err != nil {
36-
return nil, err
37-
}
33+
newRecord.Ttl = attr
3834
}
3935

4036
log.Printf("[DEBUG] record create configuration: %#v", newRecord)
4137

42-
rec, err := client.CreateRecord(rs.Attributes["domain"], newRecord)
38+
recId, err := client.CreateRecord(rs.Attributes["domain"], &newRecord)
4339

4440
if err != nil {
4541
return nil, fmt.Errorf("Failed to create record: %s", err)
4642
}
4743

48-
rs.ID = strconv.Itoa(rec.Id)
49-
44+
rs.ID = recId
5045
log.Printf("[INFO] record ID: %s", rs.ID)
5146

52-
return resource_dnsimple_record_update_state(rs, &rec)
47+
record, err := resource_dnsimple_record_retrieve(s.Attributes["domain"], s.ID, client)
48+
if err != nil {
49+
return nil, fmt.Errorf("Couldn't find record: %s", err)
50+
}
51+
52+
return resource_dnsimple_record_update_state(rs, record)
5353
}
5454

5555
func resource_dnsimple_record_update(
5656
s *terraform.ResourceState,
5757
d *terraform.ResourceDiff,
5858
meta interface{}) (*terraform.ResourceState, error) {
59+
p := meta.(*ResourceProvider)
60+
client := p.client
61+
rs := s.MergeDiff(d)
62+
63+
updateRecord := dnsimple.ChangeRecord{}
5964

60-
panic("Cannot update record")
65+
record, err := resource_dnsimple_record_retrieve(s.Attributes["domain"], s.ID, client)
66+
if err != nil {
67+
return nil, fmt.Errorf("Couldn't find record: %s", err)
68+
}
69+
70+
if attr, ok := d.Attributes["name"]; ok {
71+
updateRecord.Name = attr.New
72+
}
73+
74+
if attr, ok := d.Attributes["value"]; ok {
75+
updateRecord.Value = attr.New
76+
}
77+
78+
if attr, ok := d.Attributes["type"]; ok {
79+
updateRecord.Type = attr.New
80+
}
6181

62-
return nil, nil
82+
if attr, ok := d.Attributes["ttl"]; ok {
83+
updateRecord.Ttl = attr.New
84+
}
85+
86+
log.Printf("[DEBUG] record update configuration: %#v", updateRecord)
87+
88+
_, err = client.UpdateRecord(rs.Attributes["domain"], rs.ID, &updateRecord)
89+
if err != nil {
90+
return nil, fmt.Errorf("Failed to update record: %s", err)
91+
}
92+
93+
record, err = resource_dnsimple_record_retrieve(s.Attributes["domain"], s.ID, client)
94+
if err != nil {
95+
return nil, fmt.Errorf("Couldn't find record: %s", err)
96+
}
97+
98+
return resource_dnsimple_record_update_state(rs, record)
6399
}
64100

65101
func resource_dnsimple_record_destroy(
@@ -70,12 +106,8 @@ func resource_dnsimple_record_destroy(
70106

71107
log.Printf("[INFO] Deleting record: %s", s.ID)
72108

73-
rec, err := resource_dnsimple_record_retrieve(s.Attributes["domain"], s.ID, client)
74-
if err != nil {
75-
return err
76-
}
109+
err := client.DestroyRecord(s.ID)
77110

78-
err = rec.Delete(client)
79111
if err != nil {
80112
return fmt.Errorf("Error deleting record: %s", err)
81113
}
@@ -105,9 +137,9 @@ func resource_dnsimple_record_diff(
105137
b := &diff.ResourceBuilder{
106138
Attrs: map[string]diff.AttrType{
107139
"domain": diff.AttrTypeCreate,
108-
"name": diff.AttrTypeCreate,
140+
"name": diff.AttrTypeUpdate,
109141
"value": diff.AttrTypeUpdate,
110-
"ttl": diff.AttrTypeCreate,
142+
"ttl": diff.AttrTypeUpdate,
111143
"type": diff.AttrTypeUpdate,
112144
},
113145

@@ -127,20 +159,15 @@ func resource_dnsimple_record_update_state(
127159
s.Attributes["name"] = rec.Name
128160
s.Attributes["value"] = rec.Content
129161
s.Attributes["type"] = rec.RecordType
130-
s.Attributes["ttl"] = strconv.Itoa(rec.TTL)
131-
s.Attributes["priority"] = strconv.Itoa(rec.Priority)
132-
s.Attributes["domain_id"] = strconv.Itoa(rec.DomainId)
162+
s.Attributes["ttl"] = rec.StringTtl()
163+
s.Attributes["priority"] = rec.StringPrio()
164+
s.Attributes["domain_id"] = rec.StringDomainId()
133165

134166
return s, nil
135167
}
136168

137-
func resource_dnsimple_record_retrieve(domain string, id string, client *dnsimple.DNSimpleClient) (*dnsimple.Record, error) {
138-
intId, err := strconv.Atoi(id)
139-
if err != nil {
140-
return nil, err
141-
}
142-
143-
record, err := client.RetrieveRecord(domain, intId)
169+
func resource_dnsimple_record_retrieve(domain string, id string, client *dnsimple.Client) (*dnsimple.Record, error) {
170+
record, err := client.RetrieveRecord(domain, id)
144171
if err != nil {
145172
return nil, fmt.Errorf("Error retrieving record: %s", err)
146173
}

builtin/providers/dnsimple/resource_dnsimple_record_test.go

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ package dnsimple
33
import (
44
"fmt"
55
"os"
6-
"strconv"
76
"testing"
87

98
"github.com/hashicorp/terraform/helper/resource"
109
"github.com/hashicorp/terraform/terraform"
11-
"github.com/rubyist/go-dnsimple"
10+
"github.com/pearkes/dnsimple"
1211
)
1312

1413
func TestAccDNSimpleRecord_Basic(t *testing.T) {
@@ -37,6 +36,45 @@ func TestAccDNSimpleRecord_Basic(t *testing.T) {
3736
})
3837
}
3938

39+
func TestAccDNSimpleRecord_Updated(t *testing.T) {
40+
var record dnsimple.Record
41+
domain := os.Getenv("DNSIMPLE_DOMAIN")
42+
43+
resource.Test(t, resource.TestCase{
44+
PreCheck: func() { testAccPreCheck(t) },
45+
Providers: testAccProviders,
46+
CheckDestroy: testAccCheckDNSimpleRecordDestroy,
47+
Steps: []resource.TestStep{
48+
resource.TestStep{
49+
Config: fmt.Sprintf(testAccCheckDNSimpleRecordConfig_basic, domain),
50+
Check: resource.ComposeTestCheckFunc(
51+
testAccCheckDNSimpleRecordExists("dnsimple_record.foobar", &record),
52+
testAccCheckDNSimpleRecordAttributes(&record),
53+
resource.TestCheckResourceAttr(
54+
"dnsimple_record.foobar", "name", "terraform"),
55+
resource.TestCheckResourceAttr(
56+
"dnsimple_record.foobar", "domain", domain),
57+
resource.TestCheckResourceAttr(
58+
"dnsimple_record.foobar", "value", "192.168.0.10"),
59+
),
60+
},
61+
resource.TestStep{
62+
Config: fmt.Sprintf(testAccCheckDNSimpleRecordConfig_new_value, domain),
63+
Check: resource.ComposeTestCheckFunc(
64+
testAccCheckDNSimpleRecordExists("dnsimple_record.foobar", &record),
65+
testAccCheckDNSimpleRecordAttributesUpdated(&record),
66+
resource.TestCheckResourceAttr(
67+
"dnsimple_record.foobar", "name", "terraform"),
68+
resource.TestCheckResourceAttr(
69+
"dnsimple_record.foobar", "domain", domain),
70+
resource.TestCheckResourceAttr(
71+
"dnsimple_record.foobar", "value", "192.168.0.11"),
72+
),
73+
},
74+
},
75+
})
76+
}
77+
4078
func testAccCheckDNSimpleRecordDestroy(s *terraform.State) error {
4179
client := testAccProvider.client
4280

@@ -45,12 +83,7 @@ func testAccCheckDNSimpleRecordDestroy(s *terraform.State) error {
4583
continue
4684
}
4785

48-
intId, err := strconv.Atoi(rs.ID)
49-
if err != nil {
50-
return err
51-
}
52-
53-
_, err = client.RetrieveRecord(rs.Attributes["domain"], intId)
86+
_, err := client.RetrieveRecord(rs.Attributes["domain"], rs.ID)
5487

5588
if err == nil {
5689
return fmt.Errorf("Record still exists")
@@ -71,6 +104,17 @@ func testAccCheckDNSimpleRecordAttributes(record *dnsimple.Record) resource.Test
71104
}
72105
}
73106

107+
func testAccCheckDNSimpleRecordAttributesUpdated(record *dnsimple.Record) resource.TestCheckFunc {
108+
return func(s *terraform.State) error {
109+
110+
if record.Content != "192.168.0.11" {
111+
return fmt.Errorf("Bad content: %s", record.Content)
112+
}
113+
114+
return nil
115+
}
116+
}
117+
74118
func testAccCheckDNSimpleRecordExists(n string, record *dnsimple.Record) resource.TestCheckFunc {
75119
return func(s *terraform.State) error {
76120
rs, ok := s.Resources[n]
@@ -85,18 +129,13 @@ func testAccCheckDNSimpleRecordExists(n string, record *dnsimple.Record) resourc
85129

86130
client := testAccProvider.client
87131

88-
intId, err := strconv.Atoi(rs.ID)
89-
if err != nil {
90-
return err
91-
}
92-
93-
foundRecord, err := client.RetrieveRecord(rs.Attributes["domain"], intId)
132+
foundRecord, err := client.RetrieveRecord(rs.Attributes["domain"], rs.ID)
94133

95134
if err != nil {
96135
return err
97136
}
98137

99-
if strconv.Itoa(foundRecord.Id) != rs.ID {
138+
if foundRecord.StringId() != rs.ID {
100139
return fmt.Errorf("Record not found")
101140
}
102141

@@ -115,3 +154,13 @@ resource "dnsimple_record" "foobar" {
115154
type = "A"
116155
ttl = 3600
117156
}`
157+
158+
const testAccCheckDNSimpleRecordConfig_new_value = `
159+
resource "dnsimple_record" "foobar" {
160+
domain = "%s"
161+
162+
name = "terraform"
163+
value = "192.168.0.11"
164+
type = "A"
165+
ttl = 3600
166+
}`

builtin/providers/dnsimple/resource_provider.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import (
55

66
"github.com/hashicorp/terraform/helper/config"
77
"github.com/hashicorp/terraform/terraform"
8-
"github.com/rubyist/go-dnsimple"
8+
"github.com/pearkes/dnsimple"
99
)
1010

1111
type ResourceProvider struct {
1212
Config Config
1313

14-
client *dnsimple.DNSimpleClient
14+
client *dnsimple.Client
1515
}
1616

1717
func (p *ResourceProvider) Validate(c *terraform.ResourceConfig) ([]string, []error) {

builtin/providers/dnsimple/resource_provider_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,6 @@ func testAccPreCheck(t *testing.T) {
7575
}
7676

7777
if v := os.Getenv("DNSIMPLE_DOMAIN"); v == "" {
78-
t.Fatal("DNSIMPLE_DOMAIN must be set for acceptance tests. The domain is used to create and destroy record against.")
78+
t.Fatal("DNSIMPLE_DOMAIN must be set for acceptance tests. The domain is used to ` and destroy record against.")
7979
}
8080
}

0 commit comments

Comments
 (0)