]>
Commit | Line | Data |
---|---|---|
bae9f6d2 JC |
1 | package terraform |
2 | ||
3 | import ( | |
bae9f6d2 JC |
4 | "log" |
5 | ||
107c1cdb ND |
6 | "github.com/hashicorp/terraform/configs" |
7 | "github.com/hashicorp/terraform/dag" | |
bae9f6d2 JC |
8 | ) |
9 | ||
10 | // GraphNodeAttachResourceConfig is an interface that must be implemented by nodes | |
11 | // that want resource configurations attached. | |
12 | type GraphNodeAttachResourceConfig interface { | |
107c1cdb | 13 | GraphNodeResource |
bae9f6d2 JC |
14 | |
15 | // Sets the configuration | |
107c1cdb | 16 | AttachResourceConfig(*configs.Resource) |
bae9f6d2 JC |
17 | } |
18 | ||
19 | // AttachResourceConfigTransformer goes through the graph and attaches | |
107c1cdb ND |
20 | // resource configuration structures to nodes that implement |
21 | // GraphNodeAttachManagedResourceConfig or GraphNodeAttachDataResourceConfig. | |
bae9f6d2 JC |
22 | // |
23 | // The attached configuration structures are directly from the configuration. | |
24 | // If they're going to be modified, a copy should be made. | |
25 | type AttachResourceConfigTransformer struct { | |
107c1cdb | 26 | Config *configs.Config // Config is the root node in the config tree |
bae9f6d2 JC |
27 | } |
28 | ||
29 | func (t *AttachResourceConfigTransformer) Transform(g *Graph) error { | |
bae9f6d2 JC |
30 | |
31 | // Go through and find GraphNodeAttachResource | |
32 | for _, v := range g.Vertices() { | |
33 | // Only care about GraphNodeAttachResource implementations | |
34 | arn, ok := v.(GraphNodeAttachResourceConfig) | |
35 | if !ok { | |
36 | continue | |
37 | } | |
38 | ||
39 | // Determine what we're looking for | |
40 | addr := arn.ResourceAddr() | |
bae9f6d2 JC |
41 | |
42 | // Get the configuration. | |
107c1cdb ND |
43 | config := t.Config.DescendentForInstance(addr.Module) |
44 | if config == nil { | |
45 | log.Printf("[TRACE] AttachResourceConfigTransformer: %q (%T) has no configuration available", dag.VertexName(v), v) | |
bae9f6d2 JC |
46 | continue |
47 | } | |
48 | ||
107c1cdb ND |
49 | for _, r := range config.Module.ManagedResources { |
50 | rAddr := r.Addr() | |
51 | ||
52 | if rAddr != addr.Resource { | |
53 | // Not the same resource | |
54 | continue | |
bae9f6d2 | 55 | } |
bae9f6d2 | 56 | |
107c1cdb ND |
57 | log.Printf("[TRACE] AttachResourceConfigTransformer: attaching to %q (%T) config from %s", dag.VertexName(v), v, r.DeclRange) |
58 | arn.AttachResourceConfig(r) | |
59 | } | |
60 | for _, r := range config.Module.DataResources { | |
61 | rAddr := r.Addr() | |
62 | ||
63 | if rAddr != addr.Resource { | |
64 | // Not the same resource | |
bae9f6d2 JC |
65 | continue |
66 | } | |
67 | ||
107c1cdb | 68 | log.Printf("[TRACE] AttachResourceConfigTransformer: attaching to %q (%T) config from %#v", dag.VertexName(v), v, r.DeclRange) |
bae9f6d2 | 69 | arn.AttachResourceConfig(r) |
bae9f6d2 JC |
70 | } |
71 | } | |
72 | ||
73 | return nil | |
74 | } |