Skip to content

Commit 1fc33b9

Browse files
authored
Merge pull request #44 from cloudblue/LITE-23925
LITE-23925: Extended the action capability to the Collection object
2 parents 6fd0b44 + 9d85564 commit 1fc33b9

3 files changed

Lines changed: 113 additions & 0 deletions

File tree

connect/client/models/base.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ def __getitem__(self, resource_id):
168168
"""
169169
return self.resource(resource_id)
170170

171+
def __call__(self, name):
172+
return self.action(name)
173+
171174
def all(self):
172175
"""
173176
Return a ResourceSet instance.
@@ -258,6 +261,29 @@ def resource(self, resource_id):
258261
f'{self._path}/{resource_id}',
259262
)
260263

264+
def action(self, name):
265+
"""
266+
Returns the action called ``name``.
267+
268+
:param name: The name of the action.
269+
:type name: str
270+
:raises TypeError: if the ``name`` is not a string.
271+
:raises ValueError: if the ``name`` is blank.
272+
:raises NotFoundError: if the ``name`` does not exist.
273+
:return: The action called ``name``.
274+
:rtype: Action
275+
"""
276+
if not isinstance(name, str):
277+
raise TypeError('`name` must be a string.')
278+
279+
if not name:
280+
raise ValueError('`name` must not be blank.')
281+
282+
return self._get_action_class()(
283+
self._client,
284+
f'{self._path}/{name}',
285+
)
286+
261287
def help(self):
262288
"""
263289
Output the collection documentation to the console.
@@ -274,6 +300,9 @@ def _get_resource_class(self):
274300
def _get_resourceset_class(self):
275301
return NotImplementedError() # pragma: no cover
276302

303+
def _get_action_class(self):
304+
raise NotImplementedError() # pragma: no cover
305+
277306

278307
class Collection(_CollectionBase, CollectionMixin):
279308
def _get_resource_class(self):
@@ -282,6 +311,9 @@ def _get_resource_class(self):
282311
def _get_resourceset_class(self):
283312
return ResourceSet
284313

314+
def _get_action_class(self):
315+
return Action
316+
285317

286318
class AsyncCollection(_CollectionBase, AsyncCollectionMixin):
287319
def _get_resource_class(self):
@@ -290,6 +322,9 @@ def _get_resource_class(self):
290322
def _get_resourceset_class(self):
291323
return AsyncResourceSet
292324

325+
def _get_action_class(self):
326+
return AsyncAction
327+
293328

294329
class _ResourceBase:
295330
"""Represent a generic resource."""

tests/async_client/test_models.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,45 @@ def test_collection_help(async_col_factory):
328328
assert col1 == col
329329

330330

331+
def test_collection_action_invalid_type(async_col_factory):
332+
collection = async_col_factory()
333+
with pytest.raises(TypeError) as cv:
334+
collection.action(None)
335+
336+
assert str(cv.value) == '`name` must be a string.'
337+
338+
with pytest.raises(TypeError) as cv:
339+
collection.action(3)
340+
341+
assert str(cv.value) == '`name` must be a string.'
342+
343+
344+
def test_collection_action_invalid_value(async_col_factory):
345+
collection = async_col_factory()
346+
with pytest.raises(ValueError) as cv:
347+
collection.action('')
348+
349+
assert str(cv.value) == '`name` must not be blank.'
350+
351+
352+
def test_collection_action(async_col_factory):
353+
collection = async_col_factory()
354+
action = collection.action('action')
355+
356+
assert isinstance(action, AsyncAction)
357+
assert action._client == collection._client
358+
assert action.path == f'{collection.path}/action'
359+
360+
361+
def test_collection_action_call(async_col_factory):
362+
collection = async_col_factory()
363+
action = collection('action')
364+
365+
assert isinstance(action, AsyncAction)
366+
assert action._client == collection._client
367+
assert action.path == f'{collection.path}/action'
368+
369+
331370
def test_resource_getattr(async_res_factory):
332371
res = async_res_factory()
333372
col = res.resources

tests/client/test_models.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,45 @@ def test_collection_help(col_factory):
296296
assert col1 == col
297297

298298

299+
def test_collection_action_invalid_type(col_factory):
300+
collection = col_factory()
301+
with pytest.raises(TypeError) as cv:
302+
collection.action(None)
303+
304+
assert str(cv.value) == '`name` must be a string.'
305+
306+
with pytest.raises(TypeError) as cv:
307+
collection.action(3)
308+
309+
assert str(cv.value) == '`name` must be a string.'
310+
311+
312+
def test_collection_action_invalid_value(col_factory):
313+
collection = col_factory()
314+
with pytest.raises(ValueError) as cv:
315+
collection.action('')
316+
317+
assert str(cv.value) == '`name` must not be blank.'
318+
319+
320+
def test_collection_action(col_factory):
321+
collection = col_factory()
322+
action = collection.action('action')
323+
324+
assert isinstance(action, Action)
325+
assert action._client == collection._client
326+
assert action.path == f'{collection.path}/action'
327+
328+
329+
def test_collection_action_call(col_factory):
330+
collection = col_factory()
331+
action = collection('action')
332+
333+
assert isinstance(action, Action)
334+
assert action._client == collection._client
335+
assert action.path == f'{collection.path}/action'
336+
337+
299338
def test_resource_getattr(res_factory):
300339
res = res_factory()
301340
col = res.resources

0 commit comments

Comments
 (0)