@stacksjs/ts-cloud 0.1.3 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (187) hide show
  1. package/README.md +98 -13
  2. package/dist/aws/acm.d.ts +129 -0
  3. package/dist/aws/application-autoscaling.d.ts +282 -0
  4. package/dist/aws/bedrock.d.ts +2292 -0
  5. package/dist/aws/client.d.ts +79 -0
  6. package/dist/aws/cloudformation.d.ts +105 -0
  7. package/dist/aws/cloudfront.d.ts +265 -0
  8. package/dist/aws/cloudwatch-logs.d.ts +48 -0
  9. package/dist/aws/comprehend.d.ts +505 -0
  10. package/dist/aws/connect.d.ts +377 -0
  11. package/dist/aws/deploy-imap.d.ts +14 -0
  12. package/dist/aws/dynamodb.d.ts +176 -0
  13. package/dist/aws/ec2.d.ts +272 -0
  14. package/dist/aws/ecr.d.ts +149 -0
  15. package/dist/aws/ecs.d.ts +162 -0
  16. package/dist/aws/elasticache.d.ts +71 -0
  17. package/dist/aws/elbv2.d.ts +248 -0
  18. package/dist/aws/email.d.ts +175 -0
  19. package/dist/aws/eventbridge.d.ts +142 -0
  20. package/dist/aws/iam.d.ts +638 -0
  21. package/dist/aws/imap-server.d.ts +119 -0
  22. package/{src/aws/index.ts → dist/aws/index.d.ts} +62 -83
  23. package/{src/aws/kendra.ts → dist/aws/kendra.d.ts} +71 -386
  24. package/dist/aws/lambda.d.ts +232 -0
  25. package/dist/aws/opensearch.d.ts +87 -0
  26. package/dist/aws/personalize.d.ts +516 -0
  27. package/dist/aws/polly.d.ts +214 -0
  28. package/dist/aws/rds.d.ts +240 -0
  29. package/dist/aws/rekognition.d.ts +543 -0
  30. package/dist/aws/route53-domains.d.ts +113 -0
  31. package/dist/aws/route53.d.ts +215 -0
  32. package/dist/aws/s3.d.ts +212 -0
  33. package/dist/aws/scheduler.d.ts +140 -0
  34. package/dist/aws/secrets-manager.d.ts +170 -0
  35. package/dist/aws/ses.d.ts +288 -0
  36. package/dist/aws/setup-phone.d.ts +0 -0
  37. package/dist/aws/setup-sms.d.ts +115 -0
  38. package/dist/aws/sms.d.ts +304 -0
  39. package/dist/aws/smtp-server.d.ts +61 -0
  40. package/dist/aws/sns.d.ts +117 -0
  41. package/dist/aws/sqs.d.ts +65 -0
  42. package/dist/aws/ssm.d.ts +179 -0
  43. package/dist/aws/sts.d.ts +15 -0
  44. package/dist/aws/support.d.ts +104 -0
  45. package/dist/aws/test-imap.d.ts +0 -0
  46. package/dist/aws/textract.d.ts +403 -0
  47. package/dist/aws/transcribe.d.ts +60 -0
  48. package/dist/aws/translate.d.ts +358 -0
  49. package/dist/aws/voice.d.ts +219 -0
  50. package/dist/bin/cli.js +1724 -0
  51. package/dist/config.d.ts +7 -0
  52. package/dist/deploy/index.d.ts +2 -0
  53. package/dist/deploy/static-site-external-dns.d.ts +51 -0
  54. package/dist/deploy/static-site.d.ts +71 -0
  55. package/dist/dns/cloudflare.d.ts +52 -0
  56. package/dist/dns/godaddy.d.ts +38 -0
  57. package/dist/dns/index.d.ts +45 -0
  58. package/dist/dns/porkbun.d.ts +18 -0
  59. package/dist/dns/route53-adapter.d.ts +38 -0
  60. package/{src/dns/types.ts → dist/dns/types.d.ts} +26 -63
  61. package/dist/dns/validator.d.ts +78 -0
  62. package/dist/generators/index.d.ts +1 -0
  63. package/dist/generators/infrastructure.d.ts +30 -0
  64. package/{src/index.ts → dist/index.d.ts} +70 -93
  65. package/dist/index.js +7881 -0
  66. package/dist/push/apns.d.ts +60 -0
  67. package/dist/push/fcm.d.ts +117 -0
  68. package/dist/push/index.d.ts +14 -0
  69. package/dist/security/pre-deploy-scanner.d.ts +69 -0
  70. package/dist/ssl/acme-client.d.ts +67 -0
  71. package/dist/ssl/index.d.ts +2 -0
  72. package/dist/ssl/letsencrypt.d.ts +48 -0
  73. package/dist/types.d.ts +1 -0
  74. package/dist/utils/cli.d.ts +123 -0
  75. package/dist/validation/index.d.ts +1 -0
  76. package/dist/validation/template.d.ts +23 -0
  77. package/package.json +8 -8
  78. package/bin/cli.ts +0 -133
  79. package/bin/commands/analytics.ts +0 -328
  80. package/bin/commands/api.ts +0 -379
  81. package/bin/commands/assets.ts +0 -221
  82. package/bin/commands/audit.ts +0 -501
  83. package/bin/commands/backup.ts +0 -682
  84. package/bin/commands/cache.ts +0 -294
  85. package/bin/commands/cdn.ts +0 -281
  86. package/bin/commands/config.ts +0 -202
  87. package/bin/commands/container.ts +0 -105
  88. package/bin/commands/cost.ts +0 -208
  89. package/bin/commands/database.ts +0 -401
  90. package/bin/commands/deploy.ts +0 -674
  91. package/bin/commands/domain.ts +0 -397
  92. package/bin/commands/email.ts +0 -423
  93. package/bin/commands/environment.ts +0 -285
  94. package/bin/commands/events.ts +0 -424
  95. package/bin/commands/firewall.ts +0 -145
  96. package/bin/commands/function.ts +0 -116
  97. package/bin/commands/generate.ts +0 -280
  98. package/bin/commands/git.ts +0 -139
  99. package/bin/commands/iam.ts +0 -464
  100. package/bin/commands/index.ts +0 -48
  101. package/bin/commands/init.ts +0 -120
  102. package/bin/commands/logs.ts +0 -148
  103. package/bin/commands/network.ts +0 -579
  104. package/bin/commands/notify.ts +0 -489
  105. package/bin/commands/queue.ts +0 -407
  106. package/bin/commands/scheduler.ts +0 -370
  107. package/bin/commands/secrets.ts +0 -54
  108. package/bin/commands/server.ts +0 -629
  109. package/bin/commands/shared.ts +0 -97
  110. package/bin/commands/ssl.ts +0 -138
  111. package/bin/commands/stack.ts +0 -325
  112. package/bin/commands/status.ts +0 -385
  113. package/bin/commands/storage.ts +0 -450
  114. package/bin/commands/team.ts +0 -96
  115. package/bin/commands/tunnel.ts +0 -489
  116. package/bin/commands/utils.ts +0 -202
  117. package/build.ts +0 -15
  118. package/cloud +0 -2
  119. package/src/aws/acm.ts +0 -768
  120. package/src/aws/application-autoscaling.ts +0 -845
  121. package/src/aws/bedrock.ts +0 -4074
  122. package/src/aws/client.ts +0 -878
  123. package/src/aws/cloudformation.ts +0 -896
  124. package/src/aws/cloudfront.ts +0 -1531
  125. package/src/aws/cloudwatch-logs.ts +0 -154
  126. package/src/aws/comprehend.ts +0 -839
  127. package/src/aws/connect.ts +0 -1056
  128. package/src/aws/deploy-imap.ts +0 -384
  129. package/src/aws/dynamodb.ts +0 -340
  130. package/src/aws/ec2.ts +0 -1385
  131. package/src/aws/ecr.ts +0 -621
  132. package/src/aws/ecs.ts +0 -615
  133. package/src/aws/elasticache.ts +0 -301
  134. package/src/aws/elbv2.ts +0 -942
  135. package/src/aws/email.ts +0 -928
  136. package/src/aws/eventbridge.ts +0 -248
  137. package/src/aws/iam.ts +0 -1689
  138. package/src/aws/imap-server.ts +0 -2100
  139. package/src/aws/lambda.ts +0 -786
  140. package/src/aws/opensearch.ts +0 -158
  141. package/src/aws/personalize.ts +0 -977
  142. package/src/aws/polly.ts +0 -559
  143. package/src/aws/rds.ts +0 -888
  144. package/src/aws/rekognition.ts +0 -846
  145. package/src/aws/route53-domains.ts +0 -359
  146. package/src/aws/route53.ts +0 -1046
  147. package/src/aws/s3.ts +0 -2318
  148. package/src/aws/scheduler.ts +0 -571
  149. package/src/aws/secrets-manager.ts +0 -769
  150. package/src/aws/ses.ts +0 -1081
  151. package/src/aws/setup-phone.ts +0 -104
  152. package/src/aws/setup-sms.ts +0 -580
  153. package/src/aws/sms.ts +0 -1735
  154. package/src/aws/smtp-server.ts +0 -531
  155. package/src/aws/sns.ts +0 -758
  156. package/src/aws/sqs.ts +0 -382
  157. package/src/aws/ssm.ts +0 -807
  158. package/src/aws/sts.ts +0 -92
  159. package/src/aws/support.ts +0 -391
  160. package/src/aws/test-imap.ts +0 -86
  161. package/src/aws/textract.ts +0 -780
  162. package/src/aws/transcribe.ts +0 -108
  163. package/src/aws/translate.ts +0 -641
  164. package/src/aws/voice.ts +0 -1379
  165. package/src/config.ts +0 -35
  166. package/src/deploy/index.ts +0 -7
  167. package/src/deploy/static-site-external-dns.ts +0 -906
  168. package/src/deploy/static-site.ts +0 -1125
  169. package/src/dns/godaddy.ts +0 -412
  170. package/src/dns/index.ts +0 -183
  171. package/src/dns/porkbun.ts +0 -362
  172. package/src/dns/route53-adapter.ts +0 -414
  173. package/src/dns/validator.ts +0 -369
  174. package/src/generators/index.ts +0 -5
  175. package/src/generators/infrastructure.ts +0 -1660
  176. package/src/push/apns.ts +0 -452
  177. package/src/push/fcm.ts +0 -506
  178. package/src/push/index.ts +0 -58
  179. package/src/ssl/acme-client.ts +0 -478
  180. package/src/ssl/index.ts +0 -7
  181. package/src/ssl/letsencrypt.ts +0 -747
  182. package/src/types.ts +0 -2
  183. package/src/utils/cli.ts +0 -398
  184. package/src/validation/index.ts +0 -5
  185. package/src/validation/template.ts +0 -405
  186. package/test/index.test.ts +0 -128
  187. package/tsconfig.json +0 -18
@@ -1,896 +0,0 @@
1
- /**
2
- * AWS CloudFormation Operations
3
- * Direct API calls without AWS CLI dependency
4
- */
5
-
6
- import { AWSClient, buildQueryParams } from './client'
7
-
8
- export interface StackParameter {
9
- ParameterKey: string
10
- ParameterValue: string
11
- UsePreviousValue?: boolean
12
- }
13
-
14
- export interface StackTag {
15
- Key: string
16
- Value: string
17
- }
18
-
19
- export interface CreateStackOptions {
20
- stackName: string
21
- templateBody?: string
22
- templateUrl?: string
23
- parameters?: StackParameter[]
24
- capabilities?: string[]
25
- roleArn?: string
26
- tags?: StackTag[]
27
- timeoutInMinutes?: number
28
- onFailure?: 'DO_NOTHING' | 'ROLLBACK' | 'DELETE'
29
- }
30
-
31
- export interface UpdateStackOptions {
32
- stackName: string
33
- templateBody?: string
34
- templateUrl?: string
35
- parameters?: StackParameter[]
36
- capabilities?: string[]
37
- roleArn?: string
38
- tags?: StackTag[]
39
- }
40
-
41
- export interface DescribeStacksOptions {
42
- stackName?: string
43
- }
44
-
45
- export interface StackEvent {
46
- Timestamp: string
47
- ResourceType: string
48
- LogicalResourceId: string
49
- ResourceStatus: string
50
- ResourceStatusReason?: string
51
- }
52
-
53
- export interface Stack {
54
- StackId: string
55
- StackName: string
56
- StackStatus: string
57
- StackStatusReason?: string
58
- CreationTime: string
59
- LastUpdatedTime?: string
60
- Parameters?: StackParameter[]
61
- Outputs?: Array<{
62
- OutputKey: string
63
- OutputValue: string
64
- Description?: string
65
- ExportName?: string
66
- }>
67
- Tags?: StackTag[]
68
- }
69
-
70
- /**
71
- * CloudFormation stack management using direct API calls
72
- */
73
- export class CloudFormationClient {
74
- private client: AWSClient
75
- private region: string
76
-
77
- constructor(region: string = 'us-east-1', profile?: string) {
78
- this.region = region
79
- this.client = new AWSClient()
80
- }
81
-
82
- /**
83
- * Create a new CloudFormation stack
84
- */
85
- async createStack(options: CreateStackOptions): Promise<{ StackId: string }> {
86
- const params: Record<string, any> = {
87
- Action: 'CreateStack',
88
- StackName: options.stackName,
89
- Version: '2010-05-15',
90
- }
91
-
92
- if (options.templateBody) {
93
- params.TemplateBody = options.templateBody
94
- }
95
- else if (options.templateUrl) {
96
- params.TemplateURL = options.templateUrl
97
- }
98
- else {
99
- throw new Error('Either templateBody or templateUrl must be provided')
100
- }
101
-
102
- if (options.parameters) {
103
- options.parameters.forEach((param, index) => {
104
- params[`Parameters.member.${index + 1}.ParameterKey`] = param.ParameterKey
105
- params[`Parameters.member.${index + 1}.ParameterValue`] = param.ParameterValue
106
- })
107
- }
108
-
109
- if (options.capabilities) {
110
- options.capabilities.forEach((cap, index) => {
111
- params[`Capabilities.member.${index + 1}`] = cap
112
- })
113
- }
114
-
115
- if (options.roleArn) {
116
- params.RoleARN = options.roleArn
117
- }
118
-
119
- if (options.tags) {
120
- options.tags.forEach((tag, index) => {
121
- params[`Tags.member.${index + 1}.Key`] = tag.Key
122
- params[`Tags.member.${index + 1}.Value`] = tag.Value
123
- })
124
- }
125
-
126
- if (options.timeoutInMinutes) {
127
- params.TimeoutInMinutes = options.timeoutInMinutes
128
- }
129
-
130
- if (options.onFailure) {
131
- params.OnFailure = options.onFailure
132
- }
133
-
134
- const result = await this.client.request({
135
- service: 'cloudformation',
136
- region: this.region,
137
- method: 'POST',
138
- path: '/',
139
- body: new URLSearchParams(params).toString(),
140
- })
141
-
142
- return { StackId: result.StackId || result.CreateStackResult?.StackId }
143
- }
144
-
145
- /**
146
- * Update an existing CloudFormation stack
147
- */
148
- async updateStack(options: UpdateStackOptions): Promise<{ StackId: string }> {
149
- const params: Record<string, any> = {
150
- Action: 'UpdateStack',
151
- StackName: options.stackName,
152
- Version: '2010-05-15',
153
- }
154
-
155
- if (options.templateBody) {
156
- params.TemplateBody = options.templateBody
157
- }
158
- else if (options.templateUrl) {
159
- params.TemplateURL = options.templateUrl
160
- }
161
-
162
- if (options.parameters) {
163
- options.parameters.forEach((param, index) => {
164
- params[`Parameters.member.${index + 1}.ParameterKey`] = param.ParameterKey
165
- if (param.UsePreviousValue) {
166
- params[`Parameters.member.${index + 1}.UsePreviousValue`] = 'true'
167
- }
168
- else {
169
- params[`Parameters.member.${index + 1}.ParameterValue`] = param.ParameterValue
170
- }
171
- })
172
- }
173
-
174
- if (options.capabilities) {
175
- options.capabilities.forEach((cap, index) => {
176
- params[`Capabilities.member.${index + 1}`] = cap
177
- })
178
- }
179
-
180
- if (options.roleArn) {
181
- params.RoleARN = options.roleArn
182
- }
183
-
184
- if (options.tags) {
185
- options.tags.forEach((tag, index) => {
186
- params[`Tags.member.${index + 1}.Key`] = tag.Key
187
- params[`Tags.member.${index + 1}.Value`] = tag.Value
188
- })
189
- }
190
-
191
- const result = await this.client.request({
192
- service: 'cloudformation',
193
- region: this.region,
194
- method: 'POST',
195
- path: '/',
196
- body: new URLSearchParams(params).toString(),
197
- })
198
-
199
- return { StackId: result.StackId || result.UpdateStackResult?.StackId }
200
- }
201
-
202
- /**
203
- * Delete a CloudFormation stack
204
- */
205
- async deleteStack(stackName: string, roleArn?: string, retainResources?: string[]): Promise<void> {
206
- const params: Record<string, any> = {
207
- Action: 'DeleteStack',
208
- StackName: stackName,
209
- Version: '2010-05-15',
210
- }
211
-
212
- if (roleArn) {
213
- params.RoleARN = roleArn
214
- }
215
-
216
- // Add retained resources if provided
217
- if (retainResources && retainResources.length > 0) {
218
- retainResources.forEach((resource, index) => {
219
- params[`RetainResources.member.${index + 1}`] = resource
220
- })
221
- }
222
-
223
- await this.client.request({
224
- service: 'cloudformation',
225
- region: this.region,
226
- method: 'POST',
227
- path: '/',
228
- body: new URLSearchParams(params).toString(),
229
- })
230
- }
231
-
232
- /**
233
- * Describe CloudFormation stacks
234
- */
235
- async describeStacks(options: DescribeStacksOptions = {}): Promise<{ Stacks: Stack[] }> {
236
- const params: Record<string, any> = {
237
- Action: 'DescribeStacks',
238
- Version: '2010-05-15',
239
- }
240
-
241
- if (options.stackName) {
242
- params.StackName = options.stackName
243
- }
244
-
245
- const result = await this.client.request({
246
- service: 'cloudformation',
247
- region: this.region,
248
- method: 'POST',
249
- path: '/',
250
- body: new URLSearchParams(params).toString(),
251
- })
252
-
253
- // Parse the response
254
- const stacks = this.parseStacksResponse(result)
255
- return { Stacks: stacks }
256
- }
257
-
258
- /**
259
- * Get stack events
260
- */
261
- async describeStackEvents(stackName: string): Promise<{ StackEvents: StackEvent[] }> {
262
- const params: Record<string, any> = {
263
- Action: 'DescribeStackEvents',
264
- StackName: stackName,
265
- Version: '2010-05-15',
266
- }
267
-
268
- const result = await this.client.request({
269
- service: 'cloudformation',
270
- region: this.region,
271
- method: 'POST',
272
- path: '/',
273
- body: new URLSearchParams(params).toString(),
274
- })
275
-
276
- return { StackEvents: this.parseStackEvents(result) }
277
- }
278
-
279
- /**
280
- * List stack resources
281
- */
282
- async listStackResources(stackName: string): Promise<{ StackResourceSummaries: any[] }> {
283
- const params: Record<string, any> = {
284
- Action: 'ListStackResources',
285
- StackName: stackName,
286
- Version: '2010-05-15',
287
- }
288
-
289
- const result = await this.client.request({
290
- service: 'cloudformation',
291
- region: this.region,
292
- method: 'POST',
293
- path: '/',
294
- body: new URLSearchParams(params).toString(),
295
- })
296
-
297
- // Parse resources from XML response - handle single member (object) or multiple members (array)
298
- const member = result?.ListStackResourcesResult?.StackResourceSummaries?.member
299
- let resources: any[] = []
300
-
301
- if (member) {
302
- resources = Array.isArray(member) ? member : [member]
303
- }
304
-
305
- return { StackResourceSummaries: resources }
306
- }
307
-
308
- /**
309
- * Wait for stack to reach a specific status
310
- */
311
- async waitForStack(stackName: string, waitType: 'stack-create-complete' | 'stack-update-complete' | 'stack-delete-complete'): Promise<void> {
312
- const targetStatuses = {
313
- 'stack-create-complete': ['CREATE_COMPLETE'],
314
- 'stack-update-complete': ['UPDATE_COMPLETE'],
315
- 'stack-delete-complete': ['DELETE_COMPLETE'],
316
- }
317
-
318
- const failureStatuses = [
319
- 'CREATE_FAILED',
320
- 'ROLLBACK_FAILED',
321
- 'ROLLBACK_COMPLETE',
322
- 'UPDATE_ROLLBACK_FAILED',
323
- 'UPDATE_ROLLBACK_COMPLETE',
324
- ]
325
-
326
- const targets = targetStatuses[waitType]
327
- const maxAttempts = 360 // 30 minutes (DNS records can take 10-30 minutes)
328
- let attempts = 0
329
-
330
- while (attempts < maxAttempts) {
331
- try {
332
- const result = await this.describeStacks({ stackName })
333
-
334
- if (result.Stacks.length === 0) {
335
- if (waitType === 'stack-delete-complete') {
336
- return // Stack deleted successfully
337
- }
338
- // For create/update operations, stack might not be visible yet - retry
339
- if (attempts % 10 === 0) {
340
- console.log(`[waitForStack] Attempt ${attempts}: Stack not visible yet`)
341
- }
342
- await new Promise(resolve => setTimeout(resolve, 2000))
343
- attempts++
344
- continue
345
- }
346
-
347
- const stack = result.Stacks[0]
348
-
349
- if (attempts % 10 === 0) {
350
- console.log(`[waitForStack] Attempt ${attempts}: Status = ${stack.StackStatus}${stack.StackStatusReason ? ` (${stack.StackStatusReason})` : ''}`)
351
- }
352
-
353
- if (targets.includes(stack.StackStatus)) {
354
- return // Target status reached
355
- }
356
-
357
- // If stack is being deleted but we're waiting for create/update, something went wrong
358
- if ((waitType === 'stack-create-complete' || waitType === 'stack-update-complete') &&
359
- (stack.StackStatus === 'DELETE_IN_PROGRESS' || stack.StackStatus === 'DELETE_COMPLETE')) {
360
- console.log(`[waitForStack] Stack is being deleted (creation/update failed)`)
361
- // Try to get stack events to understand the failure
362
- let failedEventReason = ''
363
- try {
364
- const eventsResult = await this.describeStackEvents(stackName)
365
- console.log('[waitForStack] Stack events (most recent first):')
366
- for (const event of eventsResult.StackEvents.slice(0, 15)) {
367
- if (event.ResourceStatus.includes('FAILED') || event.ResourceStatusReason) {
368
- console.log(` ${event.LogicalResourceId}: ${event.ResourceStatus} - ${event.ResourceStatusReason || 'No reason provided'}`)
369
- // Capture the first failed event reason for the error message
370
- if (event.ResourceStatus.includes('FAILED') && event.ResourceStatusReason && !failedEventReason) {
371
- failedEventReason = event.ResourceStatusReason
372
- }
373
- }
374
- }
375
- }
376
- catch {
377
- // Ignore errors fetching events
378
- }
379
- // Include the detailed failure reason in the error message
380
- const errorReason = failedEventReason || stack.StackStatusReason || 'Check CloudFormation console for details.'
381
- throw new Error(`Stack creation/update failed - stack is being deleted. Reason: ${errorReason}`)
382
- }
383
-
384
- // Handle DELETE_FAILED specifically - might need to retain resources
385
- if (stack.StackStatus === 'DELETE_FAILED' && waitType === 'stack-delete-complete') {
386
- const error: any = new Error(`Stack deletion failed - may have resources that need to be retained`)
387
- error.code = 'DELETE_FAILED'
388
- error.stackStatus = stack.StackStatus
389
- error.statusReason = stack.StackStatusReason
390
- throw error
391
- }
392
-
393
- if (failureStatuses.includes(stack.StackStatus)) {
394
- throw new Error(`Stack reached failure status: ${stack.StackStatus}`)
395
- }
396
-
397
- // Wait 5 seconds before next attempt
398
- await new Promise(resolve => setTimeout(resolve, 5000))
399
- attempts++
400
- }
401
- catch (error: any) {
402
- if (waitType === 'stack-delete-complete' && error.message?.includes('does not exist')) {
403
- return // Stack deleted
404
- }
405
- // For create operations, stack might not be visible yet - retry
406
- if (waitType === 'stack-create-complete' && error.message?.includes('does not exist')) {
407
- if (attempts % 10 === 0) {
408
- console.log(`[waitForStack] Attempt ${attempts}: Stack does not exist (error), retrying...`)
409
- }
410
- await new Promise(resolve => setTimeout(resolve, 2000))
411
- attempts++
412
- continue
413
- }
414
- console.log(`[waitForStack] Unexpected error: ${error.message}`)
415
- throw error
416
- }
417
- }
418
-
419
- console.log(`[waitForStack] Timeout after ${attempts} attempts`)
420
- throw new Error(`Timeout waiting for stack to reach ${waitType}`)
421
- }
422
-
423
- /**
424
- * Validate CloudFormation template
425
- */
426
- async validateTemplate(templateBody: string): Promise<any> {
427
- const params: Record<string, any> = {
428
- Action: 'ValidateTemplate',
429
- TemplateBody: templateBody,
430
- Version: '2010-05-15',
431
- }
432
-
433
- return await this.client.request({
434
- service: 'cloudformation',
435
- region: this.region,
436
- method: 'POST',
437
- path: '/',
438
- body: new URLSearchParams(params).toString(),
439
- })
440
- }
441
-
442
- /**
443
- * List all stacks
444
- */
445
- async listStacks(statusFilter?: string[]): Promise<{ StackSummaries: Array<{
446
- StackId: string
447
- StackName: string
448
- TemplateDescription?: string
449
- CreationTime: string
450
- LastUpdatedTime?: string
451
- DeletionTime?: string
452
- StackStatus: string
453
- }> }> {
454
- const params: Record<string, any> = {
455
- Action: 'ListStacks',
456
- Version: '2010-05-15',
457
- }
458
-
459
- if (statusFilter) {
460
- statusFilter.forEach((status, index) => {
461
- params[`StackStatusFilter.member.${index + 1}`] = status
462
- })
463
- }
464
-
465
- const result = await this.client.request({
466
- service: 'cloudformation',
467
- region: this.region,
468
- method: 'POST',
469
- path: '/',
470
- body: new URLSearchParams(params).toString(),
471
- })
472
-
473
- return { StackSummaries: [] } // TODO: Parse response
474
- }
475
-
476
-
477
- /**
478
- * Create change set (for preview before updating)
479
- */
480
- async createChangeSet(options: {
481
- stackName: string
482
- changeSetName: string
483
- templateBody?: string
484
- templateUrl?: string
485
- parameters?: StackParameter[]
486
- capabilities?: string[]
487
- changeSetType?: 'CREATE' | 'UPDATE'
488
- }): Promise<{ Id: string, StackId: string }> {
489
- const params: Record<string, any> = {
490
- Action: 'CreateChangeSet',
491
- StackName: options.stackName,
492
- ChangeSetName: options.changeSetName,
493
- Version: '2010-05-15',
494
- }
495
-
496
- if (options.templateBody) {
497
- params.TemplateBody = options.templateBody
498
- }
499
- else if (options.templateUrl) {
500
- params.TemplateURL = options.templateUrl
501
- }
502
-
503
- if (options.parameters) {
504
- options.parameters.forEach((param, index) => {
505
- params[`Parameters.member.${index + 1}.ParameterKey`] = param.ParameterKey
506
- params[`Parameters.member.${index + 1}.ParameterValue`] = param.ParameterValue
507
- })
508
- }
509
-
510
- if (options.capabilities) {
511
- options.capabilities.forEach((cap, index) => {
512
- params[`Capabilities.member.${index + 1}`] = cap
513
- })
514
- }
515
-
516
- if (options.changeSetType) {
517
- params.ChangeSetType = options.changeSetType
518
- }
519
-
520
- const result = await this.client.request({
521
- service: 'cloudformation',
522
- region: this.region,
523
- method: 'POST',
524
- path: '/',
525
- body: new URLSearchParams(params).toString(),
526
- })
527
-
528
- return { Id: result.Id, StackId: result.StackId }
529
- }
530
-
531
- /**
532
- * Describe change set
533
- */
534
- async describeChangeSet(stackName: string, changeSetName: string): Promise<any> {
535
- const params: Record<string, any> = {
536
- Action: 'DescribeChangeSet',
537
- StackName: stackName,
538
- ChangeSetName: changeSetName,
539
- Version: '2010-05-15',
540
- }
541
-
542
- return await this.client.request({
543
- service: 'cloudformation',
544
- region: this.region,
545
- method: 'POST',
546
- path: '/',
547
- body: new URLSearchParams(params).toString(),
548
- })
549
- }
550
-
551
- /**
552
- * Execute change set
553
- */
554
- async executeChangeSet(stackName: string, changeSetName: string): Promise<void> {
555
- const params: Record<string, any> = {
556
- Action: 'ExecuteChangeSet',
557
- StackName: stackName,
558
- ChangeSetName: changeSetName,
559
- Version: '2010-05-15',
560
- }
561
-
562
- await this.client.request({
563
- service: 'cloudformation',
564
- region: this.region,
565
- method: 'POST',
566
- path: '/',
567
- body: new URLSearchParams(params).toString(),
568
- })
569
- }
570
-
571
- /**
572
- * Delete change set
573
- */
574
- async deleteChangeSet(stackName: string, changeSetName: string): Promise<void> {
575
- const params: Record<string, any> = {
576
- Action: 'DeleteChangeSet',
577
- StackName: stackName,
578
- ChangeSetName: changeSetName,
579
- Version: '2010-05-15',
580
- }
581
-
582
- await this.client.request({
583
- service: 'cloudformation',
584
- region: this.region,
585
- method: 'POST',
586
- path: '/',
587
- body: new URLSearchParams(params).toString(),
588
- })
589
- }
590
-
591
- /**
592
- * Get stack outputs as key-value pairs
593
- */
594
- async getStackOutputs(stackName: string): Promise<Record<string, string>> {
595
- const result = await this.describeStacks({ stackName })
596
-
597
- if (!result.Stacks || result.Stacks.length === 0) {
598
- throw new Error(`Stack ${stackName} not found`)
599
- }
600
-
601
- const stack = result.Stacks[0]
602
- const outputs: Record<string, string> = {}
603
-
604
- if (stack.Outputs) {
605
- for (const output of stack.Outputs) {
606
- outputs[output.OutputKey] = output.OutputValue
607
- }
608
- }
609
-
610
- return outputs
611
- }
612
-
613
- /**
614
- * Get stack template
615
- */
616
- async getTemplate(stackName: string): Promise<{ TemplateBody: string }> {
617
- const params: Record<string, any> = {
618
- Action: 'GetTemplate',
619
- StackName: stackName,
620
- Version: '2010-05-15',
621
- }
622
-
623
- const result = await this.client.request({
624
- service: 'cloudformation',
625
- region: this.region,
626
- method: 'POST',
627
- path: '/',
628
- body: new URLSearchParams(params).toString(),
629
- })
630
-
631
- // Handle the GetTemplateResult wrapper
632
- const templateBody = result?.GetTemplateResult?.TemplateBody || result?.TemplateBody || ''
633
- return { TemplateBody: templateBody }
634
- }
635
-
636
- /**
637
- * Parse stacks response
638
- */
639
- private parseStacksResponse(result: any): Stack[] {
640
- const stacks: Stack[] = []
641
-
642
- // Handle different response structures from XML parsing
643
- // The XML response is: DescribeStacksResponse > DescribeStacksResult > Stacks > member
644
- let stackData = result?.DescribeStacksResult?.Stacks?.member
645
- || result?.Stacks?.member
646
- || result?.Stacks
647
- || result
648
-
649
- // If it's a single stack, wrap in array
650
- if (stackData && !Array.isArray(stackData)) {
651
- stackData = [stackData]
652
- }
653
-
654
- if (Array.isArray(stackData)) {
655
- for (const s of stackData) {
656
- if (s.StackId || s.StackName) {
657
- // Parse outputs
658
- let outputs: Array<{ OutputKey: string, OutputValue: string, Description?: string, ExportName?: string }> | undefined
659
- if (s.Outputs?.member) {
660
- const outputData = Array.isArray(s.Outputs.member) ? s.Outputs.member : [s.Outputs.member]
661
- outputs = outputData.map((o: any) => ({
662
- OutputKey: o.OutputKey,
663
- OutputValue: o.OutputValue,
664
- Description: o.Description,
665
- ExportName: o.ExportName,
666
- }))
667
- }
668
-
669
- stacks.push({
670
- StackId: s.StackId,
671
- StackName: s.StackName,
672
- StackStatus: s.StackStatus,
673
- CreationTime: s.CreationTime,
674
- LastUpdatedTime: s.LastUpdatedTime,
675
- StackStatusReason: s.StackStatusReason,
676
- Outputs: outputs,
677
- })
678
- }
679
- }
680
- }
681
- // Fallback for flat response
682
- else if (result.StackId) {
683
- stacks.push({
684
- StackId: result.StackId,
685
- StackName: result.StackName,
686
- StackStatus: result.StackStatus,
687
- CreationTime: result.CreationTime,
688
- LastUpdatedTime: result.LastUpdatedTime,
689
- })
690
- }
691
-
692
- return stacks
693
- }
694
-
695
- /**
696
- * Parse stack events response
697
- */
698
- private parseStackEvents(result: any): StackEvent[] {
699
- const events: StackEvent[] = []
700
-
701
- // Handle XML response structure: DescribeStackEventsResult > StackEvents > member
702
- let eventData = result?.DescribeStackEventsResult?.StackEvents?.member
703
- || result?.StackEvents?.member
704
- || result?.StackEvents
705
- || []
706
-
707
- // If single event, wrap in array
708
- if (eventData && !Array.isArray(eventData)) {
709
- eventData = [eventData]
710
- }
711
-
712
- for (const e of eventData) {
713
- if (e.LogicalResourceId) {
714
- events.push({
715
- Timestamp: e.Timestamp,
716
- ResourceType: e.ResourceType,
717
- LogicalResourceId: e.LogicalResourceId,
718
- ResourceStatus: e.ResourceStatus,
719
- ResourceStatusReason: e.ResourceStatusReason,
720
- })
721
- }
722
- }
723
-
724
- return events
725
- }
726
-
727
- /**
728
- * Wait for stack with real-time progress output (CDK-style)
729
- */
730
- async waitForStackWithProgress(
731
- stackName: string,
732
- waitType: 'stack-create-complete' | 'stack-update-complete' | 'stack-delete-complete',
733
- onProgress?: (event: {
734
- resourceId: string
735
- resourceType: string
736
- status: string
737
- reason?: string
738
- timestamp: string
739
- }) => void,
740
- ): Promise<void> {
741
- const targetStatuses = {
742
- 'stack-create-complete': ['CREATE_COMPLETE'],
743
- 'stack-update-complete': ['UPDATE_COMPLETE'],
744
- 'stack-delete-complete': ['DELETE_COMPLETE'],
745
- }
746
-
747
- const failureStatuses = [
748
- 'CREATE_FAILED',
749
- 'ROLLBACK_FAILED',
750
- 'ROLLBACK_COMPLETE',
751
- 'UPDATE_ROLLBACK_FAILED',
752
- 'UPDATE_ROLLBACK_COMPLETE',
753
- ]
754
-
755
- const targets = targetStatuses[waitType]
756
- const maxAttempts = 360 // 30 minutes (DNS records can take 10-30 minutes)
757
- let attempts = 0
758
- const seenEvents = new Set<string>()
759
-
760
- while (attempts < maxAttempts) {
761
- try {
762
- // Get stack events for progress
763
- if (onProgress) {
764
- try {
765
- const eventsResult = await this.describeStackEvents(stackName)
766
- // Events are returned newest first, reverse for chronological order
767
- const events = [...(eventsResult.StackEvents || [])].reverse()
768
-
769
- for (const event of events) {
770
- // Create unique key for this event
771
- const eventKey = `${event.LogicalResourceId}-${event.ResourceStatus}-${event.Timestamp}`
772
-
773
- if (!seenEvents.has(eventKey)) {
774
- seenEvents.add(eventKey)
775
- onProgress({
776
- resourceId: event.LogicalResourceId,
777
- resourceType: event.ResourceType,
778
- status: event.ResourceStatus,
779
- reason: event.ResourceStatusReason,
780
- timestamp: event.Timestamp,
781
- })
782
- }
783
- }
784
- }
785
- catch {
786
- // Events might not be available yet, continue
787
- }
788
- }
789
-
790
- // Check stack status
791
- const result = await this.describeStacks({ stackName })
792
-
793
- if (result.Stacks.length === 0) {
794
- if (waitType === 'stack-delete-complete') {
795
- return // Stack deleted successfully
796
- }
797
- await new Promise(resolve => setTimeout(resolve, 2000))
798
- attempts++
799
- continue
800
- }
801
-
802
- const stack = result.Stacks[0]
803
-
804
- if (targets.includes(stack.StackStatus)) {
805
- return // Target status reached
806
- }
807
-
808
- if (stack.StackStatus === 'DELETE_FAILED' && waitType === 'stack-delete-complete') {
809
- const error: any = new Error(`Stack deletion failed - may have resources that need to be retained`)
810
- error.code = 'DELETE_FAILED'
811
- error.stackStatus = stack.StackStatus
812
- error.statusReason = stack.StackStatusReason
813
- throw error
814
- }
815
-
816
- if (failureStatuses.includes(stack.StackStatus)) {
817
- throw new Error(`Stack reached failure status: ${stack.StackStatus}`)
818
- }
819
-
820
- await new Promise(resolve => setTimeout(resolve, 3000))
821
- attempts++
822
- }
823
- catch (error: any) {
824
- if (waitType === 'stack-delete-complete' && error.message?.includes('does not exist')) {
825
- return
826
- }
827
- if (waitType === 'stack-create-complete' && error.message?.includes('does not exist')) {
828
- await new Promise(resolve => setTimeout(resolve, 2000))
829
- attempts++
830
- continue
831
- }
832
- throw error
833
- }
834
- }
835
-
836
- throw new Error(`Timeout waiting for stack to reach ${waitType}`)
837
- }
838
-
839
- /**
840
- * Wait for stack operation to complete (create, update, or delete)
841
- * Returns a result object with success status
842
- */
843
- async waitForStackComplete(
844
- stackName: string,
845
- maxAttempts: number = 120,
846
- delayMs: number = 5000,
847
- ): Promise<{ success: boolean; status: string; reason?: string }> {
848
- const successStatuses = [
849
- 'CREATE_COMPLETE',
850
- 'UPDATE_COMPLETE',
851
- 'DELETE_COMPLETE',
852
- ]
853
- const failureStatuses = [
854
- 'CREATE_FAILED',
855
- 'UPDATE_FAILED',
856
- 'DELETE_FAILED',
857
- 'ROLLBACK_COMPLETE',
858
- 'ROLLBACK_FAILED',
859
- 'UPDATE_ROLLBACK_COMPLETE',
860
- 'UPDATE_ROLLBACK_FAILED',
861
- ]
862
-
863
- for (let i = 0; i < maxAttempts; i++) {
864
- try {
865
- const result = await this.describeStacks({ stackName })
866
-
867
- if (result.Stacks.length === 0) {
868
- // Stack was deleted
869
- return { success: true, status: 'DELETE_COMPLETE' }
870
- }
871
-
872
- const stack = result.Stacks[0]
873
- const status = stack.StackStatus
874
-
875
- if (successStatuses.includes(status)) {
876
- return { success: true, status }
877
- }
878
-
879
- if (failureStatuses.includes(status)) {
880
- return { success: false, status, reason: stack.StackStatusReason }
881
- }
882
-
883
- // Still in progress, wait and retry
884
- await new Promise(resolve => setTimeout(resolve, delayMs))
885
- }
886
- catch (error: any) {
887
- if (error.message?.includes('does not exist')) {
888
- return { success: true, status: 'DELETE_COMPLETE' }
889
- }
890
- throw error
891
- }
892
- }
893
-
894
- return { success: false, status: 'TIMEOUT', reason: 'Timeout waiting for stack operation' }
895
- }
896
- }