Skip to content

Commit f772cb0

Browse files
authored
Merge pull request #30233 from hashicorp/alisdair/move-nested-modules
refactoring: Move nested modules
2 parents a72d2d4 + d7ef123 commit f772cb0

File tree

2 files changed

+74
-8
lines changed

2 files changed

+74
-8
lines changed

internal/refactoring/move_execute.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,13 @@ func ApplyMoves(stmts []MoveStatement, state *states.State) MoveResults {
8888

8989
for _, ms := range state.Modules {
9090
modAddr := ms.Addr
91-
if !stmt.From.SelectsModule(modAddr) {
92-
continue
93-
}
9491

95-
// We now know that the current module is relevant but what
96-
// we'll do with it depends on the object kind.
92+
// We don't yet know that the current module is relevant, and
93+
// we determine that differently for each the object kind.
9794
switch kind := stmt.ObjectKind(); kind {
9895
case addrs.MoveEndpointModule:
9996
// For a module endpoint we just try the module address
100-
// directly.
97+
// directly, and execute the moves if it matches.
10198
if newAddr, matches := modAddr.MoveDestination(stmt.From, stmt.To); matches {
10299
log.Printf("[TRACE] refactoring.ApplyMoves: %s has moved to %s", modAddr, newAddr)
103100

@@ -125,8 +122,15 @@ func ApplyMoves(stmts []MoveStatement, state *states.State) MoveResults {
125122
continue
126123
}
127124
case addrs.MoveEndpointResource:
128-
// For a resource endpoint we need to search each of the
129-
// resources and resource instances in the module.
125+
// For a resource endpoint we require an exact containing
126+
// module match, because by definition a matching resource
127+
// cannot be nested any deeper than that.
128+
if !stmt.From.SelectsModule(modAddr) {
129+
continue
130+
}
131+
132+
// We then need to search each of the resources and resource
133+
// instances in the module.
130134
for _, rs := range ms.Resources {
131135
rAddr := rs.Addr
132136
if newAddr, matches := rAddr.MoveDestination(stmt.From, stmt.To); matches {

internal/refactoring/move_execute_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ func TestApplyMoves(t *testing.T) {
2121
}
2222

2323
moduleBoo, _ := addrs.ParseModuleInstanceStr("module.boo")
24+
moduleBar, _ := addrs.ParseModuleInstanceStr("module.bar")
2425
moduleBarKey, _ := addrs.ParseModuleInstanceStr("module.bar[0]")
26+
moduleBooHoo, _ := addrs.ParseModuleInstanceStr("module.boo.module.hoo")
27+
moduleBarHoo, _ := addrs.ParseModuleInstanceStr("module.bar.module.hoo")
2528

2629
instAddrs := map[string]addrs.AbsResourceInstance{
2730
"foo.from": addrs.Resource{
@@ -84,6 +87,12 @@ func TestApplyMoves(t *testing.T) {
8487
Name: "to",
8588
}.Instance(addrs.IntKey(0)).Absolute(moduleBoo),
8689

90+
"module.bar.foo.from": addrs.Resource{
91+
Mode: addrs.ManagedResourceMode,
92+
Type: "foo",
93+
Name: "from",
94+
}.Instance(addrs.NoKey).Absolute(moduleBar),
95+
8796
"module.bar[0].foo.from": addrs.Resource{
8897
Mode: addrs.ManagedResourceMode,
8998
Type: "foo",
@@ -113,6 +122,18 @@ func TestApplyMoves(t *testing.T) {
113122
Type: "foo",
114123
Name: "to",
115124
}.Instance(addrs.IntKey(0)).Absolute(moduleBarKey),
125+
126+
"module.boo.module.hoo.foo.from": addrs.Resource{
127+
Mode: addrs.ManagedResourceMode,
128+
Type: "foo",
129+
Name: "from",
130+
}.Instance(addrs.NoKey).Absolute(moduleBooHoo),
131+
132+
"module.bar.module.hoo.foo.from": addrs.Resource{
133+
Mode: addrs.ManagedResourceMode,
134+
Type: "foo",
135+
Name: "from",
136+
}.Instance(addrs.NoKey).Absolute(moduleBarHoo),
116137
}
117138

118139
emptyResults := MoveResults{
@@ -289,6 +310,47 @@ func TestApplyMoves(t *testing.T) {
289310
},
290311
},
291312

313+
"module move with child module": {
314+
[]MoveStatement{
315+
testMoveStatement(t, "", "module.boo", "module.bar"),
316+
},
317+
states.BuildState(func(s *states.SyncState) {
318+
s.SetResourceInstanceCurrent(
319+
instAddrs["module.boo.foo.from"],
320+
&states.ResourceInstanceObjectSrc{
321+
Status: states.ObjectReady,
322+
AttrsJSON: []byte(`{}`),
323+
},
324+
providerAddr,
325+
)
326+
s.SetResourceInstanceCurrent(
327+
instAddrs["module.boo.module.hoo.foo.from"],
328+
&states.ResourceInstanceObjectSrc{
329+
Status: states.ObjectReady,
330+
AttrsJSON: []byte(`{}`),
331+
},
332+
providerAddr,
333+
)
334+
}),
335+
MoveResults{
336+
Changes: map[addrs.UniqueKey]MoveSuccess{
337+
instAddrs["module.bar.foo.from"].UniqueKey(): {
338+
From: instAddrs["module.boo.foo.from"],
339+
To: instAddrs["module.bar.foo.from"],
340+
},
341+
instAddrs["module.bar.module.hoo.foo.from"].UniqueKey(): {
342+
From: instAddrs["module.boo.module.hoo.foo.from"],
343+
To: instAddrs["module.bar.module.hoo.foo.from"],
344+
},
345+
},
346+
Blocked: map[addrs.UniqueKey]MoveBlocked{},
347+
},
348+
[]string{
349+
`module.bar.foo.from`,
350+
`module.bar.module.hoo.foo.from`,
351+
},
352+
},
353+
292354
"move whole single module to indexed module": {
293355
[]MoveStatement{
294356
testMoveStatement(t, "", "module.boo", "module.bar[0]"),

0 commit comments

Comments
 (0)