Skip to content

Commit 8facaf6

Browse files
authored
feat: add has_id_or_name to DomainIdentityMixin (#373)
- Add a method to check if 2 domains are equal (only check for same ID or same name) - Adds the `DomainIdentityMixin` to models that were missing it.
1 parent 6651b60 commit 8facaf6

9 files changed

Lines changed: 48 additions & 15 deletions

File tree

hcloud/core/domain.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,24 @@ def id_or_name(self) -> int | str:
3434
return self.name
3535
raise ValueError("id or name must be set")
3636

37+
def has_id_or_name(self, id_or_name: int | str) -> bool:
38+
"""
39+
Return whether this domain has the same id or same name as the other.
40+
41+
The domain calling this method MUST be a bound domain or be populated, otherwise
42+
the comparison will not work as expected (e.g. the domains are the same but
43+
cannot be equal, if one provides an id and the other the name).
44+
"""
45+
values: list[int | str] = []
46+
if self.id is not None:
47+
values.append(self.id)
48+
if self.name is not None:
49+
values.append(self.name)
50+
if not values:
51+
raise ValueError("id or name must be set")
52+
53+
return id_or_name in values
54+
3755

3856
class Pagination(BaseDomain):
3957
__slots__ = (

hcloud/firewalls/domain.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
from dateutil.parser import isoparse
66

7-
from ..core import BaseDomain
7+
from ..core import BaseDomain, DomainIdentityMixin
88

99
if TYPE_CHECKING:
1010
from ..actions import BoundAction
1111
from ..servers import BoundServer, Server
1212
from .client import BoundFirewall
1313

1414

15-
class Firewall(BaseDomain):
15+
class Firewall(BaseDomain, DomainIdentityMixin):
1616
"""Firewall Domain
1717
1818
:param id: int

hcloud/floating_ips/domain.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from dateutil.parser import isoparse
66

7-
from ..core import BaseDomain
7+
from ..core import BaseDomain, DomainIdentityMixin
88

99
if TYPE_CHECKING:
1010
from ..actions import BoundAction
@@ -13,7 +13,7 @@
1313
from .client import BoundFloatingIP
1414

1515

16-
class FloatingIP(BaseDomain):
16+
class FloatingIP(BaseDomain, DomainIdentityMixin):
1717
"""Floating IP Domain
1818
1919
:param id: int

hcloud/load_balancers/domain.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from dateutil.parser import isoparse
66

7-
from ..core import BaseDomain
7+
from ..core import BaseDomain, DomainIdentityMixin
88

99
if TYPE_CHECKING:
1010
from ..actions import BoundAction
@@ -17,7 +17,7 @@
1717
from .client import BoundLoadBalancer
1818

1919

20-
class LoadBalancer(BaseDomain):
20+
class LoadBalancer(BaseDomain, DomainIdentityMixin):
2121
"""LoadBalancer Domain
2222
2323
:param id: int

hcloud/networks/domain.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
from dateutil.parser import isoparse
66

7-
from ..core import BaseDomain
7+
from ..core import BaseDomain, DomainIdentityMixin
88

99
if TYPE_CHECKING:
1010
from ..actions import BoundAction
1111
from ..servers import BoundServer
1212
from .client import BoundNetwork
1313

1414

15-
class Network(BaseDomain):
15+
class Network(BaseDomain, DomainIdentityMixin):
1616
"""Network Domain
1717
1818
:param id: int

hcloud/placement_groups/domain.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
from dateutil.parser import isoparse
66

7-
from ..core import BaseDomain
7+
from ..core import BaseDomain, DomainIdentityMixin
88

99
if TYPE_CHECKING:
1010
from ..actions import BoundAction
1111
from .client import BoundPlacementGroup
1212

1313

14-
class PlacementGroup(BaseDomain):
14+
class PlacementGroup(BaseDomain, DomainIdentityMixin):
1515
"""Placement Group Domain
1616
1717
:param id: int

hcloud/primary_ips/domain.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
from dateutil.parser import isoparse
66

7-
from ..core import BaseDomain
7+
from ..core import BaseDomain, DomainIdentityMixin
88

99
if TYPE_CHECKING:
1010
from ..actions import BoundAction
1111
from ..datacenters import BoundDatacenter
1212
from .client import BoundPrimaryIP
1313

1414

15-
class PrimaryIP(BaseDomain):
15+
class PrimaryIP(BaseDomain, DomainIdentityMixin):
1616
"""Primary IP Domain
1717
1818
:param id: int

hcloud/servers/domain.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from dateutil.parser import isoparse
66

7-
from ..core import BaseDomain
7+
from ..core import BaseDomain, DomainIdentityMixin
88

99
if TYPE_CHECKING:
1010
from ..actions import BoundAction
@@ -22,7 +22,7 @@
2222
from .client import BoundServer
2323

2424

25-
class Server(BaseDomain):
25+
class Server(BaseDomain, DomainIdentityMixin):
2626
"""Server Domain
2727
2828
:param id: int

tests/unit/core/test_domain.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,25 @@ def test_id_or_name_exception(self):
6565
domain = SomeDomain()
6666

6767
with pytest.raises(ValueError) as exception_info:
68-
domain.id_or_name
68+
_ = domain.id_or_name
6969
error = exception_info.value
7070
assert str(error) == "id or name must be set"
7171

72+
@pytest.mark.parametrize(
73+
"other, expected",
74+
[
75+
(SomeDomain(id=1), True),
76+
(SomeDomain(name="name1"), True),
77+
(SomeDomain(id=1, name="name1"), True),
78+
(SomeDomain(id=2), False),
79+
(SomeDomain(name="name2"), False),
80+
(SomeDomain(id=2, name="name2"), False),
81+
],
82+
)
83+
def test_has_id_or_name_exception(self, other, expected):
84+
domain = SomeDomain(id=1, name="name1")
85+
assert domain.has_id_or_name(other.id_or_name) == expected
86+
7287

7388
class ActionDomain(BaseDomain, DomainIdentityMixin):
7489
__slots__ = ("id", "name", "started")

0 commit comments

Comments
 (0)