From 92e4ee201a5b5f7a9cd210bf34f59e2efaca134b Mon Sep 17 00:00:00 2001 From: eddie-sheffield Date: Tue, 2 Jul 2013 16:34:00 -0400 Subject: [PATCH] Enable client library V2 to create an image. Adds support for creating an image to the client library only, not the CLI. Replaced reference to deprecated BaseException.message Related to bp glance-client-v2 Change-Id: I8e3d09d89493368d22f7b1f69f79ebd2518e289d --- glanceclient/v2/images.py | 19 +++++++++++++++++++ tests/v2/test_images.py | 17 +++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/glanceclient/v2/images.py b/glanceclient/v2/images.py index 8e42e99..b3c5ff5 100644 --- a/glanceclient/v2/images.py +++ b/glanceclient/v2/images.py @@ -15,6 +15,8 @@ import urllib +import warlock + from glanceclient.common import utils from glanceclient.openstack.common import strutils @@ -90,6 +92,23 @@ class Controller(object): """Delete an image.""" self.http_client.json_request('DELETE', 'v2/images/%s' % image_id) + def create(self, **kwargs): + """Create an image.""" + url = '/v2/images' + + image = self.model() + for (key, value) in kwargs.items(): + try: + setattr(image, key, value) + except warlock.InvalidOperation, e: + raise TypeError(unicode(message)) + + resp, body = self.http_client.json_request('POST', url, body=image) + #NOTE(esheffield): remove 'self' for now until we have an elegant + # way to pass it into the model constructor without conflict + body.pop('self', None) + return self.model(**body) + def update(self, image_id, **kwargs): """ Update attributes of an image. diff --git a/tests/v2/test_images.py b/tests/v2/test_images.py index 430c12e..0dd814d 100644 --- a/tests/v2/test_images.py +++ b/tests/v2/test_images.py @@ -88,6 +88,15 @@ fixtures = { '', ), }, + '/v2/images': { + 'POST': ( + {}, + { + 'id': '3a4560a1-e585-443e-9b39-553b46ec92d1', + 'name': 'image-1', + }, + ), + }, 'v2/images/87b634c1-f893-33c9-28a9-e5673c99239a': { 'DELETE': ( {}, @@ -329,6 +338,14 @@ class TestController(testtools.TestCase): self.assertEqual(image.id, '3a4560a1-e585-443e-9b39-553b46ec92d1') self.assertEqual(image.name, 'image-1') + def test_create_image(self): + properties = { + 'name': 'image-1' + } + image = self.controller.create(**properties) + self.assertEqual(image.id, '3a4560a1-e585-443e-9b39-553b46ec92d1') + self.assertEqual(image.name, 'image-1') + def test_delete_image(self): self.controller.delete('87b634c1-f893-33c9-28a9-e5673c99239a') expect = [