4 "github.com/hashicorp/terraform/dag"
7 // NodeRefreshableDataResource represents a resource that is "plannable":
8 // it is ready to be planned in order to create a diff.
9 type NodeRefreshableDataResource struct {
10 *NodeAbstractCountResource
13 // GraphNodeDynamicExpandable
14 func (n *NodeRefreshableDataResource) DynamicExpand(ctx EvalContext) (*Graph, error) {
15 // Grab the state which we read
16 state, lock := ctx.State()
20 // Expand the resource count which must be available by now from EvalTree
21 count, err := n.Config.Count()
26 // The concrete resource factory we'll use
27 concreteResource := func(a *NodeAbstractResource) dag.Vertex {
28 // Add the config and state since we don't do that via transforms
30 a.ResolvedProvider = n.ResolvedProvider
32 return &NodeRefreshableDataResourceInstance{
33 NodeAbstractResource: a,
37 // We also need a destroyable resource for orphans that are a result of a
39 concreteResourceDestroyable := func(a *NodeAbstractResource) dag.Vertex {
40 // Add the config since we don't do that via transforms
43 return &NodeDestroyableDataResource{
44 NodeAbstractResource: a,
48 // Start creating the steps
49 steps := []GraphTransformer{
51 &ResourceCountTransformer{
52 Concrete: concreteResource,
54 Addr: n.ResourceAddr(),
57 // Add the count orphans. As these are orphaned refresh nodes, we add them
58 // directly as NodeDestroyableDataResource.
59 &OrphanResourceCountTransformer{
60 Concrete: concreteResourceDestroyable,
62 Addr: n.ResourceAddr(),
67 &AttachStateTransformer{State: state},
70 &TargetsTransformer{ParsedTargets: n.Targets},
72 // Connect references so ordering is correct
73 &ReferenceTransformer{},
75 // Make sure there is a single root
80 b := &BasicGraphBuilder{
83 Name: "NodeRefreshableDataResource",
86 return b.Build(ctx.Path())
89 // NodeRefreshableDataResourceInstance represents a _single_ resource instance
90 // that is refreshable.
91 type NodeRefreshableDataResourceInstance struct {
96 func (n *NodeRefreshableDataResourceInstance) EvalTree() EvalNode {
97 addr := n.NodeAbstractResource.Addr
99 // stateId is the ID to put into the state
100 stateId := addr.stateId()
102 // Build the instance info. More of this will be populated during eval
103 info := &InstanceInfo{
108 // Get the state if we have it, if not we build it
109 rs := n.ResourceState
112 Provider: n.ResolvedProvider,
116 // If the config isn't empty we update the state
120 Provider: n.Config.Provider,
121 Dependencies: n.StateReferences(),
125 // Build the resource for eval
126 resource := &Resource{
129 CountIndex: addr.Index,
131 if resource.CountIndex < 0 {
132 resource.CountIndex = 0
135 // Declare a bunch of variables that are used for state during
136 // evaluation. Most of this are written to by-address below.
137 var config *ResourceConfig
138 var diff *InstanceDiff
139 var provider ResourceProvider
140 var state *InstanceState
142 return &EvalSequence{
144 // Always destroy the existing state first, since we must
145 // make sure that values from a previous read will not
146 // get interpolated if we end up needing to defer our
147 // loading until apply time.
150 ResourceType: rs.Type,
151 Provider: n.ResolvedProvider,
152 Dependencies: rs.Dependencies,
153 State: &state, // state is nil here
157 Config: n.Config.RawConfig.Copy(),
162 // The rest of this pass can proceed only if there are no
163 // computed values in our config.
164 // (If there are, we'll deal with this during the plan and
167 If: func(ctx EvalContext) (bool, error) {
168 if config.ComputedKeys != nil && len(config.ComputedKeys) > 0 {
169 return true, EvalEarlyExitError{}
172 // If the config explicitly has a depends_on for this
173 // data source, assume the intention is to prevent
174 // refreshing ahead of that dependency.
175 if len(n.Config.DependsOn) > 0 {
176 return true, EvalEarlyExitError{}
185 // The remainder of this pass is the same as running
186 // a "plan" pass immediately followed by an "apply" pass,
187 // populating the state early so it'll be available to
188 // provider configurations that need this data during
191 Name: n.ResolvedProvider,
212 ResourceType: rs.Type,
213 Provider: n.ResolvedProvider,
214 Dependencies: rs.Dependencies,
218 &EvalUpdateStateHook{},