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
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user