forked from saulshanabrook/terraform
-
Notifications
You must be signed in to change notification settings - Fork 1
/
eval_count.go
58 lines (47 loc) · 1.4 KB
/
eval_count.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
package terraform
import (
"github.com/hashicorp/terraform/config"
)
// EvalCountFixZeroOneBoundary 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.
type EvalCountFixZeroOneBoundary struct {
Resource *config.Resource
}
// TODO: test
func (n *EvalCountFixZeroOneBoundary) Eval(ctx EvalContext) (interface{}, error) {
// Get the count, important for knowing whether we're supposed to
// be adding the zero, or trimming it.
count, err := n.Resource.Count()
if err != nil {
return nil, err
}
// Figure what to look for and what to replace it with
hunt := n.Resource.Id()
replace := hunt + ".0"
if count < 2 {
hunt, replace = replace, hunt
}
state, lock := ctx.State()
// Get a lock so we can access this instance and potentially make
// changes to it.
lock.Lock()
defer lock.Unlock()
// 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, ok := mod.Resources[hunt]
if !ok {
return nil, nil
}
// If the replacement key exists, we just keep both
if _, ok := mod.Resources[replace]; ok {
return nil, nil
}
mod.Resources[replace] = rs
delete(mod.Resources, hunt)
return nil, nil
}