/
resource_firewall_rule.go
200 lines (168 loc) · 4.79 KB
/
resource_firewall_rule.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
package vultr
import (
"fmt"
"log"
"net"
"strconv"
"strings"
"github.com/JamesClonk/vultr/lib"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceFirewallRule() *schema.Resource {
return &schema.Resource{
Create: resourceFirewallRuleCreate,
Read: resourceFirewallRuleRead,
Delete: resourceFirewallRuleDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"action": {
Type: schema.TypeString,
Computed: true,
},
"cidr_block": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateCIDRNetworkAddress,
},
"direction": {
Type: schema.TypeString,
Computed: true,
},
"firewall_group_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"from_port": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
"notes": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"protocol": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateFirewallRuleProtocol,
},
"to_port": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
},
}
}
func resourceFirewallRuleCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Client)
_, cidrBlock, err := net.ParseCIDR(d.Get("cidr_block").(string))
if err != nil {
return fmt.Errorf("Error parsing %q for firewall rule: %v", "cidr_block", err)
}
firewallGroupID := d.Get("firewall_group_id").(string)
fromPort := d.Get("from_port").(int)
notes := d.Get("notes").(string)
protocol := d.Get("protocol").(string)
toPort := d.Get("to_port").(int)
_, fok := d.GetOk("from_port")
_, tok := d.GetOk("to_port")
if fok != tok {
return fmt.Errorf("Expected %q and %q to both be provided or both be empty", "from_port", "to_port")
}
if (protocol == "tcp" || protocol == "udp") && !fok {
return fmt.Errorf("%q and %q are required for protocol of type %q", "from_port", "to_port", protocol)
}
var port string
if fok {
if fromPort != toPort {
port = fmt.Sprintf("%d:%d", fromPort, toPort)
} else {
port = strconv.Itoa(fromPort)
}
}
log.Printf("[INFO] Creating new firewall rule")
id, err := client.CreateFirewallRule(firewallGroupID, protocol, port, cidrBlock, notes)
if err != nil {
return fmt.Errorf("Error creating firewall rule: %v", err)
}
d.SetId(fmt.Sprintf("%s/%d", firewallGroupID, id))
return resourceFirewallRuleRead(d, meta)
}
func resourceFirewallRuleRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Client)
firewallGroupID, id, err := parseStringSlashInt(d.Id(), "firewall rule ID", "firewall-group-id", "firewall-rule-number")
if err != nil {
return err
}
firewallRules, err := client.GetFirewallRules(firewallGroupID)
if err != nil {
if strings.HasPrefix(err.Error(), "Invalid firewall group") {
log.Printf("[WARN] Removing firewall rule (%s) because the group is gone", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error getting firewall rule (%s): %v", d.Id(), err)
}
var firewallRule *lib.FirewallRule
for _, f := range firewallRules {
if f.RuleNumber == id {
firewallRule = &f
break
}
}
if firewallRule == nil {
log.Printf("[WARN] Removing firewall rule (%s) because it is gone", d.Id())
d.SetId("")
return nil
}
d.Set("action", firewallRule.Action)
d.Set("cidr_block", firewallRule.Network.String())
d.Set("direction", "in")
d.Set("firewall_group_id", firewallGroupID)
d.Set("notes", firewallRule.Notes)
d.Set("protocol", firewallRule.Protocol)
from, to, err := splitFirewallRule(firewallRule.Port)
if err != nil {
return fmt.Errorf("Error parsing port range for firewall rule (%s): %v", d.Id(), err)
}
d.Set("from_port", from)
d.Set("to_port", to)
return nil
}
func resourceFirewallRuleDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Client)
firewallGroupID, id, err := parseStringSlashInt(d.Id(), "firewall rule ID", "firewall-group-id", "firewall-rule-number")
if err != nil {
return err
}
log.Printf("[INFO] Destroying firewall rule (%s)", d.Id())
if err := client.DeleteFirewallRule(id, firewallGroupID); err != nil {
return fmt.Errorf("Error destroying firewall rule (%s): %v", d.Id(), err)
}
return nil
}
func splitFirewallRule(portRange string) (int, int, error) {
if len(portRange) == 0 || strings.TrimSpace(portRange) == "-" {
return 0, 0, nil
}
ports := strings.Split(portRange, "-")
from, err := strconv.Atoi(strings.TrimSpace(ports[0]))
if err != nil {
return 0, 0, err
}
if len(ports) == 1 {
return from, from, nil
}
to, err := strconv.Atoi(strings.TrimSpace(ports[1]))
if err != nil {
return 0, 0, err
}
return from, to, nil
}