feat: implement organization context in the cli (#12259)

* feat: implement organization context in the cli

`coder org show current`
This commit is contained in:
Steven Masley
2024-02-26 10:03:49 -06:00
committed by GitHub
parent f44c89d200
commit d2998c6b7b
24 changed files with 290 additions and 52 deletions
+13 -7
View File
@@ -26,11 +26,11 @@ const (
// Organization is the JSON representation of a Coder organization.
type Organization struct {
ID uuid.UUID `json:"id" validate:"required" format:"uuid"`
Name string `json:"name" validate:"required"`
CreatedAt time.Time `json:"created_at" validate:"required" format:"date-time"`
UpdatedAt time.Time `json:"updated_at" validate:"required" format:"date-time"`
IsDefault bool `json:"is_default" validate:"required"`
ID uuid.UUID `table:"id" json:"id" validate:"required" format:"uuid"`
Name string `table:"name,default_sort" json:"name" validate:"required"`
CreatedAt time.Time `table:"created_at" json:"created_at" validate:"required" format:"date-time"`
UpdatedAt time.Time `table:"updated_at" json:"updated_at" validate:"required" format:"date-time"`
IsDefault bool `table:"default" json:"is_default" validate:"required"`
}
type OrganizationMember struct {
@@ -153,8 +153,8 @@ type CreateWorkspaceRequest struct {
AutomaticUpdates AutomaticUpdates `json:"automatic_updates,omitempty"`
}
func (c *Client) Organization(ctx context.Context, id uuid.UUID) (Organization, error) {
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/organizations/%s", id.String()), nil)
func (c *Client) OrganizationByName(ctx context.Context, name string) (Organization, error) {
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/organizations/%s", name), nil)
if err != nil {
return Organization{}, xerrors.Errorf("execute request: %w", err)
}
@@ -168,6 +168,12 @@ func (c *Client) Organization(ctx context.Context, id uuid.UUID) (Organization,
return organization, json.NewDecoder(res.Body).Decode(&organization)
}
func (c *Client) Organization(ctx context.Context, id uuid.UUID) (Organization, error) {
// OrganizationByName uses the exact same endpoint. It accepts a name or uuid.
// We just provide this function for type safety.
return c.OrganizationByName(ctx, id.String())
}
// ProvisionerDaemons returns provisioner daemons available.
func (c *Client) ProvisionerDaemons(ctx context.Context) ([]ProvisionerDaemon, error) {
res, err := c.Request(ctx, http.MethodGet,
+1 -1
View File
@@ -573,7 +573,7 @@ func (c *Client) OrganizationsByUser(ctx context.Context, user string) ([]Organi
return orgs, json.NewDecoder(res.Body).Decode(&orgs)
}
func (c *Client) OrganizationByName(ctx context.Context, user string, name string) (Organization, error) {
func (c *Client) OrganizationByUserAndName(ctx context.Context, user string, name string) (Organization, error) {
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/users/%s/organizations/%s", user, name), nil)
if err != nil {
return Organization{}, err