Skip to content

Sort Fleet integration policy inputs to ensure consistency #494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 29, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Rename fleet package objects to `elasticstack_fleet_integration` and `elasticstack_fleet_integration_policy` ([#476](https://github.com./elastic/terraform-provider-elasticstack/pull/476))
- Fix a provider crash when managing SLOs outside of the default Kibana space. ([#485](https://github.com./elastic/terraform-provider-elasticstack/pull/485))
- Make input optional for `elasticstack_fleet_integration_policy` ([#493](https://github.com./elastic/terraform-provider-elasticstack/pull/493))
- Sort Fleet integration policy inputs to ensure consistency ([#494](https://github.com./elastic/terraform-provider-elasticstack/pull/494))

## [0.10.0] - 2023-11-02

Expand Down
48 changes: 42 additions & 6 deletions internal/fleet/integration_policy_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fleet
import (
"context"
"encoding/json"
"sort"

"github.com./hashicorp/terraform-plugin-sdk/v2/diag"
"github.com./hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -340,9 +341,9 @@ func resourceIntegrationPolicyRead(ctx context.Context, d *schema.ResourceData,
}
}

var inputs []any
newInputs := make([]any, 0, len(pkgPolicy.Inputs))
for inputID, input := range pkgPolicy.Inputs {
inputMap := map[string]any{
inputData := map[string]any{
"input_id": inputID,
"enabled": input.Enabled,
}
Expand All @@ -352,19 +353,23 @@ func resourceIntegrationPolicyRead(ctx context.Context, d *schema.ResourceData,
if err != nil {
return diag.FromErr(err)
}
inputMap["streams_json"] = string(data)
inputData["streams_json"] = string(data)
}
if input.Vars != nil {
data, err := json.Marshal(*input.Vars)
if err != nil {
return diag.FromErr(err)
}
inputMap["vars_json"] = string(data)
inputData["vars_json"] = string(data)
}

inputs = append(inputs, inputMap)
newInputs = append(newInputs, inputData)
}
if err := d.Set("input", inputs); err != nil {

existingInputs, _ := d.Get("input").([]any)
sortInputs(newInputs, existingInputs)

if err := d.Set("input", newInputs); err != nil {
return diag.FromErr(err)
}

Expand All @@ -386,3 +391,34 @@ func resourceIntegrationPolicyDelete(ctx context.Context, d *schema.ResourceData

return diags
}

// sortInputs will sort the 'incoming' list of input definitions based on
// the order of inputs defined in the 'existing' list. Inputs not present in
// 'existing' will be placed at the end of the list. Inputs are identified by
// their ID ('input_id'). The 'incoming' slice will be sorted in-place.
func sortInputs(incoming []any, existing []any) {
idToIndex := make(map[string]int, len(existing))
for i, v := range existing {
inputData, _ := v.(map[string]any)
inputID, _ := inputData["input_id"].(string)
idToIndex[inputID] = i
}

sort.Slice(incoming, func(i, j int) bool {
iInput, _ := incoming[i].(map[string]any)
iID, _ := iInput["input_id"].(string)
iIdx, ok := idToIndex[iID]
if !ok {
return false
}

jInput, _ := incoming[j].(map[string]any)
jID, _ := jInput["input_id"].(string)
jIdx, ok := idToIndex[jID]
if !ok {
return true
}

return iIdx < jIdx
})
}
63 changes: 63 additions & 0 deletions internal/fleet/shared_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package fleet

import (
"testing"

"github.com./stretchr/testify/require"
)

func Test_SortInputs(t *testing.T) {
t.Run("WithExisting", func(t *testing.T) {
existing := []any{
map[string]any{"input_id": "A", "enabled": true},
map[string]any{"input_id": "B", "enabled": true},
map[string]any{"input_id": "C", "enabled": true},
map[string]any{"input_id": "D", "enabled": true},
map[string]any{"input_id": "E", "enabled": true},
}

incoming := []any{
map[string]any{"input_id": "G", "enabled": true},
map[string]any{"input_id": "F", "enabled": true},
map[string]any{"input_id": "B", "enabled": true},
map[string]any{"input_id": "E", "enabled": true},
map[string]any{"input_id": "C", "enabled": true},
}

want := []any{
map[string]any{"input_id": "B", "enabled": true},
map[string]any{"input_id": "C", "enabled": true},
map[string]any{"input_id": "E", "enabled": true},
map[string]any{"input_id": "G", "enabled": true},
map[string]any{"input_id": "F", "enabled": true},
}

sortInputs(incoming, existing)

require.Equal(t, want, incoming)
})

t.Run("WithEmpty", func(t *testing.T) {
var existing []any

incoming := []any{
map[string]any{"input_id": "G", "enabled": true},
map[string]any{"input_id": "F", "enabled": true},
map[string]any{"input_id": "B", "enabled": true},
map[string]any{"input_id": "E", "enabled": true},
map[string]any{"input_id": "C", "enabled": true},
}

want := []any{
map[string]any{"input_id": "G", "enabled": true},
map[string]any{"input_id": "F", "enabled": true},
map[string]any{"input_id": "B", "enabled": true},
map[string]any{"input_id": "E", "enabled": true},
map[string]any{"input_id": "C", "enabled": true},
}

sortInputs(incoming, existing)

require.Equal(t, want, incoming)
})
}