/
resource_ibm_multi_vlan_firewall.go
424 lines (389 loc) · 14.5 KB
/
resource_ibm_multi_vlan_firewall.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
package ibm
import (
"fmt"
"log"
"strconv"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/softlayer/softlayer-go/datatypes"
"github.com/softlayer/softlayer-go/filter"
"github.com/softlayer/softlayer-go/helpers/location"
"github.com/softlayer/softlayer-go/helpers/product"
"github.com/softlayer/softlayer-go/services"
"github.com/softlayer/softlayer-go/sl"
)
func resourceIBMMultiVlanFirewall() *schema.Resource {
return &schema.Resource{
Create: resourceIBMNetworkMultiVlanCreate,
Read: resourceIBMMultiVlanFirewallRead,
Delete: resourceIBMFirewallDelete,
Update: resourceIBMMultiVlanFirewallUpdate,
Exists: resourceIBMMultiVLanFirewallExists,
Importer: &schema.ResourceImporter{},
Schema: map[string]*schema.Schema{
"datacenter": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Datacenter name",
},
"pod": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return strings.TrimSpace(old) == strings.TrimSpace(new)
},
Description: "POD name",
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "name",
},
"public_vlan_id": {
Type: schema.TypeInt,
Computed: true,
Description: "Public VLAN id",
},
"private_vlan_id": {
Type: schema.TypeInt,
Computed: true,
Description: "Private VLAN id",
},
"firewall_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateAllowedStringValue([]string{"FortiGate Firewall Appliance HA Option", "FortiGate Security Appliance"}),
Description: "Firewall type",
},
"public_ip": {
Type: schema.TypeString,
Computed: true,
Description: "Public IP Address",
},
"public_ipv6": {
Type: schema.TypeString,
Computed: true,
Description: "Public IPV6 IP",
},
"private_ip": {
Type: schema.TypeString,
Computed: true,
Description: "Private IP Address",
},
"username": {
Type: schema.TypeString,
Computed: true,
Description: "User name",
},
"password": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
Description: "Password",
},
"addon_configuration": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: `High Availability - [Web Filtering Add-on, NGFW Add-on, AV Add-on] or [Web Filtering Add-on, NGFW Add-on, AV Add-on]`,
},
},
}
}
const (
productPackageFilter = `{"keyName":{"operation":"FIREWALL_APPLIANCE"}}`
complexType = "SoftLayer_Container_Product_Order_Network_Protection_Firewall_Dedicated"
productPackageServiceMask = "description,prices.locationGroupId,prices.id"
mandatoryFirewallType = "FortiGate Security Appliance"
multiVlansMask = "id,customerManagedFlag,datacenter.name,bandwidthAllocation"
)
func resourceIBMNetworkMultiVlanCreate(d *schema.ResourceData, meta interface{}) error {
sess := meta.(ClientSession).SoftLayerSession()
name := d.Get("name").(string)
FirewallType := d.Get("firewall_type").(string)
datacenter := d.Get("datacenter").(string)
pod := d.Get("pod").(string)
podName := datacenter + "." + pod
PodService := services.GetNetworkPodService(sess)
podMask := `frontendRouterId,name`
// 1.Getting the router ID
routerids, err := PodService.Filter(filter.Path("datacenterName").Eq(datacenter).Build()).Mask(podMask).GetAllObjects()
if err != nil {
return fmt.Errorf("Encountered problem trying to get the router ID: %s", err)
}
var routerid int
for _, iterate := range routerids {
if *iterate.Name == podName {
routerid = *iterate.FrontendRouterId
}
}
//2.Get the datacenter id
dc, err := location.GetDatacenterByName(sess, datacenter, "id")
if err != nil {
return fmt.Errorf("Encountered problem trying to get the Datacenter ID: %s", err)
}
locationservice := services.GetLocationService(sess)
//3. get the pricegroups that the datacenter belongs to
priceidds, _ := locationservice.Id(*dc.Id).GetPriceGroups()
var listofpriceids []int
//store all the pricegroups a datacenter belongs to
for _, priceidd := range priceidds {
listofpriceids = append(listofpriceids, *priceidd.Id)
}
//4.get the addons that are specified
var addonconfigurations []interface{}
if _, ok := d.GetOk("addon_configuration"); ok {
addonconfigurations, ok = d.Get("addon_configuration").([]interface{})
}
var actualaddons []string
for _, addons := range addonconfigurations {
actualaddons = append(actualaddons, addons.(string))
}
//appending the 20000GB Bandwidth item as it is mandatory
actualaddons = append(actualaddons, FirewallType, "20000 GB Bandwidth Allotment")
//appending the Fortigate Security Appliance as it is mandatory parameter for placing an order
if FirewallType != mandatoryFirewallType {
actualaddons = append(actualaddons, mandatoryFirewallType)
}
//5. Getting the priceids of items which have to be ordered
priceItems := []datatypes.Product_Item_Price{}
for _, addon := range actualaddons {
actualpriceid, err := product.GetPriceIDByPackageIdandLocationGroups(sess, listofpriceids, 863, addon)
if err != nil || actualpriceid == 0 {
return fmt.Errorf("Encountered problem trying to get priceIds of items which have to be ordered: %s", err)
}
priceItem := datatypes.Product_Item_Price{
Id: &actualpriceid,
}
priceItems = append(priceItems, priceItem)
}
//6.Get the package ID
productpackageservice, _ := services.GetProductPackageService(sess).Filter(productPackageFilter).Mask(`id`).GetAllObjects()
var productid int
for _, packageid := range productpackageservice {
productid = *packageid.Id
}
//7. Populate the container which needs to be sent for Verify order and Place order
productOrderContainer := datatypes.Container_Product_Order_Network_Protection_Firewall_Dedicated{
Container_Product_Order: datatypes.Container_Product_Order{
PackageId: &productid,
Prices: priceItems,
Quantity: sl.Int(1),
Location: &datacenter,
ComplexType: sl.String(complexType),
},
Name: sl.String(name),
RouterId: &routerid,
}
//8.Calling verify order
_, err = services.GetProductOrderService(sess.SetRetries(0)).
VerifyOrder(&productOrderContainer)
if err != nil {
return fmt.Errorf("Error during Verify order for Creating: %s", err)
}
//9.Calling place order
receipt, err := services.GetProductOrderService(sess.SetRetries(0)).
PlaceOrder(&productOrderContainer, sl.Bool(false))
if err != nil {
return fmt.Errorf("Error during Place order for Creating: %s", err)
}
_, vlan, _, err := findDedicatedFirewallByOrderId(sess, *receipt.OrderId, d)
if err != nil {
return fmt.Errorf("Error during creation of dedicated hardware firewall: %s", err)
}
id := *vlan.NetworkFirewall.Id
d.SetId(fmt.Sprintf("%d", id))
log.Printf("[INFO] Firewall ID: %s", d.Id())
return resourceIBMMultiVlanFirewallRead(d, meta)
}
func resourceIBMMultiVlanFirewallRead(d *schema.ResourceData, meta interface{}) error {
sess := meta.(ClientSession).SoftLayerSession()
fwID, _ := strconv.Atoi(d.Id())
firewalls, err := services.GetAccountService(sess).
Filter(filter.Build(
filter.Path("networkGateways.networkFirewall.id").
Eq(strconv.Itoa(fwID)))).
Mask(multiVlanMask).
GetNetworkGateways()
if err != nil {
return fmt.Errorf("Error retrieving firewall information: %s", err)
}
d.Set("datacenter", *firewalls[0].NetworkFirewall.Datacenter.Name)
if *firewalls[0].NetworkFirewall.CustomerManagedFlag && *firewalls[0].MemberCount == 1 {
d.Set("firewall_type", "FortiGate Security Appliance")
} else if *firewalls[0].NetworkFirewall.CustomerManagedFlag && *firewalls[0].MemberCount > 1 {
d.Set("firewall_type", "FortiGate Firewall Appliance HA Option")
}
addonConfiguration := make([]interface{}, 0, len(firewalls[0].NetworkFirewall.BillingItem.ActiveChildren))
for _, elem := range firewalls[0].NetworkFirewall.BillingItem.ActiveChildren {
if *elem.Description != "20000 GB Bandwidth Allotment" && *elem.Description != "FortiGate Firewall Appliance HA Option" {
addonConfiguration = append(addonConfiguration, *elem.Description)
}
}
if len(addonConfiguration) > 0 {
d.Set("addon_configuration", addonConfiguration)
}
pod := *firewalls[0].NetworkFirewall.BillingItem.Notes
pod = "pod" + strings.SplitAfter(pod, "pod")[1]
d.Set("pod", &pod)
d.Set("name", *firewalls[0].Name)
d.Set("public_ip", *firewalls[0].PublicIpAddress.IpAddress)
d.Set("public_ipv6", firewalls[0].PublicIpv6Address.IpAddress)
d.Set("private_ip", *firewalls[0].PrivateIpAddress.IpAddress)
d.Set("public_vlan_id", *firewalls[0].PublicVlan.Id)
d.Set("private_vlan_id", *firewalls[0].PrivateVlan.Id)
d.Set("username", *firewalls[0].NetworkFirewall.ManagementCredentials.Username)
d.Set("password", *firewalls[0].NetworkFirewall.ManagementCredentials.Password)
return nil
}
func resourceIBMMultiVlanFirewallUpdate(d *schema.ResourceData, meta interface{}) error {
if d.HasChange("addon_configuration") {
sess := meta.(ClientSession).SoftLayerSession()
fwID, _ := strconv.Atoi(d.Id())
old, new := d.GetChange("addon_configuration")
oldaddons := old.([]interface{})
newaddons := new.([]interface{})
var oldaddon, newaddon, add []string
for _, v := range oldaddons {
oldaddon = append(oldaddon, v.(string))
}
for _, v := range newaddons {
newaddon = append(newaddon, v.(string))
}
// 1. Remove old addons no longer appearing in the new set
// 2. Add new addons not already provisioned
remove := listdifference(oldaddon, newaddon)
add = listdifference(newaddon, oldaddon)
if len(remove) > 0 {
firewalls, err := services.GetAccountService(sess).
Filter(filter.Build(
filter.Path("networkGateways.networkFirewall.id").
Eq(strconv.Itoa(fwID)))).
Mask(multiVlanMask).
GetNetworkGateways()
if err != nil {
return fmt.Errorf("Some error occured while fetching the information of the Multi-Vlan Firewall")
}
for _, i := range remove {
for _, j := range firewalls[0].NetworkFirewall.BillingItem.ActiveChildren {
if i == *j.Description {
cancelimmediately := true
cancelAssociatedBillingItems := false
reason := "No longer needed"
customerNote := "No longer needed"
billingitemservice, err := services.GetBillingItemService(sess).Id(*j.Id).CancelItem(&cancelimmediately, &cancelAssociatedBillingItems, &reason, &customerNote)
if err != nil || !billingitemservice {
return fmt.Errorf("Error while cancelling the addon")
}
}
}
}
}
if len(add) > 0 {
datacentername, ok := d.GetOk("datacenter")
if !ok {
return fmt.Errorf("The attribute datacenter is not defined")
}
//2.Get the datacenter id
dc, err := location.GetDatacenterByName(sess, datacentername.(string), "id")
if err != nil {
return fmt.Errorf("Datacenter not found")
}
locationservice := services.GetLocationService(sess)
//3. get the pricegroups that the datacenter belongs to
priceidds, _ := locationservice.Id(*dc.Id).GetPriceGroups()
var listofpriceids []int
//store all the pricegroups a datacenter belongs to
for _, priceidd := range priceidds {
listofpriceids = append(listofpriceids, *priceidd.Id)
}
priceItems := []datatypes.Product_Item_Price{}
for _, addon := range add {
actualpriceid, err := product.GetPriceIDByPackageIdandLocationGroups(sess, listofpriceids, 863, addon)
if err != nil || actualpriceid == 0 {
return fmt.Errorf("The addon or the firewall is not available for the datacenter you have selected. Please enter a different datacenter")
}
priceItem := datatypes.Product_Item_Price{
Id: &actualpriceid,
}
priceItems = append(priceItems, priceItem)
}
//6.Get the package ID
productpackageservice, _ := services.GetProductPackageService(sess).Filter(productPackageFilter).Mask(`id`).GetAllObjects()
var productid int
for _, packageid := range productpackageservice {
productid = *packageid.Id
}
var properties []datatypes.Container_Product_Order_Property
t := time.Now()
upgradeproductOrderContainer := datatypes.Container_Product_Order_Network_Protection_Firewall_Dedicated_Upgrade{
Container_Product_Order_Network_Protection_Firewall_Dedicated: datatypes.Container_Product_Order_Network_Protection_Firewall_Dedicated{
Container_Product_Order: datatypes.Container_Product_Order{
PackageId: &productid,
Prices: priceItems,
ComplexType: sl.String(complexType),
Properties: append(properties, datatypes.Container_Product_Order_Property{
Name: sl.String("MAINTENANCE_WINDOW"),
Value: sl.String(time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), 0, t.Location()).UTC().String()),
}),
},
},
FirewallId: &fwID,
}
//8.Calling verify order
_, err = services.GetProductOrderService(sess.SetRetries(0)).
VerifyOrder(&upgradeproductOrderContainer)
if err != nil {
return fmt.Errorf("Error during Verify order for Updating: %s", err)
}
//9.Calling place order
receipt, err := services.GetProductOrderService(sess.SetRetries(0)).
PlaceOrder(&upgradeproductOrderContainer, sl.Bool(false))
if err != nil {
return fmt.Errorf("Error during Place order for Updating: %s", err)
}
_, _, _, err = findDedicatedFirewallByOrderId(sess, *receipt.OrderId, d)
if err != nil {
return fmt.Errorf("Error during creation of dedicated hardware firewall: %s", err)
}
}
}
return resourceIBMMultiVlanFirewallRead(d, meta)
}
func resourceIBMMultiVLanFirewallExists(d *schema.ResourceData, meta interface{}) (bool, error) {
sess := meta.(ClientSession).SoftLayerSession()
fwID, _ := strconv.Atoi(d.Id())
firewalls, err := services.GetAccountService(sess).
Filter(filter.Build(
filter.Path("networkGateways.networkFirewall.id").
Eq(strconv.Itoa(fwID)))).
Mask(multiVlanMask).
GetNetworkGateways()
if err != nil {
return false, fmt.Errorf("Error retrieving firewall information: %s", err)
}
if firewalls[0].NetworkFirewall.BillingItem == nil {
return false, nil
}
return true, nil
}
//This function takes two lists and returns the difference between the two lists
//listdifference([1,2] [2,3]) = [1]
func listdifference(a, b []string) []string {
mb := map[string]bool{}
for _, x := range b {
mb[x] = true
}
ab := []string{}
for _, x := range a {
if _, ok := mb[x]; !ok {
ab = append(ab, x)
}
}
return ab
}