-
Notifications
You must be signed in to change notification settings - Fork 10
/
state_init.go
77 lines (61 loc) · 2.03 KB
/
state_init.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
package checks
import (
"github.com/snyk/policy-engine/pkg/internal/terraform/addrs"
"github.com/snyk/policy-engine/pkg/internal/terraform/configs"
)
func initialStatuses(cfg *configs.Config) addrs.Map[addrs.ConfigCheckable, *configCheckableState] {
ret := addrs.MakeMap[addrs.ConfigCheckable, *configCheckableState]()
if cfg == nil {
// This should not happen in normal use, but can arise in some
// unit tests that are not working with a full configuration and
// don't care about checks.
return ret
}
collectInitialStatuses(ret, cfg)
return ret
}
func collectInitialStatuses(into addrs.Map[addrs.ConfigCheckable, *configCheckableState], cfg *configs.Config) {
moduleAddr := cfg.Path
for _, rc := range cfg.Module.ManagedResources {
addr := rc.Addr().InModule(moduleAddr)
collectInitialStatusForResource(into, addr, rc)
}
for _, rc := range cfg.Module.DataResources {
addr := rc.Addr().InModule(moduleAddr)
collectInitialStatusForResource(into, addr, rc)
}
for _, oc := range cfg.Module.Outputs {
addr := oc.Addr().InModule(moduleAddr)
ct := len(oc.Preconditions)
if ct == 0 {
// We just ignore output values that don't declare any checks.
continue
}
st := &configCheckableState{}
st.checkTypes = map[addrs.CheckType]int{
addrs.OutputPrecondition: ct,
}
into.Put(addr, st)
}
// Must also visit child modules to collect everything
for _, child := range cfg.Children {
collectInitialStatuses(into, child)
}
}
func collectInitialStatusForResource(into addrs.Map[addrs.ConfigCheckable, *configCheckableState], addr addrs.ConfigResource, rc *configs.Resource) {
if (len(rc.Preconditions) + len(rc.Postconditions)) == 0 {
// Don't bother with any resource that doesn't have at least
// one condition.
return
}
st := &configCheckableState{
checkTypes: make(map[addrs.CheckType]int),
}
if ct := len(rc.Preconditions); ct > 0 {
st.checkTypes[addrs.ResourcePrecondition] = ct
}
if ct := len(rc.Postconditions); ct > 0 {
st.checkTypes[addrs.ResourcePostcondition] = ct
}
into.Put(addr, st)
}