diff --git a/tuskarclient/common/base.py b/tuskarclient/common/base.py index beb0c4a..c8fa0f2 100644 --- a/tuskarclient/common/base.py +++ b/tuskarclient/common/base.py @@ -47,6 +47,26 @@ class Manager(object): def __init__(self, api): self.api = api + @staticmethod + def _path(id=None): + """Helper method to be defined in subclasses. It returns the + resource/collection path. If id is given, then single resource + path is returned. Otherwise the collection path is returned. + + :param id: id of the resource (optional) + """ + raise NotImplementedError("_path method not implemented.") + + def _single_path(self, id): + """This is like the _path method, but it asserts that the rack_id + parameter is not None. This is useful e.g. when you want to make sure + that you can't issue a DELETE request on a collection URL. + """ + if not id: + raise ValueError("{0} id for deletion must not be null." + .format(self.resource_class)) + return self._path(id) + def _create(self, url, body): resp, body = self.api.json_request('POST', url, body=body) if body: diff --git a/tuskarclient/tests/common/test_base.py b/tuskarclient/tests/common/test_base.py index dd35939..45a4d8a 100644 --- a/tuskarclient/tests/common/test_base.py +++ b/tuskarclient/tests/common/test_base.py @@ -42,3 +42,14 @@ class ManagerTest(tutils.TestCase): self.m._list.assert_called_with('url', response_key='response_key', obj_class='obj_class', body='body', expect_single=True) + + def test_path(self): + self.assertRaises(NotImplementedError, self.m._path) + + def test_single_path(self): + self.m._path = mock.Mock(return_value='/v1/somethings/42') + self.m._single_path(42) + self.m._path.assert_called_with(42) + + def test_single_path_without_id(self): + self.assertRaises(ValueError, self.m._single_path, None)