From 439dc6fdfc9c647645e09ca5b1028c62b86757d5 Mon Sep 17 00:00:00 2001 From: Katriel Traum Date: Thu, 11 Sep 2014 10:15:31 +0300 Subject: [PATCH] [LIBCLOUD-611] Added ex_create_image for the GCE compute provider [LIBCLOUD-661] Added ex_create_image for the GCE compute provider - Fixes to docstring after pull request review [LIBCLOUD-661] Added ex_create_image for the GCE compute provider - Fixed long string in docstring --- libcloud/compute/drivers/gce.py | 62 +++++++++++++++++++++++++++++++++++++++ libcloud/test/compute/test_gce.py | 11 +++++-- 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/libcloud/compute/drivers/gce.py b/libcloud/compute/drivers/gce.py index 9914324..28b5871 100644 --- a/libcloud/compute/drivers/gce.py +++ b/libcloud/compute/drivers/gce.py @@ -1113,6 +1113,67 @@ class GCENodeDriver(NodeDriver): return self.ex_get_forwarding_rule(name) + def ex_create_image(self, name, volume, description=None, + use_existing=True, wait_for_completion=True): + """ + Create an image from the provided volume. + + :param name: The name of the image to create. + :type name: ``str`` + + :param volume: The volume to use to create the image, or the + Google Cloud Storage URI + :type volume: ``str`` or :class:`StorageVolume` + + :keyword description: Description of the new Image + :type description: ``str`` + + :keyword use_existing: If True and an image with the given name + already exists, return an object for that + image instead of attempting to create + a new image. + :type use_existing: ``bool`` + + :keyword wait_for_completion: If True, wait until the new image is + created before returning a new NodeImage + Otherwise, return a new NodeImage + instance, and let the user track the + creation progress + :type wait_for_completion: ``bool`` + + :return: A GCENodeImage object for the new image + :rtype: :class:`GCENodeImage` + + """ + image_data = {} + image_data['name'] = name + image_data['description'] = description + if isinstance(volume, StorageVolume): + image_data['sourceDisk'] = volume.extra['selfLink'] + image_data['zone'] = volume.extra['zone'].name + elif isinstance(volume, str) and \ + volume.startswith('https://') and volume.endswith('tar.gz'): + image_data['rawDisk'] = {'source': volume, 'containerType': 'TAR'} + else: + raise ValueError('Source must be instance of StorageVolume or URI') + + request = '/global/images' + + try: + if wait_for_completion: + self.connection.async_request(request, method='POST', + data=image_data) + else: + self.connection.request(request, method='POST', + data=image_data) + + except ResourceExistsError: + e = sys.exc_info()[1] + if not use_existing: + raise e + + return self.ex_get_image(name) + def ex_create_network(self, name, cidr): """ Create a network. @@ -3184,6 +3245,7 @@ class GCENodeDriver(NodeDriver): extra['creationTimestamp'] = image.get('creationTimestamp') extra['selfLink'] = image.get('selfLink') extra['deprecated'] = image.get('deprecated', None) + extra['status'] = image.get('status') return GCENodeImage(id=image['id'], name=image['name'], driver=self, extra=extra) diff --git a/libcloud/test/compute/test_gce.py b/libcloud/test/compute/test_gce.py index 74cc8b6..1806fb8 100644 --- a/libcloud/test/compute/test_gce.py +++ b/libcloud/test/compute/test_gce.py @@ -26,8 +26,8 @@ from libcloud.compute.drivers.gce import (GCENodeDriver, API_VERSION, timestamp_to_datetime, GCEAddress, GCEHealthCheck, GCEFirewall, GCEForwardingRule, - GCENetwork, - GCEZone) + GCENetwork, GCEZone, + GCENodeImage) from libcloud.common.google import (GoogleBaseAuthConnection, GoogleInstalledAppAuthConnection, GoogleBaseConnection, @@ -239,6 +239,13 @@ class GCENodeDriverTest(LibcloudTestCase, TestCaseMixin): self.assertEqual(hc.port, 8000) self.assertEqual(hc.interval, 10) + def test_ex_create_image(self): + volume = self.driver.ex_get_volume('lcdisk') + image = self.driver.ex_create_image('coreos', volume) + self.assertTrue(isinstance(image, GCENodeImage)) + self.assertEquals(image.name, 'coreos') + self.assertEquals(image.extra['description'], 'CoreOS test image') + def test_ex_create_firewall(self): firewall_name = 'lcfirewall' allowed = [{'IPProtocol': 'tcp', 'ports': ['4567']}] -- 1.9.3