find implied moves in nested modules

Implied moves in nested modules were being skipped
This commit is contained in:
James Bardin 2021-12-21 15:40:56 -05:00
parent 346418e31f
commit e761117562
4 changed files with 68 additions and 1 deletions

View File

@ -149,7 +149,7 @@ func impliedMoveStatements(cfg *configs.Config, prevRunState *states.State, expl
}
for _, childCfg := range cfg.Children {
into = findMoveStatements(childCfg, into)
into = impliedMoveStatements(childCfg, prevRunState, explicitStmts, into)
}
return into

View File

@ -18,6 +18,15 @@ func TestImpliedMoveStatements(t *testing.T) {
Name: name,
}.Absolute(addrs.RootModuleInstance)
}
nestedResourceAddr := func(mod, name string) addrs.AbsResource {
return addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "foo",
Name: name,
}.Absolute(addrs.RootModuleInstance.Child(mod, addrs.NoKey))
}
instObjState := func() *states.ResourceInstanceObjectSrc {
return &states.ResourceInstanceObjectSrc{}
}
@ -86,6 +95,19 @@ func TestImpliedMoveStatements(t *testing.T) {
instObjState(),
providerAddr,
)
// Add two resource nested in a module to ensure we find these
// recursively.
s.SetResourceInstanceCurrent(
nestedResourceAddr("child", "formerly_count").Instance(addrs.IntKey(0)),
instObjState(),
providerAddr,
)
s.SetResourceInstanceCurrent(
nestedResourceAddr("child", "now_count").Instance(addrs.NoKey),
instObjState(),
providerAddr,
)
})
explicitStmts := FindMoveStatements(rootCfg)
@ -101,6 +123,19 @@ func TestImpliedMoveStatements(t *testing.T) {
End: tfdiags.SourcePos{Line: 5, Column: 32, Byte: 211},
},
},
// Found implied moves in a nested module, ignoring the explicit moves
{
From: addrs.ImpliedMoveStatementEndpoint(nestedResourceAddr("child", "formerly_count").Instance(addrs.IntKey(0)), tfdiags.SourceRange{}),
To: addrs.ImpliedMoveStatementEndpoint(nestedResourceAddr("child", "formerly_count").Instance(addrs.NoKey), tfdiags.SourceRange{}),
Implied: true,
DeclRange: tfdiags.SourceRange{
Filename: "testdata/move-statement-implied/child/move-statement-implied.tf",
Start: tfdiags.SourcePos{Line: 5, Column: 1, Byte: 180},
End: tfdiags.SourcePos{Line: 5, Column: 32, Byte: 211},
},
},
{
From: addrs.ImpliedMoveStatementEndpoint(resourceAddr("now_count").Instance(addrs.NoKey), tfdiags.SourceRange{}),
To: addrs.ImpliedMoveStatementEndpoint(resourceAddr("now_count").Instance(addrs.IntKey(0)), tfdiags.SourceRange{}),
@ -112,6 +147,18 @@ func TestImpliedMoveStatements(t *testing.T) {
},
},
// Found implied moves in a nested module, ignoring the explicit moves
{
From: addrs.ImpliedMoveStatementEndpoint(nestedResourceAddr("child", "now_count").Instance(addrs.NoKey), tfdiags.SourceRange{}),
To: addrs.ImpliedMoveStatementEndpoint(nestedResourceAddr("child", "now_count").Instance(addrs.IntKey(0)), tfdiags.SourceRange{}),
Implied: true,
DeclRange: tfdiags.SourceRange{
Filename: "testdata/move-statement-implied/child/move-statement-implied.tf",
Start: tfdiags.SourcePos{Line: 10, Column: 11, Byte: 282},
End: tfdiags.SourcePos{Line: 10, Column: 12, Byte: 283},
},
},
// We generate foo.ambiguous[0] to foo.ambiguous here, even though
// there's already a foo.ambiguous in the state, because it's the
// responsibility of the later ApplyMoves step to deal with the

View File

@ -0,0 +1,16 @@
# This fixture is useful only in conjunction with a previous run state that
# conforms to the statements encoded in the resource names. It's for
# TestImpliedMoveStatements only.
resource "foo" "formerly_count" {
# but not count anymore
}
resource "foo" "now_count" {
count = 1
}
moved {
from = foo.no_longer_present[1]
to = foo.no_longer_present
}

View File

@ -48,3 +48,7 @@ resource "foo" "ambiguous" {
# set it up to have both no-key and zero-key instances in the
# state.
}
module "child" {
source = "./child"
}