diff --git a/jumpgate/volume/drivers/sl/volumes.py b/jumpgate/volume/drivers/sl/volumes.py index cf24e47..7ed54d2 100644 --- a/jumpgate/volume/drivers/sl/volumes.py +++ b/jumpgate/volume/drivers/sl/volumes.py @@ -249,6 +249,8 @@ def on_post(self, req, resp, tenant_id): "SoftLayerAPIError", e.faultString, code=e.faultCode) + except IndexError as e: + return error_handling.bad_request(resp, str(e)) except Exception as e: return error_handling.volume_fault(resp, str(e)) @@ -295,25 +297,31 @@ def _match_portable_storage_prices(packageId, size, exact_capacity): price_matrix = {} for x in price_list: price_matrix.update({int(x['capacity']): x['prices']}) - # find the closet capacity to the requested size - if exact_capacity: - ret = False - for x in price_matrix: - if size == x: - ret = True - capacity_idx = x - break - if not ret: - raise SoftLayer.SoftLayerAPIError( - HTTP.BAD_REQUEST, - 'volume_types: extra_specs: ' - 'drivers:exact_capacity is set to' - ' True and there is no volume with' - ' matching capacity') - else: - capacity_idx = min(price_matrix, key=lambda x: abs(x - size)) - - return price_matrix[capacity_idx] + try: + # find the closest capacity to the requested size + if exact_capacity: + ret = False + for x in price_matrix: + if size == x: + ret = True + capacity_idx = x + break + if not ret: + raise SoftLayer.SoftLayerAPIError( + HTTP.BAD_REQUEST, + 'volume_types: extra_specs: ' + 'drivers:exact_capacity is set to' + ' True and there is no volume with' + ' matching capacity') + else: + capacity_idx = sorted([cap for cap in price_matrix + if cap >= size])[0] + + return price_matrix[capacity_idx] + except IndexError: + msg = "Unsupported volume size: %d" % size + LOG.error(msg) + raise IndexError(msg) def _find_availibility_zone_location(zone): # make sure there is an availability_zone selected