Skip to content

Commit

Permalink
Add workspace operations
Browse files Browse the repository at this point in the history
Signed-off-by: Andy Lo-A-Foe <andy.loafoe@gmail.com>
  • Loading branch information
loafoe committed Sep 20, 2021
1 parent dc44b1e commit 9806f3a
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions ai/workspace/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ type Workspace struct {
LastUpdated string `json:"lastUpdated,omitempty"`
}

type AccessURL struct {
URL string `json:"url"`
}

type LogArtefact struct {
StartupLog []string `json:"startupLog"`
}

func (s *Service) path(components ...string) string {
return path.Join(components...)
}
Expand Down Expand Up @@ -122,3 +130,59 @@ func (s *Service) GetWorkspaces(opt *ai.GetOptions, options ...ai.OptionFunc) ([
}
return workspaces, resp, err
}

func (s *Service) StartWorkspace(ws Workspace) (*ai.Response, error) {
req, err := s.client.NewAIRequest("POST", s.path("Workspace", ws.ID, "$start"), nil, nil)
if err != nil {
return nil, err
}
req.Header.Set("Api-Version", ai.APIVersion)

return s.client.Do(req, nil)
}

func (s *Service) StopWorkspace(ws Workspace) (*ai.Response, error) {
req, err := s.client.NewAIRequest("POST", s.path("Workspace", ws.ID, "$stop"), nil, nil)
if err != nil {
return nil, err
}
req.Header.Set("Api-Version", ai.APIVersion)

return s.client.Do(req, nil)
}

func (s *Service) GetWorkspaceAccessURL(ws Workspace) (*AccessURL, *ai.Response, error) {
req, err := s.client.NewAIRequest("POST", s.path("Workspace", ws.ID, "$accessUrl"), nil, nil)
if err != nil {
return nil, nil, err
}
req.Header.Set("Api-Version", ai.APIVersion)

var accessURL AccessURL
resp, err := s.client.Do(req, &accessURL)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
return nil, resp, ai.ErrEmptyResult
}
return nil, resp, err
}
return &accessURL, resp, err
}

func (s *Service) GetWorkspaceLogs(ws Workspace) (*LogArtefact, *ai.Response, error) {
req, err := s.client.NewAIRequest("POST", s.path("Workspace", ws.ID, "$logs"), nil, nil)
if err != nil {
return nil, nil, err
}
req.Header.Set("Api-Version", ai.APIVersion)

var artefact LogArtefact
resp, err := s.client.Do(req, &artefact)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
return nil, resp, ai.ErrEmptyResult
}
return nil, resp, err
}
return &artefact, resp, err
}

0 comments on commit 9806f3a

Please sign in to comment.