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

Open
wants to merge 17 commits into
base: dev
Choose a base branch
from
Open
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
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 import *
from .networking import *
from .nodebalancer import *
from .object_storage import *
Expand Down
153 changes: 153 additions & 0 deletions linode_api4/groups/monitor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
__all__ = [
"MonitorGroup",
]
from typing import Any, Optional

from linode_api4 import (
PaginatedList,
)
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 dashboards(
self, *filters, service_type: Optional[str] = None
) -> PaginatedList:
"""
Returns a list of dashboards. If `service_type` is provided, it fetches dashboards
for the specific service type. If None, it fetches all dashboards.

dashboards = client.monitor.dashboards()
dashboard = client.load(MonitorDashboard, 1)
dashboards_by_service = client.monitor.dashboards(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:
- All Dashboards: https://techdocs.akamai.com/linode-api/reference/get-dashboards-all
- Dashboards by Service: https://techdocs.akamai.com/linode-api/reference/get-dashboards

:param service_type: The service type to get dashboards for.
:type service_type: Optional[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: A list of Dashboards.
:rtype: PaginatedList of Dashboard
"""
endpoint = (
f"/monitor/services/{service_type}/dashboards"
if service_type
else "/monitor/dashboards"
)

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

def services(
self, *filters, service_type: Optional[str] = None
) -> list[MonitorService]:
"""
Lists services supported by ACLP.
supported_services = client.monitor.services()
service_details = client.monitor.services(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
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: Optional[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
"""
endpoint = (
f"/monitor/services/{service_type}"
if service_type
else "/monitor/services"
)
return self.client._get_and_filter(
MonitorService,
*filters,
endpoint=endpoint,
)

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

metrics = client.monitor.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[Any]
) -> MonitorServiceToken:
"""
Returns a JWE Token for a specific service type.
token = client.monitor.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: any

: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 = 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 import *
180 changes: 180 additions & 0 deletions linode_api4/objects/monitor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
__all__ = [
"MonitorDashboard",
"MonitorMetricsDefinition",
"MonitorService",
"MonitorServiceToken",
]
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),
}


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

"""

service_type: ServiceType = ""
label: str = ""


@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