Repository: libcloud Updated Branches: refs/heads/trunk ad73a0c91 -> a6961cef1
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 Closes #358 Signed-off-by: Tomaz Muraus <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/a6961cef Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/a6961cef Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/a6961cef Branch: refs/heads/trunk Commit: a6961cef1864c7dc81fd04f76dde13f14991a3fc Parents: ad73a0c Author: Katriel Traum <[email protected]> Authored: Thu Sep 11 10:15:31 2014 +0300 Committer: Tomaz Muraus <[email protected]> Committed: Tue Sep 23 14:48:27 2014 +0200 ---------------------------------------------------------------------- CHANGES.rst | 4 +++ libcloud/compute/drivers/gce.py | 61 ++++++++++++++++++++++++++++++++++ libcloud/test/compute/test_gce.py | 10 +++++- 3 files changed, 74 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/libcloud/blob/a6961cef/CHANGES.rst ---------------------------------------------------------------------- diff --git a/CHANGES.rst b/CHANGES.rst index 54bf069..fb424e2 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -112,6 +112,10 @@ Compute (GITHUB-361) [Andy Grimm] +- Add ``ex_create_image`` method to the GCE driver. + (GITHUB-358, LIBCLOUD-611) + [Katriel Traum] + Storage ~~~~~~~ http://git-wip-us.apache.org/repos/asf/libcloud/blob/a6961cef/libcloud/compute/drivers/gce.py ---------------------------------------------------------------------- diff --git a/libcloud/compute/drivers/gce.py b/libcloud/compute/drivers/gce.py index 5c0035b..bbcfbb8 100644 --- a/libcloud/compute/drivers/gce.py +++ b/libcloud/compute/drivers/gce.py @@ -1133,6 +1133,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. http://git-wip-us.apache.org/repos/asf/libcloud/blob/a6961cef/libcloud/test/compute/test_gce.py ---------------------------------------------------------------------- diff --git a/libcloud/test/compute/test_gce.py b/libcloud/test/compute/test_gce.py index 61dab26..730ef27 100644 --- a/libcloud/test/compute/test_gce.py +++ b/libcloud/test/compute/test_gce.py @@ -27,7 +27,8 @@ from libcloud.compute.drivers.gce import (GCENodeDriver, API_VERSION, GCEAddress, GCEHealthCheck, GCEFirewall, GCEForwardingRule, GCENetwork, - GCEZone) + GCEZone, + GCENodeImage) from libcloud.common.google import (GoogleBaseAuthConnection, GoogleInstalledAppAuthConnection, GoogleBaseConnection, @@ -242,6 +243,13 @@ class GCENodeDriverTest(LibcloudTestCase, TestCaseMixin): self.assertEqual(hc.extra['host'], 'lchost') self.assertEqual(hc.extra['description'], 'test healthcheck') + 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']}]
