Skip to content

Commit

Permalink
Add missing get_by_name Method on FloatingIPsClient
Browse files Browse the repository at this point in the history
Prepare Release 1.6.0
  • Loading branch information
LKaemmerling committed Sep 17, 2019
1 parent 862c833 commit 400c019
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
History
=======

1.6.0 (2019-09-17)
-------------------

* Feature: Add missing `get_by_name` on `FloatingIPsClient`

1.5.0 (2019-09-16)
-------------------

Expand Down
2 changes: 1 addition & 1 deletion hcloud/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = '1.5.0'
VERSION = '1.6.0'
29 changes: 23 additions & 6 deletions hcloud/floating_ips/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
from hcloud.actions.client import BoundAction
from hcloud.core.client import BoundModelBase, ClientEntityBase
from hcloud.core.client import BoundModelBase, ClientEntityBase, GetEntityByNameMixin
from hcloud.core.domain import add_meta_to_result

from hcloud.floating_ips.domain import FloatingIP, CreateFloatingIPResponse
Expand Down Expand Up @@ -114,7 +114,7 @@ def change_dns_ptr(self, ip, dns_ptr):
return self._client.change_dns_ptr(self, ip, dns_ptr)


class FloatingIPsClient(ClientEntityBase):
class FloatingIPsClient(ClientEntityBase, GetEntityByNameMixin):
results_list_attribute_name = 'floating_ips'

def get_actions_list(self,
Expand Down Expand Up @@ -184,7 +184,8 @@ def get_by_id(self, id):
def get_list(self,
label_selector=None, # type: Optional[str]
page=None, # type: Optional[int]
per_page=None # type: Optional[int]
per_page=None, # type: Optional[int]
name=None, # type: Optional[str]
):
# type: (...) -> PageResults[List[BoundFloatingIP]]
"""Get a list of floating ips from this account
Expand All @@ -195,6 +196,8 @@ def get_list(self,
Specifies the page to fetch
:param per_page: int (optional)
Specifies how many results are returned by page
:param name: str (optional)
Can be used to filter networks by their name.
:return: (List[:class:`BoundFloatingIP <hcloud.floating_ips.client.BoundFloatingIP>`], :class:`Meta <hcloud.core.domain.Meta>`)
"""
params = {}
Expand All @@ -205,21 +208,35 @@ def get_list(self,
params['page'] = page
if per_page:
params['per_page'] = per_page
if name:
params['name'] = name

response = self._client.request(url="/floating_ips", method="GET", params=params)
floating_ips = [BoundFloatingIP(self, floating_ip_data) for floating_ip_data in response['floating_ips']]

return self._add_meta_to_result(floating_ips, response)

def get_all(self, label_selector=None):
# type: (Optional[str]) -> List[BoundFloatingIP]
def get_all(self, label_selector=None, name=None):
# type: (Optional[str], Optional[str]) -> List[BoundFloatingIP]
"""Get all floating ips from this account
:param label_selector: str (optional)
Can be used to filter Floating IPs by labels. The response will only contain Floating IPs matching the label selector.able values.
:param name: str (optional)
Can be used to filter networks by their name.
:return: List[:class:`BoundFloatingIP <hcloud.floating_ips.client.BoundFloatingIP>`]
"""
return super(FloatingIPsClient, self).get_all(label_selector=label_selector)
return super(FloatingIPsClient, self).get_all(label_selector=label_selector, name=name)

def get_by_name(self, name):
# type: (str) -> BoundFloatingIP
"""Get Floating IP by name
:param name: str
Used to get Floating IP by name.
:return: :class:`BoundFloatingIP <hcloud.floating_ips.client.BoundFloatingIP>`
"""
return super(FloatingIPsClient, self).get_by_name(name)

def create(self,
type, # type: str
Expand Down
7 changes: 7 additions & 0 deletions tests/integration/floating_ips/test_floating_ips.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ def test_get_by_id(self, hetzner_client):
assert bound_floating_ip.description == "Web Frontend"
assert bound_floating_ip.type == "ipv4"

def test_get_by_name(self, hetzner_client):
bound_floating_ip = hetzner_client.floating_ips.get_by_name("Web Frontend")
assert bound_floating_ip.id == 4711
assert bound_floating_ip.name == "Web Frontend"
assert bound_floating_ip.description == "Web Frontend"
assert bound_floating_ip.type == "ipv4"

def test_get_list(self, hetzner_client):
result = hetzner_client.floating_ips.get_list()
bound_floating_ips = result.floating_ips
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/floating_ips/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,43 @@ def floating_ip_response():
}


@pytest.fixture()
def one_floating_ips_response():
return {
"floating_ips": [
{
"id": 4711,
"description": "Web Frontend",
"name": "Web Frontend",
"created": "2016-01-30T23:50+00:00",
"ip": "131.232.99.1",
"type": "ipv4",
"server": 42,
"dns_ptr": [
{
"ip": "2001:db8::1",
"dns_ptr": "server.example.com"
}
],
"home_location": {
"id": 1,
"name": "fsn1",
"description": "Falkenstein DC Park 1",
"country": "DE",
"city": "Falkenstein",
"latitude": 50.47612,
"longitude": 12.370071
},
"blocked": False,
"protection": {
"delete": False
},
"labels": {}
},
]
}


@pytest.fixture()
def two_floating_ips_response():
return {
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/floating_ips/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ def test_get_by_id(self, floating_ips_client, floating_ip_response):
assert bound_floating_ip.id == 4711
assert bound_floating_ip.description == "Web Frontend"

def test_get_by_name(self, floating_ips_client, one_floating_ips_response):
floating_ips_client._client.request.return_value = one_floating_ips_response
bound_floating_ip = floating_ips_client.get_by_name("Web Frontend")
floating_ips_client._client.request.assert_called_with(url="/floating_ips", method="GET", params={"name": "Web Frontend"})
assert bound_floating_ip._client is floating_ips_client
assert bound_floating_ip.id == 4711
assert bound_floating_ip.name == "Web Frontend"
assert bound_floating_ip.description == "Web Frontend"

@pytest.mark.parametrize("params", [{'label_selector': "label1", 'page': 1, 'per_page': 10}, {}])
def test_get_list(self, floating_ips_client, two_floating_ips_response, params):
floating_ips_client._client.request.return_value = two_floating_ips_response
Expand Down

0 comments on commit 400c019

Please sign in to comment.