Skip to content

Commit e3246bc

Browse files
committed
provider/openstack: Add 'value_specs' option to 'openstack_fw_rule_v1' resource.
Refactor to use common 'types.go' and 'MapValueSpecs' function. Website docs updated.
1 parent 24e5ea8 commit e3246bc

3 files changed

Lines changed: 44 additions & 12 deletions

File tree

builtin/providers/openstack/resource_openstack_fw_rule_v1.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ func resourceFWRuleV1() *schema.Resource {
7474
Optional: true,
7575
ForceNew: true,
7676
},
77+
"value_specs": &schema.Schema{
78+
Type: schema.TypeMap,
79+
Optional: true,
80+
ForceNew: true,
81+
},
7782
},
7883
}
7984
}
@@ -90,18 +95,21 @@ func resourceFWRuleV1Create(d *schema.ResourceData, meta interface{}) error {
9095
ipVersion := resourceFWRuleV1DetermineIPVersion(d.Get("ip_version").(int))
9196
protocol := resourceFWRuleV1DetermineProtocol(d.Get("protocol").(string))
9297

93-
ruleConfiguration := rules.CreateOpts{
94-
Name: d.Get("name").(string),
95-
Description: d.Get("description").(string),
96-
Protocol: protocol,
97-
Action: d.Get("action").(string),
98-
IPVersion: ipVersion,
99-
SourceIPAddress: d.Get("source_ip_address").(string),
100-
DestinationIPAddress: d.Get("destination_ip_address").(string),
101-
SourcePort: d.Get("source_port").(string),
102-
DestinationPort: d.Get("destination_port").(string),
103-
Enabled: &enabled,
104-
TenantID: d.Get("tenant_id").(string),
98+
ruleConfiguration := RuleCreateOpts{
99+
rules.CreateOpts{
100+
Name: d.Get("name").(string),
101+
Description: d.Get("description").(string),
102+
Protocol: protocol,
103+
Action: d.Get("action").(string),
104+
IPVersion: ipVersion,
105+
SourceIPAddress: d.Get("source_ip_address").(string),
106+
DestinationIPAddress: d.Get("destination_ip_address").(string),
107+
SourcePort: d.Get("source_port").(string),
108+
DestinationPort: d.Get("destination_port").(string),
109+
Enabled: &enabled,
110+
TenantID: d.Get("tenant_id").(string),
111+
},
112+
MapValueSpecs(d),
105113
}
106114

107115
log.Printf("[DEBUG] Create firewall rule: %#v", ruleConfiguration)

builtin/providers/openstack/types.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package openstack
22

33
import (
44
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/keypairs"
5+
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/fwaas/rules"
56
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
67
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/routers"
78
"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
@@ -69,6 +70,27 @@ func (opts RouterCreateOpts) ToRouterCreateMap() (map[string]interface{}, error)
6970
return BuildRequest(opts, "router")
7071
}
7172

73+
// RuleCreateOpts represents the attributes used when creating a new firewall rule.
74+
type RuleCreateOpts struct {
75+
rules.CreateOpts
76+
ValueSpecs map[string]string `json:"value_specs,omitempty"`
77+
}
78+
79+
// ToRuleCreateMap casts a CreateOpts struct to a map.
80+
// It overrides rules.ToRuleCreateMap to add the ValueSpecs field.
81+
func (opts RuleCreateOpts) ToRuleCreateMap() (map[string]interface{}, error) {
82+
b, err := BuildRequest(opts, "firewall_rule")
83+
if err != nil {
84+
return nil, err
85+
}
86+
87+
if m := b["firewall_rule"].(map[string]interface{}); m["protocol"] == "any" {
88+
m["protocol"] = nil
89+
}
90+
91+
return b, nil
92+
}
93+
7294
// SubnetCreateOpts represents the attributes used when creating a new subnet.
7395
type SubnetCreateOpts struct {
7496
subnets.CreateOpts

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ The following arguments are supported:
7373
wants to create a firewall rule for another tenant. Changing this creates a
7474
new firewall rule.
7575

76+
* `value_specs` - (Optional) Map of additional options.
77+
7678
## Attributes Reference
7779

7880
The following attributes are exported:

0 commit comments

Comments
 (0)