mirror of
https://github.com/ansible-collections/hetzner.hcloud.git
synced 2026-02-04 08:01:49 +00:00
This PR contains the following updates: | Package | Change | Age | Confidence | |---|---|---|---| | [hcloud](https://redirect.github.com/hetznercloud/hcloud-python) ([changelog](https://redirect.github.com/hetznercloud/hcloud-python/blob/main/CHANGELOG.md)) | `2.5.4` -> `2.6.0` | [](https://docs.renovatebot.com/merge-confidence/) | [](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>hetznercloud/hcloud-python (hcloud)</summary> ### [`v2.6.0`](https://redirect.github.com/hetznercloud/hcloud-python/blob/HEAD/CHANGELOG.md#v260) [Compare Source](https://redirect.github.com/hetznercloud/hcloud-python/compare/v2.5.4...v2.6.0) ##### Features - add category property to server type ([#​549](https://redirect.github.com/hetznercloud/hcloud-python/issues/549)) ##### Bug Fixes - rename `ClientEntityBase` to `ResourceClientBase` ([#​532](https://redirect.github.com/hetznercloud/hcloud-python/issues/532)) </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, 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:eyJjcmVhdGVkSW5WZXIiOiI0MS45MS4xIiwidXBkYXRlZEluVmVyIjoiNDEuOTEuMSIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==--> --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: jo <ljonas@riseup.net>
118 lines
4 KiB
Python
118 lines
4 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any, NamedTuple
|
|
|
|
from ..core import BoundModelBase, Meta, ResourceClientBase
|
|
from ..locations import BoundLocation
|
|
from ..server_types import BoundServerType
|
|
from .domain import Datacenter, DatacenterServerTypes
|
|
|
|
|
|
class BoundDatacenter(BoundModelBase, Datacenter):
|
|
_client: DatacentersClient
|
|
|
|
model = Datacenter
|
|
|
|
def __init__(self, client: DatacentersClient, data: dict):
|
|
location = data.get("location")
|
|
if location is not None:
|
|
data["location"] = BoundLocation(client._parent.locations, location)
|
|
|
|
server_types = data.get("server_types")
|
|
if server_types is not None:
|
|
available = [
|
|
BoundServerType(
|
|
client._parent.server_types, {"id": server_type}, complete=False
|
|
)
|
|
for server_type in server_types["available"]
|
|
]
|
|
supported = [
|
|
BoundServerType(
|
|
client._parent.server_types, {"id": server_type}, complete=False
|
|
)
|
|
for server_type in server_types["supported"]
|
|
]
|
|
available_for_migration = [
|
|
BoundServerType(
|
|
client._parent.server_types, {"id": server_type}, complete=False
|
|
)
|
|
for server_type in server_types["available_for_migration"]
|
|
]
|
|
data["server_types"] = DatacenterServerTypes(
|
|
available=available,
|
|
supported=supported,
|
|
available_for_migration=available_for_migration,
|
|
)
|
|
|
|
super().__init__(client, data)
|
|
|
|
|
|
class DatacentersPageResult(NamedTuple):
|
|
datacenters: list[BoundDatacenter]
|
|
meta: Meta
|
|
|
|
|
|
class DatacentersClient(ResourceClientBase):
|
|
_base_url = "/datacenters"
|
|
|
|
def get_by_id(self, id: int) -> BoundDatacenter:
|
|
"""Get a specific datacenter by its ID.
|
|
|
|
:param id: int
|
|
:return: :class:`BoundDatacenter <hcloud.datacenters.client.BoundDatacenter>`
|
|
"""
|
|
response = self._client.request(url=f"{self._base_url}/{id}", method="GET")
|
|
return BoundDatacenter(self, response["datacenter"])
|
|
|
|
def get_list(
|
|
self,
|
|
name: str | None = None,
|
|
page: int | None = None,
|
|
per_page: int | None = None,
|
|
) -> DatacentersPageResult:
|
|
"""Get a list of datacenters
|
|
|
|
:param name: str (optional)
|
|
Can be used to filter datacenters by their name.
|
|
: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:`BoundDatacenter <hcloud.datacenters.client.BoundDatacenter>`], :class:`Meta <hcloud.core.domain.Meta>`)
|
|
"""
|
|
params: dict[str, Any] = {}
|
|
if name is not None:
|
|
params["name"] = name
|
|
|
|
if page is not None:
|
|
params["page"] = page
|
|
|
|
if per_page is not None:
|
|
params["per_page"] = per_page
|
|
|
|
response = self._client.request(url=self._base_url, method="GET", params=params)
|
|
|
|
datacenters = [
|
|
BoundDatacenter(self, datacenter_data)
|
|
for datacenter_data in response["datacenters"]
|
|
]
|
|
|
|
return DatacentersPageResult(datacenters, Meta.parse_meta(response))
|
|
|
|
def get_all(self, name: str | None = None) -> list[BoundDatacenter]:
|
|
"""Get all datacenters
|
|
|
|
:param name: str (optional)
|
|
Can be used to filter datacenters by their name.
|
|
:return: List[:class:`BoundDatacenter <hcloud.datacenters.client.BoundDatacenter>`]
|
|
"""
|
|
return self._iter_pages(self.get_list, name=name)
|
|
|
|
def get_by_name(self, name: str) -> BoundDatacenter | None:
|
|
"""Get datacenter by name
|
|
|
|
:param name: str
|
|
Used to get datacenter by name.
|
|
:return: :class:`BoundDatacenter <hcloud.datacenters.client.BoundDatacenter>`
|
|
"""
|
|
return self._get_first_by(name=name)
|