-
Notifications
You must be signed in to change notification settings - Fork 13
/
resource_static_route.go
455 lines (353 loc) · 15.7 KB
/
resource_static_route.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
451
452
453
454
455
package ddcloud
import (
"fmt"
"github.com/DimensionDataResearch/dd-cloud-compute-terraform/retry"
"github.com/DimensionDataResearch/go-dd-cloud-compute/compute"
"github.com/hashicorp/terraform/helper/schema"
"log"
)
const (
resourceKeyStaticRouteName = "name"
resourceKeyStaticRouteDescription = "description"
resourceKeyStaticRouteNetworkdomain = "networkdomain"
resourceKeyStaticRouteIpVersion = "ip_version"
resourceKeyStaticRouteDestinationNetworkAddress = "destination_network_address"
resourceKeyStaticRouteDestinationPrefixSize = "destination_prefix_size"
resourceKeyStaticRouteNextHopAddress = "next_hop_address"
resourceKeyStaticRouteState = "state"
resourceKeyStaticRouteDataCenter = "data_center"
resourceKeyStaticRouteOverwriteMatchingSystemDefault = "overwrite_system_default"
)
func resourceStaticRoute() *schema.Resource {
return &schema.Resource{
Exists: resourceStaticRouteExists,
Create: resourceStaticRouteCreate,
Read: resourceStaticRouteRead,
Update: resourceStaticRouteUpdate,
Delete: resourceStaticRouteDelete,
Importer: &schema.ResourceImporter{
State: resourceStaticRouteImport,
},
Schema: map[string]*schema.Schema{
resourceKeyStaticRouteName: &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "A name for static route",
},
resourceKeyStaticRouteDescription: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Description for static route",
},
resourceKeyStaticRouteNetworkdomain: &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "Network domain ID",
},
resourceKeyStaticRouteIpVersion: &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "Either IPv4 or IPv6",
},
resourceKeyStaticRouteDestinationNetworkAddress: &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "Destination address",
},
resourceKeyStaticRouteDestinationPrefixSize: &schema.Schema{
Type: schema.TypeInt,
Required: true,
Description: "Integer prefix defining the size of the network",
},
resourceKeyStaticRouteNextHopAddress: &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "Gateway address in the form of an INET gateway, CPNC gateway or an address on an Attached VLAN in the same Network Domain",
},
resourceKeyStaticRouteState: &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "State of Static Route",
},
resourceKeyStaticRouteDataCenter: &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Data center where Static Route reside",
},
resourceKeyStaticRouteOverwriteMatchingSystemDefault: &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Description: "Overwrite existing static route if a matching address space found. This attribute is " +
"intended to overwrite system default static route. It will delete matching a system default route if found " +
"and create a client static route",
},
},
}
}
func resourceStaticRouteExists(data *schema.ResourceData, provider interface{}) (bool, error) {
log.Println("resourceStaticRouteExists")
client := provider.(*providerState).Client()
id := data.Id()
staticRoute, _ := client.GetStaticRoute(id)
exists := staticRoute != nil
return exists, nil
}
func resourceStaticRouteCreate(data *schema.ResourceData, provider interface{}) error {
log.Println("resourceStaticRouteCreate")
providerState := provider.(*providerState)
apiClient := providerState.Client()
var networkDomainId, name, description, ipVersion, destinationNetworkAddress, nextHopAddress string
var destinationPrefixSize int
networkDomainId = data.Get(resourceKeyStaticRouteNetworkdomain).(string)
name = data.Get(resourceKeyStaticRouteName).(string)
description = data.Get(resourceKeyStaticRouteDescription).(string)
ipVersion = data.Get(resourceKeyStaticRouteIpVersion).(string)
destinationNetworkAddress = data.Get(resourceKeyStaticRouteDestinationNetworkAddress).(string)
destinationPrefixSize = data.Get(resourceKeyStaticRouteDestinationPrefixSize).(int)
nextHopAddress = data.Get(resourceKeyStaticRouteNextHopAddress).(string)
enableOverwriteSystemDefault := data.Get(resourceKeyStaticRouteOverwriteMatchingSystemDefault).(bool)
// Overwrite system default static route
if enableOverwriteSystemDefault {
// Default System Static Route
route, _ := apiClient.GetStaticRouteByAddress(nil, networkDomainId, destinationNetworkAddress, destinationPrefixSize)
if route != nil {
log.Printf("System Static route with matching destinationNetworkAddress found. "+
"networkDomainId:%s, name: %s, description: %s, ipVersion: %s, "+
"destinationNetworkAddress: %s, destinationPrefixSize: %d, nextHopAddress: %s",
networkDomainId, name, description, ipVersion, destinationNetworkAddress, destinationPrefixSize, nextHopAddress)
err := apiClient.DeleteStaticRoute(route.ID)
if err != nil {
return err
}
log.Printf("Default system route deleted successfully. Name:%s", name)
data.SetId("") // Mark resource as deleted
//return nil
}
}
log.Printf("Create static route with networkDomainId:%s, name: %s, description: %s, ipVersion: %s, "+
"destinationNetworkAddress: %s, destinationPrefixSize: %d, nextHopAddress: %s",
networkDomainId, name, description, ipVersion, destinationNetworkAddress, destinationPrefixSize, nextHopAddress)
var staticRouteID string
operationDescription := fmt.Sprintf("Create static route '%s'", name)
err := providerState.RetryAction(operationDescription, func(context retry.Context) {
// CloudControl has issues if more than one asynchronous operation is initated at a time (returns UNEXPECTED_ERROR).
asyncLock := providerState.AcquireAsyncOperationLock("Create static route '%s'", name)
defer asyncLock.Release()
var deployError error
staticRouteID, deployError = apiClient.CreateStaticRoute(networkDomainId, name, description, ipVersion,
destinationNetworkAddress, destinationPrefixSize, nextHopAddress)
if compute.IsResourceBusyError(deployError) {
context.Retry()
} else if deployError != nil {
context.Fail(deployError)
}
asyncLock.Release()
})
if err != nil {
return err
}
data.SetId(staticRouteID)
log.Printf("Static Route '%s' is being provisioned...", staticRouteID)
resource, err := apiClient.WaitForDeploy(compute.ResourceTypeStaticRoutes, staticRouteID, resourceCreateTimeoutVLAN)
if err != nil {
return err
}
data.Partial(true)
// Capture additional properties that are only available after deployment.
staticRoute := resource.(*compute.StaticRoute)
data.Set(resourceKeyStaticRouteState, staticRoute.State)
data.SetPartial(resourceKeyStaticRouteState)
data.Set(resourceKeyStaticRouteDataCenter, staticRoute.DataCenter)
data.SetPartial(resourceKeyStaticRouteDataCenter)
err = applyNetworkDomainDefaultFirewallRules(data, apiClient)
if err != nil {
return err
}
data.Partial(false)
return nil
}
func resourceStaticRouteRead(data *schema.ResourceData, provider interface{}) error {
log.Println("resourceStaticRouteRead")
var networkDomainId, name, description, ipVersion, destinationNetworkAddress, nextHopAddress string
var destinationPrefixSize int
id := data.Id()
networkDomainId = data.Get(resourceKeyStaticRouteNetworkdomain).(string)
name = data.Get(resourceKeyStaticRouteName).(string)
description = data.Get(resourceKeyStaticRouteDescription).(string)
ipVersion = data.Get(resourceKeyStaticRouteIpVersion).(string)
destinationNetworkAddress = data.Get(resourceKeyStaticRouteDestinationNetworkAddress).(string)
destinationPrefixSize = data.Get(resourceKeyStaticRouteDestinationPrefixSize).(int)
nextHopAddress = data.Get(resourceKeyStaticRouteNextHopAddress).(string)
log.Printf("Reading static route. networkDomainId:%s, name: %s, description: %s, ipVersion: %s, "+
"destinationNetworkAddress: %s, destinationPrefixSize: %d, nextHopAddress: %s",
networkDomainId, name, description, ipVersion, destinationNetworkAddress, destinationPrefixSize, nextHopAddress)
apiClient := provider.(*providerState).Client()
staticRoute, err := apiClient.GetStaticRoute(id)
if err != nil {
return err
}
data.Partial(true)
if staticRoute != nil {
data.Set(resourceKeyStaticRouteNetworkdomain, staticRoute.NetworkDomainId)
data.SetPartial(resourceKeyStaticRouteNetworkdomain)
data.Set(resourceKeyStaticRouteName, staticRoute.Name)
data.SetPartial(resourceKeyStaticRouteName)
data.Set(resourceKeyStaticRouteDescription, staticRoute.Description)
data.SetPartial(resourceKeyStaticRouteDescription)
data.Set(resourceKeyStaticRouteIpVersion, staticRoute.IpVersion)
data.SetPartial(resourceKeyStaticRouteIpVersion)
data.Set(resourceKeyStaticRouteDestinationNetworkAddress, staticRoute.DestinationNetworkAddress)
data.SetPartial(resourceKeyStaticRouteDestinationNetworkAddress)
data.Set(resourceKeyStaticRouteDestinationPrefixSize, staticRoute.DestinationPrefixSize)
data.SetPartial(resourceKeyStaticRouteDestinationPrefixSize)
data.Set(resourceKeyStaticRouteNextHopAddress, staticRoute.NextHopAddress)
data.SetPartial(resourceKeyStaticRouteNextHopAddress)
}
data.Partial(false)
return nil
}
func resourceStaticRouteUpdate(data *schema.ResourceData, provider interface{}) error {
log.Println("resourceStaticRouteUpdate")
providerState := provider.(*providerState)
apiClient := providerState.Client()
id := data.Id()
name := data.Get(resourceKeyStaticRouteName).(string)
log.Println("resourceStaticRouteUpdate: Re-Create resource")
var networkDomainId, description, ipVersion, destinationNetworkAddress, nextHopAddress string
var destinationPrefixSize int
networkDomainId = data.Get(resourceKeyStaticRouteNetworkdomain).(string)
name = data.Get(resourceKeyStaticRouteName).(string)
description = data.Get(resourceKeyStaticRouteDescription).(string)
ipVersion = data.Get(resourceKeyStaticRouteIpVersion).(string)
destinationNetworkAddress = data.Get(resourceKeyStaticRouteDestinationNetworkAddress).(string)
destinationPrefixSize = data.Get(resourceKeyStaticRouteDestinationPrefixSize).(int)
nextHopAddress = data.Get(resourceKeyStaticRouteNextHopAddress).(string)
enableOverwriteSystemDefault := data.Get(resourceKeyStaticRouteOverwriteMatchingSystemDefault).(bool)
// update step 1: delete
log.Println("resourceStaticRouteUpdate: delete resource")
operationDescriptionDel := fmt.Sprintf("Deleting static route '%s'", name)
var errDel error
errDel = providerState.RetryAction(operationDescriptionDel, func(context retry.Context) {
asyncLock := providerState.AcquireAsyncOperationLock("Delete static route: '%s'", id)
defer asyncLock.Release()
deleteErr := apiClient.DeleteStaticRoute(id)
if compute.IsResourceBusyError(deleteErr) {
context.Retry()
} else if errDel != nil {
context.Fail(deleteErr)
}
asyncLock.Release()
})
if errDel != nil {
return errDel
}
// Update step 2: re-create
// Overwrite system default static route
if enableOverwriteSystemDefault {
// Default System Static Route
route, _ := apiClient.GetStaticRouteByAddress(nil, networkDomainId, destinationNetworkAddress, destinationPrefixSize)
if route != nil {
log.Printf("System Static route with matching destinationNetworkAddress found. "+
"networkDomainId:%s, name: %s, description: %s, ipVersion: %s, "+
"destinationNetworkAddress: %s, destinationPrefixSize: %d, nextHopAddress: %s",
networkDomainId, name, description, ipVersion, destinationNetworkAddress, destinationPrefixSize, nextHopAddress)
err := apiClient.DeleteStaticRoute(route.ID)
if err != nil {
return err
}
log.Printf("Default system route deleted successfully. Name:%s", name)
data.SetId("") // Mark resource as deleted
}
}
log.Printf("Create static route with networkDomainId:%s, name: %s, description: %s, ipVersion: %s, "+
"destinationNetworkAddress: %s, destinationPrefixSize: %d, nextHopAddress: %s",
networkDomainId, name, description, ipVersion, destinationNetworkAddress, destinationPrefixSize, nextHopAddress)
var staticRouteID string
operationDescription := fmt.Sprintf("Create static route '%s'", name)
err := providerState.RetryAction(operationDescription, func(context retry.Context) {
// CloudControl has issues if more than one asynchronous operation is initated at a time (returns UNEXPECTED_ERROR).
asyncLock := providerState.AcquireAsyncOperationLock("Create static route '%s'", name)
defer asyncLock.Release()
var deployError error
staticRouteID, deployError = apiClient.CreateStaticRoute(networkDomainId, name, description, ipVersion,
destinationNetworkAddress, destinationPrefixSize, nextHopAddress)
if compute.IsResourceBusyError(deployError) {
context.Retry()
} else if deployError != nil {
context.Fail(deployError)
}
asyncLock.Release()
})
if err != nil {
return err
}
data.SetId(staticRouteID)
log.Printf("Static Route '%s' is being provisioned...", staticRouteID)
resource, err := apiClient.WaitForDeploy(compute.ResourceTypeStaticRoutes, staticRouteID, resourceCreateTimeoutVLAN)
if err != nil {
return err
}
data.Partial(true)
// Capture additional properties that are only available after deployment.
staticRoute := resource.(*compute.StaticRoute)
data.Set(resourceKeyStaticRouteState, staticRoute.State)
data.SetPartial(resourceKeyStaticRouteState)
data.Set(resourceKeyStaticRouteDataCenter, staticRoute.DataCenter)
data.SetPartial(resourceKeyStaticRouteDataCenter)
err = applyNetworkDomainDefaultFirewallRules(data, apiClient)
if err != nil {
return err
}
data.Partial(false)
return nil
}
func resourceStaticRouteDelete(data *schema.ResourceData, provider interface{}) error {
log.Println("resourceStaticRouteDelete")
providerState := provider.(*providerState)
apiClient := providerState.Client()
id := data.Id()
name := data.Get(resourceKeyStaticRouteName).(string)
operationDescription := fmt.Sprintf("Deleting static route '%s'", name)
var err error
err = providerState.RetryAction(operationDescription, func(context retry.Context) {
asyncLock := providerState.AcquireAsyncOperationLock("Delete static route: '%s'", id)
defer asyncLock.Release()
deleteErr := apiClient.DeleteStaticRoute(id)
if compute.IsResourceBusyError(deleteErr) {
context.Retry()
} else if err != nil {
context.Fail(deleteErr)
}
asyncLock.Release()
})
if err != nil {
return err
}
return nil
}
func resourceStaticRouteImport(data *schema.ResourceData, provider interface{}) (importedData []*schema.ResourceData, err error) {
log.Println("resourceStaticRouteImport")
providerState := provider.(*providerState)
apiClient := providerState.Client()
staticRouteId := data.Id()
name := data.Get(resourceKeyStaticRouteName).(string)
log.Printf("Import Static Route with ID: %s, Name:%s", staticRouteId, name)
var staticRoute *compute.StaticRoute
staticRoute, err = apiClient.GetStaticRoute(staticRouteId)
if err != nil {
return
}
if staticRoute == nil {
err = fmt.Errorf("static route ID: %s, name:%s not found", staticRouteId, name)
return
}
data.Set(resourceKeyStaticRouteNetworkdomain, staticRoute.NetworkDomainId)
data.Set(resourceKeyStaticRouteName, staticRoute.Name)
data.Set(resourceKeyStaticRouteDescription, staticRoute.Description)
data.Set(resourceKeyStaticRouteIpVersion, staticRoute.IpVersion)
data.Set(resourceKeyStaticRouteDestinationNetworkAddress, staticRoute.DestinationNetworkAddress)
data.Set(resourceKeyStaticRouteDestinationPrefixSize, staticRoute.DestinationPrefixSize)
data.Set(resourceKeyStaticRouteNextHopAddress, staticRoute.NextHopAddress)
importedData = []*schema.ResourceData{data}
return
}