diff options
author | Jenkins <jenkins@review.openstack.org> | 2015-09-28 12:28:55 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2015-09-28 12:28:55 +0000 |
commit | 79a3a2ff3d2b090bb477f118eb43bff3c88a6890 (patch) | |
tree | 5d45f00c87558f25a59b9dfa0602667f80f1dc32 /saharaclient | |
parent | 1fa10e091ba6a02cfec3d9c9dd6684c26ab4900e (diff) | |
parent | 191abebaa91688cc081676ed3b1e050b9a2675c1 (diff) | |
download | python-saharaclient-79a3a2ff3d2b090bb477f118eb43bff3c88a6890.tar.gz |
Merge "Adding return of updated image for update_image, update_tags"
Diffstat (limited to 'saharaclient')
-rw-r--r-- | saharaclient/api/base.py | 18 | ||||
-rw-r--r-- | saharaclient/api/images.py | 19 | ||||
-rw-r--r-- | 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 ab534cb..a4fc78b 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 217309d..66a89ca 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 fbc9ba5..b158fed 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) |