From 8afbb0ee0ea365035a7c11fb0ee7c1c730c003b3 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 19 Aug 2016 11:54:53 -0400 Subject: [PATCH] terraform: state mv "foo" to "foo.0" with single count --- terraform/state_add.go | 2 +- terraform/state_add_test.go | 60 +++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/terraform/state_add.go b/terraform/state_add.go index 57a8925d6..033f20614 100644 --- a/terraform/state_add.go +++ b/terraform/state_add.go @@ -162,7 +162,7 @@ func stateAddFunc_Resource_Resource(s *State, fromAddr, addr *ResourceAddress, r // If there is an index, this is an error since we can't assign // a set of resources to a single index - if addr.Index >= 0 { + if addr.Index >= 0 && len(list) > 1 { return fmt.Errorf( "multiple resources can't be moved to a single index: "+ "%s => %s", fromAddr, addr) diff --git a/terraform/state_add_test.go b/terraform/state_add_test.go index 8afc428e0..99e29ed11 100644 --- a/terraform/state_add_test.go +++ b/terraform/state_add_test.go @@ -440,6 +440,66 @@ func TestStateAdd(t *testing.T) { nil, }, + "ResourceState with single count unspecified => Resource Addr (new with count)": { + false, + "aws_instance.bar", + "aws_instance.foo[0]", + []*ResourceState{ + &ResourceState{ + Type: "test_instance", + Primary: &InstanceState{ + ID: "foo", + }, + }, + }, + + &State{}, + &State{ + Modules: []*ModuleState{ + &ModuleState{ + Path: []string{"root"}, + Resources: map[string]*ResourceState{ + "aws_instance.foo.0": &ResourceState{ + Type: "test_instance", + Primary: &InstanceState{ + ID: "foo", + }, + }, + }, + }, + }, + }, + }, + + "ResourceState => Resource Addr (new with count)": { + false, + "aws_instance.bar", + "aws_instance.foo[0]", + &ResourceState{ + Type: "test_instance", + Primary: &InstanceState{ + ID: "foo", + }, + }, + + &State{}, + &State{ + Modules: []*ModuleState{ + &ModuleState{ + Path: []string{"root"}, + Resources: map[string]*ResourceState{ + "aws_instance.foo.0": &ResourceState{ + Type: "test_instance", + Primary: &InstanceState{ + ID: "foo", + }, + }, + }, + }, + }, + }, + }, + "ResourceState => Resource Addr (existing)": { true, "aws_instance.bar",