Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

chore: update API-breaking environment routes #218

Merged
merged 1 commit into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions coder-sdk/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions coder-sdk/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions coder-sdk/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion coder-sdk/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 8 additions & 6 deletions coder-sdk/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}}})
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/ceapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/envs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
Expand Down