Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added back TF_WORKSPACE support #249

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions tfexec/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ type applyConfig struct {
// Vars: each var must be supplied as a single string, e.g. 'foo=bar'
vars []string
varFiles []string

// WorkSpace: when leveraging TF_WORKSPACE env variable
workSpace string
}

var defaultApplyOptions = applyConfig{
Expand Down Expand Up @@ -90,6 +93,10 @@ func (opt *ReattachOption) configureApply(conf *applyConfig) {
conf.reattachInfo = opt.info
}

func (opt *WorkSpaceOption) configureApply(conf *applyConfig) {
conf.workSpace = opt.workSpace
}

// Apply represents the terraform apply subcommand.
func (tf *Terraform) Apply(ctx context.Context, opts ...ApplyOption) error {
cmd, err := tf.applyCmd(ctx, opts...)
Expand Down Expand Up @@ -165,5 +172,10 @@ func (tf *Terraform) applyCmd(ctx context.Context, opts ...ApplyOption) (*exec.C
mergeEnv[reattachEnvVar] = reattachStr
}

// Check if TF_WORKSPACE is specified.
if c.workSpace != "" {
mergeEnv[workspaceEnvVar] = c.workSpace
}

return tf.buildTerraformCmd(ctx, mergeEnv, args...), nil
}
4 changes: 3 additions & 1 deletion tfexec/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestApplyCmd(t *testing.T) {
Var("var1=foo"),
Var("var2=bar"),
DirOrPlan("testfile"),
WorkSpace("some_workspace"),
)
if err != nil {
t.Fatal(err)
Expand All @@ -62,6 +63,7 @@ func TestApplyCmd(t *testing.T) {
"-var", "var1=foo",
"-var", "var2=bar",
"testfile",
}, nil, applyCmd)
}, map[string]string{"TF_WORKSPACE": "some_workspace"}, applyCmd)
})

}
4 changes: 0 additions & 4 deletions tfexec/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ var prohibitedEnvVars = []string{
logEnvVar,
reattachEnvVar,
appendUserAgentEnvVar,
workspaceEnvVar,
disablePluginTLSEnvVar,
skipProviderVerifyEnvVar,
}
Expand Down Expand Up @@ -156,9 +155,6 @@ func (tf *Terraform) buildEnv(mergeEnv map[string]string) []string {
// constant automation override env vars
env[automationEnvVar] = "1"

// force usage of workspace methods for switching
env[workspaceEnvVar] = ""

if tf.disablePluginTLS {
env[disablePluginTLSEnvVar] = "1"
}
Expand Down
1 change: 0 additions & 1 deletion tfexec/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func defaultEnv() []string {
"TF_IN_AUTOMATION=1",
"TF_LOG_PATH=",
"TF_LOG=",
"TF_WORKSPACE=",
}
}

Expand Down
12 changes: 12 additions & 0 deletions tfexec/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ type destroyConfig struct {
// Vars: each var must be supplied as a single string, e.g. 'foo=bar'
vars []string
varFiles []string

// WorkSpace: when leveraging TF_WORKSPACE env variable
workSpace string
}

var defaultDestroyOptions = destroyConfig{
Expand Down Expand Up @@ -86,6 +89,10 @@ func (opt *ReattachOption) configureDestroy(conf *destroyConfig) {
conf.reattachInfo = opt.info
}

func (opt *WorkSpaceOption) configureDestroy(conf *destroyConfig) {
conf.workSpace = opt.workSpace
}

// Destroy represents the terraform destroy subcommand.
func (tf *Terraform) Destroy(ctx context.Context, opts ...DestroyOption) error {
cmd, err := tf.destroyCmd(ctx, opts...)
Expand Down Expand Up @@ -152,5 +159,10 @@ func (tf *Terraform) destroyCmd(ctx context.Context, opts ...DestroyOption) (*ex
mergeEnv[reattachEnvVar] = reattachStr
}

// Check if TF_WORKSPACE is specified.
if c.workSpace != "" {
mergeEnv[workspaceEnvVar] = c.workSpace
}

return tf.buildTerraformCmd(ctx, mergeEnv, args...), nil
}
17 changes: 15 additions & 2 deletions tfexec/destroy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,20 @@ func TestDestroyCmd(t *testing.T) {
})

t.Run("override all defaults", func(t *testing.T) {
destroyCmd, err := tf.destroyCmd(context.Background(), Backup("testbackup"), LockTimeout("200s"), State("teststate"), StateOut("teststateout"), VarFile("testvarfile"), Lock(false), Parallelism(99), Refresh(false), Target("target1"), Target("target2"), Var("var1=foo"), Var("var2=bar"), Dir("destroydir"))
destroyCmd, err := tf.destroyCmd(context.Background(),
Backup("testbackup"),
LockTimeout("200s"),
State("teststate"),
StateOut("teststateout"),
VarFile("testvarfile"),
Lock(false),
Parallelism(99),
Refresh(false),
Target("target1"),
Target("target2"),
Var("var1=foo"),
Var("var2=bar"), Dir("destroydir"),
WorkSpace("some_workspace"))
if err != nil {
t.Fatal(err)
}
Expand All @@ -60,6 +73,6 @@ func TestDestroyCmd(t *testing.T) {
"-var", "var1=foo",
"-var", "var2=bar",
"destroydir",
}, nil, destroyCmd)
}, map[string]string{"TF_WORKSPACE": "some_workspace"}, destroyCmd)
})
}
12 changes: 12 additions & 0 deletions tfexec/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ type importConfig struct {
stateOut string
vars []string
varFiles []string

// WorkSpace: when leveraging TF_WORKSPACE env variable
workSpace string
}

var defaultImportOptions = importConfig{
Expand Down Expand Up @@ -72,6 +75,10 @@ func (opt *VarFileOption) configureImport(conf *importConfig) {
conf.varFiles = append(conf.varFiles, opt.path)
}

func (opt *WorkSpaceOption) configureImport(conf *importConfig) {
conf.workSpace = opt.workSpace
}

// Import represents the terraform import subcommand.
func (tf *Terraform) Import(ctx context.Context, address, id string, opts ...ImportOption) error {
cmd, err := tf.importCmd(ctx, address, id, opts...)
Expand Down Expand Up @@ -137,5 +144,10 @@ func (tf *Terraform) importCmd(ctx context.Context, address, id string, opts ...
mergeEnv[reattachEnvVar] = reattachStr
}

// Check if TF_WORKSPACE is specified.
if c.workSpace != "" {
mergeEnv[workspaceEnvVar] = c.workSpace
}

return tf.buildTerraformCmd(ctx, mergeEnv, args...), nil
}
3 changes: 2 additions & 1 deletion tfexec/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func TestImportCmd(t *testing.T) {
Var("var1=foo"),
Var("var2=bar"),
AllowMissingConfig(true),
WorkSpace("some_workspace"),
)
if err != nil {
t.Fatal(err)
Expand All @@ -66,6 +67,6 @@ func TestImportCmd(t *testing.T) {
"-var", "var2=bar",
"my-addr2",
"my-id2",
}, nil, importCmd)
}, map[string]string{"TF_WORKSPACE": "some_workspace"}, importCmd)
})
}
9 changes: 9 additions & 0 deletions tfexec/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,12 @@ type VerifyPluginsOption struct {
func VerifyPlugins(verifyPlugins bool) *VerifyPluginsOption {
return &VerifyPluginsOption{verifyPlugins}
}

// WorkSpaceOption is used to leverage the TF_WORKSPACE environment variable
type WorkSpaceOption struct {
workSpace string
}

func WorkSpace(workSpace string) *WorkSpaceOption {
return &WorkSpaceOption{workSpace}
}
12 changes: 12 additions & 0 deletions tfexec/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ type planConfig struct {
targets []string
vars []string
varFiles []string

// WorkSpace: when leveraging TF_WORKSPACE env variable
workSpace string
}

var defaultPlanOptions = planConfig{
Expand Down Expand Up @@ -88,6 +91,10 @@ func (opt *DestroyFlagOption) configurePlan(conf *planConfig) {
conf.destroy = opt.destroy
}

func (opt *WorkSpaceOption) configurePlan(conf *planConfig) {
conf.workSpace = opt.workSpace
}

// Plan executes `terraform plan` with the specified options and waits for it
// to complete.
//
Expand Down Expand Up @@ -176,5 +183,10 @@ func (tf *Terraform) planCmd(ctx context.Context, opts ...PlanOption) (*exec.Cmd
mergeEnv[reattachEnvVar] = reattachStr
}

// Check if TF_WORKSPACE is specified.
if c.workSpace != "" {
mergeEnv[workspaceEnvVar] = c.workSpace
}

return tf.buildTerraformCmd(ctx, mergeEnv, args...), nil
}
5 changes: 3 additions & 2 deletions tfexec/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ func TestPlanCmd(t *testing.T) {
Var("android=paranoid"),
Var("brain_size=planet"),
VarFile("trillian"),
Dir("earth"))
Dir("earth"),
WorkSpace("some_workspace"))
if err != nil {
t.Fatal(err)
}
Expand All @@ -77,6 +78,6 @@ func TestPlanCmd(t *testing.T) {
"-var", "android=paranoid",
"-var", "brain_size=planet",
"earth",
}, nil, planCmd)
}, map[string]string{"TF_WORKSPACE": "some_workspace"}, planCmd)
})
}
12 changes: 12 additions & 0 deletions tfexec/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ type refreshConfig struct {
targets []string
vars []string
varFiles []string

// WorkSpace: when leveraging TF_WORKSPACE env variable
workSpace string
}

var defaultRefreshOptions = refreshConfig{
Expand Down Expand Up @@ -69,6 +72,10 @@ func (opt *VarFileOption) configureRefresh(conf *refreshConfig) {
conf.varFiles = append(conf.varFiles, opt.path)
}

func (opt *WorkSpaceOption) configureRefresh(conf *refreshConfig) {
conf.workSpace = opt.workSpace
}

// Refresh represents the terraform refresh subcommand.
func (tf *Terraform) Refresh(ctx context.Context, opts ...RefreshCmdOption) error {
cmd, err := tf.refreshCmd(ctx, opts...)
Expand Down Expand Up @@ -133,5 +140,10 @@ func (tf *Terraform) refreshCmd(ctx context.Context, opts ...RefreshCmdOption) (
mergeEnv[reattachEnvVar] = reattachStr
}

// Check if TF_WORKSPACE is specified.
if c.workSpace != "" {
mergeEnv[workspaceEnvVar] = c.workSpace
}

return tf.buildTerraformCmd(ctx, mergeEnv, args...), nil
}
17 changes: 15 additions & 2 deletions tfexec/refresh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,20 @@ func TestRefreshCmd(t *testing.T) {
})

t.Run("override all defaults", func(t *testing.T) {
refreshCmd, err := tf.refreshCmd(context.Background(), Backup("testbackup"), LockTimeout("200s"), State("teststate"), StateOut("teststateout"), VarFile("testvarfile"), Lock(false), Target("target1"), Target("target2"), Var("var1=foo"), Var("var2=bar"), Dir("refreshdir"))
refreshCmd, err := tf.refreshCmd(context.Background(),
Backup("testbackup"),
LockTimeout("200s"),
State("teststate"),
StateOut("teststateout"),
VarFile("testvarfile"),
Lock(false),
Target("target1"),
Target("target2"),
Var("var1=foo"),
Var("var2=bar"),
Dir("refreshdir"),
WorkSpace("some_workspace"))

if err != nil {
t.Fatal(err)
}
Expand All @@ -54,6 +67,6 @@ func TestRefreshCmd(t *testing.T) {
"-var", "var1=foo",
"-var", "var2=bar",
"refreshdir",
}, nil, refreshCmd)
}, map[string]string{"TF_WORKSPACE": "some_workspace"}, refreshCmd)
})
}
27 changes: 27 additions & 0 deletions tfexec/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (

type showConfig struct {
reattachInfo ReattachInfo

// WorkSpace: when leveraging TF_WORKSPACE env variable
workSpace string
}

var defaultShowOptions = showConfig{}
Expand All @@ -23,6 +26,10 @@ func (opt *ReattachOption) configureShow(conf *showConfig) {
conf.reattachInfo = opt.info
}

func (opt *WorkSpaceOption) configureShow(conf *showConfig) {
conf.workSpace = opt.workSpace
}

// Show reads the default state path and outputs the state.
// To read a state or plan file, ShowState or ShowPlan must be used instead.
func (tf *Terraform) Show(ctx context.Context, opts ...ShowOption) (*tfjson.State, error) {
Expand All @@ -46,6 +53,11 @@ func (tf *Terraform) Show(ctx context.Context, opts ...ShowOption) (*tfjson.Stat
mergeEnv[reattachEnvVar] = reattachStr
}

// Check if TF_WORKSPACE is specified.
if c.workSpace != "" {
mergeEnv[workspaceEnvVar] = c.workSpace
}

showCmd := tf.showCmd(ctx, true, mergeEnv)

var ret tfjson.State
Expand Down Expand Up @@ -89,6 +101,11 @@ func (tf *Terraform) ShowStateFile(ctx context.Context, statePath string, opts .
mergeEnv[reattachEnvVar] = reattachStr
}

// Check if TF_WORKSPACE is specified.
if c.workSpace != "" {
mergeEnv[workspaceEnvVar] = c.workSpace
}

showCmd := tf.showCmd(ctx, true, mergeEnv, statePath)

var ret tfjson.State
Expand Down Expand Up @@ -132,6 +149,11 @@ func (tf *Terraform) ShowPlanFile(ctx context.Context, planPath string, opts ...
mergeEnv[reattachEnvVar] = reattachStr
}

// Check if TF_WORKSPACE is specified.
if c.workSpace != "" {
mergeEnv[workspaceEnvVar] = c.workSpace
}

showCmd := tf.showCmd(ctx, true, mergeEnv, planPath)

var ret tfjson.Plan
Expand Down Expand Up @@ -171,6 +193,11 @@ func (tf *Terraform) ShowPlanFileRaw(ctx context.Context, planPath string, opts
mergeEnv[reattachEnvVar] = reattachStr
}

// Check if TF_WORKSPACE is specified.
if c.workSpace != "" {
mergeEnv[workspaceEnvVar] = c.workSpace
}

showCmd := tf.showCmd(ctx, false, mergeEnv, planPath)

var outBuf strings.Builder
Expand Down