Repository: libcloud Updated Branches: refs/heads/trunk dcb0cef8e -> ab84b0304
added location parameter to driver's list_sizes method. This makes it possible to get location specific price Signed-off-by: Quentin Pradet <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/30d6d0b8 Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/30d6d0b8 Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/30d6d0b8 Branch: refs/heads/trunk Commit: 30d6d0b88a174802e24e5eda8b32437c39b6df48 Parents: 721a83a Author: Mika Lackman <[email protected]> Authored: Thu Dec 14 14:21:41 2017 +0200 Committer: Quentin Pradet <[email protected]> Committed: Thu Dec 14 21:40:32 2017 +0400 ---------------------------------------------------------------------- libcloud/common/upcloud.py | 24 ++++++++++--------- libcloud/compute/drivers/upcloud.py | 23 ++++++++++-------- libcloud/test/common/test_upcloud.py | 24 +++++++------------ libcloud/test/compute/test_upcloud.py | 38 ++++-------------------------- 4 files changed, 39 insertions(+), 70 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/libcloud/blob/30d6d0b8/libcloud/common/upcloud.py ---------------------------------------------------------------------- diff --git a/libcloud/common/upcloud.py b/libcloud/common/upcloud.py index c3fc6f7..de6f794 100644 --- a/libcloud/common/upcloud.py +++ b/libcloud/common/upcloud.py @@ -211,26 +211,28 @@ class PlanPrice(object): def __init__(self, zone_prices): self._zone_prices = zone_prices - def get_prices_in_zones(self, plan_name): + def get_price(self, plan_name, location=None): """ - Returns list of prices in different zones, - [{'zone_id': 'uk-lon1', 'price': 1.588'},...] - If plan is not found in a zone, price is set to None. + Returns the plan's price in location. If location + is not provided returns None :param plan_name: Name of the plan :type plan_name: ```str``` - rtype: ``list`` + :param location: Location, which price is returned (optional) + :type location: :class:`.NodeLocation` + + + rtype: ``float`` """ + if location is None: + return None server_plan_name = 'server_plan_' + plan_name - prices = [] - for zone_price in self._zone_prices: - zone_id = zone_price['name'] - price = zone_price.get(server_plan_name, {}).get('price') - prices.append({'zone_id': zone_id, 'price': price}) - return prices + if zone_price['name'] == location.id: + return zone_price.get(server_plan_name, {}).get('price') + return None class _LoginUser(object): http://git-wip-us.apache.org/repos/asf/libcloud/blob/30d6d0b8/libcloud/compute/drivers/upcloud.py ---------------------------------------------------------------------- diff --git a/libcloud/compute/drivers/upcloud.py b/libcloud/compute/drivers/upcloud.py index e0357fc..75a0a9e 100644 --- a/libcloud/compute/drivers/upcloud.py +++ b/libcloud/compute/drivers/upcloud.py @@ -106,20 +106,22 @@ class UpcloudDriver(NodeDriver): response = self.connection.request('1.2/zone') return self._to_node_locations(response.object['zones']['zone']) - def list_sizes(self): + def list_sizes(self, location=None): """ List available plans - Note: Node.price will be always None, because the pricing depends, - where the Node is hosted. Node.extra['zones'] will contain - pricing for different hosting zones. + :param location: Location of the deployement. Price depends on + location. lf location is not given or price not found for + location, price will be None (optional) + :type location: :class:`.NodeLocation` :rtype: ``list`` of :class:`NodeSize` """ prices_response = self.connection.request('1.2/price') response = self.connection.request('1.2/plan') return self._to_node_sizes(response.object['plans']['plan'], - prices_response.object['prices']['zone']) + prices_response.object['prices']['zone'], + location) def list_images(self): """ @@ -270,18 +272,19 @@ class UpcloudDriver(NodeDriver): Zone_id format [country]_[city][number], like fi_hel1""" return zone_id.split('-')[0].upper() - def _to_node_sizes(self, plans, prices): + def _to_node_sizes(self, plans, prices, location): plan_price = PlanPrice(prices) - return [self._construct_node_size(plan, plan_price) for plan in plans] + return [self._to_node_size(plan, plan_price, location) + for plan in plans] - def _construct_node_size(self, plan, plan_price): + def _to_node_size(self, plan, plan_price, location): extra = self._copy_dict(('core_number', 'storage_tier'), plan) - extra['zones'] = plan_price.get_prices_in_zones(plan['name']) return NodeSize(id=plan['name'], name=plan['name'], ram=plan['memory_amount'], disk=plan['storage_size'], bandwidth=plan['public_traffic_out'], - price=None, driver=self, + price=plan_price.get_price(plan['name'], location), + driver=self, extra=extra) def _to_node_images(self, images): http://git-wip-us.apache.org/repos/asf/libcloud/blob/30d6d0b8/libcloud/test/common/test_upcloud.py ---------------------------------------------------------------------- diff --git a/libcloud/test/common/test_upcloud.py b/libcloud/test/common/test_upcloud.py index 886a01a..4a2acb1 100644 --- a/libcloud/test/common/test_upcloud.py +++ b/libcloud/test/common/test_upcloud.py @@ -265,27 +265,21 @@ class TestUpcloudNodeDestroyer(unittest.TestCase): class TestPlanPrice(unittest.TestCase): - def test_zone_prices(self): + def setUp(self): prices = [{'name': 'uk-lon1', 'server_plan_1xCPU-1GB': {'amount': 1, 'price': 1.488}}, {'name': 'fi-hel1', 'server_plan_1xCPU-1GB': {'amount': 1, 'price': 1.588}}] - pp = PlanPrice(prices) - - zone_prices = pp.get_prices_in_zones('1xCPU-1GB') + self.pp = PlanPrice(prices) - self.assertEqual(len(zone_prices), 2) - self.assertIn({'zone_id': 'uk-lon1', 'price': 1.488}, zone_prices) - self.assertIn({'zone_id': 'fi-hel1', 'price': 1.588}, zone_prices) + def test_zone_prices(self): + location = NodeLocation(id='fi-hel1', name='Helsinki #1', country='FI', driver=None) + self.assertEqual(self.pp.get_price('1xCPU-1GB', location), 1.588) def test_plan_not_found_in_zone(self): - prices = [{'name': 'uk-lon1', 'server_plan_1xCPU-1GB': {'amount': 1, 'price': 1.488}}, - {'name': 'fi-hel1', 'server_plan_4xCPU-1GB': {'amount': 1, 'price': 1.588}}] - pp = PlanPrice(prices) - - zone_prices = pp.get_prices_in_zones('1xCPU-1GB') + location = NodeLocation(id='no_such_location', name='', country='', driver=None) + self.assertEqual(self.pp.get_price('1xCPU-1GB', location), None) - self.assertEqual(len(zone_prices), 2) - self.assertIn({'zone_id': 'uk-lon1', 'price': 1.488}, zone_prices) - self.assertIn({'zone_id': 'fi-hel1', 'price': None}, zone_prices) + def test_no_location_given(self): + self.assertEqual(self.pp.get_price('1xCPU-1GB'), None) if __name__ == '__main__': http://git-wip-us.apache.org/repos/asf/libcloud/blob/30d6d0b8/libcloud/test/compute/test_upcloud.py ---------------------------------------------------------------------- diff --git a/libcloud/test/compute/test_upcloud.py b/libcloud/test/compute/test_upcloud.py index 56aebe2..041582f 100644 --- a/libcloud/test/compute/test_upcloud.py +++ b/libcloud/test/compute/test_upcloud.py @@ -82,48 +82,18 @@ class UpcloudDriverTests(LibcloudTestCase): self.assert_object(expected_node_location, objects=locations) def test_list_sizes(self): - sizes = self.driver.list_sizes() + location = NodeLocation(id='fi-hel1', name='Helsinki #1', country='FI', driver=self.driver) + sizes = self.driver.list_sizes(location) self.assertTrue(len(sizes) >= 1) - expected_zones = [ - { - 'zone_id': 'de-fra1', - 'price': 1.488 - }, - { - 'zone_id': 'fi-dev2', - 'price': 2.232 - }, - { - 'zone_id': 'fi-hel1', - 'price': 2.232 - }, - { - 'zone_id': 'nl-ams1', - 'price': 1.488 - }, - { - 'zone_id': 'sg-sin1', - 'price': 1.488 - }, - { - 'zone_id': 'uk-lon1', - 'price': 1.488 - }, - { - 'zone_id': 'us-chi1', - 'price': 1.488 - } - ] expected_node_size = NodeSize(id='1xCPU-1GB', name='1xCPU-1GB', ram=1024, disk=30, bandwidth=2048, - price=None, + price=2.232, driver=self.driver, extra={'core_number': 1, - 'storage_tier': 'maxiops', - 'zones': expected_zones}) + 'storage_tier': 'maxiops'}) self.assert_object(expected_node_size, objects=sizes) def test_list_images(self):
