Skip to content

Commit e93c2aa

Browse files
committed
refactor: update listStacks
1 parent d202737 commit e93c2aa

File tree

4 files changed

+36
-18
lines changed

4 files changed

+36
-18
lines changed

packages/core/src/lambda/explorer/cloudFormationNodes.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const localize = nls.loadMessageBundle()
99
import { CloudFormation, Lambda } from 'aws-sdk'
1010
import * as os from 'os'
1111
import * as vscode from 'vscode'
12-
import { CloudFormationClient } from '../../shared/clients/cloudFormation'
12+
import { CloudFormationClient, StackSummary } from '../../shared/clients/cloudFormation'
1313
import { DefaultLambdaClient } from '../../shared/clients/lambdaClient'
1414

1515
import { AWSResourceNode } from '../../shared/treeview/nodes/awsResourceNode'
@@ -66,7 +66,7 @@ export class CloudFormationStackNode extends AWSTreeNodeBase implements AWSResou
6666
public constructor(
6767
public readonly parent: AWSTreeNodeBase,
6868
public override readonly regionCode: string,
69-
private stackSummary: CloudFormation.StackSummary,
69+
private stackSummary: StackSummary,
7070
private readonly lambdaClient = new DefaultLambdaClient(regionCode),
7171
private readonly cloudformationClient = new CloudFormationClient(regionCode)
7272
) {
@@ -114,7 +114,7 @@ export class CloudFormationStackNode extends AWSTreeNodeBase implements AWSResou
114114
})
115115
}
116116

117-
public update(stackSummary: CloudFormation.StackSummary): void {
117+
public update(stackSummary: StackSummary): void {
118118
this.stackSummary = stackSummary
119119
this.label = `${this.stackName} [${stackSummary.StackStatus}]`
120120
this.tooltip = `${this.stackName}${os.EOL}${this.stackId}`

packages/core/src/lambda/utils.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import * as nls from 'vscode-nls'
77
const localize = nls.loadMessageBundle()
88

99
import xml2js = require('xml2js')
10-
import { CloudFormation, Lambda } from 'aws-sdk'
10+
import { Lambda } from 'aws-sdk'
1111
import * as vscode from 'vscode'
12-
import { CloudFormationClient } from '../shared/clients/cloudFormation'
12+
import { CloudFormationClient, StackSummary } from '../shared/clients/cloudFormation'
1313
import { LambdaClient } from '../shared/clients/lambdaClient'
1414
import { getFamily, getNodeMajorVersion, RuntimeFamily } from './models/samLambdaRuntime'
1515
import { getLogger } from '../shared/logger/logger'
@@ -18,9 +18,7 @@ import { FileResourceFetcher } from '../shared/resourcefetcher/fileResourceFetch
1818
import { sampleRequestManifestPath } from './constants'
1919
import globals from '../shared/extensionGlobals'
2020

21-
export async function* listCloudFormationStacks(
22-
client: CloudFormationClient
23-
): AsyncIterableIterator<CloudFormation.StackSummary> {
21+
export async function* listCloudFormationStacks(client: CloudFormationClient): AsyncIterableIterator<StackSummary> {
2422
// TODO: this 'loading' message needs to go under each regional entry
2523
// in the explorer, and be removed when that region's query completes
2624
const status = vscode.window.setStatusBarMessage(

packages/core/src/shared/clients/cloudFormation.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ import * as CloudFormationV3 from '@aws-sdk/client-cloudformation'
88
import globals from '../extensionGlobals'
99
import { AsyncCollection } from '../utilities/asyncCollection'
1010
import { pageableToCollection } from '../utilities/collectionUtils'
11-
import { isNonNullable } from '../utilities/tsUtils'
11+
import { hasProps, isNonNullable, RequiredProps } from '../utilities/tsUtils'
1212
import { ClientWrapper } from './clientWrapper'
1313

14+
export interface StackSummary
15+
extends RequiredProps<CloudFormationV3.StackSummary, 'StackName' | 'CreationTime' | 'StackStatus'> {
16+
DriftInformation: RequiredProps<CloudFormationV3.StackDriftInformation, 'StackDriftStatus'>
17+
}
1418
export class CloudFormationClient extends ClientWrapper<CloudFormationV3.CloudFormationClient> {
1519
public constructor(regionCode: string) {
1620
super(regionCode, CloudFormationV3.CloudFormationClient)
@@ -33,22 +37,32 @@ export class CloudFormationClient extends ClientWrapper<CloudFormationV3.CloudFo
3337

3438
public async *listStacks(
3539
statusFilter: string[] = ['CREATE_COMPLETE', 'UPDATE_COMPLETE']
36-
): AsyncIterableIterator<CloudFormation.StackSummary> {
37-
const client = await this.createSdkClient()
38-
40+
): AsyncIterableIterator<StackSummary> {
3941
const request: CloudFormation.ListStacksInput = {
4042
StackStatusFilter: statusFilter,
4143
}
4244

4345
do {
44-
const response: CloudFormation.ListStacksOutput = await client.listStacks(request).promise()
45-
46-
if (response.StackSummaries) {
47-
yield* response.StackSummaries
46+
const response: CloudFormationV3.ListStacksOutput = await this.makeRequest(
47+
CloudFormationV3.ListStacksCommand,
48+
request
49+
)
50+
51+
const filteredResponse = response.StackSummaries?.filter(isStackSummary)
52+
if (filteredResponse && filteredResponse.length > 0) {
53+
yield* filteredResponse
4854
}
4955

5056
request.NextToken = response.NextToken
5157
} while (request.NextToken)
58+
59+
function isStackSummary(s: CloudFormationV3.StackSummary | undefined): s is StackSummary {
60+
return (
61+
isNonNullable(s) &&
62+
hasProps(s, 'StackName', 'CreationTime', 'StackStatus', 'DriftInformation') &&
63+
hasProps(s.DriftInformation, 'StackDriftStatus')
64+
)
65+
}
5266
}
5367

5468
public listAllStacks(request: CloudFormation.ListStacksInput = {}): AsyncCollection<CloudFormation.StackSummary[]> {

packages/core/src/test/lambda/explorer/cloudFormationNodes.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
contextValueCloudformationLambdaFunction,
1313
} from '../../../lambda/explorer/cloudFormationNodes'
1414
import { LambdaFunctionNode } from '../../../lambda/explorer/lambdaFunctionNode'
15-
import { CloudFormationClient } from '../../../shared/clients/cloudFormation'
15+
import { CloudFormationClient, StackSummary } from '../../../shared/clients/cloudFormation'
1616
import { DefaultLambdaClient } from '../../../shared/clients/lambdaClient'
1717
import globals from '../../../shared/extensionGlobals'
1818
import { TestAWSTreeNode } from '../../shared/treeview/nodes/testAWSTreeNode'
@@ -44,6 +44,9 @@ function createCloudFormationClient(...stackNames: string[]) {
4444
StackName: name,
4545
CreationTime: new globals.clock.Date(),
4646
StackStatus: 'CREATE_COMPLETE',
47+
DriftInformation: {
48+
StackDriftStatus: 'UNKNOWN',
49+
},
4750
}
4851
})
4952
)
@@ -53,12 +56,15 @@ function createCloudFormationClient(...stackNames: string[]) {
5356
}
5457

5558
describe('CloudFormationStackNode', function () {
56-
function createStackSummary() {
59+
function createStackSummary(): StackSummary {
5760
return {
5861
CreationTime: new globals.clock.Date(),
5962
StackId: '1',
6063
StackName: 'myStack',
6164
StackStatus: 'UPDATE_COMPLETE',
65+
DriftInformation: {
66+
StackDriftStatus: 'UNKNOWN',
67+
},
6268
}
6369
}
6470

0 commit comments

Comments
 (0)