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

Cleanup #179

Merged
merged 6 commits into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fixup! Cleanup
  • Loading branch information
cmoog committed Nov 5, 2020
commit 55802062237a9a2127cbeab3f934b92c573d338e
70 changes: 14 additions & 56 deletions coder-sdk/devurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,75 +20,33 @@ type delDevURLRequest struct {
DevURLID string `json:"url_id"`
}

// DelDevURL deletes the specified devurl.
func (c Client) DelDevURL(ctx context.Context, envID, urlID string) error {
// DeleteDevURL deletes the specified devurl.
func (c Client) DeleteDevURL(ctx context.Context, envID, urlID string) error {
reqURL := fmt.Sprintf("/api/environments/%s/devurls/%s", envID, urlID)

resp, err := c.request(ctx, http.MethodDelete, reqURL, delDevURLRequest{
return c.requestBody(ctx, http.MethodDelete, reqURL, delDevURLRequest{
EnvID: envID,
DevURLID: urlID,
})
if err != nil {
return err
}
defer func() { _ = resp.Body.Close() }() // Best effort. Likely connection drop.

if resp.StatusCode != http.StatusOK {
return bodyError(resp)
}

return nil
}, nil)
}

type createDevURLRequest struct {
// CreateDevURLReq defines the request parameters for creating a new DevURL.
type CreateDevURLReq struct {
EnvID string `json:"environment_id"`
Port int `json:"port"`
Access string `json:"access"`
Name string `json:"name"`
}

// InsertDevURL inserts a new devurl for the authenticated user.
func (c Client) InsertDevURL(ctx context.Context, envID string, port int, name, access string) error {
reqURL := fmt.Sprintf("/api/environments/%s/devurls", envID)

resp, err := c.request(ctx, http.MethodPost, reqURL, createDevURLRequest{
EnvID: envID,
Port: port,
Access: access,
Name: name,
})
if err != nil {
return err
}
defer func() { _ = resp.Body.Close() }() // Best effort. Likely connection drop.

if resp.StatusCode != http.StatusOK {
return bodyError(resp)
}

return nil
// CreateDevURL inserts a new devurl for the authenticated user.
func (c Client) CreateDevURL(ctx context.Context, envID string, req CreateDevURLReq) error {
return c.requestBody(ctx, http.MethodPost, "/api/environments/"+envID+"/devurls", req, nil)
}

type updateDevURLRequest createDevURLRequest

// UpdateDevURL updates an existing devurl for the authenticated user.
func (c Client) UpdateDevURL(ctx context.Context, envID, urlID string, port int, name, access string) error {
reqURL := fmt.Sprintf("/api/environments/%s/devurls/%s", envID, urlID)

resp, err := c.request(ctx, http.MethodPut, reqURL, updateDevURLRequest{
EnvID: envID,
Port: port,
Access: access,
Name: name,
})
if err != nil {
return err
}
defer func() { _ = resp.Body.Close() }() // Best effort. Likefly connection drop.

if resp.StatusCode != http.StatusOK {
return bodyError(resp)
}
// PutDevURLReq defines the request parameters for overwriting a DevURL.
type PutDevURLReq CreateDevURLReq

return nil
// PutDevURL updates an existing devurl for the authenticated user.
func (c Client) PutDevURL(ctx context.Context, envID, urlID string, req PutDevURLReq) error {
return c.requestBody(ctx, http.MethodPut, "/api/environments/"+envID+"/devurls/"+urlID, req, nil)
}
33 changes: 22 additions & 11 deletions internal/cmd/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,13 @@ func accessLevelIsValid(level string) bool {
func listDevURLsCmd(outputFmt *string) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
client, err := newClient(ctx)
if err != nil {
return err
}
envName := args[0]
devURLs, err := urlList(ctx, envName)

devURLs, err := urlList(ctx, client, envName)
if err != nil {
return err
}
Expand Down Expand Up @@ -162,21 +167,31 @@ func createDevURLCmd() *cobra.Command {
return err
}

urls, err := urlList(ctx, envName)
urls, err := urlList(ctx, client, envName)
if err != nil {
return err
}

urlID, found := devURLID(portNum, urls)
if found {
clog.LogInfo(fmt.Sprintf("updating devurl for port %v", port))
err := client.UpdateDevURL(ctx, env.ID, urlID, portNum, urlname, access)
err := client.PutDevURL(ctx, env.ID, urlID, coder.PutDevURLReq{
Port: portNum,
Name: urlname,
Access: access,
EnvID: env.ID,
})
if err != nil {
return xerrors.Errorf("update DevURL: %w", err)
}
} else {
clog.LogInfo(fmt.Sprintf("Adding devurl for port %v", port))
err := client.InsertDevURL(ctx, env.ID, portNum, urlname, access)
err := client.CreateDevURL(ctx, env.ID, coder.CreateDevURLReq{
Port: portNum,
Name: urlname,
Access: access,
EnvID: env.ID,
})
if err != nil {
return xerrors.Errorf("insert DevURL: %w", err)
}
Expand Down Expand Up @@ -231,7 +246,7 @@ func removeDevURL(cmd *cobra.Command, args []string) error {
return err
}

urls, err := urlList(ctx, envName)
urls, err := urlList(ctx, client, envName)
if err != nil {
return err
}
Expand All @@ -243,18 +258,14 @@ func removeDevURL(cmd *cobra.Command, args []string) error {
return xerrors.Errorf("No devurl found for port %v", port)
}

if err := client.DelDevURL(ctx, env.ID, urlID); err != nil {
if err := client.DeleteDevURL(ctx, env.ID, urlID); err != nil {
return xerrors.Errorf("delete DevURL: %w", err)
}
return nil
}

// urlList returns the list of active devURLs from the cemanager.
func urlList(ctx context.Context, envName string) ([]DevURL, error) {
client, err := newClient(ctx)
if err != nil {
return nil, err
}
func urlList(ctx context.Context, client *coder.Client, envName string) ([]DevURL, error) {
env, err := findEnv(ctx, client, envName, coder.Me)
if err != nil {
return nil, err
Expand Down