Skip to content

Commit b04eb60

Browse files
authored
chore: update API-breaking environment routes (coder#218)
- The structure of our routing tree is being refactored as part of the effort to prepare our API for public consumption. This PR fixes some routes that were broken in a way that was not backwards compatible.
1 parent fb4ac31 commit b04eb60

File tree

7 files changed

+30
-19
lines changed

7 files changed

+30
-19
lines changed

coder-sdk/env.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ const (
7575
type CreateEnvironmentRequest struct {
7676
Name string `json:"name"`
7777
ImageID string `json:"image_id"`
78+
OrgID string `json:"org_id"`
7879
ImageTag string `json:"image_tag"`
7980
CPUCores float32 `json:"cpu_cores"`
8081
MemoryGB float32 `json:"memory_gb"`
@@ -85,9 +86,9 @@ type CreateEnvironmentRequest struct {
8586
}
8687

8788
// CreateEnvironment sends a request to create an environment.
88-
func (c Client) CreateEnvironment(ctx context.Context, orgID string, req CreateEnvironmentRequest) (*Environment, error) {
89+
func (c Client) CreateEnvironment(ctx context.Context, req CreateEnvironmentRequest) (*Environment, error) {
8990
var env Environment
90-
if err := c.requestBody(ctx, http.MethodPost, "/api/private/orgs/"+orgID+"/environments", req, &env); err != nil {
91+
if err := c.requestBody(ctx, http.MethodPost, "/api/v0/environments", req, &env); err != nil {
9192
return nil, err
9293
}
9394
return &env, nil
@@ -103,10 +104,17 @@ func (c Client) Environments(ctx context.Context) ([]Environment, error) {
103104
return envs, nil
104105
}
105106

106-
// EnvironmentsByOrganization gets the list of environments owned by the given user.
107-
func (c Client) EnvironmentsByOrganization(ctx context.Context, userID, orgID string) ([]Environment, error) {
108-
var envs []Environment
109-
if err := c.requestBody(ctx, http.MethodGet, "/api/private/orgs/"+orgID+"/members/"+userID+"/environments", nil, &envs); err != nil {
107+
// UserEnvironmentsByOrganization gets the list of environments owned by the given user.
108+
func (c Client) UserEnvironmentsByOrganization(ctx context.Context, userID, orgID string) ([]Environment, error) {
109+
var (
110+
envs []Environment
111+
query = url.Values{}
112+
)
113+
114+
query.Add("orgs", orgID)
115+
query.Add("users", userID)
116+
117+
if err := c.requestBody(ctx, http.MethodGet, "/api/v0/environments", nil, &envs, withQueryParams(query)); err != nil {
110118
return nil, err
111119
}
112120
return envs, nil

coder-sdk/org.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const (
3939
// Organizations gets all Organizations.
4040
func (c Client) Organizations(ctx context.Context) ([]Organization, error) {
4141
var orgs []Organization
42-
if err := c.requestBody(ctx, http.MethodGet, "/api/private/orgs", nil, &orgs); err != nil {
42+
if err := c.requestBody(ctx, http.MethodGet, "/api/v0/orgs", nil, &orgs); err != nil {
4343
return nil, err
4444
}
4545
return orgs, nil
@@ -48,7 +48,7 @@ func (c Client) Organizations(ctx context.Context) ([]Organization, error) {
4848
// OrganizationByID get the Organization by its ID.
4949
func (c Client) OrganizationByID(ctx context.Context, orgID string) (*Organization, error) {
5050
var org Organization
51-
err := c.requestBody(ctx, http.MethodGet, "/api/private/orgs/"+orgID, nil, &org)
51+
err := c.requestBody(ctx, http.MethodGet, "/api/v0/orgs/"+orgID, nil, &org)
5252
if err != nil {
5353
return nil, err
5454
}

coder-sdk/request.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ func (c Client) request(ctx context.Context, method, path string, in interface{}
5050

5151
// requestBody is a helper extending the Client.request helper, checking the response code
5252
// and decoding the response payload.
53-
func (c Client) requestBody(ctx context.Context, method, path string, in, out interface{}) error {
54-
resp, err := c.request(ctx, method, path, in)
53+
func (c Client) requestBody(ctx context.Context, method, path string, in, out interface{}, opts ...requestOption) error {
54+
resp, err := c.request(ctx, method, path, in, opts...)
5555
if err != nil {
5656
return xerrors.Errorf("Execute request: %q", err)
5757
}

coder-sdk/users.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (c Client) Me(ctx context.Context) (*User, error) {
4949
// UserByID get the details of a user by their id.
5050
func (c Client) UserByID(ctx context.Context, id string) (*User, error) {
5151
var u User
52-
if err := c.requestBody(ctx, http.MethodGet, "/api/private/users/"+id, nil, &u); err != nil {
52+
if err := c.requestBody(ctx, http.MethodGet, "/api/v0/users/"+id, nil, &u); err != nil {
5353
return nil, err
5454
}
5555
return &u, nil

coder-sdk/ws.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,18 @@ import (
1010

1111
type requestOptions struct {
1212
BaseURLOverride *url.URL
13+
Query url.Values
1314
}
1415

1516
type requestOption func(*requestOptions)
1617

18+
// withQueryParams sets the provided query parameters on the request.
19+
func withQueryParams(q url.Values) func(o *requestOptions) {
20+
return func(o *requestOptions) {
21+
o.Query = q
22+
}
23+
}
24+
1725
func withBaseURL(base *url.URL) func(o *requestOptions) {
1826
return func(o *requestOptions) {
1927
o.BaseURLOverride = base
@@ -31,12 +39,6 @@ func (c Client) dialWebsocket(ctx context.Context, path string, options ...reque
3139
if config.BaseURLOverride != nil {
3240
url = *config.BaseURLOverride
3341
}
34-
35-
if url.Scheme == "https" {
36-
url.Scheme = "wss"
37-
} else {
38-
url.Scheme = "ws"
39-
}
4042
url.Path = path
4143

4244
conn, resp, err := websocket.Dial(ctx, url.String(), &websocket.DialOptions{HTTPHeader: http.Header{"Session-Token": {c.Token}}})

internal/cmd/ceapi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func getEnvs(ctx context.Context, client *coder.Client, email string) ([]coder.E
4848
var allEnvs []coder.Environment
4949

5050
for _, org := range orgs {
51-
envs, err := client.EnvironmentsByOrganization(ctx, user.ID, org.ID)
51+
envs, err := client.UserEnvironmentsByOrganization(ctx, user.ID, org.ID)
5252
if err != nil {
5353
return nil, xerrors.Errorf("get envs for %s: %w", org.Name, err)
5454
}

internal/cmd/envs.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ coder envs create my-new-powerful-env --cpu 12 --disk 100 --memory 16 --image ub
197197
createReq := &coder.CreateEnvironmentRequest{
198198
Name: args[0],
199199
ImageID: importedImg.ID,
200+
OrgID: importedImg.OrganizationID,
200201
ImageTag: tag,
201202
CPUCores: cpu,
202203
MemoryGB: memory,
@@ -217,7 +218,7 @@ coder envs create my-new-powerful-env --cpu 12 --disk 100 --memory 16 --image ub
217218
createReq.DiskGB = importedImg.DefaultDiskGB
218219
}
219220

220-
env, err := client.CreateEnvironment(ctx, importedImg.OrganizationID, *createReq)
221+
env, err := client.CreateEnvironment(ctx, *createReq)
221222
if err != nil {
222223
return xerrors.Errorf("create environment: %w", err)
223224
}

0 commit comments

Comments
 (0)