/
workspace.go
109 lines (94 loc) · 3.63 KB
/
workspace.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package terraform
import (
"fmt"
"regexp"
"strings"
"github.com/gruntwork-io/terratest/modules/testing"
"github.com/stretchr/testify/require"
)
// WorkspaceSelectOrNew runs terraform workspace with the given options and the workspace name
// and returns a name of the current workspace. It tries to select a workspace with the given
// name, or it creates a new one if it doesn't exist.
func WorkspaceSelectOrNew(t testing.TestingT, options *Options, name string) string {
out, err := WorkspaceSelectOrNewE(t, options, name)
if err != nil {
t.Fatal(err)
}
return out
}
// WorkspaceSelectOrNewE runs terraform workspace with the given options and the workspace name
// and returns a name of the current workspace. It tries to select a workspace with the given
// name, or it creates a new one if it doesn't exist.
func WorkspaceSelectOrNewE(t testing.TestingT, options *Options, name string) (string, error) {
out, err := RunTerraformCommandE(t, options, "workspace", "list")
if err != nil {
return "", err
}
if isExistingWorkspace(out, name) {
_, err = RunTerraformCommandE(t, options, "workspace", "select", name)
} else {
_, err = RunTerraformCommandE(t, options, "workspace", "new", name)
}
if err != nil {
return "", err
}
return RunTerraformCommandE(t, options, "workspace", "show")
}
func isExistingWorkspace(out string, name string) bool {
workspaces := strings.Split(out, "\n")
for _, ws := range workspaces {
if nameMatchesWorkspace(name, ws) {
return true
}
}
return false
}
func nameMatchesWorkspace(name string, workspace string) bool {
// Regex for matching workspace should match for strings with optional leading asterisk "*"
// following optional white spaces following the workspace name.
// E.g. for the given name "terratest", following strings will match:
//
// "* terratest"
// " terratest"
match, _ := regexp.MatchString(fmt.Sprintf("^\\*?\\s*%s$", name), workspace)
return match
}
// WorkspaceDelete removes the specified terraform workspace with the given options.
// It returns the name of the current workspace AFTER deletion, and the returned error (that can be nil).
// If the workspace to delete is the current one, then it tries to switch to the "default" workspace.
// Deleting the workspace "default" is not supported.
func WorkspaceDeleteE(t testing.TestingT, options *Options, name string) (string, error) {
currentWorkspace, err := RunTerraformCommandE(t, options, "workspace", "show")
if err != nil {
return currentWorkspace, err
}
if name == "default" {
return currentWorkspace, &UnsupportedDefaultWorkspaceDeletion{}
}
out, err := RunTerraformCommandE(t, options, "workspace", "list")
if err != nil {
return currentWorkspace, err
}
if !isExistingWorkspace(out, name) {
return currentWorkspace, WorkspaceDoesNotExist(name)
}
// Switch workspace before deleting if it is the current
if currentWorkspace == name {
currentWorkspace, err = WorkspaceSelectOrNewE(t, options, "default")
if err != nil {
return currentWorkspace, err
}
}
// delete workspace
_, err = RunTerraformCommandE(t, options, "workspace", "delete", name)
return currentWorkspace, err
}
// WorkspaceDelete removes the specified terraform workspace with the given options.
// It returns the name of the current workspace AFTER deletion.
// If the workspace to delete is the current one, then it tries to switch to the "default" workspace.
// Deleting the workspace "default" is not supported and only return an empty string (to avoid a fatal error).
func WorkspaceDelete(t testing.TestingT, options *Options, name string) string {
out, err := WorkspaceDeleteE(t, options, name)
require.NoError(t, err)
return out
}