Skip to content

Commit ddae261

Browse files
authored
Add status filter for volumes, images and servers (#38)
Co-Authored-By: LKaemmerling <4281581+LKaemmerling@users.noreply.github.com>
1 parent a17b8eb commit ddae261

3 files changed

Lines changed: 55 additions & 21 deletions

File tree

hcloud/images/client.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ def __init__(self, client, data):
2020

2121
super(BoundImage, self).__init__(client, data)
2222

23-
def get_actions_list(self, sort=None, page=None, per_page=None):
24-
# type: (Optional[List[str]], Optional[int], Optional[int]) -> PageResult[BoundAction, Meta]
23+
def get_actions_list(self, sort=None, page=None, per_page=None, status=None):
24+
# type: (Optional[List[str]], Optional[int], Optional[int], Optional[List[str]]) -> PageResult[BoundAction, Meta]
2525
"""Returns a list of action objects for the image.
2626
27+
:param status: List[str] (optional)
28+
Response will have only actions with specified statuses. Choices: `running` `success` `error`
2729
:param sort: List[str] (optional)
2830
Specify how the results are sorted. Choices: `id` `id:asc` `id:desc` `command` `command:asc` `command:desc` `status` `status:asc` `status:desc` `progress` `progress:asc` `progress:desc` `started` `started:asc` `started:desc` `finished` `finished:asc` `finished:desc`
2931
:param page: int (optional)
@@ -32,17 +34,19 @@ def get_actions_list(self, sort=None, page=None, per_page=None):
3234
Specifies how many results are returned by page
3335
:return: (List[:class:`BoundAction <hcloud.actions.client.BoundAction>`], :class:`Meta <hcloud.core.domain.Meta>`)
3436
"""
35-
return self._client.get_actions_list(self, sort, page, per_page)
37+
return self._client.get_actions_list(self, sort=sort, page=page, per_page=per_page, status=status)
3638

37-
def get_actions(self, sort=None):
38-
# type: (Optional[List[str]]) -> List[BoundAction]
39+
def get_actions(self, sort=None, status=None):
40+
# type: (Optional[List[str]], Optional[List[str]]) -> List[BoundAction]
3941
"""Returns all action objects for the image.
4042
43+
:param status: List[str] (optional)
44+
Response will have only actions with specified statuses. Choices: `running` `success` `error`
4145
:param sort: List[str] (optional)
4246
Specify how the results are sorted. Choices: `id` `id:asc` `id:desc` `command` `command:asc` `command:desc` `status` `status:asc` `status:desc` `progress` `progress:asc` `progress:desc` `started` `started:asc` `started:desc` `finished` `finished:asc` `finished:desc`
4347
:return: List[:class:`BoundAction <hcloud.actions.client.BoundAction>`]
4448
"""
45-
return self._client.get_actions(self, sort)
49+
return self._client.get_actions(self, status=status, sort=sort)
4650

4751
def update(self, description=None, type=None, labels=None):
4852
# type: (Optional[str], Optional[Dict[str, str]]) -> BoundImage
@@ -85,12 +89,15 @@ def get_actions_list(self,
8589
image, # type: Image
8690
sort=None, # type: Optional[List[str]]
8791
page=None, # type: Optional[int]
88-
per_page=None # type: Optional[int]
92+
per_page=None, # type: Optional[int]
93+
status=None, # type: Optional[List[str]]
8994
):
9095
# type: (...) -> PageResults[List[BoundAction], Meta]
9196
"""Returns a list of action objects for an image.
9297
9398
:param image: :class:`BoundImage <hcloud.images.client.BoundImage>` or :class:`Image <hcloud.images.domain.Image>`
99+
:param status: List[str] (optional)
100+
Response will have only actions with specified statuses. Choices: `running` `success` `error`
94101
:param sort: List[str] (optional)
95102
Specify how the results are sorted. Choices: `id` `id:asc` `id:desc` `command` `command:asc` `command:desc` `status` `status:asc` `status:desc` `progress` `progress:asc` `progress:desc` `started` `started:asc` `started:desc` `finished` `finished:asc` `finished:desc`
96103
:param page: int (optional)
@@ -102,6 +109,8 @@ def get_actions_list(self,
102109
params = {}
103110
if sort is not None:
104111
params["sort"] = sort
112+
if status is not None:
113+
params["status"] = status
105114
if page is not None:
106115
params["page"] = page
107116
if per_page is not None:
@@ -112,17 +121,20 @@ def get_actions_list(self,
112121

113122
def get_actions(self,
114123
image, # type: Image
115-
sort=None, # type: Optional[List[str]]
124+
sort=None, # type: Optional[List[str]]
125+
status=None, # type: Optional[List[str]]
116126
):
117127
# type: (...) -> List[BoundAction]
118128
"""Returns all action objects for an image.
119129
120130
:param image: :class:`BoundImage <hcloud.images.client.BoundImage>` or :class:`Image <hcloud.images.domain.Image>`
131+
:param status: List[str] (optional)
132+
Response will have only actions with specified statuses. Choices: `running` `success` `error`
121133
:param sort: List[str] (optional)
122134
Specify how the results are sorted. Choices: `id` `command` `status` `progress` `started` `finished` . You can add one of ":asc", ":desc" to modify sort order. ( ":asc" is default)
123135
:return: List[:class:`BoundAction <hcloud.actions.client.BoundAction>`]
124136
"""
125-
return super(ImagesClient, self).get_actions(image, sort=sort)
137+
return super(ImagesClient, self).get_actions(image, sort=sort, status=status)
126138

127139
def get_by_id(self, id):
128140
# type: (int) -> BoundImage
@@ -141,7 +153,8 @@ def get_list(self,
141153
type=None, # type: Optional[List[str]]
142154
sort=None, # type: Optional[List[str]]
143155
page=None, # type: Optional[int]
144-
per_page=None # type: Optional[int]
156+
per_page=None, # type: Optional[int]
157+
status=None # type: Optional[List[str]]
145158
):
146159
# type: (...) -> PageResults[List[BoundImage]]
147160
"""Get all images
@@ -154,6 +167,8 @@ def get_list(self,
154167
Server Id linked to the image. Only available for images of type backup
155168
:param type: List[str] (optional)
156169
Choices: system snapshot backup
170+
:param status: List[str] (optional)
171+
Can be used to filter images by their status. The response will only contain images matching the status.
157172
:param sort: List[str] (optional)
158173
Choices: id id:asc id:desc name name:asc name:desc created created:asc created:desc
159174
:param page: int (optional)
@@ -177,6 +192,8 @@ def get_list(self,
177192
params['page'] = page
178193
if per_page:
179194
params['per_page'] = per_page
195+
if status:
196+
params['status'] = per_page
180197

181198
response = self._client.request(url="/images", method="GET", params=params)
182199
images = [BoundImage(self, image_data) for image_data in response['images']]
@@ -189,6 +206,7 @@ def get_all(self,
189206
bound_to=None, # type: Optional[List[str]]
190207
type=None, # type: Optional[List[str]]
191208
sort=None, # type: Optional[List[str]]
209+
status=None, # type: Optional[List[str]]
192210
):
193211
# type: (...) -> List[BoundImage]
194212
"""Get all images
@@ -201,11 +219,13 @@ def get_all(self,
201219
Server Id linked to the image. Only available for images of type backup
202220
:param type: List[str] (optional)
203221
Choices: system snapshot backup
222+
:param status: List[str] (optional)
223+
Can be used to filter images by their status. The response will only contain images matching the status.
204224
:param sort: List[str] (optional)
205225
Choices: id name created (You can add one of ":asc", ":desc" to modify sort order. ( ":asc" is default))
206226
:return: List[:class:`BoundImage <hcloud.images.client.BoundImage>`]
207227
"""
208-
return super(ImagesClient, self).get_all(name=name, label_selector=label_selector, bound_to=bound_to, type=type, sort=sort)
228+
return super(ImagesClient, self).get_all(name=name, label_selector=label_selector, bound_to=bound_to, type=type, sort=sort, status=status)
209229

210230
def get_by_name(self, name):
211231
# type: (str) -> BoundImage

hcloud/servers/client.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,8 @@ def get_list(self,
287287
name=None, # type: Optional[str]
288288
label_selector=None, # type: Optional[str]
289289
page=None, # type: Optional[int]
290-
per_page=None # type: Optional[int]
290+
per_page=None, # type: Optional[int]
291+
status=None, # type: Optional[List[str]]
291292
):
292293
# type: (...) -> PageResults[List[BoundServer], Meta]
293294
"""Get a list of servers from this account
@@ -296,6 +297,8 @@ def get_list(self,
296297
Can be used to filter servers by their name.
297298
:param label_selector: str (optional)
298299
Can be used to filter servers by labels. The response will only contain servers matching the label selector.
300+
:param status: List[str] (optional)
301+
Can be used to filter servers by their status. The response will only contain servers matching the status.
299302
:param page: int (optional)
300303
Specifies the page to fetch
301304
:param per_page: int (optional)
@@ -307,6 +310,8 @@ def get_list(self,
307310
params['name'] = name
308311
if label_selector:
309312
params['label_selector'] = label_selector
313+
if status:
314+
params["status"] = status
310315
if page:
311316
params['page'] = page
312317
if per_page:
@@ -317,17 +322,19 @@ def get_list(self,
317322
ass_servers = [BoundServer(self, server_data) for server_data in response['servers']]
318323
return self._add_meta_to_result(ass_servers, response)
319324

320-
def get_all(self, name=None, label_selector=None):
321-
# type: (Optional[str], Optional[str]) -> List[BoundServer]
325+
def get_all(self, name=None, label_selector=None, status=None):
326+
# type: (Optional[str], Optional[str], Optional[List[str]]) -> List[BoundServer]
322327
"""Get all servers from this account
323328
324329
:param name: str (optional)
325330
Can be used to filter servers by their name.
326331
:param label_selector: str (optional)
327332
Can be used to filter servers by labels. The response will only contain servers matching the label selector.
333+
:param status: List[str] (optional)
334+
Can be used to filter servers by their status. The response will only contain servers matching the status.
328335
:return: List[:class:`BoundServer <hcloud.servers.client.BoundServer>`]
329336
"""
330-
return super(ServersClient, self).get_all(name=name, label_selector=label_selector)
337+
return super(ServersClient, self).get_all(name=name, label_selector=label_selector, status=status)
331338

332339
def get_by_name(self, name):
333340
# type: (str) -> BoundServer

hcloud/volumes/client.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,16 @@ def get_by_id(self, id):
121121
response = self._client.request(url="/volumes/{volume_id}".format(volume_id=id), method="GET")
122122
return BoundVolume(self, response['volume'])
123123

124-
def get_list(self, name=None, label_selector=None, page=None, per_page=None):
125-
# type: (Optional[str], Optional[str], Optional[int], Optional[int]) -> PageResults[List[BoundVolume], Meta]
124+
def get_list(self, name=None, label_selector=None, page=None, per_page=None, status=None):
125+
# type: (Optional[str], Optional[str], Optional[int], Optional[int], Optional[List[str]]) -> PageResults[List[BoundVolume], Meta]
126126
"""Get a list of volumes from this account
127127
128128
:param name: str (optional)
129129
Can be used to filter volumes by their name.
130130
:param label_selector: str (optional)
131131
Can be used to filter volumes by labels. The response will only contain volumes matching the label selector.
132+
:param status: List[str] (optional)
133+
Can be used to filter volumes by their status. The response will only contain volumes matching the status.
132134
:param page: int (optional)
133135
Specifies the page to fetch
134136
:param per_page: int (optional)
@@ -140,6 +142,8 @@ def get_list(self, name=None, label_selector=None, page=None, per_page=None):
140142
params['name'] = name
141143
if label_selector:
142144
params['label_selector'] = label_selector
145+
if status:
146+
params["status"] = status
143147
if page is not None:
144148
params['page'] = page
145149
if per_page is not None:
@@ -149,14 +153,17 @@ def get_list(self, name=None, label_selector=None, page=None, per_page=None):
149153
volumes = [BoundVolume(self, volume_data) for volume_data in response['volumes']]
150154
return self._add_meta_to_result(volumes, response)
151155

152-
def get_all(self, label_selector=None):
153-
# type: (Optional[str]) -> List[BoundVolume]
156+
def get_all(self, label_selector=None, status=None):
157+
# type: (Optional[str], Optional[List[str]]) -> List[BoundVolume]
154158
"""Get all volumes from this account
155159
156160
:param label_selector:
161+
Can be used to filter volumes by labels. The response will only contain volumes matching the label selector.
162+
:param status: List[str] (optional)
163+
Can be used to filter volumes by their status. The response will only contain volumes matching the status.
157164
:return: List[:class:`BoundVolume <hcloud.volumes.client.BoundVolume>`]
158165
"""
159-
return super(VolumesClient, self).get_all(label_selector=label_selector)
166+
return super(VolumesClient, self).get_all(label_selector=label_selector, status=status)
160167

161168
def get_by_name(self, name):
162169
# type: (str) -> BoundVolume
@@ -267,7 +274,7 @@ def get_actions(self, volume, status=None, sort=None):
267274
Specify how the results are sorted. Choices: `id` `id:asc` `id:desc` `command` `command:asc` `command:desc` `status` `status:asc` `status:desc` `progress` `progress:asc` `progress:desc` `started` `started:asc` `started:desc` `finished` `finished:asc` `finished:desc`
268275
:return: List[:class:`BoundAction <hcloud.actions.client.BoundAction>`]
269276
"""
270-
return super(VolumesClient, self).get_actions(volume, sort=sort)
277+
return super(VolumesClient, self).get_actions(volume, status=status, sort=sort)
271278

272279
def update(self, volume, name=None, labels=None):
273280
# type:(Union[Volume, BoundVolume], Optional[str], Optional[Dict[str, str]]) -> BoundVolume

0 commit comments

Comments
 (0)