Fixed problem with tags update

Now tags can be added and removed simultaneously
in one call

Change-Id: I7709c408e78cf08f220f9e0eae9e8359eb5ed284
Closes-bug: #1500790
This commit is contained in:
Andrey Pavlov
2015-09-29 13:21:49 +03:00
parent 1506e94106
commit cbaf34c69f
2 changed files with 18 additions and 21 deletions

View File

@@ -49,12 +49,14 @@ class ImageManager(base.ResourceManager):
to_add = list(new_tags - old_tags)
to_remove = list(old_tags - new_tags)
if len(to_add) != 0:
return self._post('/images/%s/tag' % image_id,
{'tags': to_add}, 'image')
add_response, remove_response = None, None
if len(to_remove) != 0:
return self._post('/images/%s/untag' % image_id,
{'tags': to_remove}, 'image')
if to_add:
add_response = self._post('/images/%s/tag' % image_id,
{'tags': to_add}, 'image')
return self._get('/images/%s' % image_id, 'image')
if to_remove:
remove_response = self._post('/images/%s/untag' % image_id,
{'tags': to_remove}, 'image')
return remove_response or add_response or self.get(image_id)

View File

@@ -12,8 +12,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from saharaclient.api import images
from saharaclient.tests.unit import base
@@ -64,27 +62,24 @@ class ImageTest(base.BaseTestCase):
self.assertEqual(self.body,
json.loads(self.responses.last_request.body))
@mock.patch('saharaclient.api.images.ImageManager.get')
def test_update_tags(self, mget):
def test_update_tags(self):
url = self.URL + '/images/id'
tag_url = self.URL + '/images/id/tag'
untag_url = self.URL + '/images/id/untag'
self.responses.post(tag_url, json={'image': self.body},
status_code=202)
self.responses.post(untag_url, json={'image': self.body},
status_code=202)
body = self.body.copy()
body['tags'] = ['fake', '0.1']
image = mock.Mock()
mget.return_value = image
self.responses.post(tag_url, json={'image': body},
status_code=202)
self.responses.post(untag_url, json={'image': body},
status_code=202)
self.responses.get(url, json={'image': body})
image.tags = []
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']
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)