Skip to content

Commit 1b05d4c

Browse files
committed
allow modifying rule number
1 parent 6b1239c commit 1b05d4c

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

cloudstack/resource_cloudstack_network_acl_rule.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -509,18 +509,14 @@ func resourceCloudStackNetworkACLRuleRead(d *schema.ResourceData, meta interface
509509
// Handle deprecated ports field (multiple ports)
510510
log.Printf("[DEBUG] Processing %d ports for TCP/UDP rule (deprecated field)", ps.Len())
511511

512-
// Create an empty list to hold all ports
513512
var ports []interface{}
514-
515-
// Loop through all ports and retrieve their info
516513
for _, port := range ps.List() {
517514
id, ok := uuids[port.(string)]
518515
if !ok {
519516
log.Printf("[DEBUG] No UUID for port %s, skipping", port.(string))
520517
continue
521518
}
522519

523-
// Get the rule
524520
r, ok := ruleMap[id.(string)]
525521
if !ok {
526522
log.Printf("[DEBUG] TCP/UDP rule for port %s with ID %s not found, removing UUID", port.(string), id.(string))
@@ -531,13 +527,11 @@ func resourceCloudStackNetworkACLRuleRead(d *schema.ResourceData, meta interface
531527
// Delete the known rule so only unknown rules remain in the ruleMap
532528
delete(ruleMap, id.(string))
533529

534-
// Create a list with all CIDR's
535530
var cidrs []interface{}
536531
for _, cidr := range strings.Split(r.Cidrlist, ",") {
537532
cidrs = append(cidrs, cidr)
538533
}
539534

540-
// Update the values
541535
rule["action"] = strings.ToLower(r.Action)
542536
rule["protocol"] = r.Protocol
543537
rule["traffic_type"] = strings.ToLower(r.Traffictype)
@@ -546,7 +540,6 @@ func resourceCloudStackNetworkACLRuleRead(d *schema.ResourceData, meta interface
546540
log.Printf("[DEBUG] Added port %s to TCP/UDP rule", port.(string))
547541
}
548542

549-
// Add this rule to the rules list with ports
550543
rule["ports"] = schema.NewSet(schema.HashString, ports)
551544
rules = append(rules, rule)
552545
log.Printf("[DEBUG] Added TCP/UDP rule with deprecated ports to state: %+v", rule)
@@ -570,13 +563,11 @@ func resourceCloudStackNetworkACLRuleRead(d *schema.ResourceData, meta interface
570563
// Delete the known rule so only unknown rules remain in the ruleMap
571564
delete(ruleMap, id.(string))
572565

573-
// Create a list with all CIDR's
574566
var cidrs []interface{}
575567
for _, cidr := range strings.Split(r.Cidrlist, ",") {
576568
cidrs = append(cidrs, cidr)
577569
}
578570

579-
// Update the values
580571
rule["action"] = strings.ToLower(r.Action)
581572
rule["protocol"] = r.Protocol
582573
rule["traffic_type"] = strings.ToLower(r.Traffictype)
@@ -603,13 +594,11 @@ func resourceCloudStackNetworkACLRuleRead(d *schema.ResourceData, meta interface
603594

604595
delete(ruleMap, id.(string))
605596

606-
// Create a list with all CIDR's
607597
var cidrs []interface{}
608598
for _, cidr := range strings.Split(r.Cidrlist, ",") {
609599
cidrs = append(cidrs, cidr)
610600
}
611601

612-
// Update the values
613602
rule["action"] = strings.ToLower(r.Action)
614603
rule["protocol"] = r.Protocol
615604
rule["traffic_type"] = strings.ToLower(r.Traffictype)
@@ -972,6 +961,14 @@ func ruleNeedsUpdate(oldRule, newRule map[string]interface{}) bool {
972961
return true
973962
}
974963

964+
// Check rule_number
965+
oldRuleNum, oldHasRuleNum := oldRule["rule_number"].(int)
966+
newRuleNum, newHasRuleNum := newRule["rule_number"].(int)
967+
if oldHasRuleNum != newHasRuleNum || (oldHasRuleNum && newHasRuleNum && oldRuleNum != newRuleNum) {
968+
log.Printf("[DEBUG] Rule number changed: %d -> %d", oldRuleNum, newRuleNum)
969+
return true
970+
}
971+
975972
oldDesc, oldHasDesc := oldRule["description"].(string)
976973
newDesc, newHasDesc := newRule["description"].(string)
977974
if oldHasDesc != newHasDesc || (oldHasDesc && newHasDesc && oldDesc != newDesc) {
@@ -1055,6 +1052,12 @@ func updateNetworkACLRule(cs *cloudstack.CloudStackClient, oldRule, newRule map[
10551052

10561053
p.SetTraffictype(newRule["traffic_type"].(string))
10571054

1055+
// Set rule number if provided and non-zero
1056+
if ruleNum, ok := newRule["rule_number"].(int); ok && ruleNum > 0 {
1057+
p.SetNumber(ruleNum)
1058+
log.Printf("[DEBUG] Set rule_number=%d", ruleNum)
1059+
}
1060+
10581061
protocol := newRule["protocol"].(string)
10591062
switch protocol {
10601063
case "icmp":

0 commit comments

Comments
 (0)