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
Changes from all 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
42 changes: 41 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 * as uuid from 'uuid';
import { DeploymentMethod } from './api';
import { SdkProvider } from './api/aws-auth';
Expand All @@ -29,6 +31,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 @@ -589,6 +592,9 @@ export class CdkToolkit {
stacks = stacks.reversed();

if (!options.force) {
if (stacks.stackArtifacts.length === 0) {
return;
}
// eslint-disable-next-line max-len
const confirmed = await promptly.confirm(`Are you sure you want to delete: ${chalk.blue(stacks.stackArtifacts.map(s => s.hierarchicalId).join(', '))} (y/n)?`);
if (!confirmed) {
Expand Down Expand Up @@ -882,9 +888,43 @@ export class CdkToolkit {
extend: exclusively ? ExtendedStackSelection.None : ExtendedStackSelection.Downstream,
defaultBehavior: DefaultSelection.OnlySingle,
});
const selectorWithoutPatterns: StackSelector = {
...selector,
allTopLevel: true,
patterns: [],
};
const stacksWithoutPatterns = await assembly.selectStacks(selectorWithoutPatterns, {
extend: exclusively ? ExtendedStackSelection.None : ExtendedStackSelection.Downstream,
defaultBehavior: DefaultSelection.OnlySingle,
});

// No validation
const patterns = selector.patterns.map(pattern => {
const notExist = !stacks.stackArtifacts.find(stack =>
minimatch(stack.hierarchicalId, pattern) || (stack.id === pattern && semver.major(versionNumber()) < 2),
);

const closelyMatched = notExist ? stacksWithoutPatterns.stackArtifacts.map(stack => {
if (minimatch(stack.hierarchicalId.toLowerCase(), pattern.toLowerCase())) {
return stack.hierarchicalId;
}
if (stack.id.toLowerCase() === pattern.toLowerCase() && semver.major(versionNumber()) < 2) {
return stack.id;
}
return;
}).filter((stack): stack is string => stack !== undefined) : [];
return {
pattern,
notExist,
closelyMatched,
};
});

patterns.forEach(pattern => {
if (pattern.notExist) {
const closelyMatched = pattern.closelyMatched.length > 0 ? ` Do you mean ${pattern.closelyMatched.join(', ')}?` : '';
warning(`${pattern.pattern} does not exist.${closelyMatched}`);
}
});
return stacks;
}

Expand Down