From 51ce6e1e5a096be291eca08c718e02b9e7c58fdf Mon Sep 17 00:00:00 2001 From: yijiwang Date: Tue, 17 Dec 2019 18:41:48 -0800 Subject: [PATCH] feat(core): add support for the ref intrinsic function (#5468) --- packages/@aws-cdk/core/lib/cfn-fn.ts | 24 ++++++++++++++++++++++++ packages/@aws-cdk/core/test/test.fn.ts | 9 +++++++++ 2 files changed, 33 insertions(+) diff --git a/packages/@aws-cdk/core/lib/cfn-fn.ts b/packages/@aws-cdk/core/lib/cfn-fn.ts index 1f775abee6671..31619d551424e 100644 --- a/packages/@aws-cdk/core/lib/cfn-fn.ts +++ b/packages/@aws-cdk/core/lib/cfn-fn.ts @@ -13,6 +13,15 @@ import { Token } from './token'; * http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference.html */ export class Fn { + /** + * The ``Ref`` intrinsic function returns the value of the specified parameter or resource. + * Note that it doesn't validate the logicalName, it mainly serves paremeter/resource reference defined in a ``CfnInclude`` template. + * @param logicalName The logical name of a parameter/resource for which you want to retrieve its value. + */ + public static ref(logicalName: string): string { + return new FnRef(logicalName).toString(); + } + /** * The ``Fn::GetAtt`` intrinsic function returns the value of an attribute * from a resource in the template. @@ -311,6 +320,21 @@ class FnBase extends Intrinsic { } } +/** + * The intrinsic function ``Ref`` returns the value of the specified parameter or resource. + * When you specify a parameter's logical name, it returns the value of the parameter. + * When you specify a resource's logical name, it returns a value that you can typically use to refer to that resource, such as a physical ID. + */ +class FnRef extends FnBase { + /** + * Creates an ``Ref`` function. + * @param logicalName The logical name of a parameter/resource for which you want to retrieve its value. + */ + constructor(logicalName: string) { + super('Ref', logicalName); + } +} + /** * The intrinsic function ``Fn::FindInMap`` returns the value corresponding to keys in a two-level * map that is declared in the Mappings section. diff --git a/packages/@aws-cdk/core/test/test.fn.ts b/packages/@aws-cdk/core/test/test.fn.ts index 134775b13af2d..7a5b05e08524f 100644 --- a/packages/@aws-cdk/core/test/test.fn.ts +++ b/packages/@aws-cdk/core/test/test.fn.ts @@ -168,6 +168,15 @@ export = nodeunit.testCase({ }); }), }, + 'Ref': { + 'returns a reference given a logical name'(test: nodeunit.Test) { + const stack = new Stack(); + test.deepEqual(stack.resolve(Fn.ref('hello')), { + Ref: 'hello' + }); + test.done(); + } + } }); function stringListToken(o: any): string[] {