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

fix: locals.tf.json getting overwritten #746

Merged
merged 1 commit into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,16 +219,26 @@ func applyTFE(fs afero.Fs, plan *plan.Plan, tmpl *templates.T) error {
}

logrus.Debug("applying tfe")
path := fmt.Sprintf("%s/tfe", rootPath)
path := filepath.Join(rootPath, "tfe")
err := fs.MkdirAll(path, 0755)
if err != nil {
return errors.Wrapf(err, "unable to make directory %s", path)
}
err = applyTree(fs, tmpl.Components[v2.ComponentKindTerraform], tmpl.Common, path, plan.TFE)

// only create the locals.tf.json template is one doesn't exist yet
tfePath := filepath.Join(rootPath, "tfe", "locals.tf.json")
Comment on lines +228 to +229
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could join with path since that already joined the first 2 items:

Suggested change
// only create the locals.tf.json template is one doesn't exist yet
tfePath := filepath.Join(rootPath, "tfe", "locals.tf.json")
// only create the locals.tf.json template if one doesn't exist yet
tfePath := filepath.Join(path, "locals.tf.json")

_, err = fs.Stat(tfePath)
if err != nil {
return err
if errors.Is(err, os.ErrNotExist) {
err = applyTree(fs, tmpl.TFE, tmpl.Common, path, plan.TFE)
if err != nil {
return err
}
} else {
return errors.Wrapf(err, "unable to stat on %s", tfePath)
}
}
err = applyTree(fs, tmpl.TFE, tmpl.Common, path, plan.TFE)
err = applyTree(fs, tmpl.Components[v2.ComponentKindTerraform], tmpl.Common, path, plan.TFE)
if err != nil {
return err
}
Expand All @@ -243,7 +253,6 @@ func applyTFE(fs afero.Fs, plan *plan.Plan, tmpl *templates.T) error {
}
}

tfePath := filepath.Join("terraform", "tfe", "locals.tf.json")
_, err = fs.Stat(tfePath)
if err != nil {
return errors.Wrapf(err, "unable to stat on %s", tfePath)
Expand Down
111 changes: 111 additions & 0 deletions apply/apply_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package apply

import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
Expand Down Expand Up @@ -429,6 +431,115 @@ func TestApplyModuleInvocation(t *testing.T) {
r.Equal(expected, string(i))
}

func TestTFEConfigOmitEmpty(t *testing.T) {
r := require.New(t)
testFS, d, err := util.TestFs()
r.NoError(err)
defer os.RemoveAll(d)
pwdFS, err := util.PwdFs()
r.NoError(err)
afs := afero.NewCopyOnWriteFs(pwdFS, testFS)

plan := &plan.Plan{
TFE: &plan.TFEConfig{},
Envs: map[string]plan.Env{
"prod": {
Components: map[string]plan.Component{
"test": {},
},
Env: "prod",
},
},
}
existingLocals := LocalsTFE{
Locals: &Locals{
Envs: map[string]map[string]*TFEWorkspace{
"prod": {
"test": &TFEWorkspace{},
},
},
},
}
path := fmt.Sprintf("%s/tfe", rootPath)
err = afs.MkdirAll(path, 0755)
r.NoError(err)
localsPath := filepath.Join("terraform", "tfe", "locals.tf.json")
existingLocalsFile, err := afs.OpenFile(localsPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
r.NoError(err)
err = json.NewEncoder(existingLocalsFile).Encode(&existingLocals)
r.NoError(err)
existingLocalsFile.Close()

err = applyTFE(afs, plan, templates.Templates)
r.NoError(err)
alteredLocalsFile, err := afs.Open(localsPath)
r.NoError(err)
b, err := io.ReadAll(alteredLocalsFile)
r.NoError(err)
r.Equal(
strings.Join(strings.Fields(`{"locals":{"envs":{"prod":{"test":{}}}}}`), ""),
strings.Join(strings.Fields(string(b)), ""))
}

func TestTFEConfig(t *testing.T) {
r := require.New(t)
testFS, d, err := util.TestFs()
r.NoError(err)
defer os.RemoveAll(d)
pwdFS, err := util.PwdFs()
r.NoError(err)
afs := afero.NewCopyOnWriteFs(pwdFS, testFS)

plan := &plan.Plan{
TFE: &plan.TFEConfig{},
Envs: map[string]plan.Env{
"prod": {
Components: map[string]plan.Component{
"test": {},
},
Env: "prod",
},
},
}
emptyTrigger := []string{}
tfVersion := "0.13.5"
existingLocals := LocalsTFE{
Locals: &Locals{
Envs: map[string]map[string]*TFEWorkspace{
"prod": {
"test": &TFEWorkspace{
TriggerPrefixes: &emptyTrigger,
TerraformVersion: &tfVersion,
},
},
},
},
}
path := fmt.Sprintf("%s/tfe", rootPath)
err = afs.MkdirAll(path, 0755)
r.NoError(err)
localsPath := filepath.Join("terraform", "tfe", "locals.tf.json")
existingLocalsFile, err := afs.OpenFile(localsPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
r.NoError(err)
err = json.NewEncoder(existingLocalsFile).Encode(&existingLocals)
r.NoError(err)
existingLocalsFile.Close()

err = applyTFE(afs, plan, templates.Templates)
r.NoError(err)
locals := LocalsTFE{}
alteredLocalsFile, err := afs.Open(localsPath)
r.NoError(err)
err = json.NewDecoder(alteredLocalsFile).Decode(&locals)
r.NoError(err)
_, ok := locals.Locals.Envs["prod"]
r.True(ok)
_, ok = locals.Locals.Envs["prod"]["test"]
r.True(ok)
r.Equal(*existingLocals.Locals.Envs["prod"]["test"].TerraformVersion, *locals.Locals.Envs["prod"]["test"].TerraformVersion)
r.Equal(*existingLocals.Locals.Envs["prod"]["test"].TriggerPrefixes, *locals.Locals.Envs["prod"]["test"].TriggerPrefixes)
}

func TestApplyModuleInvocationWithEmptyVariables(t *testing.T) {
r := require.New(t)
testFs, d, err := util.TestFs()
Expand Down