This repository has been archived by the owner on Jul 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathresource_vultr_dns_record.go
188 lines (154 loc) · 4.2 KB
/
resource_vultr_dns_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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// This code was originally based on the Digital Ocean provider from
// https://github.com/terraform-providers/terraform-provider-digitalocean.
package main
import (
"fmt"
"strconv"
"strings"
"github.com/JamesClonk/vultr/lib"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceVultrDNSRecord() *schema.Resource {
return &schema.Resource{
Create: resourceVultrDNSRecordCreate,
Read: resourceVultrDNSRecordRead,
Update: resourceVultrDNSRecordUpdate,
Delete: resourceVultrDNSRecordDelete,
Schema: map[string]*schema.Schema{
"domain": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"data": {
Type: schema.TypeString,
Required: true,
},
"ttl": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"priority": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"fqdn": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceVultrDNSRecordCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*lib.Client)
domain := d.Get("domain").(string)
rtype := d.Get("type").(string)
name := d.Get("name").(string)
data := d.Get("data").(string)
priority := 0 // only used in MX and SRV resource records.
ttl := 0
if attr, ok := d.GetOk("priority"); ok {
priority = attr.(int)
}
if attr, ok := d.GetOk("ttl"); ok {
ttl = attr.(int)
}
err := client.CreateDNSRecord(domain, name, rtype, data, priority, ttl)
if err != nil {
return fmt.Errorf("Failed to create dns record: %s", err)
}
records, err := client.GetDNSRecords(domain)
if err != nil {
return fmt.Errorf("Failed to get dns records: %s", err)
}
for _, r := range records {
if r.Name == name && r.Type == rtype && r.Data == data {
d.SetId(strconv.Itoa(r.RecordID))
return resourceVultrDNSRecordRead(d, meta)
}
}
return fmt.Errorf("Failed to get the just created dns record: %s", err)
}
func resourceVultrDNSRecordRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*lib.Client)
id, err := strconv.Atoi(d.Id())
if err != nil {
return fmt.Errorf("Failed to parse resource id: %s", err)
}
domain := d.Get("domain").(string)
records, err := client.GetDNSRecords(domain)
if err != nil {
return fmt.Errorf("Failed to get dns records: %s", err)
}
var record *lib.DNSRecord
for _, r := range records {
if r.RecordID == id {
record = &r
break
}
}
// if it is somehow already destroyed mark as succesfully gone.
if record == nil {
d.SetId("")
return nil
}
d.Set("type", record.Type)
d.Set("name", record.Name)
d.Set("data", record.Data)
d.Set("priority", strconv.Itoa(record.Priority))
d.Set("ttl", strconv.Itoa(record.TTL))
d.Set("fqdn", constructFqdn(record.Name, domain))
return nil
}
func resourceVultrDNSRecordUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*lib.Client)
id, err := strconv.Atoi(d.Id())
if err != nil {
return fmt.Errorf("Failed to parse resource id: %s", err)
}
domain := d.Get("domain").(string)
record := lib.DNSRecord{
RecordID: id,
Type: d.Get("type").(string),
Name: d.Get("name").(string),
Data: d.Get("data").(string),
Priority: d.Get("priority").(int),
TTL: d.Get("ttl").(int),
}
if err := client.UpdateDNSRecord(domain, record); err != nil {
return fmt.Errorf("Failed to update record: %v", err)
}
return resourceVultrDNSRecordRead(d, meta)
}
func resourceVultrDNSRecordDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*lib.Client)
domain := d.Get("domain").(string)
id, err := strconv.Atoi(d.Id())
if err != nil {
return fmt.Errorf("Failed to parse resource id: %s", err)
}
err = client.DeleteDNSRecord(domain, id)
if err != nil {
return fmt.Errorf("Failed to delete record: %s", err)
}
return nil
}
func constructFqdn(name, domain string) string {
rn := strings.ToLower(strings.TrimSuffix(name, "."))
domain = strings.TrimSuffix(domain, ".")
if !strings.HasSuffix(rn, domain) {
rn = strings.Join([]string{name, domain}, ".")
}
return rn
}