forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(s3-deployment): deploy data with deploy-time values (aws#18659)
Allow deploying test-based content that can potentially include deploy-time values such as attributes of cloud resources. Introduce a `Source.data(objectKey, text)` and `Source.jsonData(objectKey, obj)` where the data can naturally include deploy-time tokens such as references to resources (`Ref`) or to resource attributes (`Fn::GetAtt`). For example: ```ts const appConfig = { topic_arn: topic.topicArn, base_url: 'https://my-endpoint', }; new s3deploy.BucketDeployment(this, 'BucketDeployment', { sources: [s3deploy.Source.jsonData('config.json', config)], destinationBucket: destinationBucket, }); ``` This is implemented by replacing the deploy-time tokens with markers that are replaced inside the s3-deployment custom resource. Fixes aws#12903 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
17 changed files
with
1,132 additions
and
81 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { Stack } from '@aws-cdk/core'; | ||
|
||
// keep this import separate from other imports to reduce chance for merge conflicts with v2-main | ||
// eslint-disable-next-line no-duplicate-imports, import/order | ||
import { Construct } from '@aws-cdk/core'; | ||
|
||
export interface Content { | ||
readonly text: string; | ||
readonly markers: Record<string, any>; | ||
} | ||
|
||
/** | ||
* Renders the given string data as deployable content with markers substituted | ||
* for all "Ref" and "Fn::GetAtt" objects. | ||
* | ||
* @param scope Construct scope | ||
* @param data The input data | ||
* @returns The markered text (`text`) and a map that maps marker names to their | ||
* values (`markers`). | ||
*/ | ||
export function renderData(scope: Construct, data: string): Content { | ||
const obj = Stack.of(scope).resolve(data); | ||
if (typeof(obj) === 'string') { | ||
return { text: obj, markers: {} }; | ||
} | ||
|
||
if (typeof(obj) !== 'object') { | ||
throw new Error(`Unexpected: after resolve() data must either be a string or a CloudFormation intrinsic. Got: ${JSON.stringify(obj)}`); | ||
} | ||
|
||
let markerIndex = 0; | ||
const markers: Record<string, FnJoinPart> = {}; | ||
const result = new Array<string>(); | ||
const fnJoin: FnJoin | undefined = obj['Fn::Join']; | ||
|
||
if (fnJoin) { | ||
const sep = fnJoin[0]; | ||
const parts = fnJoin[1]; | ||
|
||
if (sep !== '') { | ||
throw new Error(`Unexpected "Fn::Join", expecting separator to be an empty string but got "${sep}"`); | ||
} | ||
|
||
for (const part of parts) { | ||
if (typeof (part) === 'string') { | ||
result.push(part); | ||
continue; | ||
} | ||
|
||
if (typeof (part) === 'object') { | ||
addMarker(part); | ||
continue; | ||
} | ||
|
||
throw new Error(`Unexpected "Fn::Join" part, expecting string or object but got ${typeof (part)}`); | ||
} | ||
|
||
} else if (obj.Ref || obj['Fn::GetAtt']) { | ||
addMarker(obj); | ||
} else { | ||
throw new Error('Unexpected: Expecting `resolve()` to return "Fn::Join", "Ref" or "Fn::GetAtt"'); | ||
} | ||
|
||
function addMarker(part: Ref | GetAtt) { | ||
const keys = Object.keys(part); | ||
if (keys.length !== 1 || (keys[0] != 'Ref' && keys[0] != 'Fn::GetAtt')) { | ||
throw new Error(`Invalid CloudFormation reference. "Ref" or "Fn::GetAtt". Got ${JSON.stringify(part)}`); | ||
} | ||
|
||
const marker = `<<marker:0xbaba:${markerIndex++}>>`; | ||
result.push(marker); | ||
markers[marker] = part; | ||
} | ||
|
||
return { text: result.join(''), markers }; | ||
} | ||
|
||
type FnJoin = [string, FnJoinPart[]]; | ||
type FnJoinPart = string | Ref | GetAtt; | ||
type Ref = { Ref: string }; | ||
type GetAtt = { 'Fn::GetAtt': [string, string] }; |
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
Oops, something went wrong.