8000 fix(cli): `cdk ls` returns stack id instead of stack display name (#2… · aws/aws-cdk@effad1c · GitHub
[go: up one dir, main page]

Skip to content

Commit effad1c

Browse files
committed
fix(cli): cdk ls returns stack id instead of stack display name (#29447)
### Issue # (if applicable) Closes #29420 ### Reason for this change The `cdk list` functionality displays the stacks . For instance ``` > cdk ls producer consumer ``` With the latest changes for list stack dependencies we did add a new flag `-d` to show the dependencies. The dependencies between stacks can be established in 2 ways: 1. Using the resource defined from one stack in another. 2. Using `addDependency()` to add dependency among stacks. Current we are fetching the dependency details through the `CloudStackArtifact`. * Establishing the dependency between stacks through the first method would have the same `displayName` and `id` for the stacks. Using the `-d` flag to display dependencies - ``` ❯ cdk list --show-dependencies - id: producer dependencies: [] - id: consumer dependencies: - id: producer dependencies: [] ``` * Establishing the dependency through `addDependency()` will create a different `displayName` and `id`. In such a case when a user runs `cdk ls` we would want to show the `displayName` and if not present then use the `id` For instance: ``` > cdk ls producer producer/consumer ``` With the `-d` flag we would want to display something like: ``` > cdk ls -d - id: producer dependencies: [] - id: producer/consumer dependencies: - id: producer dependencies: [] ``` With our previous change we didn't consider `displayName` and just fetched `id`s which changes the previous functionality and caused a regression. ### Description of changes With the new changes we are looking out for `displayName` first and if it does not exist we fetch the `id`. ### Description of how you validated changes Added a new unit test and updated integ tests. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 9a51c89 commit effad1c

File tree

5 files changed

+101
-8
lines changed

5 files changed

+101
-8
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This patch brings the [fix](https://github.com/aws/aws-cdk/issues/29420) into the regression suite.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Skipping the test to fix issue https://github.com/aws/aws-cdk/issues/29420.
2+
# cli-integ tests failing for the old tests with the new cli changes for list stacks.
3+
4+
cdk ls --show-dependencies --json

packages/@aws-cdk-testing/cli-integ/tests/cli-integ-tests/cli.integtest.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -886,10 +886,10 @@ integTest('cdk ls --show-dependencies --json', withDefaultFixture(async (fixture
886886
id: 'list-stacks',
887887
dependencies: [
888888
{
889-
id: 'liststacksDependentStack',
889+
id: 'list-stacks/DependentStack',
890890
dependencies: [
891891
{
892-
id: 'liststacksDependentStackInnerDependentStack',
892+
id: 'list-stacks/DependentStack/InnerDependentStack',
893893
dependencies: [],
894894
},
895895
],
@@ -900,11 +900,11 @@ integTest('cdk ls --show-dependencies --json', withDefaultFixture(async (fixture
900900
id: 'list-multiple-dependent-stacks',
901901
dependencies: [
902902
{
903-
id: 'listmultipledependentstacksDependentStack1',
903+
id: 'list-multiple-dependent-stacks/DependentStack1',
904904
dependencies: [],
905905
},
906906
{
907-
id: 'listmultipledependentstacksDependentStack2',
907+
id: 'list-multiple-dependent-stacks/DependentStack2',
908908
dependencies: [],
909909
},
910910
],

packages/aws-cdk/lib/list-stacks.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export async function listStacks(toolkit: CdkToolkit, options: ListStacksOptions
5656

5757
for (const stack of collectionOfStacks.stackArtifacts) {
5858
const data: StackDetails = {
59-
id: stack.id,
59+
id: stack.displayName ?? stack.id,
6060
name: stack.stackName,
6161
environment: stack.environment,
6262
dependencies: [],
@@ -82,7 +82,7 @@ export async function listStacks(toolkit: CdkToolkit, options: ListStacksOptions
8282
}
8383
} else {
8484
data.dependencies.push({
85-
id: depStack.stackArtifacts[0].id,
85+
id: depStack.stackArtifacts[0].displayName ?? depStack.stackArtifacts[0].id,
8686
dependencies: [],
8787
});
8888
}

packages/aws-cdk/test/list-stacks.test.ts

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ describe('list', () => {
171171
dependencies: [],
172172
},
173173
{
174-
id: 'Test-Stack-B',
174+
id: 'Test-Stack-A/Test-Stack-B',
175175
name: 'Test-Stack-B',
176176
environment: {
177177
account: '123456789012',
@@ -185,7 +185,7 @@ describe('list', () => {
185185
}]));
186186
});
187187

188-
test('stacks with nested dependencies', async () => {
188+
test('stacks with display names and have nested dependencies', async () => {
189189
let cloudExecutable = new MockCloudExecutable({
190190
stacks: [
191191
MockStack.MOCK_STACK_A,
@@ -201,9 +201,84 @@ describe('list', () => {
201201
],
202202
},
203203
depends: ['Test-Stack-A'],
204+
displayName: 'Test-Stack-A/Test-Stack-B',
204205
},
205206
{
206207
stackName: 'Test-Stack-C',
208+
template: { Resources: { TemplateName: 'Test-Stack-C' } },
209+
env: 'aws://123456789012/bermuda-triangle-1',
210+
metadata: {
211+
'/Test-Stack-C': [
212+
{
213+
type: cxschema.ArtifactMetadataEntryType.STACK_TAGS,
214+
},
215+
],
216+
},
217+
depends: ['Test-Stack-B'],
218+
displayName: 'Test-Stack-A/Test-Stack-B/Test-Stack-C',
219+
},
220+
],
221+
});
222+
223+
// GIVEN
224+
const toolkit = new CdkToolkit({
225+
cloudExecutable,
226+
configuration: cloudExecutable.configuration,
227+
sdkProvider: cloudExecutable.sdkProvider,
228+
deployments: cloudFormation,
229+
});
230+
231+
// WHEN
232+
const workflow = await listStacks( toolkit, { selectors: ['Test-Stack-A', 'Test-Stack-A/Test-Stack-B', 'Test-Stack-A/Test-Stack-B/Test-Stack-C'] });
233+
234+
// THEN
235+
expect(JSON.stringify(workflow)).toEqual(JSON.stringify([{
236+
id: 'Test-Stack-A',
237+
name: 'Test-Stack-A',
238+
environment: {
239+
account: '123456789012',
240+
region: 'bermuda-triangle-1',
241+
name: 'aws://123456789012/bermuda-triangle-1',
242+
},
243+
dependencies: [],
244+
},
245+
{
246+
id: 'Test-Stack-A/Test-Stack-B',
247+
name: 'Test-Stack-B',
248+
environment: {
249+
account: '123456789012',
250+
region: 'bermuda-triangle-1',
251+
name: 'aws://123456789012/bermuda-triangle-1',
252+
},
253+
dependencies: [{
254+
id: 'Test-Stack-A',
255+
dependencies: [],
256+
}],
257+
},
258+
{
259+
id: 'Test-Stack-A/Test-Stack-B/Test-Stack-C',
260+
name: 'Test-Stack-C',
261+
environment: {
262+
account: '123456789012',
263+
region: 'bermuda-triangle-1',
264+
name: 'aws://123456789012/bermuda-triangle-1',
265+
},
266+
dependencies: [{
267+
id: 'Test-Stack-A/Test-Stack-B',
268+
dependencies: [{
269+
id: 'Test-Stack-A',
270+
dependencies: [],
271+
}],
272+
}],
273+
}]));
274+
});
275+
276+
test('stacks with nested dependencies', async () => {
277+
let cloudExecutable = new MockCloudExecutable({
278+
stacks: [
279+
MockStack.MOCK_STACK_A,
280+
{
281+
stackName: 'Test-Stack-B',
207282
template: { Resources: { TemplateName: 'Test-Stack-B' } },
208283
env: 'aws://123456789012/bermuda-triangle-1',
209284
metadata: {
@@ -213,6 +288,19 @@ describe('list', () => {
213288
},
214289
],
215290
},
291+
depends: ['Test-Stack-A'],
292+
},
293+
{
294+
stackName: 'Test-Stack-C',
295+
template: { Resources: { TemplateName: 'Test-Stack-C' } },
296+
env: 'aws://123456789012/bermuda-triangle-1',
297+
metadata: {
298+
'/Test-Stack-C': [
299+
{
300+
type: cxschema.ArtifactMetadataEntryType.STACK_TAGS,
301+
},
302+
],
303+
},
216304
depends: ['Test-Stack-B'],
217305
},
218306
],

0 commit comments

Comments
 (0)
0