Skip to content

Commit 4fa70cf

Browse files
authored
Fix floating ip creation with location (#33)
1 parent 90a82a3 commit 4fa70cf

2 files changed

Lines changed: 30 additions & 13 deletions

File tree

hcloud/floating_ips/client.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,13 @@ def create(self,
255255

256256
response = self._client.request(url="/floating_ips", json=data, method="POST")
257257

258+
action = None
259+
if response.get('action') is not None:
260+
action = BoundAction(self._client.actions, response['action'])
261+
258262
result = CreateFloatingIPResponse(
259263
floating_ip=BoundFloatingIP(self, response['floating_ip']),
260-
action=BoundAction(self._client.actions, response['action'])
264+
action=action
261265
)
262266
return result
263267

tests/unit/floating_ips/test_client.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ def test_bound_floating_ip_init(self, floating_ip_response):
4545
def test_get_actions(self, hetzner_client, bound_floating_ip, response_get_actions):
4646
hetzner_client.request.return_value = response_get_actions
4747
actions = bound_floating_ip.get_actions(sort="id")
48-
hetzner_client.request.assert_called_with(url="/floating_ips/14/actions", method="GET", params={"sort": "id", 'page': 1, 'per_page': 50})
48+
hetzner_client.request.assert_called_with(url="/floating_ips/14/actions", method="GET",
49+
params={"sort": "id", 'page': 1, 'per_page': 50})
4950

5051
assert len(actions) == 1
5152
assert isinstance(actions[0], BoundAction)
@@ -55,7 +56,8 @@ def test_get_actions(self, hetzner_client, bound_floating_ip, response_get_actio
5556
def test_update(self, hetzner_client, bound_floating_ip, response_update_floating_ip):
5657
hetzner_client.request.return_value = response_update_floating_ip
5758
floating_ip = bound_floating_ip.update(description="New description")
58-
hetzner_client.request.assert_called_with(url="/floating_ips/14", method="PUT", json={"description": "New description"})
59+
hetzner_client.request.assert_called_with(url="/floating_ips/14", method="PUT",
60+
json={"description": "New description"})
5961

6062
assert floating_ip.id == 4711
6163
assert floating_ip.description == "New description"
@@ -70,7 +72,8 @@ def test_delete(self, hetzner_client, bound_floating_ip, generic_action):
7072
def test_change_protection(self, hetzner_client, bound_floating_ip, generic_action):
7173
hetzner_client.request.return_value = generic_action
7274
action = bound_floating_ip.change_protection(True)
73-
hetzner_client.request.assert_called_with(url="/floating_ips/14/actions/change_protection", method="POST", json={"delete": True})
75+
hetzner_client.request.assert_called_with(url="/floating_ips/14/actions/change_protection", method="POST",
76+
json={"delete": True})
7477

7578
assert action.id == 1
7679
assert action.progress == 0
@@ -168,8 +171,8 @@ def test_get_all(self, floating_ips_client, two_floating_ips_response, params):
168171
assert bound_floating_ip2.id == 4712
169172
assert bound_floating_ip2.description == "Web Backend"
170173

171-
def test_create_with_location(self, floating_ips_client, floating_ip_create_response):
172-
floating_ips_client._client.request.return_value = floating_ip_create_response
174+
def test_create_with_location(self, floating_ips_client, floating_ip_response):
175+
floating_ips_client._client.request.return_value = floating_ip_response
173176
response = floating_ips_client.create(
174177
"ipv6",
175178
"Web Frontend",
@@ -191,13 +194,12 @@ def test_create_with_location(self, floating_ips_client, floating_ip_create_resp
191194
assert bound_floating_ip._client is floating_ips_client
192195
assert bound_floating_ip.id == 4711
193196
assert bound_floating_ip.description == "Web Frontend"
194-
195-
assert action.id == 13
197+
assert action is None
196198

197199
@pytest.mark.parametrize("server", [Server(id=1), BoundServer(mock.MagicMock(), dict(id=1))])
198200
def test_create_with_server(self, floating_ips_client, server, floating_ip_create_response):
199201
floating_ips_client._client.request.return_value = floating_ip_create_response
200-
floating_ips_client.create(
202+
response = floating_ips_client.create(
201203
type="ipv6",
202204
description="Web Frontend",
203205
server=server
@@ -211,12 +213,20 @@ def test_create_with_server(self, floating_ips_client, server, floating_ip_creat
211213
'server': 1
212214
}
213215
)
216+
bound_floating_ip = response.floating_ip
217+
action = response.action
218+
219+
assert bound_floating_ip._client is floating_ips_client
220+
assert bound_floating_ip.id == 4711
221+
assert bound_floating_ip.description == "Web Frontend"
222+
assert action.id == 13
214223

215224
@pytest.mark.parametrize("floating_ip", [FloatingIP(id=1), BoundFloatingIP(mock.MagicMock(), dict(id=1))])
216225
def test_get_actions(self, floating_ips_client, floating_ip, response_get_actions):
217226
floating_ips_client._client.request.return_value = response_get_actions
218227
actions = floating_ips_client.get_actions(floating_ip)
219-
floating_ips_client._client.request.assert_called_with(url="/floating_ips/1/actions", method="GET", params={'page': 1, 'per_page': 50})
228+
floating_ips_client._client.request.assert_called_with(url="/floating_ips/1/actions", method="GET",
229+
params={'page': 1, 'per_page': 50})
220230

221231
assert len(actions) == 1
222232
assert isinstance(actions[0], BoundAction)
@@ -229,7 +239,8 @@ def test_get_actions(self, floating_ips_client, floating_ip, response_get_action
229239
def test_update(self, floating_ips_client, floating_ip, response_update_floating_ip):
230240
floating_ips_client._client.request.return_value = response_update_floating_ip
231241
floating_ip = floating_ips_client.update(floating_ip, description="New description")
232-
floating_ips_client._client.request.assert_called_with(url="/floating_ips/1", method="PUT", json={"description": "New description"})
242+
floating_ips_client._client.request.assert_called_with(url="/floating_ips/1", method="PUT",
243+
json={"description": "New description"})
233244

234245
assert floating_ip.id == 4711
235246
assert floating_ip.description == "New description"
@@ -238,7 +249,8 @@ def test_update(self, floating_ips_client, floating_ip, response_update_floating
238249
def test_change_protection(self, floating_ips_client, floating_ip, generic_action):
239250
floating_ips_client._client.request.return_value = generic_action
240251
action = floating_ips_client.change_protection(floating_ip, True)
241-
floating_ips_client._client.request.assert_called_with(url="/floating_ips/1/actions/change_protection", method="POST", json={"delete": True})
252+
floating_ips_client._client.request.assert_called_with(url="/floating_ips/1/actions/change_protection",
253+
method="POST", json={"delete": True})
242254

243255
assert action.id == 1
244256
assert action.progress == 0
@@ -253,7 +265,8 @@ def test_delete(self, floating_ips_client, floating_ip, generic_action):
253265

254266
@pytest.mark.parametrize("server,floating_ip",
255267
[(Server(id=1), FloatingIP(id=12)),
256-
(BoundServer(mock.MagicMock(), dict(id=1)), BoundFloatingIP(mock.MagicMock(), dict(id=12)))])
268+
(BoundServer(mock.MagicMock(), dict(id=1)),
269+
BoundFloatingIP(mock.MagicMock(), dict(id=12)))])
257270
def test_assign(self, floating_ips_client, server, floating_ip, generic_action):
258271
floating_ips_client._client.request.return_value = generic_action
259272
action = floating_ips_client.assign(floating_ip, server)

0 commit comments

Comments
 (0)