forked from burkostya/terraform-provider-vscale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_record.go
173 lines (142 loc) · 3.88 KB
/
resource_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
package main
import (
"io/ioutil"
"net/http"
"strconv"
"github.com/hashicorp/terraform/helper/schema"
"github.com/pkg/errors"
vscale "github.com/vscale/go-vscale"
)
func resourceRecord() *schema.Resource {
return &schema.Resource{
Create: resourceRecordCreate,
Read: resourceRecordRead,
Exists: resourceRecordExists,
Update: resourceRecordUpdate,
Delete: resourceRecordDelete,
Schema: map[string]*schema.Schema{
"domain": &schema.Schema{
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"type": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"content": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"ttl": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 86400,
},
},
}
}
func resourceRecordCreate(d *schema.ResourceData, m interface{}) error {
client := m.(*vscale.WebClient)
domainID := d.Get("domain").(int)
name := d.Get("name").(string)
recordType := d.Get("type").(string)
content := d.Get("content").(string)
ttl := d.Get("ttl").(int)
record, res, err := client.DomainRecord.Create(int64(domainID), name, recordType, int64(ttl), content)
if err != nil {
b, berr := ioutil.ReadAll(res.Body)
if berr != nil {
return errors.Wrap(err, "reading response body failed")
}
return errors.Wrapf(err, "creating domain record failed with body: %s", string(b))
}
d.SetId(strconv.FormatInt(record.ID, 10))
return nil
}
func resourceRecordUpdate(d *schema.ResourceData, m interface{}) error {
client := m.(*vscale.WebClient)
recordID, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return errors.Wrap(err, "invalid id")
}
domainID := d.Get("domain").(int)
name := d.Get("name").(string)
recordType := d.Get("type").(string)
content := d.Get("content").(string)
ttl := d.Get("ttl").(int)
_, res, err := client.DomainRecord.Update(
int64(domainID), recordID, name, recordType, int64(ttl), content,
)
if res.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return errors.Wrap(err, "updating domain record failed")
}
return nil
}
func resourceRecordRead(d *schema.ResourceData, m interface{}) error {
client := m.(*vscale.WebClient)
recordID, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return errors.Wrap(err, "invalid id")
}
domainID := d.Get("domain").(int)
record, res, err := client.DomainRecord.Get(int64(domainID), recordID)
if res.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return errors.Wrap(err, "getting domain record failed")
}
d.Set("domain", domainID)
d.Set("name", record.Name)
d.Set("type", record.Type)
d.Set("content", record.Content)
d.Set("ttl", record.TTL)
return nil
}
func resourceRecordExists(d *schema.ResourceData, m interface{}) (bool, error) {
client := m.(*vscale.WebClient)
recordID, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return true, errors.Wrap(err, "invalid id")
}
domainID := d.Get("domain").(int)
_, res, err := client.DomainRecord.Get(int64(domainID), recordID)
if res.StatusCode == http.StatusNotFound {
d.SetId("")
return false, nil
}
if err != nil {
return true, errors.Wrap(err, "getting domain record failed")
}
return true, nil
}
func resourceRecordDelete(d *schema.ResourceData, m interface{}) error {
client := m.(*vscale.WebClient)
recordID, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return errors.Wrap(err, "invalid id")
}
domainID := d.Get("domain").(int)
_, res, err := client.DomainRecord.Remove(int64(domainID), recordID)
if res.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return errors.Wrap(err, "removing domain record failed")
}
return nil
}