/
instance.go
307 lines (263 loc) · 8.04 KB
/
instance.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
package helper
import (
"context"
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"time"
fwdiag "github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/linode/linodego"
)
const (
RootPassMinimumCharacters = 11
RootPassMaximumCharacters = 128
DefaultFrameworkRebootTimeout = 600
)
var bootEvents = []linodego.EventAction{linodego.ActionLinodeBoot, linodego.ActionLinodeReboot}
// set bootConfig = 0 if using existing boot config
func RebootInstance(ctx context.Context, d *schema.ResourceData, linodeID int,
meta interface{}, bootConfig int,
) diag.Diagnostics {
client := meta.(*ProviderMeta).Client
ctx, cancel := context.WithTimeout(
ctx,
time.Duration(GetDeadlineSeconds(ctx, d))*time.Second,
)
defer cancel()
return diag.FromErr(rebootInstance(ctx, linodeID, &client, bootConfig))
}
func FrameworkRebootInstance(
ctx context.Context,
linodeID int,
client *linodego.Client,
bootConfig int,
) fwdiag.Diagnostics {
var diags fwdiag.Diagnostics
if _, ok := ctx.Deadline(); !ok {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(
ctx,
time.Duration(DefaultFrameworkRebootTimeout)*time.Second,
)
defer cancel()
}
err := rebootInstance(ctx, linodeID, client, bootConfig)
if err != nil {
diags.AddError("Failed to Reboot Instance", err.Error())
}
return diags
}
func rebootInstance(
ctx context.Context,
entityID int,
client *linodego.Client,
bootConfig int,
) error {
ctx = SetLogFieldBulk(ctx, map[string]any{
"linode_id": entityID,
"config_id": bootConfig,
})
instance, err := client.GetInstance(ctx, entityID)
if err != nil {
return fmt.Errorf("Error fetching data about the current linode: %s", err)
}
if instance.Status != linodego.InstanceRunning {
tflog.Info(ctx, "Instance is not running")
return nil
}
tflog.Info(ctx, "Rebooting instance")
p, err := client.NewEventPoller(ctx, entityID, linodego.EntityLinode, linodego.ActionLinodeReboot)
if err != nil {
return fmt.Errorf("failed to initialize event poller: %s", err)
}
err = client.RebootInstance(ctx, instance.ID, bootConfig)
if err != nil {
return fmt.Errorf("Error rebooting Instance [%d]: %s", instance.ID, err)
}
deadlineSeconds := 600
if deadline, ok := ctx.Deadline(); ok {
deadlineSeconds = int(time.Until(deadline).Seconds())
}
_, err = p.WaitForFinished(ctx, deadlineSeconds)
if err != nil {
return fmt.Errorf("Error waiting for Instance [%d] to finish rebooting: %s", instance.ID, err)
}
if deadline, ok := ctx.Deadline(); ok {
deadlineSeconds = int(time.Until(deadline).Seconds())
}
if _, err = client.WaitForInstanceStatus(
ctx, instance.ID, linodego.InstanceRunning, deadlineSeconds,
); err != nil {
return fmt.Errorf("Timed-out waiting for Linode instance [%d] to boot: %s", instance.ID, err)
}
tflog.Debug(ctx, "Instance has finished rebooting")
return nil
}
// GetDeadlineSeconds gets the seconds remaining until deadline is met.
func GetDeadlineSeconds(ctx context.Context, d *schema.ResourceData) int {
duration := d.Timeout(schema.TimeoutUpdate)
if deadline, ok := ctx.Deadline(); ok {
duration = time.Until(deadline)
}
return int(duration.Seconds())
}
// IsInstanceInBootedState checks whether an instance is in a booted or booting state
func IsInstanceInBootedState(status linodego.InstanceStatus) bool {
// For diffing purposes, transition states need to be treated as
// booted == true. This is because these statuses will eventually
// result in a powered on Linode.
return status == linodego.InstanceRunning ||
status == linodego.InstanceRebooting ||
status == linodego.InstanceBooting
}
// GetCurrentBootedConfig gets the config a linode instance is current booted to
func GetCurrentBootedConfig(ctx context.Context, client *linodego.Client, instID int) (int, error) {
inst, err := client.GetInstance(ctx, instID)
if err != nil {
return 0, err
}
// Valid exit condition where no config is booted
if !IsInstanceInBootedState(inst.Status) {
return 0, nil
}
filter := map[string]any{
"entity.id": instID,
"entity.type": linodego.EntityLinode,
"+or": []map[string]any{},
"+order_by": "created",
"+order": "desc",
}
for _, v := range bootEvents {
filter["+or"] = append(filter["+or"].([]map[string]any), map[string]any{"action": v})
}
filterBytes, err := json.Marshal(filter)
if err != nil {
return 0, err
}
events, err := client.ListEvents(ctx, &linodego.ListOptions{
Filter: string(filterBytes),
})
if err != nil {
return 0, err
}
if len(events) < 1 {
// This is a valid exit case
return 0, nil
}
// Special case for instances booted into rescue mode
if events[0].SecondaryEntity == nil {
return 0, nil
}
return int(events[0].SecondaryEntity.ID.(float64)), nil
}
func FrameworkCreateRandomRootPassword(diags *fwdiag.Diagnostics) string {
rootPass, err := CreateRandomRootPassword()
if err != nil {
diags.AddError("Failed to Generate Random Root Password", err.Error())
return ""
}
return rootPass
}
func CreateRandomRootPassword() (string, error) {
rawRootPass := make([]byte, 50)
_, err := rand.Read(rawRootPass)
if err != nil {
return "", fmt.Errorf("Failed to generate random password: %w", err)
}
rootPass := base64.StdEncoding.EncodeToString(rawRootPass)
return rootPass, nil
}
func ExpandInterfaceIPv4(ipv4 any) *linodego.VPCIPv4 {
IPv4 := ipv4.(map[string]any)
vpcAddress := IPv4["vpc"].(string)
nat1To1 := IPv4["nat_1_1"].(string)
if vpcAddress == "" && nat1To1 == "" {
return nil
}
result := &linodego.VPCIPv4{
VPC: vpcAddress,
}
if nat1To1 != "" {
result.NAT1To1 = &nat1To1
}
return result
}
func ExpandConfigInterface(ifaceMap map[string]interface{}) linodego.InstanceConfigInterfaceCreateOptions {
purpose := linodego.ConfigInterfacePurpose(ifaceMap["purpose"].(string))
result := linodego.InstanceConfigInterfaceCreateOptions{
Purpose: purpose,
Primary: ifaceMap["primary"].(bool),
}
if purpose == linodego.InterfacePurposeVLAN {
if ifaceMap["ipam_address"] != nil {
result.IPAMAddress = ifaceMap["ipam_address"].(string)
}
if ifaceMap["label"] != nil {
result.Label = ifaceMap["label"].(string)
}
}
if purpose == linodego.InterfacePurposeVPC {
if ifaceMap["subnet_id"] != nil {
if subnetId := ifaceMap["subnet_id"].(int); subnetId != 0 {
result.SubnetID = &subnetId
}
}
if ifaceMap["ipv4"] != nil {
if ipv4 := ifaceMap["ipv4"].([]any); len(ipv4) > 0 {
result.IPv4 = ExpandInterfaceIPv4(ipv4[0])
}
}
if ifaceMap["ip_ranges"] != nil {
// this is for keep result.IPRanges as a nil value rather than a value of empty slice
// when there is not a range.
if ranges := ifaceMap["ip_ranges"].([]interface{}); len(ranges) > 0 {
result.IPRanges = ExpandStringList(ranges)
}
}
}
return result
}
func ExpandConfigInterfaces(ctx context.Context, ifaces []any) []linodego.InstanceConfigInterfaceCreateOptions {
result := make([]linodego.InstanceConfigInterfaceCreateOptions, len(ifaces))
for i, iface := range ifaces {
ifaceMap := iface.(map[string]any)
result[i] = ExpandConfigInterface(ifaceMap)
}
return result
}
func FlattenInterfaceIPv4(ipv4 *linodego.VPCIPv4) []map[string]any {
if ipv4 == nil {
return nil
}
return []map[string]any{
{
"vpc": ipv4.VPC,
"nat_1_1": ipv4.NAT1To1,
},
}
}
func FlattenInterface(iface linodego.InstanceConfigInterface) map[string]any {
return map[string]any{
"purpose": iface.Purpose,
"ipam_address": iface.IPAMAddress,
"label": iface.Label,
"id": iface.ID,
"vpc_id": iface.VPCID,
"subnet_id": iface.SubnetID,
"primary": iface.Primary,
"active": iface.Active,
"ip_ranges": iface.IPRanges,
"ipv4": FlattenInterfaceIPv4(iface.IPv4),
}
}
func FlattenInterfaces(interfaces []linodego.InstanceConfigInterface) []map[string]any {
result := make([]map[string]any, len(interfaces))
for i, iface := range interfaces {
result[i] = FlattenInterface(iface)
}
return result
}