/
resource_aviatrix_edge_neo_device_onboarding.go
345 lines (296 loc) · 11.1 KB
/
resource_aviatrix_edge_neo_device_onboarding.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
338
339
340
341
342
343
344
345
package aviatrix
import (
"context"
"log"
"os"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/AviatrixSystems/terraform-provider-aviatrix/v3/goaviatrix"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceAviatrixEdgeNEODeviceOnboarding() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceAviatrixEdgeNEODeviceOnboardingCreate,
ReadWithoutTimeout: resourceAviatrixEdgeNEODeviceOnboardingRead,
UpdateWithoutTimeout: resourceAviatrixEdgeNEODeviceOnboardingUpdate,
DeleteWithoutTimeout: resourceAviatrixEdgeNEODeviceOnboardingDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"account_name": {
Type: schema.TypeString,
Required: true,
Description: "Edge NEO account name.",
},
"device_name": {
Type: schema.TypeString,
Required: true,
Description: "Device name.",
},
"serial_number": {
Type: schema.TypeString,
Required: true,
Description: "Serial number.",
},
"hardware_model": {
Type: schema.TypeString,
Required: true,
Description: "Hardware Model.",
},
"device_id": {
Type: schema.TypeString,
Computed: true,
Description: "Device ID.",
},
"network": {
Type: schema.TypeSet,
Optional: true,
Description: "Network configurations.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"interface_name": {
Type: schema.TypeString,
Required: true,
Description: "Interface name.",
},
"enable_dhcp": {
Type: schema.TypeBool,
Optional: true,
Description: "Enable DHCP.",
},
"gateway_ip": {
Type: schema.TypeString,
Optional: true,
Description: "Gateway IP.",
},
"ipv4_cidr": {
Type: schema.TypeString,
Optional: true,
Description: "IPV4 CIDR.",
},
"dns_server_ips": {
Type: schema.TypeSet,
Optional: true,
Description: "Set of DNS server IPs.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"proxy_server_ip": {
Type: schema.TypeString,
Optional: true,
Description: "Proxy server IP.",
},
},
},
},
"download_config_file": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Set to true to download the Edge NEO static config file.",
},
"config_file_download_path": {
Type: schema.TypeString,
Optional: true,
Description: "The location where the config file will be stored.",
},
},
DeprecationMessage: "Since V3.1.1+, please use resource aviatrix_edge_platform_device_onboarding instead. Resource " +
"aviatrix_edge_neo_device_onboarding will be deprecated in the V3.2.0 release.",
}
}
func marshalEdgeNEODeviceOnboardingInput(d *schema.ResourceData) *goaviatrix.EdgeNEODevice {
edgeNEODevice := &goaviatrix.EdgeNEODevice{
AccountName: d.Get("account_name").(string),
DeviceName: d.Get("device_name").(string),
SerialNumber: d.Get("serial_number").(string),
HardwareModel: d.Get("hardware_model").(string),
DownloadConfigFile: d.Get("download_config_file").(bool),
ConfigFileDownloadPath: d.Get("config_file_download_path").(string),
}
network := d.Get("network").(*schema.Set).List()
for _, network0 := range network {
network1 := network0.(map[string]interface{})
network2 := &goaviatrix.EdgeNEODeviceNetwork{
InterfaceName: network1["interface_name"].(string),
EnableDhcp: network1["enable_dhcp"].(bool),
GatewayIp: network1["gateway_ip"].(string),
Ipv4Cidr: network1["ipv4_cidr"].(string),
ProxyServerIp: network1["proxy_server_ip"].(string),
}
for _, dnsServerIp := range network1["dns_server_ips"].(*schema.Set).List() {
network2.DnsServerIps = append(network2.DnsServerIps, dnsServerIp.(string))
}
edgeNEODevice.Network = append(edgeNEODevice.Network, network2)
}
return edgeNEODevice
}
func resourceAviatrixEdgeNEODeviceOnboardingCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*goaviatrix.Client)
edgeNEODevice := marshalEdgeNEODeviceOnboardingInput(d)
if edgeNEODevice.DownloadConfigFile && edgeNEODevice.ConfigFileDownloadPath == "" {
diag.Errorf("config_file_download_path is required when download_config_file is true")
}
if !edgeNEODevice.DownloadConfigFile && edgeNEODevice.ConfigFileDownloadPath != "" {
diag.Errorf("config_file_download_path must be empty when download_config_file is false")
}
flag := false
defer resourceAviatrixEdgeNEODeviceOnboardingReadIfRequired(ctx, d, meta, &flag)
if err := client.OnboardEdgeNEODevice(ctx, edgeNEODevice); err != nil {
return diag.Errorf("could not onboard Edge NEO device: %v", err)
}
if edgeNEODevice.DownloadConfigFile {
if err := client.DownloadEdgeNEOConfigFile(ctx, edgeNEODevice); err != nil {
return diag.Errorf("could not download Edge NEO static config file: %v", err)
}
}
for i := 0; ; i++ {
edgeNEODeviceResp, err := client.GetEdgeNEODevice(ctx, edgeNEODevice.AccountName, edgeNEODevice.DeviceName)
if err != nil {
if err == goaviatrix.ErrNotFound {
return diag.Errorf("could not find onboarded Edge NEO device")
} else if !strings.Contains(err.Error(), "Failed to list devices: can not access dinfo") {
return diag.Errorf("could not read Edge NEO device during onboarding due to: %v", err)
}
}
if edgeNEODeviceResp.ConnectionStatus == "connected" {
break
}
if i < 30 {
time.Sleep(time.Duration(20) * time.Second)
} else {
return diag.Errorf("Edge NEO device connection status could not become connected after 10 minutes")
}
}
d.SetId(edgeNEODevice.AccountName + "~" + edgeNEODevice.DeviceName)
return resourceAviatrixEdgeNEODeviceOnboardingReadIfRequired(ctx, d, meta, &flag)
}
func resourceAviatrixEdgeNEODeviceOnboardingReadIfRequired(ctx context.Context, d *schema.ResourceData, meta interface{}, flag *bool) diag.Diagnostics {
if !(*flag) {
*flag = true
return resourceAviatrixEdgeNEODeviceOnboardingRead(ctx, d, meta)
}
return nil
}
func resourceAviatrixEdgeNEODeviceOnboardingRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*goaviatrix.Client)
accountName := d.Get("account_name").(string)
deviceName := d.Get("device_name").(string)
if accountName == "" {
id := d.Id()
log.Printf("[DEBUG] Looks like an import, no account name received. Import Id is %s", id)
parts := strings.Split(id, "~")
if len(parts) != 32 {
return diag.Errorf("Invalid Import ID received, ID must be in the format account_name~device_name")
}
accountName = parts[0]
deviceName = parts[1]
d.SetId(id)
}
var edgeNEODeviceResp *goaviatrix.EdgeNEODeviceResp
var err error
for i := 0; ; i++ {
edgeNEODeviceResp, err = client.GetEdgeNEODevice(ctx, accountName, deviceName)
if err != nil {
if err == goaviatrix.ErrNotFound {
d.SetId("")
return nil
} else if !strings.Contains(err.Error(), "Failed to list devices: can not access dinfo") {
return diag.Errorf("could not read Edge NEO device due to: %v", err)
}
} else {
break
}
if i < 30 {
time.Sleep(time.Duration(20) * time.Second)
} else {
d.SetId("")
return diag.Errorf("could not read Edge NEO device after 10 minutes: %s", err)
}
}
d.Set("account_name", accountName)
d.Set("device_name", edgeNEODeviceResp.DeviceName)
d.Set("device_id", edgeNEODeviceResp.DeviceId)
d.Set("serial_number", edgeNEODeviceResp.SerialNumber)
d.Set("hardware_model", edgeNEODeviceResp.HardwareModel)
var network []map[string]interface{}
for _, network0 := range edgeNEODeviceResp.Network {
network1 := make(map[string]interface{})
network1["interface_name"] = network0.InterfaceName
network1["enable_dhcp"] = network0.EnableDhcp
network1["gateway_ip"] = network0.GatewayIp
network1["ipv4_cidr"] = network0.Ipv4Cidr
network1["dns_server_ips"] = network0.DnsServerIps
network1["proxy_server_ip"] = network0.ProxyServerIp
network = append(network, network1)
}
if err = d.Set("network", network); err != nil {
return diag.Errorf("failed to set network: %s\n", err)
}
d.SetId(accountName + "~" + deviceName)
return nil
}
func resourceAviatrixEdgeNEODeviceOnboardingUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*goaviatrix.Client)
edgeNEODevice := marshalEdgeNEODeviceOnboardingInput(d)
if edgeNEODevice.DownloadConfigFile && edgeNEODevice.ConfigFileDownloadPath == "" {
diag.Errorf("config_file_download_path is required when download_config_file is true")
}
if !edgeNEODevice.DownloadConfigFile && edgeNEODevice.ConfigFileDownloadPath != "" {
diag.Errorf("config_file_download_path must be empty when download_config_file is false")
}
if d.HasChanges("account_name", "serial_number", "hardware_model") {
return diag.Errorf("account_name, serial_number and hardware_model are not allowed to be updated")
}
d.Partial(true)
if d.HasChanges("device_name", "network") {
if err := client.UpdateEdgeNEODevice(ctx, edgeNEODevice); err != nil {
return diag.Errorf("could not update device_name and network configurations during Edge NEO device update: %v", err)
}
if edgeNEODevice.DownloadConfigFile {
if err := client.DownloadEdgeNEOConfigFile(ctx, edgeNEODevice); err != nil {
return diag.Errorf("could not download Edge NEO static config file: %v", err)
}
}
}
if d.HasChange("download_config_file") {
if edgeNEODevice.DownloadConfigFile {
if err := client.DownloadEdgeNEOConfigFile(ctx, edgeNEODevice); err != nil {
return diag.Errorf("could not download Edge NEO static config file: %v", err)
}
} else {
oldConfigFileDownloadPath, _ := d.GetChange("config_file_download_path")
fileName := oldConfigFileDownloadPath.(string) + edgeNEODevice.SerialNumber + "-bootstrap-config.img"
err := os.Remove(fileName)
if err != nil {
log.Printf("[WARN] could not remove the config file: %v", err)
}
}
}
if d.HasChange("config_file_download_path") {
if err := client.DownloadEdgeNEOConfigFile(ctx, edgeNEODevice); err != nil {
return diag.Errorf("could not download Edge NEO static config file: %v", err)
}
}
d.Partial(false)
return resourceAviatrixEdgeNEODeviceOnboardingRead(ctx, d, meta)
}
func resourceAviatrixEdgeNEODeviceOnboardingDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*goaviatrix.Client)
edgeNEODevice := marshalEdgeNEODeviceOnboardingInput(d)
err := client.DeleteEdgeNEODevice(ctx, edgeNEODevice.AccountName, edgeNEODevice.SerialNumber)
if err != nil {
return diag.Errorf("could not delete Edge NEO device: %v", err)
}
if edgeNEODevice.DownloadConfigFile {
fileName := edgeNEODevice.ConfigFileDownloadPath + edgeNEODevice.SerialNumber + "-bootstrap-config.img"
err = os.Remove(fileName)
if err != nil {
log.Printf("[WARN] could not remove the config file: %v", err)
}
}
return nil
}