Skip to content

Commit 60506e5

Browse files
authored
fix(toolkit-lib): drift detection options are unnecessarily required (#552)
## Description This PR improves the drift detection implementation in the CDK toolkit-lib by making the 'stacks' parameter optional in the DriftOptions interface and providing default options in the toolkit.drift() method. These changes enhance the API's usability and consistency. ### Changes: 1. Made 'stacks' parameter optional in DriftOptions interface 2. Added default empty options object to toolkit.drift() method 3. Added a test case to verify that drift action can be invoked without options These changes make the drift detection API more flexible and consistent with other toolkit methods that typically provide default options. --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license
1 parent 4d467ca commit 60506e5

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

packages/@aws-cdk/toolkit-lib/lib/actions/drift/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import type { StackSelector } from '../../api/cloud-assembly';
22

33
export interface DriftOptions {
44
/**
5-
* Criteria for selecting stacks to check for drift
5+
* Select stacks to check for drift
6+
*
7+
* @default - all stacks
68
*/
7-
readonly stacks: StackSelector;
9+
readonly stacks?: StackSelector;
810
}
911

1012
/**

packages/@aws-cdk/toolkit-lib/lib/toolkit/toolkit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ export class Toolkit extends CloudAssemblySourceBuilder {
382382
/**
383383
* Drift Action
384384
*/
385-
public async drift(cx: ICloudAssemblySource, options: DriftOptions): Promise<{ [name: string]: DriftResult }> {
385+
public async drift(cx: ICloudAssemblySource, options: DriftOptions = {}): Promise<{ [name: string]: DriftResult }> {
386386
const ioHelper = asIoHelper(this.ioHost, 'drift');
387387
const selectStacks = options.stacks ?? ALL_STACKS;
388388
const synthSpan = await ioHelper.span(SPAN.SYNTH_ASSEMBLY).begin({ stacks: selectStacks });

packages/@aws-cdk/toolkit-lib/test/actions/drift.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,19 @@ describe('drift', () => {
7373
ioHost.expectMessage({ containing: 'Modified Resources', level: 'info' });
7474
ioHost.expectMessage({ containing: '[~] AWS::S3::Bucket MyBucket MyBucketF68F3FF0', level: 'info' });
7575
});
76+
77+
test('can invoke drift action without options', async () => {
78+
// GIVEN
79+
mockCloudFormationClient.on(DetectStackDriftCommand).resolves({ StackDriftDetectionId: '12345' });
80+
mockCloudFormationClient.on(DescribeStackDriftDetectionStatusCommand).resolves({ DetectionStatus: 'DETECTION_COMPLETE' });
81+
mockCloudFormationClient.on(DescribeStackResourceDriftsCommand).resolvesOnce({});
82+
83+
// WHEN
84+
const cx = await builderFixture(toolkit, 'stack-with-bucket');
85+
const result = await toolkit.drift(cx);
86+
87+
// THEN
88+
expect(Object.keys(result).length).toBe(0);
89+
ioHost.expectMessage({ containing: 'No drift results available' });
90+
});
7691
});

0 commit comments

Comments
 (0)