feat: Add endpoint to get all workspaces a user can access (#1354)

This iterates through user organizations to get permitted
workspaces. This will allow admins to manage user workspaces!
This commit is contained in:
Kyle Carberry
2022-05-09 21:38:20 -05:00
committed by GitHub
parent e6f1ce1fb2
commit b675aec4dd
10 changed files with 177 additions and 280 deletions

View File

@ -402,6 +402,22 @@ func (c *Client) AuthMethods(ctx context.Context) (AuthMethods, error) {
return userAuth, json.NewDecoder(res.Body).Decode(&userAuth)
}
// WorkspacesByUser returns all workspaces a user has access to.
func (c *Client) WorkspacesByUser(ctx context.Context, userID uuid.UUID) ([]Workspace, error) {
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/users/%s/workspaces", uuidOrMe(userID)), nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, readBodyAsError(res)
}
var workspaces []Workspace
return workspaces, json.NewDecoder(res.Body).Decode(&workspaces)
}
// uuidOrMe returns the provided uuid as a string if it's valid, ortherwise
// `me`.
func uuidOrMe(id uuid.UUID) string {