From 400c01959453a2cddd3d0b3f8920d00a0b066c96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= Date: Tue, 17 Sep 2019 09:03:34 +0200 Subject: [PATCH] Add missing get_by_name Method on FloatingIPsClient Prepare Release 1.6.0 --- CHANGELOG.rst | 5 +++ hcloud/__version__.py | 2 +- hcloud/floating_ips/client.py | 29 ++++++++++++--- .../floating_ips/test_floating_ips.py | 7 ++++ tests/unit/floating_ips/conftest.py | 37 +++++++++++++++++++ tests/unit/floating_ips/test_client.py | 9 +++++ 6 files changed, 82 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 7303ead..1955493 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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) ------------------- diff --git a/hcloud/__version__.py b/hcloud/__version__.py index 84262d3..f23b083 100644 --- a/hcloud/__version__.py +++ b/hcloud/__version__.py @@ -1 +1 @@ -VERSION = '1.5.0' +VERSION = '1.6.0' diff --git a/hcloud/floating_ips/client.py b/hcloud/floating_ips/client.py index 63018a5..e5420b1 100644 --- a/hcloud/floating_ips/client.py +++ b/hcloud/floating_ips/client.py @@ -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 @@ -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, @@ -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 @@ -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 `], :class:`Meta `) """ params = {} @@ -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 `] """ - 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 ` + """ + return super(FloatingIPsClient, self).get_by_name(name) def create(self, type, # type: str diff --git a/tests/integration/floating_ips/test_floating_ips.py b/tests/integration/floating_ips/test_floating_ips.py index e01f222..373d8ad 100644 --- a/tests/integration/floating_ips/test_floating_ips.py +++ b/tests/integration/floating_ips/test_floating_ips.py @@ -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 diff --git a/tests/unit/floating_ips/conftest.py b/tests/unit/floating_ips/conftest.py index 787f460..6bdd635 100644 --- a/tests/unit/floating_ips/conftest.py +++ b/tests/unit/floating_ips/conftest.py @@ -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 { diff --git a/tests/unit/floating_ips/test_client.py b/tests/unit/floating_ips/test_client.py index fe81075..5fa7593 100644 --- a/tests/unit/floating_ips/test_client.py +++ b/tests/unit/floating_ips/test_client.py @@ -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