|
| 1 | +__all__ = [ |
| 2 | + "MonitorGroup", |
| 3 | +] |
| 4 | +from typing import Any, Optional |
| 5 | + |
| 6 | +from linode_api4 import ( |
| 7 | + PaginatedList, |
| 8 | +) |
| 9 | +from linode_api4.errors import UnexpectedResponseError |
| 10 | +from linode_api4.groups import Group |
| 11 | +from linode_api4.objects import ( |
| 12 | + MonitorDashboard, |
| 13 | + MonitorMetricsDefinition, |
| 14 | + MonitorService, |
| 15 | + MonitorServiceToken, |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +class MonitorGroup(Group): |
| 20 | + """ |
| 21 | + Encapsulates Monitor-related methods of the :any:`LinodeClient`. |
| 22 | +
|
| 23 | + This group contains all features beneath the `/monitor` group in the API v4. |
| 24 | + """ |
| 25 | + |
| 26 | + def dashboards( |
| 27 | + self, *filters, service_type: Optional[str] = None |
| 28 | + ) -> PaginatedList: |
| 29 | + """ |
| 30 | + Returns a list of dashboards. If `service_type` is provided, it fetches dashboards |
| 31 | + for the specific service type. If None, it fetches all dashboards. |
| 32 | +
|
| 33 | + dashboards = client.monitor.dashboards() |
| 34 | + dashboard = client.load(MonitorDashboard, 1) |
| 35 | + dashboards_by_service = client.monitor.dashboards(service_type="dbaas") |
| 36 | +
|
| 37 | + .. note:: This endpoint is in beta. This will only function if base_url is set to `https://api.linode.com/v4beta`. |
| 38 | +
|
| 39 | + API Documentation: |
| 40 | + - All Dashboards: https://techdocs.akamai.com/linode-api/reference/get-dashboards-all |
| 41 | + - Dashboards by Service: https://techdocs.akamai.com/linode-api/reference/get-dashboards |
| 42 | +
|
| 43 | + :param service_type: The service type to get dashboards for. |
| 44 | + :type service_type: Optional[str] |
| 45 | + :param filters: Any number of filters to apply to this query. |
| 46 | + See :doc:`Filtering Collections</linode_api4/objects/filtering>` |
| 47 | + for more details on filtering. |
| 48 | +
|
| 49 | + :returns: A list of Dashboards. |
| 50 | + :rtype: PaginatedList of Dashboard |
| 51 | + """ |
| 52 | + endpoint = ( |
| 53 | + f"/monitor/services/{service_type}/dashboards" |
| 54 | + if service_type |
| 55 | + else "/monitor/dashboards" |
| 56 | + ) |
| 57 | + |
| 58 | + return self.client._get_and_filter( |
| 59 | + MonitorDashboard, |
| 60 | + *filters, |
| 61 | + endpoint=endpoint, |
| 62 | + ) |
| 63 | + |
| 64 | + def services( |
| 65 | + self, *filters, service_type: Optional[str] = None |
| 66 | + ) -> list[MonitorService]: |
| 67 | + """ |
| 68 | + Lists services supported by ACLP. |
| 69 | + supported_services = client.monitor.services() |
| 70 | + service_details = client.monitor.services(service_type="dbaas") |
| 71 | +
|
| 72 | + .. note:: This endpoint is in beta. This will only function if base_url is set to `https://api.linode.com/v4beta`. |
| 73 | +
|
| 74 | + API Documentation: https://techdocs.akamai.com/linode-api/reference/get-monitor-services |
| 75 | + API Documentation: https://techdocs.akamai.com/linode-api/reference/get-monitor-services-for-service-type |
| 76 | +
|
| 77 | + :param service_type: The service type to get details for. |
| 78 | + :type service_type: Optional[str] |
| 79 | + :param filters: Any number of filters to apply to this query. |
| 80 | + See :doc:`Filtering Collections</linode_api4/objects/filtering>` |
| 81 | + for more details on filtering. |
| 82 | +
|
| 83 | + :returns: Lists monitor services by a given service_type |
| 84 | + :rtype: PaginatedList of the Services |
| 85 | + """ |
| 86 | + endpoint = ( |
| 87 | + f"/monitor/services/{service_type}" |
| 88 | + if service_type |
| 89 | + else "/monitor/services" |
| 90 | + ) |
| 91 | + return self.client._get_and_filter( |
| 92 | + MonitorService, |
| 93 | + *filters, |
| 94 | + endpoint=endpoint, |
| 95 | + ) |
| 96 | + |
| 97 | + def metric_definitions( |
| 98 | + self, service_type: str, *filters |
| 99 | + ) -> list[MonitorMetricsDefinition]: |
| 100 | + """ |
| 101 | + Returns metrics for a specific service type. |
| 102 | +
|
| 103 | + metrics = client.monitor.list_metric_definitions(service_type="dbaas") |
| 104 | + .. note:: This endpoint is in beta. This will only function if base_url is set to `https://api.linode.com/v4beta`. |
| 105 | +
|
| 106 | + API Documentation: https://techdocs.akamai.com/linode-api/reference/get-monitor-information |
| 107 | +
|
| 108 | + :param service_type: The service type to get metrics for. |
| 109 | + :type service_type: str |
| 110 | + :param filters: Any number of filters to apply to this query. |
| 111 | + See :doc:`Filtering Collections</linode_api4/objects/filtering>` |
| 112 | + for more details on filtering. |
| 113 | +
|
| 114 | + :returns: Returns a List of metrics for a service |
| 115 | + :rtype: PaginatedList of metrics |
| 116 | + """ |
| 117 | + return self.client._get_and_filter( |
| 118 | + MonitorMetricsDefinition, |
| 119 | + *filters, |
| 120 | + endpoint=f"/monitor/services/{service_type}/metric-definitions", |
| 121 | + ) |
| 122 | + |
| 123 | + def create_token( |
| 124 | + self, service_type: str, entity_ids: list[Any] |
| 125 | + ) -> MonitorServiceToken: |
| 126 | + """ |
| 127 | + Returns a JWE Token for a specific service type. |
| 128 | + token = client.monitor.create_token(service_type="dbaas", entity_ids=[1234]) |
| 129 | +
|
| 130 | + .. note:: This endpoint is in beta. This will only function if base_url is set to `https://api.linode.com/v4beta`. |
| 131 | +
|
| 132 | + API Documentation: https://techdocs.akamai.com/linode-api/reference/post-get-token |
| 133 | +
|
| 134 | + :param service_type: The service type to create token for. |
| 135 | + :type service_type: str |
| 136 | + :param entity_ids: The list of entity IDs for which the token is valid. |
| 137 | + :type entity_ids: any |
| 138 | +
|
| 139 | + :returns: Returns a token for a service |
| 140 | + :rtype: str |
| 141 | + """ |
| 142 | + |
| 143 | + params = {"entity_ids": entity_ids} |
| 144 | + |
| 145 | + result = self.client.post( |
| 146 | + f"/monitor/services/{service_type}/token", data=params |
| 147 | + ) |
| 148 | + |
| 149 | + if "token" not in result: |
| 150 | + raise UnexpectedResponseError( |
| 151 | + "Unexpected response when creating token!", json=result |
| 152 | + ) |
| 153 | + return MonitorServiceToken(token=result["token"]) |
0 commit comments