@@ -5,59 +5,38 @@ import { ISDK } from './aws-auth';
5
5
import { LazyListStackResources , ListStackResources } from './evaluate-cloudformation-template' ;
6
6
import { CloudFormationStack , Template } from './util/cloudformation' ;
7
7
8
- export interface TemplateWithNestedStackNames {
8
+ export interface NestedStackTemplates {
9
+ readonly physicalName : string | undefined ;
9
10
readonly deployedTemplate : Template ;
10
- readonly nestedStackNames : { [ nestedStackLogicalId : string ] : NestedStackNames } ;
11
+ readonly generatedTemplate : Template ;
12
+ readonly nestedStackTemplates : { [ nestedStackLogicalId : string ] : NestedStackTemplates } ;
11
13
}
12
14
13
- export interface NestedStackNames {
14
- readonly nestedStackPhysicalName : string | undefined ;
15
- readonly nestedChildStackNames : { [ logicalId : string ] : NestedStackNames } ;
16
- }
17
-
18
- export interface TemplateWithNestedStackCount {
19
- readonly deployedTemplate : Template ;
20
- readonly nestedStackCount : number ;
15
+ export interface RootTemplateWithNestedStacks {
16
+ readonly deployedRootTemplate : Template ;
17
+ readonly nestedStacks : { [ nestedStackLogicalId : string ] : NestedStackTemplates } ;
21
18
}
22
19
23
20
/**
24
- * Reads the currently deployed template from CloudFormation and adds a
25
- * property, `NestedTemplate`, to any nested stacks that appear in either
26
- * the deployed template or the newly synthesized template. `NestedTemplate`
27
- * is populated with contents of the nested template by mutating the
28
- * `template` property of `rootStackArtifact`. This is done for all
29
- * nested stack resources to arbitrary depths.
21
+ * Reads the currently deployed template and all of its nested stack templates from CloudFormation.
30
22
*/
31
23
export async function loadCurrentTemplateWithNestedStacks (
32
24
rootStackArtifact : cxapi . CloudFormationStackArtifact , sdk : ISDK ,
33
25
retrieveProcessedTemplate : boolean = false ,
34
- ) : Promise < TemplateWithNestedStackNames > {
35
- const deployedTemplate = await loadCurrentTemplate ( rootStackArtifact , sdk , retrieveProcessedTemplate ) ;
36
- const nestedStackNames = await addNestedTemplatesToGeneratedAndDeployedStacks ( rootStackArtifact , sdk , {
26
+ ) : Promise < RootTemplateWithNestedStacks > {
27
+ const deployedRootTemplate = await loadCurrentTemplate ( rootStackArtifact , sdk , retrieveProcessedTemplate ) ;
28
+ const nestedStacks = await loadNestedStacks ( rootStackArtifact , sdk , {
37
29
generatedTemplate : rootStackArtifact . template ,
38
- deployedTemplate : deployedTemplate ,
30
+ deployedTemplate : deployedRootTemplate ,
39
31
deployedStackName : rootStackArtifact . stackName ,
40
32
} ) ;
41
33
42
34
return {
43
- deployedTemplate ,
44
- nestedStackNames ,
35
+ deployedRootTemplate ,
36
+ nestedStacks ,
45
37
} ;
46
38
}
47
39
48
- export function flattenNestedStackNames ( nestedStackNames : { [ nestedStackLogicalId : string ] : NestedStackNames } ) : string [ ] {
49
- const nameList = [ ] ;
50
- for ( const key of Object . keys ( nestedStackNames ) ) {
51
- nameList . push ( key ) ;
52
-
53
- if ( Object . keys ( nestedStackNames [ key ] . nestedChildStackNames ) . length !== 0 ) {
54
- flattenNestedStacksHelper ( nestedStackNames [ key ] . nestedChildStackNames , nameList ) ;
55
- }
56
- }
57
-
58
- return nameList ;
59
- }
60
-
61
40
/**
62
41
* Returns the currently deployed template from CloudFormation that corresponds to `stackArtifact`.
63
42
*/
@@ -76,13 +55,13 @@ async function loadCurrentStackTemplate(
76
55
return stack . template ( ) ;
77
56
}
78
57
79
- async function addNestedTemplatesToGeneratedAndDeployedStacks (
58
+ async function loadNestedStacks (
80
59
rootStackArtifact : cxapi . CloudFormationStackArtifact ,
81
60
sdk : ISDK ,
82
61
parentTemplates : StackTemplates ,
83
- ) : Promise < { [ nestedStackLogicalId : string ] : NestedStackNames } > {
62
+ ) : Promise < { [ nestedStackLogicalId : string ] : NestedStackTemplates } > {
84
63
const listStackResources = parentTemplates . deployedStackName ? new LazyListStackResources ( sdk , parentTemplates . deployedStackName ) : undefined ;
85
- const nestedStackNames : { [ nestedStackLogicalId : string ] : NestedStackNames } = { } ;
64
+ const nestedStacks : { [ nestedStackLogicalId : string ] : NestedStackTemplates } = { } ;
86
65
for ( const [ nestedStackLogicalId , generatedNestedStackResource ] of Object . entries ( parentTemplates . generatedTemplate . Resources ?? { } ) ) {
87
66
if ( ! isCdkManagedNestedStack ( generatedNestedStackResource ) ) {
88
67
continue ;
@@ -91,27 +70,19 @@ async function addNestedTemplatesToGeneratedAndDeployedStacks(
91
70
const assetPath = generatedNestedStackResource . Metadata [ 'aws:asset:path' ] ;
92
71
const nestedStackTemplates = await getNestedStackTemplates ( rootStackArtifact , assetPath , nestedStackLogicalId , listStackResources , sdk ) ;
93
72
94
- generatedNestedStackResource . Properties . NestedTemplate = nestedStackTemplates . generatedTemplate ;
95
-
96
- const deployedParentTemplate = parentTemplates . deployedTemplate ;
97
- deployedParentTemplate . Resources = deployedParentTemplate . Resources ?? { } ;
98
- const deployedNestedStackResource = deployedParentTemplate . Resources [ nestedStackLogicalId ] ?? { } ;
99
- deployedParentTemplate . Resources [ nestedStackLogicalId ] = deployedNestedStackResource ;
100
- deployedNestedStackResource . Type = deployedNestedStackResource . Type ?? 'AWS::CloudFormation::Stack' ;
101
- deployedNestedStackResource . Properties = deployedNestedStackResource . Properties ?? { } ;
102
- deployedNestedStackResource . Properties . NestedTemplate = nestedStackTemplates . deployedTemplate ;
103
-
104
- nestedStackNames [ nestedStackLogicalId ] = {
105
- nestedStackPhysicalName : nestedStackTemplates . deployedStackName ,
106
- nestedChildStackNames : await addNestedTemplatesToGeneratedAndDeployedStacks (
73
+ nestedStacks [ nestedStackLogicalId ] = {
74
+ deployedTemplate : nestedStackTemplates . deployedTemplate ,
75
+ generatedTemplate : nestedStackTemplates . generatedTemplate ,
76
+ physicalName : nestedStackTemplates . deployedStackName ,
77
+ nestedStackTemplates : await loadNestedStacks (
107
78
rootStackArtifact ,
108
79
sdk ,
109
80
nestedStackTemplates ,
110
81
) ,
111
82
} ;
112
83
}
113
84
114
- return nestedStackNames ;
85
+ return nestedStacks ;
115
86
}
116
87
117
88
async function getNestedStackTemplates (
@@ -153,16 +124,6 @@ function isCdkManagedNestedStack(stackResource: any): stackResource is NestedSta
153
124
return stackResource . Type === 'AWS::CloudFormation::Stack' && stackResource . Metadata && stackResource . Metadata [ 'aws:asset:path' ] ;
154
125
}
155
126
156
- function flattenNestedStacksHelper ( nestedStackNames : { [ logicalId : string ] : NestedStackNames } , nameList : string [ ] ) {
157
- for ( const key of Object . keys ( nestedStackNames ) ) {
158
- nameList . push ( key ) ;
159
-
160
- if ( Object . keys ( nestedStackNames [ key ] . nestedChildStackNames ) . length !== 0 ) {
161
- flattenNestedStacksHelper ( nestedStackNames [ key ] . nestedChildStackNames , nameList ) ;
162
- }
163
- }
164
- }
165
-
166
127
interface StackTemplates {
167
128
readonly generatedTemplate : any ;
168
129
readonly deployedTemplate : any ;
0 commit comments