Skip to content

Commit f239441

Browse files
committed
fix: correctly assess quota for stopped resources
#5710 introduced a bug whereby old resources were included in the Terraform provisioner's plan, even when transition was set to stop.
1 parent 6b8102c commit f239441

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

cli/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1339,7 +1339,7 @@ func newProvisionerDaemon(
13391339
Listener: terraformServer,
13401340
},
13411341
CachePath: tfDir,
1342-
Logger: logger,
1342+
Logger: logger.Named("terraform"),
13431343
Tracer: tracer,
13441344
})
13451345
if err != nil && !xerrors.Is(err, context.Canceled) {

provisioner/terraform/executor.go

+28-1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ func (e *executor) execWriteOutput(ctx, killCtx context.Context, args, env []str
8484
cmd.Stdout = syncWriter{mut, stdOutWriter}
8585
cmd.Stderr = syncWriter{mut, stdErrWriter}
8686

87+
e.server.logger.Debug(ctx, "executing terraform command",
88+
slog.F("binary_path", e.binaryPath),
89+
slog.F("args", args),
90+
)
8791
err = cmd.Start()
8892
if err != nil {
8993
return err
@@ -260,6 +264,20 @@ func (e *executor) plan(ctx, killCtx context.Context, env, vars []string, logr l
260264
}, nil
261265
}
262266

267+
func onlyDataResources(sm *tfjson.StateModule) *tfjson.StateModule {
268+
sm2 := *sm
269+
sm2.Resources = make([]*tfjson.StateResource, 0, len(sm.Resources))
270+
for _, r := range sm.Resources {
271+
if r.Mode == "data" {
272+
sm2.Resources = append(sm2.Resources, r)
273+
}
274+
}
275+
for _, c := range sm.ChildModules {
276+
sm2.ChildModules = append(sm2.ChildModules, onlyDataResources(c))
277+
}
278+
return &sm2
279+
}
280+
263281
// planResources must only be called while the lock is held.
264282
func (e *executor) planResources(ctx, killCtx context.Context, planfilePath string) (*State, error) {
265283
ctx, span := e.server.startTrace(ctx, tracing.FuncName())
@@ -276,7 +294,16 @@ func (e *executor) planResources(ctx, killCtx context.Context, planfilePath stri
276294
}
277295
modules := []*tfjson.StateModule{}
278296
if plan.PriorState != nil {
279-
modules = append(modules, plan.PriorState.Values.RootModule)
297+
// We need the data resources for rich parameters. For some reason, they
298+
// only show up in the PriorState.
299+
//
300+
// We don't want all prior resources, because Quotas (and
301+
// future features) would never know which resources are getting
302+
// deleted by a stop.
303+
modules = append(
304+
modules,
305+
onlyDataResources(plan.PriorState.Values.RootModule),
306+
)
280307
}
281308
modules = append(modules, plan.PlannedValues.RootModule)
282309

0 commit comments

Comments
 (0)