Skip to content

Commit 5b76ec9

Browse files
author
Ben Lucas
authored
tag order should not matter when comparing rules (#306)
* tag order should not matter when comparing rules * fix bug in getTagsFromResourceData function
1 parent bcdc51e commit 5b76ec9

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

sysdig/resource_sysdig_secure_rule.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package sysdig
22

33
import (
4+
"reflect"
5+
"sort"
6+
47
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
58

69
"github.com/draios/terraform-provider-sysdig/sysdig/internal/client/secure"
@@ -46,22 +49,37 @@ func ruleFromResourceData(d *schema.ResourceData) secure.Rule {
4649
Version: d.Get("version").(int),
4750
}
4851

49-
rule.Tags = []string{}
50-
if tags, ok := d.Get("tags").([]interface{}); ok {
51-
for _, rawTag := range tags {
52-
if tag, ok := rawTag.(string); ok {
53-
rule.Tags = append(rule.Tags, tag)
54-
}
55-
}
56-
}
52+
rule.Tags = getTagsFromResourceData(d)
53+
5754
return rule
5855
}
5956

6057
// Saves in the resource data the information from the common fields of the rule.
6158
func updateResourceDataForRule(d *schema.ResourceData, rule secure.Rule) {
59+
currentTags := getTagsFromResourceData(d)
60+
newTags := append([]string{}, rule.Tags...)
61+
sort.Strings(currentTags)
62+
sort.Strings(newTags)
63+
areTagsSame := reflect.DeepEqual(currentTags, newTags)
64+
6265
_ = d.Set("name", rule.Name)
6366
_ = d.Set("description", rule.Description)
64-
_ = d.Set("tags", rule.Tags)
67+
if !areTagsSame {
68+
_ = d.Set("tags", rule.Tags)
69+
}
6570
_ = d.Set("version", rule.Version)
6671

6772
}
73+
74+
func getTagsFromResourceData(d *schema.ResourceData) []string {
75+
result := []string{}
76+
if tags, ok := d.Get("tags").([]interface{}); ok {
77+
for _, rawTag := range tags {
78+
if tag, ok := rawTag.(string); ok {
79+
result = append(result, tag)
80+
}
81+
}
82+
}
83+
84+
return result
85+
}

0 commit comments

Comments
 (0)