Merge pull request #12155 from hashicorp/b-state-backend
command: refresh state in old commands for backend
This commit is contained in:
commit
d2d87bccf0
|
@ -5,7 +5,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/hashicorp/errwrap"
|
|
||||||
"github.com/hashicorp/terraform/backend"
|
"github.com/hashicorp/terraform/backend"
|
||||||
"github.com/hashicorp/terraform/helper/schema"
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
"github.com/hashicorp/terraform/state"
|
"github.com/hashicorp/terraform/state"
|
||||||
|
@ -113,11 +112,6 @@ func (b *Local) State() (state.State, error) {
|
||||||
PathOut: b.StateOutPath,
|
PathOut: b.StateOutPath,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load the state as a sanity check
|
|
||||||
if err := s.RefreshState(); err != nil {
|
|
||||||
return nil, errwrap.Wrapf("Error reading local state: {{err}}", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we are backing up the state, wrap it
|
// If we are backing up the state, wrap it
|
||||||
if path := b.StateBackupPath; path != "" {
|
if path := b.StateBackupPath; path != "" {
|
||||||
s = &state.BackupState{
|
s = &state.BackupState{
|
||||||
|
|
|
@ -103,6 +103,9 @@ func TestMetaBackend_emptyWithDefaultState(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("bad: %s", err)
|
t.Fatalf("bad: %s", err)
|
||||||
}
|
}
|
||||||
|
if err := s.RefreshState(); err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
if actual := s.State().String(); actual != testState().String() {
|
if actual := s.State().String(); actual != testState().String() {
|
||||||
t.Fatalf("bad: %s", actual)
|
t.Fatalf("bad: %s", actual)
|
||||||
}
|
}
|
||||||
|
@ -173,6 +176,9 @@ func TestMetaBackend_emptyWithExplicitState(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("bad: %s", err)
|
t.Fatalf("bad: %s", err)
|
||||||
}
|
}
|
||||||
|
if err := s.RefreshState(); err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
if actual := s.State().String(); actual != testState().String() {
|
if actual := s.State().String(); actual != testState().String() {
|
||||||
t.Fatalf("bad: %s", actual)
|
t.Fatalf("bad: %s", actual)
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,6 +38,11 @@ func (c *StateListCommand) Run(args []string) int {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := state.RefreshState(); err != nil {
|
||||||
|
c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
stateReal := state.State()
|
stateReal := state.State()
|
||||||
if stateReal == nil {
|
if stateReal == nil {
|
||||||
c.Ui.Error(fmt.Sprintf(errStateNotFound))
|
c.Ui.Error(fmt.Sprintf(errStateNotFound))
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
package command
|
package command
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hashicorp/terraform/helper/copy"
|
||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -35,6 +37,35 @@ func TestStateList(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStateList_backendState(t *testing.T) {
|
||||||
|
// Create a temporary working directory that is empty
|
||||||
|
td := tempDir(t)
|
||||||
|
copy.CopyDir(testFixturePath("state-list-backend"), td)
|
||||||
|
defer os.RemoveAll(td)
|
||||||
|
defer testChdir(t, td)()
|
||||||
|
|
||||||
|
p := testProvider()
|
||||||
|
ui := new(cli.MockUi)
|
||||||
|
c := &StateListCommand{
|
||||||
|
Meta: Meta{
|
||||||
|
ContextOpts: testCtxConfig(p),
|
||||||
|
Ui: ui,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
args := []string{}
|
||||||
|
if code := c.Run(args); code != 0 {
|
||||||
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test that outputs were displayed
|
||||||
|
expected := "null_resource.a\n"
|
||||||
|
actual := ui.OutputWriter.String()
|
||||||
|
if actual != expected {
|
||||||
|
t.Fatalf("Expected:\n%q\n\nTo equal: %q", actual, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestStateList_noState(t *testing.T) {
|
func TestStateList_noState(t *testing.T) {
|
||||||
tmp, cwd := testCwd(t)
|
tmp, cwd := testCwd(t)
|
||||||
defer testFixCwd(t, tmp, cwd)
|
defer testFixCwd(t, tmp, cwd)
|
||||||
|
|
|
@ -45,6 +45,11 @@ func (c *StateMvCommand) Run(args []string) int {
|
||||||
return cli.RunResultHelp
|
return cli.RunResultHelp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := stateFrom.RefreshState(); err != nil {
|
||||||
|
c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
stateFromReal := stateFrom.State()
|
stateFromReal := stateFrom.State()
|
||||||
if stateFromReal == nil {
|
if stateFromReal == nil {
|
||||||
c.Ui.Error(fmt.Sprintf(errStateNotFound))
|
c.Ui.Error(fmt.Sprintf(errStateNotFound))
|
||||||
|
@ -61,6 +66,11 @@ func (c *StateMvCommand) Run(args []string) int {
|
||||||
return cli.RunResultHelp
|
return cli.RunResultHelp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := stateTo.RefreshState(); err != nil {
|
||||||
|
c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
stateToReal = stateTo.State()
|
stateToReal = stateTo.State()
|
||||||
if stateToReal == nil {
|
if stateToReal == nil {
|
||||||
stateToReal = terraform.NewState()
|
stateToReal = terraform.NewState()
|
||||||
|
|
|
@ -29,6 +29,10 @@ func (c *StateRmCommand) Run(args []string) int {
|
||||||
c.Ui.Error(fmt.Sprintf(errStateLoadingState, err))
|
c.Ui.Error(fmt.Sprintf(errStateLoadingState, err))
|
||||||
return cli.RunResultHelp
|
return cli.RunResultHelp
|
||||||
}
|
}
|
||||||
|
if err := state.RefreshState(); err != nil {
|
||||||
|
c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
stateReal := state.State()
|
stateReal := state.State()
|
||||||
if stateReal == nil {
|
if stateReal == nil {
|
||||||
|
|
|
@ -39,6 +39,10 @@ func (c *StateShowCommand) Run(args []string) int {
|
||||||
c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
|
c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
if err := state.RefreshState(); err != nil {
|
||||||
|
c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
stateReal := state.State()
|
stateReal := state.State()
|
||||||
if stateReal == nil {
|
if stateReal == nil {
|
||||||
|
|
|
@ -72,6 +72,10 @@ func (c *TaintCommand) Run(args []string) int {
|
||||||
c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
|
c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
if err := st.RefreshState(); err != nil {
|
||||||
|
c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
if c.Meta.stateLock {
|
if c.Meta.stateLock {
|
||||||
lockInfo := state.NewLockInfo()
|
lockInfo := state.NewLockInfo()
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
{
|
||||||
|
"version": 3,
|
||||||
|
"serial": 0,
|
||||||
|
"lineage": "666f9301-7e65-4b19-ae23-71184bb19b03",
|
||||||
|
"backend": {
|
||||||
|
"type": "local",
|
||||||
|
"config": {
|
||||||
|
"path": "local-state.tfstate"
|
||||||
|
},
|
||||||
|
"hash": 9073424445967744180
|
||||||
|
},
|
||||||
|
"modules": [
|
||||||
|
{
|
||||||
|
"path": [
|
||||||
|
"root"
|
||||||
|
],
|
||||||
|
"outputs": {},
|
||||||
|
"resources": {},
|
||||||
|
"depends_on": []
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
{
|
||||||
|
"version": 3,
|
||||||
|
"terraform_version": "0.8.2",
|
||||||
|
"serial": 7,
|
||||||
|
"lineage": "configuredUnchanged",
|
||||||
|
"modules": [
|
||||||
|
{
|
||||||
|
"path": [
|
||||||
|
"root"
|
||||||
|
],
|
||||||
|
"outputs": {},
|
||||||
|
"resources": {
|
||||||
|
"null_resource.a": {
|
||||||
|
"type": "null_resource",
|
||||||
|
"depends_on": [],
|
||||||
|
"primary": {
|
||||||
|
"id": "5416263284413907707",
|
||||||
|
"attributes": {
|
||||||
|
"id": "5416263284413907707"
|
||||||
|
},
|
||||||
|
"meta": {},
|
||||||
|
"tainted": false
|
||||||
|
},
|
||||||
|
"deposed": [],
|
||||||
|
"provider": ""
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"depends_on": []
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
terraform {
|
||||||
|
backend "local" {
|
||||||
|
path = "local-state.tfstate"
|
||||||
|
}
|
||||||
|
}
|
|
@ -60,6 +60,10 @@ func (c *UntaintCommand) Run(args []string) int {
|
||||||
c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
|
c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
if err := st.RefreshState(); err != nil {
|
||||||
|
c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
if c.Meta.stateLock {
|
if c.Meta.stateLock {
|
||||||
lockInfo := state.NewLockInfo()
|
lockInfo := state.NewLockInfo()
|
||||||
|
|
|
@ -207,6 +207,7 @@ func (c *Context) Graph(typ GraphType, opts *ContextGraphOpts) (*Graph, error) {
|
||||||
opts = &ContextGraphOpts{Validate: true}
|
opts = &ContextGraphOpts{Validate: true}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Printf("[INFO] terraform: building graph: %s", typ)
|
||||||
switch typ {
|
switch typ {
|
||||||
case GraphTypeApply:
|
case GraphTypeApply:
|
||||||
return (&ApplyGraphBuilder{
|
return (&ApplyGraphBuilder{
|
||||||
|
|
Loading…
Reference in New Issue