Skip to content

feat(cockpit): create route to list all alerts OP-1289 #558

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
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions scaleway-async/scaleway_async/cockpit/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .types import GrafanaUserRole
from .types import ListDataSourcesRequestOrderBy
from .types import ListGrafanaUsersRequestOrderBy
from .types import ListManagedAlertsRequestOrderBy
from .types import ListPlansRequestOrderBy
from .types import ListTokensRequestOrderBy
from .types import PlanName
Expand All @@ -15,6 +16,7 @@
from .types import DataSource
from .types import GrafanaProductDashboard
from .types import GrafanaUser
from .types import Alert
from .types import Plan
from .types import Token
from .types import Usage
Expand All @@ -35,6 +37,7 @@
from .types import ListDataSourcesResponse
from .types import ListGrafanaProductDashboardsResponse
from .types import ListGrafanaUsersResponse
from .types import ListManagedAlertsResponse
from .types import ListPlansResponse
from .types import ListTokensResponse
from .types import RegionalApiCreateContactPointRequest
Expand All @@ -53,6 +56,7 @@
from .types import RegionalApiGetUsageOverviewRequest
from .types import RegionalApiListContactPointsRequest
from .types import RegionalApiListDataSourcesRequest
from .types import RegionalApiListManagedAlertsRequest
from .types import RegionalApiListTokensRequest
from .types import RegionalApiTriggerTestAlertRequest
from .types import UsageOverview
Expand All @@ -65,6 +69,7 @@
"GrafanaUserRole",
"ListDataSourcesRequestOrderBy",
"ListGrafanaUsersRequestOrderBy",
"ListManagedAlertsRequestOrderBy",
"ListPlansRequestOrderBy",
"ListTokensRequestOrderBy",
"PlanName",
Expand All @@ -75,6 +80,7 @@
"DataSource",
"GrafanaProductDashboard",
"GrafanaUser",
"Alert",
"Plan",
"Token",
"Usage",
Expand All @@ -95,6 +101,7 @@
"ListDataSourcesResponse",
"ListGrafanaProductDashboardsResponse",
"ListGrafanaUsersResponse",
"ListManagedAlertsResponse",
"ListPlansResponse",
"ListTokensResponse",
"RegionalApiCreateContactPointRequest",
Expand All @@ -113,6 +120,7 @@
"RegionalApiGetUsageOverviewRequest",
"RegionalApiListContactPointsRequest",
"RegionalApiListDataSourcesRequest",
"RegionalApiListManagedAlertsRequest",
"RegionalApiListTokensRequest",
"RegionalApiTriggerTestAlertRequest",
"UsageOverview",
Expand Down
85 changes: 85 additions & 0 deletions scaleway-async/scaleway_async/cockpit/v1/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
GrafanaUserRole,
ListDataSourcesRequestOrderBy,
ListGrafanaUsersRequestOrderBy,
ListManagedAlertsRequestOrderBy,
ListPlansRequestOrderBy,
ListTokensRequestOrderBy,
PlanName,
TokenScope,
Alert,
AlertManager,
ContactPoint,
ContactPointEmail,
Expand All @@ -36,6 +38,7 @@
ListDataSourcesResponse,
ListGrafanaProductDashboardsResponse,
ListGrafanaUsersResponse,
ListManagedAlertsResponse,
ListPlansResponse,
ListTokensResponse,
Plan,
Expand Down Expand Up @@ -64,6 +67,7 @@
unmarshal_ListDataSourcesResponse,
unmarshal_ListGrafanaProductDashboardsResponse,
unmarshal_ListGrafanaUsersResponse,
unmarshal_ListManagedAlertsResponse,
unmarshal_ListPlansResponse,
unmarshal_ListTokensResponse,
unmarshal_UsageOverview,
Expand Down Expand Up @@ -1289,6 +1293,87 @@ async def delete_contact_point(

self._throw_on_error(res)

async def list_managed_alerts(
self,
*,
region: Optional[Region] = None,
page: Optional[int] = None,
page_size: Optional[int] = None,
order_by: Optional[ListManagedAlertsRequestOrderBy] = None,
project_id: Optional[str] = None,
) -> ListManagedAlertsResponse:
"""
List managed alerts.
List all managed alerts for the specified Project.
:param region: Region to target. If none is passed will use default region from the config.
:param page: Page number to return, from the paginated results.
:param page_size: Number of data sources to return per page.
:param order_by: Sort order for data sources in the response.
:param project_id: Project ID to filter for, only data sources from this Project will be returned.
:return: :class:`ListManagedAlertsResponse <ListManagedAlertsResponse>`

Usage:
::

result = await api.list_managed_alerts()
"""

param_region = validate_path_param(
"region", region or self.client.default_region
)

res = self._request(
"GET",
f"/cockpit/v1/regions/{param_region}/managed-alerts",
params={
"order_by": order_by,
"page": page,
"page_size": page_size or self.client.default_page_size,
"project_id": project_id or self.client.default_project_id,
},
)

self._throw_on_error(res)
return unmarshal_ListManagedAlertsResponse(res.json())

async def list_managed_alerts_all(
self,
*,
region: Optional[Region] = None,
page: Optional[int] = None,
page_size: Optional[int] = None,
order_by: Optional[ListManagedAlertsRequestOrderBy] = None,
project_id: Optional[str] = None,
) -> List[Alert]:
"""
List managed alerts.
List all managed alerts for the specified Project.
:param region: Region to target. If none is passed will use default region from the config.
:param page: Page number to return, from the paginated results.
:param page_size: Number of data sources to return per page.
:param order_by: Sort order for data sources in the response.
:param project_id: Project ID to filter for, only data sources from this Project will be returned.
:return: :class:`List[Alert] <List[Alert]>`

Usage:
::

result = await api.list_managed_alerts_all()
"""

return await fetch_all_pages_async(
type=ListManagedAlertsResponse,
key="alerts",
fetcher=self.list_managed_alerts,
args={
"region": region,
"page": page,
"page_size": page_size,
"order_by": order_by,
"project_id": project_id,
},
)

async def enable_managed_alerts(
self,
*,
Expand Down
54 changes: 54 additions & 0 deletions scaleway-async/scaleway_async/cockpit/v1/marshalling.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
ListDataSourcesResponse,
ListGrafanaProductDashboardsResponse,
ListGrafanaUsersResponse,
Alert,
ListManagedAlertsResponse,
ListPlansResponse,
ListTokensResponse,
Usage,
Expand Down Expand Up @@ -433,6 +435,58 @@ def unmarshal_ListGrafanaUsersResponse(data: Any) -> ListGrafanaUsersResponse:
return ListGrafanaUsersResponse(**args)


def unmarshal_Alert(data: Any) -> Alert:
if not isinstance(data, dict):
raise TypeError(
"Unmarshalling the type 'Alert' failed as data isn't a dictionary."
)

args: Dict[str, Any] = {}

field = data.get("product_family", None)
if field is not None:
args["product_family"] = field

field = data.get("product", None)
if field is not None:
args["product"] = field

field = data.get("name", None)
if field is not None:
args["name"] = field

field = data.get("rule", None)
if field is not None:
args["rule"] = field

field = data.get("description", None)
if field is not None:
args["description"] = field

return Alert(**args)


def unmarshal_ListManagedAlertsResponse(data: Any) -> ListManagedAlertsResponse:
if not isinstance(data, dict):
raise TypeError(
"Unmarshalling the type 'ListManagedAlertsResponse' failed as data isn't a dictionary."
)

args: Dict[str, Any] = {}

field = data.get("total_count", None)
if field is not None:
args["total_count"] = field

field = data.get("alerts", None)
if field is not None:
args["alerts"] = (
[unmarshal_Alert(v) for v in field] if field is not None else None
)

return ListManagedAlertsResponse(**args)


def unmarshal_ListPlansResponse(data: Any) -> ListPlansResponse:
if not isinstance(data, dict):
raise TypeError(
Expand Down
74 changes: 74 additions & 0 deletions scaleway-async/scaleway_async/cockpit/v1/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ def __str__(self) -> str:
return str(self.value)


class ListManagedAlertsRequestOrderBy(str, Enum, metaclass=StrEnumMeta):
CREATED_AT_ASC = "created_at_asc"
CREATED_AT_DESC = "created_at_desc"
NAME_ASC = "name_asc"
NAME_DESC = "name_desc"
TYPE_ASC = "type_asc"
TYPE_DESC = "type_desc"

def __str__(self) -> str:
return str(self.value)


class ListPlansRequestOrderBy(str, Enum, metaclass=StrEnumMeta):
NAME_ASC = "name_asc"
NAME_DESC = "name_desc"
Expand Down Expand Up @@ -251,6 +263,19 @@ class GrafanaUser:
"""


@dataclass
class Alert:
product_family: str

product: str

name: str

rule: str

description: str


@dataclass
class Plan:
"""
Expand Down Expand Up @@ -708,6 +733,23 @@ class ListGrafanaUsersResponse:
"""


@dataclass
class ListManagedAlertsResponse:
"""
Response returned when listing data sources.
"""

total_count: int
"""
Total count of data sources matching the request.
"""

alerts: List[Alert]
"""
Alerts matching the request within the pagination.
"""


@dataclass
class ListPlansResponse:
"""
Expand Down Expand Up @@ -1068,6 +1110,38 @@ class RegionalApiListDataSourcesRequest:
"""


@dataclass
class RegionalApiListManagedAlertsRequest:
"""
Enable the sending of managed alerts.
"""

region: Optional[Region]
"""
Region to target. If none is passed will use default region from the config.
"""

page: Optional[int]
"""
Page number to return, from the paginated results.
"""

page_size: Optional[int]
"""
Number of data sources to return per page.
"""

order_by: Optional[ListManagedAlertsRequestOrderBy]
"""
Sort order for data sources in the response.
"""

project_id: Optional[str]
"""
Project ID to filter for, only data sources from this Project will be returned.
"""


@dataclass
class RegionalApiListTokensRequest:
"""
Expand Down
Loading