findWorkspaceAndAgent finds workspace and agent by name with auto-start support
(ctx context.Context, client *codersdk.Client, workspaceName string)
| 181 | |
| 182 | // findWorkspaceAndAgent finds workspace and agent by name with auto-start support |
| 183 | func findWorkspaceAndAgent(ctx context.Context, client *codersdk.Client, workspaceName string) (codersdk.Workspace, codersdk.WorkspaceAgent, error) { |
| 184 | // Parse workspace name to extract workspace and agent parts |
| 185 | parts := strings.Split(workspaceName, ".") |
| 186 | var agentName string |
| 187 | if len(parts) >= 2 { |
| 188 | agentName = parts[1] |
| 189 | workspaceName = parts[0] |
| 190 | } |
| 191 | |
| 192 | // Get workspace |
| 193 | workspace, err := client.ResolveWorkspace(ctx, workspaceName) |
| 194 | if err != nil { |
| 195 | return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, err |
| 196 | } |
| 197 | |
| 198 | // Auto-start workspace if needed |
| 199 | if workspace.LatestBuild.Transition != codersdk.WorkspaceTransitionStart { |
| 200 | if workspace.LatestBuild.Transition == codersdk.WorkspaceTransitionDelete { |
| 201 | return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, xerrors.Errorf("workspace %q is deleted", workspace.Name) |
| 202 | } |
| 203 | if workspace.LatestBuild.Job.Status == codersdk.ProvisionerJobFailed { |
| 204 | return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, xerrors.Errorf("workspace %q is in failed state", workspace.Name) |
| 205 | } |
| 206 | if workspace.LatestBuild.Status != codersdk.WorkspaceStatusStopped { |
| 207 | return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, xerrors.Errorf("workspace must be started; was unable to autostart as the last build job is %q, expected %q", |
| 208 | workspace.LatestBuild.Status, codersdk.WorkspaceStatusStopped) |
| 209 | } |
| 210 | |
| 211 | // Start workspace |
| 212 | build, err := client.CreateWorkspaceBuild(ctx, workspace.ID, codersdk.CreateWorkspaceBuildRequest{ |
| 213 | Transition: codersdk.WorkspaceTransitionStart, |
| 214 | }) |
| 215 | if err != nil { |
| 216 | return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, xerrors.Errorf("failed to start workspace: %w", err) |
| 217 | } |
| 218 | |
| 219 | // Wait for build to complete |
| 220 | if build.Job.CompletedAt == nil { |
| 221 | err := cliui.WorkspaceBuild(ctx, io.Discard, client, build.ID) |
| 222 | if err != nil { |
| 223 | return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, xerrors.Errorf("failed to wait for build completion: %w", err) |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | // Refresh workspace after build completes |
| 228 | workspace, err = client.Workspace(ctx, workspace.ID) |
| 229 | if err != nil { |
| 230 | return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, err |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | // Find agent |
| 235 | workspaceAgent, err := getWorkspaceAgent(workspace, agentName) |
| 236 | if err != nil { |
| 237 | return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, err |
| 238 | } |
| 239 | |
| 240 | return workspace, workspaceAgent, nil |
no test coverage detected