forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform_attach_config_resource.go
78 lines (65 loc) · 2.07 KB
/
transform_attach_config_resource.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
78
package terraform
import (
"fmt"
"log"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/config/module"
)
// GraphNodeAttachResourceConfig is an interface that must be implemented by nodes
// that want resource configurations attached.
type GraphNodeAttachResourceConfig interface {
// ResourceAddr is the address to the resource
ResourceAddr() *ResourceAddress
// Sets the configuration
AttachResourceConfig(*config.Resource)
}
// AttachResourceConfigTransformer goes through the graph and attaches
// resource configuration structures to nodes that implement the interfaces
// above.
//
// The attached configuration structures are directly from the configuration.
// If they're going to be modified, a copy should be made.
type AttachResourceConfigTransformer struct {
Module *module.Tree // Module is the root module for the config
}
func (t *AttachResourceConfigTransformer) Transform(g *Graph) error {
log.Printf("[TRACE] AttachResourceConfigTransformer: Beginning...")
// Go through and find GraphNodeAttachResource
for _, v := range g.Vertices() {
// Only care about GraphNodeAttachResource implementations
arn, ok := v.(GraphNodeAttachResourceConfig)
if !ok {
continue
}
// Determine what we're looking for
addr := arn.ResourceAddr()
log.Printf(
"[TRACE] AttachResourceConfigTransformer: Attach resource "+
"config request: %s", addr)
// Get the configuration.
path := normalizeModulePath(addr.Path)
path = path[1:]
tree := t.Module.Child(path)
if tree == nil {
continue
}
// Go through the resource configs to find the matching config
for _, r := range tree.Config().Resources {
// Get a resource address so we can compare
a, err := parseResourceAddressConfig(r)
if err != nil {
panic(fmt.Sprintf(
"Error parsing config address, this is a bug: %#v", r))
}
a.Path = addr.Path
// If this is not the same resource, then continue
if !a.Equals(addr) {
continue
}
log.Printf("[TRACE] Attaching resource config: %#v", r)
arn.AttachResourceConfig(r)
break
}
}
return nil
}