Skip to content

Commit e320507

Browse files
committed
remove NodeApplyableResource
Move the logic for NodeApplyableResource into nodeExpandApplyableResource to remove an unnecessary level of expansion.
1 parent 64b6695 commit e320507

File tree

2 files changed

+21
-60
lines changed

2 files changed

+21
-60
lines changed

internal/terraform/node_resource_apply.go

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
package terraform
55

66
import (
7-
"log"
8-
97
"github.com/hashicorp/terraform/internal/addrs"
108
"github.com/hashicorp/terraform/internal/tfdiags"
119
)
@@ -19,7 +17,6 @@ type nodeExpandApplyableResource struct {
1917
}
2018

2119
var (
22-
_ GraphNodeDynamicExpandable = (*nodeExpandApplyableResource)(nil)
2320
_ GraphNodeReferenceable = (*nodeExpandApplyableResource)(nil)
2421
_ GraphNodeReferencer = (*nodeExpandApplyableResource)(nil)
2522
_ GraphNodeConfigResource = (*nodeExpandApplyableResource)(nil)
@@ -49,56 +46,14 @@ func (n *nodeExpandApplyableResource) Name() string {
4946
return n.NodeAbstractResource.Name() + " (expand)"
5047
}
5148

52-
func (n *nodeExpandApplyableResource) DynamicExpand(ctx EvalContext) (*Graph, error) {
53-
var g Graph
54-
49+
func (n *nodeExpandApplyableResource) Execute(ctx EvalContext, op walkOperation) tfdiags.Diagnostics {
50+
var diags tfdiags.Diagnostics
5551
expander := ctx.InstanceExpander()
5652
moduleInstances := expander.ExpandModule(n.Addr.Module)
5753
for _, module := range moduleInstances {
58-
g.Add(&NodeApplyableResource{
59-
NodeAbstractResource: n.NodeAbstractResource,
60-
Addr: n.Addr.Resource.Absolute(module),
61-
})
62-
}
63-
addRootNodeToGraph(&g)
64-
65-
return &g, nil
66-
}
67-
68-
// NodeApplyableResource represents a resource that is "applyable":
69-
// it may need to have its record in the state adjusted to match configuration.
70-
//
71-
// Unlike in the plan walk, this resource node does not DynamicExpand. Instead,
72-
// it should be inserted into the same graph as any instances of the nodes
73-
// with dependency edges ensuring that the resource is evaluated before any
74-
// of its instances, which will turn ensure that the whole-resource record
75-
// in the state is suitably prepared to receive any updates to instances.
76-
type NodeApplyableResource struct {
77-
*NodeAbstractResource
78-
79-
Addr addrs.AbsResource
80-
}
81-
82-
var (
83-
_ GraphNodeModuleInstance = (*NodeApplyableResource)(nil)
84-
_ GraphNodeConfigResource = (*NodeApplyableResource)(nil)
85-
_ GraphNodeExecutable = (*NodeApplyableResource)(nil)
86-
_ GraphNodeProviderConsumer = (*NodeApplyableResource)(nil)
87-
_ GraphNodeAttachResourceConfig = (*NodeApplyableResource)(nil)
88-
_ GraphNodeReferencer = (*NodeApplyableResource)(nil)
89-
)
90-
91-
func (n *NodeApplyableResource) Path() addrs.ModuleInstance {
92-
return n.Addr.Module
93-
}
94-
95-
// GraphNodeExecutable
96-
func (n *NodeApplyableResource) Execute(ctx EvalContext, op walkOperation) tfdiags.Diagnostics {
97-
if n.Config == nil {
98-
// Nothing to do, then.
99-
log.Printf("[TRACE] NodeApplyableResource: no configuration present for %s", n.Name())
100-
return nil
54+
ctx = ctx.WithPath(module)
55+
diags = diags.Append(n.writeResourceState(ctx, n.Addr.Resource.Absolute(module)))
10156
}
10257

103-
return n.writeResourceState(ctx, n.Addr)
58+
return diags
10459
}

internal/terraform/node_resource_apply_test.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,40 @@ import (
1212
"github.com/hashicorp/terraform/internal/states"
1313
)
1414

15-
func TestNodeApplyableResourceExecute(t *testing.T) {
15+
func TestNodeExpandApplyableResourceExecute(t *testing.T) {
1616
state := states.NewState()
17-
ctx := &MockEvalContext{
18-
StateState: state.SyncWrapper(),
19-
InstanceExpanderExpander: instances.NewExpander(),
20-
}
21-
2217
t.Run("no config", func(t *testing.T) {
23-
node := NodeApplyableResource{
18+
ctx := &MockEvalContext{
19+
StateState: state.SyncWrapper(),
20+
InstanceExpanderExpander: instances.NewExpander(),
21+
}
22+
23+
node := &nodeExpandApplyableResource{
2424
NodeAbstractResource: &NodeAbstractResource{
25+
Addr: mustConfigResourceAddr("test_instance.foo"),
2526
Config: nil,
2627
},
27-
Addr: mustAbsResourceAddr("test_instance.foo"),
2828
}
2929
diags := node.Execute(ctx, walkApply)
3030
if diags.HasErrors() {
3131
t.Fatalf("unexpected error: %s", diags.Err())
3232
}
33+
34+
state.PruneResourceHusks()
3335
if !state.Empty() {
3436
t.Fatalf("expected no state, got:\n %s", state.String())
3537
}
3638
})
3739

3840
t.Run("simple", func(t *testing.T) {
41+
ctx := &MockEvalContext{
42+
StateState: state.SyncWrapper(),
43+
InstanceExpanderExpander: instances.NewExpander(),
44+
}
3945

40-
node := NodeApplyableResource{
46+
node := &nodeExpandApplyableResource{
4147
NodeAbstractResource: &NodeAbstractResource{
48+
Addr: mustConfigResourceAddr("test_instance.foo"),
4249
Config: &configs.Resource{
4350
Mode: addrs.ManagedResourceMode,
4451
Type: "test_instance",
@@ -49,7 +56,6 @@ func TestNodeApplyableResourceExecute(t *testing.T) {
4956
Module: addrs.RootModule,
5057
},
5158
},
52-
Addr: mustAbsResourceAddr("test_instance.foo"),
5359
}
5460
diags := node.Execute(ctx, walkApply)
5561
if diags.HasErrors() {

0 commit comments

Comments
 (0)