Repository navigation
Expand file tree
/
Copy patheval_state.go
More file actions
102 lines (83 loc) · 2.23 KB
/
Copy patheval_state.go
File metadata and controls
102 lines (83 loc) · 2.23 KB
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
package terraform
import (
"fmt"
)
// EvalReadState is an EvalNode implementation that reads the
// InstanceState for a specific resource out of the state.
type EvalReadState struct {
Name string
}
func (n *EvalReadState) Args() ([]EvalNode, []EvalType) {
return nil, nil
}
// TODO: test
func (n *EvalReadState) Eval(
ctx EvalContext, args []interface{}) (interface{}, error) {
state, lock := ctx.State()
// Get a read lock so we can access this instance
lock.RLock()
defer lock.RUnlock()
// Look for the module state. If we don't have one, then it doesn't matter.
mod := state.ModuleByPath(ctx.Path())
if mod == nil {
return nil, nil
}
// Look for the resource state. If we don't have one, then it is okay.
rs := mod.Resources[n.Name]
if rs == nil {
return nil, nil
}
// Return the primary
return rs.Primary, nil
}
func (n *EvalReadState) Type() EvalType {
return EvalTypeInstanceState
}
// EvalWriteState is an EvalNode implementation that reads the
// InstanceState for a specific resource out of the state.
type EvalWriteState struct {
Name string
ResourceType string
Dependencies []string
State EvalNode
}
func (n *EvalWriteState) Args() ([]EvalNode, []EvalType) {
return []EvalNode{n.State}, []EvalType{EvalTypeInstanceState}
}
// TODO: test
func (n *EvalWriteState) Eval(
ctx EvalContext, args []interface{}) (interface{}, error) {
var instanceState *InstanceState
if args[0] != nil {
instanceState = args[0].(*InstanceState)
}
state, lock := ctx.State()
if state == nil {
return nil, fmt.Errorf("cannot write state to nil state")
}
// Get a write lock so we can access this instance
lock.Lock()
defer lock.Unlock()
// Look for the module state. If we don't have one, create it.
mod := state.ModuleByPath(ctx.Path())
if mod == nil {
mod = state.AddModule(ctx.Path())
}
// Look for the resource state.
rs := mod.Resources[n.Name]
if rs == nil {
rs = &ResourceState{}
rs.init()
mod.Resources[n.Name] = rs
}
rs.Type = n.ResourceType
rs.Dependencies = n.Dependencies
// Set the primary state
rs.Primary = instanceState
// Prune because why not, we can clear out old useless entries now
rs.prune()
return nil, nil
}
func (n *EvalWriteState) Type() EvalType {
return EvalTypeNull
}