Skip to content

Commit

Permalink
adds support for creating new workspaces (#27)
Browse files Browse the repository at this point in the history
* added workspace new

* renamed select test file

* moved to feature folder

* doc'd workspace new
  • Loading branch information
charleszipp authored May 14, 2021
1 parent d6c4a78 commit 5e19414
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 1 deletion.
12 changes: 12 additions & 0 deletions overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,15 @@ The task supports managing workspaces within pipelines. The following workspace
workspaceSubCommand: select
workspaceName: foo
```

### Workspace New

```yaml
- task: TerraformCLI@0
displayName: select workspace foo
inputs:
workingDirectory: $(terraform_templates_dir)
command: workspace
workspaceSubCommand: new
workspaceName: foo
```
2 changes: 1 addition & 1 deletion pipelines/pipeline-alpha.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ stages:
stage: alpha
scenarios:
- smoke_test.yml
- init_with_self_configured_backend.yml
- switch_workspaces.yml
11 changes: 11 additions & 0 deletions pipelines/test/switch_workspaces.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ jobs:
backendAzureRmStorageAccountSku: Standard_RAGRS
backendAzureRmContainerName: azure-pipelines-terraform
backendAzureRmKey: workspace_switch.tfstate
- task: charleszipp.azure-pipelines-tasks-terraform-${{ parameters.stage }}.azure-pipelines-tasks-terraform-cli.TerraformCLI@0
displayName: new workspace foo
inputs:
workingDirectory: $(terraform_templates_dir)
command: workspace
workspaceSubCommand: new
workspaceName: foo
- task: charleszipp.azure-pipelines-tasks-terraform-${{ parameters.stage }}.azure-pipelines-tasks-terraform-cli.TerraformCLI@0
displayName: select workspace default
inputs:
Expand All @@ -37,4 +44,8 @@ jobs:
- bash: |
terraform workspace show
displayName: show workspace
workingDirectory: $(terraform_templates_dir)
- bash: |
terraform workspace delete foo
displayName: delete new workspace
workingDirectory: $(terraform_templates_dir)
1 change: 1 addition & 0 deletions tasks/terraform-cli/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export { TerraformForceUnlock as ForceUnlockCommandHandler } from './tf-force-un
export { TerraformShow as ShowCommandHandler } from './tf-show';
export { TerraformFormat as FormatCommandHandler } from './tf-fmt';
export { TerraformWorkspaceSelect as WorkspaceSelectCommandHandler } from './tf-workspace-select';
export { TerraformWorkspaceNew as WorkspaceNewCommandHandler } from './tf-workspace-new';

declare module '../runners'{
interface RunnerResult{
Expand Down
22 changes: 22 additions & 0 deletions tasks/terraform-cli/src/commands/tf-workspace-new.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { CommandResponse, ICommand } from ".";
import { ITaskContext } from "../context";
import { IRunner } from "../runners";
import { RunWithTerraform } from "../runners/builders";

export class TerraformWorkspaceNew implements ICommand {
constructor(
private readonly runner: IRunner
) {
}

async exec(ctx: ITaskContext): Promise<CommandResponse> {
const options = await new RunWithTerraform(ctx, false, ctx.workspaceSubCommand, [ctx.name])
.withCommandOptions(ctx.commandOptions)
.withWorkspace(ctx.workspaceName)
.build();

const result = await this.runner.exec(options);

return result.toCommandResponse();
}
}
2 changes: 2 additions & 0 deletions tasks/terraform-cli/src/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ export class Task {
switch(this.ctx.workspaceSubCommand){
case "select":
return new commands.WorkspaceSelectCommandHandler(this.runner);
case "new":
return new commands.WorkspaceNewCommandHandler(this.runner);
default:
throw new Error(`Workspace sub-command "${this.ctx.workspaceSubCommand}" is not supported`);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\u001b[0m\u001b[32m\u001b[1mCreated and switched to workspace \"bar\"!\u001b[0m\u001b[32m\n\nYou're now on a new, empty workspace. Workspaces isolate their state,\nso if you run \"terraform plan\" Terraform will not see any existing state\nfor this configuration.\u001b[0m\n
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Feature: terraform workspace new

terraform workspace new [OPTIONS] [name] [DIR]

Scenario: new workspace
Given terraform exists
And terraform command is "workspace"
And workspace command is "new" with name "bar"
And running command "terraform workspace new bar" returns successful result with stdout from file "./src/tests/features/workspace/stdout-new-workspace-bar.txt"
When the terraform cli task is run
Then the terraform cli task executed command "terraform workspace new bar"
And the terraform cli task is successful

Scenario: new workspace with command options
Given terraform exists
And terraform command is "workspace" with options "-state=terraform.tfstate"
And workspace command is "new" with name "bar"
And running command "terraform workspace new -state=terraform.tfstate bar" returns successful result with stdout from file "./src/tests/features/workspace/stdout-new-workspace-bar.txt"
When the terraform cli task is run
Then the terraform cli task executed command "terraform workspace new -state=terraform.tfstate bar"
And the terraform cli task is successful

Scenario: new workspace already exists
Given terraform exists
And terraform command is "workspace"
And workspace command is "new" with name "bar"
And running command "terraform workspace new bar" fails with error "\u001b[31mWorkspace \"bar\" already exists\u001b[0m\u001b[0m\n"
When the terraform cli task is run
Then the terraform cli task executed command "terraform workspace new bar"
And the terraform cli task fails with message "\u001b[31mWorkspace \"bar\" already exists\u001b[0m\u001b[0m\n"

0 comments on commit 5e19414

Please sign in to comment.