summaryrefslogtreecommitdiff
path: root/vendor/github.com/hashicorp/terraform/terraform/eval_count_boundary.go
blob: 91e2b904e92cf64d85fdfb31c5e6e8365c169fb4 (plain)
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
package terraform

import (
	"log"
)

// EvalCountFixZeroOneBoundaryGlobal is an EvalNode that fixes up the state
// when there is a resource count with zero/one boundary, i.e. fixing
// a resource named "aws_instance.foo" to "aws_instance.foo.0" and vice-versa.
//
// This works on the global state.
type EvalCountFixZeroOneBoundaryGlobal struct{}

// TODO: test
func (n *EvalCountFixZeroOneBoundaryGlobal) Eval(ctx EvalContext) (interface{}, error) {
	// Get the state and lock it since we'll potentially modify it
	state, lock := ctx.State()
	lock.Lock()
	defer lock.Unlock()

	// Prune the state since we require a clean state to work
	state.prune()

	// Go through each modules since the boundaries are restricted to a
	// module scope.
	for _, m := range state.Modules {
		if err := n.fixModule(m); err != nil {
			return nil, err
		}
	}

	return nil, nil
}

func (n *EvalCountFixZeroOneBoundaryGlobal) fixModule(m *ModuleState) error {
	// Counts keeps track of keys and their counts
	counts := make(map[string]int)
	for k, _ := range m.Resources {
		// Parse the key
		key, err := ParseResourceStateKey(k)
		if err != nil {
			return err
		}

		// Set the index to -1 so that we can keep count
		key.Index = -1

		// Increment
		counts[key.String()]++
	}

	// Go through the counts and do the fixup for each resource
	for raw, count := range counts {
		// Search and replace this resource
		search := raw
		replace := raw + ".0"
		if count < 2 {
			search, replace = replace, search
		}
		log.Printf("[TRACE] EvalCountFixZeroOneBoundaryGlobal: count %d, search %q, replace %q", count, search, replace)

		// Look for the resource state. If we don't have one, then it is okay.
		rs, ok := m.Resources[search]
		if !ok {
			continue
		}

		// If the replacement key exists, we just keep both
		if _, ok := m.Resources[replace]; ok {
			continue
		}

		m.Resources[replace] = rs
		delete(m.Resources, search)
	}

	return nil
}