From d905b990a58e0b5d18b56e4abe5f67ae8aced557 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Sun, 15 Mar 2020 11:32:06 -0400 Subject: [PATCH] s/GraphNodeResource/GraphNodeConfigResource/ Make the interface name reflect the new return type of the method. Remove the confusingly named and unused ResourceAddress method from the resource nodes as well. --- terraform/eval_count.go | 4 +- terraform/eval_count_boundary.go | 2 +- terraform/node_data_refresh.go | 2 +- terraform/node_resource_abstract.go | 19 +++--- terraform/node_resource_apply.go | 2 +- terraform/node_resource_apply_instance.go | 11 +++- terraform/node_resource_destroy.go | 12 ++-- terraform/node_resource_destroy_deposed.go | 4 +- terraform/node_resource_plan.go | 2 +- terraform/node_resource_plan_destroy.go | 2 +- terraform/node_resource_plan_instance.go | 2 +- terraform/node_resource_plan_orphan.go | 2 +- terraform/node_resource_refresh.go | 13 +++-- terraform/node_resource_validate.go | 2 +- terraform/resource_test.go | 58 ------------------- .../transform_attach_config_provider_meta.go | 2 +- terraform/transform_attach_config_resource.go | 4 +- terraform/transform_attach_schema.go | 2 +- terraform/transform_diff.go | 4 +- terraform/transform_orphan_count.go | 49 +++++++--------- terraform/transform_orphan_resource.go | 2 +- terraform/transform_reference.go | 16 ++--- terraform/transform_resource_count.go | 2 +- terraform/transform_targets.go | 8 +-- 24 files changed, 83 insertions(+), 143 deletions(-) diff --git a/terraform/eval_count.go b/terraform/eval_count.go index 4a38f6e94..3da947f1f 100644 --- a/terraform/eval_count.go +++ b/terraform/eval_count.go @@ -111,9 +111,9 @@ func evaluateResourceCountExpressionKnown(expr hcl.Expression, ctx EvalContext) // Since the state is modified in-place, this function must take a writer lock // on the state. The caller must therefore not also be holding a state lock, // or this function will block forever awaiting the lock. -func fixResourceCountSetTransition(ctx EvalContext, addr addrs.AbsResource, countEnabled bool) { +func fixResourceCountSetTransition(ctx EvalContext, addr addrs.ConfigResource, countEnabled bool) { state := ctx.State() - changed := state.MaybeFixUpResourceInstanceAddressForCount(addr.Config(), countEnabled) + changed := state.MaybeFixUpResourceInstanceAddressForCount(addr, countEnabled) if changed { log.Printf("[TRACE] renamed first %s instance in transient state due to count argument change", addr) } diff --git a/terraform/eval_count_boundary.go b/terraform/eval_count_boundary.go index 32ecca173..855f14897 100644 --- a/terraform/eval_count_boundary.go +++ b/terraform/eval_count_boundary.go @@ -69,7 +69,7 @@ func (n *EvalCountFixZeroOneBoundaryGlobal) fixModule(ctx EvalContext, moduleAdd continue } hasCount := rCfg.Count != nil - fixResourceCountSetTransition(ctx, r.Addr, hasCount) + fixResourceCountSetTransition(ctx, r.Addr.Config(), hasCount) } return nil diff --git a/terraform/node_data_refresh.go b/terraform/node_data_refresh.go index b16a73bd9..02d230155 100644 --- a/terraform/node_data_refresh.go +++ b/terraform/node_data_refresh.go @@ -19,7 +19,7 @@ var ( _ GraphNodeDynamicExpandable = (*NodeRefreshableDataResource)(nil) _ GraphNodeReferenceable = (*NodeRefreshableDataResource)(nil) _ GraphNodeReferencer = (*NodeRefreshableDataResource)(nil) - _ GraphNodeResource = (*NodeRefreshableDataResource)(nil) + _ GraphNodeConfigResource = (*NodeRefreshableDataResource)(nil) _ GraphNodeAttachResourceConfig = (*NodeRefreshableDataResource)(nil) _ GraphNodeAttachProviderMetaConfigs = (*NodeAbstractResource)(nil) ) diff --git a/terraform/node_resource_abstract.go b/terraform/node_resource_abstract.go index 0fc3ee0ea..77086bd1d 100644 --- a/terraform/node_resource_abstract.go +++ b/terraform/node_resource_abstract.go @@ -16,11 +16,11 @@ import ( // abstract resource to a concrete one of some type. type ConcreteResourceNodeFunc func(*NodeAbstractResource) dag.Vertex -// GraphNodeResource is implemented by any nodes that represent a resource. +// GraphNodeConfigResource is implemented by any nodes that represent a resource. // The type of operation cannot be assumed, only that this node represents // the given resource. -type GraphNodeResource interface { - ResourceAddr() addrs.AbsResource +type GraphNodeConfigResource interface { + ResourceAddr() addrs.ConfigResource } // ConcreteResourceInstanceNodeFunc is a callback type used to convert an @@ -69,7 +69,7 @@ var ( _ GraphNodeReferencer = (*NodeAbstractResource)(nil) _ GraphNodeProviderConsumer = (*NodeAbstractResource)(nil) _ GraphNodeProvisionerConsumer = (*NodeAbstractResource)(nil) - _ GraphNodeResource = (*NodeAbstractResource)(nil) + _ GraphNodeConfigResource = (*NodeAbstractResource)(nil) _ GraphNodeAttachResourceConfig = (*NodeAbstractResource)(nil) _ GraphNodeAttachResourceSchema = (*NodeAbstractResource)(nil) _ GraphNodeAttachProvisionerSchema = (*NodeAbstractResource)(nil) @@ -117,7 +117,7 @@ var ( _ GraphNodeReferencer = (*NodeAbstractResourceInstance)(nil) _ GraphNodeProviderConsumer = (*NodeAbstractResourceInstance)(nil) _ GraphNodeProvisionerConsumer = (*NodeAbstractResourceInstance)(nil) - _ GraphNodeResource = (*NodeAbstractResourceInstance)(nil) + _ GraphNodeConfigResource = (*NodeAbstractResourceInstance)(nil) _ GraphNodeResourceInstance = (*NodeAbstractResourceInstance)(nil) _ GraphNodeAttachResourceState = (*NodeAbstractResourceInstance)(nil) _ GraphNodeAttachResourceConfig = (*NodeAbstractResourceInstance)(nil) @@ -375,8 +375,8 @@ func (n *NodeAbstractResource) AttachProvisionerSchema(name string, schema *conf } // GraphNodeResource -func (n *NodeAbstractResource) ResourceAddr() addrs.AbsResource { - return n.addr() +func (n *NodeAbstractResource) ResourceAddr() addrs.ConfigResource { + return n.Addr } // GraphNodeResourceInstance @@ -384,11 +384,6 @@ func (n *NodeAbstractResourceInstance) ResourceInstanceAddr() addrs.AbsResourceI return n.NodeAbstractResource.addr().Instance(n.InstanceKey) } -// GraphNodeAddressable, TODO: remove, used by target, should unify -func (n *NodeAbstractResource) ResourceAddress() *ResourceAddress { - return NewLegacyResourceAddress(n.addr()) -} - // GraphNodeTargetable func (n *NodeAbstractResource) SetTargets(targets []addrs.Targetable) { n.Targets = targets diff --git a/terraform/node_resource_apply.go b/terraform/node_resource_apply.go index c21158a9f..2423ad04b 100644 --- a/terraform/node_resource_apply.go +++ b/terraform/node_resource_apply.go @@ -21,7 +21,7 @@ type NodeApplyableResource struct { } var ( - _ GraphNodeResource = (*NodeApplyableResource)(nil) + _ GraphNodeConfigResource = (*NodeApplyableResource)(nil) _ GraphNodeEvalable = (*NodeApplyableResource)(nil) _ GraphNodeProviderConsumer = (*NodeApplyableResource)(nil) _ GraphNodeAttachResourceConfig = (*NodeApplyableResource)(nil) diff --git a/terraform/node_resource_apply_instance.go b/terraform/node_resource_apply_instance.go index 84c1fd148..5d767dd20 100644 --- a/terraform/node_resource_apply_instance.go +++ b/terraform/node_resource_apply_instance.go @@ -28,7 +28,7 @@ type NodeApplyableResourceInstance struct { } var ( - _ GraphNodeResource = (*NodeApplyableResourceInstance)(nil) + _ GraphNodeConfigResource = (*NodeApplyableResourceInstance)(nil) _ GraphNodeResourceInstance = (*NodeApplyableResourceInstance)(nil) _ GraphNodeCreator = (*NodeApplyableResourceInstance)(nil) _ GraphNodeReferencer = (*NodeApplyableResourceInstance)(nil) @@ -99,8 +99,13 @@ func (n *NodeApplyableResourceInstance) References() []*addrs.Reference { } // GraphNodeAttachDependencies -func (n *NodeApplyableResourceInstance) AttachDependencies(deps []addrs.AbsResource) { - n.Dependencies = deps +func (n *NodeApplyableResourceInstance) AttachDependencies(deps []addrs.ConfigResource) { + var shimmed []addrs.AbsResource + for _, r := range deps { + shimmed = append(shimmed, r.Absolute(r.Module.UnkeyedInstanceShim())) + } + + n.Dependencies = shimmed } // GraphNodeEvalable diff --git a/terraform/node_resource_destroy.go b/terraform/node_resource_destroy.go index 54fba1dec..16c327fd1 100644 --- a/terraform/node_resource_destroy.go +++ b/terraform/node_resource_destroy.go @@ -26,7 +26,7 @@ type NodeDestroyResourceInstance struct { } var ( - _ GraphNodeResource = (*NodeDestroyResourceInstance)(nil) + _ GraphNodeConfigResource = (*NodeDestroyResourceInstance)(nil) _ GraphNodeResourceInstance = (*NodeDestroyResourceInstance)(nil) _ GraphNodeDestroyer = (*NodeDestroyResourceInstance)(nil) _ GraphNodeDestroyerCBD = (*NodeDestroyResourceInstance)(nil) @@ -292,10 +292,10 @@ type NodeDestroyResource struct { } var ( - _ GraphNodeResource = (*NodeDestroyResource)(nil) - _ GraphNodeReferenceable = (*NodeDestroyResource)(nil) - _ GraphNodeReferencer = (*NodeDestroyResource)(nil) - _ GraphNodeEvalable = (*NodeDestroyResource)(nil) + _ GraphNodeConfigResource = (*NodeDestroyResource)(nil) + _ GraphNodeReferenceable = (*NodeDestroyResource)(nil) + _ GraphNodeReferencer = (*NodeDestroyResource)(nil) + _ GraphNodeEvalable = (*NodeDestroyResource)(nil) // FIXME: this is here to document that this node is both // GraphNodeProviderConsumer by virtue of the embedded @@ -339,7 +339,7 @@ func (n *NodeDestroyResource) EvalTree() EvalNode { } // GraphNodeResource -func (n *NodeDestroyResource) ResourceAddr() addrs.AbsResource { +func (n *NodeDestroyResource) ResourceAddr() addrs.ConfigResource { return n.NodeAbstractResource.ResourceAddr() } diff --git a/terraform/node_resource_destroy_deposed.go b/terraform/node_resource_destroy_deposed.go index 7afab72bc..162655bf3 100644 --- a/terraform/node_resource_destroy_deposed.go +++ b/terraform/node_resource_destroy_deposed.go @@ -33,7 +33,7 @@ type NodePlanDeposedResourceInstanceObject struct { var ( _ GraphNodeDeposedResourceInstanceObject = (*NodePlanDeposedResourceInstanceObject)(nil) - _ GraphNodeResource = (*NodePlanDeposedResourceInstanceObject)(nil) + _ GraphNodeConfigResource = (*NodePlanDeposedResourceInstanceObject)(nil) _ GraphNodeResourceInstance = (*NodePlanDeposedResourceInstanceObject)(nil) _ GraphNodeReferenceable = (*NodePlanDeposedResourceInstanceObject)(nil) _ GraphNodeReferencer = (*NodePlanDeposedResourceInstanceObject)(nil) @@ -167,7 +167,7 @@ type NodeDestroyDeposedResourceInstanceObject struct { var ( _ GraphNodeDeposedResourceInstanceObject = (*NodeDestroyDeposedResourceInstanceObject)(nil) - _ GraphNodeResource = (*NodeDestroyDeposedResourceInstanceObject)(nil) + _ GraphNodeConfigResource = (*NodeDestroyDeposedResourceInstanceObject)(nil) _ GraphNodeResourceInstance = (*NodeDestroyDeposedResourceInstanceObject)(nil) _ GraphNodeDestroyer = (*NodeDestroyDeposedResourceInstanceObject)(nil) _ GraphNodeDestroyerCBD = (*NodeDestroyDeposedResourceInstanceObject)(nil) diff --git a/terraform/node_resource_plan.go b/terraform/node_resource_plan.go index 7d61d9ce0..0a339e6bb 100644 --- a/terraform/node_resource_plan.go +++ b/terraform/node_resource_plan.go @@ -23,7 +23,7 @@ var ( _ GraphNodeDynamicExpandable = (*NodePlannableResource)(nil) _ GraphNodeReferenceable = (*NodePlannableResource)(nil) _ GraphNodeReferencer = (*NodePlannableResource)(nil) - _ GraphNodeResource = (*NodePlannableResource)(nil) + _ GraphNodeConfigResource = (*NodePlannableResource)(nil) _ GraphNodeAttachResourceConfig = (*NodePlannableResource)(nil) ) diff --git a/terraform/node_resource_plan_destroy.go b/terraform/node_resource_plan_destroy.go index deb6b4f7b..d53c1441c 100644 --- a/terraform/node_resource_plan_destroy.go +++ b/terraform/node_resource_plan_destroy.go @@ -21,7 +21,7 @@ var ( _ GraphNodeReferenceable = (*NodePlanDestroyableResourceInstance)(nil) _ GraphNodeReferencer = (*NodePlanDestroyableResourceInstance)(nil) _ GraphNodeDestroyer = (*NodePlanDestroyableResourceInstance)(nil) - _ GraphNodeResource = (*NodePlanDestroyableResourceInstance)(nil) + _ GraphNodeConfigResource = (*NodePlanDestroyableResourceInstance)(nil) _ GraphNodeResourceInstance = (*NodePlanDestroyableResourceInstance)(nil) _ GraphNodeAttachResourceConfig = (*NodePlanDestroyableResourceInstance)(nil) _ GraphNodeAttachResourceState = (*NodePlanDestroyableResourceInstance)(nil) diff --git a/terraform/node_resource_plan_instance.go b/terraform/node_resource_plan_instance.go index 27465981a..38c115842 100644 --- a/terraform/node_resource_plan_instance.go +++ b/terraform/node_resource_plan_instance.go @@ -23,7 +23,7 @@ var ( _ GraphNodeModuleInstance = (*NodePlannableResourceInstance)(nil) _ GraphNodeReferenceable = (*NodePlannableResourceInstance)(nil) _ GraphNodeReferencer = (*NodePlannableResourceInstance)(nil) - _ GraphNodeResource = (*NodePlannableResourceInstance)(nil) + _ GraphNodeConfigResource = (*NodePlannableResourceInstance)(nil) _ GraphNodeResourceInstance = (*NodePlannableResourceInstance)(nil) _ GraphNodeAttachResourceConfig = (*NodePlannableResourceInstance)(nil) _ GraphNodeAttachResourceState = (*NodePlannableResourceInstance)(nil) diff --git a/terraform/node_resource_plan_orphan.go b/terraform/node_resource_plan_orphan.go index 61f8e621f..20ad53c35 100644 --- a/terraform/node_resource_plan_orphan.go +++ b/terraform/node_resource_plan_orphan.go @@ -16,7 +16,7 @@ var ( _ GraphNodeModuleInstance = (*NodePlannableResourceInstanceOrphan)(nil) _ GraphNodeReferenceable = (*NodePlannableResourceInstanceOrphan)(nil) _ GraphNodeReferencer = (*NodePlannableResourceInstanceOrphan)(nil) - _ GraphNodeResource = (*NodePlannableResourceInstanceOrphan)(nil) + _ GraphNodeConfigResource = (*NodePlannableResourceInstanceOrphan)(nil) _ GraphNodeResourceInstance = (*NodePlannableResourceInstanceOrphan)(nil) _ GraphNodeAttachResourceConfig = (*NodePlannableResourceInstanceOrphan)(nil) _ GraphNodeAttachResourceState = (*NodePlannableResourceInstanceOrphan)(nil) diff --git a/terraform/node_resource_refresh.go b/terraform/node_resource_refresh.go index ee3f084be..2284d1e43 100644 --- a/terraform/node_resource_refresh.go +++ b/terraform/node_resource_refresh.go @@ -28,14 +28,19 @@ var ( _ GraphNodeDynamicExpandable = (*NodeRefreshableManagedResource)(nil) _ GraphNodeReferenceable = (*NodeRefreshableManagedResource)(nil) _ GraphNodeReferencer = (*NodeRefreshableManagedResource)(nil) - _ GraphNodeResource = (*NodeRefreshableManagedResource)(nil) + _ GraphNodeConfigResource = (*NodeRefreshableManagedResource)(nil) _ GraphNodeAttachResourceConfig = (*NodeRefreshableManagedResource)(nil) _ GraphNodeAttachDependencies = (*NodeRefreshableManagedResource)(nil) ) // GraphNodeAttachDependencies -func (n *NodeRefreshableManagedResource) AttachDependencies(deps []addrs.AbsResource) { - n.Dependencies = deps +func (n *NodeRefreshableManagedResource) AttachDependencies(deps []addrs.ConfigResource) { + var shimmed []addrs.AbsResource + for _, r := range deps { + shimmed = append(shimmed, r.Absolute(r.Module.UnkeyedInstanceShim())) + } + + n.Dependencies = shimmed } // GraphNodeDynamicExpandable @@ -144,7 +149,7 @@ var ( _ GraphNodeReferenceable = (*NodeRefreshableManagedResourceInstance)(nil) _ GraphNodeReferencer = (*NodeRefreshableManagedResourceInstance)(nil) _ GraphNodeDestroyer = (*NodeRefreshableManagedResourceInstance)(nil) - _ GraphNodeResource = (*NodeRefreshableManagedResourceInstance)(nil) + _ GraphNodeConfigResource = (*NodeRefreshableManagedResourceInstance)(nil) _ GraphNodeResourceInstance = (*NodeRefreshableManagedResourceInstance)(nil) _ GraphNodeAttachResourceConfig = (*NodeRefreshableManagedResourceInstance)(nil) _ GraphNodeAttachResourceState = (*NodeRefreshableManagedResourceInstance)(nil) diff --git a/terraform/node_resource_validate.go b/terraform/node_resource_validate.go index f8374f3fb..83a67ed0c 100644 --- a/terraform/node_resource_validate.go +++ b/terraform/node_resource_validate.go @@ -18,7 +18,7 @@ var ( _ GraphNodeEvalable = (*NodeValidatableResource)(nil) _ GraphNodeReferenceable = (*NodeValidatableResource)(nil) _ GraphNodeReferencer = (*NodeValidatableResource)(nil) - _ GraphNodeResource = (*NodeValidatableResource)(nil) + _ GraphNodeConfigResource = (*NodeValidatableResource)(nil) _ GraphNodeAttachResourceConfig = (*NodeValidatableResource)(nil) _ GraphNodeAttachProviderMetaConfigs = (*NodeValidatableResource)(nil) ) diff --git a/terraform/resource_test.go b/terraform/resource_test.go index 5ad2de415..835163c4a 100644 --- a/terraform/resource_test.go +++ b/terraform/resource_test.go @@ -3,7 +3,6 @@ package terraform import ( "fmt" "reflect" - "strconv" "testing" "github.com/hashicorp/terraform/configs/configschema" @@ -13,63 +12,6 @@ import ( "github.com/mitchellh/reflectwalk" ) -func TestInstanceInfoResourceAddress(t *testing.T) { - tests := []struct { - Input *InstanceInfo - Want string - }{ - { - &InstanceInfo{ - Id: "test_resource.baz", - }, - "test_resource.baz", - }, - { - &InstanceInfo{ - Id: "test_resource.baz", - ModulePath: rootModulePath, - }, - "test_resource.baz", - }, - { - &InstanceInfo{ - Id: "test_resource.baz", - ModulePath: []string{"root", "foo"}, - }, - "module.foo.test_resource.baz", - }, - { - &InstanceInfo{ - Id: "test_resource.baz", - ModulePath: []string{"root", "foo", "bar"}, - }, - "module.foo.module.bar.test_resource.baz", - }, - { - &InstanceInfo{ - Id: "test_resource.baz (tainted)", - }, - "test_resource.baz.tainted", - }, - { - &InstanceInfo{ - Id: "test_resource.baz (deposed #0)", - }, - "test_resource.baz.deposed", - }, - } - - for i, test := range tests { - t.Run(strconv.Itoa(i), func(t *testing.T) { - gotAddr := test.Input.ResourceAddress() - got := gotAddr.String() - if got != test.Want { - t.Fatalf("wrong result\ngot: %s\nwant: %s", got, test.Want) - } - }) - } -} - func TestResourceConfigGet(t *testing.T) { fooStringSchema := &configschema.Block{ Attributes: map[string]*configschema.Attribute{ diff --git a/terraform/transform_attach_config_provider_meta.go b/terraform/transform_attach_config_provider_meta.go index 9947d120c..4eab86d7e 100644 --- a/terraform/transform_attach_config_provider_meta.go +++ b/terraform/transform_attach_config_provider_meta.go @@ -8,7 +8,7 @@ import ( // GraphNodeAttachProviderMetaConfigs is an interface that must be implemented // by nodes that want provider meta configurations attached. type GraphNodeAttachProviderMetaConfigs interface { - GraphNodeResource + GraphNodeConfigResource // Sets the configuration AttachProviderMetaConfigs(map[addrs.Provider]*configs.ProviderMeta) diff --git a/terraform/transform_attach_config_resource.go b/terraform/transform_attach_config_resource.go index a0f981bae..37afbde2e 100644 --- a/terraform/transform_attach_config_resource.go +++ b/terraform/transform_attach_config_resource.go @@ -10,7 +10,7 @@ import ( // GraphNodeAttachResourceConfig is an interface that must be implemented by nodes // that want resource configurations attached. type GraphNodeAttachResourceConfig interface { - GraphNodeResource + GraphNodeConfigResource // Sets the configuration AttachResourceConfig(*configs.Resource) @@ -40,7 +40,7 @@ func (t *AttachResourceConfigTransformer) Transform(g *Graph) error { addr := arn.ResourceAddr() // Get the configuration. - config := t.Config.DescendentForInstance(addr.Module) + config := t.Config.Descendent(addr.Module) if config == nil { log.Printf("[TRACE] AttachResourceConfigTransformer: %q (%T) has no configuration available", dag.VertexName(v), v) continue diff --git a/terraform/transform_attach_schema.go b/terraform/transform_attach_schema.go index 3165b0df1..0e1096392 100644 --- a/terraform/transform_attach_schema.go +++ b/terraform/transform_attach_schema.go @@ -13,7 +13,7 @@ import ( // GraphNodeAttachResourceSchema is an interface implemented by node types // that need a resource schema attached. type GraphNodeAttachResourceSchema interface { - GraphNodeResource + GraphNodeConfigResource GraphNodeProviderConsumer AttachResourceSchema(schema *configschema.Block, version uint64) diff --git a/terraform/transform_diff.go b/terraform/transform_diff.go index 637309fad..bed71a0e3 100644 --- a/terraform/transform_diff.go +++ b/terraform/transform_diff.go @@ -36,9 +36,9 @@ func (t *DiffTransformer) Transform(g *Graph) error { // get evaluated before any of the corresponding instances by creating // dependency edges, so we'll do some prep work here to ensure we'll only // create connections to nodes that existed before we started here. - resourceNodes := map[string][]GraphNodeResource{} + resourceNodes := map[string][]GraphNodeConfigResource{} for _, node := range g.Vertices() { - rn, ok := node.(GraphNodeResource) + rn, ok := node.(GraphNodeConfigResource) if !ok { continue } diff --git a/terraform/transform_orphan_count.go b/terraform/transform_orphan_count.go index 59c15c198..ed3dc8f15 100644 --- a/terraform/transform_orphan_count.go +++ b/terraform/transform_orphan_count.go @@ -18,44 +18,39 @@ import ( type OrphanResourceCountTransformer struct { Concrete ConcreteResourceInstanceNodeFunc - Addr addrs.AbsResource // Addr of the resource to look for orphans + Addr addrs.ConfigResource // Addr of the resource to look for orphans InstanceAddrs []addrs.AbsResourceInstance // Addresses that currently exist in config State *states.State // Full global state } func (t *OrphanResourceCountTransformer) Transform(g *Graph) error { - // FIXME: This is currently assuming that all of the instances of - // this resource belong to a single module instance, which is true - // at the time of writing this because Terraform Core doesn't support - // repetition of module calls yet, but this will need to be corrected - // in order to support count and for_each on module calls, where - // our t.InstanceAddrs may contain resource instances from many different - // module instances. - rs := t.State.Resource(t.Addr) - if rs == nil { + resources := t.State.Resources(t.Addr) + if len(resources) == 0 { return nil // Resource doesn't exist in state, so nothing to do! } - // This is an O(n*m) analysis, which we accept for now because the - // number of instances of a single resource ought to always be small in any - // reasonable Terraform configuration. -Have: - for key := range rs.Instances { - thisAddr := t.Addr.Instance(key) - for _, wantAddr := range t.InstanceAddrs { - if wantAddr.Equal(thisAddr) { - continue Have + for _, rs := range resources { + // This is an O(n*m) analysis, which we accept for now because the + // number of instances of a single resource ought to always be small in any + // reasonable Terraform configuration. + Have: + for key := range rs.Instances { + thisAddr := rs.Addr.Instance(key) + for _, wantAddr := range t.InstanceAddrs { + if wantAddr.Equal(thisAddr) { + continue Have + } } - } - // If thisAddr is not in t.InstanceAddrs then we've found an "orphan" + // If thisAddr is not in t.InstanceAddrs then we've found an "orphan" - abstract := NewNodeAbstractResourceInstance(thisAddr) - var node dag.Vertex = abstract - if f := t.Concrete; f != nil { - node = f(abstract) + abstract := NewNodeAbstractResourceInstance(thisAddr) + var node dag.Vertex = abstract + if f := t.Concrete; f != nil { + node = f(abstract) + } + log.Printf("[TRACE] OrphanResourceCountTransformer: adding %s as %T", thisAddr, node) + g.Add(node) } - log.Printf("[TRACE] OrphanResourceCountTransformer: adding %s as %T", thisAddr, node) - g.Add(node) } return nil diff --git a/terraform/transform_orphan_resource.go b/terraform/transform_orphan_resource.go index ce44bfac5..9872a704d 100644 --- a/terraform/transform_orphan_resource.go +++ b/terraform/transform_orphan_resource.go @@ -137,7 +137,7 @@ func (t *OrphanResourceTransformer) Transform(g *Graph) error { case GraphNodeResourceInstance: k := tv.ResourceInstanceAddr().ContainingResource().String() deps[k] = append(deps[k], v) - case GraphNodeResource: + case GraphNodeConfigResource: k := tv.ResourceAddr().String() deps[k] = append(deps[k], v) case GraphNodeDestroyer: diff --git a/terraform/transform_reference.go b/terraform/transform_reference.go index 5dff1a0ba..14353d43b 100644 --- a/terraform/transform_reference.go +++ b/terraform/transform_reference.go @@ -41,8 +41,8 @@ type GraphNodeReferencer interface { } type GraphNodeAttachDependencies interface { - GraphNodeResource - AttachDependencies([]addrs.AbsResource) + GraphNodeConfigResource + AttachDependencies([]addrs.ConfigResource) } // GraphNodeReferenceOutside is an interface that can optionally be implemented. @@ -116,6 +116,8 @@ type AttachDependenciesTransformer struct { } func (t AttachDependenciesTransformer) Transform(g *Graph) error { + // FIXME: this is only working with ResourceConfigAddr for now + for _, v := range g.Vertices() { attacher, ok := v.(GraphNodeAttachDependencies) if !ok { @@ -135,15 +137,15 @@ func (t AttachDependenciesTransformer) Transform(g *Graph) error { // dedupe addrs when there's multiple instances involved, or // multiple paths in the un-reduced graph - depMap := map[string]addrs.AbsResource{} + depMap := map[string]addrs.ConfigResource{} for _, d := range ans { - var addr addrs.AbsResource + var addr addrs.ConfigResource switch d := d.(type) { case GraphNodeResourceInstance: instAddr := d.ResourceInstanceAddr() - addr = instAddr.Resource.Resource.Absolute(instAddr.Module) - case GraphNodeResource: + addr = instAddr.ContainingResource().Config() + case GraphNodeConfigResource: addr = d.ResourceAddr() default: continue @@ -160,7 +162,7 @@ func (t AttachDependenciesTransformer) Transform(g *Graph) error { depMap[addr.String()] = addr } - deps := make([]addrs.AbsResource, 0, len(depMap)) + deps := make([]addrs.ConfigResource, 0, len(depMap)) for _, d := range depMap { deps = append(deps, d) } diff --git a/terraform/transform_resource_count.go b/terraform/transform_resource_count.go index 439cd9255..70a843ab5 100644 --- a/terraform/transform_resource_count.go +++ b/terraform/transform_resource_count.go @@ -16,7 +16,7 @@ type ResourceCountTransformer struct { Concrete ConcreteResourceInstanceNodeFunc Schema *configschema.Block - Addr addrs.AbsResource + Addr addrs.ConfigResource InstanceAddrs []addrs.AbsResourceInstance } diff --git a/terraform/transform_targets.go b/terraform/transform_targets.go index 80cff5364..a5d4ba934 100644 --- a/terraform/transform_targets.go +++ b/terraform/transform_targets.go @@ -58,7 +58,7 @@ func (t *TargetsTransformer) Transform(g *Graph) error { for _, v := range g.Vertices() { removable := false - if _, ok := v.(GraphNodeResource); ok { + if _, ok := v.(GraphNodeConfigResource); ok { removable = true } @@ -225,16 +225,12 @@ func (t *TargetsTransformer) nodeIsTarget(v dag.Vertex, targets []addrs.Targetab switch r := v.(type) { case GraphNodeResourceInstance: vertexAddr = r.ResourceInstanceAddr() - case GraphNodeResource: + case GraphNodeConfigResource: vertexAddr = r.ResourceAddr() default: // Only resource and resource instance nodes can be targeted. return false } - _, ok := v.(GraphNodeResource) - if !ok { - return false - } for _, targetAddr := range targets { if t.IgnoreIndices {