Repository: libcloud Updated Branches: refs/heads/trunk 71e38407e -> 62477a421
[google compute] make better use of GCEDiskType Closes #428 Signed-off-by: Eric Johnson <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/62477a42 Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/62477a42 Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/62477a42 Branch: refs/heads/trunk Commit: 62477a4217e808992deb4992a4cdd0ccde3b595c Parents: 71e3840 Author: Eric Johnson <[email protected]> Authored: Wed Jan 7 22:24:08 2015 +0000 Committer: Eric Johnson <[email protected]> Committed: Thu Jan 8 14:26:11 2015 +0000 ---------------------------------------------------------------------- CHANGES.rst | 4 ++ demos/gce_demo.py | 11 ++--- libcloud/compute/drivers/gce.py | 32 +++++++++--- ...nes_us-central1-a_diskTypes_pd_standard.json | 10 ++++ libcloud/test/compute/test_gce.py | 52 ++++++++++++++++++-- 5 files changed, 92 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/libcloud/blob/62477a42/CHANGES.rst ---------------------------------------------------------------------- diff --git a/CHANGES.rst b/CHANGES.rst index b2145ce..4037b87 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -16,6 +16,10 @@ General Compute ~~~~~~~ +- GCE driver updated to make better use of GCEDiskTypes + (GITHUB-428) + [Eric Johnson] + - GCE driver list_images() now returns all non-deprecated images by default (LIBCLOUD-602, GITHUB-423) [Eric Johnson] http://git-wip-us.apache.org/repos/asf/libcloud/blob/62477a42/demos/gce_demo.py ---------------------------------------------------------------------- diff --git a/demos/gce_demo.py b/demos/gce_demo.py index 9e370ce..e3a445e 100755 --- a/demos/gce_demo.py +++ b/demos/gce_demo.py @@ -293,7 +293,7 @@ def main(): # == Setting Metadata for Node == print('Setting Metadata for %s' % node_2.name) - if gce.ex_set_node_metadata(node_2, {'foo': 'bar'}): + if gce.ex_set_node_metadata(node_2, {'foo': 'bar', 'baz': 'foobarbaz'}): print(' Metadata updated for %s' % node_2.name) check_node = gce.ex_get_node(node_2.name) print(' New Metadata: %s' % check_node.extra['metadata']) @@ -306,7 +306,7 @@ def main(): multi_nodes = gce.ex_create_multiple_nodes(base_name, size, image, number, ex_tags=['libcloud'], - ex_disk_auto_delete=False) + ex_disk_auto_delete=True) for node in multi_nodes: print(' Node %s created.' % node.name) @@ -341,9 +341,6 @@ def main(): addresses = gce.ex_list_addresses() display('Addresses', addresses) - volumes = gce.list_volumes() - display('Volumes', volumes) - firewalls = gce.ex_list_firewalls() display('Firewalls', firewalls) @@ -356,7 +353,9 @@ def main(): if CLEANUP: print('Cleaning up %s resources created.' % DEMO_BASE_NAME) clean_up(gce, DEMO_BASE_NAME, nodes, - addresses + volumes + firewalls + networks + snapshots) + addresses + firewalls + networks + snapshots) + volumes = gce.list_volumes() + clean_up(gce, DEMO_BASE_NAME, None, volumes) if __name__ == '__main__': main() http://git-wip-us.apache.org/repos/asf/libcloud/blob/62477a42/libcloud/compute/drivers/gce.py ---------------------------------------------------------------------- diff --git a/libcloud/compute/drivers/gce.py b/libcloud/compute/drivers/gce.py index 6fa32f8..254d95a 100644 --- a/libcloud/compute/drivers/gce.py +++ b/libcloud/compute/drivers/gce.py @@ -1887,7 +1887,7 @@ class GCENodeDriver(NodeDriver): :keyword ex_disk_type: Specify a pd-standard (default) disk or pd-ssd for an SSD disk. - :type ex_disk_type: ``str`` + :type ex_disk_type: ``str`` or :class:`GCEDiskType` :keyword ex_disk_auto_delete: Indicate that the boot disk should be deleted when the Node is deleted. Set to @@ -1958,6 +1958,11 @@ n raise ValueError("Cannot specify both 'ex_boot_disk' and " "'ex_disks_gce_struct'") + if not image and not ex_boot_disk and not ex_disks_gce_struct: + raise ValueError("Missing root device or image. Must specify an " + "'image', existing 'ex_boot_disk', or use the " + "'ex_disks_gce_struct'.") + location = location or self.zone if not hasattr(location, 'name'): location = self.ex_get_zone(location) @@ -1967,6 +1972,8 @@ n ex_network = self.ex_get_network(ex_network) if image and not hasattr(image, 'name'): image = self.ex_get_image(image) + if not hasattr(ex_disk_type, 'name'): + ex_disk_type = self.ex_get_disktype(ex_disk_type) # Use disks[].initializeParams to auto-create the boot disk if not ex_disks_gce_struct and not ex_boot_disk: @@ -1978,6 +1985,7 @@ n 'deviceName': name, 'initializeParams': { 'diskName': name, + 'diskType': ex_disk_type.extra['selfLink'], 'sourceImage': image.extra['selfLink'] } }] @@ -2070,7 +2078,7 @@ n :keyword ex_disk_type: Specify a pd-standard (default) disk or pd-ssd for an SSD disk. - :type ex_disk_type: ``str`` + :type ex_disk_type: ``str`` or :class:`GCEDiskType` :keyword ex_disk_auto_delete: Indicate that the boot disk should be deleted when the Node is deleted. Set to @@ -2148,8 +2156,10 @@ n size = self.ex_get_size(size, location) if not hasattr(ex_network, 'name'): ex_network = self.ex_get_network(ex_network) - if not hasattr(image, 'name'): + if image and not hasattr(image, 'name'): image = self.ex_get_image(image) + if not hasattr(ex_disk_type, 'name'): + ex_disk_type = self.ex_get_disktype(ex_disk_type) node_attrs = {'size': size, 'image': image, @@ -2161,6 +2171,7 @@ n 'use_existing_disk': use_existing_disk, 'external_ip': external_ip, 'ex_disk_type': ex_disk_type, + 'ex_disk_auto_delete': ex_disk_auto_delete, 'ex_service_accounts': ex_service_accounts, 'description': description, 'ex_can_ip_forward': ex_can_ip_forward, @@ -2364,7 +2375,7 @@ n :keyword ex_disk_type: Specify a pd-standard (default) disk or pd-ssd for an SSD disk. - :type ex_disk_type: ``str`` + :type ex_disk_type: ``str`` or :class:`GCEDiskType` :return: Storage Volume object :rtype: :class:`StorageVolume` @@ -3969,7 +3980,7 @@ n :keyword ex_disk_type: Specify a pd-standard (default) disk or pd-ssd for an SSD disk. - :type ex_disk_type: ``str`` + :type ex_disk_type: ``str`` or :class:`GCEDiskType` or ``None`` :keyword ex_disk_auto_delete: Indicate that the boot disk should be deleted when the Node is deleted. Set to @@ -4081,6 +4092,11 @@ n raise ValueError("Cannot specify both 'boot_disk' and " "'ex_disks_gce_struct'. Use one or the other.") + if not image and not boot_disk and not ex_disks_gce_struct: + raise ValueError("Missing root device or image. Must specify an " + "'image', existing 'boot_disk', or use the " + "'ex_disks_gce_struct'.") + if boot_disk: if not isinstance(ex_disk_auto_delete, bool): raise ValueError("ex_disk_auto_delete field is not a bool.") @@ -4300,7 +4316,7 @@ n :type image: :class:`GCENodeImage` or ``str`` or ``None`` :keyword ex_disk_type: Specify pd-standard (default) or pd-ssd - :type ex_disk_type: ``str`` + :type ex_disk_type: ``str`` or :class:`GCEDiskType` :return: Tuple containing the request string, the data dictionary and the URL parameters @@ -4329,7 +4345,9 @@ n location = location or self.zone if not hasattr(location, 'name'): location = self.ex_get_zone(location) - if ex_disk_type.startswith('https'): + if hasattr(ex_disk_type, 'name'): + volume_data['type'] = ex_disk_type.extra['selfLink'] + elif ex_disk_type.startswith('https'): volume_data['type'] = ex_disk_type else: volume_data['type'] = 'https://www.googleapis.com/compute/' http://git-wip-us.apache.org/repos/asf/libcloud/blob/62477a42/libcloud/test/compute/fixtures/gce/zones_us-central1-a_diskTypes_pd_standard.json ---------------------------------------------------------------------- diff --git a/libcloud/test/compute/fixtures/gce/zones_us-central1-a_diskTypes_pd_standard.json b/libcloud/test/compute/fixtures/gce/zones_us-central1-a_diskTypes_pd_standard.json new file mode 100644 index 0000000..09f9bb4 --- /dev/null +++ b/libcloud/test/compute/fixtures/gce/zones_us-central1-a_diskTypes_pd_standard.json @@ -0,0 +1,10 @@ +{ + "kind": "compute#diskType", + "creationTimestamp": "2014-06-02T11:07:28.529-07:00", + "name": "pd-standard", + "description": "Standard Persistent Disk", + "validDiskSize": "10GB-10240GB", + "zone": "https://content.googleapis.com/compute/v1/projects/project_name/zones/us-central1-a", + "selfLink": "https://content.googleapis.com/compute/v1/projects/project_name/zones/us-central1-a/diskTypes/pd-standard", + "defaultDiskSizeGb": "500" +} http://git-wip-us.apache.org/repos/asf/libcloud/blob/62477a42/libcloud/test/compute/test_gce.py ---------------------------------------------------------------------- diff --git a/libcloud/test/compute/test_gce.py b/libcloud/test/compute/test_gce.py index c2fe333..d1f2b75 100644 --- a/libcloud/test/compute/test_gce.py +++ b/libcloud/test/compute/test_gce.py @@ -374,6 +374,48 @@ class GCENodeDriverTest(LibcloudTestCase, TestCaseMixin): self.assertIsInstance(node_data['serviceAccounts'][0]['scopes'], list) self.assertTrue(len(node_data['serviceAccounts'][0]['scopes']), 1) + def test_create_node_disk_opts(self): + node_name = 'node-name' + size = self.driver.ex_get_size('n1-standard-1') + image = self.driver.ex_get_image('debian-7') + boot_disk = self.driver.ex_get_volume('lcdisk') + disk_type = self.driver.ex_get_disktype('pd-ssd', 'us-central1-a') + DEMO_BASE_NAME = "lc-test" + gce_disk_struct = [ + { + "type": "PERSISTENT", + "deviceName": '%s-gstruct' % DEMO_BASE_NAME, + "initializeParams": { + "diskName": '%s-gstruct' % DEMO_BASE_NAME, + "sourceImage": image.extra['selfLink'] + }, + "boot": True, + "autoDelete": True + }, + { + "type": "SCRATCH", + "deviceName": '%s-gstruct-lssd' % DEMO_BASE_NAME, + "initializeParams": { + "diskType": disk_type.extra['selfLink'] + }, + "autoDelete": True + } + ] + + self.assertRaises(ValueError, self.driver.create_node, node_name, + size, None) + node = self.driver.create_node(node_name, size, image) + self.assertTrue(isinstance(node, Node)) + node = self.driver.create_node(node_name, size, None, + ex_boot_disk=boot_disk) + self.assertTrue(isinstance(node, Node)) + node = self.driver.create_node(node_name, size, None, + ex_disks_gce_struct=gce_disk_struct) + self.assertTrue(isinstance(node, Node)) + self.assertRaises(ValueError, self.driver.create_node, node_name, + size, None, ex_boot_disk=boot_disk, + ex_disks_gce_struct=gce_disk_struct) + def test_create_node(self): node_name = 'node-name' image = self.driver.ex_get_image('debian-7') @@ -1777,13 +1819,15 @@ class GCEMockHttp(MockHttpTestCase): return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK]) def _zones_us_central1_a_diskTypes(self, method, url, body, headers): - if method == 'GET': - body = self.fixtures.load('zones_us-central1-a_diskTypes.json') + body = self.fixtures.load('zones_us-central1-a_diskTypes.json') + return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK]) + + def _zones_us_central1_a_diskTypes_pd_standard(self, method, url, body, headers): + body = self.fixtures.load('zones_us-central1-a_diskTypes_pd_standard.json') return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK]) def _zones_us_central1_a_diskTypes_pd_ssd(self, method, url, body, headers): - if method == 'GET': - body = self.fixtures.load('zones_us-central1-a_diskTypes_pd_ssd.json') + body = self.fixtures.load('zones_us-central1-a_diskTypes_pd_ssd.json') return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK]) def _zones_us_central1_a_disks(self, method, url, body, headers):
