Просмотр исходного кода

Added openstack provider implementation

nuwan_ag 10 лет назад
Родитель
Сommit
478dc0eac5
2 измененных файлов с 70 добавлено и 2 удалено
  1. 5 2
      cloudbridge/providers/factory.py
  2. 65 0
      cloudbridge/providers/openstack/__init__.py

+ 5 - 2
cloudbridge/providers/factory.py

@@ -1,11 +1,14 @@
 import importlib
 import importlib
 
 
+
 class ProviderList():
 class ProviderList():
     EC2 = 'ec2'
     EC2 = 'ec2'
     OPENSTACK = 'openstack'
     OPENSTACK = 'openstack'
     AZURE = 'azure'
     AZURE = 'azure'
 
 
+
 class CloudProviderFactory():
 class CloudProviderFactory():
+
     """
     """
     Get info and handle on the available cloud provider implementations.
     Get info and handle on the available cloud provider implementations.
     """
     """
@@ -23,7 +26,7 @@ class CloudProviderFactory():
         """
         """
         return [{"name": "openstack",
         return [{"name": "openstack",
                  "implementation":
                  "implementation":
-                 [{"class": "cloudbridge.providers.openstack.OpenstackCloudProviderV1",
+                 [{"class": "cloudbridge.providers.openstack.OpenStackCloudProviderV1",
                    "version": 1}]},
                    "version": 1}]},
                 {"name": "ec2",
                 {"name": "ec2",
                  "implementation":
                  "implementation":
@@ -88,4 +91,4 @@ class CloudProviderFactory():
             module_name, class_name = provider.rsplit(".", 1)
             module_name, class_name = provider.rsplit(".", 1)
             provider_class = getattr(importlib.import_module(module_name),
             provider_class = getattr(importlib.import_module(module_name),
                                      class_name)
                                      class_name)
-            return provider_class(config)
+            return provider_class(config)

+ 65 - 0
cloudbridge/providers/openstack/__init__.py

@@ -0,0 +1,65 @@
+"""
+Provider implementation based on boto library for EC2-compatible clouds.
+"""
+
+import os
+from novaclient import client
+from cloudbridge.providers.interfaces import CloudProvider
+from cloudbridge.providers.interfaces import SecurityService
+from cloudbridge.providers.interfaces import KeyPair
+
+
+class OpenStackCloudProviderV1(CloudProvider):
+
+    def __init__(self, config):
+        self.config = config
+
+        # Initialize optional fields
+        if isinstance(config, dict):
+            self.api_version = config.get(
+                'api_version', os.environ.get('OS_COMPUTE_API_VERSION', 2))
+            self.username = config.get('username', os.environ.get('OS_USERNAME', None))
+            self.password = config.get('password', os.environ.get('OS_PASSWORD', None))
+            self.tenant_name = config.get('tenant_name', os.environ.get('OS_TENANT_NAME', None))
+            self.auth_url = config.get('auth_url', os.environ.get('OS_AUTH_URL', None))
+        else:
+            self.api_version = config.api_version if hasattr(
+                config, 'api_version') and config.api_version else os.environ.get('OS_COMPUTE_API_VERSION', None)
+            self.username = config.username if hasattr(
+                config, 'username') and config.username else os.environ.get('OS_USERNAME', None)
+            self.password = config.password if hasattr(
+                config, 'password') and config.password else os.environ.get('OS_PASSWORD', None)
+            self.tenant_name = config.tenant_name if hasattr(
+                config, 'tenant_name') and config.tenant_name else os.environ.get('OS_TENANT_NAME', None)
+            self.auth_url = config.auth_url if hasattr(
+                config, 'auth_url') and config.auth_url else os.environ.get('OS_AUTH_URL', None)
+
+        self.nova = self._connect_nova()
+
+        # self.Compute = EC2ComputeService(self)
+        # self.Images = EC2ImageService(self)
+        self.security = OpenStackSecurityService(self)
+        # self.BlockStore = EC2BlockStore(self)
+        # self.ObjectStore = EC2ObjectStore(self)
+
+    def _connect_nova(self):
+        """
+        Get an openstack client object for the given cloud.
+        """
+        return client.Client(self.api_version, self.username, self.password, self.tenant_name, self.auth_url)
+
+
+class OpenStackSecurityService(SecurityService):
+
+    def __init__(self, provider):
+        self.provider = provider
+
+    def list_key_pairs(self):
+        """
+        List all key pairs.
+
+        :rtype: ``list`` of :class:`.KeyPair`
+        :return:  list of KeyPair objects
+        """
+        key_pairs = self.provider.nova.keypairs.list()
+        return [KeyPair(kp.id) for kp in key_pairs]