getTargetedWorkspaces retrieves the workspaces based on the template filter and target range. warnWriter is where to write a warning message if any workspaces were skipped due to ownership mismatch.
(ctx context.Context, client *codersdk.Client, organizationIDs []uuid.UUID, warnWriter io.Writer)
| 427 | // getTargetedWorkspaces retrieves the workspaces based on the template filter and target range. warnWriter is where to |
| 428 | // write a warning message if any workspaces were skipped due to ownership mismatch. |
| 429 | func (f *workspaceTargetFlags) getTargetedWorkspaces(ctx context.Context, client *codersdk.Client, organizationIDs []uuid.UUID, warnWriter io.Writer) ([]codersdk.Workspace, error) { |
| 430 | // Validate template if provided |
| 431 | if f.template != "" { |
| 432 | _, err := parseTemplate(ctx, client, organizationIDs, f.template) |
| 433 | if err != nil { |
| 434 | return nil, xerrors.Errorf("parse template: %w", err) |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | // Parse target range |
| 439 | targetStart, targetEnd, err := parseTargetRange("workspaces", f.targetWorkspaces) |
| 440 | if err != nil { |
| 441 | return nil, xerrors.Errorf("parse target workspaces: %w", err) |
| 442 | } |
| 443 | |
| 444 | // Determine owner based on useHostLogin |
| 445 | var owner string |
| 446 | if f.useHostLogin { |
| 447 | owner = codersdk.Me |
| 448 | } |
| 449 | |
| 450 | // Get workspaces |
| 451 | workspaces, numSkipped, err := getScaletestWorkspaces(ctx, client, owner, f.template) |
| 452 | if err != nil { |
| 453 | return nil, err |
| 454 | } |
| 455 | if numSkipped > 0 { |
| 456 | cliui.Warnf(warnWriter, "CODER_DISABLE_OWNER_WORKSPACE_ACCESS is set on the deployment.\n\t%d workspace(s) were skipped due to ownership mismatch.\n\tSet --use-host-login to only target workspaces you own.", numSkipped) |
| 457 | } |
| 458 | |
| 459 | // Adjust targetEnd if not specified |
| 460 | if targetEnd == 0 { |
| 461 | targetEnd = len(workspaces) |
| 462 | } |
| 463 | |
| 464 | // Validate range |
| 465 | if len(workspaces) == 0 { |
| 466 | return nil, xerrors.Errorf("no scaletest workspaces exist") |
| 467 | } |
| 468 | if targetEnd > len(workspaces) { |
| 469 | return nil, xerrors.Errorf("target workspace end %d is greater than the number of workspaces %d", targetEnd, len(workspaces)) |
| 470 | } |
| 471 | |
| 472 | // Return the sliced workspaces |
| 473 | return workspaces[targetStart:targetEnd], nil |
| 474 | } |
| 475 | |
| 476 | func RequireAdmin(ctx context.Context, client *codersdk.Client) (codersdk.User, error) { |
| 477 | me, err := client.User(ctx, codersdk.Me) |
no test coverage detected