-
Notifications
You must be signed in to change notification settings - Fork 3.6k
feat(cloudwatch): add mute and unmute alarm operations #4602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+450
−3
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { CloudWatchClient, DisableAlarmActionsCommand } from '@aws-sdk/client-cloudwatch' | ||
| import { createLogger } from '@sim/logger' | ||
| import { toError } from '@sim/utils/errors' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { awsCloudwatchMuteAlarmContract } from '@/lib/api/contracts/tools/aws/cloudwatch-mute-alarm' | ||
| import { parseToolRequest } from '@/lib/api/server' | ||
| import { checkInternalAuth } from '@/lib/auth/hybrid' | ||
| import { withRouteHandler } from '@/lib/core/utils/with-route-handler' | ||
|
|
||
| const logger = createLogger('CloudWatchMuteAlarm') | ||
|
|
||
| export const POST = withRouteHandler(async (request: NextRequest) => { | ||
| try { | ||
| const auth = await checkInternalAuth(request) | ||
| if (!auth.success || !auth.userId) { | ||
| return NextResponse.json({ error: auth.error || 'Unauthorized' }, { status: 401 }) | ||
| } | ||
|
|
||
| const parsed = await parseToolRequest(awsCloudwatchMuteAlarmContract, request, { | ||
| errorFormat: 'details', | ||
| logger, | ||
| }) | ||
| if (!parsed.success) return parsed.response | ||
| const validatedData = parsed.data.body | ||
|
|
||
| logger.info(`Muting ${validatedData.alarmNames.length} CloudWatch alarm(s)`) | ||
|
|
||
| const client = new CloudWatchClient({ | ||
| region: validatedData.region, | ||
| credentials: { | ||
| accessKeyId: validatedData.accessKeyId, | ||
| secretAccessKey: validatedData.secretAccessKey, | ||
| }, | ||
| }) | ||
|
|
||
| try { | ||
| const command = new DisableAlarmActionsCommand({ | ||
| AlarmNames: validatedData.alarmNames, | ||
| }) | ||
|
|
||
| await client.send(command) | ||
|
|
||
| logger.info(`Successfully muted ${validatedData.alarmNames.length} alarm(s)`) | ||
|
|
||
| return NextResponse.json({ | ||
| success: true, | ||
| output: { | ||
| success: true, | ||
| alarmNames: validatedData.alarmNames, | ||
| }, | ||
| }) | ||
| } finally { | ||
| client.destroy() | ||
| } | ||
| } catch (error) { | ||
| logger.error('MuteAlarm failed', { error: toError(error).message }) | ||
| return NextResponse.json( | ||
| { error: `Failed to mute CloudWatch alarm: ${toError(error).message}` }, | ||
| { status: 500 } | ||
| ) | ||
| } | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { CloudWatchClient, EnableAlarmActionsCommand } from '@aws-sdk/client-cloudwatch' | ||
| import { createLogger } from '@sim/logger' | ||
| import { toError } from '@sim/utils/errors' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { awsCloudwatchUnmuteAlarmContract } from '@/lib/api/contracts/tools/aws/cloudwatch-unmute-alarm' | ||
| import { parseToolRequest } from '@/lib/api/server' | ||
| import { checkInternalAuth } from '@/lib/auth/hybrid' | ||
| import { withRouteHandler } from '@/lib/core/utils/with-route-handler' | ||
|
|
||
| const logger = createLogger('CloudWatchUnmuteAlarm') | ||
|
|
||
| export const POST = withRouteHandler(async (request: NextRequest) => { | ||
| try { | ||
| const auth = await checkInternalAuth(request) | ||
| if (!auth.success || !auth.userId) { | ||
| return NextResponse.json({ error: auth.error || 'Unauthorized' }, { status: 401 }) | ||
| } | ||
|
|
||
| const parsed = await parseToolRequest(awsCloudwatchUnmuteAlarmContract, request, { | ||
| errorFormat: 'details', | ||
| logger, | ||
| }) | ||
| if (!parsed.success) return parsed.response | ||
| const validatedData = parsed.data.body | ||
|
|
||
| logger.info(`Unmuting ${validatedData.alarmNames.length} CloudWatch alarm(s)`) | ||
|
|
||
| const client = new CloudWatchClient({ | ||
| region: validatedData.region, | ||
| credentials: { | ||
| accessKeyId: validatedData.accessKeyId, | ||
| secretAccessKey: validatedData.secretAccessKey, | ||
| }, | ||
| }) | ||
|
|
||
| try { | ||
| const command = new EnableAlarmActionsCommand({ | ||
| AlarmNames: validatedData.alarmNames, | ||
| }) | ||
|
|
||
| await client.send(command) | ||
|
|
||
| logger.info(`Successfully unmuted ${validatedData.alarmNames.length} alarm(s)`) | ||
|
|
||
| return NextResponse.json({ | ||
| success: true, | ||
| output: { | ||
| success: true, | ||
| alarmNames: validatedData.alarmNames, | ||
| }, | ||
| }) | ||
| } finally { | ||
| client.destroy() | ||
| } | ||
| } catch (error) { | ||
| logger.error('UnmuteAlarm failed', { error: toError(error).message }) | ||
| return NextResponse.json( | ||
| { error: `Failed to unmute CloudWatch alarm: ${toError(error).message}` }, | ||
| { status: 500 } | ||
| ) | ||
| } | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
apps/sim/lib/api/contracts/tools/aws/cloudwatch-mute-alarm.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import { z } from 'zod' | ||
| import type { | ||
| ContractBody, | ||
| ContractBodyInput, | ||
| ContractJsonResponse, | ||
| } from '@/lib/api/contracts/types' | ||
| import { defineRouteContract } from '@/lib/api/contracts/types' | ||
| import { validateAwsRegion } from '@/lib/core/security/input-validation' | ||
|
|
||
| const MuteAlarmSchema = z.object({ | ||
| region: z | ||
| .string() | ||
| .min(1, 'AWS region is required') | ||
| .refine((v) => validateAwsRegion(v).isValid, { | ||
| message: 'Invalid AWS region format (e.g., us-east-1, eu-west-2)', | ||
| }), | ||
| accessKeyId: z.string().min(1, 'AWS access key ID is required'), | ||
| secretAccessKey: z.string().min(1, 'AWS secret access key is required'), | ||
| alarmNames: z | ||
| .array(z.string().min(1, 'Alarm name cannot be empty')) | ||
| .min(1, 'At least one alarm name is required') | ||
| .max(100, 'At most 100 alarm names are allowed per request'), | ||
| }) | ||
|
|
||
| const MuteAlarmResponseSchema = z.object({ | ||
| success: z.literal(true), | ||
| output: z.object({ | ||
| success: z.literal(true), | ||
| alarmNames: z.array(z.string()), | ||
| }), | ||
| }) | ||
|
|
||
| export const awsCloudwatchMuteAlarmContract = defineRouteContract({ | ||
| method: 'POST', | ||
| path: '/api/tools/cloudwatch/mute-alarm', | ||
| body: MuteAlarmSchema, | ||
| response: { mode: 'json', schema: MuteAlarmResponseSchema }, | ||
| }) | ||
| export type AwsCloudwatchMuteAlarmRequest = ContractBodyInput<typeof awsCloudwatchMuteAlarmContract> | ||
| export type AwsCloudwatchMuteAlarmBody = ContractBody<typeof awsCloudwatchMuteAlarmContract> | ||
| export type AwsCloudwatchMuteAlarmResponse = ContractJsonResponse< | ||
| typeof awsCloudwatchMuteAlarmContract | ||
| > | ||
45 changes: 45 additions & 0 deletions
45
apps/sim/lib/api/contracts/tools/aws/cloudwatch-unmute-alarm.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { z } from 'zod' | ||
| import type { | ||
| ContractBody, | ||
| ContractBodyInput, | ||
| ContractJsonResponse, | ||
| } from '@/lib/api/contracts/types' | ||
| import { defineRouteContract } from '@/lib/api/contracts/types' | ||
| import { validateAwsRegion } from '@/lib/core/security/input-validation' | ||
|
|
||
| const UnmuteAlarmSchema = z.object({ | ||
| region: z | ||
| .string() | ||
| .min(1, 'AWS region is required') | ||
| .refine((v) => validateAwsRegion(v).isValid, { | ||
| message: 'Invalid AWS region format (e.g., us-east-1, eu-west-2)', | ||
| }), | ||
| accessKeyId: z.string().min(1, 'AWS access key ID is required'), | ||
| secretAccessKey: z.string().min(1, 'AWS secret access key is required'), | ||
| alarmNames: z | ||
| .array(z.string().min(1, 'Alarm name cannot be empty')) | ||
| .min(1, 'At least one alarm name is required') | ||
| .max(100, 'At most 100 alarm names are allowed per request'), | ||
| }) | ||
|
|
||
| const UnmuteAlarmResponseSchema = z.object({ | ||
| success: z.literal(true), | ||
| output: z.object({ | ||
| success: z.literal(true), | ||
| alarmNames: z.array(z.string()), | ||
| }), | ||
| }) | ||
|
|
||
| export const awsCloudwatchUnmuteAlarmContract = defineRouteContract({ | ||
| method: 'POST', | ||
| path: '/api/tools/cloudwatch/unmute-alarm', | ||
| body: UnmuteAlarmSchema, | ||
| response: { mode: 'json', schema: UnmuteAlarmResponseSchema }, | ||
| }) | ||
| export type AwsCloudwatchUnmuteAlarmRequest = ContractBodyInput< | ||
| typeof awsCloudwatchUnmuteAlarmContract | ||
| > | ||
| export type AwsCloudwatchUnmuteAlarmBody = ContractBody<typeof awsCloudwatchUnmuteAlarmContract> | ||
| export type AwsCloudwatchUnmuteAlarmResponse = ContractJsonResponse< | ||
| typeof awsCloudwatchUnmuteAlarmContract | ||
| > |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.