Skip to content

Commit 890e65d

Browse files
tobiobiscout42
andauthored
Validate that mappings are a JSON object, not just valid json (#719)
* Validate that mappings are a JSON object, not just valid json * Docs * Changelog * Update internal/elasticsearch/index/validation_test.go Co-authored-by: Boris Ilyushonak <[email protected]> --------- Co-authored-by: Boris Ilyushonak <[email protected]>
1 parent 1920946 commit 890e65d

File tree

8 files changed

+95
-11
lines changed

8 files changed

+95
-11
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## [Unreleased]
2-
- Add support for Kibana synthetics http and tcp monitors ([#699](https://github.com/elastic/terraform-provider-elasticstack/pull/699))
32

3+
- Improve validation for index settings and mappings ([#719](https://github.com/elastic/terraform-provider-elasticstack/pull/719))
4+
- Add support for Kibana synthetics http and tcp monitors ([#699](https://github.com/elastic/terraform-provider-elasticstack/pull/699))
45
- Add `elasticstack_kibana_spaces` data source ([#682](https://github.com/elastic/terraform-provider-elasticstack/pull/682))
56

67
## [0.11.5] - 2024-08-12

docs/resources/elasticsearch_component_template.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ resource "elasticstack_elasticsearch_index_template" "my_template" {
6363
Optional:
6464

6565
- `alias` (Block Set) Alias to add. (see [below for nested schema](#nestedblock--template--alias))
66-
- `mappings` (String) Mapping for fields in the index.
66+
- `mappings` (String) Mapping for fields in the index. Should be specified as a JSON object of field mappings. See the documentation (https://www.elastic.co/guide/en/elasticsearch/reference/current/explicit-mapping.html) for more details
6767
- `settings` (String) Configuration options for the index. See, https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html#index-modules-settings
6868

6969
<a id="nestedblock--template--alias"></a>

docs/resources/elasticsearch_index_template.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ Optional:
102102
Optional:
103103

104104
- `alias` (Block Set) Alias to add. (see [below for nested schema](#nestedblock--template--alias))
105-
- `mappings` (String) Mapping for fields in the index.
105+
- `mappings` (String) Mapping for fields in the index. Should be specified as a JSON object of field mappings. See the documentation (https://www.elastic.co/guide/en/elasticsearch/reference/current/explicit-mapping.html) for more details
106106
- `settings` (String) Configuration options for the index. See, https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html#index-modules-settings
107107

108108
<a id="nestedblock--template--alias"></a>

internal/elasticsearch/index/component_template.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,22 @@ func ResourceComponentTemplate() *schema.Resource {
9393
},
9494
},
9595
"mappings": {
96-
Description: "Mapping for fields in the index.",
96+
Description: "Mapping for fields in the index. Should be specified as a JSON object of field mappings. See the documentation (https://www.elastic.co/guide/en/elasticsearch/reference/current/explicit-mapping.html) for more details",
9797
Type: schema.TypeString,
9898
Optional: true,
9999
DiffSuppressFunc: utils.DiffJsonSuppress,
100-
ValidateFunc: validation.StringIsJSON,
100+
ValidateFunc: validation.All(
101+
validation.StringIsJSON, stringIsJSONObject,
102+
),
101103
},
102104
"settings": {
103105
Description: "Configuration options for the index. See, https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html#index-modules-settings",
104106
Type: schema.TypeString,
105107
Optional: true,
106108
DiffSuppressFunc: utils.DiffIndexSettingSuppress,
107-
ValidateFunc: validation.StringIsJSON,
109+
ValidateFunc: validation.All(
110+
validation.StringIsJSON, stringIsJSONObject,
111+
),
108112
},
109113
},
110114
},

internal/elasticsearch/index/index.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,8 +489,10 @@ If specified, this mapping can include: field names, [field data types](https://
489489
Type: schema.TypeString,
490490
Optional: true,
491491
DiffSuppressFunc: utils.DiffJsonSuppress,
492-
ValidateFunc: validation.StringIsJSON,
493-
Default: "{}",
492+
ValidateFunc: validation.All(
493+
validation.StringIsJSON, stringIsJSONObject,
494+
),
495+
Default: "{}",
494496
},
495497
// Deprecated: individual setting field should be used instead
496498
"settings": {

internal/elasticsearch/index/template.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,22 @@ func ResourceTemplate() *schema.Resource {
140140
},
141141
},
142142
"mappings": {
143-
Description: "Mapping for fields in the index.",
143+
Description: "Mapping for fields in the index. Should be specified as a JSON object of field mappings. See the documentation (https://www.elastic.co/guide/en/elasticsearch/reference/current/explicit-mapping.html) for more details",
144144
Type: schema.TypeString,
145145
Optional: true,
146146
DiffSuppressFunc: utils.DiffJsonSuppress,
147-
ValidateFunc: validation.StringIsJSON,
147+
ValidateFunc: validation.All(
148+
validation.StringIsJSON, stringIsJSONObject,
149+
),
148150
},
149151
"settings": {
150152
Description: "Configuration options for the index. See, https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html#index-modules-settings",
151153
Type: schema.TypeString,
152154
Optional: true,
153155
DiffSuppressFunc: utils.DiffIndexSettingSuppress,
154-
ValidateFunc: validation.StringIsJSON,
156+
ValidateFunc: validation.All(
157+
validation.StringIsJSON, stringIsJSONObject,
158+
),
155159
},
156160
},
157161
},
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package index
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
)
7+
8+
func stringIsJSONObject(i interface{}, s string) (warnings []string, errors []error) {
9+
iStr, ok := i.(string)
10+
if !ok {
11+
errors = append(errors, fmt.Errorf("expected type of %s to be string", s))
12+
return warnings, errors
13+
}
14+
15+
m := map[string]interface{}{}
16+
if err := json.Unmarshal([]byte(iStr), &m); err != nil {
17+
errors = append(errors, fmt.Errorf("expected %s to be a JSON object. Check the documentation for the expected format. %w", s, err))
18+
return
19+
}
20+
21+
return
22+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package index
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func Test_stringIsJSONObject(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
fieldVal interface{}
13+
expectedErrsToContain []string
14+
}{
15+
{
16+
name: "should not return an error for a valid json object",
17+
fieldVal: "{}",
18+
},
19+
{
20+
name: "should not return an error for a null",
21+
fieldVal: "null",
22+
},
23+
24+
{
25+
name: "should return an error if the field is not a string",
26+
fieldVal: true,
27+
expectedErrsToContain: []string{
28+
"expected type of field-name to be string",
29+
},
30+
},
31+
{
32+
name: "should return an error if the field is valid json, but not an object",
33+
fieldVal: "[]",
34+
expectedErrsToContain: []string{
35+
"expected field-name to be a JSON object. Check the documentation for the expected format.",
36+
},
37+
},
38+
}
39+
40+
for _, tt := range tests {
41+
t.Run(tt.name, func(t *testing.T) {
42+
warnings, errors := stringIsJSONObject(tt.fieldVal, "field-name")
43+
require.Empty(t, warnings)
44+
45+
require.Equal(t, len(tt.expectedErrsToContain), len(errors))
46+
for i, err := range errors {
47+
require.ErrorContains(t, err, tt.expectedErrsToContain[i])
48+
}
49+
})
50+
}
51+
}

0 commit comments

Comments
 (0)