2023-08-08 16:17:22 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-07-11 09:15:08 +00:00
|
|
|
import time
|
2023-08-17 13:02:50 +00:00
|
|
|
import warnings
|
2023-08-08 16:17:22 +00:00
|
|
|
from typing import TYPE_CHECKING, Any, NamedTuple
|
2023-07-11 09:15:08 +00:00
|
|
|
|
2023-08-08 16:17:22 +00:00
|
|
|
from ..core import BoundModelBase, ClientEntityBase, Meta
|
2023-07-11 09:15:08 +00:00
|
|
|
from .domain import Action, ActionFailedException, ActionTimeoutException
|
|
|
|
|
2023-08-08 16:17:22 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from .._client import Client
|
|
|
|
|
2023-07-11 09:15:08 +00:00
|
|
|
|
2023-09-25 11:34:49 +00:00
|
|
|
class BoundAction(BoundModelBase, Action):
|
2023-08-08 16:17:22 +00:00
|
|
|
_client: ActionsClient
|
|
|
|
|
2023-07-11 09:15:08 +00:00
|
|
|
model = Action
|
|
|
|
|
2023-08-08 16:17:22 +00:00
|
|
|
def wait_until_finished(self, max_retries: int = 100) -> None:
|
2023-07-11 09:15:08 +00:00
|
|
|
"""Wait until the specific action has status="finished" (set Client.poll_interval to specify a delay between checks)
|
|
|
|
|
|
|
|
:param max_retries: int
|
|
|
|
Specify how many retries will be performed before an ActionTimeoutException will be raised
|
|
|
|
:raises: ActionFailedException when action is finished with status=="error"
|
|
|
|
:raises: ActionTimeoutException when Action is still in "running" state after max_retries reloads.
|
|
|
|
"""
|
|
|
|
while self.status == Action.STATUS_RUNNING:
|
|
|
|
if max_retries > 0:
|
|
|
|
self.reload()
|
2023-09-25 11:34:49 +00:00
|
|
|
# pylint: disable=protected-access
|
2023-07-11 09:15:08 +00:00
|
|
|
time.sleep(self._client._client.poll_interval)
|
|
|
|
max_retries = max_retries - 1
|
|
|
|
else:
|
|
|
|
raise ActionTimeoutException(action=self)
|
|
|
|
|
|
|
|
if self.status == Action.STATUS_ERROR:
|
|
|
|
raise ActionFailedException(action=self)
|
|
|
|
|
|
|
|
|
2023-08-08 16:17:22 +00:00
|
|
|
class ActionsPageResult(NamedTuple):
|
|
|
|
actions: list[BoundAction]
|
|
|
|
meta: Meta | None
|
|
|
|
|
|
|
|
|
2023-08-17 13:02:50 +00:00
|
|
|
class ResourceActionsClient(ClientEntityBase):
|
|
|
|
_resource: str
|
|
|
|
|
|
|
|
def __init__(self, client: Client, resource: str | None):
|
|
|
|
super().__init__(client)
|
|
|
|
self._resource = resource or ""
|
2023-07-11 09:15:08 +00:00
|
|
|
|
2023-08-08 16:17:22 +00:00
|
|
|
def get_by_id(self, id: int) -> BoundAction:
|
2023-07-11 09:15:08 +00:00
|
|
|
"""Get a specific action by its ID.
|
|
|
|
|
|
|
|
:param id: int
|
|
|
|
:return: :class:`BoundAction <hcloud.actions.client.BoundAction>`
|
|
|
|
"""
|
2023-08-17 13:02:50 +00:00
|
|
|
response = self._client.request(
|
|
|
|
url=f"{self._resource}/actions/{id}",
|
|
|
|
method="GET",
|
|
|
|
)
|
|
|
|
return BoundAction(self._client.actions, response["action"])
|
2023-07-11 09:15:08 +00:00
|
|
|
|
|
|
|
def get_list(
|
|
|
|
self,
|
2023-08-08 16:17:22 +00:00
|
|
|
status: list[str] | None = None,
|
|
|
|
sort: list[str] | None = None,
|
|
|
|
page: int | None = None,
|
|
|
|
per_page: int | None = None,
|
|
|
|
) -> ActionsPageResult:
|
2023-08-17 13:02:50 +00:00
|
|
|
"""Get a list of actions.
|
2023-07-11 09:15:08 +00:00
|
|
|
|
|
|
|
:param status: List[str] (optional)
|
|
|
|
Response will have only actions with specified statuses. Choices: `running` `success` `error`
|
|
|
|
:param sort: List[str] (optional)
|
|
|
|
Specify how the results are sorted. Choices: `id` `command` `status` `progress` `started` `finished` . You can add one of ":asc", ":desc" to modify sort order. ( ":asc" is default)
|
|
|
|
:param page: int (optional)
|
|
|
|
Specifies the page to fetch
|
|
|
|
:param per_page: int (optional)
|
|
|
|
Specifies how many results are returned by page
|
|
|
|
:return: (List[:class:`BoundAction <hcloud.actions.client.BoundAction>`], :class:`Meta <hcloud.core.domain.Meta>`)
|
|
|
|
"""
|
2023-08-08 16:17:22 +00:00
|
|
|
params: dict[str, Any] = {}
|
2023-07-11 09:15:08 +00:00
|
|
|
if status is not None:
|
|
|
|
params["status"] = status
|
|
|
|
if sort is not None:
|
|
|
|
params["sort"] = sort
|
|
|
|
if page is not None:
|
|
|
|
params["page"] = page
|
|
|
|
if per_page is not None:
|
|
|
|
params["per_page"] = per_page
|
|
|
|
|
2023-08-17 13:02:50 +00:00
|
|
|
response = self._client.request(
|
|
|
|
url=f"{self._resource}/actions",
|
|
|
|
method="GET",
|
|
|
|
params=params,
|
|
|
|
)
|
2023-07-11 09:15:08 +00:00
|
|
|
actions = [
|
2023-08-17 13:02:50 +00:00
|
|
|
BoundAction(self._client.actions, action_data)
|
|
|
|
for action_data in response["actions"]
|
2023-07-11 09:15:08 +00:00
|
|
|
]
|
2023-08-08 16:17:22 +00:00
|
|
|
return ActionsPageResult(actions, Meta.parse_meta(response))
|
2023-07-11 09:15:08 +00:00
|
|
|
|
2023-08-08 16:17:22 +00:00
|
|
|
def get_all(
|
|
|
|
self,
|
|
|
|
status: list[str] | None = None,
|
|
|
|
sort: list[str] | None = None,
|
|
|
|
) -> list[BoundAction]:
|
2023-08-17 13:02:50 +00:00
|
|
|
"""Get all actions.
|
2023-07-11 09:15:08 +00:00
|
|
|
|
|
|
|
:param status: List[str] (optional)
|
|
|
|
Response will have only actions with specified statuses. Choices: `running` `success` `error`
|
|
|
|
:param sort: List[str] (optional)
|
|
|
|
Specify how the results are sorted. Choices: `id` `command` `status` `progress` `started` `finished` . You can add one of ":asc", ":desc" to modify sort order. ( ":asc" is default)
|
|
|
|
:return: List[:class:`BoundAction <hcloud.actions.client.BoundAction>`]
|
|
|
|
"""
|
2023-08-08 16:17:22 +00:00
|
|
|
return self._iter_pages(self.get_list, status=status, sort=sort)
|
2023-08-17 13:02:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ActionsClient(ResourceActionsClient):
|
|
|
|
def __init__(self, client: Client):
|
|
|
|
super().__init__(client, None)
|
|
|
|
|
|
|
|
def get_list(
|
|
|
|
self,
|
|
|
|
status: list[str] | None = None,
|
|
|
|
sort: list[str] | None = None,
|
|
|
|
page: int | None = None,
|
|
|
|
per_page: int | None = None,
|
|
|
|
) -> ActionsPageResult:
|
|
|
|
"""
|
|
|
|
.. deprecated:: 1.28
|
|
|
|
Use :func:`client.<resource>.actions.get_list` instead,
|
|
|
|
e.g. using :attr:`hcloud.certificates.client.CertificatesClient.actions`.
|
|
|
|
|
|
|
|
`Starting 1 October 2023, it will no longer be available. <https://docs.hetzner.cloud/changelog#2023-07-20-actions-list-endpoint-is-deprecated>`_
|
|
|
|
"""
|
|
|
|
warnings.warn(
|
|
|
|
"The 'client.actions.get_list' method is deprecated, please use the "
|
|
|
|
"'client.<resource>.actions.get_list' method instead (e.g. "
|
|
|
|
"'client.certificates.actions.get_list').",
|
|
|
|
DeprecationWarning,
|
|
|
|
stacklevel=2,
|
|
|
|
)
|
|
|
|
return super().get_list(status=status, sort=sort, page=page, per_page=per_page)
|
|
|
|
|
|
|
|
def get_all(
|
|
|
|
self,
|
|
|
|
status: list[str] | None = None,
|
|
|
|
sort: list[str] | None = None,
|
|
|
|
) -> list[BoundAction]:
|
|
|
|
"""
|
|
|
|
.. deprecated:: 1.28
|
|
|
|
Use :func:`client.<resource>.actions.get_all` instead,
|
|
|
|
e.g. using :attr:`hcloud.certificates.client.CertificatesClient.actions`.
|
|
|
|
|
|
|
|
`Starting 1 October 2023, it will no longer be available. <https://docs.hetzner.cloud/changelog#2023-07-20-actions-list-endpoint-is-deprecated>`_
|
|
|
|
"""
|
|
|
|
warnings.warn(
|
|
|
|
"The 'client.actions.get_all' method is deprecated, please use the "
|
|
|
|
"'client.<resource>.actions.get_all' method instead (e.g. "
|
|
|
|
"'client.certificates.actions.get_all').",
|
|
|
|
DeprecationWarning,
|
|
|
|
stacklevel=2,
|
|
|
|
)
|
|
|
|
return super().get_all(status=status, sort=sort)
|