|
| 1 | +import type { GuStack } from '@guardian/cdk/lib/constructs/core'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Adjusts CloudFormation parameters in a given stack (if they exist). |
| 5 | + * |
| 6 | + * @param stack - The stack to adjust. |
| 7 | + * @param currentDefaultValue - The current default value to look for. Can be a prefix, or a full value. |
| 8 | + * @param desiredDefaultValue - The desired default value to replace with. Can be a prefix, or a full value. |
| 9 | + */ |
| 10 | +function adjustParameter( |
| 11 | + stack: GuStack, |
| 12 | + currentDefaultValue: string, |
| 13 | + desiredDefaultValue: string, |
| 14 | +) { |
| 15 | + const parameters = Object.values(stack.parameters) |
| 16 | + .filter((_) => !!_.default) |
| 17 | + .filter((_) => { |
| 18 | + const defaultValue = _.default as string; |
| 19 | + |
| 20 | + // Use `startsWith` to allow for partial matches (i.e. prefixes) |
| 21 | + return defaultValue.startsWith(currentDefaultValue); |
| 22 | + }); |
| 23 | + |
| 24 | + parameters.forEach((parameter) => { |
| 25 | + parameter.default = (parameter.default as string).replace( |
| 26 | + currentDefaultValue, |
| 27 | + desiredDefaultValue, |
| 28 | + ); |
| 29 | + parameter.allowedValues = [parameter.default]; |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Adjusts VPC-related CloudFormation parameters in the given stack to use the `notifications` VPC. |
| 35 | + * @see // https://github.com/guardian/aws-account-setup/blob/67a516b65e2e151d69687fa61a8a1aa914e8b7c0/packages/cdk/lib/__snapshots__/aws-account-setup.test.ts.snap#L27280-L27327 |
| 36 | + */ |
| 37 | +function adjustVpcParameters(stack: GuStack) { |
| 38 | + adjustParameter( |
| 39 | + stack, |
| 40 | + '/account/vpc/primary/', |
| 41 | + '/account/vpc/notifications/', |
| 42 | + ); |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Adjusts the artifact bucket parameter for n10n services. |
| 47 | + * In the Mobile account there are separate artifact buckets for different groups of applications, |
| 48 | + * so we can't use the account-wide default. |
| 49 | + */ |
| 50 | +function adjustArtifactBucketParameter(stack: GuStack) { |
| 51 | + adjustParameter( |
| 52 | + stack, |
| 53 | + '/account/services/artifact.bucket', |
| 54 | + '/account/services/artifact.bucket.n10n', |
| 55 | + ); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Applications within the `mobile-n10n` stack do not use account (/GuCDK) defaults. |
| 60 | + * This adjusts CloudFormation parameters with values suitable for the `mobile-n10n` stack. |
| 61 | + */ |
| 62 | +export function adjustCloudformationParameters(stack: GuStack) { |
| 63 | + adjustVpcParameters(stack); |
| 64 | + adjustArtifactBucketParameter(stack); |
| 65 | +} |
0 commit comments