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 2012/04/15 20:36:12 UTC

svn commit: r1326389 - in /libcloud/trunk/libcloud/storage: drivers/nimbus.py providers.py types.py

Author: tomaz
Date: Sun Apr 15 18:36:12 2012
New Revision: 1326389

URL: http://svn.apache.org/viewvc?rev=1326389&view=rev
Log:
Add Nimbus.io driver stub.

Added:
    libcloud/trunk/libcloud/storage/drivers/nimbus.py
Modified:
    libcloud/trunk/libcloud/storage/providers.py
    libcloud/trunk/libcloud/storage/types.py

Added: libcloud/trunk/libcloud/storage/drivers/nimbus.py
URL: http://svn.apache.org/viewvc/libcloud/trunk/libcloud/storage/drivers/nimbus.py?rev=1326389&view=auto
==============================================================================
--- libcloud/trunk/libcloud/storage/drivers/nimbus.py (added)
+++ libcloud/trunk/libcloud/storage/drivers/nimbus.py Sun Apr 15 18:36:12 2012
@@ -0,0 +1,113 @@
+# 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 os
+import time
+import hashlib
+import hmac
+
+try:
+    import simplejson as json
+except ImportError:
+    import json
+
+from libcloud.utils.py3 import httplib
+from libcloud.utils.py3 import urlencode
+
+from libcloud.common.base import ConnectionUserAndKey, JsonResponse
+from libcloud.common.types import InvalidCredsError, LibcloudError
+from libcloud.storage.base import Object, Container, StorageDriver
+
+
+class NimbusResponse(JsonResponse):
+    valid_response_codes = [httplib.OK, httplib.NOT_FOUND, httplib.CONFLICT,
+                            httplib.BAD_REQUEST]
+
+    def success(self):
+        return self.status in self.valid_response_codes
+
+    def parse_error(self):
+        if self.status in [httplib.UNAUTHORIZED]:
+            raise InvalidCredsError(self.body)
+        raise LibcloudError('Unknown error. Status code: %d' % (self.status),
+                            driver=self.driver)
+
+
+class NimbusConnection(ConnectionUserAndKey):
+    host = 'nimbus.io'
+    responseCls = NimbusResponse
+
+    def __init__(self, *args, **kwargs):
+        self.id = kwargs.pop('id')
+        super(NimbusConnection, self).__init__(*args, **kwargs)
+
+    def pre_connect_hook(self, params, headers):
+        timestamp = str(int(time.time()))
+        signature = self._calculate_signature(user_id=self.user_id,
+                                              method=self.method,
+                                              params=params,
+                                              path=self.action,
+                                              timestamp=timestamp,
+                                              key=self.key)
+        headers['X-NIMBUS-IO-Timestamp'] = timestamp
+        headers['Authorization'] = 'NIMBUS.IO %s:%s' % (self.id, signature)
+        return params, headers
+
+    def _calculate_signature(self, user_id, method, params, path, timestamp,
+                             key):
+        if params:
+            uri_path = path + '?' + urlencode(params)
+        else:
+            uri_path = path
+
+        string_to_sign = [user_id, method, str(timestamp), uri_path]
+        string_to_sign = '\n'.join(string_to_sign)
+
+        hmac_value = hmac.new(key, string_to_sign, hashlib.sha256)
+        return hmac_value.hexdigest()
+
+
+class NimbusStorageDriver(StorageDriver):
+    name = 'Nimbus'
+    connectionCls = NimbusConnection
+
+    def __init__(self, *args, **kwargs):
+        self.user_id = kwargs['user_id']
+        super(NimbusStorageDriver, self).__init__(*args, **kwargs)
+
+    def list_containers(self):
+        response = self.connection.request('/customers/%s/collections' %
+                                           (self.connection.user_id))
+        return self._to_containers(response.object)
+
+    def create_container(self, container_name):
+        params = {'action': 'create', 'name': container_name}
+        response = self.connection.request('/customers/%s/collections' %
+                                           (self.connection.user_id),
+                                           params=params,
+                                           method='POST')
+        return self._to_container(response.object)
+
+    def _to_containers(self, data):
+        return [self._to_container(item) for item in data]
+
+    def _to_container(self, data):
+        name = data[0]
+        extra = {'date_created': data[2]}
+        return Container(name=name, extra=extra, driver=self)
+
+    def _ex_connection_class_kwargs(self):
+        result = {'id': self.user_id}
+        return result

Modified: libcloud/trunk/libcloud/storage/providers.py
URL: http://svn.apache.org/viewvc/libcloud/trunk/libcloud/storage/providers.py?rev=1326389&r1=1326388&r2=1326389&view=diff
==============================================================================
--- libcloud/trunk/libcloud/storage/providers.py (original)
+++ libcloud/trunk/libcloud/storage/providers.py Sun Apr 15 18:36:12 2012
@@ -41,7 +41,9 @@ DRIVERS = {
         ('libcloud.storage.drivers.google_storage', 'GoogleStorageDriver'),
     Provider.CLOUDFILES_SWIFT:
         ('libcloud.storage.drivers.cloudfiles',
-            'CloudFilesSwiftStorageDriver')
+         'CloudFilesSwiftStorageDriver'),
+    Provider.NIMBUS:
+        ('libcloud.storage.drivers.nimbus', 'NimbusStorageDriver')
 }
 
 def get_driver(provider):

Modified: libcloud/trunk/libcloud/storage/types.py
URL: http://svn.apache.org/viewvc/libcloud/trunk/libcloud/storage/types.py?rev=1326389&r1=1326388&r2=1326389&view=diff
==============================================================================
--- libcloud/trunk/libcloud/storage/types.py (original)
+++ libcloud/trunk/libcloud/storage/types.py Sun Apr 15 18:36:12 2012
@@ -40,6 +40,7 @@ class Provider(object):
     @cvar NINEFOLD: Ninefold
     @cvar GOOGLE_STORAGE Google Storage
     @cvar: S3_US_WEST_OREGON: Amazon S3 US West 2 (Oregon)
+    @cvar NIMBUS: Nimbus.io driver
     """
     DUMMY = 0
     CLOUDFILES_US = 1
@@ -53,6 +54,7 @@ class Provider(object):
     GOOGLE_STORAGE = 9
     S3_US_WEST_OREGON = 10
     CLOUDFILES_SWIFT = 11
+    NIMBUS = 12
 
 class ContainerError(LibcloudError):
     error_type = 'ContainerError'