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

Add create space method for dashboard #95

Merged
merged 2 commits into from
Apr 19, 2024
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
3 changes: 2 additions & 1 deletion elasticsearchdashboard/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ type EDClient interface {
GetStateFromHealthResponse(health *Health) (esapi.DashboardServerState, error)
ExportSavedObjects(spaceName string) (*Response, error)
ImportSavedObjects(spaceName, filepath string) (*Response, error)
ListSpaces() ([]string, error)
ListSpaces() ([]Space, error)
CreateSpace(space Space) error
}
17 changes: 16 additions & 1 deletion elasticsearchdashboard/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,14 @@ const (
"observability-visualization", "search"]}`
SavedObjectsExportURL = "/api/saved_objects/_export"
SavedObjectsImportURL = "/api/saved_objects/_import"
ListSpacesURL = "/api/spaces/space"
SpacesURL = "/api/spaces/space"
)

var jsonHeaderForKibanaAPI = map[string]string{
"Content-Type": "application/json",
"kbn-xsrf": "true",
}

type Client struct {
EDClient
}
Expand Down Expand Up @@ -88,3 +93,13 @@ type ResponseBody struct {
Status map[string]interface{} `json:"status"`
Metrics map[string]interface{} `json:"metrics"`
}

type Space struct {
Id string `json:"id"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Color string `json:"color,omitempty"`
Initials string `json:"initials,omitempty"`
DisabledFeatures []string `json:"disabledFeatures,omitempty"`
ImageUrl string `json:"imageUrl,omitempty"`
}
42 changes: 27 additions & 15 deletions elasticsearchdashboard/ed_client_v7.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,7 @@ func (h *EDClientV7) GetStateFromHealthResponse(health *Health) (esapi.Dashboard
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",
}).
SetHeaders(jsonHeaderForKibanaAPI).
SetBody([]byte(SavedObjectsReqBodyES))
res, err := req.Post("/s/" + spaceName + SavedObjectsExportURL)
if err != nil {
Expand Down Expand Up @@ -147,14 +144,11 @@ func (h *EDClientV7) ImportSavedObjects(spaceName, filepath string) (*Response,
}, nil
}

func (h *EDClientV7) ListSpaces() ([]string, error) {
func (h *EDClientV7) ListSpaces() ([]Space, error) {
req := h.Client.R().
SetDoNotParseResponse(true).
SetHeaders(map[string]string{
"Content-Type": "application/json",
"kbn-xsrf": "true",
})
res, err := req.Get(ListSpacesURL)
SetHeaders(jsonHeaderForKibanaAPI)
res, err := req.Get(SpacesURL)
if err != nil {
klog.Error("Failed to send http request")
return nil, err
Expand All @@ -169,15 +163,33 @@ func (h *EDClientV7) ListSpaces() ([]string, error) {
return nil, fmt.Errorf("failed to list dashboard spaces %s", string(body))
}

var spaces []map[string]interface{}
var spaces []Space
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 spaces, nil
}

func (h *EDClientV7) CreateSpace(space Space) error {
req := h.Client.R().
SetDoNotParseResponse(true).
SetHeaders(jsonHeaderForKibanaAPI).
SetBody(space)
res, err := req.Post(SpacesURL)
if err != nil {
klog.Error(err, "Failed to send http request")
return err
}

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

if res.StatusCode() != http.StatusOK {
return fmt.Errorf("failed to create dashboard space %s: %s", space.Name, string(body))
}

return spacesName, nil
return nil
}
42 changes: 27 additions & 15 deletions elasticsearchdashboard/ed_client_v8.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,7 @@ func (h *EDClientV8) GetStateFromHealthResponse(health *Health) (esapi.Dashboard
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",
}).
SetHeaders(jsonHeaderForKibanaAPI).
SetBody([]byte(SavedObjectsReqBodyES))
res, err := req.Post("/s/" + spaceName + SavedObjectsExportURL)
if err != nil {
Expand Down Expand Up @@ -145,14 +142,11 @@ func (h *EDClientV8) ImportSavedObjects(spaceName, filepath string) (*Response,
}, nil
}

func (h *EDClientV8) ListSpaces() ([]string, error) {
func (h *EDClientV8) ListSpaces() ([]Space, error) {
req := h.Client.R().
SetDoNotParseResponse(true).
SetHeaders(map[string]string{
"Content-Type": "application/json",
"kbn-xsrf": "true",
})
res, err := req.Get(ListSpacesURL)
SetHeaders(jsonHeaderForKibanaAPI)
res, err := req.Get(SpacesURL)
if err != nil {
klog.Error("Failed to send http request")
return nil, err
Expand All @@ -167,15 +161,33 @@ func (h *EDClientV8) ListSpaces() ([]string, error) {
return nil, fmt.Errorf("failed to list dashboard spaces %s", string(body))
}

var spaces []map[string]interface{}
var spaces []Space
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 spaces, nil
}

func (h *EDClientV8) CreateSpace(space Space) error {
req := h.Client.R().
SetDoNotParseResponse(true).
SetHeaders(jsonHeaderForKibanaAPI).
SetBody(space)
res, err := req.Post(SpacesURL)
if err != nil {
klog.Error(err, "Failed to send http request")
return err
}

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

if res.StatusCode() != http.StatusOK {
return fmt.Errorf("failed to create dashboard space %s: %s", space.Name, string(body))
}

return spacesName, nil
return nil
}
8 changes: 6 additions & 2 deletions elasticsearchdashboard/os_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ func (h *OSClient) ImportSavedObjects(_, filepath string) (*Response, error) {
}, nil
}

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

func (h *OSClient) CreateSpace(_ Space) error {
return nil
}
Loading