Skip to content

Commit d3e5927

Browse files
Merge pull request #566 from linode/dev
v5.33.0
2 parents 761fe96 + 389905e commit d3e5927

19 files changed

+924
-357
lines changed

linode_api4/groups/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from .lke import *
1111
from .lke_tier import *
1212
from .longview import *
13+
from .monitor import *
1314
from .networking import *
1415
from .nodebalancer import *
1516
from .object_storage import *

linode_api4/groups/monitor.py

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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"])

linode_api4/linode_client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
LinodeGroup,
2020
LKEGroup,
2121
LongviewGroup,
22+
MonitorGroup,
2223
NetworkingGroup,
2324
NodeBalancerGroup,
2425
ObjectStorageGroup,
@@ -201,6 +202,8 @@ def __init__(
201202
#: Access methods related to VM placement - See :any:`PlacementAPIGroup` for more information.
202203
self.placement = PlacementAPIGroup(self)
203204

205+
self.monitor = MonitorGroup(self)
206+
204207
@property
205208
def _user_agent(self):
206209
return "{}python-linode_api4/{} {}".format(

linode_api4/objects/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@
2121
from .vpc import *
2222
from .beta import *
2323
from .placement import *
24+
from .monitor import *

linode_api4/objects/monitor.py

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
__all__ = [
2+
"MonitorDashboard",
3+
"MonitorMetricsDefinition",
4+
"MonitorService",
5+
"MonitorServiceToken",
6+
]
7+
from dataclasses import dataclass, field
8+
from typing import List, Optional
9+
10+
from linode_api4.objects import Base, JSONObject, Property, StrEnum
11+
12+
13+
class AggregateFunction(StrEnum):
14+
"""
15+
Enum for supported aggregate functions.
16+
"""
17+
18+
min = "min"
19+
max = "max"
20+
avg = "avg"
21+
sum = "sum"
22+
count = "count"
23+
rate = "rate"
24+
increase = "increase"
25+
last = "last"
26+
27+
28+
class ChartType(StrEnum):
29+
"""
30+
Enum for supported chart types.
31+
"""
32+
33+
line = "line"
34+
area = "area"
35+
36+
37+
class ServiceType(StrEnum):
38+
"""
39+
Enum for supported service types.
40+
"""
41+
42+
dbaas = "dbaas"
43+
linode = "linode"
44+
lke = "lke"
45+
vpc = "vpc"
46+
nodebalancer = "nodebalancer"
47+
firewall = "firewall"
48+
object_storage = "object_storage"
49+
aclb = "aclb"
50+
51+
52+
class MetricType(StrEnum):
53+
"""
54+
Enum for supported metric type
55+
"""
56+
57+
gauge = "gauge"
58+
counter = "counter"
59+
histogram = "histogram"
60+
summary = "summary"
61+
62+
63+
class MetricUnit(StrEnum):
64+
"""
65+
Enum for supported metric units.
66+
"""
67+
68+
COUNT = "count"
69+
PERCENT = "percent"
70+
BYTE = "byte"
71+
SECOND = "second"
72+
BITS_PER_SECOND = "bits_per_second"
73+
MILLISECOND = "millisecond"
74+
KB = "KB"
75+
MB = "MB"
76+
GB = "GB"
77+
RATE = "rate"
78+
BYTES_PER_SECOND = "bytes_per_second"
79+
PERCENTILE = "percentile"
80+
RATIO = "ratio"
81+
OPS_PER_SECOND = "ops_per_second"
82+
IOPS = "iops"
83+
84+
85+
class DashboardType(StrEnum):
86+
"""
87+
Enum for supported dashboard types.
88+
"""
89+
90+
standard = "standard"
91+
custom = "custom"
92+
93+
94+
@dataclass
95+
class DashboardWidget(JSONObject):
96+
"""
97+
Represents a single widget in the widgets list.
98+
"""
99+
100+
metric: str = ""
101+
unit: MetricUnit = ""
102+
label: str = ""
103+
color: str = ""
104+
size: int = 0
105+
chart_type: ChartType = ""
106+
y_label: str = ""
107+
aggregate_function: AggregateFunction = ""
108+
109+
110+
@dataclass
111+
class Dimension(JSONObject):
112+
"""
113+
Represents a single dimension in the dimensions list.
114+
"""
115+
116+
dimension_label: Optional[str] = None
117+
label: Optional[str] = None
118+
values: Optional[List[str]] = None
119+
120+
121+
@dataclass
122+
class MonitorMetricsDefinition(JSONObject):
123+
"""
124+
Represents a single metric definition in the metrics definition list.
125+
126+
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-monitor-information
127+
"""
128+
129+
metric: str = ""
130+
label: str = ""
131+
metric_type: MetricType = ""
132+
unit: MetricUnit = ""
133+
scrape_interval: int = 0
134+
is_alertable: bool = False
135+
dimensions: Optional[List[Dimension]] = None
136+
available_aggregate_functions: List[AggregateFunction] = field(
137+
default_factory=list
138+
)
139+
140+
141+
class MonitorDashboard(Base):
142+
"""
143+
Dashboard details.
144+
145+
List dashboards: https://techdocs.akamai.com/linode-api/get-dashboards-all
146+
"""
147+
148+
api_endpoint = "/monitor/dashboards/{id}"
149+
properties = {
150+
"id": Property(identifier=True),
151+
"created": Property(is_datetime=True),
152+
"label": Property(),
153+
"service_type": Property(ServiceType),
154+
"type": Property(DashboardType),
155+
"widgets": Property(List[DashboardWidget]),
156+
"updated": Property(is_datetime=True),
157+
}
158+
159+
160+
@dataclass
161+
class MonitorService(JSONObject):
162+
"""
163+
Represents a single service type.
164+
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-monitor-services
165+
166+
"""
167+
168+
service_type: ServiceType = ""
169+
label: str = ""
170+
171+
172+
@dataclass
173+
class MonitorServiceToken(JSONObject):
174+
"""
175+
A token for the requested service_type.
176+
177+
API Documentation: https://techdocs.akamai.com/linode-api/reference/post-get-token
178+
"""
179+
180+
token: str = ""

0 commit comments

Comments
 (0)