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

Add --scheme flag to devurls and cleanup #196

Merged
merged 2 commits into from
Dec 3, 2020
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
13 changes: 11 additions & 2 deletions coder-sdk/devurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (

// DevURL is the parsed json response record for a devURL from cemanager.
type DevURL struct {
ID string `json:"id" table:"ID"`
ID string `json:"id" table:"-"`
URL string `json:"url" table:"URL"`
Port int `json:"port" table:"Port"`
Access string `json:"access" table:"Access"`
Name string `json:"name" table:"Name"`
Scheme string `json:"scheme" table:"-"`
Scheme string `json:"scheme" table:"Scheme"`
}

type delDevURLRequest struct {
Expand Down Expand Up @@ -45,6 +45,15 @@ func (c Client) CreateDevURL(ctx context.Context, envID string, req CreateDevURL
return c.requestBody(ctx, http.MethodPost, "/api/private/environments/"+envID+"/devurls", req, nil)
}

// DevURLs fetches the Dev URLs for a given environment.
func (c Client) DevURLs(ctx context.Context, envID string) ([]DevURL, error) {
var devurls []DevURL
if err := c.requestBody(ctx, http.MethodGet, "/api/private/environments/"+envID+"/devurls", nil, &devurls); err != nil {
return nil, err
}
return devurls, nil
}

// PutDevURLReq defines the request parameters for overwriting a DevURL.
type PutDevURLReq CreateDevURLReq

Expand Down
3 changes: 2 additions & 1 deletion docs/coder_urls_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Create a new devurl for an environment

```
coder urls create [env_name] [port] [--access <level>] [--name <name>] [flags]
coder urls create [env_name] [port] [flags]
```

### Options
Expand All @@ -12,6 +12,7 @@ coder urls create [env_name] [port] [--access <level>] [--name <name>] [flags]
--access string Set DevURL access to [private | org | authed | public] (default "private")
-h, --help help for create
--name string DevURL name
--scheme string Server scheme (http|https) (default "http")
```

### Options inherited from parent commands
Expand Down
54 changes: 10 additions & 44 deletions internal/cmd/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"regexp"
"strconv"
Expand Down Expand Up @@ -49,15 +48,6 @@ func urlCmd() *cobra.Command {
return cmd
}

// DevURL is the parsed json response record for a devURL from cemanager.
type DevURL struct {
ID string `json:"id" table:"-"`
URL string `json:"url" table:"URL"`
Port int `json:"port" table:"Port"`
Name string `json:"name" table:"-"`
Access string `json:"access" table:"Access"`
}

var urlAccessLevel = map[string]string{
// Remote API endpoint requires these in uppercase.
"PRIVATE": "Only you can access",
Expand Down Expand Up @@ -130,9 +120,10 @@ func createDevURLCmd() *cobra.Command {
var (
access string
urlname string
scheme string
)
cmd := &cobra.Command{
Use: "create [env_name] [port] [--access <level>] [--name <name>]",
Use: "create [env_name] [port]",
Short: "Create a new devurl for an environment",
Aliases: []string{"edit"},
Args: cobra.ExactArgs(2),
Expand Down Expand Up @@ -174,36 +165,37 @@ func createDevURLCmd() *cobra.Command {

urlID, found := devURLID(portNum, urls)
if found {
clog.LogInfo(fmt.Sprintf("updating devurl for port %v", port))
err := client.PutDevURL(ctx, env.ID, urlID, coder.PutDevURLReq{
Port: portNum,
Name: urlname,
Access: access,
EnvID: env.ID,
Scheme: "http",
Scheme: scheme,
})
if err != nil {
return xerrors.Errorf("update DevURL: %w", err)
}
clog.LogSuccess(fmt.Sprintf("patched devurl for port %s", port))
} else {
clog.LogInfo(fmt.Sprintf("Adding devurl for port %v", port))
err := client.CreateDevURL(ctx, env.ID, coder.CreateDevURLReq{
Port: portNum,
Name: urlname,
Access: access,
EnvID: env.ID,
Scheme: "http",
Scheme: scheme,
})
if err != nil {
return xerrors.Errorf("insert DevURL: %w", err)
}
clog.LogSuccess(fmt.Sprintf("created devurl for port %s", port))
}
return nil
},
}

cmd.Flags().StringVar(&access, "access", "private", "Set DevURL access to [private | org | authed | public]")
cmd.Flags().StringVar(&urlname, "name", "", "DevURL name")
cmd.Flags().StringVar(&scheme, "scheme", "http", "Server scheme (http|https)")
_ = cmd.MarkFlagRequired("name")

return cmd
Expand All @@ -217,7 +209,7 @@ var devURLNameValidRx = regexp.MustCompile("^[a-zA-Z][a-zA-Z0-9]{0,63}$")
// devURLID returns the ID of a devURL, given the env name and port
// from a list of DevURL records.
// ("", false) is returned if no match is found.
func devURLID(port int, urls []DevURL) (string, bool) {
func devURLID(port int, urls []coder.DevURL) (string, bool) {
for _, url := range urls {
if url.Port == port {
return url.ID, true
Expand Down Expand Up @@ -267,36 +259,10 @@ func removeDevURL(cmd *cobra.Command, args []string) error {
}

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

reqString := "%s/api/environments/%s/devurls?session_token=%s"
reqURL := fmt.Sprintf(reqString, client.BaseURL, env.ID, client.Token)

req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqURL, nil)
if err != nil {
return nil, err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }() // Best effort.

if resp.StatusCode != http.StatusOK {
return nil, xerrors.Errorf("non-success status code: %d", resp.StatusCode)
}

dec := json.NewDecoder(resp.Body)

var devURLs []DevURL
if err := dec.Decode(&devURLs); err != nil {
return nil, err
}

return devURLs, nil
return client.DevURLs(ctx, env.ID)
}