-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): support
Fn::ImportValue
intrinsic function for hotswap d…
…eployments (#27292) ## Purpose 🎯 Extend the `EvaluateCloudFormationTemplate` class to support the `Fn::ImportValue` intrinsic function. This allows for more diverse templates to be evaluated for the purposes of determining eligibility for `--hotswap` deployments Closes #21320 ## Approach 🧠 Implement `LazyLookupExport` in similar fashion to `LazyListStackResources` to cache required CloudFormation API calls _(preference was to implement using a generator function instead so style is not entirely consistent, is this an issue?)_ Add some basic unit tests for `EvaluateCloudFormationTemplate.evaluateCfnExpression()` is they were absent, then add some tests for `Fn::ImportValue` ## Todo 📝 - [x] Update doco where appropriate - [x] Add to hotswap deployment tests - [x] Look for appropriate integration tests to update ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
1 parent
447b63c
commit a54ea0f
Showing
8 changed files
with
436 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
packages/aws-cdk/test/api/evaluate-cloudformation-template.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { | ||
CfnEvaluationException, | ||
EvaluateCloudFormationTemplate, | ||
Template, | ||
} from '../../lib/api/evaluate-cloudformation-template'; | ||
import { MockSdk } from '../util/mock-sdk'; | ||
|
||
const listStackResources = jest.fn(); | ||
const listExports: jest.Mock<AWS.CloudFormation.ListExportsOutput, AWS.CloudFormation.ListExportsInput[]> = jest.fn(); | ||
const sdk = new MockSdk(); | ||
sdk.stubCloudFormation({ | ||
listExports, | ||
listStackResources, | ||
}); | ||
|
||
const createEvaluateCloudFormationTemplate = (template: Template) => new EvaluateCloudFormationTemplate({ | ||
template, | ||
parameters: {}, | ||
account: '0123456789', | ||
region: 'ap-south-east-2', | ||
partition: 'aws', | ||
urlSuffix: (region) => sdk.getEndpointSuffix(region), | ||
sdk, | ||
stackName: 'test-stack', | ||
}); | ||
|
||
describe('evaluateCfnExpression', () => { | ||
describe('simple literal expressions', () => { | ||
const template: Template = {}; | ||
const evaluateCfnTemplate = createEvaluateCloudFormationTemplate(template); | ||
|
||
test('resolves Fn::Join correctly', async () => { | ||
// WHEN | ||
const result = await evaluateCfnTemplate.evaluateCfnExpression({ | ||
'Fn::Join': [':', ['a', 'b', 'c']], | ||
}); | ||
|
||
// THEN | ||
expect(result).toEqual('a:b:c'); | ||
}); | ||
|
||
test('resolves Fn::Split correctly', async () => { | ||
// WHEN | ||
const result = await evaluateCfnTemplate.evaluateCfnExpression({ 'Fn::Split': ['|', 'a|b|c'] }); | ||
|
||
// THEN | ||
expect(result).toEqual(['a', 'b', 'c']); | ||
}); | ||
|
||
test('resolves Fn::Select correctly', async () => { | ||
// WHEN | ||
const result = await evaluateCfnTemplate.evaluateCfnExpression({ 'Fn::Select': ['1', ['apples', 'grapes', 'oranges', 'mangoes']] }); | ||
|
||
// THEN | ||
expect(result).toEqual('grapes'); | ||
}); | ||
|
||
test('resolves Fn::Sub correctly', async () => { | ||
// WHEN | ||
const result = await evaluateCfnTemplate.evaluateCfnExpression({ 'Fn::Sub': ['Testing Fn::Sub Foo=${Foo} Bar=${Bar}', { Foo: 'testing', Bar: 1 }] }); | ||
|
||
// THEN | ||
expect(result).toEqual('Testing Fn::Sub Foo=testing Bar=1'); | ||
}); | ||
}); | ||
|
||
describe('resolving Fn::ImportValue', () => { | ||
const template: Template = {}; | ||
const evaluateCfnTemplate = createEvaluateCloudFormationTemplate(template); | ||
|
||
const createMockExport = (num: number) => ({ | ||
ExportingStackId: `test-exporting-stack-id-${num}`, | ||
Name: `test-name-${num}`, | ||
Value: `test-value-${num}`, | ||
}); | ||
|
||
beforeEach(async () => { | ||
listExports.mockReset(); | ||
listExports | ||
.mockReturnValueOnce({ | ||
Exports: [ | ||
createMockExport(1), | ||
createMockExport(2), | ||
createMockExport(3), | ||
], | ||
NextToken: 'next-token-1', | ||
}) | ||
.mockReturnValueOnce({ | ||
Exports: [ | ||
createMockExport(4), | ||
createMockExport(5), | ||
createMockExport(6), | ||
], | ||
NextToken: undefined, | ||
}); | ||
}); | ||
|
||
test('resolves Fn::ImportValue using lookup', async () => { | ||
const result = await evaluateCfnTemplate.evaluateCfnExpression({ 'Fn::ImportValue': 'test-name-5' }); | ||
expect(result).toEqual('test-value-5'); | ||
}); | ||
|
||
test('throws error when Fn::ImportValue cannot be resolved', async () => { | ||
const evaluate = () => evaluateCfnTemplate.evaluateCfnExpression({ | ||
'Fn::ImportValue': 'blah', | ||
}); | ||
await expect(evaluate).rejects.toBeInstanceOf(CfnEvaluationException); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.