Skip to content
Merged
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
2 changes: 1 addition & 1 deletion hcloud/certificates/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def get_by_name(self, name: str) -> BoundCertificate | None:
Used to get certificate by name.
:return: :class:`BoundCertificate <hcloud.certificates.client.BoundCertificate>`
"""
return self._get_first_by(name=name)
return self._get_first_by(self.get_list, name=name)

def create(
self,
Expand Down
6 changes: 2 additions & 4 deletions hcloud/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ def _iter_pages( # type: ignore[no-untyped-def]

return results

def _get_first_by(self, **kwargs): # type: ignore[no-untyped-def]
assert hasattr(self, "get_list")
# pylint: disable=no-member
entities, _ = self.get_list(**kwargs)
def _get_first_by(self, list_function: Callable, *args, **kwargs): # type: ignore[no-untyped-def]
entities, _ = list_function(*args, **kwargs)
return entities[0] if entities else None


Expand Down
2 changes: 1 addition & 1 deletion hcloud/datacenters/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,4 @@ def get_by_name(self, name: str) -> BoundDatacenter | None:
Used to get datacenter by name.
:return: :class:`BoundDatacenter <hcloud.datacenters.client.BoundDatacenter>`
"""
return self._get_first_by(name=name)
return self._get_first_by(self.get_list, name=name)
2 changes: 1 addition & 1 deletion hcloud/firewalls/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ def get_by_name(self, name: str) -> BoundFirewall | None:
Used to get Firewall by name.
:return: :class:`BoundFirewall <hcloud.firewalls.client.BoundFirewall>`
"""
return self._get_first_by(name=name)
return self._get_first_by(self.get_list, name=name)

def create(
self,
Expand Down
2 changes: 1 addition & 1 deletion hcloud/floating_ips/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def get_by_name(self, name: str) -> BoundFloatingIP | None:
Used to get Floating IP by name.
:return: :class:`BoundFloatingIP <hcloud.floating_ips.client.BoundFloatingIP>`
"""
return self._get_first_by(name=name)
return self._get_first_by(self.get_list, name=name)

def create(
self,
Expand Down
3 changes: 2 additions & 1 deletion hcloud/images/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def get_by_name(self, name: str) -> BoundImage | None:
DeprecationWarning,
stacklevel=2,
)
return self._get_first_by(name=name)
return self._get_first_by(self.get_list, name=name)

def get_by_name_and_architecture(
self,
Expand All @@ -349,6 +349,7 @@ def get_by_name_and_architecture(
:return: :class:`BoundImage <hcloud.images.client.BoundImage>`
"""
return self._get_first_by(
self.get_list,
name=name,
architecture=[architecture],
include_deprecated=include_deprecated,
Expand Down
2 changes: 1 addition & 1 deletion hcloud/isos/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,4 @@ def get_by_name(self, name: str) -> BoundIso | None:
Used to get iso by name.
:return: :class:`BoundIso <hcloud.isos.client.BoundIso>`
"""
return self._get_first_by(name=name)
return self._get_first_by(self.get_list, name=name)
2 changes: 1 addition & 1 deletion hcloud/load_balancer_types/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@ def get_by_name(self, name: str) -> BoundLoadBalancerType | None:
Used to get Load Balancer type by name.
:return: :class:`BoundLoadBalancerType <hcloud.load_balancer_types.client.BoundLoadBalancerType>`
"""
return self._get_first_by(name=name)
return self._get_first_by(self.get_list, name=name)
2 changes: 1 addition & 1 deletion hcloud/load_balancers/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ def get_by_name(self, name: str) -> BoundLoadBalancer | None:
Used to get Load Balancer by name.
:return: :class:`BoundLoadBalancer <hcloud.load_balancers.client.BoundLoadBalancer>`
"""
return self._get_first_by(name=name)
return self._get_first_by(self.get_list, name=name)

def create(
self,
Expand Down
2 changes: 1 addition & 1 deletion hcloud/locations/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ def get_by_name(self, name: str) -> BoundLocation | None:
Used to get location by name.
:return: :class:`BoundLocation <hcloud.locations.client.BoundLocation>`
"""
return self._get_first_by(name=name)
return self._get_first_by(self.get_list, name=name)
2 changes: 1 addition & 1 deletion hcloud/networks/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def get_by_name(self, name: str) -> BoundNetwork | None:
Used to get network by name.
:return: :class:`BoundNetwork <hcloud.networks.client.BoundNetwork>`
"""
return self._get_first_by(name=name)
return self._get_first_by(self.get_list, name=name)

def create(
self,
Expand Down
2 changes: 1 addition & 1 deletion hcloud/placement_groups/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def get_by_name(self, name: str) -> BoundPlacementGroup | None:
Used to get Placement Group by name
:return: class:`BoundPlacementGroup <hcloud.placement_groups.client.BoundPlacementGroup>`
"""
return self._get_first_by(name=name)
return self._get_first_by(self.get_list, name=name)

def create(
self,
Expand Down
2 changes: 1 addition & 1 deletion hcloud/primary_ips/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def get_by_name(self, name: str) -> BoundPrimaryIP | None:
Used to get Primary IP by name.
:return: :class:`BoundPrimaryIP <hcloud.primary_ips.client.BoundPrimaryIP>`
"""
return self._get_first_by(name=name)
return self._get_first_by(self.get_list, name=name)

def create(
self,
Expand Down
2 changes: 1 addition & 1 deletion hcloud/server_types/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,4 @@ def get_by_name(self, name: str) -> BoundServerType | None:
Used to get Server type by name.
:return: :class:`BoundServerType <hcloud.server_types.client.BoundServerType>`
"""
return self._get_first_by(name=name)
return self._get_first_by(self.get_list, name=name)
2 changes: 1 addition & 1 deletion hcloud/servers/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ def get_by_name(self, name: str) -> BoundServer | None:
Used to get server by name.
:return: :class:`BoundServer <hcloud.servers.client.BoundServer>`
"""
return self._get_first_by(name=name)
return self._get_first_by(self.get_list, name=name)

# pylint: disable=too-many-branches,too-many-locals
def create(
Expand Down
4 changes: 2 additions & 2 deletions hcloud/ssh_keys/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def get_by_name(self, name: str) -> BoundSSHKey | None:
Used to get ssh key by name.
:return: :class:`BoundSSHKey <hcloud.ssh_keys.client.BoundSSHKey>`
"""
return self._get_first_by(name=name)
return self._get_first_by(self.get_list, name=name)

def get_by_fingerprint(self, fingerprint: str) -> BoundSSHKey | None:
"""Get ssh key by fingerprint
Expand All @@ -130,7 +130,7 @@ def get_by_fingerprint(self, fingerprint: str) -> BoundSSHKey | None:
Used to get ssh key by fingerprint.
:return: :class:`BoundSSHKey <hcloud.ssh_keys.client.BoundSSHKey>`
"""
return self._get_first_by(fingerprint=fingerprint)
return self._get_first_by(self.get_list, fingerprint=fingerprint)

def create(
self,
Expand Down
2 changes: 1 addition & 1 deletion hcloud/volumes/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def get_by_name(self, name: str) -> BoundVolume | None:
Used to get volume by name.
:return: :class:`BoundVolume <hcloud.volumes.client.BoundVolume>`
"""
return self._get_first_by(name=name)
return self._get_first_by(self.get_list, name=name)

def create(
self,
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/core/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def json_content_function(p):

candies_client = client_class_constructor(json_content_function)

result = candies_client._get_first_by(status="sweet")
result = candies_client._get_first_by(candies_client.get_list, status="sweet")

assert result == (1, None, "sweet", None)

Expand All @@ -235,6 +235,6 @@ def json_content_function(p):

candies_client = client_class_constructor(json_content_function)

result = candies_client._get_first_by(status="sweet")
result = candies_client._get_first_by(candies_client.get_list, status="sweet")

assert result is None