Skip to content

Commit

Permalink
feat: Added env var support
Browse files Browse the repository at this point in the history
* to be used like

```hcl
var options = []tfexec.PlanOption{
  tfexec.EnvVar("SOME_ENV_VAR", "SOME_VALUE"),
  tfexec.EnvVar("OTHER_ENV_VAR", "OTHER_VALUE"),
}
```
  • Loading branch information
vfoucault committed Nov 18, 2021
1 parent 8d07178 commit a4de84a
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 6 deletions.
13 changes: 13 additions & 0 deletions tfexec/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ type applyConfig struct {
// Vars: each var must be supplied as a single string, e.g. 'foo=bar'
vars []string
varFiles []string

// EnvVars support
envVars map[string]string
}

var defaultApplyOptions = applyConfig{
lock: true,
parallelism: 10,
refresh: true,
envVars: map[string]string{},
}

// ApplyOption represents options used in the Apply method.
Expand Down Expand Up @@ -66,6 +70,10 @@ func (opt *VarFileOption) configureApply(conf *applyConfig) {
conf.varFiles = append(conf.varFiles, opt.path)
}

func (opt *EnvVarOption) configureApply(conf *applyConfig) {
conf.envVars[opt.key] = opt.value
}

func (opt *LockOption) configureApply(conf *applyConfig) {
conf.lock = opt.lock
}
Expand Down Expand Up @@ -157,6 +165,11 @@ func (tf *Terraform) applyCmd(ctx context.Context, opts ...ApplyOption) (*exec.C
}

mergeEnv := map[string]string{}

for kvar, kvalue := range c.envVars {
mergeEnv[kvar] = kvalue
}

if c.reattachInfo != nil {
reattachStr, err := c.reattachInfo.marshalString()
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion tfexec/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func TestApplyCmd(t *testing.T) {
StateOut("teststateout"),
VarFile("foo.tfvars"),
VarFile("bar.tfvars"),
EnvVar("blah", "diblah"),
EnvVar("other_env_var", "other_value"),
Lock(false),
Parallelism(99),
Refresh(false),
Expand Down Expand Up @@ -62,6 +64,6 @@ func TestApplyCmd(t *testing.T) {
"-var", "var1=foo",
"-var", "var2=bar",
"testfile",
}, nil, applyCmd)
}, map[string]string{"blah": "diblah", "other_env_var": "other_value"}, applyCmd)
})
}
13 changes: 13 additions & 0 deletions tfexec/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ type destroyConfig struct {
// Vars: each var must be supplied as a single string, e.g. 'foo=bar'
vars []string
varFiles []string

// EnvVars
envVars map[string]string
}

var defaultDestroyOptions = destroyConfig{
lock: true,
lockTimeout: "0s",
parallelism: 10,
refresh: true,
envVars: map[string]string{},
}

// DestroyOption represents options used in the Destroy method.
Expand Down Expand Up @@ -70,6 +74,10 @@ func (opt *VarFileOption) configureDestroy(conf *destroyConfig) {
conf.varFiles = append(conf.varFiles, opt.path)
}

func (opt *EnvVarOption) configureDestroy(conf *destroyConfig) {
conf.envVars[opt.key] = opt.value
}

func (opt *LockOption) configureDestroy(conf *destroyConfig) {
conf.lock = opt.lock
}
Expand Down Expand Up @@ -144,6 +152,11 @@ func (tf *Terraform) destroyCmd(ctx context.Context, opts ...DestroyOption) (*ex
}

mergeEnv := map[string]string{}

for kvar, kvalue := range c.envVars {
mergeEnv[kvar] = kvalue
}

if c.reattachInfo != nil {
reattachStr, err := c.reattachInfo.marshalString()
if err != nil {
Expand Down
19 changes: 17 additions & 2 deletions tfexec/destroy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,22 @@ 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"),
EnvVar("blah", "diblah"),
EnvVar("other_env_var", "other_value"))
if err != nil {
t.Fatal(err)
}
Expand All @@ -60,6 +75,6 @@ func TestDestroyCmd(t *testing.T) {
"-var", "var1=foo",
"-var", "var2=bar",
"destroydir",
}, nil, destroyCmd)
}, map[string]string{"blah": "diblah", "other_env_var": "other_value"}, destroyCmd)
})
}
11 changes: 11 additions & 0 deletions tfexec/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ type importConfig struct {
stateOut string
vars []string
varFiles []string
envVars map[string]string
}

var defaultImportOptions = importConfig{
allowMissingConfig: false,
lock: true,
lockTimeout: "0s",
envVars: map[string]string{},
}

// ImportOption represents options used in the Import method.
Expand Down Expand Up @@ -72,6 +74,10 @@ func (opt *VarFileOption) configureImport(conf *importConfig) {
conf.varFiles = append(conf.varFiles, opt.path)
}

func (opt *EnvVarOption) configureImport(conf *importConfig) {
conf.envVars[opt.key] = opt.value
}

// 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 @@ -129,6 +135,11 @@ func (tf *Terraform) importCmd(ctx context.Context, address, id string, opts ...
args = append(args, address, id)

mergeEnv := map[string]string{}

for kvar, kvalue := range c.envVars {
mergeEnv[kvar] = kvalue
}

if c.reattachInfo != nil {
reattachStr, err := c.reattachInfo.marshalString()
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion tfexec/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func TestImportCmd(t *testing.T) {
Var("var1=foo"),
Var("var2=bar"),
AllowMissingConfig(true),
EnvVar("blah", "diblah"),
EnvVar("other_env_var", "other_value"),
)
if err != nil {
t.Fatal(err)
Expand All @@ -66,6 +68,6 @@ func TestImportCmd(t *testing.T) {
"-var", "var2=bar",
"my-addr2",
"my-id2",
}, nil, importCmd)
}, map[string]string{"blah": "diblah", "other_env_var": "other_value"}, importCmd)
})
}
9 changes: 9 additions & 0 deletions tfexec/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ func DryRun(dryRun bool) *DryRunOption {
return &DryRunOption{dryRun}
}

type EnvVarOption struct {
key, value string
}

// EnvVar represents a kv env var
func EnvVar(key, value string) *EnvVarOption {
return &EnvVarOption{key, value}
}

type FSMirrorOption struct {
fsMirror string
}
Expand Down
11 changes: 11 additions & 0 deletions tfexec/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type planConfig struct {
targets []string
vars []string
varFiles []string
envVars map[string]string
}

var defaultPlanOptions = planConfig{
Expand All @@ -29,6 +30,7 @@ var defaultPlanOptions = planConfig{
lockTimeout: "0s",
parallelism: 10,
refresh: true,
envVars: map[string]string{},
}

// PlanOption represents options used in the Plan method.
Expand All @@ -44,6 +46,10 @@ func (opt *VarFileOption) configurePlan(conf *planConfig) {
conf.varFiles = append(conf.varFiles, opt.path)
}

func (opt *EnvVarOption) configurePlan(conf *planConfig) {
conf.envVars[opt.key] = opt.value
}

func (opt *VarOption) configurePlan(conf *planConfig) {
conf.vars = append(conf.vars, opt.assignment)
}
Expand Down Expand Up @@ -168,6 +174,11 @@ func (tf *Terraform) planCmd(ctx context.Context, opts ...PlanOption) (*exec.Cmd
}

mergeEnv := map[string]string{}

for kvar, kvalue := range c.envVars {
mergeEnv[kvar] = kvalue
}

if c.reattachInfo != nil {
reattachStr, err := c.reattachInfo.marshalString()
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions tfexec/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ func TestPlanCmd(t *testing.T) {
Var("android=paranoid"),
Var("brain_size=planet"),
VarFile("trillian"),
Dir("earth"))
Dir("earth"),
EnvVar("blah", "diblah"),
EnvVar("other_env_var", "other_value"))
if err != nil {
t.Fatal(err)
}
Expand All @@ -77,6 +79,6 @@ func TestPlanCmd(t *testing.T) {
"-var", "android=paranoid",
"-var", "brain_size=planet",
"earth",
}, nil, planCmd)
}, map[string]string{"blah": "diblah", "other_env_var": "other_value"}, planCmd)
})
}

0 comments on commit a4de84a

Please sign in to comment.