Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Fn.ref (#5468) #5470

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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