-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change index name in the dashboards and index-pattern #4949
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/fail/failed/ -> not sure which one we shoud use, but we should be consistent :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I always using
fail
instead offailed
. Not sure what is more widely used.