Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion launchdarkly/custom_properties_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ func customPropertiesToResourceData(customProperties map[string]ldapi.CustomProp
for _, v := range cp.Value {
values = append(values, v)
}
// Sort the values to ensure consistency with how they're sent to the API
sort.Slice(values, func(i, j int) bool {
return values[i].(string) < values[j].(string)
})
cpRaw := map[string]interface{}{
KEY: k,
NAME: cp.Name,
Expand All @@ -98,8 +102,32 @@ func customPropertiesToResourceData(customProperties map[string]ldapi.CustomProp
return transformed
}

// hashCustomProperty is a struct used for hashing custom properties
// to ensure consistent hash values based on actual field values
type hashCustomProperty struct {
Key string
Name string
Value []string
}

// https://godoc.org/github.com/hashicorp/terraform/helper/schema#SchemaSetFunc
func customPropertyHash(val interface{}) int {
customPropertyMap := val.(map[string]interface{})
return schema.HashString(fmt.Sprintf("%v", customPropertyMap[KEY]))
// Extract and sort values to ensure consistent hashing
var values []string
if valueList, ok := customPropertyMap[VALUE].([]interface{}); ok {
for _, v := range valueList {
values = append(values, v.(string))
}
}
sort.Strings(values)

// Hash all fields together to ensure consistent hash calculation
// This prevents issues where the hash function is called with incomplete data
cp := hashCustomProperty{
Key: fmt.Sprintf("%v", customPropertyMap[KEY]),
Name: fmt.Sprintf("%v", customPropertyMap[NAME]),
Value: values,
}
return schema.HashString(fmt.Sprintf("%v", cp))
}
141 changes: 140 additions & 1 deletion launchdarkly/custom_properties_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ func TestCustomPropertiesRoundTripConversion(t *testing.T) {
Value: []string{"a cp value1", "a cp value2", "a cp value3"}},
},
},
{
name: "Unsorted values get sorted",
customProperties: map[string]interface{}{
CUSTOM_PROPERTIES: []interface{}{
map[string]interface{}{
KEY: "cp3",
NAME: "nameValue3",
VALUE: []interface{}{"zebra", "apple", "banana"},
},
},
},
expected: map[string]ldapi.CustomProperty{"cp3": {
Name: "nameValue3",
Value: []string{"apple", "banana", "zebra"}},
},
},
}

for _, tc := range testCases {
Expand All @@ -59,7 +75,130 @@ func TestCustomPropertiesRoundTripConversion(t *testing.T) {
require.Equal(t, tc.expected, actual)

actualRaw := customPropertiesToResourceData(actual)
require.Equal(t, tc.customProperties[CUSTOM_PROPERTIES], actualRaw)
// After round trip, values should be sorted
expectedAfterRoundTrip := tc.customProperties[CUSTOM_PROPERTIES].([]interface{})
require.Len(t, actualRaw, len(expectedAfterRoundTrip))
for i, item := range actualRaw {
actualMap := item.(map[string]interface{})
expectedMap := expectedAfterRoundTrip[i].(map[string]interface{})
require.Equal(t, expectedMap[KEY], actualMap[KEY])
require.Equal(t, expectedMap[NAME], actualMap[NAME])
// Values should be sorted after round trip
actualValues := actualMap[VALUE].([]interface{})
expectedValues := expectedMap[VALUE].([]interface{})
require.Equal(t, len(expectedValues), len(actualValues))
// Check that values are sorted
for j := 0; j < len(actualValues)-1; j++ {
require.LessOrEqual(t, actualValues[j].(string), actualValues[j+1].(string), "values should be sorted")
}
}
})
}
}

func TestCustomPropertyHash(t *testing.T) {
testCases := []struct {
name string
prop1 map[string]interface{}
prop2 map[string]interface{}
shouldEqual bool
}{
{
name: "identical properties have same hash",
prop1: map[string]interface{}{
KEY: "test.key",
NAME: "Test Name",
VALUE: []interface{}{"value1", "value2"},
},
prop2: map[string]interface{}{
KEY: "test.key",
NAME: "Test Name",
VALUE: []interface{}{"value1", "value2"},
},
shouldEqual: true,
},
{
name: "different keys have different hashes",
prop1: map[string]interface{}{
KEY: "test.key1",
NAME: "Test Name",
VALUE: []interface{}{"value1"},
},
prop2: map[string]interface{}{
KEY: "test.key2",
NAME: "Test Name",
VALUE: []interface{}{"value1"},
},
shouldEqual: false,
},
{
name: "different names have different hashes",
prop1: map[string]interface{}{
KEY: "test.key",
NAME: "Test Name 1",
VALUE: []interface{}{"value1"},
},
prop2: map[string]interface{}{
KEY: "test.key",
NAME: "Test Name 2",
VALUE: []interface{}{"value1"},
},
shouldEqual: false,
},
{
name: "different values have different hashes",
prop1: map[string]interface{}{
KEY: "test.key",
NAME: "Test Name",
VALUE: []interface{}{"value1"},
},
prop2: map[string]interface{}{
KEY: "test.key",
NAME: "Test Name",
VALUE: []interface{}{"value2"},
},
shouldEqual: false,
},
{
name: "sorted values produce same hash regardless of input order",
prop1: map[string]interface{}{
KEY: "test.key",
NAME: "Test Name",
VALUE: []interface{}{"zebra", "apple", "banana"},
},
prop2: map[string]interface{}{
KEY: "test.key",
NAME: "Test Name",
VALUE: []interface{}{"apple", "banana", "zebra"},
},
shouldEqual: true,
},
{
name: "different value counts have different hashes",
prop1: map[string]interface{}{
KEY: "test.key",
NAME: "Test Name",
VALUE: []interface{}{"value1", "value2"},
},
prop2: map[string]interface{}{
KEY: "test.key",
NAME: "Test Name",
VALUE: []interface{}{"value1"},
},
shouldEqual: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
hash1 := customPropertyHash(tc.prop1)
hash2 := customPropertyHash(tc.prop2)

if tc.shouldEqual {
require.Equal(t, hash1, hash2, "hashes should be equal")
} else {
require.NotEqual(t, hash1, hash2, "hashes should be different")
}
})
}
}
Loading