/
context_walk.go
119 lines (100 loc) · 3.92 KB
/
context_walk.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package terraform
import (
"log"
"github.com/hashicorp/terraform/internal/configs"
"github.com/hashicorp/terraform/internal/instances"
"github.com/hashicorp/terraform/internal/plans"
"github.com/hashicorp/terraform/internal/refactoring"
"github.com/hashicorp/terraform/internal/states"
"github.com/hashicorp/terraform/internal/tfdiags"
)
// graphWalkOpts captures some transient values we use (and possibly mutate)
// during a graph walk.
//
// The way these options get used unfortunately varies between the different
// walkOperation types. This is a historical design wart that dates back to
// us using the same graph structure for all operations; hopefully we'll
// make the necessary differences between the walk types more explicit someday.
type graphWalkOpts struct {
InputState *states.State
Changes *plans.Changes
Conditions plans.Conditions
Config *configs.Config
MoveResults refactoring.MoveResults
}
func (c *Context) walk(graph *Graph, operation walkOperation, opts *graphWalkOpts) (*ContextGraphWalker, tfdiags.Diagnostics) {
log.Printf("[DEBUG] Starting graph walk: %s", operation.String())
walker := c.graphWalker(operation, opts)
// Watch for a stop so we can call the provider Stop() API.
watchStop, watchWait := c.watchStop(walker)
// Walk the real graph, this will block until it completes
diags := graph.Walk(walker)
// Close the channel so the watcher stops, and wait for it to return.
close(watchStop)
<-watchWait
return walker, diags
}
func (c *Context) graphWalker(operation walkOperation, opts *graphWalkOpts) *ContextGraphWalker {
var state *states.SyncState
var refreshState *states.SyncState
var prevRunState *states.SyncState
// NOTE: None of the SyncState objects must directly wrap opts.InputState,
// because we use those to mutate the state object and opts.InputState
// belongs to our caller and thus we must treat it as immutable.
//
// To account for that, most of our SyncState values created below end up
// wrapping a _deep copy_ of opts.InputState instead.
inputState := opts.InputState
if inputState == nil {
// Lots of callers use nil to represent the "empty" case where we've
// not run Apply yet, so we tolerate that.
inputState = states.NewState()
}
switch operation {
case walkValidate:
// validate should not use any state
state = states.NewState().SyncWrapper()
// validate currently uses the plan graph, so we have to populate the
// refreshState and the prevRunState.
refreshState = states.NewState().SyncWrapper()
prevRunState = states.NewState().SyncWrapper()
case walkPlan, walkPlanDestroy, walkImport:
state = inputState.DeepCopy().SyncWrapper()
refreshState = inputState.DeepCopy().SyncWrapper()
prevRunState = inputState.DeepCopy().SyncWrapper()
default:
state = inputState.DeepCopy().SyncWrapper()
// Only plan-like walks use refreshState and prevRunState
}
changes := opts.Changes
if changes == nil {
// Several of our non-plan walks end up sharing codepaths with the
// plan walk and thus expect to generate planned changes even though
// we don't care about them. To avoid those crashing, we'll just
// insert a placeholder changes object which'll get discarded
// afterwards.
changes = plans.NewChanges()
}
conditions := opts.Conditions
if conditions == nil {
// This fallback conditions object is in place for the same reason as
// the changes object above: to avoid crashes for non-plan walks.
conditions = plans.NewConditions()
}
if opts.Config == nil {
panic("Context.graphWalker call without Config")
}
return &ContextGraphWalker{
Context: c,
State: state,
Config: opts.Config,
RefreshState: refreshState,
PrevRunState: prevRunState,
Changes: changes.SyncWrapper(),
Conditions: conditions.SyncWrapper(),
InstanceExpander: instances.NewExpander(),
MoveResults: opts.MoveResults,
Operation: operation,
StopContext: c.runContext,
}
}