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
/
resource_vultr_server.go
337 lines (265 loc) · 8.01 KB
/
resource_vultr_server.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// This code was originally based on the Digital Ocean provider from
// https://github.com/hashicorp/terraform/tree/master/builtin/providers/digitalocean.
package main
import (
"fmt"
"log"
"strings"
"time"
"github.com/JamesClonk/vultr/lib"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceVultrServer() *schema.Resource {
return &schema.Resource{
Create: resourceVultrServerCreate,
Read: resourceVultrServerRead,
Update: resourceVultrServerUpdate,
Delete: resourceVultrServerDelete,
Schema: map[string]*schema.Schema{
"status": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"power_status": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"default_password": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
"tag": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"hostname": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"region_id": &schema.Schema{
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
"plan_id": &schema.Schema{
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
"os_id": &schema.Schema{
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
"snapshot_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
// if you are using this make sure you set `os_id` to `159` (Custom).
"ipxe_chain_url": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
// if you are using this make sure you set `os_id` to `159` (Custom).
"iso_id": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
"user_data": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"ssh_key_ids": &schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"ipv4_address": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"ipv4_private_address": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"ipv6": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
"private_networking": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
"auto_backups": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
},
},
}
}
func resourceVultrServerCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*lib.Client)
name := d.Get("name").(string)
regionId := d.Get("region_id").(int)
planId := d.Get("plan_id").(int)
osId := d.Get("os_id").(int)
options := &lib.ServerOptions{
Tag: d.Get("tag").(string),
Hostname: d.Get("hostname").(string),
IPXEChainURL: d.Get("ipxe_chain_url").(string),
ISO: d.Get("iso_id").(int),
UserData: d.Get("user_data").(string),
Snapshot: d.Get("snapshot_id").(string),
}
if attr, ok := d.GetOk("ipv6"); ok {
options.IPV6 = attr.(bool)
}
if attr, ok := d.GetOk("private_networking"); ok {
options.PrivateNetworking = attr.(bool)
}
if attr, ok := d.GetOk("auto_backups"); ok {
options.AutoBackups = attr.(bool)
}
sshKeyIdsLen := d.Get("ssh_key_ids.#").(int)
if sshKeyIdsLen > 0 {
sshKeyIds := make([]string, 0, sshKeyIdsLen)
for i := 0; i < sshKeyIdsLen; i++ {
key := fmt.Sprintf("ssh_key_ids.%d", i)
sshKeyIds = append(sshKeyIds, d.Get(key).(string))
}
options.SSHKey = strings.Join(sshKeyIds, ",")
}
log.Printf("[DEBUG] Server create configuration: %#v", options)
server, err := client.CreateServer(name, regionId, planId, osId, options)
if err != nil {
return fmt.Errorf("Error creating server: %s", err)
}
d.SetId(server.ID)
log.Printf("[INFO] Server ID: %s", d.Id())
// wait for the server to be "ready". we have to wait for status=active and power_status=running.
_, err = WaitForServerAttribute(d, "active", []string{"pending"}, "status", meta)
if err != nil {
return fmt.Errorf(
"Error waiting for server (%s) to become active: %s", d.Id(), err)
}
_, err = WaitForServerAttribute(d, "running", []string{"starting", "stopped"}, "power_status", meta)
if err != nil {
return fmt.Errorf(
"Error waiting for server (%s) to become running: %s", d.Id(), err)
}
return resourceVultrServerRead(d, meta)
}
func resourceVultrServerRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*lib.Client)
server, err := client.GetServer(d.Id())
if err != nil {
// check if the server not longer exists.
if err.Error() == "Invalid server." {
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving server: %s", err)
}
d.Set("name", server.Name)
d.Set("tag", server.Tag)
d.Set("region_id", server.RegionID)
d.Set("plan_id", server.PlanID)
d.Set("status", server.Status)
d.Set("power_status", server.PowerStatus)
d.Set("default_password", server.DefaultPassword)
d.Set("ipv4_address", server.MainIP)
d.Set("ipv4_private_address", server.InternalIP)
d.SetConnInfo(map[string]string{
"type": "ssh",
"host": server.MainIP,
"password": server.DefaultPassword,
})
return nil
}
func resourceVultrServerUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*lib.Client)
d.Partial(true)
if d.HasChange("name") {
oldName, newName := d.GetChange("name")
err := client.RenameServer(d.Id(), newName.(string))
if err != nil {
return fmt.Errorf("Error renaming server (%s): %s", d.Id(), err)
}
_, err = WaitForServerAttribute(d, newName.(string), []string{"", oldName.(string)}, "name", meta)
if err != nil {
return fmt.Errorf("Error waiting for rename server (%s) to finish: %s", d.Id(), err)
}
d.SetPartial("name")
}
if d.HasChange("tag") {
oldValue, newValue := d.GetChange("tag")
err := client.TagServer(d.Id(), newValue.(string))
if err != nil {
return fmt.Errorf("Error tagging the server (%s): %s", d.Id(), err)
}
_, err = WaitForServerAttribute(d, newValue.(string), []string{"", oldValue.(string)}, "tag", meta)
if err != nil {
return fmt.Errorf("Error waiting for tagging the server (%s) to finish: %s", d.Id(), err)
}
d.SetPartial("tag")
}
return resourceVultrServerRead(d, meta)
}
func resourceVultrServerDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*lib.Client)
log.Printf("[INFO] Deleting server: %s", d.Id())
err := client.DeleteServer(d.Id())
if err != nil {
return fmt.Errorf("Error deleting server: %s", err)
}
return nil
}
func WaitForServerAttribute(d *schema.ResourceData, target string, pending []string, attribute string, meta interface{}) (interface{}, error) {
log.Printf(
"[INFO] Waiting for server (%s) to have %s of %s",
d.Id(), attribute, target)
stateConf := &resource.StateChangeConf{
Pending: pending,
Target: []string{target},
Refresh: newServerStateRefreshFunc(d, attribute, meta),
Timeout: 60 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
return stateConf.WaitForState()
}
// TODO This function still needs a little more refactoring to make it
// cleaner and more efficient
func newServerStateRefreshFunc(d *schema.ResourceData, attribute string, meta interface{}) resource.StateRefreshFunc {
client := meta.(*lib.Client)
return func() (interface{}, string, error) {
err := resourceVultrServerRead(d, meta)
if err != nil {
return nil, "", err
}
// See if we can access our attribute
if attr, ok := d.GetOk(attribute); ok {
// Retrieve the server properties
server, err := client.GetServer(d.Id())
if err != nil {
return nil, "", fmt.Errorf("Error retrieving server: %s", err)
}
return &server, attr.(string), nil
}
return nil, "", nil
}
}