From 1f2143f72a3a77c79e8060b4c31d97fe801d09d2 Mon Sep 17 00:00:00 2001 From: Adam Friedman Date: Thu, 8 Feb 2018 10:00:50 +1100 Subject: [PATCH 1/3] Fix IndexError in dimensiondata list_images. Fix involves filtering out images from locations not returned by the list_locations API. This is required because the CloudControl API returns all images in the target geographic region (even ones in datacenters the user's organisation does not have access to). We therefore need to filter out those images (since we can't get a NodeLocation for them). --- libcloud/compute/drivers/dimensiondata.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/libcloud/compute/drivers/dimensiondata.py b/libcloud/compute/drivers/dimensiondata.py index 2797f506fe..2543149dd7 100644 --- a/libcloud/compute/drivers/dimensiondata.py +++ b/libcloud/compute/drivers/dimensiondata.py @@ -4161,8 +4161,18 @@ def _to_images(self, object, el_name='osImage'): images = [] locations = self.list_locations() + # The CloudControl API will return all images + # in the current geographic region (even ones in + # datacenters the user's organisation does not have access to) + # + # We therefore need to filter out those images (since we can't + # get a NodeLocation for them) + location_ids = set(location.id for location in locations) + for element in object.findall(fixxpath(el_name, TYPES_URN)): - images.append(self._to_image(element, locations)) + location_id = element.get('datacenterId') + if location_id in location_ids: + images.append(self._to_image(element, locations)) return images @@ -4170,9 +4180,8 @@ def _to_image(self, element, locations=None): location_id = element.get('datacenterId') if locations is None: locations = self.list_locations(location_id) - location = list(filter(lambda x: x.id == location_id, - locations))[0] + location = filter(lambda x: x.id == location_id, locations)[0] cpu_spec = self._to_cpu_spec(element.find(fixxpath('cpu', TYPES_URN))) if LooseVersion(self.connection.active_api_version) > LooseVersion( From f3102ef1945bcbd33aa553049ba8f7300bbb0101 Mon Sep 17 00:00:00 2001 From: Adam Friedman Date: Fri, 9 Feb 2018 14:49:34 +1100 Subject: [PATCH 2/3] Use list comprehension instead of filter(). --- libcloud/compute/drivers/dimensiondata.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libcloud/compute/drivers/dimensiondata.py b/libcloud/compute/drivers/dimensiondata.py index 2543149dd7..2cd832df15 100644 --- a/libcloud/compute/drivers/dimensiondata.py +++ b/libcloud/compute/drivers/dimensiondata.py @@ -4181,7 +4181,7 @@ def _to_image(self, element, locations=None): if locations is None: locations = self.list_locations(location_id) - location = filter(lambda x: x.id == location_id, locations)[0] + location = [match_location for match_location in locations if match_location.id == location_id][0] cpu_spec = self._to_cpu_spec(element.find(fixxpath('cpu', TYPES_URN))) if LooseVersion(self.connection.active_api_version) > LooseVersion( From 4c155a2ecc8a2023dca191a6a8bb7c1b78b0b7ed Mon Sep 17 00:00:00 2001 From: Adam Friedman Date: Fri, 9 Feb 2018 14:59:51 +1100 Subject: [PATCH 3/3] Reduce line length. --- libcloud/compute/drivers/dimensiondata.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libcloud/compute/drivers/dimensiondata.py b/libcloud/compute/drivers/dimensiondata.py index 2cd832df15..ff14313123 100644 --- a/libcloud/compute/drivers/dimensiondata.py +++ b/libcloud/compute/drivers/dimensiondata.py @@ -4181,7 +4181,7 @@ def _to_image(self, element, locations=None): if locations is None: locations = self.list_locations(location_id) - location = [match_location for match_location in locations if match_location.id == location_id][0] + location = [loc for loc in locations if loc.id == location_id][0] cpu_spec = self._to_cpu_spec(element.find(fixxpath('cpu', TYPES_URN))) if LooseVersion(self.connection.active_api_version) > LooseVersion(