Skip to content

Commit

Permalink
incorporate addrs.ConfigResource
Browse files Browse the repository at this point in the history
  • Loading branch information
jbardin committed Mar 12, 2020
1 parent 5aad97b commit 6da0b1c
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 56 deletions.
11 changes: 5 additions & 6 deletions terraform/eval_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,7 @@ func (n *EvalMaybeRestoreDeposedObject) Eval(ctx EvalContext) (interface{}, erro
// in that case, allowing expression evaluation to see it as a zero-element
// list rather than as not set at all.
type EvalWriteResourceState struct {
Addr addrs.Resource
Module addrs.Module
Addr addrs.ConfigResource
Config *configs.Resource
ProviderAddr addrs.AbsProviderConfig
}
Expand Down Expand Up @@ -489,18 +488,18 @@ func (n *EvalWriteResourceState) Eval(ctx EvalContext) (interface{}, error) {
// can refer to it. Since this node represents the abstract module, we need
// to expand the module here to create all resources.
expander := ctx.InstanceExpander()
for _, module := range expander.ExpandModule(n.Module) {
for _, module := range expander.ExpandModule(n.Addr.Module) {
// This method takes care of all of the business logic of updating this
// while ensuring that any existing instances are preserved, etc.
state.SetResourceMeta(n.Addr.Absolute(module), eachMode, n.ProviderAddr)

switch eachMode {
case states.EachList:
expander.SetResourceCount(module, n.Addr, count)
expander.SetResourceCount(module, n.Addr.Resource, count)
case states.EachMap:
expander.SetResourceForEach(module, n.Addr, forEach)
expander.SetResourceForEach(module, n.Addr.Resource, forEach)
default:
expander.SetResourceSingle(module, n.Addr)
expander.SetResourceSingle(module, n.Addr.Resource)
}
}

Expand Down
2 changes: 1 addition & 1 deletion terraform/graph_builder_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func TestApplyGraphBuilder_doubleCBD(t *testing.T) {
continue
}

switch tv.Addr.Name {
switch tv.Addr.Resource.Name {
case "A":
destroyA = fmt.Sprintf("test_object.A (destroy deposed %s)", tv.DeposedKey)
case "B":
Expand Down
2 changes: 1 addition & 1 deletion terraform/node_data_refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (n *NodeRefreshableDataResource) DynamicExpand(ctx EvalContext) (*Graph, er
// Inform our instance expander about our expansion results above,
// and then use it to calculate the instance addresses we'll expand for.
expander := ctx.InstanceExpander()
for _, path := range expander.ExpandModule(n.Module) {
for _, path := range expander.ExpandModule(n.Addr.Module) {
switch {
case count >= 0:
expander.SetResourceCount(path, n.ResourceAddr().Resource, count)
Expand Down
14 changes: 2 additions & 12 deletions terraform/node_data_refresh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,7 @@ func TestNodeRefreshableDataResourceDynamicExpand_scaleOut(t *testing.T) {

n := &NodeRefreshableDataResource{
NodeAbstractResource: &NodeAbstractResource{
Addr: addrs.Resource{
Mode: addrs.DataResourceMode,
Type: "aws_instance",
Name: "foo",
},
Module: addrs.RootModule,
Addr: addrs.RootModule.Resource(addrs.DataResourceMode, "aws_instance", "foo"),
Config: m.Module.DataResources["data.aws_instance.foo"],
},
}
Expand Down Expand Up @@ -125,12 +120,7 @@ func TestNodeRefreshableDataResourceDynamicExpand_scaleIn(t *testing.T) {

n := &NodeRefreshableDataResource{
NodeAbstractResource: &NodeAbstractResource{
Addr: addrs.Resource{
Mode: addrs.DataResourceMode,
Type: "aws_instance",
Name: "foo",
},
Module: addrs.RootModule,
Addr: addrs.RootModule.Resource(addrs.DataResourceMode, "aws_instance", "foo"),
Config: m.Module.DataResources["data.aws_instance.foo"],
ResolvedProvider: addrs.AbsProviderConfig{
Provider: addrs.NewLegacyProvider("aws"),
Expand Down
28 changes: 16 additions & 12 deletions terraform/node_resource_abstract.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ type GraphNodeResourceInstance interface {
// operations. It registers all the interfaces for a resource that common
// across multiple operation types.
type NodeAbstractResource struct {
Addr addrs.Resource
Module addrs.Module
Addr addrs.ConfigResource

// The fields below will be automatically set using the Attach
// interfaces if you're running those transforms, but also be explicitly
Expand Down Expand Up @@ -80,15 +79,18 @@ var (
)

func (n *NodeAbstractResource) addr() addrs.AbsResource {
return n.Addr.Absolute(n.Module.UnkeyedInstanceShim())
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.AbsResource) *NodeAbstractResource {
// FIXME: this should probably accept a ConfigResource
return &NodeAbstractResource{
Addr: addr.Resource,
Module: addr.Module.Module(),
Addr: addrs.ConfigResource{
Resource: addr.Resource,
Module: addr.Module.Module(),
},
}
}

Expand Down Expand Up @@ -136,8 +138,10 @@ func NewNodeAbstractResourceInstance(addr addrs.AbsResourceInstance) *NodeAbstra
// request.
return &NodeAbstractResourceInstance{
NodeAbstractResource: NodeAbstractResource{
Addr: addr.Resource.Resource,
Module: addr.Module.Module(),
Addr: addrs.ConfigResource{
Resource: addr.Resource.Resource,
Module: addr.Module.Module(),
},
},
ModuleInstance: addr.Module,
InstanceKey: addr.Resource.Key,
Expand All @@ -154,7 +158,7 @@ func (n *NodeAbstractResourceInstance) Name() string {

// GraphNodeModuleInstance
func (n *NodeAbstractResource) Path() addrs.ModuleInstance {
return n.Module.UnkeyedInstanceShim()
return n.Addr.Module.UnkeyedInstanceShim()
}

func (n *NodeAbstractResourceInstance) Path() addrs.ModuleInstance {
Expand All @@ -163,12 +167,12 @@ func (n *NodeAbstractResourceInstance) Path() addrs.ModuleInstance {

// GraphNodeModulePath
func (n *NodeAbstractResource) ModulePath() addrs.Module {
return n.Module
return n.Addr.Module
}

// GraphNodeReferenceable
func (n *NodeAbstractResource) ReferenceableAddrs() []addrs.Referenceable {
return []addrs.Referenceable{n.Addr}
return []addrs.Referenceable{n.Addr.Resource}
}

// GraphNodeReferenceable
Expand Down Expand Up @@ -314,7 +318,7 @@ func (n *NodeAbstractResource) ProvidedBy() (addrs.ProviderConfig, bool) {

// GraphNodeProviderConsumer
func (n *NodeAbstractResource) ImpliedProvider() addrs.Provider {
return n.Addr.DefaultProvider()
return n.Addr.Resource.DefaultProvider()
}

// GraphNodeProviderConsumer
Expand Down Expand Up @@ -342,7 +346,7 @@ func (n *NodeAbstractResourceInstance) ProvidedBy() (addrs.ProviderConfig, bool)

// GraphNodeProviderConsumer
func (n *NodeAbstractResourceInstance) ImpliedProvider() addrs.Provider {
return n.Addr.DefaultProvider()
return n.Addr.Resource.DefaultProvider()
}

// GraphNodeProvisionerConsumer
Expand Down
1 change: 0 additions & 1 deletion terraform/node_resource_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func (n *NodeApplyableResource) EvalTree() EvalNode {

return &EvalWriteResourceState{
Addr: n.Addr,
Module: n.Module,
Config: n.Config,
ProviderAddr: n.ResolvedProvider,
}
Expand Down
1 change: 0 additions & 1 deletion terraform/node_resource_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func (n *NodePlannableResource) EvalTree() EvalNode {
// this ensures we can reference the resource even if the count is 0
return &EvalWriteResourceState{
Addr: n.Addr,
Module: n.Module,
Config: n.Config,
ProviderAddr: n.ResolvedProvider,
}
Expand Down
4 changes: 2 additions & 2 deletions terraform/node_resource_refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (n *NodeRefreshableManagedResource) DynamicExpand(ctx EvalContext) (*Graph,
// Inform our instance expander about our expansion results above,
// and then use it to calculate the instance addresses we'll expand for.
expander := ctx.InstanceExpander()
for _, module := range expander.ExpandModule(n.Module) {
for _, module := range expander.ExpandModule(n.Addr.Module) {
switch {
case count >= 0:
expander.SetResourceCount(module, n.ResourceAddr().Resource, count)
Expand All @@ -70,7 +70,7 @@ func (n *NodeRefreshableManagedResource) DynamicExpand(ctx EvalContext) (*Graph,
expander.SetResourceSingle(module, n.ResourceAddr().Resource)
}
}
instanceAddrs := expander.ExpandResource(n.Module, n.ResourceAddr().Resource)
instanceAddrs := expander.ExpandResource(n.Addr.Module, n.ResourceAddr().Resource)

// Our graph transformers require access to the full state, so we'll
// temporarily lock it while we work on this.
Expand Down
21 changes: 3 additions & 18 deletions terraform/node_resource_refresh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,7 @@ func TestNodeRefreshableManagedResourceDynamicExpand_scaleOut(t *testing.T) {

n := &NodeRefreshableManagedResource{
NodeAbstractResource: &NodeAbstractResource{
Addr: addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "aws_instance",
Name: "foo",
},
Module: addrs.RootModule,
Addr: addrs.RootModule.Resource(addrs.ManagedResourceMode, "aws_instance", "foo"),
Config: m.Module.ManagedResources["aws_instance.foo"],
},
}
Expand Down Expand Up @@ -127,12 +122,7 @@ func TestNodeRefreshableManagedResourceDynamicExpand_scaleIn(t *testing.T) {

n := &NodeRefreshableManagedResource{
NodeAbstractResource: &NodeAbstractResource{
Addr: addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "aws_instance",
Name: "foo",
},
Module: addrs.RootModule,
Addr: addrs.RootModule.Resource(addrs.ManagedResourceMode, "aws_instance", "foo"),
Config: m.Module.ManagedResources["aws_instance.foo"],
},
}
Expand Down Expand Up @@ -172,12 +162,7 @@ func TestNodeRefreshableManagedResourceEvalTree_scaleOut(t *testing.T) {
n := &NodeRefreshableManagedResourceInstance{
NodeAbstractResourceInstance: &NodeAbstractResourceInstance{
NodeAbstractResource: NodeAbstractResource{
Addr: addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "aws_instance",
Name: "foo",
},
Module: addrs.RootModule,
Addr: addrs.RootModule.Resource(addrs.ManagedResourceMode, "aws_instance", "foo"),
Config: m.Module.ManagedResources["aws_instance.foo"],
},
InstanceKey: addrs.IntKey(2),
Expand Down
6 changes: 4 additions & 2 deletions terraform/transform_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,10 @@ func (t *ConfigTransformer) transformSingle(g *Graph, config *configs.Config) er
}

abstract := &NodeAbstractResource{
Addr: relAddr,
Module: path,
Addr: addrs.ConfigResource{
Resource: relAddr,
Module: path,
},
}

if _, ok := t.uniqueMap[abstract.Name()]; ok {
Expand Down

0 comments on commit 6da0b1c

Please sign in to comment.