forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make the index name configurable when loading the 6.x and 5.x dashboa…
…rds (elastic#4949) (elastic#5018) (cherry picked from commit 36932d6)
- Loading branch information
Showing
5 changed files
with
182 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package dashboards | ||
|
||
import ( | ||
"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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters