|
| 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 | +typecreateExternalWorkspaceResultstruct { |
| 18 | +WorkspaceID uuid.UUID |
| 19 | +AgentTokenstring |
| 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 | +typeclientinterface { |
| 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 | +typeappStatusPatcherinterface { |
| 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,agentTokenstring) |
| 50 | +} |
| 51 | + |
| 52 | +// sdkClient is the concrete implementation of the client interface using |
| 53 | +// codersdk.Client. |
| 54 | +typesdkClientstruct { |
| 55 | +coderClient*codersdk.Client |
| 56 | +} |
| 57 | + |
| 58 | +// newClient creates a new client implementation using the provided codersdk.Client. |
| 59 | +funcnewClient(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 | +iferr!=nil { |
| 69 | +returncreateExternalWorkspaceResult{},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 | +iferr!=nil { |
| 75 | +returncreateExternalWorkspaceResult{},err |
| 76 | +} |
| 77 | + |
| 78 | +// Find external agents in resources |
| 79 | +for_,resource:=rangeworkspace.LatestBuild.Resources { |
| 80 | +ifresource.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 | +iferr!=nil { |
| 88 | +returncreateExternalWorkspaceResult{},err |
| 89 | +} |
| 90 | + |
| 91 | +returncreateExternalWorkspaceResult{ |
| 92 | +WorkspaceID:workspace.ID, |
| 93 | +AgentToken:credentials.AgentToken, |
| 94 | +},nil |
| 95 | +} |
| 96 | + |
| 97 | +returncreateExternalWorkspaceResult{},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 | +returnc.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 | +typesdkAppStatusPatcherstruct { |
| 113 | +agentClient*agentsdk.Client |
| 114 | +url*url.URL |
| 115 | +httpClient*http.Client |
| 116 | +} |
| 117 | + |
| 118 | +// newAppStatusPatcher creates a new appStatusPatcher implementation. |
| 119 | +funcnewAppStatusPatcher(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 | +ifp.agentClient==nil { |
| 128 | +panic("agentClient not initialized - call initialize first") |
| 129 | +} |
| 130 | +returnp.agentClient.PatchAppStatus(ctx,req) |
| 131 | +} |
| 132 | + |
| 133 | +func (p*sdkAppStatusPatcher)initialize(logger slog.Logger,agentTokenstring) { |
| 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) |