bhouse-nexthop commented on code in PR #281:
URL:
https://github.com/apache/cloudstack-terraform-provider/pull/281#discussion_r2912363498
##########
cloudstack/resource_cloudstack_network_acl_rule.go:
##########
@@ -202,6 +272,159 @@ func resourceCloudStackNetworkACLRule() *schema.Resource {
}
}
+// resourceCloudStackNetworkACLRulesetHash computes the hash for a ruleset
element
+// Only the rule_number is used for hashing since it uniquely identifies a rule
+// This prevents spurious diffs when fields with defaults are populated in
state but not in config
+func resourceCloudStackNetworkACLRulesetHash(v interface{}) int {
+ var buf bytes.Buffer
+ m := v.(map[string]interface{})
+
+ // Only hash on rule_number since it's the unique identifier
+ if v, ok := m["rule_number"]; ok {
+ buf.WriteString(fmt.Sprintf("%d-", v.(int)))
+ }
+
+ return schema.HashString(buf.String())
+}
+
+// Helper functions for UUID handling to abstract differences between
+// 'rule' (uses uuids map) and 'ruleset' (uses uuid string)
+
+// getRuleUUID gets the UUID for a rule, handling both formats
+// For ruleset: returns the uuid string
+// For rule with key: returns the UUID from uuids map for the given key
+// For rule without key: returns the first UUID from uuids map
+func getRuleUUID(rule map[string]interface{}, key string) (string, bool) {
+ // Try uuid string first (ruleset format)
+ if uuidVal, ok := rule["uuid"]; ok && uuidVal != nil {
+ if uuid, ok := uuidVal.(string); ok && uuid != "" {
+ return uuid, true
+ }
+ }
+
+ // Try uuids map (rule format)
+ if uuidsVal, ok := rule["uuids"]; ok && uuidsVal != nil {
+ if uuids, ok := uuidsVal.(map[string]interface{}); ok {
+ if key != "" {
+ // Get specific key
+ if idVal, ok := uuids[key]; ok && idVal != nil {
+ if id, ok := idVal.(string); ok {
+ return id, true
+ }
+ }
+ } else {
+ // Get first non-nil UUID
+ for _, idVal := range uuids {
+ if idVal != nil {
+ if id, ok := idVal.(string); ok
{
+ return id, true
+ }
+ }
+ }
+ }
+ }
+ }
+
+ return "", false
+}
+
+// setRuleUUID sets the UUID for a rule, handling both formats
+// For ruleset: sets the uuid string
+// For rule: sets the UUID in uuids map with the given key
+func setRuleUUID(rule map[string]interface{}, key string, uuid string) {
+ // Check if this is a ruleset (has uuid field) or rule (has uuids field)
+ if _, hasUUID := rule["uuid"]; hasUUID {
+ // Ruleset format
+ rule["uuid"] = uuid
+ } else {
+ // Rule format - ensure uuids map exists
+ var uuids map[string]interface{}
+ if uuidsVal, ok := rule["uuids"]; ok && uuidsVal != nil {
+ uuids = uuidsVal.(map[string]interface{})
+ } else {
+ uuids = make(map[string]interface{})
+ rule["uuids"] = uuids
+ }
+ uuids[key] = uuid
+ }
+}
+
+// hasRuleUUID checks if a rule has any UUID set
+func hasRuleUUID(rule map[string]interface{}) bool {
+ // Check uuid string (ruleset format)
+ if uuidVal, ok := rule["uuid"]; ok && uuidVal != nil {
+ if uuid, ok := uuidVal.(string); ok && uuid != "" {
+ return true
+ }
+ }
+
+ // Check uuids map (rule format)
+ if uuidsVal, ok := rule["uuids"]; ok && uuidsVal != nil {
+ if uuids, ok := uuidsVal.(map[string]interface{}); ok &&
len(uuids) > 0 {
+ return true
+ }
+ }
+
+ return false
+}
+
+// copyRuleUUIDs copies all UUIDs from source rule to destination rule
+func copyRuleUUIDs(dst, src map[string]interface{}) {
+ // Check if source has uuid string (ruleset format)
+ if uuidVal, ok := src["uuid"]; ok && uuidVal != nil {
+ if uuid, ok := uuidVal.(string); ok && uuid != "" {
+ dst["uuid"] = uuid
+ return
+ }
+ }
+
+ // Check if source has uuids map (rule format)
+ if uuidsVal, ok := src["uuids"]; ok && uuidsVal != nil {
+ if uuids, ok := uuidsVal.(map[string]interface{}); ok {
+ dst["uuids"] = uuids
+ return
+ }
+ }
+}
+
+// isRulesetRule checks if a rule is from a ruleset (has uuid field) vs rule
(has uuids field)
+func isRulesetRule(rule map[string]interface{}) bool {
+ _, hasUUID := rule["uuid"]
+ return hasUUID
+}
+
+// assignRuleNumbers assigns rule numbers to rules that don't have them
+// Rules are numbered sequentially starting from 1
+// If a rule has an explicit rule_number, numbering continues from that value
+func assignRuleNumbers(rules []interface{}) []interface{} {
+ result := make([]interface{}, len(rules))
+ nextNumber := 1
+
+ for i, rule := range rules {
+ ruleMap := make(map[string]interface{})
+ // Copy the rule
+ for k, v := range rule.(map[string]interface{}) {
+ ruleMap[k] = v
+ }
+
+ // Check if rule_number is set
+ if ruleNum, ok := ruleMap["rule_number"].(int); ok && ruleNum >
0 {
+ // Rule has explicit number, use it and continue from
there
+ nextNumber = ruleNum + 1
+ log.Printf("[DEBUG] Rule at index %d has explicit
rule_number=%d", i, ruleNum)
+ } else {
+ // Auto-assign sequential number
+ ruleMap["rule_number"] = nextNumber
+ log.Printf("[DEBUG] Auto-assigned rule_number=%d to
rule at index %d", nextNumber, i)
+ nextNumber++
+ }
Review Comment:
fixed in 20b8359
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]