Skip to content

Commit 10af141

Browse files
authored
fix: support reloading sub resource bound models (#590)
Sub resource bound models cannot be reloaded using `get_by_id`, a custom function must be implemented.
1 parent e071a8b commit 10af141

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

hcloud/core/client.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,13 @@ def __getattr__(self, name: str): # type: ignore[no-untyped-def]
103103
value = getattr(self.data_model, name)
104104
return value
105105

106-
def reload(self) -> None:
107-
"""Reloads the model and tries to get all data from the APIx"""
106+
def _get_self(self) -> BoundModelBase:
108107
assert hasattr(self._client, "get_by_id")
109-
bound_model = self._client.get_by_id(self.data_model.id)
108+
return self._client.get_by_id(self.data_model.id)
109+
110+
def reload(self) -> None:
111+
"""Reloads the model and tries to get all data from the API"""
112+
bound_model = self._get_self()
110113
self.data_model = bound_model.data_model
111114
self.complete = True
112115

hcloud/zones/client.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,13 @@ def __init__(self, client: ZonesClient, data: dict, complete: bool = True):
406406

407407
super().__init__(client, data, complete)
408408

409+
def _get_self(self) -> BoundZoneRRSet:
410+
return self._client.get_rrset(
411+
self.data_model.zone,
412+
self.data_model.name,
413+
self.data_model.type,
414+
)
415+
409416
def update_rrset(
410417
self,
411418
*,

tests/unit/conftest.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ def test_method_list(self, bound_model):
188188
lambda m: inspect.ismethod(m)
189189
and m.__func__ in bound_model.__class__.__dict__.values(),
190190
):
191+
# Ignore private methods
192+
if name.startswith("_"):
193+
continue
194+
191195
# Actions methods are already tested in TestBoundModelActions.
192196
if name in ("__init__", "get_actions", "get_actions_list"):
193197
continue

tests/unit/zones/test_client.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,3 +958,25 @@ def test_init(self, resource_client: ZonesClient, bound_model: BoundZoneRRSet):
958958

959959
assert isinstance(o.zone, BoundZone)
960960
assert o.zone.id == 42
961+
962+
def test_reload(
963+
self,
964+
request_mock: mock.MagicMock,
965+
resource_client: ZonesClient,
966+
zone_rrset1,
967+
):
968+
o = BoundZoneRRSet(
969+
resource_client,
970+
data={"id": "www/A", "zone": 42},
971+
complete=False,
972+
)
973+
request_mock.return_value = {"rrset": zone_rrset1}
974+
975+
o.reload()
976+
977+
request_mock.assert_called_with(
978+
method="GET",
979+
url="/zones/42/rrsets/www/A",
980+
)
981+
982+
assert o.labels is not None

0 commit comments

Comments
 (0)