Skip to content

Commit

Permalink
feat: add has_id_or_name to DomainIdentityMixin
Browse files Browse the repository at this point in the history
  • Loading branch information
jooola committed Mar 27, 2024
1 parent 04a6a42 commit 07c6c24
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 15 deletions.
18 changes: 18 additions & 0 deletions hcloud/core/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ def id_or_name(self) -> int | str:
return self.name
raise ValueError("id or name must be set")

def has_id_or_name(self, id_or_name: int | str) -> bool:
"""
Return whether this domain has the same id or same name as the other.
The domain calling this method MUST be a bound domain or be populated, otherwise
the the comparison will not work as expected (e.g. the domains are the same but
cannot be compared, if one provides only an id and the other only the name).
"""
values: list[int | str] = []
if self.id is not None:
values.append(self.id)
if self.name is not None:
values.append(self.name)
if not values:
raise ValueError("id or name must be set")

return id_or_name in values


class Pagination(BaseDomain):
__slots__ = (
Expand Down
4 changes: 2 additions & 2 deletions hcloud/firewalls/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

from dateutil.parser import isoparse

from ..core import BaseDomain
from ..core import BaseDomain, DomainIdentityMixin

if TYPE_CHECKING:
from ..actions import BoundAction
from ..servers import BoundServer, Server
from .client import BoundFirewall


class Firewall(BaseDomain):
class Firewall(BaseDomain, DomainIdentityMixin):
"""Firewall Domain
:param id: int
Expand Down
4 changes: 2 additions & 2 deletions hcloud/floating_ips/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from dateutil.parser import isoparse

from ..core import BaseDomain
from ..core import BaseDomain, DomainIdentityMixin

if TYPE_CHECKING:
from ..actions import BoundAction
Expand All @@ -13,7 +13,7 @@
from .client import BoundFloatingIP


class FloatingIP(BaseDomain):
class FloatingIP(BaseDomain, DomainIdentityMixin):
"""Floating IP Domain
:param id: int
Expand Down
4 changes: 2 additions & 2 deletions hcloud/load_balancers/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from dateutil.parser import isoparse

from ..core import BaseDomain
from ..core import BaseDomain, DomainIdentityMixin

if TYPE_CHECKING:
from ..actions import BoundAction
Expand All @@ -17,7 +17,7 @@
from .client import BoundLoadBalancer


class LoadBalancer(BaseDomain):
class LoadBalancer(BaseDomain, DomainIdentityMixin):
"""LoadBalancer Domain
:param id: int
Expand Down
4 changes: 2 additions & 2 deletions hcloud/networks/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

from dateutil.parser import isoparse

from ..core import BaseDomain
from ..core import BaseDomain, DomainIdentityMixin

if TYPE_CHECKING:
from ..actions import BoundAction
from ..servers import BoundServer
from .client import BoundNetwork


class Network(BaseDomain):
class Network(BaseDomain, DomainIdentityMixin):
"""Network Domain
:param id: int
Expand Down
4 changes: 2 additions & 2 deletions hcloud/placement_groups/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

from dateutil.parser import isoparse

from ..core import BaseDomain
from ..core import BaseDomain, DomainIdentityMixin

if TYPE_CHECKING:
from ..actions import BoundAction
from .client import BoundPlacementGroup


class PlacementGroup(BaseDomain):
class PlacementGroup(BaseDomain, DomainIdentityMixin):
"""Placement Group Domain
:param id: int
Expand Down
4 changes: 2 additions & 2 deletions hcloud/primary_ips/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

from dateutil.parser import isoparse

from ..core import BaseDomain
from ..core import BaseDomain, DomainIdentityMixin

if TYPE_CHECKING:
from ..actions import BoundAction
from ..datacenters import BoundDatacenter
from .client import BoundPrimaryIP


class PrimaryIP(BaseDomain):
class PrimaryIP(BaseDomain, DomainIdentityMixin):
"""Primary IP Domain
:param id: int
Expand Down
4 changes: 2 additions & 2 deletions hcloud/servers/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from dateutil.parser import isoparse

from ..core import BaseDomain
from ..core import BaseDomain, DomainIdentityMixin

if TYPE_CHECKING:
from ..actions import BoundAction
Expand All @@ -22,7 +22,7 @@
from .client import BoundServer


class Server(BaseDomain):
class Server(BaseDomain, DomainIdentityMixin):
"""Server Domain
:param id: int
Expand Down
17 changes: 16 additions & 1 deletion tests/unit/core/test_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,25 @@ def test_id_or_name_exception(self):
domain = SomeDomain()

with pytest.raises(ValueError) as exception_info:
domain.id_or_name
_ = domain.id_or_name
error = exception_info.value
assert str(error) == "id or name must be set"

@pytest.mark.parametrize(
"other, expected",
[
(SomeDomain(id=1), True),
(SomeDomain(name="name1"), True),
(SomeDomain(id=1, name="name1"), True),
(SomeDomain(id=2), False),
(SomeDomain(name="name2"), False),
(SomeDomain(id=2, name="name2"), False),
],
)
def test_has_id_or_name_exception(self, other, expected):
domain = SomeDomain(id=1, name="name1")
assert domain.has_id_or_name(other.id_or_name) == expected


class ActionDomain(BaseDomain, DomainIdentityMixin):
__slots__ = ("id", "name", "started")
Expand Down

0 comments on commit 07c6c24

Please sign in to comment.