Skip to content

Commit

Permalink
feat: Added back TF_WORKSPACE support
Browse files Browse the repository at this point in the history
through `WorkSpaceOption`.

usage:

```hcl
var options = []tfexec.ApplyOption{
		tfexec.WorkSpace("some_workspace"),
	}
```
  • Loading branch information
vfoucault committed May 11, 2022
1 parent 40a1161 commit d286988
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 11 deletions.
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)
})
}

0 comments on commit d286988

Please sign in to comment.