2016-04-12 20:44:12 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2019-01-08 14:57:52 +01:00
|
|
|
"context"
|
2016-04-12 20:44:12 +02:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2018-10-25 15:41:00 +02:00
|
|
|
"github.com/hashicorp/terraform/addrs"
|
2019-01-08 14:57:52 +01:00
|
|
|
"github.com/hashicorp/terraform/command/clistate"
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
"github.com/hashicorp/terraform/states"
|
2016-04-12 20:44:12 +02:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StateMvCommand is a Command implementation that shows a single resource.
|
|
|
|
type StateMvCommand struct {
|
|
|
|
StateMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *StateMvCommand) Run(args []string) int {
|
2017-03-08 05:09:48 +01:00
|
|
|
args, err := c.Meta.process(args, true)
|
|
|
|
if err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
2016-04-12 20:44:12 +02:00
|
|
|
|
2016-04-12 23:17:59 +02:00
|
|
|
// We create two metas to track the two states
|
2017-07-26 19:08:09 +02:00
|
|
|
var backupPathOut, statePathOut string
|
|
|
|
|
2018-10-25 15:41:00 +02:00
|
|
|
var dryRun bool
|
2018-11-21 15:35:27 +01:00
|
|
|
cmdFlags := c.Meta.defaultFlagSet("state mv")
|
2018-10-25 15:41:00 +02:00
|
|
|
cmdFlags.BoolVar(&dryRun, "dry-run", false, "dry run")
|
2017-07-26 19:08:09 +02:00
|
|
|
cmdFlags.StringVar(&c.backupPath, "backup", "-", "backup")
|
|
|
|
cmdFlags.StringVar(&backupPathOut, "backup-out", "-", "backup")
|
2018-11-21 15:35:27 +01:00
|
|
|
cmdFlags.BoolVar(&c.Meta.stateLock, "lock", true, "lock states")
|
|
|
|
cmdFlags.DurationVar(&c.Meta.stateLockTimeout, "lock-timeout", 0, "lock timeout")
|
|
|
|
cmdFlags.StringVar(&c.statePath, "state", "", "path")
|
2017-07-26 19:08:09 +02:00
|
|
|
cmdFlags.StringVar(&statePathOut, "state-out", "", "path")
|
2016-04-12 20:44:12 +02:00
|
|
|
if err := cmdFlags.Parse(args); err != nil {
|
|
|
|
return cli.RunResultHelp
|
|
|
|
}
|
|
|
|
args = cmdFlags.Args()
|
|
|
|
if len(args) != 2 {
|
|
|
|
c.Ui.Error("Exactly two arguments expected.\n")
|
|
|
|
return cli.RunResultHelp
|
|
|
|
}
|
|
|
|
|
2016-04-12 23:17:59 +02:00
|
|
|
// Read the from state
|
2018-10-25 15:41:00 +02:00
|
|
|
stateFromMgr, err := c.State()
|
2016-04-12 20:44:12 +02:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(errStateLoadingState, err))
|
2017-07-27 20:10:52 +02:00
|
|
|
return 1
|
2016-04-12 20:44:12 +02:00
|
|
|
}
|
2019-01-08 14:57:52 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-10-25 15:41:00 +02:00
|
|
|
if err := stateFromMgr.RefreshState(); err != nil {
|
2019-01-08 14:57:52 +01:00
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to refresh source state: %s", err))
|
2016-04-12 20:44:12 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2018-10-25 15:41:00 +02:00
|
|
|
stateFrom := stateFromMgr.State()
|
|
|
|
if stateFrom == nil {
|
2016-04-12 20:44:12 +02:00
|
|
|
c.Ui.Error(fmt.Sprintf(errStateNotFound))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-04-12 23:17:59 +02:00
|
|
|
// Read the destination state
|
2018-10-25 15:41:00 +02:00
|
|
|
stateToMgr := stateFromMgr
|
2016-04-12 23:17:59 +02:00
|
|
|
stateTo := stateFrom
|
2017-07-26 19:08:09 +02:00
|
|
|
|
|
|
|
if statePathOut != "" {
|
|
|
|
c.statePath = statePathOut
|
|
|
|
c.backupPath = backupPathOut
|
2018-10-25 15:41:00 +02:00
|
|
|
|
|
|
|
stateToMgr, err = c.State()
|
2016-04-12 23:17:59 +02:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(errStateLoadingState, err))
|
2017-07-27 20:10:52 +02:00
|
|
|
return 1
|
2016-04-12 23:17:59 +02:00
|
|
|
}
|
2019-01-08 14:57:52 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-10-25 15:41:00 +02:00
|
|
|
if err := stateToMgr.RefreshState(); err != nil {
|
2019-01-08 14:57:52 +01:00
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to refresh destination state: %s", err))
|
2017-02-22 05:35:43 +01:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2018-10-25 15:41:00 +02:00
|
|
|
stateTo = stateToMgr.State()
|
|
|
|
if stateTo == nil {
|
|
|
|
stateTo = states.NewState()
|
2016-04-12 23:17:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-25 15:41:00 +02:00
|
|
|
// Filter what we are moving.
|
|
|
|
results, err := c.filter(stateFrom, []string{args[0]})
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(errStateFilter, err))
|
|
|
|
return cli.RunResultHelp
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have no results, exit early as we're not going to do anything.
|
|
|
|
if len(results) == 0 {
|
|
|
|
if dryRun {
|
|
|
|
c.Ui.Output("Would have moved nothing.")
|
|
|
|
} else {
|
|
|
|
c.Ui.Output("No matching objects found.")
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
}
|
2018-10-25 15:41:00 +02:00
|
|
|
return 0
|
|
|
|
}
|
2016-04-12 20:44:12 +02:00
|
|
|
|
2018-10-25 15:41:00 +02:00
|
|
|
prefix := "Move"
|
|
|
|
if dryRun {
|
|
|
|
prefix = "Would move"
|
|
|
|
}
|
2016-04-12 20:44:12 +02:00
|
|
|
|
2018-10-25 15:41:00 +02:00
|
|
|
var moved int
|
|
|
|
ssFrom := stateFrom.SyncWrapper()
|
|
|
|
for _, result := range c.moveableResult(results) {
|
|
|
|
switch addrFrom := result.Address.(type) {
|
|
|
|
case addrs.ModuleInstance:
|
|
|
|
search, err := addrs.ParseModuleInstanceStr(args[0])
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(errStateMv, err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
addrTo, err := addrs.ParseModuleInstanceStr(args[1])
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(errStateMv, err))
|
|
|
|
return 1
|
|
|
|
}
|
2016-04-12 20:44:12 +02:00
|
|
|
|
2018-10-25 15:41:00 +02:00
|
|
|
if len(search) < len(addrFrom) {
|
|
|
|
addrTo = append(addrTo, addrFrom[len(search):]...)
|
|
|
|
}
|
2016-04-12 20:44:12 +02:00
|
|
|
|
2018-10-25 15:41:00 +02:00
|
|
|
if stateTo.Module(addrTo) != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(errStateMv, "destination module already exists"))
|
|
|
|
return 1
|
|
|
|
}
|
2016-04-12 23:17:59 +02:00
|
|
|
|
2018-10-25 15:41:00 +02:00
|
|
|
moved++
|
|
|
|
c.Ui.Output(fmt.Sprintf("%s %q to %q", prefix, addrFrom.String(), addrTo.String()))
|
|
|
|
if !dryRun {
|
|
|
|
ssFrom.RemoveModule(addrFrom)
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
|
2018-10-25 15:41:00 +02:00
|
|
|
// Update the address before adding it to the state.
|
|
|
|
m := result.Value.(*states.Module)
|
|
|
|
m.Addr = addrTo
|
|
|
|
stateTo.Modules[addrTo.String()] = m
|
|
|
|
}
|
|
|
|
|
|
|
|
case addrs.AbsResource:
|
|
|
|
addrTo, err := addrs.ParseAbsResourceStr(args[1])
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(errStateMv, err))
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2018-10-25 15:41:00 +02:00
|
|
|
if addrFrom.Resource.Type != addrTo.Resource.Type {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
errStateMv, "resource types do not match"))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if stateTo.Module(addrTo.Module) == nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
errStateMv, "destination module does not exist"))
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
return 1
|
|
|
|
}
|
2018-10-25 15:41:00 +02:00
|
|
|
if stateTo.Resource(addrTo) != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
errStateMv, "destination resource already exists"))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
moved++
|
|
|
|
c.Ui.Output(fmt.Sprintf("%s %q to %q", prefix, addrFrom.String(), addrTo.String()))
|
|
|
|
if !dryRun {
|
|
|
|
ssFrom.RemoveResource(addrFrom)
|
|
|
|
|
|
|
|
// Update the address before adding it to the state.
|
|
|
|
rs := result.Value.(*states.Resource)
|
|
|
|
rs.Addr = addrTo.Resource
|
|
|
|
stateTo.Module(addrTo.Module).Resources[addrTo.Resource.String()] = rs
|
|
|
|
}
|
|
|
|
|
|
|
|
case addrs.AbsResourceInstance:
|
|
|
|
addrTo, err := addrs.ParseAbsResourceInstanceStr(args[1])
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(errStateMv, err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if stateTo.Module(addrTo.Module) == nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
errStateMv, "destination module does not exist"))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if stateTo.Resource(addrTo.ContainingResource()) == nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
errStateMv, "destination resource does not exist"))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if stateTo.ResourceInstance(addrTo) != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
errStateMv, "destination resource instance already exists"))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
moved++
|
|
|
|
c.Ui.Output(fmt.Sprintf("%s %q to %q", prefix, addrFrom.String(), args[1]))
|
|
|
|
if !dryRun {
|
|
|
|
ssFrom.ForgetResourceInstanceAll(addrFrom)
|
|
|
|
ssFrom.RemoveResourceIfEmpty(addrFrom.ContainingResource())
|
|
|
|
|
|
|
|
rs := stateTo.Resource(addrTo.ContainingResource())
|
|
|
|
rs.Instances[addrTo.Resource.Key] = result.Value.(*states.ResourceInstance)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if dryRun {
|
|
|
|
if moved == 0 {
|
|
|
|
c.Ui.Output("Would have moved nothing.")
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
}
|
2018-10-25 15:41:00 +02:00
|
|
|
return 0 // This is as far as we go in dry-run mode
|
|
|
|
}
|
2016-04-12 23:17:59 +02:00
|
|
|
|
2018-10-25 15:41:00 +02:00
|
|
|
// Write the new state
|
|
|
|
if err := stateToMgr.WriteState(stateTo); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(errStateRmPersist, err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if err := stateToMgr.PersistState(); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(errStateRmPersist, err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the old state if it is different
|
|
|
|
if stateTo != stateFrom {
|
|
|
|
if err := stateFromMgr.WriteState(stateFrom); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(errStateRmPersist, err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if err := stateFromMgr.PersistState(); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(errStateRmPersist, err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if moved == 0 {
|
|
|
|
c.Ui.Output("No matching objects found.")
|
|
|
|
} else {
|
|
|
|
c.Ui.Output(fmt.Sprintf("Successfully moved %d object(s).", moved))
|
|
|
|
}
|
2016-04-12 20:44:12 +02:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2018-10-25 15:41:00 +02:00
|
|
|
// moveableResult takes the result from a filter operation and returns what
|
|
|
|
// object(s) to move. The reason we do this is because in the module case
|
2016-08-18 23:39:07 +02:00
|
|
|
// we must add the list of all modules returned versus just the root module.
|
2018-10-25 15:41:00 +02:00
|
|
|
func (c *StateMvCommand) moveableResult(results []*states.FilterResult) []*states.FilterResult {
|
|
|
|
result := results[:1]
|
|
|
|
|
|
|
|
if len(results) > 1 {
|
|
|
|
// If a state module then we should add the full list of modules.
|
|
|
|
if _, ok := result[0].Address.(addrs.ModuleInstance); ok {
|
2016-08-18 23:39:07 +02:00
|
|
|
for _, r := range results[1:] {
|
2018-10-25 15:41:00 +02:00
|
|
|
if _, ok := r.Address.(addrs.ModuleInstance); ok {
|
|
|
|
result = append(result, r)
|
2016-08-18 23:39:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-25 15:41:00 +02:00
|
|
|
|
|
|
|
return result
|
2016-08-18 23:39:07 +02:00
|
|
|
}
|
|
|
|
|
2016-04-12 20:44:12 +02:00
|
|
|
func (c *StateMvCommand) Help() string {
|
|
|
|
helpText := `
|
2017-07-27 22:03:21 +02:00
|
|
|
Usage: terraform state mv [options] SOURCE DESTINATION
|
2016-04-12 20:44:12 +02:00
|
|
|
|
2017-07-27 22:03:21 +02:00
|
|
|
This command will move an item matched by the address given to the
|
|
|
|
destination address. This command can also move to a destination address
|
|
|
|
in a completely different state file.
|
2016-04-12 20:44:12 +02:00
|
|
|
|
2017-07-27 22:03:21 +02:00
|
|
|
This can be used for simple resource renaming, moving items to and from
|
|
|
|
a module, moving entire modules, and more. And because this command can also
|
|
|
|
move data to a completely new state, it can also be used for refactoring
|
|
|
|
one configuration into multiple separately managed Terraform configurations.
|
2016-04-12 20:44:12 +02:00
|
|
|
|
2017-07-27 22:03:21 +02:00
|
|
|
This command will output a backup copy of the state prior to saving any
|
|
|
|
changes. The backup cannot be disabled. Due to the destructive nature
|
|
|
|
of this command, backups are required.
|
2016-04-12 20:44:12 +02:00
|
|
|
|
2017-07-27 22:03:21 +02:00
|
|
|
If you're moving an item to a different state file, a backup will be created
|
|
|
|
for each state file.
|
2016-04-12 20:44:12 +02:00
|
|
|
|
|
|
|
Options:
|
|
|
|
|
2018-10-25 15:41:00 +02:00
|
|
|
-dry-run If set, prints out what would've been moved but doesn't
|
|
|
|
actually move anything.
|
|
|
|
|
2016-04-12 23:17:59 +02:00
|
|
|
-backup=PATH Path where Terraform should write the backup for the original
|
2016-04-12 20:44:12 +02:00
|
|
|
state. This can't be disabled. If not set, Terraform
|
|
|
|
will write it to the same path as the statefile with
|
2017-07-27 22:03:21 +02:00
|
|
|
a ".backup" extension.
|
2016-04-12 20:44:12 +02:00
|
|
|
|
2016-04-12 23:17:59 +02:00
|
|
|
-backup-out=PATH Path where Terraform should write the backup for the destination
|
|
|
|
state. This can't be disabled. If not set, Terraform
|
|
|
|
will write it to the same path as the destination state
|
|
|
|
file with a backup extension. This only needs
|
|
|
|
to be specified if -state-out is set to a different path
|
|
|
|
than -state.
|
|
|
|
|
2018-11-21 15:35:27 +01:00
|
|
|
-lock=true Lock the state files when locking is supported.
|
|
|
|
|
|
|
|
-lock-timeout=0s Duration to retry a state lock.
|
|
|
|
|
2017-07-27 22:03:21 +02:00
|
|
|
-state=PATH Path to the source state file. Defaults to the configured
|
|
|
|
backend, or "terraform.tfstate"
|
2016-04-12 20:44:12 +02:00
|
|
|
|
2017-07-27 22:03:21 +02:00
|
|
|
-state-out=PATH Path to the destination state file to write to. If this
|
|
|
|
isn't specified, the source state file will be used. This
|
|
|
|
can be a new or existing path.
|
2016-04-12 20:44:12 +02:00
|
|
|
|
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *StateMvCommand) Synopsis() string {
|
|
|
|
return "Move an item in the state"
|
|
|
|
}
|
|
|
|
|
2018-10-25 15:41:00 +02:00
|
|
|
const errStateMv = `Error moving state: %s
|
2016-04-12 20:44:12 +02:00
|
|
|
|
|
|
|
Please ensure your addresses and state paths are valid. No
|
|
|
|
state was persisted. Your existing states are untouched.`
|
|
|
|
|
|
|
|
const errStateMvPersist = `Error saving the state: %s
|
|
|
|
|
|
|
|
The state wasn't saved properly. If the error happening after a partial
|
|
|
|
write occurred, a backup file will have been created. Otherwise, the state
|
|
|
|
is in the same state it was when the operation started.`
|