detect when backend.Hash needs update

It's possible to not change the backend config, but require updating the
stored backend state by moving init options from the config file to the
`-backend-config` flag. If the config is the same, but the hash doesn't
match, update the stored state.
This commit is contained in:
James Bardin 2017-03-29 15:51:24 -04:00
parent ff2d753062
commit c891ab50b7
2 changed files with 54 additions and 0 deletions

View File

@ -464,6 +464,50 @@ func TestInit_backendReinitWithExtra(t *testing.T) {
} }
} }
// move option from config to -backend-config args
func TestInit_backendReinitConfigToExtra(t *testing.T) {
td := tempDir(t)
copy.CopyDir(testFixturePath("init-backend"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
ui := new(cli.MockUi)
c := &InitCommand{
Meta: Meta{
ContextOpts: testCtxConfig(testProvider()),
Ui: ui,
},
}
if code := c.Run([]string{"-input=false"}); code != 0 {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
}
// Read our saved backend config and verify we have our settings
state := testStateRead(t, filepath.Join(DefaultDataDir, DefaultStateFilename))
if v := state.Backend.Config["path"]; v != "foo" {
t.Fatalf("bad: %#v", v)
}
backendHash := state.Backend.Hash
// init again but remove the path option from the config
cfg := "terraform {\n backend \"local\" {}\n}\n"
if err := ioutil.WriteFile("main.tf", []byte(cfg), 0644); err != nil {
t.Fatal(err)
}
args := []string{"-input=false", "-backend-config=path=foo"}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
}
state = testStateRead(t, filepath.Join(DefaultDataDir, DefaultStateFilename))
if state.Backend.Hash == backendHash {
t.Fatal("state.Backend.Hash was not updated")
}
}
/* /*
func TestInit_remoteState(t *testing.T) { func TestInit_remoteState(t *testing.T) {
tmp, cwd := testCwd(t) tmp, cwd := testCwd(t)

View File

@ -1158,6 +1158,16 @@ func (m *Meta) backend_C_r_S_unchanged(
c *config.Backend, sMgr state.State) (backend.Backend, error) { c *config.Backend, sMgr state.State) (backend.Backend, error) {
s := sMgr.State() s := sMgr.State()
// it's possible for a backend to be unchanged, and the config itself to
// have changed by moving a paramter from the config to `-backend-config`
// In this case we only need to update the Hash.
if c != nil && s.Backend.Hash != c.Hash {
s.Backend.Hash = c.Hash
if err := sMgr.WriteState(s); err != nil {
return nil, fmt.Errorf(errBackendWriteSaved, err)
}
}
// Create the config. We do this from the backend state since this // Create the config. We do this from the backend state since this
// has the complete configuration data whereas the config itself // has the complete configuration data whereas the config itself
// may require input. // may require input.