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) {
if !addr.Module.Equal(ctx.Path().Module()) {
// This indicates incorrect use of InitProvider: it should be used
// only from the module that the provider configuration belongs to.
panic(fmt.Sprintf("%s initialized by wrong module %s", addr, ctx.Path()))
}
//if !addr.Module.Equal(ctx.Path().Module()) {
// // This indicates incorrect use of InitProvider: it should be used
// // only from the module that the provider configuration belongs to.
// panic(fmt.Sprintf("%s initialized by wrong module %s", addr, ctx.Path()))
//}
// If we already initialized, it is an error
if p := ctx.Provider(addr); p != nil {

View File

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

View File

@ -78,10 +78,6 @@ var (
_ 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
// the given absolute resource address.
func NewNodeAbstractResource(addr addrs.ConfigResource) *NodeAbstractResource {
@ -97,8 +93,7 @@ func NewNodeAbstractResource(addr addrs.ConfigResource) *NodeAbstractResource {
// the "count" or "for_each" arguments.
type NodeAbstractResourceInstance struct {
NodeAbstractResource
ModuleInstance addrs.ModuleInstance
InstanceKey addrs.InstanceKey
Addr addrs.AbsResourceInstance
// The fields below will be automatically set using the Attach
// 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
// ResourceInstanceAddr method will stick these back together again on
// request.
r := NewNodeAbstractResource(addr.ContainingResource().Config())
return &NodeAbstractResourceInstance{
NodeAbstractResource: NodeAbstractResource{
Addr: addrs.ConfigResource{
Resource: addr.Resource.Resource,
Module: addr.Module.Module(),
},
},
ModuleInstance: addr.Module,
InstanceKey: addr.Resource.Key,
NodeAbstractResource: *r,
Addr: addr,
}
}
@ -152,13 +142,13 @@ func (n *NodeAbstractResourceInstance) Name() string {
return n.ResourceInstanceAddr().String()
}
// GraphNodeModuleInstance
func (n *NodeAbstractResource) Path() addrs.ModuleInstance {
return n.Addr.Module.UnkeyedInstanceShim()
}
//// GraphNodeModuleInstance
//func (n *NodeAbstractResource) Path() addrs.ModuleInstance {
// return n.Addr.Module.UnkeyedInstanceShim()
//}
func (n *NodeAbstractResourceInstance) Path() addrs.ModuleInstance {
return n.ModuleInstance
return n.Addr.Module
}
// GraphNodeModulePath
@ -283,7 +273,7 @@ func dottedInstanceAddr(tr addrs.ResourceInstance) string {
// StateDependencies returns the dependencies saved in the state.
func (n *NodeAbstractResourceInstance) StateDependencies() []addrs.AbsResource {
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 {
return s.Current.Dependencies
}
@ -356,7 +346,7 @@ func (n *NodeAbstractResourceInstance) Provider() addrs.Provider {
return n.Config.Provider
}
// FIXME: this will be a default provider
return addrs.NewLegacyProvider(n.Addr.Resource.ImpliedProvider())
return addrs.NewLegacyProvider(n.Addr.Resource.ContainingResource().ImpliedProvider())
}
// GraphNodeProvisionerConsumer
@ -391,7 +381,7 @@ func (n *NodeAbstractResource) ResourceAddr() addrs.ConfigResource {
// GraphNodeResourceInstance
func (n *NodeAbstractResourceInstance) ResourceInstanceAddr() addrs.AbsResourceInstance {
return n.NodeAbstractResource.addr().Instance(n.InstanceKey)
return n.Addr
}
// GraphNodeTargetable

View File

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

View File

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

View File

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

View File

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

View File

@ -1,6 +1,7 @@
package terraform
import (
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/configs"
"github.com/hashicorp/terraform/configs/configschema"
"github.com/hashicorp/terraform/providers"
@ -15,6 +16,7 @@ type NodeValidatableResource struct {
}
var (
_ GraphNodeModuleInstance = (*NodeValidatableResource)(nil)
_ GraphNodeEvalable = (*NodeValidatableResource)(nil)
_ GraphNodeReferenceable = (*NodeValidatableResource)(nil)
_ GraphNodeReferencer = (*NodeValidatableResource)(nil)
@ -23,6 +25,12 @@ var (
_ 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
func (n *NodeValidatableResource) EvalTree() EvalNode {
addr := n.ResourceAddr()

View File

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