This repository was archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Update API request, print mult. envs in tabs, handle non 200 response #56
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
96cc93b
Update API request, print mult. envs in tabs, handle non 200 response
anthonyshull ee6d3c0
Change command to `urls`
anthonyshull 9294633
Improve rsync trouble error messages
6450687
Merge remote-tracking branch 'origin/master' into devurl-CRUD-ops
4f09af1
Add subcommands 'create' and 'del' to manage devurls
4c1d110
Merge pull request #60 from cdr/devurl-CRUD-ops
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"text/tabwriter" | ||
|
||
"github.com/spf13/pflag" | ||
|
||
"go.coder.com/cli" | ||
"go.coder.com/flog" | ||
) | ||
|
||
type urlsCmd struct{} | ||
|
||
// DevURL is the parsed json response record for a devURL from cemanager | ||
type DevURL struct { | ||
ID string `json:"id"` | ||
URL string `json:"url"` | ||
Port string `json:"port"` | ||
Access string `json:"access"` | ||
} | ||
|
||
var urlAccessLevel = map[string]string{ | ||
//Remote API endpoint requires these in uppercase | ||
"PRIVATE": "Only you can access", | ||
"ORG": "All members of your organization can access", | ||
"AUTHED": "Authenticated users can access", | ||
"PUBLIC": "Anyone on the internet can access this link", | ||
} | ||
|
||
func portIsValid(port string) bool { | ||
p, err := strconv.ParseUint(port, 10, 16) | ||
if p < 1 { | ||
// port 0 means 'any free port', which we don't support | ||
err = strconv.ErrRange | ||
} | ||
if err != nil { | ||
fmt.Println("Invalid port") | ||
} | ||
return err == nil | ||
} | ||
|
||
func accessLevelIsValid(level string) bool { | ||
_, ok := urlAccessLevel[level] | ||
if !ok { | ||
fmt.Println("Invalid access level") | ||
} | ||
return ok | ||
} | ||
|
||
type createSubCmd struct { | ||
access string | ||
} | ||
|
||
func (sub *createSubCmd) RegisterFlags(fl *pflag.FlagSet) { | ||
fl.StringVarP(&sub.access, "access", "a", "private", "[private | org | authed | public] set devurl access") | ||
} | ||
|
||
func (sub createSubCmd) Spec() cli.CommandSpec { | ||
return cli.CommandSpec{ | ||
Name: "create", | ||
Usage: "<env name> <port> [--access <level>]", | ||
Desc: "create/update a devurl for external access", | ||
} | ||
} | ||
|
||
// Run creates or updates a devURL, specified by env ID and port | ||
// (fl.Arg(0) and fl.Arg(1)), with access level (fl.Arg(2)) on | ||
// the cemanager. | ||
func (sub createSubCmd) Run(fl *pflag.FlagSet) { | ||
envName := fl.Arg(0) | ||
port := fl.Arg(1) | ||
access := fl.Arg(2) | ||
|
||
if envName == "" { | ||
exitUsage(fl) | ||
} | ||
|
||
if !portIsValid(port) { | ||
exitUsage(fl) | ||
} | ||
|
||
access = strings.ToUpper(sub.access) | ||
if !accessLevelIsValid(access) { | ||
exitUsage(fl) | ||
} | ||
|
||
entClient := requireAuth() | ||
|
||
env := findEnv(entClient, envName) | ||
|
||
_, found := devURLID(port, urlList(envName)) | ||
if found { | ||
fmt.Printf("Updating devurl for port %v\n", port) | ||
} else { | ||
fmt.Printf("Adding devurl for port %v\n", port) | ||
} | ||
|
||
err := entClient.UpsertDevURL(env.ID, port, access) | ||
if err != nil { | ||
flog.Error("upsert devurl: %s", err.Error()) | ||
} | ||
} | ||
|
||
type delSubCmd struct{} | ||
|
||
func (sub delSubCmd) Spec() cli.CommandSpec { | ||
return cli.CommandSpec{ | ||
Name: "del", | ||
Usage: "<env name> <port>", | ||
Desc: "delete a devurl", | ||
} | ||
} | ||
|
||
// devURLID returns the ID of a devURL, given the env name and port. | ||
// ("", false) is returned if no match is found. | ||
func devURLID(port string, urls []DevURL) (string, bool) { | ||
for _, url := range urls { | ||
if url.Port == port { | ||
return url.ID, true | ||
} | ||
} | ||
return "", false | ||
} | ||
|
||
// Run deletes a devURL, specified by env ID and port, from the cemanager. | ||
func (sub delSubCmd) Run(fl *pflag.FlagSet) { | ||
envName := fl.Arg(0) | ||
port := fl.Arg(1) | ||
|
||
if envName == "" { | ||
exitUsage(fl) | ||
} | ||
|
||
if !portIsValid(port) { | ||
exitUsage(fl) | ||
} | ||
|
||
entClient := requireAuth() | ||
|
||
env := findEnv(entClient, envName) | ||
|
||
urlID, found := devURLID(port, urlList(envName)) | ||
if found { | ||
fmt.Printf("Deleting devurl for port %v\n", port) | ||
} else { | ||
flog.Fatal("No devurl found for port %v", port) | ||
} | ||
|
||
err := entClient.DelDevURL(env.ID, urlID) | ||
if err != nil { | ||
flog.Error("delete devurl: %s", err.Error()) | ||
} | ||
} | ||
|
||
func (cmd urlsCmd) Spec() cli.CommandSpec { | ||
return cli.CommandSpec{ | ||
Name: "urls", | ||
Usage: "<env name>", | ||
Desc: "get all development urls for external access", | ||
} | ||
} | ||
|
||
// urlList returns the list of active devURLs from the cemanager. | ||
func urlList(envName string) []DevURL { | ||
entClient := requireAuth() | ||
env := findEnv(entClient, envName) | ||
|
||
reqString := "%s/api/environments/%s/devurls?session_token=%s" | ||
reqURL := fmt.Sprintf(reqString, entClient.BaseURL, env.ID, entClient.Token) | ||
|
||
resp, err := http.Get(reqURL) | ||
if err != nil { | ||
flog.Fatal("%v", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != 200 { | ||
flog.Fatal("non-success status code: %d", resp.StatusCode) | ||
} | ||
|
||
dec := json.NewDecoder(resp.Body) | ||
|
||
devURLs := make([]DevURL, 0) | ||
err = dec.Decode(&devURLs) | ||
if err != nil { | ||
flog.Fatal("%v", err) | ||
} | ||
|
||
if len(devURLs) == 0 { | ||
fmt.Printf("no dev urls were found for environment: %s\n", envName) | ||
} | ||
|
||
return devURLs | ||
} | ||
|
||
// Run gets the list of active devURLs from the cemanager for the | ||
// specified environment and outputs info to stdout. | ||
func (cmd urlsCmd) Run(fl *pflag.FlagSet) { | ||
envName := fl.Arg(0) | ||
devURLs := urlList(envName) | ||
|
||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.TabIndent) | ||
for _, devURL := range devURLs { | ||
fmt.Fprintf(w, "%s\t%s\t%s\n", devURL.URL, devURL.Port, devURL.Access) | ||
} | ||
w.Flush() | ||
} | ||
|
||
func (cmd *urlsCmd) Subcommands() []cli.Command { | ||
return []cli.Command{ | ||
&createSubCmd{}, | ||
&delSubCmd{}, | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package entclient | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
func (c Client) DelDevURL(envID, urlID string) error { | ||
reqString := "/api/environments/%s/devurls/%s" | ||
reqUrl := fmt.Sprintf(reqString, envID, urlID) | ||
|
||
res, err := c.request("DELETE", reqUrl, map[string]string{ | ||
"environment_id": envID, | ||
"url_id": urlID, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if res.StatusCode != http.StatusOK { | ||
return bodyError(res) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c Client) UpsertDevURL(envID, port, access string) error { | ||
reqString := "/api/environments/%s/devurls" | ||
reqUrl := fmt.Sprintf(reqString, envID) | ||
|
||
res, err := c.request("POST", reqUrl, map[string]string{ | ||
"environment_id": envID, | ||
"port": port, | ||
"access": access, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if res.StatusCode != http.StatusOK { | ||
return bodyError(res) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.