Skip to content

Commit

Permalink
Create spaces for dashboard
Browse files Browse the repository at this point in the history
Signed-off-by: Md. Ishtiaq Islam <[email protected]>
  • Loading branch information
ishtiaqhimel committed Apr 17, 2024
1 parent e01cb9e commit 985900c
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 18 deletions.
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
}
12 changes: 11 additions & 1 deletion elasticsearchdashboard/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const (
"observability-visualization", "search"]}`
SavedObjectsExportURL = "/api/saved_objects/_export"
SavedObjectsImportURL = "/api/saved_objects/_import"
ListSpacesURL = "/api/spaces/space"
SpacesURL = "/api/spaces/space"
)

type Client struct {
Expand Down Expand Up @@ -88,3 +88,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"`
Color string `json:"color"`
Initials string `json:"initials"`
DisabledFeatures []string `json:"disabledFeatures"`
ImageUrl string `json:"imageUrl"`
}
30 changes: 23 additions & 7 deletions elasticsearchdashboard/ed_client_v7.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,14 @@ 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)
res, err := req.Get(SpacesURL)
if err != nil {
klog.Error("Failed to send http request")
return nil, err
Expand All @@ -169,15 +169,31 @@ 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(map[string]string{
"Content-Type": "application/json",
"kbn-xsrf": "true",
}).
SetBody(space)
res, err := req.Post(SpacesURL)
if err != nil {
klog.Error(err, "Failed to send http request")
return err
}

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

return spacesName, nil
return nil
}
30 changes: 23 additions & 7 deletions elasticsearchdashboard/ed_client_v8.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,14 @@ 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)
res, err := req.Get(SpacesURL)
if err != nil {
klog.Error("Failed to send http request")
return nil, err
Expand All @@ -167,15 +167,31 @@ 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(map[string]string{
"Content-Type": "application/json",
"kbn-xsrf": "true",
}).
SetBody(space)
res, err := req.Post(SpacesURL)
if err != nil {
klog.Error(err, "Failed to send http request")
return err
}

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

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 Space) error {
return nil
}

0 comments on commit 985900c

Please sign in to comment.