Skip to content

Commit 9fe0ac6

Browse files
authored
Merge pull request #203 from shapeblue/supportDesc
Support desc and rule_number in create_network_acl_rule
2 parents 2d90b19 + da84245 commit 9fe0ac6

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

cloudstack/resource_cloudstack_network_acl_rule.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ func resourceCloudStackNetworkACLRule() *schema.Resource {
5757
Optional: true,
5858
Elem: &schema.Resource{
5959
Schema: map[string]*schema.Schema{
60+
"rule_number": {
61+
Type: schema.TypeInt,
62+
Optional: true,
63+
Computed: true,
64+
},
65+
6066
"action": {
6167
Type: schema.TypeString,
6268
Optional: true,
@@ -100,6 +106,11 @@ func resourceCloudStackNetworkACLRule() *schema.Resource {
100106
Default: "ingress",
101107
},
102108

109+
"description": {
110+
Type: schema.TypeString,
111+
Optional: true,
112+
},
113+
103114
"uuids": {
104115
Type: schema.TypeMap,
105116
Computed: true,
@@ -198,6 +209,11 @@ func createNetworkACLRule(d *schema.ResourceData, meta interface{}, rule map[str
198209
// Create a new parameter struct
199210
p := cs.NetworkACL.NewCreateNetworkACLParams(rule["protocol"].(string))
200211

212+
// If a rule ID is specified, set it
213+
if ruleNum, ok := rule["rule_number"].(int); ok && ruleNum > 0 {
214+
p.SetNumber(ruleNum)
215+
}
216+
201217
// Set the acl ID
202218
p.SetAclid(d.Id())
203219

@@ -214,6 +230,11 @@ func createNetworkACLRule(d *schema.ResourceData, meta interface{}, rule map[str
214230
// Set the traffic type
215231
p.SetTraffictype(rule["traffic_type"].(string))
216232

233+
// Set the description
234+
if desc, ok := rule["description"].(string); ok && desc != "" {
235+
p.SetReason(desc)
236+
}
237+
217238
// If the protocol is ICMP set the needed ICMP parameters
218239
if rule["protocol"].(string) == "icmp" {
219240
p.SetIcmptype(rule["icmp_type"].(int))
@@ -623,6 +644,16 @@ func verifyNetworkACLParams(d *schema.ResourceData) error {
623644
}
624645

625646
func verifyNetworkACLRuleParams(d *schema.ResourceData, rule map[string]interface{}) error {
647+
if ruleNum, ok := rule["rule_number"]; ok && ruleNum != nil {
648+
if number, ok := ruleNum.(int); ok && number != 0 {
649+
// Validate only if rule_number is explicitly set (non-zero)
650+
if number < 1 || number > 65535 {
651+
return fmt.Errorf(
652+
"%q must be between %d and %d inclusive, got: %d", "rule_number", 1, 65535, number)
653+
}
654+
}
655+
}
656+
626657
action := rule["action"].(string)
627658
if action != "allow" && action != "deny" {
628659
return fmt.Errorf("Parameter action only accepts 'allow' or 'deny' as values")

cloudstack/resource_cloudstack_network_acl_rule_test.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func TestAccCloudStackNetworkACLRule_basic(t *testing.T) {
3838
{
3939
Config: testAccCloudStackNetworkACLRule_basic,
4040
Check: resource.ComposeTestCheckFunc(
41+
4142
testAccCheckCloudStackNetworkACLRulesExist("cloudstack_network_acl.foo"),
4243
resource.TestCheckResourceAttr(
4344
"cloudstack_network_acl_rule.foo", "rule.#", "3"),
@@ -55,6 +56,10 @@ func TestAccCloudStackNetworkACLRule_basic(t *testing.T) {
5556
"cloudstack_network_acl_rule.foo", "rule.0.ports.0", "443"),
5657
resource.TestCheckResourceAttr(
5758
"cloudstack_network_acl_rule.foo", "rule.0.traffic_type", "ingress"),
59+
resource.TestCheckResourceAttr(
60+
"cloudstack_network_acl_rule.foo", "rule.0.description", "Allow HTTP and HTTPS"),
61+
resource.TestCheckResourceAttr(
62+
"cloudstack_network_acl_rule.foo", "rule.1.rule_number", "20"),
5863
resource.TestCheckResourceAttr(
5964
"cloudstack_network_acl_rule.foo", "rule.1.action", "allow"),
6065
resource.TestCheckResourceAttr(
@@ -67,6 +72,12 @@ func TestAccCloudStackNetworkACLRule_basic(t *testing.T) {
6772
"cloudstack_network_acl_rule.foo", "rule.1.icmp_type", "-1"),
6873
resource.TestCheckResourceAttr(
6974
"cloudstack_network_acl_rule.foo", "rule.1.traffic_type", "ingress"),
75+
resource.TestCheckResourceAttr(
76+
"cloudstack_network_acl_rule.foo", "rule.1.description", "Allow ICMP traffic"),
77+
resource.TestCheckResourceAttr(
78+
"cloudstack_network_acl_rule.foo", "rule.2.rule_number", "10"),
79+
resource.TestCheckResourceAttr(
80+
"cloudstack_network_acl_rule.foo", "rule.2.description", "Allow all traffic"),
7081
),
7182
},
7283
},
@@ -99,6 +110,8 @@ func TestAccCloudStackNetworkACLRule_update(t *testing.T) {
99110
"cloudstack_network_acl_rule.foo", "rule.0.ports.0", "443"),
100111
resource.TestCheckResourceAttr(
101112
"cloudstack_network_acl_rule.foo", "rule.0.traffic_type", "ingress"),
113+
resource.TestCheckResourceAttr(
114+
"cloudstack_network_acl_rule.foo", "rule.1.rule_number", "20"),
102115
resource.TestCheckResourceAttr(
103116
"cloudstack_network_acl_rule.foo", "rule.1.action", "allow"),
104117
resource.TestCheckResourceAttr(
@@ -245,26 +258,31 @@ resource "cloudstack_network_acl_rule" "foo" {
245258
acl_id = cloudstack_network_acl.foo.id
246259
247260
rule {
261+
rule_number = 10
248262
action = "allow"
249263
cidr_list = ["172.18.100.0/24"]
250264
protocol = "all"
251265
traffic_type = "ingress"
266+
description = "Allow all traffic"
252267
}
253268
254269
rule {
270+
rule_number = 20
255271
action = "allow"
256272
cidr_list = ["172.18.100.0/24"]
257273
protocol = "icmp"
258274
icmp_type = "-1"
259275
icmp_code = "-1"
260276
traffic_type = "ingress"
277+
description = "Allow ICMP traffic"
261278
}
262279
263280
rule {
264281
cidr_list = ["172.16.100.0/24"]
265282
protocol = "tcp"
266283
ports = ["80", "443"]
267284
traffic_type = "ingress"
285+
description = "Allow HTTP and HTTPS"
268286
}
269287
}`
270288

@@ -294,26 +312,28 @@ resource "cloudstack_network_acl_rule" "foo" {
294312
295313
rule {
296314
action = "deny"
297-
cidr_list = ["172.18.100.0/24", "172.18.101.0/24"]
315+
cidr_list = ["172.18.100.0/24", "172.18.101.0/24"]
298316
protocol = "icmp"
299317
icmp_type = "-1"
300318
icmp_code = "-1"
301319
traffic_type = "ingress"
320+
description = "Deny ICMP traffic"
302321
}
303322
304323
rule {
305-
action = "allow"
324+
action = "allow"
306325
cidr_list = ["172.18.100.0/24"]
307326
protocol = "tcp"
308327
ports = ["80", "443"]
309328
traffic_type = "ingress"
310329
}
311330
312331
rule {
313-
action = "deny"
332+
action = "deny"
314333
cidr_list = ["10.0.0.0/24"]
315334
protocol = "tcp"
316335
ports = ["80", "1000-2000"]
317336
traffic_type = "egress"
337+
description = "Deny specific TCP ports"
318338
}
319339
}`

website/docs/r/network_acl_rule.html.markdown

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ The following arguments are supported:
4848

4949
The `rule` block supports:
5050

51+
* `rule_number` - (Optional) The number of the ACL item used to order the ACL rules. The ACL rule with the lowest number has the highest priority. If not specified, the ACL item will be created with a number one greater than the highest numbered rule.
52+
5153
* `action` - (Optional) The action for the rule. Valid options are: `allow` and
5254
`deny` (defaults allow).
5355

@@ -68,6 +70,8 @@ The `rule` block supports:
6870
* `traffic_type` - (Optional) The traffic type for the rule. Valid options are:
6971
`ingress` or `egress` (defaults ingress).
7072

73+
* `description` - (Optional) A description indicating why the ACL rule is required.
74+
7175
## Attributes Reference
7276

7377
The following attributes are exported:

0 commit comments

Comments
 (0)