-
Notifications
You must be signed in to change notification settings - Fork 92
/
helpers.go
78 lines (69 loc) · 2.18 KB
/
helpers.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
package firewall
import (
"strings"
"github.com/linode/linodego"
"github.com/linode/terraform-provider-linode/linode/helper"
)
func expandFirewallStatus(disabled interface{}) linodego.FirewallStatus {
return map[bool]linodego.FirewallStatus{
true: linodego.FirewallDisabled,
false: linodego.FirewallEnabled,
}[disabled.(bool)]
}
func expandFirewallRules(ruleSpecs []interface{}) []linodego.FirewallRule {
rules := make([]linodego.FirewallRule, len(ruleSpecs))
for i, ruleSpec := range ruleSpecs {
ruleSpec := ruleSpec.(map[string]interface{})
rule := linodego.FirewallRule{}
rule.Label = ruleSpec["label"].(string)
rule.Action = ruleSpec["action"].(string)
rule.Protocol = linodego.NetworkProtocol(strings.ToUpper(ruleSpec["protocol"].(string)))
rule.Ports = ruleSpec["ports"].(string)
ipv4 := helper.ExpandStringList(ruleSpec["ipv4"].([]interface{}))
if len(ipv4) > 0 {
rule.Addresses.IPv4 = &ipv4
}
ipv6 := helper.ExpandStringList(ruleSpec["ipv6"].([]interface{}))
if len(ipv6) > 0 {
rule.Addresses.IPv6 = &ipv6
}
rules[i] = rule
}
return rules
}
func flattenFirewallLinodes(devices []linodego.FirewallDevice) []int {
linodes := make([]int, 0, len(devices))
for _, device := range devices {
if device.Entity.Type == linodego.FirewallDeviceLinode {
linodes = append(linodes, device.Entity.ID)
}
}
return linodes
}
func flattenFirewallRules(rules []linodego.FirewallRule) []map[string]interface{} {
specs := make([]map[string]interface{}, len(rules))
for i, rule := range rules {
specs[i] = map[string]interface{}{
"label": rule.Label,
"action": rule.Action,
"protocol": rule.Protocol,
"ports": rule.Ports,
"ipv4": rule.Addresses.IPv4,
"ipv6": rule.Addresses.IPv6,
}
}
return specs
}
func flattenFirewallDevices(devices []linodego.FirewallDevice) []map[string]interface{} {
governedDevices := make([]map[string]interface{}, len(devices))
for i, device := range devices {
governedDevices[i] = map[string]interface{}{
"id": device.ID,
"entity_id": device.Entity.ID,
"type": device.Entity.Type,
"label": device.Entity.Label,
"url": device.Entity.URL,
}
}
return governedDevices
}