Skip to content

Commit c2f7af8

Browse files
authored
fix(toolkit-lib): refactor feature is not marked as unstable (#563)
This introduces the concept of unstable featues to the toolkit. The CLI always opts into it because it has its own similar flag. Over time, the type `UnstableFeature` will change according to the features that are unstable at the moment. All commands that are considered unstable should call the `requireUnstableFeature` method. --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license
1 parent ad4ab4d commit c2f7af8

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ export interface ToolkitOptions {
131131
* @default - A fresh plugin host
132132
*/
133133
readonly pluginHost?: PluginHost;
134+
135+
/**
136+
* Set of unstable features to opt into. If you are using an unstable feature,
137+
* you must explicitly acknowledge that you are aware of the risks of using it,
138+
* by passing it in this set.
139+
*/
140+
readonly unstableFeatures?: Array<UnstableFeature>;
134141
}
135142

136143
interface StackGroup {
@@ -139,6 +146,12 @@ interface StackGroup {
139146
deployedStacks: CloudFormationStack[];
140147
}
141148

149+
/**
150+
* Names of toolkit features that are still under development, and may change in
151+
* the future.
152+
*/
153+
export type UnstableFeature = 'refactor';
154+
142155
/**
143156
* The AWS CDK Programmatic Toolkit
144157
*/
@@ -165,6 +178,8 @@ export class Toolkit extends CloudAssemblySourceBuilder {
165178

166179
private baseCredentials: IBaseCredentialsProvider;
167180

181+
private readonly unstableFeatures: Array<UnstableFeature>;
182+
168183
public constructor(private readonly props: ToolkitOptions = {}) {
169184
super();
170185
this.toolkitStackName = props.toolkitStackName ?? DEFAULT_TOOLKIT_STACK_NAME;
@@ -183,6 +198,7 @@ export class Toolkit extends CloudAssemblySourceBuilder {
183198
this.ioHost = withTrimmedWhitespace(ioHost);
184199

185200
this.baseCredentials = props.sdkConfig?.baseCredentials ?? BaseCredentials.awsCliCompatible();
201+
this.unstableFeatures = props.unstableFeatures ?? [];
186202
}
187203

188204
/**
@@ -1051,6 +1067,8 @@ export class Toolkit extends CloudAssemblySourceBuilder {
10511067
* Refactor Action. Moves resources from one location (stack + logical ID) to another.
10521068
*/
10531069
public async refactor(cx: ICloudAssemblySource, options: RefactorOptions = {}): Promise<void> {
1070+
this.requireUnstableFeature('refactor');
1071+
10541072
const ioHelper = asIoHelper(this.ioHost, 'refactor');
10551073
const assembly = await assemblyFromSource(ioHelper, cx);
10561074
return this._refactor(assembly, ioHelper, options);
@@ -1284,6 +1302,12 @@ export class Toolkit extends CloudAssemblySourceBuilder {
12841302
// just continue - deploy will show the error
12851303
}
12861304
}
1305+
1306+
private requireUnstableFeature(requestedFeature: UnstableFeature) {
1307+
if (!this.unstableFeatures.includes(requestedFeature)) {
1308+
throw new ToolkitError(`Unstable feature '${requestedFeature}' is not enabled. Please enable it under 'unstableFeatures'`);
1309+
}
1310+
}
12871311
}
12881312

12891313
/**

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { mockCloudFormationClient, MockSdk } from '../_helpers/mock-sdk';
88
jest.setTimeout(10_000);
99

1010
const ioHost = new TestIoHost();
11-
const toolkit = new Toolkit({ ioHost });
11+
const toolkit = new Toolkit({ ioHost, unstableFeatures: ['refactor'] });
1212

1313
jest.spyOn(SdkProvider.prototype, '_makeSdk').mockReturnValue(new MockSdk());
1414

@@ -18,6 +18,19 @@ beforeEach(() => {
1818
mockCloudFormationClient.reset();
1919
});
2020

21+
test('requires acknowledgment that the feature is unstable', async () => {
22+
// GIVEN
23+
const tk = new Toolkit({ ioHost /* unstable not acknowledged */ });
24+
const cx = await builderFixture(tk, 'stack-with-bucket');
25+
26+
// WHEN
27+
await expect(
28+
tk.refactor(cx, {
29+
dryRun: true,
30+
}),
31+
).rejects.toThrow("Unstable feature 'refactor' is not enabled. Please enable it under 'unstableFeatures'");
32+
});
33+
2134
test('detects the same resource in different locations', async () => {
2235
// GIVEN
2336
mockCloudFormationClient.on(ListStacksCommand).resolves({

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ export class CdkToolkit {
179179
emojis: true,
180180
ioHost: this.ioHost,
181181
toolkitStackName: this.toolkitStackName,
182+
unstableFeatures: ['refactor'],
182183
});
183184
}
184185

0 commit comments

Comments
 (0)