diff --git a/coder-sdk/env.go b/coder-sdk/env.go index 03ac3c71..31187a1e 100644 --- a/coder-sdk/env.go +++ b/coder-sdk/env.go @@ -75,6 +75,7 @@ const ( type CreateEnvironmentRequest struct { Name string `json:"name"` ImageID string `json:"image_id"` + OrgID string `json:"org_id"` ImageTag string `json:"image_tag"` CPUCores float32 `json:"cpu_cores"` MemoryGB float32 `json:"memory_gb"` @@ -85,9 +86,9 @@ type CreateEnvironmentRequest struct { } // CreateEnvironment sends a request to create an environment. -func (c Client) CreateEnvironment(ctx context.Context, orgID string, req CreateEnvironmentRequest) (*Environment, error) { +func (c Client) CreateEnvironment(ctx context.Context, req CreateEnvironmentRequest) (*Environment, error) { var env Environment - if err := c.requestBody(ctx, http.MethodPost, "/api/private/orgs/"+orgID+"/environments", req, &env); err != nil { + if err := c.requestBody(ctx, http.MethodPost, "/api/v0/environments", req, &env); err != nil { return nil, err } return &env, nil @@ -103,10 +104,17 @@ func (c Client) Environments(ctx context.Context) ([]Environment, error) { return envs, nil } -// EnvironmentsByOrganization gets the list of environments owned by the given user. -func (c Client) EnvironmentsByOrganization(ctx context.Context, userID, orgID string) ([]Environment, error) { - var envs []Environment - if err := c.requestBody(ctx, http.MethodGet, "/api/private/orgs/"+orgID+"/members/"+userID+"/environments", nil, &envs); err != nil { +// UserEnvironmentsByOrganization gets the list of environments owned by the given user. +func (c Client) UserEnvironmentsByOrganization(ctx context.Context, userID, orgID string) ([]Environment, error) { + var ( + envs []Environment + query = url.Values{} + ) + + query.Add("orgs", orgID) + query.Add("users", userID) + + if err := c.requestBody(ctx, http.MethodGet, "/api/v0/environments", nil, &envs, withQueryParams(query)); err != nil { return nil, err } return envs, nil diff --git a/coder-sdk/org.go b/coder-sdk/org.go index bc1d5203..49053e75 100644 --- a/coder-sdk/org.go +++ b/coder-sdk/org.go @@ -39,7 +39,7 @@ const ( // Organizations gets all Organizations. func (c Client) Organizations(ctx context.Context) ([]Organization, error) { var orgs []Organization - if err := c.requestBody(ctx, http.MethodGet, "/api/private/orgs", nil, &orgs); err != nil { + if err := c.requestBody(ctx, http.MethodGet, "/api/v0/orgs", nil, &orgs); err != nil { return nil, err } return orgs, nil @@ -48,7 +48,7 @@ func (c Client) Organizations(ctx context.Context) ([]Organization, error) { // OrganizationByID get the Organization by its ID. func (c Client) OrganizationByID(ctx context.Context, orgID string) (*Organization, error) { var org Organization - err := c.requestBody(ctx, http.MethodGet, "/api/private/orgs/"+orgID, nil, &org) + err := c.requestBody(ctx, http.MethodGet, "/api/v0/orgs/"+orgID, nil, &org) if err != nil { return nil, err } diff --git a/coder-sdk/request.go b/coder-sdk/request.go index 0424d2da..c9f82ffa 100644 --- a/coder-sdk/request.go +++ b/coder-sdk/request.go @@ -50,8 +50,8 @@ func (c Client) request(ctx context.Context, method, path string, in interface{} // requestBody is a helper extending the Client.request helper, checking the response code // and decoding the response payload. -func (c Client) requestBody(ctx context.Context, method, path string, in, out interface{}) error { - resp, err := c.request(ctx, method, path, in) +func (c Client) requestBody(ctx context.Context, method, path string, in, out interface{}, opts ...requestOption) error { + resp, err := c.request(ctx, method, path, in, opts...) if err != nil { return xerrors.Errorf("Execute request: %q", err) } diff --git a/coder-sdk/users.go b/coder-sdk/users.go index ce2beee5..dd63ea3e 100644 --- a/coder-sdk/users.go +++ b/coder-sdk/users.go @@ -49,7 +49,7 @@ func (c Client) Me(ctx context.Context) (*User, error) { // UserByID get the details of a user by their id. func (c Client) UserByID(ctx context.Context, id string) (*User, error) { var u User - if err := c.requestBody(ctx, http.MethodGet, "/api/private/users/"+id, nil, &u); err != nil { + if err := c.requestBody(ctx, http.MethodGet, "/api/v0/users/"+id, nil, &u); err != nil { return nil, err } return &u, nil diff --git a/coder-sdk/ws.go b/coder-sdk/ws.go index f761a8be..81eeabf9 100644 --- a/coder-sdk/ws.go +++ b/coder-sdk/ws.go @@ -10,10 +10,18 @@ import ( type requestOptions struct { BaseURLOverride *url.URL + Query url.Values } type requestOption func(*requestOptions) +// withQueryParams sets the provided query parameters on the request. +func withQueryParams(q url.Values) func(o *requestOptions) { + return func(o *requestOptions) { + o.Query = q + } +} + func withBaseURL(base *url.URL) func(o *requestOptions) { return func(o *requestOptions) { o.BaseURLOverride = base @@ -31,12 +39,6 @@ func (c Client) dialWebsocket(ctx context.Context, path string, options ...reque if config.BaseURLOverride != nil { url = *config.BaseURLOverride } - - if url.Scheme == "https" { - url.Scheme = "wss" - } else { - url.Scheme = "ws" - } url.Path = path conn, resp, err := websocket.Dial(ctx, url.String(), &websocket.DialOptions{HTTPHeader: http.Header{"Session-Token": {c.Token}}}) diff --git a/internal/cmd/ceapi.go b/internal/cmd/ceapi.go index 32cd1f5d..04edfcb6 100644 --- a/internal/cmd/ceapi.go +++ b/internal/cmd/ceapi.go @@ -48,7 +48,7 @@ func getEnvs(ctx context.Context, client *coder.Client, email string) ([]coder.E var allEnvs []coder.Environment for _, org := range orgs { - envs, err := client.EnvironmentsByOrganization(ctx, user.ID, org.ID) + envs, err := client.UserEnvironmentsByOrganization(ctx, user.ID, org.ID) if err != nil { return nil, xerrors.Errorf("get envs for %s: %w", org.Name, err) } diff --git a/internal/cmd/envs.go b/internal/cmd/envs.go index f9b7fd41..13e463f1 100644 --- a/internal/cmd/envs.go +++ b/internal/cmd/envs.go @@ -197,6 +197,7 @@ coder envs create my-new-powerful-env --cpu 12 --disk 100 --memory 16 --image ub createReq := &coder.CreateEnvironmentRequest{ Name: args[0], ImageID: importedImg.ID, + OrgID: importedImg.OrganizationID, ImageTag: tag, CPUCores: cpu, MemoryGB: memory, @@ -217,7 +218,7 @@ coder envs create my-new-powerful-env --cpu 12 --disk 100 --memory 16 --image ub createReq.DiskGB = importedImg.DefaultDiskGB } - env, err := client.CreateEnvironment(ctx, importedImg.OrganizationID, *createReq) + env, err := client.CreateEnvironment(ctx, *createReq) if err != nil { return xerrors.Errorf("create environment: %w", err) }
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: