forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
structure.go
113 lines (97 loc) · 2.78 KB
/
structure.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
package vcd
import (
"fmt"
"strconv"
"time"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
types "github.com/hmrc/vmware-govcd/types/v56"
)
func expandIPRange(configured []interface{}) types.IPRanges {
ipRange := make([]*types.IPRange, 0, len(configured))
for _, ipRaw := range configured {
data := ipRaw.(map[string]interface{})
ip := types.IPRange{
StartAddress: data["start_address"].(string),
EndAddress: data["end_address"].(string),
}
ipRange = append(ipRange, &ip)
}
ipRanges := types.IPRanges{
IPRange: ipRange,
}
return ipRanges
}
func expandFirewallRules(d *schema.ResourceData, gateway *types.EdgeGateway) ([]*types.FirewallRule, error) {
//firewallRules := make([]*types.FirewallRule, 0, len(configured))
firewallRules := gateway.Configuration.EdgeGatewayServiceConfiguration.FirewallService.FirewallRule
rulesCount := d.Get("rule.#").(int)
for i := 0; i < rulesCount; i++ {
prefix := fmt.Sprintf("rule.%d", i)
var protocol *types.FirewallRuleProtocols
switch d.Get(prefix + ".protocol").(string) {
case "tcp":
protocol = &types.FirewallRuleProtocols{
TCP: true,
}
case "udp":
protocol = &types.FirewallRuleProtocols{
UDP: true,
}
case "icmp":
protocol = &types.FirewallRuleProtocols{
ICMP: true,
}
default:
protocol = &types.FirewallRuleProtocols{
Any: true,
}
}
rule := &types.FirewallRule{
//ID: strconv.Itoa(len(configured) - i),
IsEnabled: true,
MatchOnTranslate: false,
Description: d.Get(prefix + ".description").(string),
Policy: d.Get(prefix + ".policy").(string),
Protocols: protocol,
Port: getNumericPort(d.Get(prefix + ".destination_port")),
DestinationPortRange: d.Get(prefix + ".destination_port").(string),
DestinationIP: d.Get(prefix + ".destination_ip").(string),
SourcePort: getNumericPort(d.Get(prefix + ".source_port")),
SourcePortRange: d.Get(prefix + ".source_port").(string),
SourceIP: d.Get(prefix + ".source_ip").(string),
EnableLogging: false,
}
firewallRules = append(firewallRules, rule)
}
return firewallRules, nil
}
func getProtocol(protocol types.FirewallRuleProtocols) string {
if protocol.TCP {
return "tcp"
}
if protocol.UDP {
return "udp"
}
if protocol.ICMP {
return "icmp"
}
return "any"
}
func getNumericPort(portrange interface{}) int {
i, err := strconv.Atoi(portrange.(string))
if err != nil {
return -1
}
return i
}
func getPortString(port int) string {
if port == -1 {
return "any"
}
portstring := strconv.Itoa(port)
return portstring
}
func retryCall(seconds int, f resource.RetryFunc) error {
return resource.Retry(time.Duration(seconds)*time.Second, f)
}