Pārlūkot izejas kodu

Improved pep8 compliance

nuwan_ag 10 gadi atpakaļ
vecāks
revīzija
cfaaf0db5d

+ 26 - 20
cloudbridge/providers/ec2/impl.py

@@ -21,8 +21,10 @@ class EC2CloudProviderV1(BaseCloudProvider):
         self.cloud_type = 'ec2'
         self.cloud_type = 'ec2'
 
 
         # Initialize cloud connection fields
         # Initialize cloud connection fields
-        self.a_key = self._get_config_value('access_key', os.environ.get('EC2_ACCESS_KEY', None))
-        self.s_key = self._get_config_value('secret_key', os.environ.get('EC2_SECRET_KEY', None))
+        self.a_key = self._get_config_value(
+            'access_key', os.environ.get('EC2_ACCESS_KEY', None))
+        self.s_key = self._get_config_value(
+            'secret_key', os.environ.get('EC2_SECRET_KEY', None))
         self.is_secure = self._get_config_value('is_secure', True)
         self.is_secure = self._get_config_value('is_secure', True)
         self.region_name = self._get_config_value('region_name', 'us-east-1')
         self.region_name = self._get_config_value('region_name', 'us-east-1')
         self.region_endpoint = self._get_config_value(
         self.region_endpoint = self._get_config_value(
@@ -45,15 +47,17 @@ class EC2CloudProviderV1(BaseCloudProvider):
         Get a boto connection object for the given cloud.
         Get a boto connection object for the given cloud.
         """
         """
         r = RegionInfo(name=self.region_name, endpoint=self.region_endpoint)
         r = RegionInfo(name=self.region_name, endpoint=self.region_endpoint)
-        ec2_conn = boto.connect_ec2(aws_access_key_id=self.a_key,
-                                    aws_secret_access_key=self.s_key,
-                                    # api_version is needed for availability zone support for EC2
-                                    api_version='2012-06-01' if self.cloud_type == 'ec2' else None,
-                                    is_secure=self.is_secure,
-                                    region=r,
-                                    port=self.ec2_port,
-                                    path=self.ec2_conn_path,
-                                    validate_certs=False)
+        ec2_conn = boto.connect_ec2(
+            aws_access_key_id=self.a_key,
+            aws_secret_access_key=self.s_key,
+            # api_version is needed for availability
+            # zone support for EC2
+            api_version='2012-06-01' if self.cloud_type == 'ec2' else None,
+            is_secure=self.is_secure,
+            region=r,
+            port=self.ec2_port,
+            path=self.ec2_conn_path,
+            validate_certs=False)
         return ec2_conn
         return ec2_conn
 
 
     def _connect_s3(self):
     def _connect_s3(self):
@@ -61,13 +65,15 @@ class EC2CloudProviderV1(BaseCloudProvider):
         Get a boto connection object for the given cloud.
         Get a boto connection object for the given cloud.
         """
         """
         r = RegionInfo(name=self.region_name, endpoint=self.region_endpoint)
         r = RegionInfo(name=self.region_name, endpoint=self.region_endpoint)
-        ec2_conn = boto.connect_ec2(aws_access_key_id=self.a_key,
-                                    aws_secret_access_key=self.s_key,
-                                    # api_version is needed for availability zone support for EC2
-                                    api_version='2012-06-01' if self.cloud_type == 'ec2' else None,
-                                    is_secure=self.is_secure,
-                                    region=r,
-                                    port=self.ec2_port,
-                                    path=self.ec2_conn_path,
-                                    validate_certs=False)
+        ec2_conn = boto.connect_ec2(
+            aws_access_key_id=self.a_key,
+            aws_secret_access_key=self.s_key,
+            # api_version is needed for availability
+            # zone support for EC2
+            api_version='2012-06-01' if self.cloud_type == 'ec2' else None,
+            is_secure=self.is_secure,
+            region=r,
+            port=self.ec2_port,
+            path=self.ec2_conn_path,
+            validate_certs=False)
         return ec2_conn
         return ec2_conn

+ 10 - 14
cloudbridge/providers/ec2/services.py

@@ -88,31 +88,27 @@ class EC2ComputeService(ComputeService):
         Creates a new virtual machine instance.
         Creates a new virtual machine instance.
         """
         """
         image_id = image.image_id if isinstance(image, MachineImage) else image
         image_id = image.image_id if isinstance(image, MachineImage) else image
-        instance_size = instance_type.name if isinstance(
-            instance_type,
-            InstanceType) else instance_type
+        instance_size = instance_type.name if \
+            isinstance(instance_type, InstanceType) else instance_type
         zone_name = zone.name if isinstance(zone, PlacementZone) else zone
         zone_name = zone.name if isinstance(zone, PlacementZone) else zone
         keypair_name = keypair.name if isinstance(
         keypair_name = keypair.name if isinstance(
             keypair,
             keypair,
             KeyPair) else keypair
             KeyPair) else keypair
         if security_groups:
         if security_groups:
-            if isinstance(security_groups, list) and isinstance(
-                    security_groups[0], SecurityGroup):
+            if isinstance(security_groups, list) and \
+                    isinstance(security_groups[0], SecurityGroup):
                 security_groups_list = [sg.name for sg in security_groups]
                 security_groups_list = [sg.name for sg in security_groups]
             else:
             else:
                 security_groups_list = security_groups
                 security_groups_list = security_groups
         else:
         else:
             security_groups_list = None
             security_groups_list = None
 
 
-        reservation = self.provider.ec2_conn.run_instances(image_id=image_id,
-                                                           instance_type=instance_size,
-                                                           min_count=1,
-                                                           max_count=1,
-                                                           placement=zone_name,
-                                                           key_name=keypair_name,
-                                                           security_groups=security_groups_list,
-                                                           user_data=user_data
-                                                           )
+        reservation = self.provider.ec2_conn.run_instances(
+            image_id=image_id, instance_type=instance_size,
+            min_count=1, max_count=1, placement=zone_name,
+            key_name=keypair_name, security_groups=security_groups_list,
+            user_data=user_data
+        )
         if reservation:
         if reservation:
             instance = EC2Instance(self.provider, reservation.instances[0])
             instance = EC2Instance(self.provider, reservation.instances[0])
             instance.name = name
             instance.name = name

+ 4 - 5
cloudbridge/providers/openstack/services.py

@@ -109,12 +109,11 @@ class OpenStackComputeService(ComputeService):
             isinstance(instance_type, InstanceType) else \
             isinstance(instance_type, InstanceType) else \
             self.instance_types.find_by_name(instance_type).id
             self.instance_types.find_by_name(instance_type).id
         zone_name = zone.name if isinstance(zone, PlacementZone) else zone
         zone_name = zone.name if isinstance(zone, PlacementZone) else zone
-        keypair_name = keypair.name if isinstance(
-            keypair,
-            KeyPair) else keypair
+        keypair_name = keypair.name if \
+            isinstance(keypair, KeyPair) else keypair
         if security_groups:
         if security_groups:
-            if isinstance(security_groups, list) and isinstance(
-                    security_groups[0], SecurityGroup):
+            if isinstance(security_groups, list) and \
+                    isinstance(security_groups[0], SecurityGroup):
                 security_groups_list = [sg.name for sg in security_groups]
                 security_groups_list = [sg.name for sg in security_groups]
             else:
             else:
                 security_groups_list = security_groups
                 security_groups_list = security_groups

+ 2 - 1
cloudbridge/providers/openstack/types.py

@@ -77,7 +77,8 @@ class OpenStackMachineImage(BaseMachineImage):
         Refreshes the state of this instance by re-querying the cloud provider
         Refreshes the state of this instance by re-querying the cloud provider
         for its latest state.
         for its latest state.
         """
         """
-        self._os_image = self.provider.images.get_image(self.image_id)._os_image
+        image = self.provider.images.get_image(self.image_id)
+        self._os_image = image._os_image
 
 
 
 
 class OpenStackInstanceType(InstanceType):
 class OpenStackInstanceType(InstanceType):

+ 4 - 2
setup.py

@@ -3,11 +3,13 @@ from setuptools import setup, find_packages
 
 
 setup(name='cloudbridge',
 setup(name='cloudbridge',
       version=0.1,
       version=0.1,
-      description='A simple layer of abstraction over multiple cloud providers.',
+      description='A simple layer of abstraction over multiple cloud'
+      'providers.',
       author='Galaxy and GVL Projects',
       author='Galaxy and GVL Projects',
       author_email='support@genome.edu.au',
       author_email='support@genome.edu.au',
       url='http://cloudbridge.readthedocs.org/',
       url='http://cloudbridge.readthedocs.org/',
-      install_requires=['bunch>=1.00', 'python-keystoneclient', 'python-novaclient', 'boto'],
+      install_requires=['bunch>=1.00', 'python-keystoneclient',
+                        'python-novaclient', 'boto'],
       packages=find_packages(),
       packages=find_packages(),
       license='MIT',
       license='MIT',
       classifiers=[
       classifiers=[

+ 13 - 12
test/__init__.py

@@ -1,17 +1,18 @@
 """
 """
-Tests the functionality of each provider implementation against registered test cases.
-These tests require that provider credentials are set as environment variables,
-as required for each provider (see `tox.ini` for a list of env variables).
+Tests the functionality of each provider implementation against registered
+test cases. These tests require that provider credentials are set as
+environment variables, as required for each provider (see `tox.ini` for a list
+of env variables).
 
 
 Since the tests exercise the ``cloudbridge`` interfaces, and there are multiple
 Since the tests exercise the ``cloudbridge`` interfaces, and there are multiple
 implementations of these interfaces, for m interfaces and n implementation,
 implementations of these interfaces, for m interfaces and n implementation,
-exercising all interfaces means that m*n test case classes are needed. Otherwise,
-the standard test runners such as unittest and nose2 do not correctly pick up
-the tests.
+exercising all interfaces means that m*n test case classes are needed.
+Otherwise, the standard test runners such as unittest and nose2 do not
+correctly pick up the tests.
 
 
-To avoid an explosion of repetitive test cases, the ``ProviderTestCaseGenerator``
-class will automatically generate a new Python class for each combination of
-test and provider. The ``load_tests`` protocol
+To avoid an explosion of repetitive test cases, the
+``ProviderTestCaseGenerator`` class will automatically generate a new Python
+class for each combination of test and provider. The ``load_tests`` protocol
 (https://docs.python.org/2/library/unittest.html#load-tests-protocol)
 (https://docs.python.org/2/library/unittest.html#load-tests-protocol)
 is used to aid test discovery.
 is used to aid test discovery.
 
 
@@ -20,9 +21,9 @@ Use ``python setup.py test`` to run these unit tests (alternatively, use
 
 
 All test cases need to be registered below, and available providers will be
 All test cases need to be registered below, and available providers will be
 discovered through the ``ProviderFactory``. Test Cases must not inherit from
 discovered through the ``ProviderFactory``. Test Cases must not inherit from
-``unittest.TestCase``, to avoid confusing unittest and nose2's automatic discovery.
-(The test generator will automatically add ``unittest.TestCase`` as a base class
-to each combination).
+``unittest.TestCase``, to avoid confusing unittest and nose2's automatic
+discovery. (The test generator will automatically add ``unittest.TestCase``
+as a base class to each combination).
 """
 """
 
 
 from test.helpers import ProviderTestCaseGenerator
 from test.helpers import ProviderTestCaseGenerator

+ 32 - 17
test/helpers.py

@@ -8,12 +8,17 @@ def create_test_instance(provider):
     if "EC2CloudProvider" in provider.name:
     if "EC2CloudProvider" in provider.name:
         ami = os.environ.get('CB_AMI', 'ami-d85e75b0')
         ami = os.environ.get('CB_AMI', 'ami-d85e75b0')
         instance_type = os.environ.get('CB_INSTANCE_TYPE', 't1.micro')
         instance_type = os.environ.get('CB_INSTANCE_TYPE', 't1.micro')
-        return provider.compute.create_instance(instance_name, ami, instance_type)
+        return provider.compute.create_instance(
+            instance_name, ami, instance_type)
     elif "OpenStackCloudProvider" in provider.name:
     elif "OpenStackCloudProvider" in provider.name:
-        image_id = os.environ.get('CB_IMAGE', "d57696ba-5ed2-43fe-bf78-a587829973a9")
+        image_id = os.environ.get(
+            'CB_IMAGE',
+            "d57696ba-5ed2-43fe-bf78-a587829973a9")
         instance_type = os.environ.get('CB_FLAVOR', "m2.xsmall")
         instance_type = os.environ.get('CB_FLAVOR', "m2.xsmall")
         return provider.compute.create_instance(
         return provider.compute.create_instance(
-            "{0}-{1}".format(instance_name, provider.name), image_id, instance_type)
+            "{0}-{1}".format(instance_name, provider.name),
+            image_id,
+            instance_type)
 
 
 
 
 def get_test_instance(provider):
 def get_test_instance(provider):
@@ -27,8 +32,8 @@ class ProviderTestBase(object):
     """
     """
     A dummy base class for Test Cases. Does not inherit from unittest.TestCase
     A dummy base class for Test Cases. Does not inherit from unittest.TestCase
     to avoid confusing test discovery by unittest and nose2. unittest.TestCase
     to avoid confusing test discovery by unittest and nose2. unittest.TestCase
-    is injected as a base class by the generator, so calling the unittest constructor
-    works correctly.
+    is injected as a base class by the generator, so calling the unittest
+    constructor works correctly.
     """
     """
 
 
     def __init__(self, methodName, provider):
     def __init__(self, methodName, provider):
@@ -48,44 +53,54 @@ class ProviderTestCaseGenerator():
 
 
     def create_provider_instance(self, provider_class):
     def create_provider_instance(self, provider_class):
         """
         """
-        Instantiate a default provider instance. All required connection settings
-        are expected to be set as environment variables.
+        Instantiate a default provider instance. All required connection
+        settings are expected to be set as environment variables.
         """
         """
         return provider_class({})
         return provider_class({})
 
 
     def generate_new_test_class(self, name, testcase_class):
     def generate_new_test_class(self, name, testcase_class):
         """
         """
-        Generates a new type which inherits from the given testcase_class and unittest.TestCase
+        Generates a new type which inherits from the given testcase_class and
+        unittest.TestCase
         """
         """
         class_name = "{0}{1}".format(name, testcase_class.__name__)
         class_name = "{0}{1}".format(name, testcase_class.__name__)
         return type(class_name, (testcase_class, unittest.TestCase), {})
         return type(class_name, (testcase_class, unittest.TestCase), {})
 
 
-    def generate_test_suite_for_provider_testcase(self, provider_class, testcase_class):
+    def generate_test_suite_for_provider_testcase(
+            self, provider_class, testcase_class):
         """
         """
-        Generate and return a suite of tests for a specific provider class and testcase
-        combination
+        Generate and return a suite of tests for a specific provider class and
+        testcase combination
         """
         """
         testloader = unittest.TestLoader()
         testloader = unittest.TestLoader()
         testnames = testloader.getTestCaseNames(testcase_class)
         testnames = testloader.getTestCaseNames(testcase_class)
         suite = unittest.TestSuite()
         suite = unittest.TestSuite()
         for name in testnames:
         for name in testnames:
-            generated_cls = self.generate_new_test_class(provider_class.__name__, testcase_class)
-            suite.addTest(generated_cls(name, self.create_provider_instance(provider_class)))
+            generated_cls = self.generate_new_test_class(
+                provider_class.__name__,
+                testcase_class)
+            suite.addTest(
+                generated_cls(
+                    name,
+                    self.create_provider_instance(provider_class)))
         return suite
         return suite
 
 
     def generate_test_suite_for_provider(self, provider_class):
     def generate_test_suite_for_provider(self, provider_class):
         """
         """
-        Generate and return a suite of all available tests for a given provider class
+        Generate and return a suite of all available tests for a given provider
+        class
         """
         """
         suite = unittest.TestSuite()
         suite = unittest.TestSuite()
-        suites = map(lambda test_class: self.generate_test_suite_for_provider_testcase(
-            provider_class, test_class), self.all_test_classes)
+        suites = map(
+            lambda test_class: self.generate_test_suite_for_provider_testcase(
+                provider_class, test_class), self.all_test_classes)
         map(suite.addTest, suites)
         map(suite.addTest, suites)
         return suite
         return suite
 
 
     def generate_tests(self):
     def generate_tests(self):
         """
         """
-        Generate and return a suite of tests for all provider and test class combinations
+        Generate and return a suite of tests for all provider and test class
+        combinations
         """
         """
         factory = CloudProviderFactory()
         factory = CloudProviderFactory()
         provider_name = os.environ.get("CB_TEST_PROVIDER", None)
         provider_name = os.environ.get("CB_TEST_PROVIDER", None)

+ 2 - 2
test/test_provider_image_service.py

@@ -19,8 +19,8 @@ class ProviderImageServiceTestCase(ProviderTestBase):
     def test_create_and_list_image(self):
     def test_create_and_list_image(self):
         """
         """
         Create a new image and check whether that image can be listed.
         Create a new image and check whether that image can be listed.
-        This covers waiting till the image is ready, checking that the image name is the expected one and
-        whether list_images is functional.
+        This covers waiting till the image is ready, checking that the image
+        name is the expected one and whether list_images is functional.
         """
         """
         name = "CBUnitTestListImg-{0}".format(uuid.uuid4())
         name = "CBUnitTestListImg-{0}".format(uuid.uuid4())
         test_image = self.instance.create_image(name)
         test_image = self.instance.create_image(name)