Skip to content

Commit 434c4be

Browse files
authored
chore: Add listing proxies to cli 'coder proxy ls' (#7376)
* feat: Add listing proxies to cli 'coder proxy ls' * Add unit test * Ignore errors * Make gen and update golden files * Update golden files
1 parent 90c57a5 commit 434c4be

File tree

6 files changed

+89
-5
lines changed

6 files changed

+89
-5
lines changed

coderd/apidoc/docs.go

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

codersdk/workspaceproxy.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const (
2929
)
3030

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

6666
type CreateWorkspaceProxyRequest struct {
67-
Name string `json:"name"`
67+
Name string `json:"name" validate:"required"`
6868
DisplayName string `json:"display_name"`
6969
Icon string `json:"icon"`
7070
}

docs/api/schemas.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1615,7 +1615,7 @@ CreateParameterRequest is a structure used to create a new parameter value for a
16151615
| -------------- | ------ | -------- | ------------ | ----------- |
16161616
| `display_name` | string | false | | |
16171617
| `icon` | string | false | | |
1618-
| `name` | string | false | | |
1618+
| `name` | string | true | | |
16191619

16201620
## codersdk.CreateWorkspaceRequest
16211621

enterprise/cli/workspaceproxy.go

+66-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package cli
22

33
import (
44
"fmt"
5+
"strings"
56

7+
"github.com/fatih/color"
68
"golang.org/x/xerrors"
79

810
"github.com/coder/coder/cli/clibase"
@@ -23,6 +25,7 @@ func (r *RootCmd) workspaceProxy() *clibase.Cmd {
2325
r.proxyServer(),
2426
r.createProxy(),
2527
r.deleteProxy(),
28+
r.listProxies(),
2629
},
2730
}
2831

@@ -66,7 +69,8 @@ func (r *RootCmd) createProxy() *clibase.Cmd {
6669
if !ok {
6770
return nil, xerrors.Errorf("unexpected type %T", data)
6871
}
69-
return fmt.Sprintf("Workspace Proxy %q registered successfully\nToken: %s", response.Proxy.Name, response.ProxyToken), nil
72+
return fmt.Sprintf("Workspace Proxy %q created successfully. Save this token, it will not be shown again."+
73+
"\nToken: %s", response.Proxy.Name, response.ProxyToken), nil
7074
}),
7175
cliui.JSONFormat(),
7276
// Table formatter expects a slice, make a slice of one.
@@ -91,6 +95,10 @@ func (r *RootCmd) createProxy() *clibase.Cmd {
9195
),
9296
Handler: func(inv *clibase.Invocation) error {
9397
ctx := inv.Context()
98+
if proxyName == "" {
99+
return xerrors.Errorf("proxy name is required")
100+
}
101+
94102
resp, err := client.CreateWorkspaceProxy(ctx, codersdk.CreateWorkspaceProxyRequest{
95103
Name: proxyName,
96104
DisplayName: displayName,
@@ -140,3 +148,60 @@ func (r *RootCmd) createProxy() *clibase.Cmd {
140148
)
141149
return cmd
142150
}
151+
152+
func (r *RootCmd) listProxies() *clibase.Cmd {
153+
formatter := cliui.NewOutputFormatter(
154+
cliui.TableFormat([]codersdk.WorkspaceProxy{}, []string{"name", "url", "status status"}),
155+
cliui.JSONFormat(),
156+
cliui.ChangeFormatterData(cliui.TextFormat(), func(data any) (any, error) {
157+
resp, ok := data.([]codersdk.WorkspaceProxy)
158+
if !ok {
159+
return nil, xerrors.Errorf("unexpected type %T", data)
160+
}
161+
var str strings.Builder
162+
_, _ = str.WriteString("Workspace Proxies:\n")
163+
sep := ""
164+
for i, proxy := range resp {
165+
_, _ = str.WriteString(sep)
166+
_, _ = str.WriteString(fmt.Sprintf("%d: %s %s %s", i, proxy.Name, proxy.URL, proxy.Status.Status))
167+
for _, errMsg := range proxy.Status.Report.Errors {
168+
_, _ = str.WriteString(color.RedString("\n\tErr: %s", errMsg))
169+
}
170+
for _, warnMsg := range proxy.Status.Report.Errors {
171+
_, _ = str.WriteString(color.YellowString("\n\tWarn: %s", warnMsg))
172+
}
173+
sep = "\n"
174+
}
175+
return str.String(), nil
176+
}),
177+
)
178+
179+
client := new(codersdk.Client)
180+
cmd := &clibase.Cmd{
181+
Use: "ls",
182+
Aliases: []string{"list"},
183+
Short: "List all workspace proxies",
184+
Middleware: clibase.Chain(
185+
clibase.RequireNArgs(0),
186+
r.InitClient(client),
187+
),
188+
Handler: func(inv *clibase.Invocation) error {
189+
ctx := inv.Context()
190+
proxies, err := client.WorkspaceProxies(ctx)
191+
if err != nil {
192+
return xerrors.Errorf("list workspace proxies: %w", err)
193+
}
194+
195+
output, err := formatter.Format(ctx, proxies)
196+
if err != nil {
197+
return err
198+
}
199+
200+
_, err = fmt.Fprintln(inv.Stdout, output)
201+
return err
202+
},
203+
}
204+
205+
formatter.AttachOptions(&cmd.Options)
206+
return cmd
207+
}

enterprise/cli/workspaceproxy_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,21 @@ func Test_ProxyCRUD(t *testing.T) {
6565
_, err = uuid.Parse(parts[0])
6666
require.NoError(t, err, "expected token to be a uuid")
6767

68+
// Fetch proxies and check output
69+
inv, conf = newCLI(
70+
t,
71+
"proxy", "ls",
72+
)
73+
74+
pty = ptytest.New(t)
75+
inv.Stdout = pty.Output()
76+
clitest.SetupConfig(t, client, conf)
77+
78+
err = inv.WithContext(ctx).Run()
79+
require.NoError(t, err)
80+
pty.ExpectMatch(expectedName)
81+
82+
// Also check via the api
6883
proxies, err := client.WorkspaceProxies(ctx)
6984
require.NoError(t, err, "failed to get workspace proxies")
7085
require.Len(t, proxies, 1, "expected 1 proxy")

0 commit comments

Comments
 (0)