Skip to content

chore: Add listing proxies to cli 'coder proxy ls' #7376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 3, 2023
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
3 changes: 3 additions & 0 deletions coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions codersdk/workspaceproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const (
)

type WorkspaceProxyStatus struct {
Status ProxyHealthStatus `json:"status" table:"status"`
Status ProxyHealthStatus `json:"status" table:"status,default_sort"`
// Report provides more information about the health of the workspace proxy.
Report ProxyHealthReport `json:"report,omitempty" table:"report"`
CheckedAt time.Time `json:"checked_at" table:"checked_at" format:"date-time"`
Expand Down Expand Up @@ -60,11 +60,11 @@ type WorkspaceProxy struct {
// Status is the latest status check of the proxy. This will be empty for deleted
// proxies. This value can be used to determine if a workspace proxy is healthy
// and ready to use.
Status WorkspaceProxyStatus `json:"status,omitempty" table:"status"`
Status WorkspaceProxyStatus `json:"status,omitempty" table:"status,recursive"`
}

type CreateWorkspaceProxyRequest struct {
Name string `json:"name"`
Name string `json:"name" validate:"required"`
DisplayName string `json:"display_name"`
Icon string `json:"icon"`
}
Expand Down
2 changes: 1 addition & 1 deletion docs/api/schemas.md
Original file line number Diff line number Diff line change
Expand Up @@ -1615,7 +1615,7 @@ CreateParameterRequest is a structure used to create a new parameter value for a
| -------------- | ------ | -------- | ------------ | ----------- |
| `display_name` | string | false | | |
| `icon` | string | false | | |
| `name` | string | false | | |
| `name` | string | true | | |

## codersdk.CreateWorkspaceRequest

Expand Down
67 changes: 66 additions & 1 deletion enterprise/cli/workspaceproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package cli

import (
"fmt"
"strings"

"github.com/fatih/color"
"golang.org/x/xerrors"

"github.com/coder/coder/cli/clibase"
Expand All @@ -23,6 +25,7 @@ func (r *RootCmd) workspaceProxy() *clibase.Cmd {
r.proxyServer(),
r.createProxy(),
r.deleteProxy(),
r.listProxies(),
},
}

Expand Down Expand Up @@ -66,7 +69,8 @@ func (r *RootCmd) createProxy() *clibase.Cmd {
if !ok {
return nil, xerrors.Errorf("unexpected type %T", data)
}
return fmt.Sprintf("Workspace Proxy %q registered successfully\nToken: %s", response.Proxy.Name, response.ProxyToken), nil
return fmt.Sprintf("Workspace Proxy %q created successfully. Save this token, it will not be shown again."+
"\nToken: %s", response.Proxy.Name, response.ProxyToken), nil
}),
cliui.JSONFormat(),
// Table formatter expects a slice, make a slice of one.
Expand All @@ -91,6 +95,10 @@ func (r *RootCmd) createProxy() *clibase.Cmd {
),
Handler: func(inv *clibase.Invocation) error {
ctx := inv.Context()
if proxyName == "" {
return xerrors.Errorf("proxy name is required")
}

resp, err := client.CreateWorkspaceProxy(ctx, codersdk.CreateWorkspaceProxyRequest{
Name: proxyName,
DisplayName: displayName,
Expand Down Expand Up @@ -140,3 +148,60 @@ func (r *RootCmd) createProxy() *clibase.Cmd {
)
return cmd
}

func (r *RootCmd) listProxies() *clibase.Cmd {
formatter := cliui.NewOutputFormatter(
cliui.TableFormat([]codersdk.WorkspaceProxy{}, []string{"name", "url", "status status"}),
cliui.JSONFormat(),
cliui.ChangeFormatterData(cliui.TextFormat(), func(data any) (any, error) {
resp, ok := data.([]codersdk.WorkspaceProxy)
if !ok {
return nil, xerrors.Errorf("unexpected type %T", data)
}
var str strings.Builder
_, _ = str.WriteString("Workspace Proxies:\n")
sep := ""
for i, proxy := range resp {
_, _ = str.WriteString(sep)
_, _ = str.WriteString(fmt.Sprintf("%d: %s %s %s", i, proxy.Name, proxy.URL, proxy.Status.Status))
for _, errMsg := range proxy.Status.Report.Errors {
_, _ = str.WriteString(color.RedString("\n\tErr: %s", errMsg))
}
for _, warnMsg := range proxy.Status.Report.Errors {
_, _ = str.WriteString(color.YellowString("\n\tWarn: %s", warnMsg))
}
sep = "\n"
}
return str.String(), nil
}),
)

client := new(codersdk.Client)
cmd := &clibase.Cmd{
Use: "ls",
Aliases: []string{"list"},
Short: "List all workspace proxies",
Middleware: clibase.Chain(
clibase.RequireNArgs(0),
r.InitClient(client),
),
Handler: func(inv *clibase.Invocation) error {
ctx := inv.Context()
proxies, err := client.WorkspaceProxies(ctx)
if err != nil {
return xerrors.Errorf("list workspace proxies: %w", err)
}

output, err := formatter.Format(ctx, proxies)
if err != nil {
return err
}

_, err = fmt.Fprintln(inv.Stdout, output)
return err
},
}

formatter.AttachOptions(&cmd.Options)
return cmd
}
15 changes: 15 additions & 0 deletions enterprise/cli/workspaceproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ func Test_ProxyCRUD(t *testing.T) {
_, err = uuid.Parse(parts[0])
require.NoError(t, err, "expected token to be a uuid")

// Fetch proxies and check output
inv, conf = newCLI(
t,
"proxy", "ls",
)

pty = ptytest.New(t)
inv.Stdout = pty.Output()
clitest.SetupConfig(t, client, conf)

err = inv.WithContext(ctx).Run()
require.NoError(t, err)
pty.ExpectMatch(expectedName)

// Also check via the api
proxies, err := client.WorkspaceProxies(ctx)
require.NoError(t, err, "failed to get workspace proxies")
require.Len(t, proxies, 1, "expected 1 proxy")
Expand Down