From 6d28dc974cbde8e544b6a9dcd48b9ec8cd1c7583 Mon Sep 17 00:00:00 2001 From: Zhi Yan Liu Date: Tue, 18 Nov 2014 01:11:00 +0800 Subject: [PATCH] Using proper volume size for volume creating Correct the logic that used to find the closet capacity to the requested volume size. Without this change, for example, end user even couldn't create an 1GB volume from SL backend by jumpgate. Signed-off-by: Zhi Yan Liu --- jumpgate/volume/drivers/sl/volumes.py | 46 ++++++++++++++++----------- 1 file changed, 27 insertions(+), 19 deletions(-) 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