Skip to content

Commit 04330fc

Browse files
authored
feat(cockpit): create route to list all alerts OP-1289 (#558)
1 parent b25aded commit 04330fc

File tree

8 files changed

+442
-0
lines changed

8 files changed

+442
-0
lines changed

scaleway-async/scaleway_async/cockpit/v1/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from .types import GrafanaUserRole
66
from .types import ListDataSourcesRequestOrderBy
77
from .types import ListGrafanaUsersRequestOrderBy
8+
from .types import ListManagedAlertsRequestOrderBy
89
from .types import ListPlansRequestOrderBy
910
from .types import ListTokensRequestOrderBy
1011
from .types import PlanName
@@ -15,6 +16,7 @@
1516
from .types import DataSource
1617
from .types import GrafanaProductDashboard
1718
from .types import GrafanaUser
19+
from .types import Alert
1820
from .types import Plan
1921
from .types import Token
2022
from .types import Usage
@@ -35,6 +37,7 @@
3537
from .types import ListDataSourcesResponse
3638
from .types import ListGrafanaProductDashboardsResponse
3739
from .types import ListGrafanaUsersResponse
40+
from .types import ListManagedAlertsResponse
3841
from .types import ListPlansResponse
3942
from .types import ListTokensResponse
4043
from .types import RegionalApiCreateContactPointRequest
@@ -53,6 +56,7 @@
5356
from .types import RegionalApiGetUsageOverviewRequest
5457
from .types import RegionalApiListContactPointsRequest
5558
from .types import RegionalApiListDataSourcesRequest
59+
from .types import RegionalApiListManagedAlertsRequest
5660
from .types import RegionalApiListTokensRequest
5761
from .types import RegionalApiTriggerTestAlertRequest
5862
from .types import UsageOverview
@@ -65,6 +69,7 @@
6569
"GrafanaUserRole",
6670
"ListDataSourcesRequestOrderBy",
6771
"ListGrafanaUsersRequestOrderBy",
72+
"ListManagedAlertsRequestOrderBy",
6873
"ListPlansRequestOrderBy",
6974
"ListTokensRequestOrderBy",
7075
"PlanName",
@@ -75,6 +80,7 @@
7580
"DataSource",
7681
"GrafanaProductDashboard",
7782
"GrafanaUser",
83+
"Alert",
7884
"Plan",
7985
"Token",
8086
"Usage",
@@ -95,6 +101,7 @@
95101
"ListDataSourcesResponse",
96102
"ListGrafanaProductDashboardsResponse",
97103
"ListGrafanaUsersResponse",
104+
"ListManagedAlertsResponse",
98105
"ListPlansResponse",
99106
"ListTokensResponse",
100107
"RegionalApiCreateContactPointRequest",
@@ -113,6 +120,7 @@
113120
"RegionalApiGetUsageOverviewRequest",
114121
"RegionalApiListContactPointsRequest",
115122
"RegionalApiListDataSourcesRequest",
123+
"RegionalApiListManagedAlertsRequest",
116124
"RegionalApiListTokensRequest",
117125
"RegionalApiTriggerTestAlertRequest",
118126
"UsageOverview",

scaleway-async/scaleway_async/cockpit/v1/api.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
GrafanaUserRole,
1818
ListDataSourcesRequestOrderBy,
1919
ListGrafanaUsersRequestOrderBy,
20+
ListManagedAlertsRequestOrderBy,
2021
ListPlansRequestOrderBy,
2122
ListTokensRequestOrderBy,
2223
PlanName,
2324
TokenScope,
25+
Alert,
2426
AlertManager,
2527
ContactPoint,
2628
ContactPointEmail,
@@ -36,6 +38,7 @@
3638
ListDataSourcesResponse,
3739
ListGrafanaProductDashboardsResponse,
3840
ListGrafanaUsersResponse,
41+
ListManagedAlertsResponse,
3942
ListPlansResponse,
4043
ListTokensResponse,
4144
Plan,
@@ -64,6 +67,7 @@
6467
unmarshal_ListDataSourcesResponse,
6568
unmarshal_ListGrafanaProductDashboardsResponse,
6669
unmarshal_ListGrafanaUsersResponse,
70+
unmarshal_ListManagedAlertsResponse,
6771
unmarshal_ListPlansResponse,
6872
unmarshal_ListTokensResponse,
6973
unmarshal_UsageOverview,
@@ -1289,6 +1293,87 @@ async def delete_contact_point(
12891293

12901294
self._throw_on_error(res)
12911295

1296+
async def list_managed_alerts(
1297+
self,
1298+
*,
1299+
region: Optional[Region] = None,
1300+
page: Optional[int] = None,
1301+
page_size: Optional[int] = None,
1302+
order_by: Optional[ListManagedAlertsRequestOrderBy] = None,
1303+
project_id: Optional[str] = None,
1304+
) -> ListManagedAlertsResponse:
1305+
"""
1306+
List managed alerts.
1307+
List all managed alerts for the specified Project.
1308+
:param region: Region to target. If none is passed will use default region from the config.
1309+
:param page: Page number to return, from the paginated results.
1310+
:param page_size: Number of data sources to return per page.
1311+
:param order_by: Sort order for data sources in the response.
1312+
:param project_id: Project ID to filter for, only data sources from this Project will be returned.
1313+
:return: :class:`ListManagedAlertsResponse <ListManagedAlertsResponse>`
1314+
1315+
Usage:
1316+
::
1317+
1318+
result = await api.list_managed_alerts()
1319+
"""
1320+
1321+
param_region = validate_path_param(
1322+
"region", region or self.client.default_region
1323+
)
1324+
1325+
res = self._request(
1326+
"GET",
1327+
f"/cockpit/v1/regions/{param_region}/managed-alerts",
1328+
params={
1329+
"order_by": order_by,
1330+
"page": page,
1331+
"page_size": page_size or self.client.default_page_size,
1332+
"project_id": project_id or self.client.default_project_id,
1333+
},
1334+
)
1335+
1336+
self._throw_on_error(res)
1337+
return unmarshal_ListManagedAlertsResponse(res.json())
1338+
1339+
async def list_managed_alerts_all(
1340+
self,
1341+
*,
1342+
region: Optional[Region] = None,
1343+
page: Optional[int] = None,
1344+
page_size: Optional[int] = None,
1345+
order_by: Optional[ListManagedAlertsRequestOrderBy] = None,
1346+
project_id: Optional[str] = None,
1347+
) -> List[Alert]:
1348+
"""
1349+
List managed alerts.
1350+
List all managed alerts for the specified Project.
1351+
:param region: Region to target. If none is passed will use default region from the config.
1352+
:param page: Page number to return, from the paginated results.
1353+
:param page_size: Number of data sources to return per page.
1354+
:param order_by: Sort order for data sources in the response.
1355+
:param project_id: Project ID to filter for, only data sources from this Project will be returned.
1356+
:return: :class:`List[Alert] <List[Alert]>`
1357+
1358+
Usage:
1359+
::
1360+
1361+
result = await api.list_managed_alerts_all()
1362+
"""
1363+
1364+
return await fetch_all_pages_async(
1365+
type=ListManagedAlertsResponse,
1366+
key="alerts",
1367+
fetcher=self.list_managed_alerts,
1368+
args={
1369+
"region": region,
1370+
"page": page,
1371+
"page_size": page_size,
1372+
"order_by": order_by,
1373+
"project_id": project_id,
1374+
},
1375+
)
1376+
12921377
async def enable_managed_alerts(
12931378
self,
12941379
*,

scaleway-async/scaleway_async/cockpit/v1/marshalling.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
ListDataSourcesResponse,
2525
ListGrafanaProductDashboardsResponse,
2626
ListGrafanaUsersResponse,
27+
Alert,
28+
ListManagedAlertsResponse,
2729
ListPlansResponse,
2830
ListTokensResponse,
2931
Usage,
@@ -433,6 +435,58 @@ def unmarshal_ListGrafanaUsersResponse(data: Any) -> ListGrafanaUsersResponse:
433435
return ListGrafanaUsersResponse(**args)
434436

435437

438+
def unmarshal_Alert(data: Any) -> Alert:
439+
if not isinstance(data, dict):
440+
raise TypeError(
441+
"Unmarshalling the type 'Alert' failed as data isn't a dictionary."
442+
)
443+
444+
args: Dict[str, Any] = {}
445+
446+
field = data.get("product_family", None)
447+
if field is not None:
448+
args["product_family"] = field
449+
450+
field = data.get("product", None)
451+
if field is not None:
452+
args["product"] = field
453+
454+
field = data.get("name", None)
455+
if field is not None:
456+
args["name"] = field
457+
458+
field = data.get("rule", None)
459+
if field is not None:
460+
args["rule"] = field
461+
462+
field = data.get("description", None)
463+
if field is not None:
464+
args["description"] = field
465+
466+
return Alert(**args)
467+
468+
469+
def unmarshal_ListManagedAlertsResponse(data: Any) -> ListManagedAlertsResponse:
470+
if not isinstance(data, dict):
471+
raise TypeError(
472+
"Unmarshalling the type 'ListManagedAlertsResponse' failed as data isn't a dictionary."
473+
)
474+
475+
args: Dict[str, Any] = {}
476+
477+
field = data.get("total_count", None)
478+
if field is not None:
479+
args["total_count"] = field
480+
481+
field = data.get("alerts", None)
482+
if field is not None:
483+
args["alerts"] = (
484+
[unmarshal_Alert(v) for v in field] if field is not None else None
485+
)
486+
487+
return ListManagedAlertsResponse(**args)
488+
489+
436490
def unmarshal_ListPlansResponse(data: Any) -> ListPlansResponse:
437491
if not isinstance(data, dict):
438492
raise TypeError(

scaleway-async/scaleway_async/cockpit/v1/types.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ def __str__(self) -> str:
6363
return str(self.value)
6464

6565

66+
class ListManagedAlertsRequestOrderBy(str, Enum, metaclass=StrEnumMeta):
67+
CREATED_AT_ASC = "created_at_asc"
68+
CREATED_AT_DESC = "created_at_desc"
69+
NAME_ASC = "name_asc"
70+
NAME_DESC = "name_desc"
71+
TYPE_ASC = "type_asc"
72+
TYPE_DESC = "type_desc"
73+
74+
def __str__(self) -> str:
75+
return str(self.value)
76+
77+
6678
class ListPlansRequestOrderBy(str, Enum, metaclass=StrEnumMeta):
6779
NAME_ASC = "name_asc"
6880
NAME_DESC = "name_desc"
@@ -251,6 +263,19 @@ class GrafanaUser:
251263
"""
252264

253265

266+
@dataclass
267+
class Alert:
268+
product_family: str
269+
270+
product: str
271+
272+
name: str
273+
274+
rule: str
275+
276+
description: str
277+
278+
254279
@dataclass
255280
class Plan:
256281
"""
@@ -708,6 +733,23 @@ class ListGrafanaUsersResponse:
708733
"""
709734

710735

736+
@dataclass
737+
class ListManagedAlertsResponse:
738+
"""
739+
Response returned when listing data sources.
740+
"""
741+
742+
total_count: int
743+
"""
744+
Total count of data sources matching the request.
745+
"""
746+
747+
alerts: List[Alert]
748+
"""
749+
Alerts matching the request within the pagination.
750+
"""
751+
752+
711753
@dataclass
712754
class ListPlansResponse:
713755
"""
@@ -1068,6 +1110,38 @@ class RegionalApiListDataSourcesRequest:
10681110
"""
10691111

10701112

1113+
@dataclass
1114+
class RegionalApiListManagedAlertsRequest:
1115+
"""
1116+
Enable the sending of managed alerts.
1117+
"""
1118+
1119+
region: Optional[Region]
1120+
"""
1121+
Region to target. If none is passed will use default region from the config.
1122+
"""
1123+
1124+
page: Optional[int]
1125+
"""
1126+
Page number to return, from the paginated results.
1127+
"""
1128+
1129+
page_size: Optional[int]
1130+
"""
1131+
Number of data sources to return per page.
1132+
"""
1133+
1134+
order_by: Optional[ListManagedAlertsRequestOrderBy]
1135+
"""
1136+
Sort order for data sources in the response.
1137+
"""
1138+
1139+
project_id: Optional[str]
1140+
"""
1141+
Project ID to filter for, only data sources from this Project will be returned.
1142+
"""
1143+
1144+
10711145
@dataclass
10721146
class RegionalApiListTokensRequest:
10731147
"""

0 commit comments

Comments
 (0)