Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions libcloud/compute/drivers/digitalocean.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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"],
}
Expand Down
26 changes: 25 additions & 1 deletion libcloud/test/compute/test_digitalocean_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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])
Expand Down