Skip to content

adding SDK changes for ACLP APIs #528

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 17 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from 12 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
1 change: 1 addition & 0 deletions linode_api4/groups/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from .lke import *
from .lke_tier import *
from .longview import *
from .monitor_service import *
from .networking import *
from .nodebalancer import *
from .object_storage import *
Expand Down
170 changes: 170 additions & 0 deletions linode_api4/groups/monitor_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
from linode_api4.errors import UnexpectedResponseError
from linode_api4.groups import Group
from linode_api4.objects import (
MonitorDashboard,
MonitorMetricsDefinition,
MonitorService,
MonitorServiceToken,
)


class MonitorGroup(Group):
"""
Encapsulates Monitor-related methods of the :any:`LinodeClient`.

This group contains all features beneath the `/monitor` group in the API v4.
"""

def list_monitor_dashboards(self, *filters) -> list[MonitorDashboard]:
"""
Returns a list of dashboards.

dashboards = client.monitor_service.list_monitor_dashboards()
dashboard = client.load(MonitorDashboard, 1)

.. note:: This endpoint is in beta. This will only function if base_url is set to `https://api.linode.com/v4beta`.

API Documentation: https://techdocs.akamai.com/linode-api/reference/get-dashboards-all

:param filters: Any number of filters to apply to this query.
See :doc:`Filtering Collections</linode_api4/objects/filtering>`
for more details on filtering.

:returns: A list of Dashboards.
:rtype: PaginatedList of Dashboard
"""

return self.client._get_and_filter(MonitorDashboard, *filters)

def list_dashboards_by_service(
self, service_type: str, *filters
) -> list[MonitorDashboard]:
"""
Returns a list of dashboards for a particular service.

dashboard_by_service = client.monitor_service.list_dashboards_by_service(service_type="dbaas")

.. note:: This endpoint is in beta. This will only function if base_url is set to `https://api.linode.com/v4beta`.

API Documentation: https://techdocs.akamai.com/linode-api/reference/get-dashboards

:param service_type: The service type to get dashboards for.
:type service_type: str
:param filters: Any number of filters to apply to this query.
See :doc:`Filtering Collections</linode_api4/objects/filtering>`
for more details on filtering.

:returns: Dashboards filtered by Service Type.
:rtype: PaginatedList of the Dashboards
"""

return self.client._get_and_filter(
MonitorDashboard,
*filters,
endpoint=f"/monitor/services/{service_type}/dashboards",
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it makes more sense to combine these two functions, since they're serving the same purpose to list dashboards and returning the same type.

How about we just having one function dashboards(...), and take an optional parameter service_type? In this case if service_type is provided by user, we will call the listing dashboards by service_type endpoint; if not, we will can the basic listing endpoint. The function header should looks like:

 def dashborads(
        self, service_type: Optional[str], *filters
    ) -> PaginatedList:
      # implement the two endpoints

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could have one function but both the endpoints have different implementation and fall under different api_endpoint group, so it could be misleading.
for dashboards we have - /monitor/dashboards/{id}
for service dashboard - /monitor/services/{service_type}/dashboards


def list_supported_services(self, *filters) -> list[MonitorService]:
"""
Returns a list of services supported by ACLP.

supported_services = client.monitor_service.list_supported_services()

.. note:: This endpoint is in beta. This will only function if base_url is set to `https://api.linode.com/v4beta`.

API Documentation: https://techdocs.akamai.com/linode-api/reference/get-monitor-services

:param filters: Any number of filters to apply to this query.
See :doc:`Filtering Collections</linode_api4/objects/filtering>`
for more details on filtering.

:returns: A list of Supported Services
:rtype: PaginatedList of Services
"""

return self.client._get_and_filter(MonitorService, *filters)

def list_service_by_type(
self, service_type: str, *filters
) -> list[MonitorService]:
"""
Lists monitor services by a given service_type

service_details = client.monitor_service.list_service_by_type(service_type="dbaas")

.. note:: This endpoint is in beta. This will only function if base_url is set to `https://api.linode.com/v4beta`.

API Documentation: https://techdocs.akamai.com/linode-api/reference/get-monitor-services-for-service-type

:param service_type: The service type to get details for.
:type service_type: str
:param filters: Any number of filters to apply to this query.
See :doc:`Filtering Collections</linode_api4/objects/filtering>`
for more details on filtering.

:returns: Lists monitor services by a given service_type
:rtype: PaginatedList of the Services
"""
return self.client._get_and_filter(
MonitorService,
*filters,
endpoint=f"/monitor/services/{service_type}",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the api doc, and it looks GET /monitor/services/{service_type} is not a listing endpoint

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This endpoint should ideally return the details about a service. But the return type is a list.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh interesting, do you mean that even GET /monitor/services/{service_type} supposed to get a single service, but the response is a list, right?

)

def list_metric_definitions(
self, service_type: str, *filters
) -> list[MonitorMetricsDefinition]:
"""
Returns metrics for a specific service type.

metrics = client.monitor_service.list_metric_definitions(service_type="dbaas")
.. note:: This endpoint is in beta. This will only function if base_url is set to `https://api.linode.com/v4beta`.

API Documentation: https://techdocs.akamai.com/linode-api/reference/get-monitor-information

:param service_type: The service type to get metrics for.
:type service_type: str
:param filters: Any number of filters to apply to this query.
See :doc:`Filtering Collections</linode_api4/objects/filtering>`
for more details on filtering.

:returns: Returns a List of metrics for a service
:rtype: PaginatedList of metrics
"""
return self.client._get_and_filter(
MonitorMetricsDefinition,
*filters,
endpoint=f"/monitor/services/{service_type}/metric-definitions",
)

def create_token(
self, service_type: str, entity_ids: list
) -> MonitorServiceToken:
"""
Returns a JWE Token for a specific service type.
token = client.monitor_service.create_token(service_type="dbaas", entity_ids=[1234])

.. note:: This endpoint is in beta. This will only function if base_url is set to `https://api.linode.com/v4beta`.

API Documentation: https://techdocs.akamai.com/linode-api/reference/post-get-token

:param service_type: The service type to create token for.
:type service_type: str
:param entity_ids: The list of entity IDs for which the token is valid.
:type entity_ids: list of int

:returns: Returns a token for a service
:rtype: str
"""

params = {"entity_ids": entity_ids}

result = self.client.post(
f"/monitor/services/{service_type}/token", data=params
)

if "token" not in result:
raise UnexpectedResponseError(
"Unexpected response when creating token!", json=result
)
return MonitorServiceToken(token=result["token"])
3 changes: 3 additions & 0 deletions linode_api4/linode_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
LinodeGroup,
LKEGroup,
LongviewGroup,
MonitorGroup,
NetworkingGroup,
NodeBalancerGroup,
ObjectStorageGroup,
Expand Down Expand Up @@ -201,6 +202,8 @@ def __init__(
#: Access methods related to VM placement - See :any:`PlacementAPIGroup` for more information.
self.placement = PlacementAPIGroup(self)

self.monitor_service = MonitorGroup(self)

@property
def _user_agent(self):
return "{}python-linode_api4/{} {}".format(
Expand Down
1 change: 1 addition & 0 deletions linode_api4/objects/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
from .vpc import *
from .beta import *
from .placement import *
from .monitor_service import *
177 changes: 177 additions & 0 deletions linode_api4/objects/monitor_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
from dataclasses import dataclass, field
from typing import List, Optional

from linode_api4.objects import Base, JSONObject, Property, StrEnum


class AggregateFunction(StrEnum):
"""
Enum for supported aggregate functions.
"""

min = "min"
max = "max"
avg = "avg"
sum = "sum"
count = "count"
rate = "rate"
increase = "increase"
last = "last"


class ChartType(StrEnum):
"""
Enum for supported chart types.
"""

line = "line"
area = "area"


class ServiceType(StrEnum):
"""
Enum for supported service types.
"""

dbaas = "dbaas"
linode = "linode"
lke = "lke"
vpc = "vpc"
nodebalancer = "nodebalancer"
firewall = "firewall"
object_storage = "object_storage"
aclb = "aclb"


class MetricType(StrEnum):
"""
Enum for supported metric type
"""

gauge = "gauge"
counter = "counter"
histogram = "histogram"
summary = "summary"


class MetricUnit(StrEnum):
"""
Enum for supported metric units.
"""

COUNT = "count"
PERCENT = "percent"
BYTE = "byte"
SECOND = "second"
BITS_PER_SECOND = "bits_per_second"
MILLISECOND = "millisecond"
KB = "KB"
MB = "MB"
GB = "GB"
RATE = "rate"
BYTES_PER_SECOND = "bytes_per_second"
PERCENTILE = "percentile"
RATIO = "ratio"
OPS_PER_SECOND = "ops_per_second"
IOPS = "iops"


class DashboardType(StrEnum):
"""
Enum for supported dashboard types.
"""

standard = "standard"
custom = "custom"


@dataclass
class DashboardWidget(JSONObject):
"""
Represents a single widget in the widgets list.
"""

metric: str = ""
unit: MetricUnit = ""
label: str = ""
color: str = ""
size: int = 0
chart_type: ChartType = ""
y_label: str = ""
aggregate_function: AggregateFunction = ""


@dataclass
class Dimension(JSONObject):
"""
Represents a single dimension in the dimensions list.
"""

dimension_label: Optional[str] = None
label: Optional[str] = None
values: Optional[List[str]] = None


@dataclass
class MonitorMetricsDefinition(JSONObject):
"""
Represents a single metric definition in the metrics definition list.

API Documentation: https://techdocs.akamai.com/linode-api/reference/get-monitor-information
"""

metric: str = ""
label: str = ""
metric_type: MetricType = ""
unit: MetricUnit = ""
scrape_interval: int = 0
is_alertable: bool = False
dimensions: Optional[List[Dimension]] = None
available_aggregate_functions: List[AggregateFunction] = field(
default_factory=list
)


class MonitorDashboard(Base):
"""
Dashboard details.

List dashboards: https://techdocs.akamai.com/linode-api/get-dashboards-all
"""

api_endpoint = "/monitor/dashboards/{id}"
properties = {
"id": Property(identifier=True),
"created": Property(is_datetime=True),
"label": Property(),
"service_type": Property(ServiceType),
"type": Property(DashboardType),
"widgets": Property(List[DashboardWidget]),
"updated": Property(is_datetime=True),
}


class MonitorService(Base):
"""
Represents a single service type.
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-monitor-services

"""

api_endpoint = "/monitor/services/{service_type}"
id_attribute = "service_type"
properties = {
"service_type": Property(ServiceType, identifier=True),
"label": Property(),
}
Copy link
Contributor

@yec-akamai yec-akamai May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since GET /monitor/services/{service_type} returns a list, it can't be populated by the default _api_get() in Base. I think you can build MonitorService into a JSONObject as well, so user won't accidentally try to get this object from default approach

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated to JSONObject



@dataclass
class MonitorServiceToken(JSONObject):
"""
A token for the requested service_type.

API Documentation: https://techdocs.akamai.com/linode-api/reference/post-get-token
"""

token: str = ""
Loading