Add Path to more nodes that require it.

This commit is contained in:
James Bardin 2020-03-23 08:56:55 -04:00
parent 0b85eeab38
commit 74d85aa956
9 changed files with 47 additions and 43 deletions

View File

@ -118,11 +118,11 @@ func (ctx *BuiltinEvalContext) Input() UIInput {
} }
func (ctx *BuiltinEvalContext) InitProvider(addr addrs.AbsProviderConfig) (providers.Interface, error) { func (ctx *BuiltinEvalContext) InitProvider(addr addrs.AbsProviderConfig) (providers.Interface, error) {
if !addr.Module.Equal(ctx.Path().Module()) { //if !addr.Module.Equal(ctx.Path().Module()) {
// This indicates incorrect use of InitProvider: it should be used // // This indicates incorrect use of InitProvider: it should be used
// only from the module that the provider configuration belongs to. // // only from the module that the provider configuration belongs to.
panic(fmt.Sprintf("%s initialized by wrong module %s", addr, ctx.Path())) // panic(fmt.Sprintf("%s initialized by wrong module %s", addr, ctx.Path()))
} //}
// If we already initialized, it is an error // If we already initialized, it is an error
if p := ctx.Provider(addr); p != nil { if p := ctx.Provider(addr); p != nil {

View File

@ -209,7 +209,7 @@ func TestApplyGraphBuilder_doubleCBD(t *testing.T) {
continue continue
} }
switch tv.Addr.Resource.Name { switch tv.Addr.Resource.Resource.Name {
case "A": case "A":
destroyA = fmt.Sprintf("test_object.A (destroy deposed %s)", tv.DeposedKey) destroyA = fmt.Sprintf("test_object.A (destroy deposed %s)", tv.DeposedKey)
case "B": case "B":

View File

@ -78,10 +78,6 @@ var (
_ dag.GraphNodeDotter = (*NodeAbstractResource)(nil) _ dag.GraphNodeDotter = (*NodeAbstractResource)(nil)
) )
func (n *NodeAbstractResource) addr() addrs.AbsResource {
return n.Addr.Absolute(n.Addr.Module.UnkeyedInstanceShim())
}
// NewNodeAbstractResource creates an abstract resource graph node for // NewNodeAbstractResource creates an abstract resource graph node for
// the given absolute resource address. // the given absolute resource address.
func NewNodeAbstractResource(addr addrs.ConfigResource) *NodeAbstractResource { func NewNodeAbstractResource(addr addrs.ConfigResource) *NodeAbstractResource {
@ -97,8 +93,7 @@ func NewNodeAbstractResource(addr addrs.ConfigResource) *NodeAbstractResource {
// the "count" or "for_each" arguments. // the "count" or "for_each" arguments.
type NodeAbstractResourceInstance struct { type NodeAbstractResourceInstance struct {
NodeAbstractResource NodeAbstractResource
ModuleInstance addrs.ModuleInstance Addr addrs.AbsResourceInstance
InstanceKey addrs.InstanceKey
// The fields below will be automatically set using the Attach // The fields below will be automatically set using the Attach
// interfaces if you're running those transforms, but also be explicitly // interfaces if you're running those transforms, but also be explicitly
@ -132,15 +127,10 @@ func NewNodeAbstractResourceInstance(addr addrs.AbsResourceInstance) *NodeAbstra
// object and the InstanceKey field in our own struct. The // object and the InstanceKey field in our own struct. The
// ResourceInstanceAddr method will stick these back together again on // ResourceInstanceAddr method will stick these back together again on
// request. // request.
r := NewNodeAbstractResource(addr.ContainingResource().Config())
return &NodeAbstractResourceInstance{ return &NodeAbstractResourceInstance{
NodeAbstractResource: NodeAbstractResource{ NodeAbstractResource: *r,
Addr: addrs.ConfigResource{ Addr: addr,
Resource: addr.Resource.Resource,
Module: addr.Module.Module(),
},
},
ModuleInstance: addr.Module,
InstanceKey: addr.Resource.Key,
} }
} }
@ -152,13 +142,13 @@ func (n *NodeAbstractResourceInstance) Name() string {
return n.ResourceInstanceAddr().String() return n.ResourceInstanceAddr().String()
} }
// GraphNodeModuleInstance //// GraphNodeModuleInstance
func (n *NodeAbstractResource) Path() addrs.ModuleInstance { //func (n *NodeAbstractResource) Path() addrs.ModuleInstance {
return n.Addr.Module.UnkeyedInstanceShim() // return n.Addr.Module.UnkeyedInstanceShim()
} //}
func (n *NodeAbstractResourceInstance) Path() addrs.ModuleInstance { func (n *NodeAbstractResourceInstance) Path() addrs.ModuleInstance {
return n.ModuleInstance return n.Addr.Module
} }
// GraphNodeModulePath // GraphNodeModulePath
@ -283,7 +273,7 @@ func dottedInstanceAddr(tr addrs.ResourceInstance) string {
// StateDependencies returns the dependencies saved in the state. // StateDependencies returns the dependencies saved in the state.
func (n *NodeAbstractResourceInstance) StateDependencies() []addrs.AbsResource { func (n *NodeAbstractResourceInstance) StateDependencies() []addrs.AbsResource {
if rs := n.ResourceState; rs != nil { if rs := n.ResourceState; rs != nil {
if s := rs.Instance(n.InstanceKey); s != nil { if s := rs.Instance(n.Addr.Resource.Key); s != nil {
if s.Current != nil { if s.Current != nil {
return s.Current.Dependencies return s.Current.Dependencies
} }
@ -356,7 +346,7 @@ func (n *NodeAbstractResourceInstance) Provider() addrs.Provider {
return n.Config.Provider return n.Config.Provider
} }
// FIXME: this will be a default provider // FIXME: this will be a default provider
return addrs.NewLegacyProvider(n.Addr.Resource.ImpliedProvider()) return addrs.NewLegacyProvider(n.Addr.Resource.ContainingResource().ImpliedProvider())
} }
// GraphNodeProvisionerConsumer // GraphNodeProvisionerConsumer
@ -391,7 +381,7 @@ func (n *NodeAbstractResource) ResourceAddr() addrs.ConfigResource {
// GraphNodeResourceInstance // GraphNodeResourceInstance
func (n *NodeAbstractResourceInstance) ResourceInstanceAddr() addrs.AbsResourceInstance { func (n *NodeAbstractResourceInstance) ResourceInstanceAddr() addrs.AbsResourceInstance {
return n.NodeAbstractResource.addr().Instance(n.InstanceKey) return n.Addr
} }
// GraphNodeTargetable // GraphNodeTargetable

View File

@ -64,6 +64,7 @@ type NodeApplyableResource struct {
} }
var ( var (
_ GraphNodeModuleInstance = (*NodeApplyableResource)(nil)
_ GraphNodeConfigResource = (*NodeApplyableResource)(nil) _ GraphNodeConfigResource = (*NodeApplyableResource)(nil)
_ GraphNodeEvalable = (*NodeApplyableResource)(nil) _ GraphNodeEvalable = (*NodeApplyableResource)(nil)
_ GraphNodeProviderConsumer = (*NodeApplyableResource)(nil) _ GraphNodeProviderConsumer = (*NodeApplyableResource)(nil)
@ -71,6 +72,10 @@ var (
_ GraphNodeReferencer = (*NodeApplyableResource)(nil) _ GraphNodeReferencer = (*NodeApplyableResource)(nil)
) )
func (n *NodeApplyableResource) Path() addrs.ModuleInstance {
return n.Addr.Module
}
func (n *NodeApplyableResource) Name() string { func (n *NodeApplyableResource) Name() string {
return n.NodeAbstractResource.Name() + " (prepare state)" return n.NodeAbstractResource.Name() + " (prepare state)"
} }

View File

@ -26,6 +26,7 @@ type NodeDestroyResourceInstance struct {
} }
var ( var (
_ GraphNodeModuleInstance = (*NodeDestroyResourceInstance)(nil)
_ GraphNodeConfigResource = (*NodeDestroyResourceInstance)(nil) _ GraphNodeConfigResource = (*NodeDestroyResourceInstance)(nil)
_ GraphNodeResourceInstance = (*NodeDestroyResourceInstance)(nil) _ GraphNodeResourceInstance = (*NodeDestroyResourceInstance)(nil)
_ GraphNodeDestroyer = (*NodeDestroyResourceInstance)(nil) _ GraphNodeDestroyer = (*NodeDestroyResourceInstance)(nil)
@ -63,7 +64,7 @@ func (n *NodeDestroyResourceInstance) CreateBeforeDestroy() bool {
// Otherwise check the state for a stored destroy order // Otherwise check the state for a stored destroy order
if rs := n.ResourceState; rs != nil { if rs := n.ResourceState; rs != nil {
if s := rs.Instance(n.InstanceKey); s != nil { if s := rs.Instance(n.Addr.Resource.Key); s != nil {
if s.Current != nil { if s.Current != nil {
return s.Current.CreateBeforeDestroy return s.Current.CreateBeforeDestroy
} }
@ -136,7 +137,7 @@ func (n *NodeDestroyResourceInstance) EvalTree() EvalNode {
rs := n.ResourceState rs := n.ResourceState
var is *states.ResourceInstance var is *states.ResourceInstance
if rs != nil { if rs != nil {
is = rs.Instance(n.InstanceKey) is = rs.Instance(n.Addr.Resource.Key)
} }
if is == nil { if is == nil {
log.Printf("[WARN] NodeDestroyResourceInstance for %s with no state", addr) log.Printf("[WARN] NodeDestroyResourceInstance for %s with no state", addr)
@ -292,6 +293,7 @@ type NodeDestroyResource struct {
} }
var ( var (
_ GraphNodeModuleInstance = (*NodeDestroyResource)(nil)
_ GraphNodeConfigResource = (*NodeDestroyResource)(nil) _ GraphNodeConfigResource = (*NodeDestroyResource)(nil)
_ GraphNodeReferenceable = (*NodeDestroyResource)(nil) _ GraphNodeReferenceable = (*NodeDestroyResource)(nil)
_ GraphNodeReferencer = (*NodeDestroyResource)(nil) _ GraphNodeReferencer = (*NodeDestroyResource)(nil)
@ -305,6 +307,10 @@ var (
_ GraphNodeNoProvider = (*NodeDestroyResource)(nil) _ GraphNodeNoProvider = (*NodeDestroyResource)(nil)
) )
func (n *NodeDestroyResource) Path() addrs.ModuleInstance {
return n.Addr.Module
}
func (n *NodeDestroyResource) Name() string { func (n *NodeDestroyResource) Name() string {
return n.ResourceAddr().String() + " (clean up state)" return n.ResourceAddr().String() + " (clean up state)"
} }

View File

@ -95,6 +95,10 @@ var (
_ GraphNodeAttachResourceConfig = (*NodePlannableResource)(nil) _ GraphNodeAttachResourceConfig = (*NodePlannableResource)(nil)
) )
func (n *NodePlannableResource) Path() addrs.ModuleInstance {
return n.Addr.Module
}
// GraphNodeModuleInstance // GraphNodeModuleInstance
func (n *NodePlannableResource) ModuleInstance() addrs.ModuleInstance { func (n *NodePlannableResource) ModuleInstance() addrs.ModuleInstance {
return n.Addr.Module return n.Addr.Module

View File

@ -160,15 +160,11 @@ func TestNodeRefreshableManagedResourceEvalTree_scaleOut(t *testing.T) {
m := testModule(t, "refresh-resource-scale-inout") m := testModule(t, "refresh-resource-scale-inout")
n := &NodeRefreshableManagedResourceInstance{ n := &NodeRefreshableManagedResourceInstance{
NodeAbstractResourceInstance: &NodeAbstractResourceInstance{ NodeAbstractResourceInstance: NewNodeAbstractResourceInstance(addrs.RootModuleInstance.Resource(addrs.ManagedResourceMode, "aws_instance", "foo").Instance(addrs.IntKey(2))),
NodeAbstractResource: NodeAbstractResource{
Addr: addrs.RootModule.Resource(addrs.ManagedResourceMode, "aws_instance", "foo"),
Config: m.Module.ManagedResources["aws_instance.foo"],
},
InstanceKey: addrs.IntKey(2),
},
} }
n.AttachResourceConfig(m.Module.ManagedResources["aws_instance.foo"])
actual := n.EvalTree() actual := n.EvalTree()
expected := n.evalTreeManagedResourceNoState() expected := n.evalTreeManagedResourceNoState()

View File

@ -1,6 +1,7 @@
package terraform package terraform
import ( import (
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/configs" "github.com/hashicorp/terraform/configs"
"github.com/hashicorp/terraform/configs/configschema" "github.com/hashicorp/terraform/configs/configschema"
"github.com/hashicorp/terraform/providers" "github.com/hashicorp/terraform/providers"
@ -15,6 +16,7 @@ type NodeValidatableResource struct {
} }
var ( var (
_ GraphNodeModuleInstance = (*NodeValidatableResource)(nil)
_ GraphNodeEvalable = (*NodeValidatableResource)(nil) _ GraphNodeEvalable = (*NodeValidatableResource)(nil)
_ GraphNodeReferenceable = (*NodeValidatableResource)(nil) _ GraphNodeReferenceable = (*NodeValidatableResource)(nil)
_ GraphNodeReferencer = (*NodeValidatableResource)(nil) _ GraphNodeReferencer = (*NodeValidatableResource)(nil)
@ -23,6 +25,12 @@ var (
_ GraphNodeAttachProviderMetaConfigs = (*NodeValidatableResource)(nil) _ GraphNodeAttachProviderMetaConfigs = (*NodeValidatableResource)(nil)
) )
func (n *NodeValidatableResource) Path() addrs.ModuleInstance {
// There is no expansion during validation, so we evaluate everything as
// single module instances.
return n.Addr.Module.UnkeyedInstanceShim()
}
// GraphNodeEvalable // GraphNodeEvalable
func (n *NodeValidatableResource) EvalTree() EvalNode { func (n *NodeValidatableResource) EvalTree() EvalNode {
addr := n.ResourceAddr() addr := n.ResourceAddr()

View File

@ -237,12 +237,7 @@ module.child.test_object.c (destroy)
func testDestroyNode(addrString string) GraphNodeDestroyer { func testDestroyNode(addrString string) GraphNodeDestroyer {
instAddr := mustResourceInstanceAddr(addrString) instAddr := mustResourceInstanceAddr(addrString)
abs := NewNodeAbstractResource(instAddr.ContainingResource().Config()) inst := NewNodeAbstractResourceInstance(instAddr)
inst := &NodeAbstractResourceInstance{
NodeAbstractResource: *abs,
InstanceKey: instAddr.Resource.Key,
}
return &NodeDestroyResourceInstance{NodeAbstractResourceInstance: inst} return &NodeDestroyResourceInstance{NodeAbstractResourceInstance: inst}
} }