generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 74
refactor(cli): new internal cloudformation api module #270
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
Merged
aws-cdk-automation
merged 1 commit into
main
from
mrgrain/refactor/cloudformation-module
Mar 21, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
packages/@aws-cdk/tmp-toolkit-helpers/src/api/resource-metadata/index.ts
This file contains hidden or 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 @@ | ||
| export * from './resource-metadata'; |
This file contains hidden or 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 hidden or 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 hidden or 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,4 @@ | ||
| export * from './evaluate-cloudformation-template'; | ||
| export * from './template-body-parameter'; | ||
| export * from './nested-stack-helpers'; | ||
| export * from './stack-helpers'; |
6 changes: 3 additions & 3 deletions
6
...b/api/deployments/nested-stack-helpers.ts → ...pi/cloudformation/nested-stack-helpers.ts
This file contains hidden or 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
189 changes: 189 additions & 0 deletions
189
packages/aws-cdk/lib/api/cloudformation/stack-helpers.ts
This file contains hidden or 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,189 @@ | ||
| import type { Stack, Tag } from '@aws-sdk/client-cloudformation'; | ||
| import { ToolkitError } from '../../../../@aws-cdk/tmp-toolkit-helpers/src/api'; | ||
| import { formatErrorMessage, deserializeStructure } from '../../util'; | ||
| import type { ICloudFormationClient } from '../aws-auth'; | ||
| import { StackStatus } from '../stack-events'; | ||
|
|
||
| export interface Template { | ||
| Parameters?: Record<string, TemplateParameter>; | ||
| [section: string]: any; | ||
| } | ||
|
|
||
| export interface TemplateParameter { | ||
| Type: string; | ||
| Default?: any; | ||
| Description?: string; | ||
| [key: string]: any; | ||
| } | ||
|
|
||
| /** | ||
| * Represents an (existing) Stack in CloudFormation | ||
| * | ||
| * Bundle and cache some information that we need during deployment (so we don't have to make | ||
| * repeated calls to CloudFormation). | ||
| */ | ||
| export class CloudFormationStack { | ||
| public static async lookup( | ||
| cfn: ICloudFormationClient, | ||
| stackName: string, | ||
| retrieveProcessedTemplate: boolean = false, | ||
| ): Promise<CloudFormationStack> { | ||
| try { | ||
| const response = await cfn.describeStacks({ StackName: stackName }); | ||
| return new CloudFormationStack(cfn, stackName, response.Stacks && response.Stacks[0], retrieveProcessedTemplate); | ||
| } catch (e: any) { | ||
| if (e.name === 'ValidationError' && formatErrorMessage(e) === `Stack with id ${stackName} does not exist`) { | ||
| return new CloudFormationStack(cfn, stackName, undefined); | ||
| } | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Return a copy of the given stack that does not exist | ||
| * | ||
| * It's a little silly that it needs arguments to do that, but there we go. | ||
| */ | ||
| public static doesNotExist(cfn: ICloudFormationClient, stackName: string) { | ||
| return new CloudFormationStack(cfn, stackName); | ||
| } | ||
|
|
||
| /** | ||
| * From static information (for testing) | ||
| */ | ||
| public static fromStaticInformation(cfn: ICloudFormationClient, stackName: string, stack: Stack) { | ||
| return new CloudFormationStack(cfn, stackName, stack); | ||
| } | ||
|
|
||
| private _template: any; | ||
|
|
||
| protected constructor( | ||
| private readonly cfn: ICloudFormationClient, | ||
| public readonly stackName: string, | ||
| private readonly stack?: Stack, | ||
| private readonly retrieveProcessedTemplate: boolean = false, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * Retrieve the stack's deployed template | ||
| * | ||
| * Cached, so will only be retrieved once. Will return an empty | ||
| * structure if the stack does not exist. | ||
| */ | ||
| public async template(): Promise<Template> { | ||
| if (!this.exists) { | ||
| return {}; | ||
| } | ||
|
|
||
| if (this._template === undefined) { | ||
| const response = await this.cfn.getTemplate({ | ||
| StackName: this.stackName, | ||
| TemplateStage: this.retrieveProcessedTemplate ? 'Processed' : 'Original', | ||
| }); | ||
| this._template = (response.TemplateBody && deserializeStructure(response.TemplateBody)) || {}; | ||
| } | ||
| return this._template; | ||
| } | ||
|
|
||
| /** | ||
| * Whether the stack exists | ||
| */ | ||
| public get exists() { | ||
| return this.stack !== undefined; | ||
| } | ||
|
|
||
| /** | ||
| * The stack's ID | ||
| * | ||
| * Throws if the stack doesn't exist. | ||
| */ | ||
| public get stackId() { | ||
| this.assertExists(); | ||
| return this.stack!.StackId!; | ||
| } | ||
|
|
||
| /** | ||
| * The stack's current outputs | ||
| * | ||
| * Empty object if the stack doesn't exist | ||
| */ | ||
| public get outputs(): Record<string, string> { | ||
| if (!this.exists) { | ||
| return {}; | ||
| } | ||
| const result: { [name: string]: string } = {}; | ||
| (this.stack!.Outputs || []).forEach((output) => { | ||
| result[output.OutputKey!] = output.OutputValue!; | ||
| }); | ||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * The stack's status | ||
| * | ||
| * Special status NOT_FOUND if the stack does not exist. | ||
| */ | ||
| public get stackStatus(): StackStatus { | ||
| if (!this.exists) { | ||
| return new StackStatus('NOT_FOUND', 'Stack not found during lookup'); | ||
| } | ||
| return StackStatus.fromStackDescription(this.stack!); | ||
| } | ||
|
|
||
| /** | ||
| * The stack's current tags | ||
| * | ||
| * Empty list if the stack does not exist | ||
| */ | ||
| public get tags(): Tag[] { | ||
| return this.stack?.Tags || []; | ||
| } | ||
|
|
||
| /** | ||
| * SNS Topic ARNs that will receive stack events. | ||
| * | ||
| * Empty list if the stack does not exist | ||
| */ | ||
| public get notificationArns(): string[] { | ||
| return this.stack?.NotificationARNs ?? []; | ||
| } | ||
|
|
||
| /** | ||
| * Return the names of all current parameters to the stack | ||
| * | ||
| * Empty list if the stack does not exist. | ||
| */ | ||
| public get parameterNames(): string[] { | ||
| return Object.keys(this.parameters); | ||
| } | ||
|
|
||
| /** | ||
| * Return the names and values of all current parameters to the stack | ||
| * | ||
| * Empty object if the stack does not exist. | ||
| */ | ||
| public get parameters(): Record<string, string> { | ||
| if (!this.exists) { | ||
| return {}; | ||
| } | ||
| const ret: Record<string, string> = {}; | ||
| for (const param of this.stack!.Parameters ?? []) { | ||
| ret[param.ParameterKey!] = param.ResolvedValue ?? param.ParameterValue!; | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| /** | ||
| * Return the termination protection of the stack | ||
| */ | ||
| public get terminationProtection(): boolean | undefined { | ||
| return this.stack?.EnableTerminationProtection; | ||
| } | ||
|
|
||
| private assertExists() { | ||
| if (!this.exists) { | ||
| throw new ToolkitError(`No stack named '${this.stackName}'`); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a collection of helpers that were previously in
api/deploymentsbut in reality were used across multiple modules that don't have anything to do with deployments.