Skip to content

Commit

Permalink
find implied moves in nested modules
Browse files Browse the repository at this point in the history
Implied moves in nested modules were being skipped
  • Loading branch information
jbardin committed Dec 21, 2021
1 parent 346418e commit e761117
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/refactoring/move_statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 47 additions & 0 deletions internal/refactoring/move_statement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}
Expand Down Expand Up @@ -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)
Expand All @@ -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{}),
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit e761117

Please sign in to comment.