7 // EvalCountFixZeroOneBoundaryGlobal is an EvalNode that fixes up the state
8 // when there is a resource count with zero/one boundary, i.e. fixing
9 // a resource named "aws_instance.foo" to "aws_instance.foo.0" and vice-versa.
11 // This works on the global state.
12 type EvalCountFixZeroOneBoundaryGlobal struct{}
15 func (n *EvalCountFixZeroOneBoundaryGlobal) Eval(ctx EvalContext) (interface{}, error) {
16 // Get the state and lock it since we'll potentially modify it
17 state, lock := ctx.State()
21 // Prune the state since we require a clean state to work
24 // Go through each modules since the boundaries are restricted to a
26 for _, m := range state.Modules {
27 if err := n.fixModule(m); err != nil {
35 func (n *EvalCountFixZeroOneBoundaryGlobal) fixModule(m *ModuleState) error {
36 // Counts keeps track of keys and their counts
37 counts := make(map[string]int)
38 for k, _ := range m.Resources {
40 key, err := ParseResourceStateKey(k)
45 // Set the index to -1 so that we can keep count
49 counts[key.String()]++
52 // Go through the counts and do the fixup for each resource
53 for raw, count := range counts {
54 // Search and replace this resource
58 search, replace = replace, search
60 log.Printf("[TRACE] EvalCountFixZeroOneBoundaryGlobal: count %d, search %q, replace %q", count, search, replace)
62 // Look for the resource state. If we don't have one, then it is okay.
63 rs, ok := m.Resources[search]
68 // If the replacement key exists, we just keep both
69 if _, ok := m.Resources[replace]; ok {
73 m.Resources[replace] = rs
74 delete(m.Resources, search)