Skip to content

Commit d7bd401

Browse files
committed
provider/openstack: Allow any protocol in openstack_fw_rule_v1
This commit allows a protocol of "any" to be used in the firewall rule resource, which will allow any protocol.
1 parent aaff622 commit d7bd401

3 files changed

Lines changed: 64 additions & 8 deletions

File tree

builtin/providers/openstack/resource_openstack_fw_rule_v1.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,12 @@ func resourceFWRuleV1Create(d *schema.ResourceData, meta interface{}) error {
8888

8989
enabled := d.Get("enabled").(bool)
9090
ipVersion := resourceFWRuleV1DetermineIPVersion(d.Get("ip_version").(int))
91+
protocol := resourceFWRuleV1DetermineProtocol(d.Get("protocol").(string))
9192

9293
ruleConfiguration := rules.CreateOpts{
9394
Name: d.Get("name").(string),
9495
Description: d.Get("description").(string),
95-
Protocol: d.Get("protocol").(string),
96+
Protocol: protocol,
9697
Action: d.Get("action").(string),
9798
IPVersion: ipVersion,
9899
SourceIPAddress: d.Get("source_ip_address").(string),
@@ -103,11 +104,6 @@ func resourceFWRuleV1Create(d *schema.ResourceData, meta interface{}) error {
103104
TenantID: d.Get("tenant_id").(string),
104105
}
105106

106-
if v, ok := d.GetOk("ip_version"); ok {
107-
ipVersion := resourceFWRuleV1DetermineIPVersion(v.(int))
108-
ruleConfiguration.IPVersion = ipVersion
109-
}
110-
111107
log.Printf("[DEBUG] Create firewall rule: %#v", ruleConfiguration)
112108

113109
rule, err := rules.Create(networkingClient, ruleConfiguration).Extract()
@@ -139,7 +135,6 @@ func resourceFWRuleV1Read(d *schema.ResourceData, meta interface{}) error {
139135

140136
log.Printf("[DEBUG] Read OpenStack Firewall Rule %s: %#v", d.Id(), rule)
141137

142-
d.Set("protocol", rule.Protocol)
143138
d.Set("action", rule.Action)
144139
d.Set("name", rule.Name)
145140
d.Set("description", rule.Description)
@@ -150,6 +145,12 @@ func resourceFWRuleV1Read(d *schema.ResourceData, meta interface{}) error {
150145
d.Set("destination_port", rule.DestinationPort)
151146
d.Set("enabled", rule.Enabled)
152147

148+
if rule.Protocol == "" {
149+
d.Set("protocol", "any")
150+
} else {
151+
d.Set("protocol", rule.Protocol)
152+
}
153+
153154
return nil
154155
}
155156

@@ -259,3 +260,19 @@ func resourceFWRuleV1DetermineIPVersion(ipv int) gophercloud.IPVersion {
259260

260261
return ipVersion
261262
}
263+
264+
func resourceFWRuleV1DetermineProtocol(p string) rules.Protocol {
265+
var protocol rules.Protocol
266+
switch p {
267+
case "any":
268+
protocol = rules.ProtocolAny
269+
case "icmp":
270+
protocol = rules.ProtocolICMP
271+
case "tcp":
272+
protocol = rules.ProtocolTCP
273+
case "udp":
274+
protocol = rules.ProtocolUDP
275+
}
276+
277+
return protocol
278+
}

builtin/providers/openstack/resource_openstack_fw_rule_v1_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,32 @@ func TestAccFWRuleV1_basic(t *testing.T) {
7373
})
7474
}
7575

76+
func TestAccFWRuleV1_anyProtocol(t *testing.T) {
77+
resource.Test(t, resource.TestCase{
78+
PreCheck: func() { testAccPreCheck(t) },
79+
Providers: testAccProviders,
80+
CheckDestroy: testAccCheckFWRuleV1Destroy,
81+
Steps: []resource.TestStep{
82+
resource.TestStep{
83+
Config: testFirewallRuleAnyProtocol,
84+
Check: resource.ComposeTestCheckFunc(
85+
testAccCheckFWRuleV1Exists(
86+
"openstack_fw_rule_v1.rule_1",
87+
&rules.Rule{
88+
Name: "rule_1",
89+
Description: "Allow any protocol",
90+
Protocol: "",
91+
Action: "allow",
92+
IPVersion: 4,
93+
SourceIPAddress: "192.168.199.0/24",
94+
Enabled: true,
95+
}),
96+
),
97+
},
98+
},
99+
})
100+
}
101+
76102
func testAccCheckFWRuleV1Destroy(s *terraform.State) error {
77103

78104
config := testAccProvider.Meta().(*Config)
@@ -178,3 +204,15 @@ resource "openstack_fw_rule_v1" "accept_test" {
178204
enabled = false
179205
}
180206
`
207+
208+
const testFirewallRuleAnyProtocol = `
209+
resource "openstack_fw_rule_v1" "rule_1" {
210+
name = "rule_1"
211+
description = "Allow any protocol"
212+
protocol = "any"
213+
action = "allow"
214+
ip_version = 4
215+
source_ip_address = "192.168.199.0/24"
216+
enabled = true
217+
}
218+
`

website/source/docs/providers/openstack/r/fw_rule_v1.html.markdown

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ The following arguments are supported:
3939
updates the `description` of an existing firewall rule.
4040

4141
* `protocol` - (Required) The protocol type on which the firewall rule operates.
42-
Changing this updates the `protocol` of an existing firewall rule.
42+
Valid values are: `tcp`, `udp`, `icmp`, and `any`. Changing this updates the
43+
`protocol` of an existing firewall rule.
4344

4445
* `action` - (Required) Action to be taken ( must be "allow" or "deny") when the
4546
firewall rule matches. Changing this updates the `action` of an existing

0 commit comments

Comments
 (0)