Skip to content

Commit

Permalink
Fixes to import_dashboards (#4471)
Browse files Browse the repository at this point in the history
* Check if .kibana exists before trying to create it
  • Loading branch information
cwurm authored and andrewkroh committed Jun 12, 2017
1 parent ef614fe commit e110161
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
1 change: 1 addition & 0 deletions libbeat/dashboards/dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type DashboardLoader interface {
LoadJSON(path string, json map[string]interface{}) ([]byte, error)
CreateIndex(index string, body interface{}) (int, *elasticsearch.QueryResult, error)
IndexExists(index string) (int, error)
}

func ImportDashboards(beatName, beatVersion string, esClient DashboardLoader, cfg *common.Config) error {
Expand Down
54 changes: 35 additions & 19 deletions libbeat/dashboards/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,28 +73,41 @@ func (imp Importer) Import() error {
// some index properties which are needed as a workaround for:
// https://github.com/elastic/beats-dashboards/issues/94
func (imp Importer) CreateKibanaIndex() error {
imp.client.CreateIndex(imp.cfg.KibanaIndex,
common.MapStr{
"settings": common.MapStr{
"index.mapping.single_type": false,
},
})
_, _, err := imp.client.CreateIndex(imp.cfg.KibanaIndex+"/_mapping/search",
common.MapStr{
"search": common.MapStr{
"properties": common.MapStr{
"hits": common.MapStr{
"type": "integer",
status, err := imp.client.IndexExists(imp.cfg.KibanaIndex)

if err != nil {
if status != 404 {
return err
} else {
_, _, err = imp.client.CreateIndex(imp.cfg.KibanaIndex,
common.MapStr{
"settings": common.MapStr{
"index.mapping.single_type": false,
},
"version": common.MapStr{
"type": "integer",
})
if err != nil {
return fmt.Errorf("Failed to create index: %v", err)
}

_, _, err = imp.client.CreateIndex(imp.cfg.KibanaIndex+"/_mapping/search",
common.MapStr{
"search": common.MapStr{
"properties": common.MapStr{
"hits": common.MapStr{
"type": "integer",
},
"version": common.MapStr{
"type": "integer",
},
},
},
},
},
})
if err != nil {
imp.statusMsg("Failed to set the mapping: %v", err)
})
if err != nil {
return fmt.Errorf("Failed to set the mapping: %v", err)
}
}
}

return nil
}

Expand Down Expand Up @@ -492,6 +505,9 @@ func (imp Importer) downloadFile(url string, target string) (string, error) {
if err != nil {
return targetPath, err
}
if resp.StatusCode != 200 {
return targetPath, fmt.Errorf("Server returned: %s", resp.Status)
}
defer resp.Body.Close()

// Writer the body to file
Expand Down
8 changes: 8 additions & 0 deletions libbeat/outputs/elasticsearch/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ func (es *Connection) CreateIndex(index string, body interface{}) (int, *QueryRe
return withQueryResult(es.apiCall("PUT", index, "", "", "", nil, body))
}

// IndexExists checks if an index exists.
// Implements: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-exists.html
//
func (es *Connection) IndexExists(index string) (int, error) {
status, _, err := es.apiCall("HEAD", index, "", "", "", nil, nil)
return status, err
}

// Delete deletes a typed JSON document from a specific index based on its id.
// Implements: http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html
func (es *Connection) Delete(index string, docType string, id string, params map[string]string) (int, *QueryResult, error) {
Expand Down

0 comments on commit e110161

Please sign in to comment.