diff --git a/libcloud/compute/drivers/digitalocean.py b/libcloud/compute/drivers/digitalocean.py index 5bbe28f729..b4fe9fb0cb 100644 --- a/libcloud/compute/drivers/digitalocean.py +++ b/libcloud/compute/drivers/digitalocean.py @@ -100,12 +100,20 @@ class DigitalOcean_v2_NodeDriver(DigitalOcean_v2_BaseDriver, DigitalOceanNodeDri EX_CREATE_ATTRIBUTES = ["backups", "ipv6", "private_networking", "tags", "ssh_keys"] - def list_images( - self, - location=None, - ): + def list_images(self, location=None): + """ + List images available in the given location, or all images if it is omitted. + + :param location: Location at which to list images. + :type location: :class:`.NodeLocation` + + :rtype: ``list`` of :class:`.NodeImage` + """ data = self._paginated_request("/v2/images", "images") - return list(map(self._to_image, data)) + images = [self._to_image(image) for image in data] + if location is not None: + images = [image for image in images if location.id in image.extra["regions"]] + return images def list_key_pairs(self): """ @@ -742,7 +750,7 @@ def _to_image(self, data): "distribution": data["distribution"], "public": data["public"], "slug": data["slug"], - "regions": data["regions"], + "regions": data.get("regions") or [], "min_disk_size": data["min_disk_size"], "created_at": data["created_at"], } diff --git a/libcloud/test/compute/test_digitalocean_v2.py b/libcloud/test/compute/test_digitalocean_v2.py index 84eb5f01d5..8f1c5fde27 100644 --- a/libcloud/test/compute/test_digitalocean_v2.py +++ b/libcloud/test/compute/test_digitalocean_v2.py @@ -20,7 +20,7 @@ from libcloud.test import MockHttp, LibcloudTestCase from libcloud.utils.py3 import httplib, assertRaisesRegex from libcloud.common.types import InvalidCredsError -from libcloud.compute.base import NodeImage +from libcloud.compute.base import NodeImage, NodeLocation from libcloud.test.secrets import DIGITALOCEAN_v1_PARAMS, DIGITALOCEAN_v2_PARAMS from libcloud.utils.iso8601 import UTC from libcloud.test.file_fixtures import ComputeFileFixtures @@ -82,6 +82,24 @@ def test_list_images_success(self): self.assertTrue(image.id is not None) self.assertTrue(image.name is not None) + def test_list_images_filters_by_location(self): + images = self.driver.list_images() + self.assertEqual([image.id for image in images], ["119192817", "449676376", "449676856"]) + + location = NodeLocation(id="nyc3", name="New York 3", country="US", driver=self.driver) + self.assertEqual([image.id for image in self.driver.list_images(location)], ["449676856"]) + + location = NodeLocation(id="sfo1", name="San Francisco 1", country="US", driver=self.driver) + self.assertEqual(self.driver.list_images(location), []) + + def test_list_images_with_unreported_regions(self): + DigitalOceanComputeMockHttp.type = "NO_REGIONS" + images = self.driver.list_images() + self.assertEqual([image.extra["regions"] for image in images], [[], [], ["nyc1", "nyc3"]]) + + location = NodeLocation(id="nyc1", name="New York 1", country="US", driver=self.driver) + self.assertEqual([image.id for image in self.driver.list_images(location)], ["449676856"]) + def test_list_sizes_success(self): sizes = self.driver.list_sizes() self.assertTrue(len(sizes) >= 1) @@ -452,6 +470,12 @@ def _v2_images(self, method, url, body, headers): body = self.fixtures.load("list_images.json") return (httplib.OK, body, {}, httplib.responses[httplib.OK]) + def _v2_images_NO_REGIONS(self, method, url, body, headers): + response = json.loads(self.fixtures.load("list_images.json")) + response["images"][0].pop("regions") + response["images"][1]["regions"] = None + return (httplib.OK, json.dumps(response), {}, httplib.responses[httplib.OK]) + def _v2_sizes(self, method, url, body, headers): body = self.fixtures.load("list_sizes.json") return (httplib.OK, body, {}, httplib.responses[httplib.OK])