forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_dnsimple_record.go
160 lines (122 loc) · 3.57 KB
/
resource_dnsimple_record.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package dnsimple
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"github.com/pearkes/dnsimple"
)
func resourceDNSimpleRecord() *schema.Resource {
return &schema.Resource{
Create: resourceDNSimpleRecordCreate,
Read: resourceDNSimpleRecordRead,
Update: resourceDNSimpleRecordUpdate,
Delete: resourceDNSimpleRecordDelete,
Schema: map[string]*schema.Schema{
"domain": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"domain_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"hostname": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"type": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"value": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"ttl": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "3600",
},
"priority": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceDNSimpleRecordCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*dnsimple.Client)
// Create the new record
newRecord := &dnsimple.ChangeRecord{
Name: d.Get("name").(string),
Type: d.Get("type").(string),
Value: d.Get("value").(string),
}
if ttl, ok := d.GetOk("ttl"); ok {
newRecord.Ttl = ttl.(string)
}
log.Printf("[DEBUG] DNSimple Record create configuration: %#v", newRecord)
recId, err := client.CreateRecord(d.Get("domain").(string), newRecord)
if err != nil {
return fmt.Errorf("Failed to create DNSimple Record: %s", err)
}
d.SetId(recId)
log.Printf("[INFO] record ID: %s", d.Id())
return resourceDNSimpleRecordRead(d, meta)
}
func resourceDNSimpleRecordRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*dnsimple.Client)
rec, err := client.RetrieveRecord(d.Get("domain").(string), d.Id())
if err != nil {
return fmt.Errorf("Couldn't find DNSimple Record: %s", err)
}
d.Set("domain_id", rec.StringDomainId())
d.Set("name", rec.Name)
d.Set("type", rec.RecordType)
d.Set("value", rec.Content)
d.Set("ttl", rec.StringTtl())
d.Set("priority", rec.StringPrio())
if rec.Name == "" {
d.Set("hostname", d.Get("domain").(string))
} else {
d.Set("hostname", fmt.Sprintf("%s.%s", rec.Name, d.Get("domain").(string)))
}
return nil
}
func resourceDNSimpleRecordUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*dnsimple.Client)
updateRecord := &dnsimple.ChangeRecord{}
if attr, ok := d.GetOk("name"); ok {
updateRecord.Name = attr.(string)
}
if attr, ok := d.GetOk("type"); ok {
updateRecord.Type = attr.(string)
}
if attr, ok := d.GetOk("value"); ok {
updateRecord.Value = attr.(string)
}
if attr, ok := d.GetOk("ttl"); ok {
updateRecord.Ttl = attr.(string)
}
log.Printf("[DEBUG] DNSimple Record update configuration: %#v", updateRecord)
_, err := client.UpdateRecord(d.Get("domain").(string), d.Id(), updateRecord)
if err != nil {
return fmt.Errorf("Failed to update DNSimple Record: %s", err)
}
return resourceDNSimpleRecordRead(d, meta)
}
func resourceDNSimpleRecordDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*dnsimple.Client)
log.Printf("[INFO] Deleting DNSimple Record: %s, %s", d.Get("domain").(string), d.Id())
err := client.DestroyRecord(d.Get("domain").(string), d.Id())
if err != nil {
return fmt.Errorf("Error deleting DNSimple Record: %s", err)
}
return nil
}