-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexport_firewall_rule.go
More file actions
113 lines (100 loc) · 2.91 KB
/
Copy pathexport_firewall_rule.go
File metadata and controls
113 lines (100 loc) · 2.91 KB
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 main
import (
"compute-api/compute"
"fmt"
"strings"
)
func makeFirewallRuleResourceName(uniquenessKey int) string {
return fmt.Sprintf("firewall_rule%02d", uniquenessKey)
}
const configurationTemplateFirewallRule = `
resource "ddcloud_firewall_rule" "%s" {
name = "%s"
placement = "first"
action = "%s"
enabled = %t
ip_version = "%s"
protocol = "%s"
%s= "%s"
source_port = "%s"
%s= "%s"
destination_port = "%s"
networkdomain = "%s"
}
`
// ExportFirewallRule exports a ddcloud_firewallRule resource to Terraform configuration.
func (exporter *Exporter) ExportFirewallRule(firewallRule compute.FirewallRule, networkDomainID string, natResourceName string, uniquenessKey int) error {
if firewallRule.RuleType == "DEFAULT_RULE" {
return nil // Ignore built-in rules.
}
var (
sourceLabel string
source string
sourcePort string
destinationLabel string
destination string
destinationPort string
)
if firewallRule.Source.Port != nil {
sourcePort = fmt.Sprintf("%d", firewallRule.Source.Port.Begin)
} else {
sourcePort = "any"
}
sourceAddress := firewallRule.Source.IPAddress
if sourceAddress != nil {
if sourceAddress.PrefixSize != nil {
sourceLabel = "source_network "
source = fmt.Sprintf("%s/%d", sourceAddress.Address, *sourceAddress.PrefixSize)
} else {
sourceLabel = "source_address "
source = strings.ToLower(firewallRule.Source.IPAddress.Address)
}
}
if firewallRule.Destination.Port != nil {
destinationPort = fmt.Sprintf("%d", firewallRule.Destination.Port.Begin)
} else {
destinationPort = "any"
}
destinationAddress := firewallRule.Destination.IPAddress
if destinationAddress != nil {
if !isEmpty(natResourceName) {
destinationLabel = "destination_address "
destination = fmt.Sprintf("${ddcloud_nat.%s.public_ipv4}", natResourceName)
} else if destinationAddress.PrefixSize != nil {
destinationLabel = "destination_network "
destination = fmt.Sprintf("%s/%d", destinationAddress.Address, *destinationAddress.PrefixSize)
} else {
destinationLabel = "destination_address "
destination = strings.ToLower(firewallRule.Destination.IPAddress.Address)
}
}
configuration := strings.TrimSpace(
fmt.Sprintf(configurationTemplateFirewallRule,
makeFirewallRuleResourceName(uniquenessKey),
firewallRule.Name,
convertFirewallAction(firewallRule.Action),
firewallRule.Enabled,
firewallRule.IPVersion,
firewallRule.Protocol,
sourceLabel,
source,
sourcePort,
destinationLabel,
destination,
destinationPort,
networkDomainID,
),
)
fmt.Println(configuration)
return nil
}
func convertFirewallAction(action string) string {
switch action {
case "ACCEPT_DECISIVELY":
return "accept"
case "DROP":
return "DROP"
default:
return "UNKNOWN"
}
}