2016-04-12 20:44:12 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
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/hashicorp/terraform/terraform"
|
|
|
|
"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
|
|
|
|
|
2016-06-22 10:46:38 +02:00
|
|
|
cmdFlags := c.Meta.flagSet("state mv")
|
2017-07-26 19:08:09 +02:00
|
|
|
cmdFlags.StringVar(&c.backupPath, "backup", "-", "backup")
|
|
|
|
cmdFlags.StringVar(&c.statePath, "state", "", "path")
|
|
|
|
cmdFlags.StringVar(&backupPathOut, "backup-out", "-", "backup")
|
|
|
|
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
|
2017-07-26 19:08:09 +02:00
|
|
|
stateFrom, 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
|
|
|
}
|
|
|
|
|
2017-02-22 22:01:16 +01:00
|
|
|
if err := stateFrom.RefreshState(); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
|
2016-04-12 20:44:12 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-04-12 23:17:59 +02:00
|
|
|
stateFromReal := stateFrom.State()
|
|
|
|
if stateFromReal == 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
|
|
|
|
stateTo := stateFrom
|
|
|
|
stateToReal := stateFromReal
|
2017-07-26 19:08:09 +02:00
|
|
|
|
|
|
|
if statePathOut != "" {
|
|
|
|
c.statePath = statePathOut
|
|
|
|
c.backupPath = backupPathOut
|
|
|
|
stateTo, 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
|
|
|
}
|
|
|
|
|
2017-02-22 05:35:43 +01:00
|
|
|
if err := stateTo.RefreshState(); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-04-12 23:17:59 +02:00
|
|
|
stateToReal = stateTo.State()
|
|
|
|
if stateToReal == nil {
|
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
|
|
|
stateToReal = states.NewState()
|
2016-04-12 23:17:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
c.Ui.Error("state mv command not yet updated for new state types")
|
|
|
|
/*
|
|
|
|
// Filter what we're moving
|
|
|
|
filter := &terraform.StateFilter{State: stateFromReal}
|
|
|
|
results, err := filter.Filter(args[0])
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(errStateMv, err))
|
|
|
|
return cli.RunResultHelp
|
|
|
|
}
|
|
|
|
if len(results) == 0 {
|
|
|
|
c.Ui.Output(fmt.Sprintf("Item to move doesn't exist: %s", args[0]))
|
|
|
|
return 1
|
|
|
|
}
|
2016-04-12 20:44:12 +02:00
|
|
|
|
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
|
|
|
// Get the item to add to the state
|
|
|
|
add := c.addableResult(results)
|
2016-04-12 20:44:12 +02:00
|
|
|
|
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
|
|
|
// Do the actual move
|
|
|
|
if err := stateFromReal.Remove(args[0]); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(errStateMv, err))
|
|
|
|
return 1
|
|
|
|
}
|
2016-04-12 20:44:12 +02:00
|
|
|
|
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
|
|
|
if err := stateToReal.Add(args[0], args[1], add); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(errStateMv, err))
|
|
|
|
return 1
|
|
|
|
}
|
2016-04-12 20:44:12 +02:00
|
|
|
|
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
|
|
|
// Write the new state
|
|
|
|
if err := stateTo.WriteState(stateToReal); err != nil {
|
2016-04-12 23:17:59 +02:00
|
|
|
c.Ui.Error(fmt.Sprintf(errStateMvPersist, err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
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
|
|
|
if err := stateTo.PersistState(); err != nil {
|
2016-04-12 23:17:59 +02:00
|
|
|
c.Ui.Error(fmt.Sprintf(errStateMvPersist, err))
|
|
|
|
return 1
|
|
|
|
}
|
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
|
|
|
|
|
|
|
// Write the old state if it is different
|
|
|
|
if stateTo != stateFrom {
|
|
|
|
if err := stateFrom.WriteState(stateFromReal); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(errStateMvPersist, err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := stateFrom.PersistState(); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(errStateMvPersist, err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2016-04-12 23:17:59 +02:00
|
|
|
|
2016-04-12 20:44:12 +02:00
|
|
|
c.Ui.Output(fmt.Sprintf(
|
|
|
|
"Moved %s to %s", args[0], args[1]))
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2016-08-18 23:39:07 +02:00
|
|
|
// addableResult takes the result from a filter operation and returns what to
|
2017-04-26 16:10:04 +02:00
|
|
|
// call State.Add with. 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.
|
|
|
|
func (c *StateMvCommand) addableResult(results []*terraform.StateFilterResult) interface{} {
|
|
|
|
switch v := results[0].Value.(type) {
|
|
|
|
case *terraform.ModuleState:
|
|
|
|
// If a module state then we should add the full list of modules
|
|
|
|
result := []*terraform.ModuleState{v}
|
|
|
|
if len(results) > 1 {
|
|
|
|
for _, r := range results[1:] {
|
|
|
|
if ms, ok := r.Value.(*terraform.ModuleState); ok {
|
|
|
|
result = append(result, ms)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
2016-08-19 18:05:20 +02:00
|
|
|
|
|
|
|
case *terraform.ResourceState:
|
2016-08-20 05:56:27 +02:00
|
|
|
// If a resource state with more than one result, it has a multi-count
|
|
|
|
// and we need to add all of them.
|
2016-08-19 18:05:20 +02:00
|
|
|
result := []*terraform.ResourceState{v}
|
|
|
|
if len(results) > 1 {
|
|
|
|
for _, r := range results[1:] {
|
|
|
|
rs, ok := r.Value.(*terraform.ResourceState)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Type == v.Type {
|
|
|
|
result = append(result, rs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-19 18:09:19 +02:00
|
|
|
// If we only have one item, add it directly
|
|
|
|
if len(result) == 1 {
|
|
|
|
return result[0]
|
|
|
|
}
|
|
|
|
|
2016-08-19 18:05:20 +02:00
|
|
|
return result
|
|
|
|
|
2016-08-18 23:39:07 +02:00
|
|
|
default:
|
|
|
|
// By default just add the first result
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
|
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.
|
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
|
|
|
const errStateMv = `Error moving state: %[1]s
|
|
|
|
|
|
|
|
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.`
|