forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 9
/
modify_json.go
119 lines (95 loc) · 3.19 KB
/
modify_json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package dashboards
import (
"bytes"
"encoding/json"
"fmt"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
)
type JSONObjectAttribute struct {
Description string `json:"description"`
KibanaSavedObjectMeta map[string]interface{} `json:"kibanaSavedObjectMeta"`
Title string `json:"title"`
Type string `json:"type"`
}
type JSONObject struct {
Attributes JSONObjectAttribute `json:"attributes"`
}
type JSONFormat struct {
Objects []JSONObject `json:"objects"`
}
func ReplaceIndexInIndexPattern(index string, content common.MapStr) common.MapStr {
if index != "" {
// change index pattern name
if objects, ok := content["objects"].([]interface{}); ok {
for i, object := range objects {
if objectMap, ok := object.(map[string]interface{}); ok {
objectMap["id"] = index
if attributes, ok := objectMap["attributes"].(map[string]interface{}); ok {
attributes["title"] = index
objectMap["attributes"] = attributes
}
objects[i] = objectMap
}
}
content["objects"] = objects
}
}
return content
}
func replaceIndexInSearchObject(index string, savedObject string) (string, error) {
var record common.MapStr
err := json.Unmarshal([]byte(savedObject), &record)
if err != nil {
return "", fmt.Errorf("fail to unmarshal searchSourceJSON from search : %v", err)
}
if _, ok := record["index"]; ok {
record["index"] = index
}
searchSourceJSON, err := json.Marshal(record)
if err != nil {
return "", fmt.Errorf("fail to marshal searchSourceJSON: %v", err)
}
return string(searchSourceJSON), nil
}
func ReplaceIndexInSavedObject(index string, kibanaSavedObject map[string]interface{}) map[string]interface{} {
if searchSourceJSON, ok := kibanaSavedObject["searchSourceJSON"].(string); ok {
searchSourceJSON, err := replaceIndexInSearchObject(index, searchSourceJSON)
if err != nil {
logp.Err("Fail to replace searchSourceJSON: %v", err)
return kibanaSavedObject
}
kibanaSavedObject["searchSourceJSON"] = searchSourceJSON
}
return kibanaSavedObject
}
func ReplaceIndexInDashboardObject(index string, content common.MapStr) common.MapStr {
if index == "" {
return content
}
if objects, ok := content["objects"].([]interface{}); ok {
for i, object := range objects {
if objectMap, ok := object.(map[string]interface{}); ok {
if attributes, ok := objectMap["attributes"].(map[string]interface{}); ok {
if kibanaSavedObject, ok := attributes["kibanaSavedObjectMeta"].(map[string]interface{}); ok {
attributes["kibanaSavedObjectMeta"] = ReplaceIndexInSavedObject(index, kibanaSavedObject)
}
objectMap["attributes"] = attributes
}
objects[i] = objectMap
}
}
content["objects"] = objects
}
return content
}
func ReplaceStringInDashboard(old, new string, content common.MapStr) (common.MapStr, error) {
marshaled, err := json.Marshal(content)
if err != nil {
return nil, fmt.Errorf("fail to marshal dashboard object: %v", content)
}
replaced := bytes.Replace(marshaled, []byte(old), []byte(new), -1)
var result common.MapStr
err = json.Unmarshal(replaced, &result)
return result, nil
}