|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import * as CloudFormation from '@aws-sdk/client-cloudformation' |
| 7 | +import { AsyncCollection } from '../utilities/asyncCollection' |
| 8 | +import { hasProps, isNonNullable, RequiredProps } from '../utilities/tsUtils' |
| 9 | +import { ClientWrapper } from './clientWrapper' |
| 10 | + |
| 11 | +export interface StackSummary |
| 12 | + extends RequiredProps<CloudFormation.StackSummary, 'StackName' | 'CreationTime' | 'StackStatus'> { |
| 13 | + DriftInformation: RequiredProps<CloudFormation.StackDriftInformation, 'StackDriftStatus'> |
| 14 | +} |
| 15 | + |
| 16 | +export type StackResource = RequiredProps<CloudFormation.StackResource, 'ResourceType'> |
| 17 | + |
| 18 | +export interface DescribeStackResourcesOutput extends CloudFormation.DescribeStackResourcesOutput { |
| 19 | + StackResources: StackResource[] |
| 20 | +} |
| 21 | +export class CloudFormationClient extends ClientWrapper<CloudFormation.CloudFormationClient> { |
| 22 | + public constructor(regionCode: string) { |
| 23 | + super(regionCode, CloudFormation.CloudFormationClient) |
| 24 | + } |
| 25 | + |
| 26 | + public async deleteStack(name: string): Promise<CloudFormation.DeleteStackCommandOutput> { |
| 27 | + return await this.makeRequest(CloudFormation.DeleteStackCommand, { StackName: name }) |
| 28 | + } |
| 29 | + |
| 30 | + public async describeType(typeName: string): Promise<CloudFormation.DescribeTypeOutput> { |
| 31 | + return await this.makeRequest(CloudFormation.DescribeTypeCommand, { TypeName: typeName }) |
| 32 | + } |
| 33 | + |
| 34 | + public async *listStacks( |
| 35 | + statusFilter: CloudFormation.StackStatus[] = ['CREATE_COMPLETE', 'UPDATE_COMPLETE'] |
| 36 | + ): AsyncIterableIterator<StackSummary> { |
| 37 | + const request: CloudFormation.ListStacksInput = { |
| 38 | + StackStatusFilter: statusFilter, |
| 39 | + } |
| 40 | + |
| 41 | + do { |
| 42 | + const response: CloudFormation.ListStacksOutput = await this.makeRequest( |
| 43 | + CloudFormation.ListStacksCommand, |
| 44 | + request |
| 45 | + ) |
| 46 | + |
| 47 | + const filteredResponse = response.StackSummaries?.filter(isStackSummary) |
| 48 | + if (filteredResponse && filteredResponse.length > 0) { |
| 49 | + yield* filteredResponse |
| 50 | + } |
| 51 | + |
| 52 | + request.NextToken = response.NextToken |
| 53 | + } while (request.NextToken) |
| 54 | + } |
| 55 | + |
| 56 | + public listAllStacks(request: CloudFormation.ListStacksInput = {}): AsyncCollection<StackSummary[]> { |
| 57 | + return this.makePaginatedRequest(CloudFormation.paginateListStacks, request, (page) => page.StackSummaries).map( |
| 58 | + (s) => s.filter(isStackSummary) |
| 59 | + ) |
| 60 | + } |
| 61 | + |
| 62 | + public async *listTypes(): AsyncIterableIterator<CloudFormation.TypeSummary> { |
| 63 | + const request: CloudFormation.ListTypesInput = { |
| 64 | + DeprecatedStatus: 'LIVE', |
| 65 | + Type: 'RESOURCE', |
| 66 | + Visibility: 'PUBLIC', |
| 67 | + } |
| 68 | + |
| 69 | + do { |
| 70 | + const response: CloudFormation.ListTypesOutput = await this.makeRequest( |
| 71 | + CloudFormation.ListTypesCommand, |
| 72 | + request |
| 73 | + ) |
| 74 | + |
| 75 | + if (response.TypeSummaries) { |
| 76 | + yield* response.TypeSummaries |
| 77 | + } |
| 78 | + |
| 79 | + request.NextToken = response.NextToken |
| 80 | + } while (request.NextToken) |
| 81 | + } |
| 82 | + |
| 83 | + public async describeStackResources(name: string): Promise<DescribeStackResourcesOutput> { |
| 84 | + return await this.makeRequest(CloudFormation.DescribeStackResourcesCommand, { StackName: name }) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +function isStackSummary(s: CloudFormation.StackSummary | undefined): s is StackSummary { |
| 89 | + return ( |
| 90 | + isNonNullable(s) && |
| 91 | + hasProps(s, 'StackName', 'CreationTime', 'StackStatus', 'DriftInformation') && |
| 92 | + hasProps(s.DriftInformation, 'StackDriftStatus') |
| 93 | + ) |
| 94 | +} |
0 commit comments