github.com/hashicorp/terraform-plugin-sdk@v1.17.2/terraform/transform_attach_config_resource.go (about) 1 package terraform 2 3 import ( 4 "log" 5 6 "github.com/hashicorp/terraform-plugin-sdk/internal/configs" 7 "github.com/hashicorp/terraform-plugin-sdk/internal/dag" 8 ) 9 10 // GraphNodeAttachResourceConfig is an interface that must be implemented by nodes 11 // that want resource configurations attached. 12 type GraphNodeAttachResourceConfig interface { 13 GraphNodeResource 14 15 // Sets the configuration 16 AttachResourceConfig(*configs.Resource) 17 } 18 19 // AttachResourceConfigTransformer goes through the graph and attaches 20 // resource configuration structures to nodes that implement 21 // GraphNodeAttachManagedResourceConfig or GraphNodeAttachDataResourceConfig. 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 { 26 Config *configs.Config // Config is the root node in the config tree 27 } 28 29 func (t *AttachResourceConfigTransformer) Transform(g *Graph) error { 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() 41 42 // Get the configuration. 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) 46 continue 47 } 48 49 for _, r := range config.Module.ManagedResources { 50 rAddr := r.Addr() 51 52 if rAddr != addr.Resource { 53 // Not the same resource 54 continue 55 } 56 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 65 continue 66 } 67 68 log.Printf("[TRACE] AttachResourceConfigTransformer: attaching to %q (%T) config from %#v", dag.VertexName(v), v, r.DeclRange) 69 arn.AttachResourceConfig(r) 70 } 71 } 72 73 return nil 74 }