Skip to content

Commit

Permalink
Do not create attributes.title if it didn't exist previously
Browse files Browse the repository at this point in the history
  • Loading branch information
adriansr committed Apr 22, 2020
1 parent ea029b6 commit e3642de
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
5 changes: 4 additions & 1 deletion libbeat/dashboards/modify_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ func ReplaceIndexInIndexPattern(index string, content common.MapStr) (err error)
// This uses Put instead of DeepUpdate to avoid modifying types for
// inner objects. (DeepUpdate will replace maps with MapStr).
obj.Put("id", index)
obj.Put("attributes.title", index)
// Only overwrite title if it exists.
if _, err := obj.GetValue("attributes.title"); err == nil {
obj.Put("attributes.title", index)
}
}

switch v := list.(type) {
Expand Down
37 changes: 34 additions & 3 deletions libbeat/dashboards/modify_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,13 @@ func TestReplaceIndexInIndexPattern(t *testing.T) {
// what the inner types are (MapStr, map[string]interface{} or interface{}).
// Also ensures that the inner types are not modified after replacement.
tests := []struct {
title string
input common.MapStr
index string
expected common.MapStr
}{
{
title: "Replace in []interface(map).map",
input: common.MapStr{"objects": []interface{}{map[string]interface{}{
"id": "phonybeat-*",
"type": "index-pattern",
Expand All @@ -139,6 +141,7 @@ func TestReplaceIndexInIndexPattern(t *testing.T) {
}}}},
},
{
title: "Replace in []interface(map).mapstr",
input: common.MapStr{"objects": []interface{}{map[string]interface{}{
"id": "phonybeat-*",
"type": "index-pattern",
Expand All @@ -156,6 +159,7 @@ func TestReplaceIndexInIndexPattern(t *testing.T) {
}}}},
},
{
title: "Replace in []map.mapstr",
input: common.MapStr{"objects": []map[string]interface{}{{
"id": "phonybeat-*",
"type": "index-pattern",
Expand All @@ -173,6 +177,7 @@ func TestReplaceIndexInIndexPattern(t *testing.T) {
}}}},
},
{
title: "Replace in []mapstr.mapstr",
input: common.MapStr{"objects": []common.MapStr{{
"id": "phonybeat-*",
"type": "index-pattern",
Expand All @@ -190,6 +195,7 @@ func TestReplaceIndexInIndexPattern(t *testing.T) {
}}}},
},
{
title: "Replace in []mapstr.interface(mapstr)",
input: common.MapStr{"objects": []common.MapStr{{
"id": "phonybeat-*",
"type": "index-pattern",
Expand All @@ -206,11 +212,36 @@ func TestReplaceIndexInIndexPattern(t *testing.T) {
"timeFieldName": "@timestamp",
})}}},
},
{
title: "Do not create missing attributes",
input: common.MapStr{"objects": []common.MapStr{{
"id": "phonybeat-*",
"type": "index-pattern",
}}},
index: "otherindex-*",
expected: common.MapStr{"objects": []common.MapStr{{
"id": "otherindex-*",
"type": "index-pattern",
}}},
},
{
title: "Create missing id",
input: common.MapStr{"objects": []common.MapStr{{
"type": "index-pattern",
}}},
index: "otherindex-*",
expected: common.MapStr{"objects": []common.MapStr{{
"id": "otherindex-*",
"type": "index-pattern",
}}},
},
}

for _, test := range tests {
err := ReplaceIndexInIndexPattern(test.index, test.input)
assert.NoError(t, err)
assert.Equal(t, test.expected, test.input)
t.Run(test.title, func(t *testing.T) {
err := ReplaceIndexInIndexPattern(test.index, test.input)
assert.NoError(t, err)
assert.Equal(t, test.expected, test.input)
})
}
}

0 comments on commit e3642de

Please sign in to comment.