Skip to content

Commit

Permalink
Fix import-beats: prepend module names to Kibana dashboards (elastic#322
Browse files Browse the repository at this point in the history
)

* Prepend module names to imported Kibana dashboards

* Fix: bug, sample event may not be defined

* Fix: mage format

* Address PR comments
  • Loading branch information
mtojek authored Oct 20, 2020
1 parent 48953d1 commit fc2a350
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
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

0 comments on commit fc2a350

Please sign in to comment.