-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tfexec: Add
WorkspaceDelete()
(#212)
* tfexec: Add WorkspaceDelete() * add E2E test for workspace deletion
- Loading branch information
1 parent
53bf871
commit 7262075
Showing
3 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package tfexec | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os/exec" | ||
"strconv" | ||
) | ||
|
||
type workspaceDeleteConfig struct { | ||
lock bool | ||
lockTimeout string | ||
force bool | ||
} | ||
|
||
var defaultWorkspaceDeleteOptions = workspaceDeleteConfig{ | ||
lock: true, | ||
lockTimeout: "0s", | ||
} | ||
|
||
// WorkspaceDeleteCmdOption represents options that are applicable to the WorkspaceDelete method. | ||
type WorkspaceDeleteCmdOption interface { | ||
configureWorkspaceDelete(*workspaceDeleteConfig) | ||
} | ||
|
||
func (opt *LockOption) configureWorkspaceDelete(conf *workspaceDeleteConfig) { | ||
conf.lock = opt.lock | ||
} | ||
|
||
func (opt *LockTimeoutOption) configureWorkspaceDelete(conf *workspaceDeleteConfig) { | ||
conf.lockTimeout = opt.timeout | ||
} | ||
|
||
func (opt *ForceOption) configureWorkspaceDelete(conf *workspaceDeleteConfig) { | ||
conf.force = opt.force | ||
} | ||
|
||
// WorkspaceDelete represents the workspace delete subcommand to the Terraform CLI. | ||
func (tf *Terraform) WorkspaceDelete(ctx context.Context, workspace string, opts ...WorkspaceDeleteCmdOption) error { | ||
cmd, err := tf.workspaceDeleteCmd(ctx, workspace, opts...) | ||
if err != nil { | ||
return err | ||
} | ||
return tf.runTerraformCmd(ctx, cmd) | ||
} | ||
|
||
func (tf *Terraform) workspaceDeleteCmd(ctx context.Context, workspace string, opts ...WorkspaceDeleteCmdOption) (*exec.Cmd, error) { | ||
c := defaultWorkspaceDeleteOptions | ||
|
||
for _, o := range opts { | ||
switch o.(type) { | ||
case *LockOption, *LockTimeoutOption: | ||
err := tf.compatible(ctx, tf0_12_0, nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("-lock and -lock-timeout were added to workspace delete in Terraform 0.12: %w", err) | ||
} | ||
} | ||
|
||
o.configureWorkspaceDelete(&c) | ||
} | ||
|
||
args := []string{"workspace", "delete", "-no-color"} | ||
|
||
if c.force { | ||
args = append(args, "-force") | ||
} | ||
if c.lockTimeout != "" && c.lockTimeout != defaultWorkspaceDeleteOptions.lockTimeout { | ||
// only pass if not default, so we don't need to worry about the 0.11 version check | ||
args = append(args, "-lock-timeout="+c.lockTimeout) | ||
} | ||
if !c.lock { | ||
// only pass if false, so we don't need to worry about the 0.11 version check | ||
args = append(args, "-lock="+strconv.FormatBool(c.lock)) | ||
} | ||
|
||
args = append(args, workspace) | ||
|
||
cmd := tf.buildTerraformCmd(ctx, nil, args...) | ||
|
||
return cmd, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package tfexec | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-exec/tfexec/internal/testutil" | ||
) | ||
|
||
func TestWorkspaceDeleteCmd(t *testing.T) { | ||
td := testTempDir(t) | ||
|
||
tf, err := NewTerraform(td, tfVersion(t, testutil.Latest013)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// empty env, to avoid environ mismatch in testing | ||
tf.SetEnv(map[string]string{}) | ||
|
||
t.Run("defaults", func(t *testing.T) { | ||
workspaceDeleteCmd, err := tf.workspaceDeleteCmd(context.Background(), "workspace-name") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
assertCmd(t, []string{ | ||
"workspace", "delete", | ||
"-no-color", | ||
"workspace-name", | ||
}, nil, workspaceDeleteCmd) | ||
}) | ||
|
||
t.Run("override all defaults", func(t *testing.T) { | ||
workspaceDeleteCmd, err := tf.workspaceDeleteCmd(context.Background(), "workspace-name", | ||
LockTimeout("200s"), | ||
Force(true), | ||
Lock(false)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
assertCmd(t, []string{ | ||
"workspace", "delete", | ||
"-no-color", | ||
"-force", | ||
"-lock-timeout=200s", | ||
"-lock=false", | ||
"workspace-name", | ||
}, nil, workspaceDeleteCmd) | ||
}) | ||
} |