Skip to content
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

Fix import-beats: prepend module names to Kibana dashboards #322

Merged
merged 4 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions dev/import-beats/kibana.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,29 +234,40 @@ func convertToKibanaObjects(dashboardFile []byte, moduleName string, dataStreamN
return nil, errors.Wrapf(err, "retrieving type failed")
}

id, err := object.getValue("id")
if err != nil {
return nil, errors.Wrapf(err, "retrieving id failed")
}

dashboardName := id.(string)
if aType == "dashboard" && !strings.HasPrefix(dashboardName, moduleName+"-") {
dashboardName = moduleName + "-" + dashboardName
}

_, err = object.put("id", dashboardName)
if err != nil {
return nil, errors.Wrapf(err, "putting new ID failed")
}

data, err := json.MarshalIndent(object, "", " ")
if err != nil {
return nil, errors.Wrapf(err, "marshalling object failed")
}

data = replaceBlacklistedWords(
replaceFieldEventDataStreamWithStreamDataStream(
data))
data = prependModuleNameToDashboardLinks(replaceBlacklistedWords(
replaceFieldEventDatasetWithDataStreamDataset(
data)), moduleName)

err = verifyKibanaObjectConvertion(data)
if err != nil {
return nil, errors.Wrapf(err, "Kibana object convertion failed")
}

id, err := object.getValue("id")
if err != nil {
return nil, errors.Wrapf(err, "retrieving id failed")
}

if _, ok := extracted[aType.(string)]; !ok {
extracted[aType.(string)] = map[string][]byte{}
}
extracted[aType.(string)][id.(string)+".json"] = data

extracted[aType.(string)][dashboardName+".json"] = data
}

return extracted, nil
Expand Down Expand Up @@ -449,7 +460,7 @@ func stripReferencesToEventModuleInQuery(object mapStr, objectKey, moduleName st
return object, nil
}

func replaceFieldEventDataStreamWithStreamDataStream(data []byte) []byte {
func replaceFieldEventDatasetWithDataStreamDataset(data []byte) []byte {
return bytes.ReplaceAll(data, []byte("event.dataset"), []byte("data_stream.dataset"))
}

Expand All @@ -463,6 +474,12 @@ func replaceBlacklistedWords(data []byte) []byte {
return data
}

func prependModuleNameToDashboardLinks(data []byte, moduleName string) []byte {
data = bytes.ReplaceAll(data, []byte("/app/kibana#/dashboard/"+moduleName+"-"), []byte("/app/kibana#/dashboard/"))
data = bytes.ReplaceAll(data, []byte("/app/kibana#/dashboard/"), []byte("/app/kibana#/dashboard/"+moduleName+"-"))
return data
}

func verifyKibanaObjectConvertion(data []byte) error {
i := bytes.Index(data, []byte("event.module"))
if i > 0 {
Expand Down
3 changes: 3 additions & 0 deletions dev/import-beats/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,9 @@ func writeDoc(docsPath string, doc docContent, aPackage packageContent) error {
t = template.Must(t.Parse("TODO"))
} else {
t, err = t.Funcs(template.FuncMap{
"event": func(dataStream string) (string, error) {
return "TODO", nil
},
"fields": func(dataStream string) (string, error) {
return renderExportedFields(dataStream, aPackage.dataStreams)
},
Expand Down