Skip to content

Commit

Permalink
feat(core): add support for the ref intrinsic function (#5468) (#5470)
Browse files Browse the repository at this point in the history
  • Loading branch information
flyingImer authored and mergify[bot] committed Dec 18, 2019
1 parent 16a1200 commit cad5bc1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
24 changes: 24 additions & 0 deletions packages/@aws-cdk/core/lib/cfn-fn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions packages/@aws-cdk/core/test/test.fn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] {
Expand Down

0 comments on commit cad5bc1

Please sign in to comment.