Skip to content

Commit

Permalink
List spaces for kibana dashboard (#87)
Browse files Browse the repository at this point in the history
Signed-off-by: Md. Ishtiaq Islam <[email protected]>
  • Loading branch information
ishtiaqhimel authored Feb 8, 2024
1 parent 902c39a commit 5046209
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 14 deletions.
5 changes: 3 additions & 2 deletions elasticsearchdashboard/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
type EDClient interface {
GetHealthStatus() (*Health, error)
GetStateFromHealthResponse(health *Health) (esapi.DashboardServerState, error)
ExportSavedObjects() (*Response, error)
ImportSavedObjects(filepath string) (*Response, error)
ExportSavedObjects(spaceName string) (*Response, error)
ImportSavedObjects(spaceName, filepath string) (*Response, error)
ListSpaces() ([]string, error)
}
5 changes: 3 additions & 2 deletions elasticsearchdashboard/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ const (
SavedObjectsReqBodyES = `{"type": ["dashboard", "config", "index-pattern", "url", "query", "tag", "canvas-element", "canvas-workpad", "action", "alert", "visualization",
"graph-workspace", "map", "lens", "cases", "search", "osquery-saved-query", "osquery-pack", "uptime-dynamic-settings", "infrastructure-ui-source", "metrics-explorer-view",
"inventory-view", "apm-indices"]}`
SavedObjectsReqBodyOS = `{"type": ["config", "url", "index-pattern", "query", "dashboard", "visualization", "visualization-visbuilder", "augment-vis", "map",
"observability-panel", "observability-visualization", "search"]}`
SavedObjectsReqBodyOS = `{"type": ["config", "url", "index-pattern", "query", "dashboard", "visualization", "visualization-visbuilder", "map", "observability-panel",
"observability-visualization", "search"]}`
SavedObjectsExportURL = "/api/saved_objects/_export"
SavedObjectsImportURL = "/api/saved_objects/_import"
ListSpacesURL = "/api/spaces/space"
)

type Client struct {
Expand Down
45 changes: 41 additions & 4 deletions elasticsearchdashboard/ed_client_v7.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package elasticsearchdashboard

import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"

esapi "kubedb.dev/apimachinery/apis/elasticsearch/v1alpha1"
Expand Down Expand Up @@ -107,15 +109,15 @@ func (h *EDClientV7) GetStateFromHealthResponse(health *Health) (esapi.Dashboard
return esapi.DashboardServerState(health.OverallState), nil
}

func (h *EDClientV7) ExportSavedObjects() (*Response, error) {
func (h *EDClientV7) ExportSavedObjects(spaceName string) (*Response, error) {
req := h.Client.R().
SetDoNotParseResponse(true).
SetHeaders(map[string]string{
"Content-Type": "application/json",
"kbn-xsrf": "true",
}).
SetBody([]byte(SavedObjectsReqBodyES))
res, err := req.Post(SavedObjectsExportURL)
res, err := req.Post("/s/" + spaceName + SavedObjectsExportURL)
if err != nil {
klog.Error(err, "Failed to send http request")
return nil, err
Expand All @@ -127,13 +129,13 @@ func (h *EDClientV7) ExportSavedObjects() (*Response, error) {
}, nil
}

func (h *EDClientV7) ImportSavedObjects(filepath string) (*Response, error) {
func (h *EDClientV7) ImportSavedObjects(spaceName, filepath string) (*Response, error) {
req := h.Client.R().
SetDoNotParseResponse(true).
SetHeader("kbn-xsrf", "true").
SetFile("file", filepath).
SetQueryParam("overwrite", "true")
res, err := req.Post(SavedObjectsImportURL)
res, err := req.Post("/s/" + spaceName + SavedObjectsImportURL)
if err != nil {
klog.Error(err, "Failed to send http request")
return nil, err
Expand All @@ -144,3 +146,38 @@ func (h *EDClientV7) ImportSavedObjects(filepath string) (*Response, error) {
Body: res.RawBody(),
}, nil
}

func (h *EDClientV7) ListSpaces() ([]string, error) {
req := h.Client.R().
SetDoNotParseResponse(true).
SetHeaders(map[string]string{
"Content-Type": "application/json",
"kbn-xsrf": "true",
})
res, err := req.Get(ListSpacesURL)
if err != nil {
klog.Error("Failed to send http request")
return nil, err
}

body, err := io.ReadAll(res.RawBody())
if err != nil {
return nil, err
}

if res.StatusCode() != http.StatusOK {
return nil, fmt.Errorf("failed to list dashboard spaces %s", string(body))
}

var spaces []map[string]interface{}
if err = json.Unmarshal(body, &spaces); err != nil {
return nil, err
}

var spacesName []string
for _, space := range spaces {
spacesName = append(spacesName, space["id"].(string))
}

return spacesName, nil
}
45 changes: 41 additions & 4 deletions elasticsearchdashboard/ed_client_v8.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package elasticsearchdashboard

import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"

esapi "kubedb.dev/apimachinery/apis/elasticsearch/v1alpha1"
Expand Down Expand Up @@ -105,15 +107,15 @@ func (h *EDClientV8) GetStateFromHealthResponse(health *Health) (esapi.Dashboard
return esapi.DashboardServerState(health.OverallState), nil
}

func (h *EDClientV8) ExportSavedObjects() (*Response, error) {
func (h *EDClientV8) ExportSavedObjects(spaceName string) (*Response, error) {
req := h.Client.R().
SetDoNotParseResponse(true).
SetHeaders(map[string]string{
"Content-Type": "application/json",
"kbn-xsrf": "true",
}).
SetBody([]byte(SavedObjectsReqBodyES))
res, err := req.Post(SavedObjectsExportURL)
res, err := req.Post("/s/" + spaceName + SavedObjectsExportURL)
if err != nil {
klog.Error(err, "Failed to send http request")
return nil, err
Expand All @@ -125,13 +127,13 @@ func (h *EDClientV8) ExportSavedObjects() (*Response, error) {
}, nil
}

func (h *EDClientV8) ImportSavedObjects(filepath string) (*Response, error) {
func (h *EDClientV8) ImportSavedObjects(spaceName, filepath string) (*Response, error) {
req := h.Client.R().
SetDoNotParseResponse(true).
SetHeader("kbn-xsrf", "true").
SetFile("file", filepath).
SetQueryParam("overwrite", "true")
res, err := req.Post(SavedObjectsImportURL)
res, err := req.Post("/s/" + spaceName + SavedObjectsImportURL)
if err != nil {
klog.Error(err, "Failed to send http request")
return nil, err
Expand All @@ -142,3 +144,38 @@ func (h *EDClientV8) ImportSavedObjects(filepath string) (*Response, error) {
Body: res.RawBody(),
}, nil
}

func (h *EDClientV8) ListSpaces() ([]string, error) {
req := h.Client.R().
SetDoNotParseResponse(true).
SetHeaders(map[string]string{
"Content-Type": "application/json",
"kbn-xsrf": "true",
})
res, err := req.Get(ListSpacesURL)
if err != nil {
klog.Error("Failed to send http request")
return nil, err
}

body, err := io.ReadAll(res.RawBody())
if err != nil {
return nil, err
}

if res.StatusCode() != http.StatusOK {
return nil, fmt.Errorf("failed to list dashboard spaces %s", string(body))
}

var spaces []map[string]interface{}
if err = json.Unmarshal(body, &spaces); err != nil {
return nil, err
}

var spacesName []string
for _, space := range spaces {
spacesName = append(spacesName, space["id"].(string))
}

return spacesName, nil
}
8 changes: 6 additions & 2 deletions elasticsearchdashboard/os_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (h *OSClient) GetStateFromHealthResponse(health *Health) (esapi.DashboardSe
return esapi.DashboardServerState(health.OverallState), nil
}

func (h *OSClient) ExportSavedObjects() (*Response, error) {
func (h *OSClient) ExportSavedObjects(_ string) (*Response, error) {
req := h.Client.R().
SetDoNotParseResponse(true).
SetHeaders(map[string]string{
Expand All @@ -127,7 +127,7 @@ func (h *OSClient) ExportSavedObjects() (*Response, error) {
}, nil
}

func (h *OSClient) ImportSavedObjects(filepath string) (*Response, error) {
func (h *OSClient) ImportSavedObjects(_, filepath string) (*Response, error) {
req := h.Client.R().
SetDoNotParseResponse(true).
SetHeader("osd-xsrf", "true").
Expand All @@ -144,3 +144,7 @@ func (h *OSClient) ImportSavedObjects(filepath string) (*Response, error) {
Body: res.RawBody(),
}, nil
}

func (h *OSClient) ListSpaces() ([]string, error) {
return []string{"default"}, nil
}

0 comments on commit 5046209

Please sign in to comment.