mirror of
https://github.com/ansible-collections/hetzner.hcloud.git
synced 2026-02-03 23:51:48 +00:00
This PR contains the following updates: | Package | Change | Age | Adoption | Passing | Confidence | |---|---|---|---|---|---| | [hcloud](https://redirect.github.com/hetznercloud/hcloud-python) ([changelog](https://redirect.github.com/hetznercloud/hcloud-python/blob/main/CHANGELOG.md)) | `2.5.1` -> `2.5.2` | [](https://docs.renovatebot.com/merge-confidence/) | [](https://docs.renovatebot.com/merge-confidence/) | [](https://docs.renovatebot.com/merge-confidence/) | [](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>hetznercloud/hcloud-python (hcloud)</summary> ### [`v2.5.2`](https://redirect.github.com/hetznercloud/hcloud-python/blob/HEAD/CHANGELOG.md#v252) [Compare Source](https://redirect.github.com/hetznercloud/hcloud-python/compare/v2.5.1...v2.5.2) ##### Bug Fixes - listing page result always provide meta ([#​496](https://redirect.github.com/hetznercloud/hcloud-python/issues/496)) </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Whenever PR is behind base branch, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR was generated by [Mend Renovate](https://mend.io/renovate/). View the [repository job log](https://developer.mend.io/github/ansible-collections/hetzner.hcloud). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0MC4xNi4wIiwidXBkYXRlZEluVmVyIjoiNDAuMTYuMCIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==--> --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: jo <ljonas@riseup.net>
174 lines
6.4 KiB
Python
174 lines
6.4 KiB
Python
from __future__ import annotations
|
|
|
|
import time
|
|
import warnings
|
|
from typing import TYPE_CHECKING, Any, NamedTuple
|
|
|
|
from ..core import BoundModelBase, ClientEntityBase, Meta
|
|
from .domain import Action, ActionFailedException, ActionTimeoutException
|
|
|
|
if TYPE_CHECKING:
|
|
from .._client import Client
|
|
|
|
|
|
class BoundAction(BoundModelBase, Action):
|
|
_client: ActionsClient
|
|
|
|
model = Action
|
|
|
|
def wait_until_finished(self, max_retries: int | None = None) -> None:
|
|
"""Wait until the specific action has status=finished.
|
|
|
|
: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 status==running after max_retries is reached.
|
|
"""
|
|
if max_retries is None:
|
|
# pylint: disable=protected-access
|
|
max_retries = self._client._client._poll_max_retries
|
|
|
|
retries = 0
|
|
while True:
|
|
self.reload()
|
|
if self.status != Action.STATUS_RUNNING:
|
|
break
|
|
|
|
retries += 1
|
|
if retries < max_retries:
|
|
# pylint: disable=protected-access
|
|
time.sleep(self._client._client._poll_interval_func(retries))
|
|
continue
|
|
|
|
raise ActionTimeoutException(action=self)
|
|
|
|
if self.status == Action.STATUS_ERROR:
|
|
raise ActionFailedException(action=self)
|
|
|
|
|
|
class ActionsPageResult(NamedTuple):
|
|
actions: list[BoundAction]
|
|
meta: Meta
|
|
|
|
|
|
class ResourceActionsClient(ClientEntityBase):
|
|
_resource: str
|
|
|
|
def __init__(self, client: Client, resource: str | None):
|
|
super().__init__(client)
|
|
self._resource = resource or ""
|
|
|
|
def get_by_id(self, id: int) -> BoundAction:
|
|
"""Get a specific action by its ID.
|
|
|
|
:param id: int
|
|
:return: :class:`BoundAction <hcloud.actions.client.BoundAction>`
|
|
"""
|
|
response = self._client.request(
|
|
url=f"{self._resource}/actions/{id}",
|
|
method="GET",
|
|
)
|
|
return BoundAction(self._client.actions, response["action"])
|
|
|
|
def get_list(
|
|
self,
|
|
status: list[str] | None = None,
|
|
sort: list[str] | None = None,
|
|
page: int | None = None,
|
|
per_page: int | None = None,
|
|
) -> ActionsPageResult:
|
|
"""Get a list of actions.
|
|
|
|
: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>`)
|
|
"""
|
|
params: dict[str, Any] = {}
|
|
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
|
|
|
|
response = self._client.request(
|
|
url=f"{self._resource}/actions",
|
|
method="GET",
|
|
params=params,
|
|
)
|
|
actions = [
|
|
BoundAction(self._client.actions, action_data)
|
|
for action_data in response["actions"]
|
|
]
|
|
return ActionsPageResult(actions, Meta.parse_meta(response))
|
|
|
|
def get_all(
|
|
self,
|
|
status: list[str] | None = None,
|
|
sort: list[str] | None = None,
|
|
) -> list[BoundAction]:
|
|
"""Get all actions.
|
|
|
|
: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>`]
|
|
"""
|
|
return self._iter_pages(self.get_list, status=status, sort=sort)
|
|
|
|
|
|
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)
|