diff --git a/command/state_mv.go b/command/state_mv.go index 05fff8c7b..3c871a7f6 100644 --- a/command/state_mv.go +++ b/command/state_mv.go @@ -1,10 +1,12 @@ package command import ( + "context" "fmt" "strings" "github.com/hashicorp/terraform/addrs" + "github.com/hashicorp/terraform/command/clistate" "github.com/hashicorp/terraform/states" "github.com/mitchellh/cli" ) @@ -47,8 +49,18 @@ func (c *StateMvCommand) Run(args []string) int { c.Ui.Error(fmt.Sprintf(errStateLoadingState, err)) return 1 } + + if c.stateLock { + stateLocker := clistate.NewLocker(context.Background(), c.stateLockTimeout, c.Ui, c.Colorize()) + if err := stateLocker.Lock(stateFromMgr, "state-mv"); err != nil { + c.Ui.Error(fmt.Sprintf("Error locking source state: %s", err)) + return 1 + } + defer stateLocker.Unlock(nil) + } + if err := stateFromMgr.RefreshState(); err != nil { - c.Ui.Error(fmt.Sprintf("Failed to refresh state: %s", err)) + c.Ui.Error(fmt.Sprintf("Failed to refresh source state: %s", err)) return 1 } @@ -71,8 +83,18 @@ func (c *StateMvCommand) Run(args []string) int { c.Ui.Error(fmt.Sprintf(errStateLoadingState, err)) return 1 } + + if c.stateLock { + stateLocker := clistate.NewLocker(context.Background(), c.stateLockTimeout, c.Ui, c.Colorize()) + if err := stateLocker.Lock(stateToMgr, "state-mv"); err != nil { + c.Ui.Error(fmt.Sprintf("Error locking destination state: %s", err)) + return 1 + } + defer stateLocker.Unlock(nil) + } + if err := stateToMgr.RefreshState(); err != nil { - c.Ui.Error(fmt.Sprintf("Failed to refresh state: %s", err)) + c.Ui.Error(fmt.Sprintf("Failed to refresh destination state: %s", err)) return 1 } diff --git a/command/state_push.go b/command/state_push.go index 8de0ad9f7..9a3cf6292 100644 --- a/command/state_push.go +++ b/command/state_push.go @@ -84,7 +84,7 @@ func (c *StatePushCommand) Run(args []string) int { if c.stateLock { stateLocker := clistate.NewLocker(context.Background(), c.stateLockTimeout, c.Ui, c.Colorize()) - if err := stateLocker.Lock(stateMgr, "taint"); err != nil { + if err := stateLocker.Lock(stateMgr, "state-push"); err != nil { c.Ui.Error(fmt.Sprintf("Error locking state: %s", err)) return 1 } diff --git a/command/state_rm.go b/command/state_rm.go index f3965810d..33cce521e 100644 --- a/command/state_rm.go +++ b/command/state_rm.go @@ -1,11 +1,13 @@ package command import ( + "context" "fmt" "sort" "strings" "github.com/hashicorp/terraform/addrs" + "github.com/hashicorp/terraform/command/clistate" "github.com/hashicorp/terraform/states" "github.com/mitchellh/cli" ) @@ -44,6 +46,16 @@ func (c *StateRmCommand) Run(args []string) int { c.Ui.Error(fmt.Sprintf(errStateLoadingState, err)) return 1 } + + if c.stateLock { + stateLocker := clistate.NewLocker(context.Background(), c.stateLockTimeout, c.Ui, c.Colorize()) + if err := stateLocker.Lock(stateMgr, "state-rm"); err != nil { + c.Ui.Error(fmt.Sprintf("Error locking state: %s", err)) + return 1 + } + defer stateLocker.Unlock(nil) + } + if err := stateMgr.RefreshState(); err != nil { c.Ui.Error(fmt.Sprintf("Failed to refresh state: %s", err)) return 1