Skip to content

Commit 2951c2d

Browse files
committed
Move license retrieval to DeploymentInfo, refactor license formatting and catch errors
1 parent 5512cc0 commit 2951c2d

File tree

3 files changed

+44
-37
lines changed

3 files changed

+44
-37
lines changed

cli/support.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cli
33
import (
44
"archive/zip"
55
"bytes"
6+
"context"
67
"encoding/base64"
78
"encoding/json"
89
"fmt"
@@ -13,6 +14,8 @@ import (
1314
"text/tabwriter"
1415
"time"
1516

17+
"github.com/coder/coder/v2/cli/cliutil"
18+
1619
"github.com/google/uuid"
1720
"golang.org/x/xerrors"
1821

@@ -303,6 +306,11 @@ func writeBundle(src *support.Bundle, dest *zip.Writer) error {
303306
return xerrors.Errorf("decode template zip from base64")
304307
}
305308

309+
licenseStatus, err := humanizeLicenses(src.Deployment.Licenses)
310+
if err != nil {
311+
return xerrors.Errorf("format license status: %w", err)
312+
}
313+
306314
// The below we just write as we have them:
307315
for k, v := range map[string]string{
308316
"agent/logs.txt": string(src.Agent.Logs),
@@ -316,7 +324,7 @@ func writeBundle(src *support.Bundle, dest *zip.Writer) error {
316324
"network/tailnet_debug.html": src.Network.TailnetDebug,
317325
"workspace/build_logs.txt": humanizeBuildLogs(src.Workspace.BuildLogs),
318326
"workspace/template_file.zip": string(templateVersionBytes),
319-
"license-status.txt": src.LicenseStatus,
327+
"license-status.txt": licenseStatus,
320328
} {
321329
f, err := dest.Create(k)
322330
if err != nil {
@@ -361,3 +369,15 @@ func humanizeBuildLogs(ls []codersdk.ProvisionerJobLog) string {
361369
_ = tw.Flush()
362370
return buf.String()
363371
}
372+
373+
func humanizeLicenses(licenses []codersdk.License) (string, error) {
374+
formatter := cliutil.NewLicenseFormatter(cliutil.LicenseFormatterOpts{
375+
Sanitize: true,
376+
})
377+
378+
if len(licenses) == 0 {
379+
return "No licenses found", nil
380+
}
381+
382+
return formatter.Format(context.Background(), licenses)
383+
}

support/support.go

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"net/http/httptest"
1111
"strings"
1212

13-
"github.com/coder/coder/v2/cli/cliutil"
14-
1513
"github.com/google/uuid"
1614
"golang.org/x/sync/errgroup"
1715
"golang.org/x/xerrors"
@@ -32,20 +30,20 @@ import (
3230
// Even though we do attempt to sanitize data, it may still contain
3331
// sensitive information and should thus be treated as secret.
3432
type Bundle struct {
35-
Deployment Deployment `json:"deployment"`
36-
Network Network `json:"network"`
37-
Workspace Workspace `json:"workspace"`
38-
Agent Agent `json:"agent"`
39-
LicenseStatus string
40-
Logs []string `json:"logs"`
41-
CLILogs []byte `json:"cli_logs"`
33+
Deployment Deployment `json:"deployment"`
34+
Network Network `json:"network"`
35+
Workspace Workspace `json:"workspace"`
36+
Agent Agent `json:"agent"`
37+
Logs []string `json:"logs"`
38+
CLILogs []byte `json:"cli_logs"`
4239
}
4340

4441
type Deployment struct {
4542
BuildInfo *codersdk.BuildInfoResponse `json:"build"`
4643
Config *codersdk.DeploymentConfig `json:"config"`
4744
Experiments codersdk.Experiments `json:"experiments"`
4845
HealthReport *healthsdk.HealthcheckReport `json:"health_report"`
46+
Licenses []codersdk.License `json:"licenses"`
4947
}
5048

5149
type Network struct {
@@ -141,6 +139,21 @@ func DeploymentInfo(ctx context.Context, client *codersdk.Client, log slog.Logge
141139
return nil
142140
})
143141

142+
eg.Go(func() error {
143+
licenses, err := client.Licenses(ctx)
144+
if err != nil {
145+
// Ignore 404 because AGPL doesn't have this endpoint
146+
if cerr, ok := codersdk.AsError(err); ok && cerr.StatusCode() != http.StatusNotFound {
147+
return xerrors.Errorf("fetch license status: %w", err)
148+
}
149+
}
150+
if licenses == nil {
151+
licenses = make([]codersdk.License, 0)
152+
}
153+
d.Licenses = licenses
154+
return nil
155+
})
156+
144157
if err := eg.Wait(); err != nil {
145158
log.Error(ctx, "fetch deployment information", slog.Error(err))
146159
}
@@ -354,27 +367,6 @@ func AgentInfo(ctx context.Context, client *codersdk.Client, log slog.Logger, ag
354367
return a
355368
}
356369

357-
func LicenseStatus(ctx context.Context, client *codersdk.Client, log slog.Logger) string {
358-
licenses, err := client.Licenses(ctx)
359-
if err != nil {
360-
log.Warn(ctx, "fetch licenses", slog.Error(err))
361-
return "No licenses found"
362-
}
363-
// Ensure that we print "[]" instead of "null" when there are no licenses.
364-
if licenses == nil {
365-
licenses = make([]codersdk.License, 0)
366-
}
367-
368-
formatter := cliutil.NewLicenseFormatter(cliutil.LicenseFormatterOpts{
369-
Sanitize: true,
370-
})
371-
out, err := formatter.Format(ctx, licenses)
372-
if err != nil {
373-
log.Error(ctx, "format licenses", slog.Error(err))
374-
}
375-
return out
376-
}
377-
378370
func connectedAgentInfo(ctx context.Context, client *codersdk.Client, log slog.Logger, agentID uuid.UUID, eg *errgroup.Group, a *Agent) (closer func()) {
379371
conn, err := workspacesdk.New(client).
380372
DialAgent(ctx, agentID, &workspacesdk.DialAgentOptions{
@@ -534,11 +526,6 @@ func Run(ctx context.Context, d *Deps) (*Bundle, error) {
534526
b.Agent = ai
535527
return nil
536528
})
537-
eg.Go(func() error {
538-
ls := LicenseStatus(ctx, d.Client, d.Log)
539-
b.LicenseStatus = ls
540-
return nil
541-
})
542529

543530
_ = eg.Wait()
544531

support/support_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ func TestRun(t *testing.T) {
6262
assertSanitizedDeploymentConfig(t, bun.Deployment.Config)
6363
assertNotNilNotEmpty(t, bun.Deployment.HealthReport, "deployment health report should be present")
6464
assertNotNilNotEmpty(t, bun.Deployment.Experiments, "deployment experiments should be present")
65+
require.NotNil(t, bun.Deployment.Licenses, "license status should be present")
6566
assertNotNilNotEmpty(t, bun.Network.ConnectionInfo, "agent connection info should be present")
6667
assertNotNilNotEmpty(t, bun.Network.CoordinatorDebug, "network coordinator debug should be present")
6768
assertNotNilNotEmpty(t, bun.Network.Netcheck, "network netcheck should be present")
@@ -87,7 +88,6 @@ func TestRun(t *testing.T) {
8788
assertNotNilNotEmpty(t, bun.Agent.Prometheus, "agent prometheus metrics should be present")
8889
assertNotNilNotEmpty(t, bun.Agent.StartupLogs, "agent startup logs should be present")
8990
assertNotNilNotEmpty(t, bun.Logs, "bundle logs should be present")
90-
assertNotNilNotEmpty(t, bun.LicenseStatus, "license status should be present")
9191
})
9292

9393
t.Run("OK_NoWorkspace", func(t *testing.T) {

0 commit comments

Comments
 (0)