libcloud-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject svn commit: r1081248 - in /incubator/libcloud/trunk: libcloud/pricing.py test/pricing_test.json test/test_pricing.py
Date Mon, 14 Mar 2011 03:04:11 GMT
Author: tomaz
Date: Mon Mar 14 03:04:10 2011
New Revision: 1081248

URL: http://svn.apache.org/viewvc?rev=1081248&view=rev
Log:
Add pricing module.

Added:
    incubator/libcloud/trunk/libcloud/pricing.py
    incubator/libcloud/trunk/test/pricing_test.json
    incubator/libcloud/trunk/test/test_pricing.py

Added: incubator/libcloud/trunk/libcloud/pricing.py
URL: http://svn.apache.org/viewvc/incubator/libcloud/trunk/libcloud/pricing.py?rev=1081248&view=auto
==============================================================================
--- incubator/libcloud/trunk/libcloud/pricing.py (added)
+++ incubator/libcloud/trunk/libcloud/pricing.py Mon Mar 14 03:04:10 2011
@@ -0,0 +1,69 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+"""
+A class which handles loading the pricing files.
+"""
+
+try:
+    import json
+except:
+    import simplejson as json
+
+PRICING_FILE_PATH = 'data/pricing.json'
+
+PRICING_DATA = {
+    'compute': {},
+    'storage': {}
+}
+
+def get_pricing(driver_type, driver_name, pricing_file_path=None):
+    """
+    Return pricing for the provided driver.
+
+    @type driver_type: C{str}
+    @param driver_type: Driver type ('compute' or 'storage')
+
+    @type driver_name: C{str}
+    @param driver_name: Driver name
+
+    @return C{dict} Dictionary with pricing where a key name iz size ID and
+                    the value is a price.
+    """
+    if not driver_type in [ 'compute', 'storage' ]:
+        raise AttributeError('Invalid driver type: %s', driver_type)
+
+    driver_name = driver_name.lower().replace('nodedriver', '')
+
+    if driver_name in PRICING_DATA[driver_type]:
+        return PRICING_DATA[driver_type][driver_name]
+
+    if not pricing_file_path:
+        pricing_file_path = PRICING_FILE_PATH
+
+    with open(pricing_file_path) as fp:
+        content = fp.read()
+
+    pricing = json.loads(content)[driver_name]
+
+    PRICING_DATA[driver_type][driver_name] = pricing
+    return pricing
+
+def invalidate_pricing_cache():
+    PRICING_DATA['compute'] = {}
+    PRICING_DATA['storage'] = {}
+
+def invalidate_module_pricing_cache(driver_type, driver_name):
+    if driver_name in PRICING_DATA[driver_type]:
+        del PRICING_DATA[driver_type][driver_name]

Added: incubator/libcloud/trunk/test/pricing_test.json
URL: http://svn.apache.org/viewvc/incubator/libcloud/trunk/test/pricing_test.json?rev=1081248&view=auto
==============================================================================
--- incubator/libcloud/trunk/test/pricing_test.json (added)
+++ incubator/libcloud/trunk/test/pricing_test.json Mon Mar 14 03:04:10 2011
@@ -0,0 +1,6 @@
+{
+    "foo": {
+        "1": 1.00,
+        "2": 2.00
+    }
+}

Added: incubator/libcloud/trunk/test/test_pricing.py
URL: http://svn.apache.org/viewvc/incubator/libcloud/trunk/test/test_pricing.py?rev=1081248&view=auto
==============================================================================
--- incubator/libcloud/trunk/test/test_pricing.py (added)
+++ incubator/libcloud/trunk/test/test_pricing.py Mon Mar 14 03:04:10 2011
@@ -0,0 +1,68 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import unittest
+
+import libcloud.pricing
+
+class PricingTestCase(unittest.TestCase):
+
+    def test_get_pricing_success(self):
+        self.assertFalse('foo' in libcloud.pricing.PRICING_DATA['compute'])
+
+        pricing = libcloud.pricing.get_pricing(driver_type='compute',
+                                               driver_name='foo',
+                                               pricing_file_path='test/pricing_test.json')
+        self.assertEqual(pricing['1'], 1.0)
+        self.assertEqual(pricing['2'], 2.0)
+
+        self.assertEqual(libcloud.pricing.PRICING_DATA['compute']['foo']['1'], 1.0)
+        self.assertEqual(libcloud.pricing.PRICING_DATA['compute']['foo']['2'], 2.0)
+
+    def test_get_pricing_invalid_file_path(self):
+        try:
+            get_pricing(driver_type='compute', driver_name='bar',
+                        pricing_file_path='inexistent.json')
+        except Exception:
+            pass
+        else:
+            self.fail('Invalid pricing file path provided, but an exception was not'
+                       ' thrown')
+
+    def test_get_pricing_invalid_driver_type(self):
+        try:
+            get_pricing(driver_type='invalid_type', driver_name='bar',
+                        pricing_file_path='inexistent.json')
+        except Exception:
+            pass
+        else:
+            self.fail('Invalid driver_type provided, but an exception was not'
+                       ' thrown')
+
+    def test_invalid_pricing_cache(self):
+        libcloud.pricing.PRICING_DATA['compute']['foo'] = { 2: 2 }
+        self.assertTrue('foo' in libcloud.pricing.PRICING_DATA['compute'])
+
+        libcloud.pricing.invalidate_pricing_cache()
+        self.assertFalse('foo' in libcloud.pricing.PRICING_DATA['compute'])
+
+    def test_invalid_module_pricing_cache(self):
+        libcloud.pricing.PRICING_DATA['compute']['foo'] = { 1:1 }
+
+        self.assertTrue('foo' in libcloud.pricing.PRICING_DATA['compute'])
+
+        libcloud.pricing.invalidate_module_pricing_cache(driver_type='compute',
+                                                         driver_name='foo')
+        self.assertFalse('foo' in libcloud.pricing.PRICING_DATA['compute'])



Mime
View raw message