-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathstructures_vps.go
112 lines (92 loc) · 2.67 KB
/
structures_vps.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
package main
import (
"fmt"
"log"
"net"
"strconv"
"strings"
"github.com/transip/gotransip/v6/ipaddress"
"github.com/transip/gotransip/v6/vps"
)
// Transfrom the terraform rules to the API rules (FirewallRule)
func vpsFirewallRulesExpand(stateRules []interface{}) ([]vps.FirewallRule, error) {
rules := make([]vps.FirewallRule, len(stateRules))
for i, rule := range stateRules {
r, err := vpsFirewallRuleExpand(rule)
if err != nil {
return nil, err
}
rules[i] = *r
}
return rules, nil
}
// Transfrom the terraform rule to the API rule (FirewallRule)
func vpsFirewallRuleExpand(i interface{}) (*vps.FirewallRule, error) {
// Top level rule is <string> = <value>
rawRule := i.(map[string]interface{})
log.Printf("%+v\n", rawRule)
// Parse port (can be X or X-Y, if range, split it)
var startPort, endPort int
rawPorts := strings.Split(rawRule["port"].(string), "-")
if len(rawPorts) < 2 {
startPort, _ = strconv.Atoi(rawPorts[0])
endPort = startPort
} else {
startPort, _ = strconv.Atoi(rawPorts[0])
endPort, _ = strconv.Atoi(rawPorts[1])
}
// Parse whitelist IP addresses
rawAddresses := rawRule["whitelist"].([]interface{})
ipAdresses := make([]ipaddress.IPRange, len(rawAddresses))
for i, ip := range rawAddresses {
// Parse the IP
_, ipNetwork, err := net.ParseCIDR(ip.(string))
// Check if we had some trouble parsing
if err != nil {
return nil, fmt.Errorf("failed to parse %q: %s", ip, err)
}
ipAdresses[i].IPNet = *ipNetwork
}
// Create the rule object
rule := &vps.FirewallRule{
Description: rawRule["description"].(string),
StartPort: startPort,
EndPort: endPort,
Protocol: rawRule["protocol"].(string),
Whitelist: ipAdresses,
}
return rule, nil
}
// Transform the API rule (FirewallRule) to terraform rule
func vpsFirewallRulesFlatten(rules []vps.FirewallRule) []interface{} {
// Flatten each rule
stateRules := make([]interface{}, len(rules))
for i, rule := range rules {
stateRules[i] = vpsFirewallRuleFlatten(&rule)
}
// Return the set
return stateRules
}
// Transform the API rule (FirewallRule) to terraform rule
func vpsFirewallRuleFlatten(rule *vps.FirewallRule) map[string]interface{} {
// Parse the port
var port string
if rule.StartPort != rule.EndPort {
port = fmt.Sprintf("%d-%d", rule.StartPort, rule.EndPort)
} else {
port = strconv.Itoa(rule.StartPort)
}
// Parse the IP addresses
ipAdresses := make([]interface{}, len(rule.Whitelist))
for i, ip := range rule.Whitelist {
ipAdresses[i] = ip.IPNet.String()
}
// Build
res := map[string]interface{}{
"description": rule.Description,
"protocol": rule.Protocol,
"port": port,
"whitelist": ipAdresses,
}
return res
}