Skip to content

Commit

Permalink
Merge pull request #24254 from hashicorp/jbardin/state-mv
Browse files Browse the repository at this point in the history
state mv should always use target address to determine each mode.
  • Loading branch information
jbardin committed Mar 3, 2020
2 parents 430b7c8 + 10d9269 commit a3be475
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 9 deletions.
15 changes: 7 additions & 8 deletions command/state_mv.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,31 +280,30 @@ func (c *StateMvCommand) Run(args []string) int {
fromResourceAddr := addrFrom.ContainingResource()
fromResource := ssFrom.Resource(fromResourceAddr)
fromProviderAddr := fromResource.ProviderConfig
fromEachMode := fromResource.EachMode
ssFrom.ForgetResourceInstanceAll(addrFrom)
ssFrom.RemoveResourceIfEmpty(fromResourceAddr)

// since this is moving an instance, we can infer the target
// mode from the address.
toEachMode := eachModeForInstanceKey(addrTo.Resource.Key)

rs := stateTo.Resource(addrTo.ContainingResource())
if rs == nil {
// If we're moving to an address without an index then that
// suggests the user's intent is to establish both the
// resource and the instance at the same time (since the
// address covers both). If there's an index in the
// target then allow creating the new instance here,
// inferring the mode from how the new address was parsed.
if addrTo.Resource.Key != addrs.NoKey {
fromEachMode = eachModeForInstanceKey(addrTo.Resource.Key)
}

// target then allow creating the new instance here.
resourceAddr := addrTo.ContainingResource()
stateTo.SyncWrapper().SetResourceMeta(
resourceAddr,
fromEachMode,
toEachMode,
fromProviderAddr, // in this case, we bring the provider along as if we were moving the whole resource
)
rs = stateTo.Resource(resourceAddr)
}

rs.EachMode = toEachMode
rs.Instances[addrTo.Resource.Key] = is
}
default:
Expand Down
66 changes: 65 additions & 1 deletion command/state_mv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestStateMv(t *testing.T) {
"test_instance.bar",
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
t.Fatalf("return code: %d\n\n%s", code, ui.ErrorWriter.String())
}

// Test it is correct
Expand All @@ -80,6 +80,70 @@ func TestStateMv(t *testing.T) {
t.Fatalf("bad: %#v", backups)
}
testStateOutput(t, backups[0], testStateMvOutputOriginal)

// Change the single instance to a counted instance
args = []string{
"-state", statePath,
"test_instance.bar",
"test_instance.bar[0]",
}
if code := c.Run(args); code != 0 {
t.Fatalf("return code: %d\n\n%s", code, ui.ErrorWriter.String())
}

// extract the resource and verify the mode
s := testStateRead(t, statePath)
addr, diags := addrs.ParseAbsResourceStr("test_instance.bar")
if diags.HasErrors() {
t.Fatal(diags.Err())
}
i := s.Resource(addr)
if i.EachMode != states.EachList {
t.Fatalf("expected each mode List, got %s", i.EachMode)
}

// change from list to map
args = []string{
"-state", statePath,
"test_instance.bar[0]",
"test_instance.bar[\"baz\"]",
}
if code := c.Run(args); code != 0 {
t.Fatalf("return code: %d\n\n%s", code, ui.ErrorWriter.String())
}

// extract the resource and verify the mode
s = testStateRead(t, statePath)
addr, diags = addrs.ParseAbsResourceStr("test_instance.bar")
if diags.HasErrors() {
t.Fatal(diags.Err())
}
i = s.Resource(addr)
if i.EachMode != states.EachMap {
t.Fatalf("expected each mode Map, got %s", i.EachMode)
}

// change from from map back to single
args = []string{
"-state", statePath,
"test_instance.bar[\"baz\"]",
"test_instance.bar",
}
if code := c.Run(args); code != 0 {
t.Fatalf("return code: %d\n\n%s", code, ui.ErrorWriter.String())
}

// extract the resource and verify the mode
s = testStateRead(t, statePath)
addr, diags = addrs.ParseAbsResourceStr("test_instance.bar")
if diags.HasErrors() {
t.Fatal(diags.Err())
}
i = s.Resource(addr)
if i.EachMode != states.NoEach {
t.Fatalf("expected each mode NoEach, got %s", i.EachMode)
}

}

func TestStateMv_resourceToInstance(t *testing.T) {
Expand Down

0 comments on commit a3be475

Please sign in to comment.