Skip to content

Commit 400c019

Browse files
committed
Add missing get_by_name Method on FloatingIPsClient
Prepare Release 1.6.0
1 parent 862c833 commit 400c019

File tree

6 files changed

+82
-7
lines changed

6 files changed

+82
-7
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
History
33
=======
44

5+
1.6.0 (2019-09-17)
6+
-------------------
7+
8+
* Feature: Add missing `get_by_name` on `FloatingIPsClient`
9+
510
1.5.0 (2019-09-16)
611
-------------------
712

hcloud/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION = '1.5.0'
1+
VERSION = '1.6.0'

hcloud/floating_ips/client.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
from hcloud.actions.client import BoundAction
3-
from hcloud.core.client import BoundModelBase, ClientEntityBase
3+
from hcloud.core.client import BoundModelBase, ClientEntityBase, GetEntityByNameMixin
44
from hcloud.core.domain import add_meta_to_result
55

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

116116

117-
class FloatingIPsClient(ClientEntityBase):
117+
class FloatingIPsClient(ClientEntityBase, GetEntityByNameMixin):
118118
results_list_attribute_name = 'floating_ips'
119119

120120
def get_actions_list(self,
@@ -184,7 +184,8 @@ def get_by_id(self, id):
184184
def get_list(self,
185185
label_selector=None, # type: Optional[str]
186186
page=None, # type: Optional[int]
187-
per_page=None # type: Optional[int]
187+
per_page=None, # type: Optional[int]
188+
name=None, # type: Optional[str]
188189
):
189190
# type: (...) -> PageResults[List[BoundFloatingIP]]
190191
"""Get a list of floating ips from this account
@@ -195,6 +196,8 @@ def get_list(self,
195196
Specifies the page to fetch
196197
:param per_page: int (optional)
197198
Specifies how many results are returned by page
199+
:param name: str (optional)
200+
Can be used to filter networks by their name.
198201
:return: (List[:class:`BoundFloatingIP <hcloud.floating_ips.client.BoundFloatingIP>`], :class:`Meta <hcloud.core.domain.Meta>`)
199202
"""
200203
params = {}
@@ -205,21 +208,35 @@ def get_list(self,
205208
params['page'] = page
206209
if per_page:
207210
params['per_page'] = per_page
211+
if name:
212+
params['name'] = name
208213

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

212217
return self._add_meta_to_result(floating_ips, response)
213218

214-
def get_all(self, label_selector=None):
215-
# type: (Optional[str]) -> List[BoundFloatingIP]
219+
def get_all(self, label_selector=None, name=None):
220+
# type: (Optional[str], Optional[str]) -> List[BoundFloatingIP]
216221
"""Get all floating ips from this account
217222
218223
:param label_selector: str (optional)
219224
Can be used to filter Floating IPs by labels. The response will only contain Floating IPs matching the label selector.able values.
225+
:param name: str (optional)
226+
Can be used to filter networks by their name.
220227
:return: List[:class:`BoundFloatingIP <hcloud.floating_ips.client.BoundFloatingIP>`]
221228
"""
222-
return super(FloatingIPsClient, self).get_all(label_selector=label_selector)
229+
return super(FloatingIPsClient, self).get_all(label_selector=label_selector, name=name)
230+
231+
def get_by_name(self, name):
232+
# type: (str) -> BoundFloatingIP
233+
"""Get Floating IP by name
234+
235+
:param name: str
236+
Used to get Floating IP by name.
237+
:return: :class:`BoundFloatingIP <hcloud.floating_ips.client.BoundFloatingIP>`
238+
"""
239+
return super(FloatingIPsClient, self).get_by_name(name)
223240

224241
def create(self,
225242
type, # type: str

tests/integration/floating_ips/test_floating_ips.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ def test_get_by_id(self, hetzner_client):
5959
assert bound_floating_ip.description == "Web Frontend"
6060
assert bound_floating_ip.type == "ipv4"
6161

62+
def test_get_by_name(self, hetzner_client):
63+
bound_floating_ip = hetzner_client.floating_ips.get_by_name("Web Frontend")
64+
assert bound_floating_ip.id == 4711
65+
assert bound_floating_ip.name == "Web Frontend"
66+
assert bound_floating_ip.description == "Web Frontend"
67+
assert bound_floating_ip.type == "ipv4"
68+
6269
def test_get_list(self, hetzner_client):
6370
result = hetzner_client.floating_ips.get_list()
6471
bound_floating_ips = result.floating_ips

tests/unit/floating_ips/conftest.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,43 @@ def floating_ip_response():
3636
}
3737

3838

39+
@pytest.fixture()
40+
def one_floating_ips_response():
41+
return {
42+
"floating_ips": [
43+
{
44+
"id": 4711,
45+
"description": "Web Frontend",
46+
"name": "Web Frontend",
47+
"created": "2016-01-30T23:50+00:00",
48+
"ip": "131.232.99.1",
49+
"type": "ipv4",
50+
"server": 42,
51+
"dns_ptr": [
52+
{
53+
"ip": "2001:db8::1",
54+
"dns_ptr": "server.example.com"
55+
}
56+
],
57+
"home_location": {
58+
"id": 1,
59+
"name": "fsn1",
60+
"description": "Falkenstein DC Park 1",
61+
"country": "DE",
62+
"city": "Falkenstein",
63+
"latitude": 50.47612,
64+
"longitude": 12.370071
65+
},
66+
"blocked": False,
67+
"protection": {
68+
"delete": False
69+
},
70+
"labels": {}
71+
},
72+
]
73+
}
74+
75+
3976
@pytest.fixture()
4077
def two_floating_ips_response():
4178
return {

tests/unit/floating_ips/test_client.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ def test_get_by_id(self, floating_ips_client, floating_ip_response):
129129
assert bound_floating_ip.id == 4711
130130
assert bound_floating_ip.description == "Web Frontend"
131131

132+
def test_get_by_name(self, floating_ips_client, one_floating_ips_response):
133+
floating_ips_client._client.request.return_value = one_floating_ips_response
134+
bound_floating_ip = floating_ips_client.get_by_name("Web Frontend")
135+
floating_ips_client._client.request.assert_called_with(url="/floating_ips", method="GET", params={"name": "Web Frontend"})
136+
assert bound_floating_ip._client is floating_ips_client
137+
assert bound_floating_ip.id == 4711
138+
assert bound_floating_ip.name == "Web Frontend"
139+
assert bound_floating_ip.description == "Web Frontend"
140+
132141
@pytest.mark.parametrize("params", [{'label_selector': "label1", 'page': 1, 'per_page': 10}, {}])
133142
def test_get_list(self, floating_ips_client, two_floating_ips_response, params):
134143
floating_ips_client._client.request.return_value = two_floating_ips_response

0 commit comments

Comments
 (0)