-
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
Use Saved Objects API for exporting and importing dashboards #27220
Changes from 1 commit
d584667
b5d8612
e9c0418
172ca9d
ef2cc98
459e3de
23c97d7
fc404c0
2d44a6c
3f9a156
e93c8d3
773ba59
08b925c
d1c4067
88c52b3
8ec3b6e
def1720
23f47fe
60642e8
ffcc7a3
53072cc
978336e
ff1ca85
eec349e
85cd464
5541d2d
513eedf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -181,40 +181,49 @@ func ReplaceIndexInDashboardObject(index string, content []byte) []byte { | |
line, err := r.ReadBytes('\n') | ||
if err != nil { | ||
if err == io.EOF { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there's a subtle usage issue here in that
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I initially started with
I might misunderstood the documentation, but I believe what I had written is correct. The data I am processing here is NDJSON. Thus, there must be a new line at the end of the JSON because it is the delimiter. If the delimiter is missing the JSON is invalid, and I return the data collected up until the error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't you think it there should be some kind of error returned or error handling for malformed data that does not end in a newline? This implementation silently discards the data. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There an error is logged a few lines below when there is an error. Also, the original data remain untouched, so the user can rerun the command after inspecting/fixing the input data. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the case where the file ends with JSON not terminated by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed. I have tested with incomplete lines in a more complex scenario where it was correct. I have looked at the data exported from Kibana also, the spec from http://ndjson.org/ and it seems that EOF is a valid separator as well. So the errors we have to handle is anything besides EOF. So I still believe this is correct. :D |
||
return result | ||
return append(result, replaceInNDJSON(logger, index, line)...) | ||
} | ||
logger.Error("Error reading bytes from raw dashboard object: %+v", err) | ||
return content | ||
} | ||
objectMap := make(map[string]interface{}, 0) | ||
err = json.Unmarshal(line, &objectMap) | ||
if err != nil { | ||
result = append(result, append(line, newline...)...) | ||
continue | ||
} | ||
result = append(result, replaceInNDJSON(logger, index, line)...) | ||
} | ||
} | ||
|
||
attributes, ok := objectMap["attributes"].(map[string]interface{}) | ||
if !ok { | ||
result = append(result, append(line, newline...)...) | ||
continue | ||
} | ||
func replaceInNDJSON(logger *logp.Logger, index string, line []byte) []byte { | ||
if len(line) == 0 { | ||
return line | ||
} | ||
|
||
if kibanaSavedObject, ok := attributes["kibanaSavedObjectMeta"].(map[string]interface{}); ok { | ||
attributes["kibanaSavedObjectMeta"] = ReplaceIndexInSavedObject(logger, index, kibanaSavedObject) | ||
} | ||
objectMap := make(map[string]interface{}, 0) | ||
err := json.Unmarshal(line, &objectMap) | ||
if err != nil { | ||
logger.Errorf("Failed to convert bytes to map[string]interface: %+v", err) | ||
return line | ||
} | ||
|
||
if visState, ok := attributes["visState"].(string); ok { | ||
attributes["visState"] = ReplaceIndexInVisState(logger, index, visState) | ||
} | ||
attributes, ok := objectMap["attributes"].(map[string]interface{}) | ||
if !ok { | ||
logger.Errorf("Object does not have attributes key") | ||
return line | ||
} | ||
|
||
b, err := json.Marshal(objectMap) | ||
if err != nil { | ||
logger.Error("Error marshaling modified dashboard: %+v", err) | ||
result = append(result, append(line, newline...)...) | ||
} | ||
if kibanaSavedObject, ok := attributes["kibanaSavedObjectMeta"].(map[string]interface{}); ok { | ||
attributes["kibanaSavedObjectMeta"] = ReplaceIndexInSavedObject(logger, index, kibanaSavedObject) | ||
} | ||
|
||
result = append(result, append(b, newline...)...) | ||
if visState, ok := attributes["visState"].(string); ok { | ||
attributes["visState"] = ReplaceIndexInVisState(logger, index, visState) | ||
} | ||
|
||
b, err := json.Marshal(objectMap) | ||
if err != nil { | ||
logger.Error("Error marshaling modified dashboard: %+v", err) | ||
return line | ||
} | ||
|
||
return append(b, newline...) | ||
|
||
} | ||
|
||
// ReplaceStringInDashboard replaces a string field in a dashboard | ||
|
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 think json.Unmarshal will return an error if you try to decode an empty slice.
You might want to do a
if len(bytes.TrimSpace(line)) == 0 { return }
. This handles both empty slices and blank lines (which are allowable per the NDJSON spec you linked to.)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 have added this check to more functions that needed it.