Repository: libcloud Updated Branches: refs/heads/trunk bf703e60c -> 416a8fdfb
[google compute] Add DiskTypes resource to GCE driver Closes #391 Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/416a8fdf Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/416a8fdf Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/416a8fdf Branch: refs/heads/trunk Commit: 416a8fdfbf3635f753fcb18d013505c81211456b Parents: bf703e6 Author: Eric Johnson <[email protected]> Authored: Tue Dec 9 21:58:03 2014 +0000 Committer: Eric Johnson <[email protected]> Committed: Tue Dec 9 21:58:03 2014 +0000 ---------------------------------------------------------------------- CHANGES.rst | 4 + libcloud/compute/drivers/gce.py | 94 ++++++++++++++++ .../fixtures/gce/aggregated_disktypes.json | 112 +++++++++++++++++++ .../gce/zones_us-central1-a_diskTypes.json | 27 +++++ .../zones_us-central1-a_diskTypes_pd_ssd.json | 10 ++ .../zones_us-central1-a_disktypes_pd-ssd.json | 10 ++ libcloud/test/compute/test_gce.py | 37 ++++++ 7 files changed, 294 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/libcloud/blob/416a8fdf/CHANGES.rst ---------------------------------------------------------------------- diff --git a/CHANGES.rst b/CHANGES.rst index 4e96e37..2467a3b 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -16,6 +16,10 @@ General Compute ~~~~~~~ +- Adding DiskTypes resource to GCE driver. + (GITHUB-391) + [Eric Johnson] + - Fix boot disk auto_delete in GCE driver. (GITHUB-412) [Igor Bogomazov] http://git-wip-us.apache.org/repos/asf/libcloud/blob/416a8fdf/libcloud/compute/drivers/gce.py ---------------------------------------------------------------------- diff --git a/libcloud/compute/drivers/gce.py b/libcloud/compute/drivers/gce.py index 31b11c8..1b2851d 100644 --- a/libcloud/compute/drivers/gce.py +++ b/libcloud/compute/drivers/gce.py @@ -26,6 +26,7 @@ from libcloud.common.google import GoogleBaseConnection from libcloud.common.google import GoogleBaseError from libcloud.common.google import ResourceNotFoundError from libcloud.common.google import ResourceExistsError +from libcloud.common.types import ProviderError from libcloud.compute.base import Node, NodeDriver, NodeImage, NodeLocation from libcloud.compute.base import NodeSize, StorageVolume, VolumeSnapshot @@ -76,6 +77,24 @@ class GCEConnection(GoogleBaseConnection): project) +class GCEDiskType(UuidMixin): + """A GCE DiskType resource.""" + def __init__(self, id, name, zone, driver, extra=None): + self.id = str(id) + self.name = name + self.zone = zone + self.driver = driver + self.extra = extra + UuidMixin.__init__(self) + + def destroy(self): + raise ProviderError("Can not destroy a DiskType resource.") + + def __repr__(self): + return '<GCEDiskType id="%s" name="%s" zone="%s">' % ( + self.id, self.name, self.zone) + + class GCEAddress(UuidMixin): """A GCE Static address.""" def __init__(self, id, name, address, region, driver, extra=None): @@ -661,6 +680,38 @@ class GCENodeDriver(NodeDriver): else: self.region = None + def ex_list_disktypes(self, zone=None): + """ + Return a list of DiskTypes for a zone or all. + + :keyword zone: The zone to return DiskTypes from. For example: + 'us-central1-a'. If None, will return DiskTypes from + self.zone. If 'all', will return all DiskTypes. + :type zone: ``str`` or ``None`` + + :return: A list of static DiskType objects. + :rtype: ``list`` of :class:`GCEDiskType` + """ + list_disktypes = [] + zone = self._set_zone(zone) + if zone is None: + request = '/aggregated/diskTypes' + else: + request = '/zones/%s/diskTypes' % (zone.name) + response = self.connection.request(request, method='GET').object + + if 'items' in response: + # The aggregated result returns dictionaries for each region + if zone is None: + for v in response['items'].values(): + zone_disktypes = [self._to_disktype(a) for a in + v.get('diskTypes', [])] + list_disktypes.extend(zone_disktypes) + else: + list_disktypes = [self._to_disktype(a) for a in + response['items']] + return list_disktypes + def ex_list_addresses(self, region=None): """ Return a list of static addresses for a region, 'global', or all. @@ -2640,6 +2691,25 @@ class GCENodeDriver(NodeDriver): self.connection.async_request(request, method='DELETE') return True + def ex_get_disktype(self, name, zone=None): + """ + Return a DiskType object based on a name and optional zone. + + :param name: The name of the DiskType + :type name: ``str`` + + :keyword zone: The zone to search for the DiskType in (set to + 'all' to search all zones) + :type zone: ``str`` :class:`GCEZone` or ``None`` + + :return: A DiskType object for the name + :rtype: :class:`GCEDiskType` + """ + zone = self._set_zone(zone) + request = '/zones/%s/diskTypes/%s' % (zone.name, name) + response = self.connection.request(request, method='GET').object + return self._to_disktype(response) + def ex_get_address(self, name, region=None): """ Return an Address object based on an address name and optional region. @@ -3484,6 +3554,30 @@ class GCENodeDriver(NodeDriver): return request, volume_data, params + def _to_disktype(self, disktype): + """ + Return a DiskType object from the json-response dictionary. + + :param disktype: The dictionary describing the disktype. + :type disktype: ``dict`` + + :return: DiskType object + :rtype: :class:`GCEDiskType` + """ + extra = {} + + zone = self.ex_get_zone(disktype['zone']) + + extra['selfLink'] = disktype.get('selfLink') + extra['creationTimestamp'] = disktype.get('creationTimestamp') + extra['description'] = disktype.get('description') + extra['valid_disk_size'] = disktype.get('validDiskSize') + extra['default_disk_size_gb'] = disktype.get('defaultDiskSizeGb') + type_id = "%s:%s" % (zone.name, disktype['name']) + + return GCEDiskType(id=type_id, name=disktype['name'], + zone=zone, driver=self, extra=extra) + def _to_address(self, address): """ Return an Address object from the json-response dictionary. http://git-wip-us.apache.org/repos/asf/libcloud/blob/416a8fdf/libcloud/test/compute/fixtures/gce/aggregated_disktypes.json ---------------------------------------------------------------------- diff --git a/libcloud/test/compute/fixtures/gce/aggregated_disktypes.json b/libcloud/test/compute/fixtures/gce/aggregated_disktypes.json new file mode 100644 index 0000000..a9a4751 --- /dev/null +++ b/libcloud/test/compute/fixtures/gce/aggregated_disktypes.json @@ -0,0 +1,112 @@ +{ + "kind": "compute#diskTypeAggregatedList", + "selfLink": "https://www.googleapis.com/compute/v1/projects/project_name/aggregated/diskTypes", + "items": { + "zones/us-central1-a": { + "diskTypes": [ + { + "kind": "compute#diskType", + "creationTimestamp": "2014-06-02T11:07:28.529-07:00", + "name": "pd-ssd", + "description": "SSD 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-ssd", + "defaultDiskSizeGb": "100" + }, + { + "kind": "compute#diskType", + "creationTimestamp": "2014-06-02T11:07:28.529-07:00", + "name": "local-ssd", + "description": "Local SSD", + "validDiskSize": "375GB-375", + "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/local-ssd", + "defaultDiskSizeGb": "375" + }, + { + "kind": "compute#diskType", + "creationTimestamp": "2014-06-02T11:07:28.530-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" + } + ] + }, + "zones/us-central1-b": { + "diskTypes": [ + { + "kind": "compute#diskType", + "creationTimestamp": "2014-06-02T11:07:28.529-07:00", + "name": "pd-ssd", + "description": "SSD Persistent Disk", + "validDiskSize": "10GB-10240GB", + "zone": "https://content.googleapis.com/compute/v1/projects/project_name/zones/us-central1-b", + "selfLink": "https://content.googleapis.com/compute/v1/projects/project_name/zones/us-central1-b/diskTypes/pd-ssd", + "defaultDiskSizeGb": "100" + }, + { + "kind": "compute#diskType", + "creationTimestamp": "2014-06-02T11:07:28.530-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-b", + "selfLink": "https://content.googleapis.com/compute/v1/projects/project_name/zones/us-central1-b/diskTypes/pd-standard", + "defaultDiskSizeGb": "500" + } + ] + }, + "zones/europe-west1-a": { + "diskTypes": [ + { + "kind": "compute#diskType", + "creationTimestamp": "2014-06-02T11:07:28.529-07:00", + "name": "pd-ssd", + "description": "SSD Persistent Disk", + "validDiskSize": "10GB-10240GB", + "zone": "https://content.googleapis.com/compute/v1/projects/project_name/zones/europe-west1-a", + "selfLink": "https://content.googleapis.com/compute/v1/projects/project_name/zones/europe-west1-a/diskTypes/pd-ssd", + "defaultDiskSizeGb": "100" + }, + { + "kind": "compute#diskType", + "creationTimestamp": "2014-06-02T11:07:28.530-07:00", + "name": "pd-standard", + "description": "Standard Persistent Disk", + "validDiskSize": "10GB-10240GB", + "zone": "https://content.googleapis.com/compute/v1/projects/project_name/zones/europe-west1-a", + "selfLink": "https://content.googleapis.com/compute/v1/projects/project_name/zones/europe-west1-a/diskTypes/pd-standard", + "defaultDiskSizeGb": "500" + } + ] + }, + "zones/europe-west1-b": { + "diskTypes": [ + { + "kind": "compute#diskType", + "creationTimestamp": "2014-06-02T11:07:28.529-07:00", + "name": "pd-ssd", + "description": "SSD Persistent Disk", + "validDiskSize": "10GB-10240GB", + "zone": "https://content.googleapis.com/compute/v1/projects/project_name/zones/europe-west1-b", + "selfLink": "https://content.googleapis.com/compute/v1/projects/project_name/zones/europe-west1-b/diskTypes/pd-ssd", + "defaultDiskSizeGb": "100" + }, + { + "kind": "compute#diskType", + "creationTimestamp": "2014-06-02T11:07:28.530-07:00", + "name": "pd-standard", + "description": "Standard Persistent Disk", + "validDiskSize": "10GB-10240GB", + "zone": "https://content.googleapis.com/compute/v1/projects/project_name/zones/europe-west1-b", + "selfLink": "https://content.googleapis.com/compute/v1/projects/project_name/zones/europe-west1-b/diskTypes/pd-standard", + "defaultDiskSizeGb": "500" + } + ] + } + } +} http://git-wip-us.apache.org/repos/asf/libcloud/blob/416a8fdf/libcloud/test/compute/fixtures/gce/zones_us-central1-a_diskTypes.json ---------------------------------------------------------------------- diff --git a/libcloud/test/compute/fixtures/gce/zones_us-central1-a_diskTypes.json b/libcloud/test/compute/fixtures/gce/zones_us-central1-a_diskTypes.json new file mode 100644 index 0000000..66fe52a --- /dev/null +++ b/libcloud/test/compute/fixtures/gce/zones_us-central1-a_diskTypes.json @@ -0,0 +1,27 @@ +{ + "kind": "compute#diskTypeList", + "selfLink": "https://www.googleapis.com/compute/v1/projects/project_name/zones/us-central1-a/diskTypes", + "id": "projects/project_name/zones/us-central1-a/diskTypes", + "items": [ + { + "kind": "compute#diskType", + "creationTimestamp": "2014-06-02T11:07:28.529-07:00", + "name": "pd-ssd", + "description": "SSD 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-ssd", + "defaultDiskSizeGb": "100" + }, + { + "kind": "compute#diskType", + "creationTimestamp": "2014-06-02T11:07:28.530-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/416a8fdf/libcloud/test/compute/fixtures/gce/zones_us-central1-a_diskTypes_pd_ssd.json ---------------------------------------------------------------------- diff --git a/libcloud/test/compute/fixtures/gce/zones_us-central1-a_diskTypes_pd_ssd.json b/libcloud/test/compute/fixtures/gce/zones_us-central1-a_diskTypes_pd_ssd.json new file mode 100644 index 0000000..eb29e18 --- /dev/null +++ b/libcloud/test/compute/fixtures/gce/zones_us-central1-a_diskTypes_pd_ssd.json @@ -0,0 +1,10 @@ +{ + "kind": "compute#diskType", + "creationTimestamp": "2014-06-02T11:07:28.529-07:00", + "name": "pd-ssd", + "description": "SSD 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-ssd", + "defaultDiskSizeGb": "100" +} http://git-wip-us.apache.org/repos/asf/libcloud/blob/416a8fdf/libcloud/test/compute/fixtures/gce/zones_us-central1-a_disktypes_pd-ssd.json ---------------------------------------------------------------------- diff --git a/libcloud/test/compute/fixtures/gce/zones_us-central1-a_disktypes_pd-ssd.json b/libcloud/test/compute/fixtures/gce/zones_us-central1-a_disktypes_pd-ssd.json new file mode 100644 index 0000000..eb29e18 --- /dev/null +++ b/libcloud/test/compute/fixtures/gce/zones_us-central1-a_disktypes_pd-ssd.json @@ -0,0 +1,10 @@ +{ + "kind": "compute#diskType", + "creationTimestamp": "2014-06-02T11:07:28.529-07:00", + "name": "pd-ssd", + "description": "SSD 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-ssd", + "defaultDiskSizeGb": "100" +} http://git-wip-us.apache.org/repos/asf/libcloud/blob/416a8fdf/libcloud/test/compute/test_gce.py ---------------------------------------------------------------------- diff --git a/libcloud/test/compute/test_gce.py b/libcloud/test/compute/test_gce.py index b46e04d..f0e399b 100644 --- a/libcloud/test/compute/test_gce.py +++ b/libcloud/test/compute/test_gce.py @@ -212,6 +212,19 @@ class GCENodeDriverTest(LibcloudTestCase, TestCaseMixin): names = [s.name for s in sizes_all] self.assertEqual(names.count('n1-standard-1'), 5) + def test_list_disktypes(self): + disktypes = self.driver.ex_list_disktypes() + disktypes_all = self.driver.ex_list_disktypes('all') + disktypes_uc1a = self.driver.ex_list_disktypes('us-central1-a') + self.assertEqual(len(disktypes), 2) + self.assertEqual(len(disktypes_all), 9) + self.assertEqual(len(disktypes_uc1a), 2) + self.assertEqual(disktypes[0].name, 'pd-ssd') + self.assertEqual(disktypes_uc1a[0].name, 'pd-ssd') + names = [v.name for v in disktypes_all] + self.assertTrue('pd-standard' in names) + self.assertTrue('local-ssd' in names) + def test_list_volumes(self): volumes = self.driver.list_volumes() volumes_all = self.driver.list_volumes('all') @@ -772,6 +785,16 @@ class GCENodeDriverTest(LibcloudTestCase, TestCaseMixin): self.assertEqual(volume.extra['status'], 'READY') self.assertEqual(volume.extra['type'], 'pd-ssd') + def test_ex_get_disktype(self): + disktype_name = 'pd-ssd' + disktype_zone = 'us-central1-a' + disktype = self.driver.ex_get_disktype(disktype_name, disktype_zone) + self.assertEqual(disktype.name, disktype_name) + self.assertEqual(disktype.zone.name, disktype_zone) + self.assertEqual(disktype.extra['description'], 'SSD Persistent Disk') + self.assertEqual(disktype.extra['valid_disk_size'], '10GB-10240GB') + self.assertEqual(disktype.extra['default_disk_size_gb'], '100') + def test_ex_get_zone(self): zone_name = 'us-central1-b' zone = self.driver.ex_get_zone(zone_name) @@ -807,6 +830,10 @@ class GCEMockHttp(MockHttpTestCase): body = self.fixtures.load('aggregated_addresses.json') return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK]) + def _aggregated_diskTypes(self, method, url, body, headers): + body = self.fixtures.load('aggregated_disktypes.json') + return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK]) + def _aggregated_disks(self, method, url, body, headers): body = self.fixtures.load('aggregated_disks.json') return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK]) @@ -1310,6 +1337,16 @@ class GCEMockHttp(MockHttpTestCase): body = self.fixtures.load('zones.json') 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') + 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') + return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK]) + def _zones_us_central1_a_disks(self, method, url, body, headers): if method == 'POST': body = self.fixtures.load('zones_us-central1-a_disks_post.json')
