Skip to content

Commit 3eb7651

Browse files
committed
adding monitor APIs
1 parent cf04ca6 commit 3eb7651

14 files changed

+700
-0
lines changed

linode_api4/groups/__init__.py

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

linode_api4/groups/monitor.py

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
from linode_api4.errors import UnexpectedResponseError
2+
from linode_api4.groups import Group
3+
from linode_api4.objects import (
4+
CreateToken,
5+
DashboardByService,
6+
ServiceDetails,
7+
Dashboard,
8+
MonitorServiceSupported,
9+
MetricDefinition,
10+
DashboardsByID,
11+
)
12+
13+
class MonitorGroup(Group):
14+
"""
15+
Encapsulates LKE-related methods of the :any:`LinodeClient`. This
16+
should not be instantiated on its own, but should instead be used through
17+
an instance of :any:`LinodeClient`::
18+
19+
client = LinodeClient(token)
20+
instances = client.lke.clusters() # use the LKEGroup
21+
22+
This group contains all features beneath the `/lke` group in the API v4.
23+
"""
24+
25+
def dashboards(self, *filters):
26+
"""
27+
Returns a list of dashboards on your account.
28+
29+
.. note:: This endpoint is in beta. This will only function if base_url is set to `https://api.linode.com/v4beta`.
30+
31+
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-dashboards-all
32+
33+
:param filters: Any number of filters to apply to this query.
34+
See :doc:`Filtering Collections</linode_api4/objects/filtering>`
35+
for more details on filtering.
36+
37+
:returns: A list of Dashboards.
38+
:rtype: PaginatedList of Dashboard
39+
"""
40+
return self.client._get_and_filter(Dashboard, *filters)
41+
42+
def dashboard_by_ID(self, dashboard_id: int, *filters):
43+
"""
44+
Returns a dashboards on your account based on the ID passed.
45+
46+
.. note:: This endpoint is in beta. This will only function if base_url is set to `https://api.linode.com/v4beta`.
47+
48+
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-dashboards-by-id
49+
50+
:param filters: Any number of filters to apply to this query.
51+
See :doc:`Filtering Collections</linode_api4/objects/filtering>`
52+
for more details on filtering.
53+
54+
:returns: A Dashboards.
55+
:rtype: PaginatedList of the Dashboard
56+
"""
57+
result = self.client.get(f"/monitor/dashboards/{dashboard_id}")
58+
59+
if not "id" in result:
60+
raise UnexpectedResponseError(
61+
"Unexpected response when getting Dashboard!", json=result
62+
)
63+
return DashboardsByID(self.client, result["id"], result)
64+
65+
66+
def dashboard_by_service(self, service_type: str,*filters):
67+
"""
68+
Returns a dashboards on your account based on the service passed.
69+
70+
.. note:: This endpoint is in beta. This will only function if base_url is set to `https://api.linode.com/v4beta`.
71+
72+
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-dashboards
73+
74+
:param filters: Any number of filters to apply to this query.
75+
See :doc:`Filtering Collections</linode_api4/objects/filtering>`
76+
for more details on filtering.
77+
78+
:returns: A Dashboards filtered by Service Type.
79+
:rtype: PaginatedList of the Dashboards
80+
"""
81+
82+
return self.client._get_and_filter(
83+
DashboardByService,
84+
*filters,
85+
endpoint=f"/monitor/services/{service_type}/dashboards",
86+
)
87+
88+
89+
def supported_services(self, *filters):
90+
"""
91+
Returns a list of services supported by ACLP.
92+
93+
.. note:: This endpoint is in beta. This will only function if base_url is set to `https://api.linode.com/v4beta`.
94+
95+
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-monitor-services
96+
97+
:param filters: Any number of filters to apply to this query.
98+
See :doc:`Filtering Collections</linode_api4/objects/filtering>`
99+
for more details on filtering.
100+
101+
:returns: A list of Supported Services
102+
:rtype: PaginatedList of the Dashboards
103+
"""
104+
105+
return self.client._get_and_filter(MonitorServiceSupported, *filters)
106+
107+
def details_by_service(self, service_type: str,*filters):
108+
"""
109+
Returns a details about a particular service.
110+
111+
.. note:: This endpoint is in beta. This will only function if base_url is set to `https://api.linode.com/v4beta`.
112+
113+
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-monitor-services-for-service-type
114+
115+
:param filters: Any number of filters to apply to this query.
116+
See :doc:`Filtering Collections</linode_api4/objects/filtering>`
117+
for more details on filtering.
118+
119+
:returns: Details about a Supported Services
120+
:rtype: PaginatedList of the Service
121+
"""
122+
return self.client._get_and_filter(
123+
ServiceDetails,
124+
*filters,
125+
endpoint=f"/monitor/services/{service_type}",
126+
)
127+
128+
def metric_definitions(self, service_type: str,*filters):
129+
"""
130+
Returns metrics for a specific service type.
131+
132+
.. note:: This endpoint is in beta. This will only function if base_url is set to `https://api.linode.com/v4beta`.
133+
134+
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-monitor-information
135+
136+
:param filters: Any number of filters to apply to this query.
137+
See :doc:`Filtering Collections</linode_api4/objects/filtering>`
138+
for more details on filtering.
139+
140+
:returns: Returns a List of metrics for a service
141+
:rtype: PaginatedList of metrics
142+
"""
143+
return self.client._get_and_filter(
144+
MetricDefinition,
145+
*filters,
146+
endpoint=f"/monitor/services/{service_type}/metric-definitions",
147+
)
148+
149+
def create_token(self, service_type: str, entity_ids: list, *filters):
150+
"""
151+
Returns a JWE Token for a specific service type.
152+
153+
.. note:: This endpoint is in beta. This will only function if base_url is set to `https://api.linode.com/v4beta`.
154+
155+
API Documentation: https://techdocs.akamai.com/linode-api/reference/post-get-token
156+
:param filters: Any number of filters to apply to this query.
157+
See :doc:`Filtering Collections</linode_api4/objects/filtering>`
158+
for more details on filtering.
159+
160+
:returns: Returns a token for a service
161+
:rtype: str
162+
"""
163+
164+
params = {"entity_ids": entity_ids}
165+
166+
result = self.client.post(f"/monitor/services/{service_type}/token", data=params)
167+
168+
if "token" not in result:
169+
raise UnexpectedResponseError(
170+
"Unexpected response when creating token!", json=result
171+
)
172+
return CreateToken(self.client, result["token"], result)
173+
174+
175+
176+
177+
178+
179+
180+
181+
182+

linode_api4/linode_client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
TagGroup,
3030
VolumeGroup,
3131
VPCGroup,
32+
MonitorGroup,
3233
)
3334
from linode_api4.objects import Image, and_
3435

@@ -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: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
from linode_api4.objects import (
2+
Base,
3+
Property,
4+
)
5+
6+
class Dashboard(Base):
7+
"""
8+
List dashboards: https://techdocs.akamai.com/linode-api/get-dashboards-all
9+
"""
10+
11+
api_endpoint = "/monitor/dashboards/"
12+
properties = {
13+
"id": Property(identifier=True),
14+
"created": Property(is_datetime=True),
15+
"label": Property(),
16+
"service_type": Property(),
17+
"type": Property(),
18+
"widgets": Property(mutable=True),
19+
"updated": Property(is_datetime=True),
20+
21+
}
22+
23+
class DashboardsByID(Base):
24+
"""
25+
Get a dashboard: https://techdocs.akamai.com/linode-api/reference/get-dashboards-by-id
26+
"""
27+
28+
29+
properties = {
30+
"id": Property(identifier=True),
31+
"created": Property(is_datetime=True),
32+
"label": Property(),
33+
"service_type": Property(),
34+
"type": Property(),
35+
"widgets": Property(mutable=True),
36+
"updated": Property(is_datetime=True),
37+
38+
}
39+
40+
class DashboardByService(Base):
41+
"""
42+
Get a dashboard: https://techdocs.akamai.com/linode-api/reference/get-dashboards
43+
"""
44+
45+
properties = {
46+
"id": Property(identifier=True),
47+
"created": Property(is_datetime=True),
48+
"label": Property(),
49+
"service_type": Property(),
50+
"type": Property(),
51+
"widgets": Property(mutable=True),
52+
"updated": Property(is_datetime=True),
53+
54+
}
55+
56+
57+
class MonitorServiceSupported(Base):
58+
59+
api_endpoint = "/monitor/services/"
60+
id_attribute = "service_type"
61+
properties = {
62+
"service_type": Property(),
63+
"label": Property(mutable=True),
64+
65+
}
66+
67+
class ServiceDetails(Base):
68+
"""
69+
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-monitor-services-for-service-type
70+
"""
71+
id_attribute = "service_type"
72+
properties = {
73+
"label": Property(),
74+
"service_type": Property(),
75+
}
76+
77+
78+
79+
class MetricDefinition(Base):
80+
"""
81+
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-monitor-information
82+
"""
83+
84+
id_attribute = "metric"
85+
properties = {
86+
"available_aggregate_functions": Property(),
87+
"dimensions": Property(mutable=True),
88+
"label": Property(),
89+
"is_alertable": Property(),
90+
"metric": Property(),
91+
"metric_type": Property(),
92+
"scrape_interval": Property(),
93+
"unit": Property(),
94+
}
95+
96+
class CreateToken(Base):
97+
"""
98+
API Documentation: https://techdocs.akamai.com/linode-api/reference/post-get-token
99+
"""
100+
101+
properties = {
102+
"token": Property(mutable=True)
103+
}
104+
105+
106+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"data": [
3+
{
4+
"created": "2024-10-10T05:01:58",
5+
"id": 1,
6+
"label": "Resource Usage",
7+
"service_type": "dbaas",
8+
"type": "standard",
9+
"updated": "2024-10-10T05:01:58",
10+
"widgets": [
11+
{
12+
"aggregate_function": "sum",
13+
"chart_type": "area",
14+
"color": "default",
15+
"label": "CPU Usage",
16+
"metric": "cpu_usage",
17+
"size": 12,
18+
"unit": "%",
19+
"y_label": "cpu_usage"
20+
},
21+
{
22+
"aggregate_function": "sum",
23+
"chart_type": "area",
24+
"color": "default",
25+
"label": "Disk I/O Write",
26+
"metric": "write_iops",
27+
"size": 6,
28+
"unit": "IOPS",
29+
"y_label": "write_iops"
30+
}
31+
]
32+
}
33+
],
34+
"page": 1,
35+
"pages": 1,
36+
"results": 1
37+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"created": "2024-10-10T05:01:58",
3+
"id": 1,
4+
"label": "Resource Usage",
5+
"service_type": "dbaas",
6+
"type": "standard",
7+
"updated": "2024-10-10T05:01:58",
8+
"widgets": [
9+
{
10+
"aggregate_function": "sum",
11+
"chart_type": "area",
12+
"color": "default",
13+
"label": "CPU Usage",
14+
"metric": "cpu_usage",
15+
"size": 12,
16+
"unit": "%",
17+
"y_label": "cpu_usage"
18+
},
19+
{
20+
"aggregate_function": "sum",
21+
"chart_type": "area",
22+
"color": "default",
23+
"label": "Available Memory",
24+
"metric": "available_memory",
25+
"size": 6,
26+
"unit": "GB",
27+
"y_label": "available_memory"
28+
}
29+
]
30+
}

0 commit comments

Comments
 (0)