Skip to content
This repository has been archived by the owner on Apr 27, 2021. It is now read-only.

Commit

Permalink
check SetEnv errs throughout
Browse files Browse the repository at this point in the history
  • Loading branch information
kmoe committed Sep 9, 2020
1 parent 05d42a8 commit dec3708
Showing 1 changed file with 49 additions and 13 deletions.
62 changes: 49 additions & 13 deletions working_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ func (wd *WorkingDir) SetConfig(cfg string) error {
tf.SetLogPath(p)
}

tf.SetEnv(wd.buildEnv())
err = tf.SetEnv(wd.buildEnv())
if err != nil {
return err
}

wd.configDir = configDir
wd.tf = tf
Expand Down Expand Up @@ -205,7 +208,10 @@ func (wd *WorkingDir) Init() error {
return fmt.Errorf("must call SetConfig before Init")
}

wd.tf.SetEnv(wd.buildEnv())
err := wd.tf.SetEnv(wd.buildEnv())
if err != nil {
return err
}
return wd.tf.Init(context.Background(), tfexec.Reattach(wd.reattachInfo), tfexec.Dir(wd.configDir))
}

Expand All @@ -226,7 +232,10 @@ func (wd *WorkingDir) planFilename() string {
// CreatePlan runs "terraform plan" to create a saved plan file, which if successful
// will then be used for the next call to Apply.
func (wd *WorkingDir) CreatePlan() error {
wd.tf.SetEnv(wd.buildEnv())
err := wd.tf.SetEnv(wd.buildEnv())
if err != nil {
return err
}
_, err := wd.tf.Plan(context.Background(), tfexec.Reattach(wd.reattachInfo), tfexec.Refresh(false), tfexec.Out("tfplan"), tfexec.Dir(wd.configDir))
return err
}
Expand All @@ -244,7 +253,10 @@ func (wd *WorkingDir) RequireCreatePlan(t TestControl) {
// CreateDestroyPlan runs "terraform plan -destroy" to create a saved plan
// file, which if successful will then be used for the next call to Apply.
func (wd *WorkingDir) CreateDestroyPlan() error {
wd.tf.SetEnv(wd.buildEnv())
err := wd.tf.SetEnv(wd.buildEnv())
if err != nil {
return err
}
_, err := wd.tf.Plan(context.Background(), tfexec.Reattach(wd.reattachInfo), tfexec.Refresh(false), tfexec.Out("tfplan"), tfexec.Destroy(true), tfexec.Dir(wd.configDir))
return err
}
Expand All @@ -268,7 +280,10 @@ func (wd *WorkingDir) Apply() error {
}
args = append(args, tfexec.DirOrPlan(configDir))
}
wd.tf.SetEnv(wd.buildEnv())
err := wd.tf.SetEnv(wd.buildEnv())
if err != nil {
return err
}
return wd.tf.Apply(context.Background(), args...)
}

Expand All @@ -288,7 +303,10 @@ func (wd *WorkingDir) RequireApply(t TestControl) {
// If destroy fails then remote objects might still exist, and continue to
// exist after a particular test is concluded.
func (wd *WorkingDir) Destroy() error {
wd.tf.SetEnv(wd.buildEnv())
err := wd.tf.SetEnv(wd.buildEnv())
if err != nil {
return err
}
return wd.tf.Destroy(context.Background(), tfexec.Reattach(wd.reattachInfo), tfexec.Refresh(false), tfexec.Dir(wd.configDir))
}

Expand Down Expand Up @@ -322,7 +340,10 @@ func (wd *WorkingDir) SavedPlan() (*tfjson.Plan, error) {
return nil, fmt.Errorf("there is no current saved plan")
}

wd.tf.SetEnv(wd.buildEnv())
err := wd.tf.SetEnv(wd.buildEnv())
if err != nil {
return nil, err
}
return wd.tf.ShowPlanFile(context.Background(), wd.planFilename(), tfexec.Reattach(wd.reattachInfo))
}

Expand All @@ -349,10 +370,13 @@ func (wd *WorkingDir) SavedPlanStdout() (string, error) {

var ret bytes.Buffer

wd.tf.SetEnv(wd.buildEnv())
err := wd.tf.SetEnv(wd.buildEnv())
if err != nil {
return "", err
}
wd.tf.SetStdout(&ret)
defer wd.tf.SetStdout(ioutil.Discard)
_, err := wd.tf.ShowPlanFile(context.Background(), wd.planFilename(), tfexec.Reattach(wd.reattachInfo))
_, err = wd.tf.ShowPlanFile(context.Background(), wd.planFilename(), tfexec.Reattach(wd.reattachInfo))
if err != nil {
return "", err
}
Expand All @@ -376,7 +400,10 @@ func (wd *WorkingDir) RequireSavedPlanStdout(t TestControl) string {
//
// If the state cannot be read, State returns an error.
func (wd *WorkingDir) State() (*tfjson.State, error) {
wd.tf.SetEnv(wd.buildEnv())
err := wd.tf.SetEnv(wd.buildEnv())
if err != nil {
return nil, err
}
return wd.tf.Show(context.Background(), tfexec.Reattach(wd.reattachInfo))
}

Expand All @@ -394,7 +421,10 @@ func (wd *WorkingDir) RequireState(t TestControl) *tfjson.State {

// Import runs terraform import
func (wd *WorkingDir) Import(resource, id string) error {
wd.tf.SetEnv(wd.buildEnv())
err := wd.tf.SetEnv(wd.buildEnv())
if err != nil {
return err
}
return wd.tf.Import(context.Background(), resource, id, tfexec.Config(wd.configDir), tfexec.Reattach(wd.reattachInfo))
}

Expand All @@ -410,7 +440,10 @@ func (wd *WorkingDir) RequireImport(t TestControl, resource, id string) {

// Refresh runs terraform refresh
func (wd *WorkingDir) Refresh() error {
wd.tf.SetEnv(wd.buildEnv())
err := wd.tf.SetEnv(wd.buildEnv())
if err != nil {
return err
}
return wd.tf.Refresh(context.Background(), tfexec.Reattach(wd.reattachInfo), tfexec.State(filepath.Join(wd.baseDir, "terraform.tfstate")), tfexec.Dir(wd.configDir))
}

Expand All @@ -428,7 +461,10 @@ func (wd *WorkingDir) RequireRefresh(t TestControl) {
//
// If the schemas cannot be read, Schemas returns an error.
func (wd *WorkingDir) Schemas() (*tfjson.ProviderSchemas, error) {
wd.tf.SetEnv(wd.buildEnv())
err := wd.tf.SetEnv(wd.buildEnv())
if err != nil {
return err
}
return wd.tf.ProvidersSchema(context.Background())
}

Expand Down

0 comments on commit dec3708

Please sign in to comment.