Skip to content

Commit

Permalink
feat: accept onDestroy command to run before resource destruction
Browse files Browse the repository at this point in the history
Accept an optional command via `onDestroy` parameter to enable running
a command during destroy-time.
See also: https://developer.hashicorp.com/terraform/language/resources/provisioners/syntax#destroy-time-provisioners
  • Loading branch information
ZauberNerd committed Jul 17, 2024
1 parent fa86688 commit e1724f0
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 6 deletions.
24 changes: 24 additions & 0 deletions API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 32 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,27 @@ export interface LocalExecConfig {
* @default true
*/
readonly copyBeforeRun?: boolean;

/**
* Command to run at destroy-time.
*/
readonly onDestroy?: string;
}

export { NullProvider as Provider } from "@cdktf/provider-null/lib/provider";

interface LocalExecProvisioner {
"local-exec": {
working_dir: string;
command: string;
when?: string;
};
}

export class LocalExec extends Resource {
public cwd: string;
public command: string;
public onDestroy?: string;

constructor(scope: Construct, id: string, config: LocalExecConfig) {
super(scope, id, config);
Expand All @@ -63,8 +77,9 @@ export class LocalExec extends Resource {

this.cwd = workingDir;
this.command = config.command;
this.onDestroy = config.onDestroy;

this.addOverride("provisioner", [
const overrides: LocalExecProvisioner[] = [
{
"local-exec": {
working_dir: workingDir,
Expand All @@ -74,6 +89,21 @@ export class LocalExec extends Resource {
}),
},
},
]);
];

if (typeof this.onDestroy === "string") {
overrides.push({
"local-exec": {
when: "destroy",
working_dir: workingDir,
command: Lazy.stringValue({
// TODO: wrap command to capture stdout and stderr
produce: () => this.onDestroy,
}),
},
});
}

this.addOverride("provisioner", overrides);
}
}
31 changes: 27 additions & 4 deletions test/local-exec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ const apply = (addConstructs: (scope: Construct) => void) => {
cwd: outdir,
});

return outdir;
const destroy = () => {
execSync("terraform destroy -auto-approve", {
cwd: outdir,
});
};

return { outdir, destroy };
};

const workingDirectoryForAsset = (manifestPath: string, name: string) => {
Expand Down Expand Up @@ -61,7 +67,7 @@ describe("LocalExec", () => {

test("runs command inside copy of working directory", () => {
// Create a file in the stack directory
const outdir = apply((stack) => {
const { outdir } = apply((stack) => {
new LocalExec(stack, "myresource", {
command: `cp ${__filename} test.txt`,
cwd: __dirname,
Expand Down Expand Up @@ -112,7 +118,7 @@ describe("LocalExec", () => {

test("runs command can use token value inside a command", () => {
const passwordLength = 4;
const outdir = apply((stack) => {
const { outdir } = apply((stack) => {
new provider.RandomProvider(stack, "random");

const waiter = new LocalExec(stack, "timer", {
Expand Down Expand Up @@ -144,7 +150,7 @@ describe("LocalExec", () => {

test("command can be overwritten", () => {
// Create a file in the stack directory
const outdir = apply((stack) => {
const { outdir } = apply((stack) => {
const exec = new LocalExec(stack, "myresource", {
command: "ls -la",
cwd: __dirname,
Expand All @@ -158,4 +164,21 @@ describe("LocalExec", () => {
expect(fs.existsSync(path.resolve(workingDir, "test.txt"))).toBe(true);
expect(fs.existsSync(path.resolve(__dirname, "test.txt"))).toBe(false);
});

test("can specify a command to run at destroy-time", () => {
// Create a file in the working directory
const { destroy } = apply((stack) => {
new LocalExec(stack, "test", {
command: "cp origin.txt test.txt",
cwd: testdir,
onDestroy: "rm test.txt",
});
});

expect(fs.existsSync(path.resolve(testdir, "test.txt"))).toBe(true);

destroy();

expect(fs.existsSync(path.resolve(testdir, "test.txt"))).toBe(false);
});
});

0 comments on commit e1724f0

Please sign in to comment.