Skip to content

Commit

Permalink
add ReattachOption
Browse files Browse the repository at this point in the history
  • Loading branch information
kmoe committed Sep 7, 2020
1 parent 37f4547 commit 6f361a8
Show file tree
Hide file tree
Showing 16 changed files with 186 additions and 58 deletions.
24 changes: 17 additions & 7 deletions tfexec/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ type applyConfig struct {
lock bool

// LockTimeout must be a string with time unit, e.g. '10s'
lockTimeout string
parallelism int
refresh bool
state string
stateOut string
targets []string
lockTimeout string
parallelism int
reattachInfo string
refresh bool
state string
stateOut string
targets []string

// Vars: each var must be supplied as a single string, e.g. 'foo=bar'
vars []string
Expand Down Expand Up @@ -80,6 +81,10 @@ func (opt *DirOrPlanOption) configureApply(conf *applyConfig) {
conf.dirOrPlan = opt.path
}

func (opt *ReattachOption) configureApply(conf *applyConfig) {
conf.reattachInfo = opt.info
}

// Apply represents the terraform apply subcommand.
func (tf *Terraform) Apply(ctx context.Context, opts ...ApplyOption) error {
return tf.runTerraformCmd(tf.applyCmd(ctx, opts...))
Expand Down Expand Up @@ -133,5 +138,10 @@ func (tf *Terraform) applyCmd(ctx context.Context, opts ...ApplyOption) *exec.Cm
args = append(args, c.dirOrPlan)
}

return tf.buildTerraformCmd(ctx, args...)
mergeEnv := map[string]string{}
if c.reattachInfo != "" {
mergeEnv[reattachEnvVar] = c.reattachInfo
}

return tf.buildTerraformCmd(ctx, mergeEnv, args...)
}
16 changes: 13 additions & 3 deletions tfexec/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ func envSlice(environ map[string]string) []string {
}

func (tf *Terraform) buildEnv(mergeEnv map[string]string) []string {
osEnv := envMap(os.Environ())

// set Terraform level env, if env is nil, fall back to os.Environ
var env map[string]string
if tf.env == nil {
env = envMap(os.Environ())
env = osEnv
} else {
env = make(map[string]string, len(tf.env))
for k, v := range tf.env {
Expand Down Expand Up @@ -114,12 +116,20 @@ func (tf *Terraform) buildEnv(mergeEnv map[string]string) []string {
// force usage of workspace methods for switching
env[workspaceEnvVar] = ""

if tf.disablePluginTls {
env[disablePluginTlsEnvVar] = "1"
}

if tf.skipProviderVerify {
env[skipProviderVerifyEnvVar] = "1"
}

return envSlice(env)
}

func (tf *Terraform) buildTerraformCmd(ctx context.Context, args ...string) *exec.Cmd {
func (tf *Terraform) buildTerraformCmd(ctx context.Context, mergeEnv map[string]string, args ...string) *exec.Cmd {
cmd := exec.CommandContext(ctx, tf.execPath, args...)
cmd.Env = tf.buildEnv(nil)
cmd.Env = tf.buildEnv(mergeEnv)
cmd.Dir = tf.workingDir

tf.logger.Printf("[INFO] running Terraform command: %s", cmdString(cmd))
Expand Down
24 changes: 17 additions & 7 deletions tfexec/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ type destroyConfig struct {
lock bool

// LockTimeout must be a string with time unit, e.g. '10s'
lockTimeout string
parallelism int
refresh bool
state string
stateOut string
targets []string
lockTimeout string
parallelism int
reattachInfo string
refresh bool
state string
stateOut string
targets []string

// Vars: each var must be supplied as a single string, e.g. 'foo=bar'
vars []string
Expand Down Expand Up @@ -81,6 +82,10 @@ func (opt *VarOption) configureDestroy(conf *destroyConfig) {
conf.vars = append(conf.vars, opt.assignment)
}

func (opt *ReattachOption) configureDestroy(conf *destroyConfig) {
conf.reattachInfo = opt.info
}

// Destroy represents the terraform destroy subcommand.
func (tf *Terraform) Destroy(ctx context.Context, opts ...DestroyOption) error {
return tf.runTerraformCmd(tf.destroyCmd(ctx, opts...))
Expand Down Expand Up @@ -134,5 +139,10 @@ func (tf *Terraform) destroyCmd(ctx context.Context, opts ...DestroyOption) *exe
args = append(args, c.dir)
}

return tf.buildTerraformCmd(ctx, args...)
mergeEnv := map[string]string{}
if c.reattachInfo != "" {
mergeEnv[reattachEnvVar] = c.reattachInfo
}

return tf.buildTerraformCmd(ctx, mergeEnv, args...)
}
13 changes: 12 additions & 1 deletion tfexec/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type importConfig struct {
allowMissingConfig bool
lock bool
lockTimeout string
reattachInfo string
state string
stateOut string
vars []string
Expand Down Expand Up @@ -51,6 +52,10 @@ func (opt *LockTimeoutOption) configureImport(conf *importConfig) {
conf.lockTimeout = opt.timeout
}

func (opt *ReattachOption) configureImport(conf *importConfig) {
conf.reattachInfo = opt.info
}

func (opt *StateOption) configureImport(conf *importConfig) {
conf.state = opt.path
}
Expand Down Expand Up @@ -119,5 +124,11 @@ func (tf *Terraform) importCmd(ctx context.Context, address, id string, opts ...
// required args, always pass
args = append(args, address, id)

return tf.buildTerraformCmd(ctx, args...)
mergeEnv := map[string]string{}
if c.reattachInfo != "" {
mergeEnv[reattachEnvVar] = c.reattachInfo
}

return tf.buildTerraformCmd(ctx, mergeEnv, args...)

}
12 changes: 11 additions & 1 deletion tfexec/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type initConfig struct {
lock bool
lockTimeout string
pluginDir []string
reattachInfo string
reconfigure bool
upgrade bool
verifyPlugins bool
Expand Down Expand Up @@ -75,6 +76,10 @@ func (opt *PluginDirOption) configureInit(conf *initConfig) {
conf.pluginDir = append(conf.pluginDir, opt.pluginDir)
}

func (opt *ReattachOption) configureInit(conf *initConfig) {
conf.reattachInfo = opt.info
}

func (opt *ReconfigureOption) configureInit(conf *initConfig) {
conf.reconfigure = opt.reconfigure
}
Expand Down Expand Up @@ -139,5 +144,10 @@ func (tf *Terraform) initCmd(ctx context.Context, opts ...InitOption) *exec.Cmd
args = append(args, c.dir)
}

return tf.buildTerraformCmd(ctx, args...)
mergeEnv := map[string]string{}
if c.reattachInfo != "" {
mergeEnv[reattachEnvVar] = c.reattachInfo
}

return tf.buildTerraformCmd(ctx, mergeEnv, args...)
}
8 changes: 8 additions & 0 deletions tfexec/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ func PluginDir(pluginDir string) *PluginDirOption {
return &PluginDirOption{pluginDir}
}

type ReattachOption struct {
info string
}

func Reattach(reattach string) *ReattachOption {
return &ReattachOption{reattach}
}

type ReconfigureOption struct {
reconfigure bool
}
Expand Down
2 changes: 1 addition & 1 deletion tfexec/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ func (tf *Terraform) outputCmd(ctx context.Context, opts ...OutputOption) *exec.
args = append(args, "-state="+c.state)
}

return tf.buildTerraformCmd(ctx, args...)
return tf.buildTerraformCmd(ctx, nil, args...)
}
35 changes: 23 additions & 12 deletions tfexec/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ import (
)

type planConfig struct {
destroy bool
dir string
lock bool
lockTimeout string
out string
parallelism int
refresh bool
state string
targets []string
vars []string
varFiles []string
destroy bool
dir string
lock bool
lockTimeout string
out string
parallelism int
reattachInfo string
refresh bool
state string
targets []string
vars []string
varFiles []string
}

var defaultPlanOptions = planConfig{
Expand Down Expand Up @@ -54,6 +55,10 @@ func (opt *StateOption) configurePlan(conf *planConfig) {
conf.state = opt.path
}

func (opt *ReattachOption) configurePlan(conf *planConfig) {
conf.reattachInfo = opt.info
}

func (opt *RefreshOption) configurePlan(conf *planConfig) {
conf.refresh = opt.refresh
}
Expand Down Expand Up @@ -145,5 +150,11 @@ func (tf *Terraform) planCmd(ctx context.Context, opts ...PlanOption) *exec.Cmd
args = append(args, c.dir)
}

return tf.buildTerraformCmd(ctx, args...)
mergeEnv := map[string]string{}
if c.reattachInfo != "" {
mergeEnv[reattachEnvVar] = c.reattachInfo
}

return tf.buildTerraformCmd(ctx, mergeEnv, args...)

}
2 changes: 1 addition & 1 deletion tfexec/providers_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ func (tf *Terraform) providersSchemaCmd(ctx context.Context, args ...string) *ex
allArgs := []string{"providers", "schema", "-json", "-no-color"}
allArgs = append(allArgs, args...)

return tf.buildTerraformCmd(ctx, allArgs...)
return tf.buildTerraformCmd(ctx, nil, allArgs...)
}
31 changes: 21 additions & 10 deletions tfexec/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import (
)

type refreshConfig struct {
backup string
dir string
lock bool
lockTimeout string
state string
stateOut string
targets []string
vars []string
varFiles []string
backup string
dir string
lock bool
lockTimeout string
reattachInfo string
state string
stateOut string
targets []string
vars []string
varFiles []string
}

var defaultRefreshOptions = refreshConfig{
Expand Down Expand Up @@ -44,6 +45,10 @@ func (opt *LockTimeoutOption) configureRefresh(conf *refreshConfig) {
conf.lockTimeout = opt.timeout
}

func (opt *ReattachOption) configureRefresh(conf *refreshConfig) {
conf.reattachInfo = opt.info
}

func (opt *StateOption) configureRefresh(conf *refreshConfig) {
conf.state = opt.path
}
Expand Down Expand Up @@ -115,5 +120,11 @@ func (tf *Terraform) refreshCmd(ctx context.Context, opts ...RefreshCmdOption) *
args = append(args, c.dir)
}

return tf.buildTerraformCmd(ctx, args...)
mergeEnv := map[string]string{}
if c.reattachInfo != "" {
mergeEnv[reattachEnvVar] = c.reattachInfo
}

return tf.buildTerraformCmd(ctx, mergeEnv, args...)

}
Loading

0 comments on commit 6f361a8

Please sign in to comment.