Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): warn of non-existent stacks in cdk destroy #27921

Merged
merged 36 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
56293ef
fix(cli): No exception when stack with wrong cases is deployed
go-to-k Sep 23, 2023
bec1d03
Revert "fix(cli): No exception when stack with wrong cases is deployed"
go-to-k Sep 23, 2023
16b0bab
add a test
go-to-k Sep 23, 2023
c3963b1
using matchingPattern
go-to-k Sep 23, 2023
85284b2
update for selectStacksForDestroy
go-to-k Sep 26, 2023
6563a5f
fix order of import
go-to-k Sep 26, 2023
27a39fc
search for v1
go-to-k Oct 12, 2023
6b4dbe2
change import order
go-to-k Oct 12, 2023
3dbc266
cli-integ-tests
go-to-k Oct 12, 2023
b16eb1f
Merge branch 'main' into fix/destroy-not-exits
go-to-k Oct 20, 2023
df5002f
Merge branch 'main' into fix/destroy-not-exits
go-to-k Dec 4, 2023
cb396c1
change an error message
go-to-k Dec 5, 2023
f757fa3
add one unit test
go-to-k Dec 5, 2023
8539c61
Merge branch 'main' into fix/destroy-not-exits
vinayak-kukreja Dec 5, 2023
5d99fd7
change cli integ test for cdk migrate
go-to-k Dec 6, 2023
896db29
Merge branch 'main' into fix/destroy-not-exits
vinayak-kukreja Dec 6, 2023
e985e72
change for cli integ tests
go-to-k Dec 7, 2023
0ea97b8
Merge branch 'main' into fix/destroy-not-exits
go-to-k Dec 7, 2023
a22025b
Merge branch 'main' into fix/destroy-not-exits
go-to-k Dec 7, 2023
749f755
Merge branch 'main' into fix/destroy-not-exits
vinayak-kukreja Dec 7, 2023
a16c8c6
undo fixture.cdkDestroy
go-to-k Dec 9, 2023
4e0730a
to log this to the user instead of throwing an error
go-to-k Dec 9, 2023
eb363c7
Merge branch 'fix/destroy-not-exits' of https://github.com/go-to-k/aw…
go-to-k Dec 9, 2023
df4c865
Merge branch 'main' into fix/destroy-not-exits
go-to-k Jan 8, 2024
1335926
Merge branch 'main' into fix/destroy-not-exits
go-to-k Jan 11, 2024
1e90fed
Merge branch 'main' into fix/destroy-not-exits
paulhcsun Jan 23, 2024
ce29bb9
Merge branch 'main' into fix/destroy-not-exits
vinayak-kukreja Jan 23, 2024
a2de817
Merge branch 'main' into fix/destroy-not-exits
paulhcsun Jan 23, 2024
01d7c21
recommendation for stacks matched closely
go-to-k Jan 24, 2024
aa9e080
Merge branch 'main' into fix/destroy-not-exits
paulhcsun Jan 24, 2024
1ac0cf9
Merge branch 'main' into fix/destroy-not-exits
paulhcsun Jan 25, 2024
7a64bae
Merge branch 'main' into fix/destroy-not-exits
go-to-k Mar 3, 2024
75ae70f
Merge branch 'main' into fix/destroy-not-exits
go-to-k Mar 13, 2024
95f2caa
Merge branch 'main' into fix/destroy-not-exits
paulhcsun Mar 15, 2024
e5ac121
Merge branch 'main' into fix/destroy-not-exits
paulhcsun Mar 18, 2024
ab639fa
Merge branch 'main' into fix/destroy-not-exits
paulhcsun Mar 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,13 @@ integTest('hotswap deployment supports Fn::ImportValue intrinsic', withDefaultFi
}
}));

integTest('cdk destroy fails when the stacks do not exist', withDefaultFixture(async (fixture) => {
const nonExistingStackName1 = 'non-existing-stack-1';
const nonExistingStackName2 = 'non-existing-stack-2';

await expect(fixture.cdkDestroy([nonExistingStackName1, nonExistingStackName2])).rejects.toThrow('exited with error');
}));

async function listChildren(parent: string, pred: (x: string) => Promise<boolean>) {
const ret = new Array<string>();
for (const child of await fs.readdir(parent, { encoding: 'utf-8' })) {
Expand Down
10 changes: 9 additions & 1 deletion packages/aws-cdk/lib/cdk-toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import * as cxapi from '@aws-cdk/cx-api';
import * as chalk from 'chalk';
import * as chokidar from 'chokidar';
import * as fs from 'fs-extra';
import { minimatch } from 'minimatch';
import * as promptly from 'promptly';
import * as semver from 'semver';
import { DeploymentMethod } from './api';
import { SdkProvider } from './api/aws-auth';
import { Bootstrapper, BootstrapEnvironmentOptions } from './api/bootstrap';
Expand All @@ -26,6 +28,7 @@ import { validateSnsTopicArn } from './util/validate-notification-arn';
import { Concurrency, WorkGraph } from './util/work-graph';
import { WorkGraphBuilder } from './util/work-graph-builder';
import { AssetBuildNode, AssetPublishNode, StackNode } from './util/work-graph-types';
import { versionNumber } from './version';
import { environmentsFromDescriptors, globEnvironmentsFromStacks, looksLikeGlob } from '../lib/api/cxapp/environments';

export interface CdkToolkitProps {
Expand Down Expand Up @@ -768,7 +771,12 @@ export class CdkToolkit {
defaultBehavior: DefaultSelection.OnlySingle,
});

// No validation
const notExistPatterns = selector.patterns.filter(pattern => !stacks.stackArtifacts.find(stack =>
minimatch(stack.hierarchicalId, pattern) || (stack.id === pattern && semver.major(versionNumber()) < 2),
));
if (notExistPatterns.length > 0) {
throw new Error(`Cannot run cdk destroy on stack(s) ${selector.patterns.join(', ')}. ${notExistPatterns.join(', ')} not exist.`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new Error(`Cannot run cdk destroy on stack(s) ${selector.patterns.join(', ')}. ${notExistPatterns.join(', ')} not exist.`);
throw new Error(`Cannot run cdk destroy on stack(s). ${notExistPatterns.join(', ')} ${notExistsPatterns.length === 1 ? 'does' : 'do'} not exist.`);

More concise.

}

return stacks;
}
Expand Down
13 changes: 13 additions & 0 deletions packages/aws-cdk/test/cdk-toolkit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,19 @@ describe('destroy', () => {
});
}).resolves;
});

test('fail on non-existent stack', async () => {
const toolkit = defaultToolkitSetup();

await expect(() => {
return toolkit.destroy({
selector: { patterns: ['Test-Stack-A/Test-Stack-C', 'Test-Stack-X', 'Test-Stack-Y'] },
exclusively: true,
force: true,
fromDeploy: true,
});
}).rejects.toThrowError('Cannot run cdk destroy on stack(s) Test-Stack-A/Test-Stack-C, Test-Stack-X, Test-Stack-Y. Test-Stack-X, Test-Stack-Y not exist.');
});
});

describe('watch', () => {
Expand Down
Loading