-
Notifications
You must be signed in to change notification settings - Fork 522
/
graph_vertex_variable.go
73 lines (57 loc) 路 1.76 KB
/
graph_vertex_variable.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
package hcl
import (
"fmt"
"sync"
"github.com/rs/zerolog"
"github.com/zclconf/go-cty/cty"
)
type VertexVariable struct {
logger zerolog.Logger
moduleConfigs *ModuleConfigs
block *Block
}
func (v *VertexVariable) ID() string {
return v.block.FullName()
}
func (v *VertexVariable) ModuleAddress() string {
return v.block.ModuleAddress()
}
func (v *VertexVariable) References() []VertexReference {
return v.block.VerticesReferenced()
}
func (v *VertexVariable) Visit(mutex *sync.Mutex) error {
mutex.Lock()
defer mutex.Unlock()
moduleInstances := v.moduleConfigs.Get(v.block.ModuleAddress())
if len(moduleInstances) == 0 {
return fmt.Errorf("no module instances found for module address %q", v.block.ModuleAddress())
}
for _, moduleInstance := range moduleInstances {
e := moduleInstance.evaluator
blockInstance := e.module.Blocks.FindLocalName(v.block.LocalName())
if blockInstance == nil {
return fmt.Errorf("could not find block %q in module %q", v.ID(), moduleInstance.name)
}
// Re-evaluate the matching module input variables for this variable block
// to ensure we have the most up-to-date value
inputVars := e.inputVars
if moduleInstance.moduleCall != nil {
attrName := v.block.TypeLabel()
attr, ok := moduleInstance.moduleCall.Definition.AttributesAsMap()[attrName]
if ok {
inputVars = map[string]cty.Value{
attrName: attr.Value(),
}
}
}
val, err := e.evaluateVariable(blockInstance, inputVars)
if err != nil {
return fmt.Errorf("could not evaluate variable %s: %w", v.ID(), err)
}
v.logger.Debug().Msgf("adding variable %s to the evaluation context", v.ID())
key := fmt.Sprintf("var.%s", blockInstance.Label())
e.ctx.SetByDot(val, key)
e.AddFilteredBlocks(blockInstance)
}
return nil
}