Skip to content

Commit

Permalink
chore(core): migrate cfn-utils handler (#27904)
Browse files Browse the repository at this point in the history
This PR moves the cfn-utils handler from aws-cdk-lib to our new centralized location for custom resource handlers in the [@aws-cdk](https://github.com/aws-cdk) package.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
colifran authored and Mike Wrighton committed Nov 13, 2023
1 parent f5cb671 commit 05fc182
Show file tree
Hide file tree
Showing 17 changed files with 46 additions and 82 deletions.

This file was deleted.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"S3Bucket": {
"Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}"
},
"S3Key": "3e61d858eaa7724170872a455b8f788d5fcf18adba89aadbe603a52dab59d917.zip"
"S3Key": "f58e5b0009fc349d5e78d41594373bd6723e705a3c961a42adfbefa36cbe1bcf.zip"
},
"Timeout": 900,
"MemorySize": 128,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
import { CfnUtilsResourceType } from './consts';
/**
* Supported resource type.
*/
export enum CfnUtilsResourceType {
/**
* CfnJson
*/
CFN_JSON = 'Custom::AWSCDKCfnJson',

/**
* CfnJsonStringify
*/
CFN_JSON_STRINGIFY = 'Custom::AWSCDKCfnJsonStringify',
}

/**
* Parses the value of "Value" and reflects it back as attribute.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { CfnUtilsResourceType } from '../../lib/private/cfn-utils-provider/consts';
import { handler } from '../../lib/private/cfn-utils-provider/index';
import { handler, CfnUtilsResourceType } from '../../lib/core/cfn-utils-provider/index';

test('parses value as JSON', async () => {
// GIVEN
Expand Down Expand Up @@ -67,6 +66,17 @@ test('fails if wrong resource type', async () => {
await expect(() => invokeHandler(event)).rejects.toThrow(/unexpected resource type "Create"/);
});

test('resource provider simply parses json and reflects back as an attribute', async () => {
const input = { foo: 1234 };
const response = await handler({
ResourceType: CfnUtilsResourceType.CFN_JSON,
ResourceProperties: {
Value: JSON.stringify(input),
},
} as any);
expect(input).toEqual(response.Data.Value);
});

// helper function to get around TypeScript expecting a complete event object,
// even though our tests only need some of the fields
async function invokeHandler(event: Partial<AWSLambda.CloudFormationCustomResourceEvent>) {
Expand Down
3 changes: 2 additions & 1 deletion packages/aws-cdk-lib/core/lib/private/cfn-utils-provider.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as path from 'path';
import { Construct } from 'constructs';
import { CfnUtilsResourceType } from './cfn-utils-provider/consts';
import { CustomResource } from '../custom-resource';
Expand All @@ -10,7 +11,7 @@ export class CfnUtilsProvider extends Construct {
public static getOrCreate(scope: Construct) {
return CustomResourceProvider.getOrCreate(scope, 'AWSCDKCfnUtilsProvider', {
runtime: CustomResourceProviderRuntime.NODEJS_18_X,
codeDirectory: `${__dirname}/cfn-utils-provider`,
codeDirectory: path.join(__dirname, '..', '..', '..', 'custom-resource-handlers', 'dist', 'core', 'cfn-utils-provider'),
});
}
}
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Shared definition with packages/@aws-cdk/custom-resource-handlers/lib/core/cfn-utils-provider/index.ts
/**
* Supported resource type.
*/
Expand Down
13 changes: 0 additions & 13 deletions packages/aws-cdk-lib/core/test/cfn-json.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { App, CfnResource, Lazy, Stack } from '../lib';
import { CfnJson } from '../lib/cfn-json';
import { CfnUtilsResourceType } from '../lib/private/cfn-utils-provider/consts';
import { handler } from '../lib/private/cfn-utils-provider/index';

describe('cfn json', () => {

Expand Down Expand Up @@ -71,15 +69,4 @@ describe('cfn json', () => {
'Fn::Join': ['', ['"{"ref=', { Ref: 'MyResource' }, '":"this is a I am lazy"}"']],
});
});

test('resource provider simply parses json and reflects back as an attribute', async () => {
const input = { foo: 1234 };
const response = await handler({
ResourceType: CfnUtilsResourceType.CFN_JSON,
ResourceProperties: {
Value: JSON.stringify(input),
},
} as any);
expect(input).toEqual(response.Data.Value);
});
});

0 comments on commit 05fc182

Please sign in to comment.