Skip to content

Commit 0a5c90d

Browse files
committed
fix: rewrite onlyDataResources
1 parent 545a256 commit 0a5c90d

File tree

2 files changed

+70
-10
lines changed

2 files changed

+70
-10
lines changed

provisioner/terraform/executor.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -264,18 +264,21 @@ func (e *executor) plan(ctx, killCtx context.Context, env, vars []string, logr l
264264
}, nil
265265
}
266266

267-
func onlyDataResources(sm *tfjson.StateModule) *tfjson.StateModule {
268-
sm2 := *sm
269-
sm2.Resources = make([]*tfjson.StateResource, 0, len(sm.Resources))
267+
func onlyDataResources(sm tfjson.StateModule) tfjson.StateModule {
268+
filtered := sm
269+
filtered.Resources = []*tfjson.StateResource{}
270270
for _, r := range sm.Resources {
271271
if r.Mode == "data" {
272-
sm2.Resources = append(sm2.Resources, r)
272+
filtered.Resources = append(filtered.Resources, r)
273273
}
274274
}
275+
276+
filtered.ChildModules = []*tfjson.StateModule{}
275277
for _, c := range sm.ChildModules {
276-
sm2.ChildModules = append(sm2.ChildModules, onlyDataResources(c))
278+
filteredChild := onlyDataResources(*c)
279+
filtered.ChildModules = append(filtered.ChildModules, &filteredChild)
277280
}
278-
return &sm2
281+
return filtered
279282
}
280283

281284
// planResources must only be called while the lock is held.
@@ -300,10 +303,9 @@ func (e *executor) planResources(ctx, killCtx context.Context, planfilePath stri
300303
// We don't want all prior resources, because Quotas (and
301304
// future features) would never know which resources are getting
302305
// deleted by a stop.
303-
modules = append(
304-
modules,
305-
onlyDataResources(plan.PriorState.Values.RootModule),
306-
)
306+
307+
filtered := onlyDataResources(*plan.PriorState.Values.RootModule)
308+
modules = append(modules, &filtered)
307309
}
308310
modules = append(modules, plan.PlannedValues.RootModule)
309311

provisioner/terraform/executor_internal_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package terraform
33
import (
44
"testing"
55

6+
tfjson "github.com/hashicorp/terraform-json"
67
"github.com/stretchr/testify/require"
78

89
"github.com/coder/coder/v2/provisionersdk/proto"
@@ -41,3 +42,60 @@ From standing in the English rain`))
4142
}
4243
require.Equal(t, expected, logr.logs)
4344
}
45+
46+
func TestOnlyDataResources(t *testing.T) {
47+
t.Parallel()
48+
49+
sm := &tfjson.StateModule{
50+
Resources: []*tfjson.StateResource{
51+
{
52+
Name: "cat",
53+
Type: "coder_parameter",
54+
Mode: "data",
55+
Address: "cat-address",
56+
},
57+
{
58+
Name: "dog",
59+
Type: "foobar",
60+
Mode: "magic",
61+
Address: "dog-address",
62+
},
63+
{
64+
Name: "cow",
65+
Type: "foobaz",
66+
Mode: "data",
67+
Address: "cow-address",
68+
},
69+
},
70+
ChildModules: []*tfjson.StateModule{
71+
{
72+
Resources: []*tfjson.StateResource{
73+
{
74+
Name: "child-cat",
75+
Type: "coder_parameter",
76+
Mode: "data",
77+
Address: "child-cat-address",
78+
},
79+
{
80+
Name: "child-dog",
81+
Type: "foobar",
82+
Mode: "data",
83+
Address: "child-dog-address",
84+
},
85+
{
86+
Name: "child-cow",
87+
Mode: "data",
88+
Type: "magic",
89+
Address: "child-cow-address",
90+
},
91+
},
92+
Address: "child-module-1",
93+
},
94+
},
95+
Address: "fake-module",
96+
}
97+
98+
filtered := onlyDataResources(*sm)
99+
require.Len(t, filtered.Resources, 2)
100+
require.Len(t, filtered.ChildModules, 1)
101+
}

0 commit comments

Comments
 (0)