-
Notifications
You must be signed in to change notification settings - Fork 4
/
stack.ts
286 lines (254 loc) · 6.72 KB
/
stack.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import { CloudFormation } from "@aws-sdk/client-cloudformation"
import { AwsCredentialIdentity } from "@aws-sdk/types"
import {
CloudFormationStack,
StackCapability,
StackName,
StackParameterKey,
StackPolicyBody,
} from "../aws/cloudformation/model.js"
import { CloudFormationClient } from "../aws/cloudformation/client.js"
import {
CredentialManager,
InternalCredentialManager,
} from "../aws/common/credentials.js"
import { AccountId, Region, TagKey } from "../aws/common/model.js"
import { TemplateBucketConfig, TimeoutConfig, Vars } from "../common/model.js"
import { HookExecutor } from "../hooks/hook-executor.js"
import { ResolverExecutor } from "../resolvers/resolver-executor.js"
import { CommandRole, Project } from "../takomo-core/command.js"
import { ROOT_STACK_GROUP_PATH } from "../takomo-stacks-model/constants.js"
import { Schemas } from "../takomo-stacks-model/schemas.js"
import { FilePath } from "../utils/files.js"
import { TkmLogger } from "../utils/logging.js"
import { StackGroupName, StackGroupPath } from "./stack-group.js"
export type RawTagValue = string | number | boolean
/**
* Stack path.
*/
export type StackPath = string
/**
* Blueprint path.
*/
export type BlueprintPath = string
export interface Template {
readonly dynamic: boolean
readonly filename?: FilePath
readonly inline?: string
}
export interface StackProps {
project?: Project
path: StackPath
stackGroupPath: StackGroupPath
name: StackName
template: Template
templateBucket?: TemplateBucketConfig
region: Region
accountIds: ReadonlyArray<AccountId>
commandRole?: CommandRole
tags: Map<TagKey, RawTagValue>
timeout: TimeoutConfig
parameters: Map<StackParameterKey, ResolverExecutor>
dependencies: ReadonlyArray<StackPath>
dependents: ReadonlyArray<StackPath>
data: Vars
hooks: ReadonlyArray<HookExecutor>
credentialManager: InternalCredentialManager
capabilities?: ReadonlyArray<StackCapability>
ignore: boolean
obsolete: boolean
terminationProtection: boolean
logger: TkmLogger
getCloudFormationClient: () => Promise<CloudFormationClient>
stackPolicy?: StackPolicyBody
stackPolicyDuringUpdate?: StackPolicyBody
schemas?: Schemas
}
/**
* An interface representing a CloudFormation stack configuration.
*/
export interface Stack {
/**
* Project of the stack
*/
readonly project?: Project
/**
* Path of the stack
*/
readonly path: StackPath
/**
* Path of the stack group where the stack belongs to
*/
readonly stackGroupPath: StackGroupPath
/**
* Name of the stack
*/
readonly name: StackName
/**
* Region where the stack is deployed
*/
readonly region: Region
/**
* Other stacks that the stack depends on
*/
readonly dependencies: ReadonlyArray<StackPath>
/**
* Other stacks that depend on the stack
*/
readonly dependents: ReadonlyArray<StackPath>
/**
* Data associated with the stack
*/
readonly data: Record<string, unknown>
/**
* Credential manager holding credentials associated with the stack
*/
readonly credentialManager: CredentialManager
/**
* Credentials associated with the stack
*/
readonly getCredentials: () => Promise<AwsCredentialIdentity>
/**
* Logger instance associated with the stack
*/
readonly logger: TkmLogger
/**
* Get CloudFormation client
*/
readonly getClient: () => Promise<CloudFormation>
/**
* Get current CloudFormation stack
*/
readonly getCurrentCloudFormationStack: () => Promise<
CloudFormationStack | undefined
>
}
export interface InternalStack extends Stack {
readonly template: Template
readonly templateBucket?: TemplateBucketConfig
readonly accountIds: ReadonlyArray<AccountId>
readonly commandRole?: CommandRole
readonly tags: Map<TagKey, RawTagValue>
readonly timeout: TimeoutConfig
readonly parameters: Map<StackParameterKey, ResolverExecutor>
readonly hooks: ReadonlyArray<HookExecutor>
readonly capabilities?: ReadonlyArray<StackCapability>
readonly ignore: boolean
readonly obsolete: boolean
readonly terminationProtection: boolean
readonly stackPolicy?: StackPolicyBody
readonly stackPolicyDuringUpdate?: StackPolicyBody
readonly schemas?: Schemas
readonly getCloudFormationClient: () => Promise<CloudFormationClient>
readonly credentialManager: InternalCredentialManager
readonly toProps: () => StackProps
}
export const createStack = (props: StackProps): InternalStack => {
const {
accountIds,
capabilities,
commandRole,
credentialManager,
data,
dependents,
dependencies,
hooks,
ignore,
obsolete,
logger,
name,
parameters,
path,
project,
region,
stackGroupPath,
tags,
template,
templateBucket,
terminationProtection,
timeout,
getCloudFormationClient,
stackPolicy,
stackPolicyDuringUpdate,
schemas,
} = props
const getClient = async () =>
getCloudFormationClient().then((c) => c.getNativeClient())
const getCurrentCloudFormationStack = () =>
getCloudFormationClient().then((c) => c.describeStack(name))
const getCredentials = () => credentialManager.getCredentials()
return {
accountIds,
capabilities,
commandRole,
getCredentials,
credentialManager,
data,
dependents,
dependencies,
getClient,
getCloudFormationClient,
getCurrentCloudFormationStack,
hooks,
ignore,
obsolete,
logger,
name,
parameters,
path,
project,
region,
stackGroupPath,
tags,
template,
templateBucket,
terminationProtection,
timeout,
stackPolicy,
stackPolicyDuringUpdate,
schemas,
toProps: () => props,
}
}
const normalizeStackPathInternal = (
stackPath: StackPath,
parentPathParts: ReadonlyArray<StackGroupName>,
[firstStackPathPart, ...restStackPathParts]: ReadonlyArray<StackPath>,
): StackPath => {
if (firstStackPathPart !== "..") {
return `/${[
...parentPathParts,
firstStackPathPart,
...restStackPathParts,
].join("/")}`
}
if (parentPathParts.length === 0) {
throw new Error(`Invalid relative stack path '${stackPath}'`)
}
return normalizeStackPathInternal(
stackPath,
parentPathParts.slice(0, -1),
restStackPathParts,
)
}
export const normalizeStackPath = (
parentPath: StackGroupPath,
stackPath: StackPath,
): StackPath => {
if (stackPath.startsWith("/")) {
return stackPath
}
if (!stackPath.startsWith("../")) {
return parentPath === ROOT_STACK_GROUP_PATH
? `/${stackPath}`
: `${parentPath}/${stackPath}`
}
if (parentPath === ROOT_STACK_GROUP_PATH) {
throw new Error(`Invalid relative stack path '${stackPath}'`)
}
return normalizeStackPathInternal(
stackPath,
parentPath.slice(1).split("/"),
stackPath.split("/"),
)
}