From 191abebaa91688cc081676ed3b1e050b9a2675c1 Mon Sep 17 00:00:00 2001 From: Andrey Pavlov Date: Tue, 8 Sep 2015 13:58:56 +0300 Subject: [PATCH] Adding return of updated image for update_image, update_tags update_image and update_tags don't return updated images that requires to make extra "get" call. This change will not break backward compatibility because the only change is replacement of None value with Image object. Change-Id: Ifd9ddabdf03f5e8f9342faab8f349777512a0eb6 Closes-bug: #1493325 --- saharaclient/api/base.py | 18 ++++++++++++++++++ saharaclient/api/images.py | 19 ++++++------------- saharaclient/tests/unit/test_images.py | 14 ++++++++++---- 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/saharaclient/api/base.py b/saharaclient/api/base.py index ab534cb1..a4fc78ba 100644 --- a/saharaclient/api/base.py +++ b/saharaclient/api/base.py @@ -139,6 +139,24 @@ class ResourceManager(object): return self.resource_class(self, data) + def _post(self, url, data, response_key=None, dump_json=True): + if dump_json: + kwargs = {'json': data} + else: + kwargs = {'data': data} + + resp = self.api.post(url, **kwargs) + + if resp.status_code != 202: + self._raise_api_exception(resp) + + if response_key is not None: + data = get_json(resp)[response_key] + else: + data = get_json(resp) + + return self.resource_class(self, data) + def _list(self, url, response_key): resp = self.api.get(url) if resp.status_code == 200: diff --git a/saharaclient/api/images.py b/saharaclient/api/images.py index 217309db..66a89ca3 100644 --- a/saharaclient/api/images.py +++ b/saharaclient/api/images.py @@ -38,9 +38,7 @@ class ImageManager(base.ResourceManager): body = {"username": user_name, "description": desc} - resp = self.api.post('/images/%s' % image_id, json=body) - if resp.status_code != 202: - raise RuntimeError('Failed to register image %s' % image_id) + return self._post('/images/%s' % image_id, body) def update_tags(self, image_id, new_tags): old_image = self.get(image_id) @@ -52,16 +50,11 @@ class ImageManager(base.ResourceManager): to_remove = list(old_tags - new_tags) if len(to_add) != 0: - resp = self.api.post('/images/%s/tag' % image_id, - json={'tags': to_add}) - - if resp.status_code != 202: - raise RuntimeError('Failed to add tags to image %s' % image_id) + return self._post('/images/%s/tag' % image_id, + {'tags': to_add}, 'image') if len(to_remove) != 0: - resp = self.api.post('/images/%s/untag' % image_id, - json={'tags': to_remove}) + return self._post('/images/%s/untag' % image_id, + {'tags': to_remove}, 'image') - if resp.status_code != 202: - raise RuntimeError('Failed to remove tags from image %s' % - image_id) + return self._get('/images/%s' % image_id, 'image') diff --git a/saharaclient/tests/unit/test_images.py b/saharaclient/tests/unit/test_images.py index fbc9ba56..b158fedd 100644 --- a/saharaclient/tests/unit/test_images.py +++ b/saharaclient/tests/unit/test_images.py @@ -69,16 +69,22 @@ class ImageTest(base.BaseTestCase): tag_url = self.URL + '/images/id/tag' untag_url = self.URL + '/images/id/untag' - self.responses.post(tag_url, status_code=202) - self.responses.post(untag_url, status_code=202) + self.responses.post(tag_url, json={'image': self.body}, + status_code=202) + self.responses.post(untag_url, json={'image': self.body}, + status_code=202) image = mock.Mock() mget.return_value = image image.tags = [] - self.client.images.update_tags('id', ['username', 'tag']) + resp = self.client.images.update_tags('id', ['username', 'tag']) self.assertEqual(tag_url, self.responses.last_request.url) + self.assertIsInstance(resp, images.Image) + self.assertFields(self.body, resp) image.tags = ['username', 'tag'] - self.client.images.update_tags('id', ['username']) + resp = self.client.images.update_tags('id', ['username']) self.assertEqual(untag_url, self.responses.last_request.url) + self.assertIsInstance(resp, images.Image) + self.assertFields(self.body, resp)