Skip to content

Commit e0570c3

Browse files
author
Tarun Belani
committed
Addressed review comments
1 parent 189bee7 commit e0570c3

12 files changed

+88
-35
lines changed

packages/@aws-cdk/aws-imagebuilder-alpha/lib/image-recipe.ts

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ const IMAGE_RECIPE_SYMBOL = Symbol.for('@aws-cdk/aws-imagebuilder-alpha.ImageRec
1717
*/
1818
const LATEST_VERSION = 'x.x.x';
1919

20+
/**
21+
* The default version to use in the image recipe. When the recipe is updated, the `x` will be incremented off from
22+
* the latest recipe version that exists.
23+
*
24+
* @see https://docs.aws.amazon.com/imagebuilder/latest/userguide/create-image-recipes.html
25+
*/
26+
const DEFAULT_RECIPE_VERSION = '1.0.x';
27+
2028
/**
2129
* An EC2 Image Builder Image Recipe.
2230
*/
@@ -48,7 +56,7 @@ export interface IImageRecipe extends IRecipeBase {
4856
*/
4957
export interface ImageRecipeProps {
5058
/**
51-
* The base image for customizations specified in the container recipe.
59+
* The base image for customizations specified in the image recipe.
5260
*/
5361
readonly baseImage: BaseImage;
5462

@@ -147,7 +155,7 @@ export interface ImageRecipeAttributes {
147155
/**
148156
* The version of the image recipe
149157
*
150-
* @default - derived from containerRecipeArn. if a containerRecipeName is provided, the latest version, x.x.x, will
158+
* @default - derived from imageRecipeArn. if a imageRecipeName is provided, the latest version, x.x.x, will
151159
* be used
152160
*/
153161
readonly imageRecipeVersion?: string;
@@ -237,17 +245,13 @@ export class ImageRecipe extends ImageRecipeBase {
237245
* underscores with hyphens.
238246
*/
239247
public static fromImageRecipeAttributes(scope: Construct, id: string, attrs: ImageRecipeAttributes): IImageRecipe {
240-
if (attrs.imageRecipeArn && (attrs.imageRecipeName || attrs.imageRecipeVersion)) {
248+
if (!attrs.imageRecipeArn && !attrs.imageRecipeName) {
241249
throw new cdk.ValidationError(
242-
'an imageRecipeName and imageRecipeVersion cannot be provided when an imageRecipeArn is provided',
250+
'either imageRecipeArn or imageRecipeName must be provided to import a image recipe',
243251
scope,
244252
);
245253
}
246254

247-
if (!attrs.imageRecipeArn && !attrs.imageRecipeName) {
248-
throw new cdk.ValidationError('either imageRecipeArn or imageRecipeName is required', scope);
249-
}
250-
251255
const imageRecipeArn =
252256
attrs.imageRecipeArn ??
253257
cdk.Stack.of(scope).formatArn({
@@ -321,20 +325,23 @@ export class ImageRecipe extends ImageRecipeBase {
321325

322326
this.validateImageRecipeName();
323327

324-
this.addBlockDevices(...(props.blockDevices ?? []));
328+
this.addBlockDevice(...(props.blockDevices ?? []));
325329

326330
const components: CfnImageRecipe.ComponentConfigurationProperty[] | undefined = props.components?.map(
327331
(component) => ({
328332
componentArn: component.component.componentArn,
329-
...(Object.keys(component.parameters ?? {}).length && {
330-
parameters: Object.entries(component.parameters!).map(
331-
([key, value]): CfnImageRecipe.ComponentParameterProperty => ({ name: key, value: value.value }),
333+
...(component.parameters && {
334+
parameters: Object.entries(component.parameters).map(
335+
([name, param]): CfnImageRecipe.ComponentParameterProperty => ({
336+
name,
337+
value: param.value,
338+
}),
332339
),
333340
}),
334341
}),
335342
);
336343

337-
const imageRecipeVersion = props.imageRecipeVersion ?? '1.0.x';
344+
const imageRecipeVersion = props.imageRecipeVersion ?? DEFAULT_RECIPE_VERSION;
338345
const imageRecipe = new CfnImageRecipe(this, 'Resource', {
339346
name: this.physicalName,
340347
version: imageRecipeVersion,
@@ -362,10 +369,17 @@ export class ImageRecipe extends ImageRecipeBase {
362369
*
363370
* @param blockDevices The list of block devices to attach
364371
*/
365-
public addBlockDevices(...blockDevices: ec2.BlockDevice[]): void {
372+
public addBlockDevice(...blockDevices: ec2.BlockDevice[]): void {
366373
this.blockDevices.push(...blockDevices);
367374
}
368375

376+
/**
377+
* Renders the input block devices, into the `BlockDeviceMapping[]` structure that CfnImageRecipe expects to receive.
378+
* This is rendered at synthesis time, as users can add additional block devices with `addBlockDevice`, after the
379+
* construct has been instantiated.
380+
*
381+
* @private
382+
*/
369383
private renderBlockDevices(): CfnImageRecipe.InstanceBlockDeviceMappingProperty[] | undefined {
370384
const blockDevices = this.blockDevices.map((blockDevice): CfnImageRecipe.InstanceBlockDeviceMappingProperty => {
371385
const ebsDevice = blockDevice.volume.ebsDevice;
@@ -380,18 +394,24 @@ export class ImageRecipe extends ImageRecipeBase {
380394
...(ebsDevice?.volumeType !== undefined && { volumeType: ebsDevice.volumeType }),
381395
};
382396

383-
const mappingEnabled = blockDevice.mappingEnabled ?? true;
384397
return {
385398
deviceName: blockDevice.deviceName,
386399
virtualName: blockDevice.volume.virtualName,
387-
...(!mappingEnabled && { noDevice: '' }),
400+
...(blockDevice.mappingEnabled === false && { noDevice: '' }),
388401
...(Object.keys(ebs).length && { ebs }),
389402
};
390403
});
391404

392405
return blockDevices.length ? blockDevices : undefined;
393406
}
394407

408+
/**
409+
* Generates the additional instance configuration property into the `AdditionalInstanceConfiguration` type in the
410+
* CloudFormation L1 definition.
411+
*
412+
* @param props The props passed as input to the construct
413+
* @private
414+
*/
395415
private buildAdditionalInstanceConfiguration(
396416
props: ImageRecipeProps,
397417
): CfnImageRecipe.AdditionalInstanceConfigurationProperty | undefined {

packages/@aws-cdk/aws-imagebuilder-alpha/test/image-recipe.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ describe('Image Recipe', () => {
181181
},
182182
});
183183

184-
imageRecipe.addBlockDevices(
184+
imageRecipe.addBlockDevice(
185185
{
186186
deviceName: '/dev/sda2',
187187
volume: ec2.BlockDeviceVolume.ebs(75, {
@@ -416,13 +416,13 @@ describe('Image Recipe', () => {
416416
});
417417
});
418418

419-
test('throws a validation error when an imageRecipeArn and imageRecipeName are provided when importing by attributes', () => {
419+
test('does not throw a validation error when an imageRecipeArn and imageRecipeName are provided when importing by attributes', () => {
420420
expect(() =>
421421
ImageRecipe.fromImageRecipeAttributes(stack, 'ImageRecipe', {
422422
imageRecipeArn: 'arn:aws:imagebuilder:us-east-1:123456789012:image-recipe/imported-image-recipe/x.x.x',
423423
imageRecipeName: 'imported-image-recipe',
424424
}),
425-
).toThrow(cdk.ValidationError);
425+
).not.toThrow(cdk.ValidationError);
426426
});
427427

428428
test('throws a validation error when neither an imageRecipeArn or imageRecipeName are provided when importing by attributes', () => {

packages/@aws-cdk/aws-imagebuilder-alpha/test/integ.all-parameters.image-recipe.js.snapshot/aws-cdk-imagebuilder-image-recipe-all-parameters.assets.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-imagebuilder-alpha/test/integ.all-parameters.image-recipe.js.snapshot/aws-cdk-imagebuilder-image-recipe-all-parameters.template.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@
116116
}
117117
}
118118
},
119+
"Outputs": {
120+
"ImageRecipeVersion": {
121+
"Value": {
122+
"Fn::GetAtt": [
123+
"ImageRecipe8C789631",
124+
"Version"
125+
]
126+
}
127+
}
128+
},
119129
"Parameters": {
120130
"BootstrapVersion": {
121131
"Type": "AWS::SSM::Parameter::Value<String>",

packages/@aws-cdk/aws-imagebuilder-alpha/test/integ.all-parameters.image-recipe.js.snapshot/manifest.json

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-imagebuilder-alpha/test/integ.all-parameters.image-recipe.js.snapshot/tree.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-imagebuilder-alpha/test/integ.all-parameters.image-recipe.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ const imageRecipe = new imagebuilder.ImageRecipe(stack, 'ImageRecipe', {
8484
},
8585
});
8686

87-
imageRecipe.addBlockDevices({
87+
imageRecipe.addBlockDevice({
8888
deviceName: '/dev/sda2',
8989
volume: ec2.BlockDeviceVolume.ebs(75, {
9090
encrypted: true,
@@ -96,6 +96,8 @@ imageRecipe.addBlockDevices({
9696
}),
9797
});
9898

99+
new cdk.CfnOutput(stack, 'ImageRecipeVersion', { value: imageRecipe.imageRecipeVersion });
100+
99101
new integ.IntegTest(app, 'ImageRecipeTest', {
100102
testCases: [stack],
101103
});

0 commit comments

Comments
 (0)