You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by to...@apache.org on 2011/03/14 16:29:23 UTC

svn commit: r1081423 - in /incubator/libcloud/trunk: libcloud/pricing.py test/test_pricing.py

Author: tomaz
Date: Mon Mar 14 15:29:23 2011
New Revision: 1081423

URL: http://svn.apache.org/viewvc?rev=1081423&view=rev
Log:
Update pricing module and add get_size_price method.

Modified:
    incubator/libcloud/trunk/libcloud/pricing.py
    incubator/libcloud/trunk/test/test_pricing.py

Modified: incubator/libcloud/trunk/libcloud/pricing.py
URL: http://svn.apache.org/viewvc/incubator/libcloud/trunk/libcloud/pricing.py?rev=1081423&r1=1081422&r2=1081423&view=diff
==============================================================================
--- incubator/libcloud/trunk/libcloud/pricing.py (original)
+++ incubator/libcloud/trunk/libcloud/pricing.py Mon Mar 14 15:29:23 2011
@@ -21,6 +21,9 @@ try:
 except:
     import simplejson as json
 
+import os.path
+from os.path import join as pjoin
+
 PRICING_FILE_PATH = 'data/pricing.json'
 
 PRICING_DATA = {
@@ -50,7 +53,8 @@ def get_pricing(driver_type, driver_name
         return PRICING_DATA[driver_type][driver_name]
 
     if not pricing_file_path:
-        pricing_file_path = PRICING_FILE_PATH
+        pricing_directory = os.path.dirname(os.path.abspath(__file__))
+        pricing_file_path = pjoin(pricing_directory, PRICING_FILE_PATH)
 
     with open(pricing_file_path) as fp:
         content = fp.read()
@@ -60,6 +64,25 @@ def get_pricing(driver_type, driver_name
     PRICING_DATA[driver_type][driver_name] = pricing
     return pricing
 
+def get_size_price(driver_type, driver_name, size_id):
+    """
+    Return price for the provided size.
+
+    @type driver_type: C{str}
+    @param driver_type: Driver type ('compute' or 'storage')
+
+    @type driver_name: C{str}
+    @param driver_name: Driver name
+
+    @type size_id: C{int/str}
+    @param size_id: Unique size ID (can be an integer or a string - depends on
+                    the driver)
+
+    @return C{int} Size price.
+    """
+    pricing = get_pricing(driver_type=driver_type, driver_name=driver_name)
+    return pricing[size_id]
+
 def invalidate_pricing_cache():
     """
     Invalidate the cache for all the drivers.

Modified: incubator/libcloud/trunk/test/test_pricing.py
URL: http://svn.apache.org/viewvc/incubator/libcloud/trunk/test/test_pricing.py?rev=1081423&r1=1081422&r2=1081423&view=diff
==============================================================================
--- incubator/libcloud/trunk/test/test_pricing.py (original)
+++ incubator/libcloud/trunk/test/test_pricing.py Mon Mar 14 15:29:23 2011
@@ -61,6 +61,17 @@ class PricingTestCase(unittest.TestCase)
             self.fail('Invalid driver provided, but an exception was not'
                        ' thrown')
 
+    def test_get_size_price(self):
+        libcloud.pricing.PRICING_DATA['compute']['foo'] = { 2: 2, '3': 3 }
+        price1 = libcloud.pricing.get_size_price(driver_type='compute',
+                                                driver_name='foo',
+                                                size_id=2)
+        price2 = libcloud.pricing.get_size_price(driver_type='compute',
+                                                driver_name='foo',
+                                                size_id='3')
+        self.assertEqual(price1, 2)
+        self.assertEqual(price3, 3)
+
     def test_invalid_pricing_cache(self):
         libcloud.pricing.PRICING_DATA['compute']['foo'] = { 2: 2 }
         self.assertTrue('foo' in libcloud.pricing.PRICING_DATA['compute'])