|
| 1 | +package taskstatus |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/url" |
| 7 | + |
| 8 | + "github.com/google/uuid" |
| 9 | + "golang.org/x/xerrors" |
| 10 | + |
| 11 | + "cdr.dev/slog" |
| 12 | + "github.com/coder/coder/v2/codersdk" |
| 13 | + "github.com/coder/coder/v2/codersdk/agentsdk" |
| 14 | +) |
| 15 | + |
| 16 | +// createExternalWorkspaceResult contains the results from creating an external workspace. |
| 17 | +type createExternalWorkspaceResult struct { |
| 18 | + WorkspaceID uuid.UUID |
| 19 | + AgentToken string |
| 20 | +} |
| 21 | + |
| 22 | +// client abstracts the details of using codersdk.Client for workspace operations. |
| 23 | +// This interface allows for easier testing by enabling mock implementations and |
| 24 | +// provides a cleaner separation of concerns. |
| 25 | +// |
| 26 | +// The interface is designed to be initialized in two phases: |
| 27 | +// 1. Create the client with newClient(coderClient) |
| 28 | +// 2. Configure logging when the io.Writer is available in Run() |
| 29 | +type client interface { |
| 30 | + // createExternalWorkspace creates an external workspace and returns the workspace ID |
| 31 | + // and agent token for the first external agent found in the workspace resources. |
| 32 | + createExternalWorkspace(ctx context.Context, req codersdk.CreateWorkspaceRequest) (createExternalWorkspaceResult, error) |
| 33 | + |
| 34 | + // watchWorkspace watches for updates to a workspace. |
| 35 | + watchWorkspace(ctx context.Context, workspaceID uuid.UUID) (<-chan codersdk.Workspace, error) |
| 36 | + |
| 37 | + // initialize sets up the client with the provided logger, which is only available after Run() is called. |
| 38 | + initialize(logger slog.Logger) |
| 39 | +} |
| 40 | + |
| 41 | +// appStatusPatcher abstracts the details of using agentsdk.Client for updating app status. |
| 42 | +// This interface is separate from client because it requires an agent token which is only |
| 43 | +// available after creating an external workspace. |
| 44 | +type appStatusPatcher interface { |
| 45 | + // patchAppStatus updates the status of a workspace app. |
| 46 | + patchAppStatus(ctx context.Context, req agentsdk.PatchAppStatus) error |
| 47 | + |
| 48 | + // initialize sets up the patcher with the provided logger and agent token. |
| 49 | + initialize(logger slog.Logger, agentToken string) |
| 50 | +} |
| 51 | + |
| 52 | +// sdkClient is the concrete implementation of the client interface using |
| 53 | +// codersdk.Client. |
| 54 | +type sdkClient struct { |
| 55 | + coderClient *codersdk.Client |
| 56 | +} |
| 57 | + |
| 58 | +// newClient creates a new client implementation using the provided codersdk.Client. |
| 59 | +func newClient(coderClient *codersdk.Client) client { |
| 60 | + return &sdkClient{ |
| 61 | + coderClient: coderClient, |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func (c *sdkClient) createExternalWorkspace(ctx context.Context, req codersdk.CreateWorkspaceRequest) (createExternalWorkspaceResult, error) { |
| 66 | + // Create the workspace |
| 67 | + workspace, err := c.coderClient.CreateUserWorkspace(ctx, codersdk.Me, req) |
| 68 | + if err != nil { |
| 69 | + return createExternalWorkspaceResult{}, err |
| 70 | + } |
| 71 | + |
| 72 | + // Get the workspace with latest build details |
| 73 | + workspace, err = c.coderClient.WorkspaceByOwnerAndName(ctx, codersdk.Me, workspace.Name, codersdk.WorkspaceOptions{}) |
| 74 | + if err != nil { |
| 75 | + return createExternalWorkspaceResult{}, err |
| 76 | + } |
| 77 | + |
| 78 | + // Find external agents in resources |
| 79 | + for _, resource := range workspace.LatestBuild.Resources { |
| 80 | + if resource.Type != "coder_external_agent" || len(resource.Agents) == 0 { |
| 81 | + continue |
| 82 | + } |
| 83 | + |
| 84 | + // Get credentials for the first agent |
| 85 | + agent := resource.Agents[0] |
| 86 | + credentials, err := c.coderClient.WorkspaceExternalAgentCredentials(ctx, workspace.ID, agent.Name) |
| 87 | + if err != nil { |
| 88 | + return createExternalWorkspaceResult{}, err |
| 89 | + } |
| 90 | + |
| 91 | + return createExternalWorkspaceResult{ |
| 92 | + WorkspaceID: workspace.ID, |
| 93 | + AgentToken: credentials.AgentToken, |
| 94 | + }, nil |
| 95 | + } |
| 96 | + |
| 97 | + return createExternalWorkspaceResult{}, xerrors.Errorf("no external agent found in workspace") |
| 98 | +} |
| 99 | + |
| 100 | +func (c *sdkClient) watchWorkspace(ctx context.Context, workspaceID uuid.UUID) (<-chan codersdk.Workspace, error) { |
| 101 | + return c.coderClient.WatchWorkspace(ctx, workspaceID) |
| 102 | +} |
| 103 | + |
| 104 | +func (c *sdkClient) initialize(logger slog.Logger) { |
| 105 | + // Configure the coder client logging |
| 106 | + c.coderClient.SetLogger(logger) |
| 107 | + c.coderClient.SetLogBodies(true) |
| 108 | +} |
| 109 | + |
| 110 | +// sdkAppStatusPatcher is the concrete implementation of the appStatusPatcher interface |
| 111 | +// using agentsdk.Client. |
| 112 | +type sdkAppStatusPatcher struct { |
| 113 | + agentClient *agentsdk.Client |
| 114 | + url *url.URL |
| 115 | + httpClient *http.Client |
| 116 | +} |
| 117 | + |
| 118 | +// newAppStatusPatcher creates a new appStatusPatcher implementation. |
| 119 | +func newAppStatusPatcher(client *codersdk.Client) appStatusPatcher { |
| 120 | + return &sdkAppStatusPatcher{ |
| 121 | + url: client.URL, |
| 122 | + httpClient: client.HTTPClient, |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +func (p *sdkAppStatusPatcher) patchAppStatus(ctx context.Context, req agentsdk.PatchAppStatus) error { |
| 127 | + if p.agentClient == nil { |
| 128 | + panic("agentClient not initialized - call initialize first") |
| 129 | + } |
| 130 | + return p.agentClient.PatchAppStatus(ctx, req) |
| 131 | +} |
| 132 | + |
| 133 | +func (p *sdkAppStatusPatcher) initialize(logger slog.Logger, agentToken string) { |
| 134 | + // Create and configure the agent client with the provided token |
| 135 | + p.agentClient = agentsdk.New( |
| 136 | + p.url, |
| 137 | + agentsdk.WithFixedToken(agentToken), |
| 138 | + codersdk.WithHTTPClient(p.httpClient), |
| 139 | + codersdk.WithLogger(logger), |
| 140 | + codersdk.WithLogBodies(), |
| 141 | + ) |
| 142 | +} |
| 143 | + |
| 144 | +// Ensure sdkClient implements the client interface. |
| 145 | +var _ client = (*sdkClient)(nil) |
| 146 | + |
| 147 | +// Ensure sdkAppStatusPatcher implements the appStatusPatcher interface. |
| 148 | +var _ appStatusPatcher = (*sdkAppStatusPatcher)(nil) |
0 commit comments