From 04c811b4a54965c3860ab8ad1a4db8258808c6fd Mon Sep 17 00:00:00 2001 From: Toby Brain Date: Thu, 21 Mar 2024 18:16:53 +1100 Subject: [PATCH 1/2] Support CSV indices in SLM policy definitions --- internal/models/models.go | 31 ++++++++++++++++++- internal/models/models_test.go | 56 ++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 internal/models/models_test.go diff --git a/internal/models/models.go b/internal/models/models.go index 11084fac9..35fb78f6b 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -1,6 +1,9 @@ package models import ( + "encoding/json" + "errors" + "strings" "time" ) @@ -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"` diff --git a/internal/models/models_test.go b/internal/models/models_test.go new file mode 100644 index 000000000..0319fe339 --- /dev/null +++ b/internal/models/models_test.go @@ -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) + }) + } +} From 954a6c7992f061c336bb3bca89575bfea0fda48d Mon Sep 17 00:00:00 2001 From: Toby Brain Date: Sat, 6 Apr 2024 19:59:12 +1100 Subject: [PATCH 2/2] Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 05175b131..cef50a687 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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