From 00ba17965ad90d6ebed38f867c093aa6c0d4bb2d Mon Sep 17 00:00:00 2001 From: Brian Waldon Date: Sun, 20 Jan 2013 16:29:32 -0800 Subject: [PATCH] Add library support for v2 image update Related to bp glance-client-v2 Change-Id: Ia6fe16e462ce8827175577cbed8e15c326bf8ad3 --- glanceclient/v2/images.py | 22 ++++++++++++++++++++++ tests/v2/test_images.py | 25 ++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/glanceclient/v2/images.py b/glanceclient/v2/images.py index 680c5fc3..cc19ec88 100644 --- a/glanceclient/v2/images.py +++ b/glanceclient/v2/images.py @@ -75,3 +75,25 @@ class Controller(object): def delete(self, image_id): """Delete an image.""" self.http_client.json_request('DELETE', 'v2/images/%s' % image_id) + + def update(self, image_id, **kwargs): + """ + Update attributes of an image. + + :param image_id: ID of the image to modify. + :param **kwargs: Image attribute names and their new values. + """ + image = self.get(image_id) + for (key, value) in kwargs.items(): + setattr(image, key, value) + + url = '/v2/images/%s' % image_id + hdrs = {'Content-Type': 'application/openstack-images-v2.0-json-patch'} + self.http_client.raw_request('PATCH', url, + headers=hdrs, + body=image.patch) + + #NOTE(bcwaldon): calling image.patch doesn't clear the changes, so + # we need to fetch the image again to get a clean history. This is + # an obvious optimization for warlock + return self.get(image_id) diff --git a/tests/v2/test_images.py b/tests/v2/test_images.py index 36011d90..766951a0 100644 --- a/tests/v2/test_images.py +++ b/tests/v2/test_images.py @@ -72,6 +72,10 @@ fixtures = { 'name': 'image-1', }, ), + 'PATCH': ( + {}, + '', + ), }, 'v2/images/87b634c1-f893-33c9-28a9-e5673c99239a': { 'DELETE': ( @@ -102,7 +106,7 @@ fixtures = { }, 'CCC', ), - } + }, } @@ -180,3 +184,22 @@ class TestController(testtools.TestCase): body = self.controller.data('1b1c6366-dd57-11e1-af0f-02163e68b1d8') body = ''.join([b for b in body]) self.assertEqual(body, 'CCC') + + def test_update_without_data(self): + image_id = '3a4560a1-e585-443e-9b39-553b46ec92d1' + params = {'name': 'pong'} + image = self.controller.update(image_id, **params) + expect_hdrs = { + 'Content-Type': 'application/openstack-images-v2.0-json-patch', + } + expect_body = '[{"path": "/name", "value": "pong", "op": "replace"}]' + expect = [ + ('GET', '/v2/images/%s' % image_id, {}, None), + ('PATCH', '/v2/images/%s' % image_id, expect_hdrs, expect_body), + ('GET', '/v2/images/%s' % image_id, {}, None), + ] + self.assertEqual(self.api.calls, expect) + self.assertEqual(image.id, image_id) + #NOTE(bcwaldon): due to limitations of our fake api framework, the name + # will not actually change - yet in real life it will... + self.assertEqual(image.name, 'image-1')