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 continue
} }
switch tv.Addr.Resource.Name { switch tv.Addr.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

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

View File

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

View File

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

View File

@ -37,6 +37,11 @@ type ConfigTransformer struct {
uniqueMap map[string]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 { func (t *ConfigTransformer) Transform(g *Graph) error {
// Lock since we use some internal state // Lock since we use some internal state
t.l.Lock() t.l.Lock()
@ -53,8 +58,8 @@ func (t *ConfigTransformer) Transform(g *Graph) error {
defer func() { t.uniqueMap = nil }() defer func() { t.uniqueMap = nil }()
if t.Unique { if t.Unique {
for _, v := range g.Vertices() { for _, v := range g.Vertices() {
if rn, ok := v.(GraphNodeResource); ok { if rn, ok := v.(configName); ok {
t.uniqueMap[rn.ResourceAddr().String()] = struct{}{} t.uniqueMap[rn.Name()] = struct{}{}
} }
} }
} }
@ -89,14 +94,6 @@ func (t *ConfigTransformer) transformSingle(g *Graph, config *configs.Config) er
module := config.Module module := config.Module
log.Printf("[TRACE] ConfigTransformer: Starting for path: %v", path) 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)) allResources := make([]*configs.Resource, 0, len(module.ManagedResources)+len(module.DataResources))
for _, r := range module.ManagedResources { for _, r := range module.ManagedResources {
allResources = append(allResources, r) allResources = append(allResources, r)
@ -113,14 +110,17 @@ func (t *ConfigTransformer) transformSingle(g *Graph, config *configs.Config) er
continue continue
} }
addr := relAddr.Absolute(instPath) abstract := &NodeAbstractResource{
if _, ok := t.uniqueMap[addr.String()]; ok { Addr: relAddr,
Module: path,
}
if _, ok := t.uniqueMap[abstract.Name()]; ok {
// We've already seen a resource with this address. This should // We've already seen a resource with this address. This should
// never happen, because we enforce uniqueness in the config loader. // never happen, because we enforce uniqueness in the config loader.
continue continue
} }
abstract := &NodeAbstractResource{Addr: addr}
var node dag.Vertex = abstract var node dag.Vertex = abstract
if f := t.Concrete; f != nil { if f := t.Concrete; f != nil {
node = f(abstract) node = f(abstract)