2016-03-31 18:29:39 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StateRmCommand is a Command implementation that shows a single resource.
|
|
|
|
type StateRmCommand struct {
|
|
|
|
StateMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *StateRmCommand) 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-03-31 18:29:39 +02:00
|
|
|
|
|
|
|
cmdFlags := c.Meta.flagSet("state show")
|
2017-07-26 19:08:09 +02:00
|
|
|
cmdFlags.StringVar(&c.backupPath, "backup", "-", "backup")
|
|
|
|
cmdFlags.StringVar(&c.statePath, "state", "", "path")
|
2016-03-31 18:29:39 +02:00
|
|
|
if err := cmdFlags.Parse(args); err != nil {
|
|
|
|
return cli.RunResultHelp
|
|
|
|
}
|
|
|
|
args = cmdFlags.Args()
|
|
|
|
|
2017-07-05 23:58:08 +02:00
|
|
|
if len(args) < 1 {
|
|
|
|
c.Ui.Error("At least one resource address is required.")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-07-26 19:08:09 +02:00
|
|
|
state, err := c.State()
|
2016-03-31 18:29:39 +02:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(errStateLoadingState, err))
|
2017-07-27 20:10:52 +02:00
|
|
|
return 1
|
2016-03-31 18:29:39 +02:00
|
|
|
}
|
2017-02-22 05:35:43 +01:00
|
|
|
if err := state.RefreshState(); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
2016-03-31 18:29:39 +02:00
|
|
|
|
|
|
|
stateReal := state.State()
|
|
|
|
if stateReal == nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(errStateNotFound))
|
|
|
|
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
|
|
|
c.Ui.Error("state rm not yet updated for new state types")
|
|
|
|
return 1
|
|
|
|
|
|
|
|
/*
|
|
|
|
if err := stateReal.Remove(args...); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(errStateRm, err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
*/
|
2016-03-31 18:29:39 +02:00
|
|
|
|
2017-09-20 21:48:27 +02:00
|
|
|
c.Ui.Output(fmt.Sprintf("%d items removed.", len(args)))
|
|
|
|
|
2016-03-31 18:29:39 +02:00
|
|
|
if err := state.WriteState(stateReal); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(errStateRmPersist, err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := state.PersistState(); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(errStateRmPersist, err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output("Item removal successful.")
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *StateRmCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: terraform state rm [options] ADDRESS...
|
|
|
|
|
|
|
|
Remove one or more items from the Terraform state.
|
|
|
|
|
|
|
|
This command removes one or more items from the Terraform state based
|
|
|
|
on the address given. You can view and list the available resources
|
|
|
|
with "terraform state list".
|
|
|
|
|
|
|
|
This command creates a timestamped backup of the state on every invocation.
|
|
|
|
This can't be disabled. Due to the destructive nature of this command,
|
|
|
|
the backup is ensured by Terraform for safety reasons.
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
|
|
|
-backup=PATH Path where Terraform should write the backup
|
|
|
|
state. This can't be disabled. If not set, Terraform
|
|
|
|
will write it to the same path as the statefile with
|
2017-06-23 20:46:09 +02:00
|
|
|
a backup extension.
|
2016-03-31 18:29:39 +02:00
|
|
|
|
2017-08-09 20:03:06 +02:00
|
|
|
-state=PATH Path to the source state file. Defaults to the configured
|
|
|
|
backend, or "terraform.tfstate"
|
2016-03-31 18:29:39 +02:00
|
|
|
|
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *StateRmCommand) Synopsis() string {
|
|
|
|
return "Remove an item from the state"
|
|
|
|
}
|
|
|
|
|
|
|
|
const errStateRm = `Error removing items from the state: %s
|
|
|
|
|
|
|
|
The state was not saved. No items were removed from the persisted
|
|
|
|
state. No backup was created since no modification occurred. Please
|
|
|
|
resolve the issue above and try again.`
|
|
|
|
|
|
|
|
const errStateRmPersist = `Error saving the state: %s
|
|
|
|
|
|
|
|
The state was not saved. No items were removed from the persisted
|
|
|
|
state. No backup was created since no modification occurred. Please
|
|
|
|
resolve the issue above and try again.`
|