2016-03-31 11:29:39 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-10-22 08:52:53 -05:00
|
|
|
"sort"
|
2016-03-31 11:29:39 -05:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/mitchellh/cli"
|
2018-10-15 10:38:06 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
2018-10-22 08:52:53 -05:00
|
|
|
"github.com/hashicorp/terraform/states"
|
2016-03-31 11:29:39 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// StateRmCommand is a Command implementation that shows a single resource.
|
|
|
|
type StateRmCommand struct {
|
|
|
|
StateMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *StateRmCommand) Run(args []string) int {
|
2017-03-07 22:09:48 -06:00
|
|
|
args, err := c.Meta.process(args, true)
|
|
|
|
if err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
2016-03-31 11:29:39 -05:00
|
|
|
|
|
|
|
cmdFlags := c.Meta.flagSet("state show")
|
2017-07-26 12:08:09 -05:00
|
|
|
cmdFlags.StringVar(&c.backupPath, "backup", "-", "backup")
|
|
|
|
cmdFlags.StringVar(&c.statePath, "state", "", "path")
|
2018-10-22 08:52:53 -05:00
|
|
|
dryRun := cmdFlags.Bool("dry-run", false, "dry run")
|
2016-03-31 11:29:39 -05:00
|
|
|
if err := cmdFlags.Parse(args); err != nil {
|
|
|
|
return cli.RunResultHelp
|
|
|
|
}
|
|
|
|
args = cmdFlags.Args()
|
|
|
|
|
2017-07-05 16:58:08 -05:00
|
|
|
if len(args) < 1 {
|
|
|
|
c.Ui.Error("At least one resource address is required.")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2018-10-22 08:52:53 -05:00
|
|
|
// Get the state
|
2018-10-15 10:38:06 -05:00
|
|
|
stateMgr, err := c.State()
|
2016-03-31 11:29:39 -05:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(errStateLoadingState, err))
|
2017-07-27 13:10:52 -05:00
|
|
|
return 1
|
2016-03-31 11:29:39 -05:00
|
|
|
}
|
2018-10-15 10:38:06 -05:00
|
|
|
if err := stateMgr.RefreshState(); err != nil {
|
2018-10-22 08:52:53 -05:00
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to refresh state: %s", err))
|
2017-02-21 22:35:43 -06:00
|
|
|
return 1
|
|
|
|
}
|
2016-03-31 11:29:39 -05:00
|
|
|
|
2018-10-15 10:38:06 -05:00
|
|
|
state := stateMgr.State()
|
|
|
|
if state == nil {
|
2016-03-31 11:29:39 -05:00
|
|
|
c.Ui.Error(fmt.Sprintf(errStateNotFound))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2018-10-22 08:52:53 -05:00
|
|
|
results, err := c.filter(state, args)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(errStateFilter, err))
|
|
|
|
return cli.RunResultHelp
|
2018-10-15 10:38:06 -05: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 16:24:45 -05:00
|
|
|
|
2018-10-22 08:52:53 -05:00
|
|
|
// If we have no results, just exit early, we're not going to do anything.
|
|
|
|
// While what happens below is fairly fast, this is an important early
|
|
|
|
// exit since the prune below might modify the state more and we don't
|
|
|
|
// want to modify the state if we don't have to.
|
|
|
|
if len(results) == 0 {
|
|
|
|
if *dryRun {
|
|
|
|
c.Ui.Output("Would have removed nothing.")
|
|
|
|
} else {
|
|
|
|
c.Ui.Output("No matching resources found.")
|
2018-10-15 10:38:06 -05:00
|
|
|
}
|
2018-10-22 08:52:53 -05:00
|
|
|
return 0
|
2018-10-15 10:38:06 -05:00
|
|
|
}
|
|
|
|
|
2018-10-22 08:52:53 -05:00
|
|
|
prefix := "Remove resource "
|
|
|
|
if *dryRun {
|
|
|
|
prefix = "Would remove resource "
|
2018-10-15 10:38:06 -05:00
|
|
|
}
|
2016-03-31 11:29:39 -05:00
|
|
|
|
2018-10-22 08:52:53 -05:00
|
|
|
var isCount int
|
2018-10-15 10:38:06 -05:00
|
|
|
ss := state.SyncWrapper()
|
2018-10-22 08:52:53 -05:00
|
|
|
for _, result := range results {
|
|
|
|
switch addr := result.Address.(type) {
|
|
|
|
case addrs.ModuleInstance:
|
|
|
|
var output []string
|
|
|
|
for _, rs := range result.Value.(*states.Module).Resources {
|
|
|
|
for k := range rs.Instances {
|
|
|
|
isCount++
|
|
|
|
output = append(output, prefix+rs.Addr.Absolute(addr).Instance(k).String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(output) > 0 {
|
|
|
|
c.Ui.Output(strings.Join(sort.StringSlice(output), "\n"))
|
|
|
|
}
|
|
|
|
if !*dryRun {
|
|
|
|
ss.RemoveModule(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
case addrs.AbsResource:
|
|
|
|
var output []string
|
|
|
|
for k := range result.Value.(*states.Resource).Instances {
|
|
|
|
isCount++
|
|
|
|
output = append(output, prefix+addr.Instance(k).String())
|
|
|
|
}
|
|
|
|
if len(output) > 0 {
|
|
|
|
c.Ui.Output(strings.Join(sort.StringSlice(output), "\n"))
|
|
|
|
}
|
|
|
|
if !*dryRun {
|
|
|
|
ss.RemoveResource(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
case addrs.AbsResourceInstance:
|
|
|
|
isCount++
|
|
|
|
c.Ui.Output(prefix + addr.String())
|
|
|
|
if !*dryRun {
|
|
|
|
ss.ForgetResourceInstanceAll(addr)
|
|
|
|
}
|
|
|
|
}
|
2018-10-15 10:38:06 -05:00
|
|
|
}
|
|
|
|
|
2018-10-22 08:52:53 -05:00
|
|
|
if *dryRun {
|
|
|
|
if isCount == 0 {
|
|
|
|
c.Ui.Output("Would have removed nothing.")
|
|
|
|
}
|
|
|
|
return 0 // This is as far as we go in dry-run mode
|
2018-10-15 10:38:06 -05:00
|
|
|
}
|
2017-09-20 14:48:27 -05:00
|
|
|
|
2018-10-22 08:52:53 -05:00
|
|
|
// Prune the state before writing and persisting it.
|
|
|
|
state.PruneResourceHusks()
|
|
|
|
|
2018-10-15 10:38:06 -05:00
|
|
|
if err := stateMgr.WriteState(state); err != nil {
|
2016-03-31 11:29:39 -05:00
|
|
|
c.Ui.Error(fmt.Sprintf(errStateRmPersist, err))
|
|
|
|
return 1
|
|
|
|
}
|
2018-10-15 10:38:06 -05:00
|
|
|
if err := stateMgr.PersistState(); err != nil {
|
2016-03-31 11:29:39 -05:00
|
|
|
c.Ui.Error(fmt.Sprintf(errStateRmPersist, err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2018-10-22 08:52:53 -05:00
|
|
|
if isCount == 0 {
|
|
|
|
c.Ui.Output("No matching resources found.")
|
|
|
|
} else {
|
|
|
|
c.Ui.Output(fmt.Sprintf("Successfully removed %d resource(s).", isCount))
|
|
|
|
}
|
2016-03-31 11:29:39 -05:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *StateRmCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: terraform state rm [options] ADDRESS...
|
|
|
|
|
|
|
|
Remove one or more items from the Terraform state.
|
|
|
|
|
2018-10-15 10:38:06 -05:00
|
|
|
This command removes one or more resource instances from the Terraform state
|
|
|
|
based on the addresses given. You can view and list the available instances
|
2016-03-31 11:29:39 -05:00
|
|
|
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:
|
|
|
|
|
2018-10-15 10:38:06 -05:00
|
|
|
-dry-run If set, prints out what would've been removed but
|
|
|
|
doesn't actually remove anything.
|
|
|
|
|
2016-03-31 11:29:39 -05:00
|
|
|
-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 13:46:09 -05:00
|
|
|
a backup extension.
|
2016-03-31 11:29:39 -05:00
|
|
|
|
2017-08-09 13:03:06 -05:00
|
|
|
-state=PATH Path to the source state file. Defaults to the configured
|
|
|
|
backend, or "terraform.tfstate"
|
2016-03-31 11:29:39 -05:00
|
|
|
|
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *StateRmCommand) Synopsis() string {
|
2018-10-15 10:38:06 -05:00
|
|
|
return "Remove instances from the state"
|
2016-03-31 11:29:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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.`
|