From 88649ed01038ff681547cc0aa0b01d56672a98f7 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 19 Aug 2016 11:51:31 -0400 Subject: [PATCH] terraform: StateAdd edge case test for multi-count to single index --- terraform/state_add.go | 8 ++++++++ terraform/state_add_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/terraform/state_add.go b/terraform/state_add.go index f78d293a7..57a8925d6 100644 --- a/terraform/state_add.go +++ b/terraform/state_add.go @@ -160,6 +160,14 @@ func stateAddFunc_Resource_Resource(s *State, fromAddr, addr *ResourceAddress, r return fmt.Errorf("resource move with no value to: %s", addr) } + // 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 { + return fmt.Errorf( + "multiple resources can't be moved to a single index: "+ + "%s => %s", fromAddr, addr) + } + // Add each with a specific index for i, rs := range list { addrCopy := addr.Copy() diff --git a/terraform/state_add_test.go b/terraform/state_add_test.go index bb1e619c1..8afc428e0 100644 --- a/terraform/state_add_test.go +++ b/terraform/state_add_test.go @@ -416,6 +416,30 @@ func TestStateAdd(t *testing.T) { }, }, + "ResourceState with count unspecified => Resource Addr (new with count)": { + true, + "aws_instance.bar", + "aws_instance.foo[0]", + []*ResourceState{ + &ResourceState{ + Type: "test_instance", + Primary: &InstanceState{ + ID: "foo", + }, + }, + + &ResourceState{ + Type: "test_instance", + Primary: &InstanceState{ + ID: "bar", + }, + }, + }, + + &State{}, + nil, + }, + "ResourceState => Resource Addr (existing)": { true, "aws_instance.bar",