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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Fix authentication for fleet API (using ApiKey instead of Bearer keyword) ([#576](https://github.com/elastic/terraform-provider-elasticstack/pull/576))
- Ensure all Kibana resources use the supplied `ca_certs` value. ([#585](https://github.com/elastic/terraform-provider-elasticstack/pull/585))
- Don't panic when SLM indices are specified as a CSV string rather than an array ([#593](https://github.com/elastic/terraform-provider-elasticstack/pull/593))

## [0.11.1] - 2024-02-17

Expand Down
31 changes: 30 additions & 1 deletion internal/models/models.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package models

import (
"encoding/json"
"errors"
"strings"
"time"
)

Expand Down Expand Up @@ -194,12 +197,38 @@ type SnapshotPolicyConfig struct {
ExpandWildcards *string `json:"expand_wildcards,omitempty"`
IgnoreUnavailable *bool `json:"ignore_unavailable,omitempty"`
IncludeGlobalState *bool `json:"include_global_state,omitempty"`
Indices []string `json:"indices,omitempty"`
Indices StringSliceOrCSV `json:"indices,omitempty"`
FeatureStates []string `json:"feature_states,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
Partial *bool `json:"partial,omitempty"`
}

type StringSliceOrCSV []string

var ErrInvalidStringSliceOrCSV = errors.New("expected array of strings, or a csv string")

func (i *StringSliceOrCSV) UnmarshalJSON(data []byte) error {
// Ignore null, like in the main JSON package.
if string(data) == "null" || string(data) == `""` {
return nil
}

// First try to parse as an array
var sliceResult []string
if err := json.Unmarshal(data, &sliceResult); err == nil {
*i = StringSliceOrCSV(sliceResult)
return nil
}

var stringResult string
if err := json.Unmarshal(data, &stringResult); err == nil {
*i = StringSliceOrCSV(strings.Split(stringResult, ","))
return nil
}

return ErrInvalidStringSliceOrCSV
}

type Index struct {
Name string `json:"-"`
Aliases map[string]IndexAlias `json:"aliases,omitempty"`
Expand Down
56 changes: 56 additions & 0 deletions internal/models/models_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package models

import (
"encoding/json"
"testing"

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

func TestStringSliceOrCSV_UnmarshalJSON9(t *testing.T) {
tests := []struct {
name string
jsonString string
expectedResult StringSliceOrCSV
expectedErr error
}{
{
name: "should handle json arrays",
jsonString: `["a", "b", "c"]`,
expectedResult: StringSliceOrCSV{"a", "b", "c"},
},
{
name: "should handle csv strings",
jsonString: `"a,b,c"`,
expectedResult: StringSliceOrCSV{"a", "b", "c"},
},
{
name: "should handle explicit nulls",
jsonString: `null`,
},
{
name: "should handle empty strings",
jsonString: `""`,
},
{
name: "should fail on invalid data",
jsonString: "true",
expectedErr: ErrInvalidStringSliceOrCSV,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var actualModel StringSliceOrCSV
err := json.Unmarshal([]byte(tt.jsonString), &actualModel)

if tt.expectedErr == nil {
require.NoError(t, err)
} else {
require.ErrorIs(t, err, tt.expectedErr)
}

require.Equal(t, tt.expectedResult, actualModel)
})
}
}