Skip to content

Commit ccf0e79

Browse files
committed
fix(cloudformation): add pagination to list-stack-resources, describe-stacks, and describe-stack-events routes
1 parent 6dce82e commit ccf0e79

File tree

3 files changed

+51
-21
lines changed

3 files changed

+51
-21
lines changed

apps/sim/app/api/tools/cloudformation/describe-stack-events/route.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { CloudFormationClient, DescribeStackEventsCommand } from '@aws-sdk/client-cloudformation'
1+
import {
2+
CloudFormationClient,
3+
DescribeStackEventsCommand,
4+
type StackEvent,
5+
} from '@aws-sdk/client-cloudformation'
26
import { createLogger } from '@sim/logger'
37
import { type NextRequest, NextResponse } from 'next/server'
48
import { z } from 'zod'
@@ -35,15 +39,21 @@ export async function POST(request: NextRequest) {
3539
},
3640
})
3741

38-
const command = new DescribeStackEventsCommand({
39-
StackName: validatedData.stackName,
40-
})
41-
4242
const limit = validatedData.limit ?? 50
4343

44-
const response = await client.send(command)
44+
const allEvents: StackEvent[] = []
45+
let nextToken: string | undefined
46+
do {
47+
const command = new DescribeStackEventsCommand({
48+
StackName: validatedData.stackName,
49+
...(nextToken && { NextToken: nextToken }),
50+
})
51+
const response = await client.send(command)
52+
allEvents.push(...(response.StackEvents ?? []))
53+
nextToken = allEvents.length >= limit ? undefined : response.NextToken
54+
} while (nextToken)
4555

46-
const events = (response.StackEvents ?? []).slice(0, limit).map((e) => ({
56+
const events = allEvents.slice(0, limit).map((e) => ({
4757
stackId: e.StackId ?? '',
4858
eventId: e.EventId ?? '',
4959
stackName: e.StackName ?? '',

apps/sim/app/api/tools/cloudformation/describe-stacks/route.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { CloudFormationClient, DescribeStacksCommand } from '@aws-sdk/client-cloudformation'
1+
import {
2+
CloudFormationClient,
3+
DescribeStacksCommand,
4+
type Stack,
5+
} from '@aws-sdk/client-cloudformation'
26
import { createLogger } from '@sim/logger'
37
import { type NextRequest, NextResponse } from 'next/server'
48
import { z } from 'zod'
@@ -31,13 +35,19 @@ export async function POST(request: NextRequest) {
3135
},
3236
})
3337

34-
const command = new DescribeStacksCommand({
35-
...(validatedData.stackName && { StackName: validatedData.stackName }),
36-
})
37-
38-
const response = await client.send(command)
38+
const allStacks: Stack[] = []
39+
let nextToken: string | undefined
40+
do {
41+
const command = new DescribeStacksCommand({
42+
...(validatedData.stackName && { StackName: validatedData.stackName }),
43+
...(nextToken && { NextToken: nextToken }),
44+
})
45+
const response = await client.send(command)
46+
allStacks.push(...(response.Stacks ?? []))
47+
nextToken = response.NextToken
48+
} while (nextToken)
3949

40-
const stacks = (response.Stacks ?? []).map((s) => ({
50+
const stacks = allStacks.map((s) => ({
4151
stackName: s.StackName ?? '',
4252
stackId: s.StackId ?? '',
4353
stackStatus: s.StackStatus ?? 'UNKNOWN',

apps/sim/app/api/tools/cloudformation/list-stack-resources/route.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { CloudFormationClient, ListStackResourcesCommand } from '@aws-sdk/client-cloudformation'
1+
import {
2+
CloudFormationClient,
3+
ListStackResourcesCommand,
4+
type StackResourceSummary,
5+
} from '@aws-sdk/client-cloudformation'
26
import { createLogger } from '@sim/logger'
37
import { type NextRequest, NextResponse } from 'next/server'
48
import { z } from 'zod'
@@ -31,13 +35,19 @@ export async function POST(request: NextRequest) {
3135
},
3236
})
3337

34-
const command = new ListStackResourcesCommand({
35-
StackName: validatedData.stackName,
36-
})
37-
38-
const response = await client.send(command)
38+
const allSummaries: StackResourceSummary[] = []
39+
let nextToken: string | undefined
40+
do {
41+
const command = new ListStackResourcesCommand({
42+
StackName: validatedData.stackName,
43+
...(nextToken && { NextToken: nextToken }),
44+
})
45+
const response = await client.send(command)
46+
allSummaries.push(...(response.StackResourceSummaries ?? []))
47+
nextToken = response.NextToken
48+
} while (nextToken)
3949

40-
const resources = (response.StackResourceSummaries ?? []).map((r) => ({
50+
const resources = allSummaries.map((r) => ({
4151
logicalResourceId: r.LogicalResourceId ?? '',
4252
physicalResourceId: r.PhysicalResourceId,
4353
resourceType: r.ResourceType ?? '',

0 commit comments

Comments
 (0)