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 1 commit
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
}
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,omitempty"`
Color string `json:"color,omitempty"`
Initials string `json:"initials,omitempty"`
DisabledFeatures []string `json:"disabledFeatures,omitempty"`
ImageUrl string `json:"imageUrl,omitempty"`
}
35 changes: 28 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,36 @@ 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
}

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
}
35 changes: 28 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,36 @@ 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{
ishtiaqhimel marked this conversation as resolved.
Show resolved Hide resolved
"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
}

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