|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import warnings |
| 4 | +from datetime import datetime |
4 | 5 | from typing import TYPE_CHECKING, Any, NamedTuple |
5 | 6 |
|
| 7 | +from dateutil.parser import isoparse |
| 8 | + |
6 | 9 | from ..actions import ActionsPageResult, BoundAction, ResourceActionsClient |
7 | 10 | from ..core import BoundModelBase, ClientEntityBase, Meta |
8 | 11 | from ..datacenters import BoundDatacenter |
9 | 12 | from ..firewalls import BoundFirewall |
10 | 13 | from ..floating_ips import BoundFloatingIP |
11 | 14 | from ..images import BoundImage, CreateImageResponse |
12 | 15 | from ..isos import BoundIso |
| 16 | +from ..metrics import Metrics |
13 | 17 | from ..placement_groups import BoundPlacementGroup |
14 | 18 | from ..primary_ips import BoundPrimaryIP |
15 | 19 | from ..server_types import BoundServerType |
16 | 20 | from ..volumes import BoundVolume |
17 | 21 | from .domain import ( |
18 | 22 | CreateServerResponse, |
19 | 23 | EnableRescueResponse, |
| 24 | + GetMetricsResponse, |
20 | 25 | IPv4Address, |
21 | 26 | IPv6Network, |
| 27 | + MetricsType, |
22 | 28 | PrivateNet, |
23 | 29 | PublicNetwork, |
24 | 30 | PublicNetworkFirewall, |
@@ -210,6 +216,29 @@ def update( |
210 | 216 | """ |
211 | 217 | return self._client.update(self, name, labels) |
212 | 218 |
|
| 219 | + def get_metrics( |
| 220 | + self, |
| 221 | + type: MetricsType | list[MetricsType], |
| 222 | + start: datetime | str, |
| 223 | + end: datetime | str, |
| 224 | + step: float | None = None, |
| 225 | + ) -> GetMetricsResponse: |
| 226 | + """Get Metrics for a Server. |
| 227 | +
|
| 228 | + :param server: The Server to get the metrics for. |
| 229 | + :param type: Type of metrics to get. |
| 230 | + :param start: Start of period to get Metrics for (in ISO-8601 format). |
| 231 | + :param end: End of period to get Metrics for (in ISO-8601 format). |
| 232 | + :param step: Resolution of results in seconds. |
| 233 | + """ |
| 234 | + return self._client.get_metrics( |
| 235 | + self, |
| 236 | + type=type, |
| 237 | + start=start, |
| 238 | + end=end, |
| 239 | + step=step, |
| 240 | + ) |
| 241 | + |
213 | 242 | def delete(self) -> BoundAction: |
214 | 243 | """Deletes a server. This immediately removes the server from your account, and it is no longer accessible. |
215 | 244 |
|
@@ -742,6 +771,46 @@ def update( |
742 | 771 | ) |
743 | 772 | return BoundServer(self, response["server"]) |
744 | 773 |
|
| 774 | + def get_metrics( |
| 775 | + self, |
| 776 | + server: Server | BoundServer, |
| 777 | + type: MetricsType | list[MetricsType], |
| 778 | + start: datetime | str, |
| 779 | + end: datetime | str, |
| 780 | + step: float | None = None, |
| 781 | + ) -> GetMetricsResponse: |
| 782 | + """Get Metrics for a Server. |
| 783 | +
|
| 784 | + :param server: The Server to get the metrics for. |
| 785 | + :param type: Type of metrics to get. |
| 786 | + :param start: Start of period to get Metrics for (in ISO-8601 format). |
| 787 | + :param end: End of period to get Metrics for (in ISO-8601 format). |
| 788 | + :param step: Resolution of results in seconds. |
| 789 | + """ |
| 790 | + if not isinstance(type, list): |
| 791 | + type = [type] |
| 792 | + if isinstance(start, str): |
| 793 | + start = isoparse(start) |
| 794 | + if isinstance(end, str): |
| 795 | + end = isoparse(end) |
| 796 | + |
| 797 | + params: dict[str, Any] = { |
| 798 | + "type": ",".join(type), |
| 799 | + "start": start.isoformat(), |
| 800 | + "end": end.isoformat(), |
| 801 | + } |
| 802 | + if step is not None: |
| 803 | + params["step"] = step |
| 804 | + |
| 805 | + response = self._client.request( |
| 806 | + url=f"/servers/{server.id}/metrics", |
| 807 | + method="GET", |
| 808 | + params=params, |
| 809 | + ) |
| 810 | + return GetMetricsResponse( |
| 811 | + metrics=Metrics(**response["metrics"]), |
| 812 | + ) |
| 813 | + |
745 | 814 | def delete(self, server: Server | BoundServer) -> BoundAction: |
746 | 815 | """Deletes a server. This immediately removes the server from your account, and it is no longer accessible. |
747 | 816 |
|
|
0 commit comments