command: Un-stub and reimplement "terraform state rm"

This was previously targeting the old state manager and state types, so it
needed some considerable rework to get it working again with the new state
types.

Since our new resource address syntax lacks the weird extra .deposed
special case we had before, we instead interpret addresses as
whole-instance addresses here and remove the deposed objects along with
the current one (if present), since this is more likely to match with
user expectations because we don't consider deposed objects to be
independently addressable in any other situation.

With that said, to be more explicit about what is going on we do now have
a -dry-run mode and maintain separate counts of current and deposed
instances so that we can expose that in the UI where relevant.
This commit is contained in:
Martin Atkins 2018-10-15 08:38:06 -07:00
parent 40eda180d6
commit 66f96cf842
4 changed files with 174 additions and 18 deletions

View File

@ -1,10 +1,14 @@
package command
import (
"bytes"
"fmt"
"strings"
"github.com/mitchellh/cli"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/tfdiags"
)
// StateRmCommand is a Command implementation that shows a single resource.
@ -19,6 +23,8 @@ func (c *StateRmCommand) Run(args []string) int {
}
cmdFlags := c.Meta.flagSet("state show")
var dryRun bool
cmdFlags.BoolVar(&dryRun, "dry-run", false, "dry run")
cmdFlags.StringVar(&c.backupPath, "backup", "-", "backup")
cmdFlags.StringVar(&c.statePath, "state", "", "path")
if err := cmdFlags.Parse(args); err != nil {
@ -26,50 +32,109 @@ func (c *StateRmCommand) Run(args []string) int {
}
args = cmdFlags.Args()
var diags tfdiags.Diagnostics
if len(args) < 1 {
c.Ui.Error("At least one resource address is required.")
return 1
}
state, err := c.State()
stateMgr, err := c.State()
if err != nil {
c.Ui.Error(fmt.Sprintf(errStateLoadingState, err))
return 1
}
if err := state.RefreshState(); err != nil {
if err := stateMgr.RefreshState(); err != nil {
c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
return 1
}
stateReal := state.State()
if stateReal == nil {
state := stateMgr.State()
if state == nil {
c.Ui.Error(fmt.Sprintf(errStateNotFound))
return 1
}
c.Ui.Error("state rm not yet updated for new state types")
return 1
toRemove := make([]addrs.AbsResourceInstance, len(args))
for i, rawAddr := range args {
addr, moreDiags := addrs.ParseAbsResourceInstanceStr(rawAddr)
diags = diags.Append(moreDiags)
toRemove[i] = addr
}
if diags.HasErrors() {
c.showDiagnostics(diags)
return 1
}
/*
if err := stateReal.Remove(args...); err != nil {
c.Ui.Error(fmt.Sprintf(errStateRm, err))
return 1
// We will first check that all of the instances are present, so we can
// either remove all of them successfully or make no change at all.
// (If we're in dry run mode, this is also where we print out what
// we would've done.)
var currentCount, deposedCount int
var dryRunBuf bytes.Buffer
for _, addr := range toRemove {
is := state.ResourceInstance(addr)
if is == nil {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"No such resource instance in state",
fmt.Sprintf("There is no resource instance in the current state with the address %s.", addr),
))
continue
}
*/
if is.Current != nil {
currentCount++
}
deposedCount += len(is.Deposed)
if dryRun {
if is.Current != nil {
fmt.Fprintf(&dryRunBuf, "Would remove %s\n", addr)
}
for k := range is.Deposed {
fmt.Fprintf(&dryRunBuf, "Would remove %s deposed object %s\n", addr, k)
}
}
}
if diags.HasErrors() {
c.showDiagnostics(diags)
return 1
}
c.Ui.Output(fmt.Sprintf("%d items removed.", len(args)))
if dryRun {
c.Ui.Output(fmt.Sprintf("%s\nWould've removed %d current and %d deposed objects, without -dry-run.", dryRunBuf.String(), currentCount, deposedCount))
return 0 // This is as far as we go in dry-run mode
}
if err := state.WriteState(stateReal); err != nil {
// Now we will actually remove them. Due to our validation above, we should
// succeed in removing every one.
// We'll use the "SyncState" wrapper to do this not because we're doing
// any concurrent work here (we aren't) but because it guarantees to clean
// up any leftover empty module we might leave behind.
ss := state.SyncWrapper()
for _, addr := range toRemove {
ss.ForgetResourceInstanceAll(addr)
}
switch {
case currentCount == 0:
c.Ui.Output(fmt.Sprintf("Removed %d deposed objects.", deposedCount))
case deposedCount == 0:
c.Ui.Output(fmt.Sprintf("Removed %d objects.", currentCount))
default:
c.Ui.Output(fmt.Sprintf("Removed %d current and %d deposed objects.", currentCount, deposedCount))
}
if err := stateMgr.WriteState(state); err != nil {
c.Ui.Error(fmt.Sprintf(errStateRmPersist, err))
return 1
}
if err := state.PersistState(); err != nil {
if err := stateMgr.PersistState(); err != nil {
c.Ui.Error(fmt.Sprintf(errStateRmPersist, err))
return 1
}
c.Ui.Output("Item removal successful.")
c.Ui.Output("Updated state written successfully.")
return 0
}
@ -79,8 +144,8 @@ 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
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
with "terraform state list".
This command creates a timestamped backup of the state on every invocation.
@ -89,6 +154,9 @@ Usage: terraform state rm [options] ADDRESS...
Options:
-dry-run If set, prints out what would've been removed but
doesn't actually remove anything.
-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
@ -102,7 +170,7 @@ Options:
}
func (c *StateRmCommand) Synopsis() string {
return "Remove an item from the state"
return "Remove instances from the state"
}
const errStateRm = `Error removing items from the state: %s

View File

@ -126,6 +126,60 @@ func TestStateRmNoArgs(t *testing.T) {
}
func TestStateRmNonExist(t *testing.T) {
state := states.BuildState(func(s *states.SyncState) {
s.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_instance",
Name: "foo",
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
&states.ResourceInstanceObjectSrc{
AttrsJSON: []byte(`{"id":"bar","foo":"value","bar":"value"}`),
Status: states.ObjectReady,
},
addrs.ProviderConfig{Type: "test"}.Absolute(addrs.RootModuleInstance),
)
s.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_instance",
Name: "bar",
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
&states.ResourceInstanceObjectSrc{
AttrsJSON: []byte(`{"id":"foo","foo":"value","bar":"value"}`),
Status: states.ObjectReady,
},
addrs.ProviderConfig{Type: "test"}.Absolute(addrs.RootModuleInstance),
)
})
statePath := testStateFile(t, state)
p := testProvider()
ui := new(cli.MockUi)
c := &StateRmCommand{
StateMeta{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
},
}
args := []string{
"-state", statePath,
"test_instance.baz", // doesn't exist in the state constructed above
}
if code := c.Run(args); code != 1 {
t.Errorf("wrong exit status %d; want %d", code, 1)
}
if msg := ui.ErrorWriter.String(); !strings.Contains(msg, "No such resource instance in state") {
t.Errorf("not the error we were looking for:\n%s", msg)
}
}
func TestStateRm_backupExplicit(t *testing.T) {
td := tempDir(t)
defer os.RemoveAll(td)
@ -314,10 +368,12 @@ func TestStateRm_backendState(t *testing.T) {
const testStateRmOutputOriginal = `
test_instance.bar:
ID = foo
provider = provider.test
bar = value
foo = value
test_instance.foo:
ID = bar
provider = provider.test
bar = value
foo = value
`
@ -325,6 +381,7 @@ test_instance.foo:
const testStateRmOutput = `
test_instance.bar:
ID = foo
provider = provider.test
bar = value
foo = value
`

View File

@ -146,6 +146,23 @@ func (ms *Module) SetResourceInstanceDeposed(addr addrs.ResourceInstance, key De
}
}
// ForgetResourceInstanceAll removes the record of all objects associated with
// the specified resource instance, if present. If not present, this is a no-op.
func (ms *Module) ForgetResourceInstanceAll(addr addrs.ResourceInstance) {
rs := ms.Resource(addr.Resource)
if rs == nil {
return
}
delete(rs.Instances, addr.Key)
if rs.EachMode == NoEach && len(rs.Instances) == 0 {
// Also clean up if we only expect to have one instance anyway
// and there are none. We leave the resource behind if an each mode
// is active because an empty list or map of instances is a valid state.
delete(ms.Resources, addr.Resource.String())
}
}
// ForgetResourceInstanceDeposed removes the record of the deposed object with
// the given address and key, if present. If not present, this is a no-op.
func (ms *Module) ForgetResourceInstanceDeposed(addr addrs.ResourceInstance, key DeposedKey) {

View File

@ -377,6 +377,20 @@ func (s *SyncState) DeposeResourceInstanceObjectForceKey(addr addrs.AbsResourceI
ms.deposeResourceInstanceObject(addr.Resource, forcedKey)
}
// ForgetResourceInstanceAll removes the record of all objects associated with
// the specified resource instance, if present. If not present, this is a no-op.
func (s *SyncState) ForgetResourceInstanceAll(addr addrs.AbsResourceInstance) {
s.lock.Lock()
defer s.lock.Unlock()
ms := s.state.Module(addr.Module)
if ms == nil {
return
}
ms.ForgetResourceInstanceAll(addr.Resource)
s.maybePruneModule(addr.Module)
}
// ForgetResourceInstanceDeposed removes the record of the deposed object with
// the given address and key, if present. If not present, this is a no-op.
func (s *SyncState) ForgetResourceInstanceDeposed(addr addrs.AbsResourceInstance, key DeposedKey) {