Skip to content

Commit bb46893

Browse files
feat(cockpit): add support for RegionalApiListAlertsRequest (#1806)
Co-authored-by: Rémy Léone <[email protected]>
1 parent 2fa209a commit bb46893

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

packages/clients/src/api/cockpit/v1/api.gen.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
unmarshalGrafana,
3131
unmarshalGrafanaProductDashboard,
3232
unmarshalGrafanaUser,
33+
unmarshalListAlertsResponse,
3334
unmarshalListContactPointsResponse,
3435
unmarshalListDataSourcesResponse,
3536
unmarshalListGrafanaProductDashboardsResponse,
@@ -60,6 +61,7 @@ import type {
6061
Grafana,
6162
GrafanaProductDashboard,
6263
GrafanaUser,
64+
ListAlertsResponse,
6365
ListContactPointsResponse,
6466
ListDataSourcesResponse,
6567
ListGrafanaProductDashboardsResponse,
@@ -83,6 +85,7 @@ import type {
8385
RegionalApiGetDataSourceRequest,
8486
RegionalApiGetTokenRequest,
8587
RegionalApiGetUsageOverviewRequest,
88+
RegionalApiListAlertsRequest,
8689
RegionalApiListContactPointsRequest,
8790
RegionalApiListDataSourcesRequest,
8891
RegionalApiListManagedAlertsRequest,
@@ -868,6 +871,31 @@ export class RegionalAPI extends ParentAPI {
868871
request: Readonly<RegionalApiListManagedAlertsRequest> = {},
869872
) => enrichForPagination('alerts', this.pageOfListManagedAlerts, request)
870873

874+
/**
875+
* List alerts. List preconfigured and/or custom alerts for the specified
876+
* Project.
877+
*
878+
* @param request - The request {@link RegionalApiListAlertsRequest}
879+
* @returns A Promise of ListAlertsResponse
880+
*/
881+
listAlerts = (request: Readonly<RegionalApiListAlertsRequest> = {}) =>
882+
this.client.fetch<ListAlertsResponse>(
883+
{
884+
method: 'GET',
885+
path: `/cockpit/v1/regions/${validatePathParam('region', request.region ?? this.client.settings.defaultRegion)}/alerts`,
886+
urlParams: urlParams(
887+
['is_enabled', request.isEnabled],
888+
['is_preconfigured', request.isPreconfigured],
889+
[
890+
'project_id',
891+
request.projectId ?? this.client.settings.defaultProjectId,
892+
],
893+
['state', request.state],
894+
),
895+
},
896+
unmarshalListAlertsResponse,
897+
)
898+
871899
/**
872900
* Enable managed alerts. Enable the sending of managed alerts for the
873901
* specified Project. Managed alerts are predefined alerts that apply to

packages/clients/src/api/cockpit/v1/index.gen.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ export { GlobalAPI, RegionalAPI } from './api.gen'
44
export type {
55
Alert,
66
AlertManager,
7+
AnyAlert,
8+
AnyAlertState,
79
ContactPoint,
810
ContactPointEmail,
911
DataSource,
@@ -26,6 +28,7 @@ export type {
2628
GrafanaProductDashboard,
2729
GrafanaUser,
2830
GrafanaUserRole,
31+
ListAlertsResponse,
2932
ListContactPointsResponse,
3033
ListDataSourcesRequestOrderBy,
3134
ListDataSourcesResponse,
@@ -55,6 +58,7 @@ export type {
5558
RegionalApiGetDataSourceRequest,
5659
RegionalApiGetTokenRequest,
5760
RegionalApiGetUsageOverviewRequest,
61+
RegionalApiListAlertsRequest,
5862
RegionalApiListContactPointsRequest,
5963
RegionalApiListDataSourcesRequest,
6064
RegionalApiListManagedAlertsRequest,

packages/clients/src/api/cockpit/v1/marshalling.gen.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { DefaultValues } from '../../../bridge'
1010
import type {
1111
Alert,
1212
AlertManager,
13+
AnyAlert,
1314
ContactPoint,
1415
ContactPointEmail,
1516
DataSource,
@@ -22,6 +23,7 @@ import type {
2223
Grafana,
2324
GrafanaProductDashboard,
2425
GrafanaUser,
26+
ListAlertsResponse,
2527
ListContactPointsResponse,
2628
ListDataSourcesResponse,
2729
ListGrafanaProductDashboardsResponse,
@@ -236,6 +238,39 @@ export const unmarshalGrafana = (data: unknown): Grafana => {
236238
} as Grafana
237239
}
238240

241+
const unmarshalAnyAlert = (data: unknown): AnyAlert => {
242+
if (!isJSONObject(data)) {
243+
throw new TypeError(
244+
`Unmarshalling the type 'AnyAlert' failed as data isn't a dictionary.`,
245+
)
246+
}
247+
248+
return {
249+
annotations: data.annotations,
250+
duration: data.duration,
251+
name: data.name,
252+
preconfigured: data.preconfigured,
253+
region: data.region,
254+
rule: data.rule,
255+
state: data.state,
256+
} as AnyAlert
257+
}
258+
259+
export const unmarshalListAlertsResponse = (
260+
data: unknown,
261+
): ListAlertsResponse => {
262+
if (!isJSONObject(data)) {
263+
throw new TypeError(
264+
`Unmarshalling the type 'ListAlertsResponse' failed as data isn't a dictionary.`,
265+
)
266+
}
267+
268+
return {
269+
alerts: unmarshalArrayOfObject(data.alerts, unmarshalAnyAlert),
270+
totalCount: data.total_count,
271+
} as ListAlertsResponse
272+
}
273+
239274
export const unmarshalListContactPointsResponse = (
240275
data: unknown,
241276
): ListContactPointsResponse => {

packages/clients/src/api/cockpit/v1/types.gen.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
// If you have any remark or suggestion do not hesitate to open an issue.
33
import type { Region as ScwRegion } from '../../../bridge'
44

5+
export type AnyAlertState =
6+
| 'unknown_state'
7+
| 'disabled'
8+
| 'enabled'
9+
| 'pending'
10+
| 'firing'
11+
512
export type DataSourceOrigin =
613
| 'unknown_origin'
714
| 'scaleway'
@@ -64,6 +71,20 @@ export interface GetConfigResponseRetention {
6471
defaultDays: number
6572
}
6673

74+
export interface AnyAlert {
75+
/**
76+
* Region to target. If none is passed will use default region from the
77+
* config.
78+
*/
79+
region: ScwRegion
80+
preconfigured: boolean
81+
name: string
82+
rule: string
83+
duration: string
84+
state: AnyAlertState
85+
annotations: Record<string, string>
86+
}
87+
6788
/** Contact point. */
6889
export interface ContactPoint {
6990
/**
@@ -329,6 +350,14 @@ export interface Grafana {
329350
grafanaUrl: string
330351
}
331352

353+
/** Retrieve a list of alerts matching the request. */
354+
export interface ListAlertsResponse {
355+
/** Total count of alerts matching the request. */
356+
totalCount: number
357+
/** List of alerts matching the applied filters. */
358+
alerts: AnyAlert[]
359+
}
360+
332361
/** Response returned when listing contact points. */
333362
export interface ListContactPointsResponse {
334363
/** Total count of contact points associated with the default receiver. */
@@ -581,6 +610,34 @@ export type RegionalApiGetUsageOverviewRequest = {
581610
interval?: string
582611
}
583612

613+
/** Retrieve a list of alerts. */
614+
export type RegionalApiListAlertsRequest = {
615+
/**
616+
* Region to target. If none is passed will use default region from the
617+
* config.
618+
*/
619+
region?: ScwRegion
620+
/** Project ID to filter for, only alerts from this Project will be returned. */
621+
projectId?: string
622+
/**
623+
* True returns only enabled alerts. False returns only disabled alerts. If
624+
* omitted, no alert filtering is applied. Other filters may still apply.
625+
*/
626+
isEnabled?: boolean
627+
/**
628+
* True returns only preconfigured alerts. False returns only custom alerts.
629+
* If omitted, no filtering is applied on alert types. Other filters may still
630+
* apply.
631+
*/
632+
isPreconfigured?: boolean
633+
/**
634+
* Valid values to filter on are `disabled`, `enabled`, `pending` and
635+
* `firing`. If omitted, no filtering is applied on alert states. Other
636+
* filters may still apply.
637+
*/
638+
state?: AnyAlertState
639+
}
640+
584641
/** List contact points. */
585642
export type RegionalApiListContactPointsRequest = {
586643
/**

0 commit comments

Comments
 (0)