Skip to content

Commit

Permalink
Fix floating ip creation with location (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
LKaemmerling authored Feb 28, 2019
1 parent 90a82a3 commit 4fa70cf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
6 changes: 5 additions & 1 deletion hcloud/floating_ips/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,13 @@ def create(self,

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

action = None
if response.get('action') is not None:
action = BoundAction(self._client.actions, response['action'])

result = CreateFloatingIPResponse(
floating_ip=BoundFloatingIP(self, response['floating_ip']),
action=BoundAction(self._client.actions, response['action'])
action=action
)
return result

Expand Down
37 changes: 25 additions & 12 deletions tests/unit/floating_ips/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def test_bound_floating_ip_init(self, floating_ip_response):
def test_get_actions(self, hetzner_client, bound_floating_ip, response_get_actions):
hetzner_client.request.return_value = response_get_actions
actions = bound_floating_ip.get_actions(sort="id")
hetzner_client.request.assert_called_with(url="/floating_ips/14/actions", method="GET", params={"sort": "id", 'page': 1, 'per_page': 50})
hetzner_client.request.assert_called_with(url="/floating_ips/14/actions", method="GET",
params={"sort": "id", 'page': 1, 'per_page': 50})

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

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

assert action.id == 1
assert action.progress == 0
Expand Down Expand Up @@ -168,8 +171,8 @@ def test_get_all(self, floating_ips_client, two_floating_ips_response, params):
assert bound_floating_ip2.id == 4712
assert bound_floating_ip2.description == "Web Backend"

def test_create_with_location(self, floating_ips_client, floating_ip_create_response):
floating_ips_client._client.request.return_value = floating_ip_create_response
def test_create_with_location(self, floating_ips_client, floating_ip_response):
floating_ips_client._client.request.return_value = floating_ip_response
response = floating_ips_client.create(
"ipv6",
"Web Frontend",
Expand All @@ -191,13 +194,12 @@ def test_create_with_location(self, floating_ips_client, floating_ip_create_resp
assert bound_floating_ip._client is floating_ips_client
assert bound_floating_ip.id == 4711
assert bound_floating_ip.description == "Web Frontend"

assert action.id == 13
assert action is None

@pytest.mark.parametrize("server", [Server(id=1), BoundServer(mock.MagicMock(), dict(id=1))])
def test_create_with_server(self, floating_ips_client, server, floating_ip_create_response):
floating_ips_client._client.request.return_value = floating_ip_create_response
floating_ips_client.create(
response = floating_ips_client.create(
type="ipv6",
description="Web Frontend",
server=server
Expand All @@ -211,12 +213,20 @@ def test_create_with_server(self, floating_ips_client, server, floating_ip_creat
'server': 1
}
)
bound_floating_ip = response.floating_ip
action = response.action

assert bound_floating_ip._client is floating_ips_client
assert bound_floating_ip.id == 4711
assert bound_floating_ip.description == "Web Frontend"
assert action.id == 13

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

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

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

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

@pytest.mark.parametrize("server,floating_ip",
[(Server(id=1), FloatingIP(id=12)),
(BoundServer(mock.MagicMock(), dict(id=1)), BoundFloatingIP(mock.MagicMock(), dict(id=12)))])
(BoundServer(mock.MagicMock(), dict(id=1)),
BoundFloatingIP(mock.MagicMock(), dict(id=12)))])
def test_assign(self, floating_ips_client, server, floating_ip, generic_action):
floating_ips_client._client.request.return_value = generic_action
action = floating_ips_client.assign(floating_ip, server)
Expand Down

0 comments on commit 4fa70cf

Please sign in to comment.