From 0d1ea84d39340730262c969479967367574d1af4 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 19 Aug 2016 12:05:20 -0400 Subject: [PATCH] command: test for moving resource with count [GH-7797] --- command/state_mv.go | 19 +++++++ command/state_mv_test.go | 110 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) diff --git a/command/state_mv.go b/command/state_mv.go index b16f99828..ea6c5a8cc 100644 --- a/command/state_mv.go +++ b/command/state_mv.go @@ -139,6 +139,25 @@ func (c *StateMvCommand) addableResult(results []*terraform.StateFilterResult) i } return result + + case *terraform.ResourceState: + // If a module state then we should add the full list of modules + 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) + } + } + } + + return result + default: // By default just add the first result return v diff --git a/command/state_mv_test.go b/command/state_mv_test.go index bec0504aa..4a81e79b2 100644 --- a/command/state_mv_test.go +++ b/command/state_mv_test.go @@ -223,6 +223,83 @@ func TestStateMv_noState(t *testing.T) { } } +func TestStateMv_stateOutNew_count(t *testing.T) { + state := &terraform.State{ + Modules: []*terraform.ModuleState{ + &terraform.ModuleState{ + Path: []string{"root"}, + Resources: map[string]*terraform.ResourceState{ + "test_instance.foo.0": &terraform.ResourceState{ + Type: "test_instance", + Primary: &terraform.InstanceState{ + ID: "foo", + Attributes: map[string]string{ + "foo": "value", + "bar": "value", + }, + }, + }, + + "test_instance.foo.1": &terraform.ResourceState{ + Type: "test_instance", + Primary: &terraform.InstanceState{ + ID: "bar", + Attributes: map[string]string{ + "foo": "value", + "bar": "value", + }, + }, + }, + + "test_instance.bar": &terraform.ResourceState{ + Type: "test_instance", + Primary: &terraform.InstanceState{ + ID: "bar", + Attributes: map[string]string{ + "foo": "value", + "bar": "value", + }, + }, + }, + }, + }, + }, + } + + statePath := testStateFile(t, state) + stateOutPath := statePath + ".out" + + p := testProvider() + ui := new(cli.MockUi) + c := &StateMvCommand{ + Meta: Meta{ + ContextOpts: testCtxConfig(p), + Ui: ui, + }, + } + + args := []string{ + "-state", statePath, + "-state-out", stateOutPath, + "test_instance.foo", + "test_instance.bar", + } + if code := c.Run(args); code != 0 { + t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) + } + + // Test it is correct + testStateOutput(t, stateOutPath, testStateMvCount_stateOut) + testStateOutput(t, statePath, testStateMvCount_stateOutSrc) + + // Test we have backups + backups := testStateBackups(t, filepath.Dir(statePath)) + if len(backups) != 1 { + t.Fatalf("bad: %#v", backups) + } + testStateOutput(t, backups[0], testStateMvCount_stateOutOriginal) +} + func TestStateMv_stateOutNew_nestedModule(t *testing.T) { state := &terraform.State{ Modules: []*terraform.ModuleState{ @@ -326,6 +403,39 @@ test_instance.baz: foo = value ` +const testStateMvCount_stateOut = ` +test_instance.bar.0: + ID = foo + bar = value + foo = value +test_instance.bar.1: + ID = bar + bar = value + foo = value +` + +const testStateMvCount_stateOutSrc = ` +test_instance.bar: + ID = bar + bar = value + foo = value +` + +const testStateMvCount_stateOutOriginal = ` +test_instance.bar: + ID = bar + bar = value + foo = value +test_instance.foo.0: + ID = foo + bar = value + foo = value +test_instance.foo.1: + ID = bar + bar = value + foo = value +` + const testStateMvNestedModule_stateOut = ` module.bar: