Skip to content

Commit bc7d95f

Browse files
committed
feat: Add destroy to workspace provision job
This enables the full flow of create/update/delete.
1 parent dac812d commit bc7d95f

File tree

4 files changed

+35
-25
lines changed

4 files changed

+35
-25
lines changed

coderd/provisionerdaemons.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,8 @@ func convertWorkspaceTransition(transition database.WorkspaceTransition) (sdkpro
693693
return sdkproto.WorkspaceTransition_START, nil
694694
case database.WorkspaceTransitionStop:
695695
return sdkproto.WorkspaceTransition_STOP, nil
696+
case database.WorkspaceTransitionDelete:
697+
return sdkproto.WorkspaceTransition_DESTROY, nil
696698
default:
697699
return 0, xerrors.Errorf("unrecognized transition: %q", transition)
698700
}

provisioner/terraform/provision.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ func (t *terraform) runTerraformApply(ctx context.Context, terraform *tfexec.Ter
317317
}()
318318

319319
t.logger.Debug(ctx, "running apply", slog.F("vars", len(vars)), slog.F("env", len(env)))
320-
err := runApplyCommand(ctx, t.shutdownCtx, terraform.ExecPath(), terraform.WorkingDir(), writer, env, vars)
320+
err := runApplyCommand(ctx, t.shutdownCtx, request.Metadata.WorkspaceTransition, terraform.ExecPath(), terraform.WorkingDir(), writer, env, vars)
321321
if err != nil {
322322
errorMessage := err.Error()
323323
// Terraform can fail and apply and still need to store it's state.
@@ -440,7 +440,7 @@ func (t *terraform) runTerraformApply(ctx context.Context, terraform *tfexec.Ter
440440

441441
// This couldn't use terraform-exec, because it doesn't support cancellation, and there didn't appear
442442
// to be a straight-forward way to add it.
443-
func runApplyCommand(ctx, shutdownCtx context.Context, bin, dir string, stdout io.Writer, env, vars []string) error {
443+
func runApplyCommand(ctx, shutdownCtx context.Context, transition proto.WorkspaceTransition, bin, dir string, stdout io.Writer, env, vars []string) error {
444444
args := []string{
445445
"apply",
446446
"-no-color",
@@ -449,6 +449,9 @@ func runApplyCommand(ctx, shutdownCtx context.Context, bin, dir string, stdout i
449449
"-json",
450450
"-refresh=true",
451451
}
452+
if transition == proto.WorkspaceTransition_DESTROY {
453+
args = append(args, "-destroy")
454+
}
452455
for _, variable := range vars {
453456
args = append(args, "-var", variable)
454457
}

provisionersdk/proto/provisioner.pb.go

Lines changed: 27 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

provisionersdk/proto/provisioner.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ message Parse {
109109
enum WorkspaceTransition {
110110
START = 0;
111111
STOP = 1;
112+
DESTROY = 2;
112113
}
113114

114115
// Provision consumes source-code from a directory to produce resources.

0 commit comments

Comments
 (0)