|
| 1 | +import { Capi } from '@tencent-sdk/capi'; |
| 2 | +import { CreateAlarmOptions, AlarmInfo, AlarmDetail } from './interface'; |
| 3 | +import APIS, { ActionType } from './apis'; |
| 4 | +import { pascalCaseProps, camelCaseProps } from '../../utils'; |
| 5 | +import { ApiError } from '../../utils/error'; |
| 6 | +import { ApiServiceType, CapiCredentials, RegionType } from '../interface'; |
| 7 | +import { formatAlarmOptions } from './utils'; |
| 8 | + |
| 9 | +export default class Alarm { |
| 10 | + credentials: CapiCredentials; |
| 11 | + capi: Capi; |
| 12 | + region: RegionType; |
| 13 | + |
| 14 | + constructor(credentials: CapiCredentials, region: RegionType = 'ap-guangzhou') { |
| 15 | + this.credentials = credentials; |
| 16 | + this.region = region; |
| 17 | + |
| 18 | + this.capi = new Capi({ |
| 19 | + Region: region, |
| 20 | + ServiceType: ApiServiceType.cls, |
| 21 | + SecretId: this.credentials.SecretId!, |
| 22 | + SecretKey: this.credentials.SecretKey!, |
| 23 | + Token: this.credentials.Token, |
| 24 | + }); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * 获取告警详情 |
| 29 | + * @param options 告警 id 或者 name |
| 30 | + * @returns 告警详情 |
| 31 | + */ |
| 32 | + async get({ id, name }: { id?: string; name?: string }): Promise<AlarmDetail | null> { |
| 33 | + if (!id && !name) { |
| 34 | + throw new ApiError({ |
| 35 | + type: 'PARAMETER_ERROR', |
| 36 | + message: `Alarm id or name is required`, |
| 37 | + }); |
| 38 | + } |
| 39 | + let filter = { |
| 40 | + Key: 'name', |
| 41 | + Values: [name], |
| 42 | + }; |
| 43 | + if (id) { |
| 44 | + filter = { |
| 45 | + Key: 'alarmId', |
| 46 | + Values: [id], |
| 47 | + }; |
| 48 | + } |
| 49 | + const { Alarms = [] }: { Alarms: AlarmInfo[] } = await this.request({ |
| 50 | + Action: 'DescribeAlarms', |
| 51 | + Filters: [filter], |
| 52 | + Offset: 0, |
| 53 | + Limit: 100, |
| 54 | + }); |
| 55 | + const detail = Alarms.find((alarm) => alarm.Name === name || alarm.AlarmId === id); |
| 56 | + if (detail) { |
| 57 | + return camelCaseProps(detail as AlarmInfo); |
| 58 | + } |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + async create(options: CreateAlarmOptions): Promise<CreateAlarmOptions & { id: string }> { |
| 63 | + const detail = await this.get({ name: options.name }); |
| 64 | + const alarmOptions = formatAlarmOptions(options); |
| 65 | + let id = ''; |
| 66 | + if (detail) { |
| 67 | + id = detail.alarmId; |
| 68 | + await this.request({ |
| 69 | + Action: 'ModifyAlarm', |
| 70 | + AlarmId: id, |
| 71 | + ...alarmOptions, |
| 72 | + }); |
| 73 | + } else { |
| 74 | + const { AlarmId } = await this.request({ |
| 75 | + Action: 'CreateAlarm', |
| 76 | + ...alarmOptions, |
| 77 | + }); |
| 78 | + id = AlarmId; |
| 79 | + } |
| 80 | + |
| 81 | + return { |
| 82 | + ...options, |
| 83 | + id, |
| 84 | + }; |
| 85 | + } |
| 86 | + |
| 87 | + async delete({ id, name }: { id?: string; name?: string }) { |
| 88 | + if (!id && !name) { |
| 89 | + throw new ApiError({ |
| 90 | + type: 'PARAMETER_ERROR', |
| 91 | + message: `Alarm id or name is required`, |
| 92 | + }); |
| 93 | + } |
| 94 | + if (id) { |
| 95 | + const detail = await this.get({ id }); |
| 96 | + if (detail) { |
| 97 | + await this.request({ |
| 98 | + Action: 'DeleteAlarm', |
| 99 | + AlarmId: id, |
| 100 | + }); |
| 101 | + } else { |
| 102 | + console.log(`Alarm ${id} not exist`); |
| 103 | + } |
| 104 | + } |
| 105 | + if (name) { |
| 106 | + const detail = await this.get({ name }); |
| 107 | + if (detail) { |
| 108 | + await this.request({ |
| 109 | + Action: 'DeleteAlarm', |
| 110 | + AlarmId: detail.alarmId, |
| 111 | + }); |
| 112 | + } else { |
| 113 | + console.log(`Alarm ${name} not exist`); |
| 114 | + } |
| 115 | + } |
| 116 | + return true; |
| 117 | + } |
| 118 | + |
| 119 | + async request({ Action, ...data }: { Action: ActionType; [key: string]: any }) { |
| 120 | + const result = await APIS[Action](this.capi, pascalCaseProps(data)); |
| 121 | + return result; |
| 122 | + } |
| 123 | +} |
0 commit comments