remove abs addrs from NodeAbstractResource

This adds more shimming into that node itself, but allows us to pull it
out of the config transformer, and ensure we can create the resources
correctly from the config. The shimmed address usage can then be raised
out of the abstract resource, into the expanded node types.
This commit is contained in:
James Bardin 2020-03-07 12:48:39 -05:00
parent 245296850b
commit 68b500c5c7
5 changed files with 62 additions and 49 deletions

View File

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

View File

@ -40,11 +40,12 @@ func TestNodeRefreshableDataResourceDynamicExpand_scaleOut(t *testing.T) {
n := &NodeRefreshableDataResource{
NodeAbstractResource: &NodeAbstractResource{
Addr: addrs.RootModuleInstance.Resource(
addrs.DataResourceMode,
"aws_instance",
"foo",
),
Addr: addrs.Resource{
Mode: addrs.DataResourceMode,
Type: "aws_instance",
Name: "foo",
},
Module: addrs.RootModule,
Config: m.Module.DataResources["data.aws_instance.foo"],
},
}
@ -124,11 +125,12 @@ func TestNodeRefreshableDataResourceDynamicExpand_scaleIn(t *testing.T) {
n := &NodeRefreshableDataResource{
NodeAbstractResource: &NodeAbstractResource{
Addr: addrs.RootModuleInstance.Resource(
addrs.DataResourceMode,
"aws_instance",
"foo",
),
Addr: addrs.Resource{
Mode: addrs.DataResourceMode,
Type: "aws_instance",
Name: "foo",
},
Module: addrs.RootModule,
Config: m.Module.DataResources["data.aws_instance.foo"],
ResolvedProvider: addrs.AbsProviderConfig{
Provider: addrs.NewLegacyProvider("aws"),

View File

@ -43,11 +43,8 @@ type GraphNodeResourceInstance interface {
// operations. It registers all the interfaces for a resource that common
// across multiple operation types.
type NodeAbstractResource struct {
//FIXME: AbstractResources are no longer absolute, because modules are not expanded.
// Addr addrs.Resource
// Module addrs.Module
Addr addrs.AbsResource // Addr is the address for this resource
Addr addrs.Resource
Module addrs.Module
// The fields below will be automatically set using the Attach
// interfaces if you're running those transforms, but also be explicitly
@ -69,7 +66,6 @@ type NodeAbstractResource struct {
}
var (
_ GraphNodeModuleInstance = (*NodeAbstractResource)(nil)
_ GraphNodeReferenceable = (*NodeAbstractResource)(nil)
_ GraphNodeReferencer = (*NodeAbstractResource)(nil)
_ GraphNodeProviderConsumer = (*NodeAbstractResource)(nil)
@ -83,11 +79,16 @@ var (
_ dag.GraphNodeDotter = (*NodeAbstractResource)(nil)
)
func (n *NodeAbstractResource) addr() addrs.AbsResource {
return n.Addr.Absolute(n.Module.UnkeyedInstanceShim())
}
// NewNodeAbstractResource creates an abstract resource graph node for
// the given absolute resource address.
func NewNodeAbstractResource(addr addrs.AbsResource) *NodeAbstractResource {
return &NodeAbstractResource{
Addr: addr,
Addr: addr.Resource,
Module: addr.Module.Module(),
}
}
@ -134,7 +135,8 @@ func NewNodeAbstractResourceInstance(addr addrs.AbsResourceInstance) *NodeAbstra
// request.
return &NodeAbstractResourceInstance{
NodeAbstractResource: NodeAbstractResource{
Addr: addr.ContainingResource(),
Addr: addr.Resource.Resource,
Module: addr.Module.Module(),
},
InstanceKey: addr.Resource.Key,
}
@ -150,17 +152,17 @@ func (n *NodeAbstractResourceInstance) Name() string {
// GraphNodeModuleInstance
func (n *NodeAbstractResource) Path() addrs.ModuleInstance {
return n.Addr.Module
return n.Module.UnkeyedInstanceShim()
}
// GraphNodeModulePath
func (n *NodeAbstractResource) ModulePath() addrs.Module {
return n.Addr.Module.Module()
return n.Module
}
// GraphNodeReferenceable
func (n *NodeAbstractResource) ReferenceableAddrs() []addrs.Referenceable {
return []addrs.Referenceable{n.Addr.Resource}
return []addrs.Referenceable{n.Addr}
}
// GraphNodeReferenceable
@ -306,7 +308,7 @@ func (n *NodeAbstractResource) ProvidedBy() (addrs.ProviderConfig, bool) {
// GraphNodeProviderConsumer
func (n *NodeAbstractResource) ImpliedProvider() addrs.Provider {
return n.Addr.Resource.DefaultProvider()
return n.Addr.DefaultProvider()
}
// GraphNodeProviderConsumer
@ -334,7 +336,7 @@ func (n *NodeAbstractResourceInstance) ProvidedBy() (addrs.ProviderConfig, bool)
// GraphNodeProviderConsumer
func (n *NodeAbstractResourceInstance) ImpliedProvider() addrs.Provider {
return n.Addr.Resource.DefaultProvider()
return n.Addr.DefaultProvider()
}
// GraphNodeProvisionerConsumer
@ -364,17 +366,17 @@ func (n *NodeAbstractResource) AttachProvisionerSchema(name string, schema *conf
// GraphNodeResource
func (n *NodeAbstractResource) ResourceAddr() addrs.AbsResource {
return n.Addr
return n.addr()
}
// GraphNodeResourceInstance
func (n *NodeAbstractResourceInstance) ResourceInstanceAddr() addrs.AbsResourceInstance {
return n.NodeAbstractResource.Addr.Instance(n.InstanceKey)
return n.NodeAbstractResource.addr().Instance(n.InstanceKey)
}
// GraphNodeAddressable, TODO: remove, used by target, should unify
func (n *NodeAbstractResource) ResourceAddress() *ResourceAddress {
return NewLegacyResourceAddress(n.Addr)
return NewLegacyResourceAddress(n.addr())
}
// GraphNodeTargetable

View File

@ -42,9 +42,12 @@ func TestNodeRefreshableManagedResourceDynamicExpand_scaleOut(t *testing.T) {
n := &NodeRefreshableManagedResource{
NodeAbstractResource: &NodeAbstractResource{
Addr: addrs.RootModuleInstance.Resource(
addrs.ManagedResourceMode, "aws_instance", "foo",
),
Addr: addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "aws_instance",
Name: "foo",
},
Module: addrs.RootModule,
Config: m.Module.ManagedResources["aws_instance.foo"],
},
}
@ -124,9 +127,12 @@ func TestNodeRefreshableManagedResourceDynamicExpand_scaleIn(t *testing.T) {
n := &NodeRefreshableManagedResource{
NodeAbstractResource: &NodeAbstractResource{
Addr: addrs.RootModuleInstance.Resource(
addrs.ManagedResourceMode, "aws_instance", "foo",
),
Addr: addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "aws_instance",
Name: "foo",
},
Module: addrs.RootModule,
Config: m.Module.ManagedResources["aws_instance.foo"],
},
}
@ -166,9 +172,12 @@ func TestNodeRefreshableManagedResourceEvalTree_scaleOut(t *testing.T) {
n := &NodeRefreshableManagedResourceInstance{
NodeAbstractResourceInstance: &NodeAbstractResourceInstance{
NodeAbstractResource: NodeAbstractResource{
Addr: addrs.RootModuleInstance.Resource(
addrs.ManagedResourceMode, "aws_instance", "foo",
),
Addr: addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "aws_instance",
Name: "foo",
},
Module: addrs.RootModule,
Config: m.Module.ManagedResources["aws_instance.foo"],
},
InstanceKey: addrs.IntKey(2),

View File

@ -37,6 +37,11 @@ type ConfigTransformer struct {
uniqueMap map[string]struct{}
}
// FIXME: should we have an addr.Module + addr.Resource type?
type configName interface {
Name() string
}
func (t *ConfigTransformer) Transform(g *Graph) error {
// Lock since we use some internal state
t.l.Lock()
@ -53,8 +58,8 @@ func (t *ConfigTransformer) Transform(g *Graph) error {
defer func() { t.uniqueMap = nil }()
if t.Unique {
for _, v := range g.Vertices() {
if rn, ok := v.(GraphNodeResource); ok {
t.uniqueMap[rn.ResourceAddr().String()] = struct{}{}
if rn, ok := v.(configName); ok {
t.uniqueMap[rn.Name()] = struct{}{}
}
}
}
@ -89,14 +94,6 @@ func (t *ConfigTransformer) transformSingle(g *Graph, config *configs.Config) er
module := config.Module
log.Printf("[TRACE] ConfigTransformer: Starting for path: %v", path)
// For now we assume that each module call produces only one module
// instance with no key, since we don't yet support "count" and "for_each"
// on modules.
// FIXME: As part of supporting "count" and "for_each" on modules, rework
// this so that we'll "expand" the module call first and then create graph
// nodes for each module instance separately.
instPath := path.UnkeyedInstanceShim()
allResources := make([]*configs.Resource, 0, len(module.ManagedResources)+len(module.DataResources))
for _, r := range module.ManagedResources {
allResources = append(allResources, r)
@ -113,14 +110,17 @@ func (t *ConfigTransformer) transformSingle(g *Graph, config *configs.Config) er
continue
}
addr := relAddr.Absolute(instPath)
if _, ok := t.uniqueMap[addr.String()]; ok {
abstract := &NodeAbstractResource{
Addr: relAddr,
Module: path,
}
if _, ok := t.uniqueMap[abstract.Name()]; ok {
// We've already seen a resource with this address. This should
// never happen, because we enforce uniqueness in the config loader.
continue
}
abstract := &NodeAbstractResource{Addr: addr}
var node dag.Vertex = abstract
if f := t.Concrete; f != nil {
node = f(abstract)