/
resource_edgecenter_subnet.go
450 lines (391 loc) · 12.6 KB
/
resource_edgecenter_subnet.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package edgecenter
import (
"context"
"fmt"
"log"
"net"
"net/http"
"regexp"
"time"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
edgecloudV2 "github.com/Edge-Center/edgecentercloud-go/v2"
utilV2 "github.com/Edge-Center/edgecentercloud-go/v2/util"
)
const (
SubnetCreatingTimeout = 1200 * time.Second
SubnetPoint = "subnets"
disable = "disable"
)
func resourceSubnet() *schema.Resource {
return &schema.Resource{
CreateContext: resourceSubnetCreate,
ReadContext: resourceSubnetRead,
UpdateContext: resourceSubnetUpdate,
DeleteContext: resourceSubnetDelete,
Description: "Represent subnets. Subnetwork is a range of IP addresses in a cloud network. Addresses from this range will be assigned to machines in the cloud",
Importer: &schema.ResourceImporter{
StateContext: func(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
projectID, regionID, subnetID, err := ImportStringParser(d.Id())
if err != nil {
return nil, err
}
d.Set("project_id", projectID)
d.Set("region_id", regionID)
d.SetId(subnetID)
return []*schema.ResourceData{d}, nil
},
},
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "The uuid of the project. Either 'project_id' or 'project_name' must be specified.",
ExactlyOneOf: []string{"project_id", "project_name"},
},
"project_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The name of the project. Either 'project_id' or 'project_name' must be specified.",
ExactlyOneOf: []string{"project_id", "project_name"},
},
"region_id": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "The uuid of the region. Either 'region_id' or 'region_name' must be specified.",
ExactlyOneOf: []string{"region_id", "region_name"},
},
"region_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The name of the region. Either 'region_id' or 'region_name' must be specified.",
ExactlyOneOf: []string{"region_id", "region_name"},
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the subnet.",
},
"enable_dhcp": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
Description: "Enable DHCP for this subnet. If true, DHCP will be used to assign IP addresses to instances within this subnet.",
},
"cidr": {
Type: schema.TypeString,
Required: true,
Description: "Represents the IP address range of the subnet.",
},
"network_id": {
Type: schema.TypeString,
Required: true,
Description: "The ID of the network to which this subnet belongs.",
},
"connect_to_network_router": {
Type: schema.TypeBool,
Description: "True if the network's router should get a gateway in this subnet. Must be explicitly 'false' when gateway_ip is null. Default true.",
Optional: true,
Default: true,
},
"dns_nameservers": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Description: "List of DNS name servers for the subnet.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"host_routes": {
Type: schema.TypeList,
Optional: true,
Description: "List of additional routes to be added to instances that are part of this subnet.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"destination": {
Type: schema.TypeString,
Required: true,
},
"nexthop": {
Type: schema.TypeString,
Required: true,
Description: "IPv4 address to forward traffic to if it's destination IP matches 'destination' CIDR",
},
},
},
},
"gateway_ip": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The IP address of the gateway for this subnet.",
ValidateDiagFunc: func(val interface{}, key cty.Path) diag.Diagnostics {
v := val.(string)
IP := regexp.MustCompile(`(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}`)
if v == disable || IP.MatchString(v) {
return nil
}
return diag.FromErr(fmt.Errorf("%q must be a valid ip, got: %s", key, v))
},
},
"metadata_map": {
Type: schema.TypeMap,
Optional: true,
Description: "A map containing metadata, for example tags.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"metadata_read_only": {
Type: schema.TypeList,
Computed: true,
Description: `A list of read-only metadata items, e.g. tags.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Computed: true,
},
"value": {
Type: schema.TypeString,
Computed: true,
},
"read_only": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
"last_updated": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The timestamp of the last update (use with update context).",
},
},
}
}
func resourceSubnetCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Println("[DEBUG] Start Subnet creating")
var diags diag.Diagnostics
config := m.(*Config)
clientV2 := config.CloudClient
regionID, projectID, err := GetRegionIDandProjectID(ctx, clientV2, d)
if err != nil {
return diag.FromErr(err)
}
clientV2.Region = regionID
clientV2.Project = projectID
createOpts := &edgecloudV2.SubnetworkCreateRequest{
Name: d.Get("name").(string),
EnableDHCP: d.Get("enable_dhcp").(bool),
NetworkID: d.Get("network_id").(string),
ConnectToNetworkRouter: d.Get("connect_to_network_router").(bool),
}
cidr := d.Get("cidr").(string)
if cidr != "" {
_, _, err := net.ParseCIDR(cidr)
if err != nil {
return diag.FromErr(err)
}
createOpts.CIDR = cidr
}
dnsNameservers := d.Get("dns_nameservers").([]interface{})
createOpts.DNSNameservers = make([]net.IP, 0)
if len(dnsNameservers) > 0 {
ns := dnsNameservers
dns := make([]net.IP, len(ns))
for i, s := range ns {
dns[i] = net.ParseIP(s.(string))
}
createOpts.DNSNameservers = dns
}
hostRoutes := d.Get("host_routes").([]interface{})
createOpts.HostRoutes = make([]edgecloudV2.HostRoute, 0)
if len(hostRoutes) > 0 {
createOpts.HostRoutes, err = extractHostRoutesMapV2(hostRoutes)
if err != nil {
return diag.FromErr(err)
}
}
gatewayIP := d.Get("gateway_ip").(string)
gw := net.ParseIP(gatewayIP)
if gatewayIP == disable {
createOpts.ConnectToNetworkRouter = false
} else if gw != nil {
createOpts.GatewayIP = &gw
}
if metadataRaw, ok := d.GetOk("metadata_map"); ok {
meta, err := MapInterfaceToMapString(metadataRaw)
if err != nil {
return diag.FromErr(err)
}
createOpts.Metadata = *meta
}
log.Printf("Create subnet ops: %+v", createOpts)
taskResult, err := utilV2.ExecuteAndExtractTaskResult(ctx, clientV2.Subnetworks.Create, createOpts, clientV2, SubnetCreatingTimeout)
if err != nil {
return diag.FromErr(err)
}
subnetID := taskResult.Subnets[0]
d.SetId(subnetID)
resourceSubnetRead(ctx, d, m)
log.Printf("[DEBUG] Finish Subnet creating (%s)", subnetID)
return diags
}
func resourceSubnetRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Println("[DEBUG] Start subnet reading")
log.Printf("[DEBUG] Start subnet reading%s", d.State())
var diags diag.Diagnostics
config := m.(*Config)
clientV2 := config.CloudClient
subnetID := d.Id()
log.Printf("[DEBUG] Subnet id = %s", subnetID)
regionID, projectID, err := GetRegionIDandProjectID(ctx, clientV2, d)
if err != nil {
return diag.FromErr(err)
}
clientV2.Region = regionID
clientV2.Project = projectID
subnet, _, err := clientV2.Subnetworks.Get(ctx, subnetID)
if err != nil {
return diag.Errorf("cannot get subnet with ID: %s. Error: %s", subnetID, err)
}
d.Set("name", subnet.Name)
d.Set("enable_dhcp", subnet.EnableDHCP)
d.Set("cidr", subnet.CIDR)
d.Set("network_id", subnet.NetworkID)
dns := make([]string, len(subnet.DNSNameservers))
for i, ns := range subnet.DNSNameservers {
dns[i] = ns.String()
}
d.Set("dns_nameservers", dns)
hrs := make([]map[string]string, len(subnet.HostRoutes))
for i, hr := range subnet.HostRoutes {
hR := map[string]string{"destination": "", "nexthop": ""}
hR["destination"] = hr.Destination.String()
hR["nexthop"] = hr.NextHop.String()
hrs[i] = hR
}
d.Set("host_routes", hrs)
d.Set("region_id", subnet.RegionID)
d.Set("project_id", subnet.ProjectID)
d.Set("gateway_ip", subnet.GatewayIP.String())
fields := []string{"connect_to_network_router"}
revertState(d, &fields)
if subnet.GatewayIP == nil {
d.Set("connect_to_network_router", false)
d.Set("gateway_ip", disable)
}
metadataMap, metadataReadOnly := PrepareMetadata(subnet.Metadata)
if err = d.Set("metadata_map", metadataMap); err != nil {
return diag.FromErr(err)
}
if err = d.Set("metadata_read_only", metadataReadOnly); err != nil {
return diag.FromErr(err)
}
log.Println("[DEBUG] Finish subnet reading")
return diags
}
func resourceSubnetUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Println("[DEBUG] Start subnet updating")
subnetID := d.Id()
log.Printf("[DEBUG] Subnet id = %s", subnetID)
config := m.(*Config)
clientV2 := config.CloudClient
regionID, projectID, err := GetRegionIDandProjectID(ctx, clientV2, d)
if err != nil {
return diag.FromErr(err)
}
clientV2.Region = regionID
clientV2.Project = projectID
updateOpts := &edgecloudV2.SubnetworkUpdateRequest{}
if d.HasChange("name") {
updateOpts.Name = d.Get("name").(string)
}
updateOpts.EnableDHCP = d.Get("enable_dhcp").(bool)
// In the structure, the field is mandatory for the ability to transfer the absence of data,
// if you do not initialize it with a empty list, marshalling will send null and receive a validation error.
dnsNameservers := d.Get("dns_nameservers").([]interface{})
updateOpts.DNSNameservers = make([]net.IP, 0)
if len(dnsNameservers) > 0 {
ns := dnsNameservers
dns := make([]net.IP, len(ns))
for i, s := range ns {
dns[i] = net.ParseIP(s.(string))
}
updateOpts.DNSNameservers = dns
}
hostRoutes := d.Get("host_routes").([]interface{})
updateOpts.HostRoutes = make([]edgecloudV2.HostRoute, 0)
if len(hostRoutes) > 0 {
updateOpts.HostRoutes, err = extractHostRoutesMapV2(hostRoutes)
if err != nil {
return diag.FromErr(err)
}
}
if d.HasChange("gateway_ip") {
_, newValue := d.GetChange("gateway_ip")
if newValue.(string) != disable {
gatewayIP := net.ParseIP(newValue.(string))
updateOpts.GatewayIP = &gatewayIP
}
}
_, _, err = clientV2.Subnetworks.Update(ctx, subnetID, updateOpts)
if err != nil {
return diag.FromErr(err)
}
if d.HasChange("metadata_map") {
_, nmd := d.GetChange("metadata_map")
meta, err := MapInterfaceToMapString(nmd)
if err != nil {
return diag.Errorf("metadata wrong fmt. Error: %s", err)
}
metaSubnet := edgecloudV2.Metadata(*meta)
_, err = clientV2.Subnetworks.MetadataUpdate(ctx, subnetID, &metaSubnet)
if err != nil {
return diag.Errorf("cannot update metadata. Error: %s", err)
}
}
d.Set("last_updated", time.Now().Format(time.RFC850))
log.Println("[DEBUG] Finish subnet updating")
return resourceSubnetRead(ctx, d, m)
}
func resourceSubnetDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Println("[DEBUG] Start subnet deleting")
var diags diag.Diagnostics
config := m.(*Config)
clientV2 := config.CloudClient
subnetID := d.Id()
log.Printf("[DEBUG] Subnet id = %s", subnetID)
regionID, projectID, err := GetRegionIDandProjectID(ctx, clientV2, d)
if err != nil {
return diag.FromErr(err)
}
clientV2.Region = regionID
clientV2.Project = projectID
results, resp, err := clientV2.Subnetworks.Delete(ctx, subnetID)
if err != nil {
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
log.Printf("[DEBUG] Finish of Subnet deleting")
return diags
}
return diag.FromErr(err)
}
taskID := results.Tasks[0]
err = utilV2.WaitForTaskComplete(ctx, clientV2, taskID)
if err != nil {
return diag.FromErr(err)
}
d.SetId("")
log.Printf("[DEBUG] Finish of subnet deleting")
return diags
}