From 3bc1e5cc8058e6f0a170942b3e1dda3689b403ee Mon Sep 17 00:00:00 2001 From: Charlie Moog Date: Thu, 3 Dec 2020 11:17:21 -0600 Subject: [PATCH 1/2] Add --scheme flag to devurls and cleanup --- coder-sdk/devurl.go | 13 +++++++++-- internal/cmd/urls.go | 54 ++++++++------------------------------------ 2 files changed, 21 insertions(+), 46 deletions(-) diff --git a/coder-sdk/devurl.go b/coder-sdk/devurl.go index 9b1e0b36..331d46e7 100644 --- a/coder-sdk/devurl.go +++ b/coder-sdk/devurl.go @@ -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 { @@ -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 diff --git a/internal/cmd/urls.go b/internal/cmd/urls.go index ca93ea01..b4cd60f1 100644 --- a/internal/cmd/urls.go +++ b/internal/cmd/urls.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "fmt" - "net/http" "os" "regexp" "strconv" @@ -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", @@ -130,9 +120,10 @@ func createDevURLCmd() *cobra.Command { var ( access string urlname string + scheme string ) cmd := &cobra.Command{ - Use: "create [env_name] [port] [--access ] [--name ]", + Use: "create [env_name] [port]", Short: "Create a new devurl for an environment", Aliases: []string{"edit"}, Args: cobra.ExactArgs(2), @@ -174,29 +165,29 @@ 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 }, @@ -204,6 +195,7 @@ func createDevURLCmd() *cobra.Command { 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 @@ -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 @@ -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) } From 470d158be92ef65f12ca3cf44e625a6ae8ec831e Mon Sep 17 00:00:00 2001 From: Charlie Moog Date: Thu, 3 Dec 2020 11:19:49 -0600 Subject: [PATCH 2/2] fixup! Add --scheme flag to devurls and cleanup --- docs/coder_urls_create.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/coder_urls_create.md b/docs/coder_urls_create.md index 6d293b89..ac60824d 100644 --- a/docs/coder_urls_create.md +++ b/docs/coder_urls_create.md @@ -3,7 +3,7 @@ Create a new devurl for an environment ``` -coder urls create [env_name] [port] [--access ] [--name ] [flags] +coder urls create [env_name] [port] [flags] ``` ### Options @@ -12,6 +12,7 @@ coder urls create [env_name] [port] [--access ] [--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