mirror of
https://github.com/opentffoundation/opentf.git
synced 2025-12-25 01:00:16 -05:00
refactoring: ApplyMoves new return type
When we originally stubbed ApplyMoves we didn't know yet how exactly we'd be using the result, so we made it a double-indexed map allowing looking up moves in both directions. However, in practice we only actually need to look up old addresses by new addresses, and so this commit first removes the double indexing so that each move is only represented by one element in the map. We also need to describe situations where a move was blocked, because in a future commit we'll generate some warnings in those cases. Therefore ApplyMoves now returns a MoveResults object which contains both a map of changes and a map of blocks. The map of blocks isn't used yet as of this commit, but we'll use it in a later commit to produce warnings within the "terraform" package.
This commit is contained in:
@@ -10,10 +10,6 @@ import (
|
||||
"github.com/hashicorp/terraform/internal/states"
|
||||
)
|
||||
|
||||
type MoveResult struct {
|
||||
From, To addrs.AbsResourceInstance
|
||||
}
|
||||
|
||||
// ApplyMoves modifies in-place the given state object so that any existing
|
||||
// objects that are matched by a "from" argument of one of the move statements
|
||||
// will be moved to instead appear at the "to" argument of that statement.
|
||||
@@ -29,8 +25,11 @@ type MoveResult struct {
|
||||
//
|
||||
// ApplyMoves expects exclusive access to the given state while it's running.
|
||||
// Don't read or write any part of the state structure until ApplyMoves returns.
|
||||
func ApplyMoves(stmts []MoveStatement, state *states.State) map[addrs.UniqueKey]MoveResult {
|
||||
results := make(map[addrs.UniqueKey]MoveResult)
|
||||
func ApplyMoves(stmts []MoveStatement, state *states.State) MoveResults {
|
||||
ret := MoveResults{
|
||||
Changes: make(map[addrs.UniqueKey]MoveSuccess),
|
||||
Blocked: make(map[addrs.UniqueKey]MoveBlocked),
|
||||
}
|
||||
|
||||
// The methodology here is to construct a small graph of all of the move
|
||||
// statements where the edges represent where a particular statement
|
||||
@@ -44,7 +43,7 @@ func ApplyMoves(stmts []MoveStatement, state *states.State) map[addrs.UniqueKey]
|
||||
// at all. The separate validation step should detect this and return
|
||||
// an error.
|
||||
if len(g.Cycles()) != 0 {
|
||||
return results
|
||||
return ret
|
||||
}
|
||||
|
||||
// The starting nodes are the ones that don't depend on any other nodes.
|
||||
@@ -57,11 +56,33 @@ func ApplyMoves(stmts []MoveStatement, state *states.State) map[addrs.UniqueKey]
|
||||
|
||||
if startNodes.Len() == 0 {
|
||||
log.Println("[TRACE] refactoring.ApplyMoves: No 'moved' statements to consider in this configuration")
|
||||
return results
|
||||
return ret
|
||||
}
|
||||
|
||||
log.Printf("[TRACE] refactoring.ApplyMoves: Processing 'moved' statements in the configuration\n%s", logging.Indent(g.String()))
|
||||
|
||||
recordOldAddr := func(oldAddr, newAddr addrs.AbsResourceInstance) {
|
||||
oldAddrKey := oldAddr.UniqueKey()
|
||||
newAddrKey := newAddr.UniqueKey()
|
||||
if prevMove, exists := ret.Changes[oldAddrKey]; exists {
|
||||
// If the old address was _already_ the result of a move then
|
||||
// we'll replace that entry so that our results summarize a chain
|
||||
// of moves into a single entry.
|
||||
delete(ret.Changes, oldAddrKey)
|
||||
oldAddr = prevMove.From
|
||||
}
|
||||
ret.Changes[newAddrKey] = MoveSuccess{
|
||||
From: oldAddr,
|
||||
To: newAddr,
|
||||
}
|
||||
}
|
||||
recordBlockage := func(newAddr, wantedAddr addrs.AbsMoveable) {
|
||||
ret.Blocked[newAddr.UniqueKey()] = MoveBlocked{
|
||||
Wanted: wantedAddr,
|
||||
Actual: newAddr,
|
||||
}
|
||||
}
|
||||
|
||||
g.ReverseDepthFirstWalk(startNodes, func(v dag.Vertex, depth int) error {
|
||||
stmt := v.(*MoveStatement)
|
||||
|
||||
@@ -83,11 +104,9 @@ func ApplyMoves(stmts []MoveStatement, state *states.State) map[addrs.UniqueKey]
|
||||
// If we already have a module at the new address then
|
||||
// we'll skip this move and let the existing object take
|
||||
// priority.
|
||||
// TODO: This should probably generate a user-visible
|
||||
// warning, but we'd need to rethink the signature of this
|
||||
// function to achieve that.
|
||||
if ms := state.Module(newAddr); ms != nil {
|
||||
log.Printf("[WARN] Skipped moving %s to %s, because there's already another module instance at the destination", modAddr, newAddr)
|
||||
recordBlockage(modAddr, newAddr)
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -98,12 +117,7 @@ func ApplyMoves(stmts []MoveStatement, state *states.State) map[addrs.UniqueKey]
|
||||
for key := range rs.Instances {
|
||||
oldInst := relAddr.Instance(key).Absolute(modAddr)
|
||||
newInst := relAddr.Instance(key).Absolute(newAddr)
|
||||
result := MoveResult{
|
||||
From: oldInst,
|
||||
To: newInst,
|
||||
}
|
||||
results[oldInst.UniqueKey()] = result
|
||||
results[newInst.UniqueKey()] = result
|
||||
recordOldAddr(oldInst, newInst)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,23 +135,16 @@ func ApplyMoves(stmts []MoveStatement, state *states.State) map[addrs.UniqueKey]
|
||||
// If we already have a resource at the new address then
|
||||
// we'll skip this move and let the existing object take
|
||||
// priority.
|
||||
// TODO: This should probably generate a user-visible
|
||||
// warning, but we'd need to rethink the signature of this
|
||||
// function to achieve that.
|
||||
if rs := state.Resource(newAddr); rs != nil {
|
||||
log.Printf("[WARN] Skipped moving %s to %s, because there's already another resource at the destination", rAddr, newAddr)
|
||||
recordBlockage(rAddr, newAddr)
|
||||
continue
|
||||
}
|
||||
|
||||
for key := range rs.Instances {
|
||||
oldInst := rAddr.Instance(key)
|
||||
newInst := newAddr.Instance(key)
|
||||
result := MoveResult{
|
||||
From: oldInst,
|
||||
To: newInst,
|
||||
}
|
||||
results[oldInst.UniqueKey()] = result
|
||||
results[newInst.UniqueKey()] = result
|
||||
recordOldAddr(oldInst, newInst)
|
||||
}
|
||||
state.MoveAbsResource(rAddr, newAddr)
|
||||
continue
|
||||
@@ -150,17 +157,13 @@ func ApplyMoves(stmts []MoveStatement, state *states.State) map[addrs.UniqueKey]
|
||||
// If we already have a resource instance at the new
|
||||
// address then we'll skip this move and let the existing
|
||||
// object take priority.
|
||||
// TODO: This should probably generate a user-visible
|
||||
// warning, but we'd need to rethink the signature of this
|
||||
// function to achieve that.
|
||||
if is := state.ResourceInstance(newAddr); is != nil {
|
||||
log.Printf("[WARN] Skipped moving %s to %s, because there's already another resource instance at the destination", iAddr, newAddr)
|
||||
recordBlockage(iAddr, newAddr)
|
||||
continue
|
||||
}
|
||||
|
||||
result := MoveResult{From: iAddr, To: newAddr}
|
||||
results[iAddr.UniqueKey()] = result
|
||||
results[newAddr.UniqueKey()] = result
|
||||
recordOldAddr(iAddr, newAddr)
|
||||
|
||||
state.MoveAbsResourceInstance(iAddr, newAddr)
|
||||
continue
|
||||
@@ -175,17 +178,7 @@ func ApplyMoves(stmts []MoveStatement, state *states.State) map[addrs.UniqueKey]
|
||||
return nil
|
||||
})
|
||||
|
||||
// FIXME: In the case of either chained or nested moves, "results" will
|
||||
// be left in a pretty interesting shape where the "old" address will
|
||||
// refer to a result that describes only the first step, while the "new"
|
||||
// address will refer to a result that describes only the last step.
|
||||
// To make that actually useful we'll need a different strategy where
|
||||
// the result describes the _effective_ source and destination, skipping
|
||||
// over any intermediate steps we took to get there, so that ultimately
|
||||
// we'll have enough information to annotate items in the plan with the
|
||||
// addresses the originally moved from.
|
||||
|
||||
return results
|
||||
return ret
|
||||
}
|
||||
|
||||
// buildMoveStatementGraph constructs a dependency graph of the given move
|
||||
@@ -218,3 +211,67 @@ func buildMoveStatementGraph(stmts []MoveStatement) *dag.AcyclicGraph {
|
||||
|
||||
return g
|
||||
}
|
||||
|
||||
// MoveResults describes the outcome of an ApplyMoves call.
|
||||
type MoveResults struct {
|
||||
// Changes is a map from the unique keys of the final new resource
|
||||
// instance addresses to an object describing what changed.
|
||||
//
|
||||
// This includes one entry for each resource instance address that was
|
||||
// the destination of a move statement. It doesn't include resource
|
||||
// instances that were not affected by moves at all, but it does include
|
||||
// resource instance addresses that were "blocked" (also recorded in
|
||||
// BlockedAddrs) if and only if they were able to move at least
|
||||
// partially along a chain before being blocked.
|
||||
//
|
||||
// In the return value from ApplyMoves, all of the keys are guaranteed to
|
||||
// be unique keys derived from addrs.AbsResourceInstance values.
|
||||
Changes map[addrs.UniqueKey]MoveSuccess
|
||||
|
||||
// Blocked is a map from the unique keys of the final new
|
||||
// resource instances addresses to information about where they "wanted"
|
||||
// to move, but were blocked by a pre-existing object at the same address.
|
||||
//
|
||||
// "Blocking" can arise in unusual situations where multiple points along
|
||||
// a move chain were already bound to objects, and thus only one of them
|
||||
// can actually adopt the final position in the chain. It can also
|
||||
// occur in other similar situations, such as if a configuration contains
|
||||
// a move of an entire module and a move of an individual resource into
|
||||
// that module, such that the individual resource would collide with a
|
||||
// resource in the whole module that was moved.
|
||||
//
|
||||
// In the return value from ApplyMoves, all of the keys are guaranteed to
|
||||
// be unique keys derived from values of addrs.AbsMoveable types.
|
||||
Blocked map[addrs.UniqueKey]MoveBlocked
|
||||
}
|
||||
|
||||
type MoveSuccess struct {
|
||||
From addrs.AbsResourceInstance
|
||||
To addrs.AbsResourceInstance
|
||||
}
|
||||
|
||||
type MoveBlocked struct {
|
||||
Wanted addrs.AbsMoveable
|
||||
Actual addrs.AbsMoveable
|
||||
}
|
||||
|
||||
// AddrMoved returns true if and only if the given resource instance moved to
|
||||
// a new address in the ApplyMoves call that the receiver is describing.
|
||||
//
|
||||
// If AddrMoved returns true, you can pass the same address to method OldAddr
|
||||
// to find its original address prior to moving.
|
||||
func (rs MoveResults) AddrMoved(newAddr addrs.AbsResourceInstance) bool {
|
||||
_, ok := rs.Changes[newAddr.UniqueKey()]
|
||||
return ok
|
||||
}
|
||||
|
||||
// OldAddr returns the old address of the given resource instance address, or
|
||||
// just returns back the same address if the given instance wasn't affected by
|
||||
// any move statements.
|
||||
func (rs MoveResults) OldAddr(newAddr addrs.AbsResourceInstance) addrs.AbsResourceInstance {
|
||||
change, ok := rs.Changes[newAddr.UniqueKey()]
|
||||
if !ok {
|
||||
return newAddr
|
||||
}
|
||||
return change.From
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user