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

Fix issues for GCEInstanceTypesService.

baizhang 10 лет назад
Родитель
Сommit
05f7606da5

+ 3 - 3
cloudbridge/cloud/interfaces/services.py

@@ -960,10 +960,10 @@ class InstanceTypesService(PageableObjectMixin, CloudService):
     @abstractmethod
     def find(self, **kwargs):
         """
-        Searches for an instance by a given list of attributes.
+        Searches for instances by a given list of attributes.
 
-        :rtype: ``object`` of :class:`.InstanceType`
-        :return: an Instance object
+        :rtype: ``list`` of :class:`.InstanceType`
+        :return: list of InstanceType objects
         """
         pass
 

+ 3 - 4
cloudbridge/cloud/providers/gce/provider.py

@@ -10,8 +10,8 @@ import os
 import time
 
 from googleapiclient import discovery
-import httplib2
 from oauth2client.client import GoogleCredentials
+from oauth2client.service_account import ServiceAccountCredentials
 
 from .services import GCEComputeService
 from .services import GCESecurityService
@@ -27,8 +27,6 @@ class GCECloudProvider(BaseCloudProvider):
         # Initialize cloud connection fields
         self.client_email = self._get_config_value(
             'gce_client_email', os.environ.get('GCE_CLIENT_EMAIL'))
-        self.private_key = self._get_config_value(
-            'gce_private_key', os.environ.get('GCE_PRIVATE_KEY'))
         self.project_name = self._get_config_value(
             'gce_project_name', os.environ.get('GCE_PROJECT_NAME'))
         self.credentials_file = self._get_config_value(
@@ -74,7 +72,8 @@ class GCECloudProvider(BaseCloudProvider):
 
     def _connect_gce_compute(self):
         if self.credentials_file:
-            credentials = GoogleCredentials.from_json(self.credentials_file)
+            credentials = ServiceAccountCredentials.from_json_keyfile_name(
+                self.credentials_file)
         else:
             credentials = GoogleCredentials.get_application_default()
         return discovery.build('compute', 'v1', credentials=credentials)

+ 4 - 4
cloudbridge/cloud/providers/gce/resources.py

@@ -84,7 +84,7 @@ class GCEInstanceType(BaseInstanceType):
 
     @property
     def extra_data(self):
-        return {key: val for key, val in enumerate(self._inst_dict)
-                if key not in ['guestCpus', 'maximumPersistentDisksSizeGb',
-                               'kind', 'maximumPersistentDisks', 'memoryMb',
-                               'id']}
+        return {key: val for key, val in self._inst_dict.items()
+                if key not in ['id', 'name', 'kind', 'guestCpus', 'memoryMb',
+                               'maximumPersistentDisksSizeGb',
+                               'maximumPersistentDisks']}

+ 4 - 3
cloudbridge/cloud/providers/gce/services.py

@@ -4,7 +4,6 @@ from cloudbridge.cloud.base.services import BaseInstanceTypesService
 from cloudbridge.cloud.base.services import BaseKeyPairService
 from cloudbridge.cloud.base.services import BaseSecurityGroupService
 from cloudbridge.cloud.base.services import BaseSecurityService
-from cloudbridge.cloud.interfaces.resources import InstanceType
 from cloudbridge.cloud.providers.gce import helpers
 from collections import namedtuple
 import hashlib
@@ -212,6 +211,7 @@ class GCEInstanceTypesService(BaseInstanceTypesService):
         return None
 
     def find(self, **kwargs):
+        matched_inst_types = []
         for inst_type in self.instance_data:
             is_match = True
             for key, value in kwargs.iteritems():
@@ -221,8 +221,9 @@ class GCEInstanceTypesService(BaseInstanceTypesService):
                     is_match = False
                     break
             if is_match:
-                return GCEInstanceType(self.provider, inst_type)
-        return None
+                matched_inst_types.append(
+                    GCEInstanceType(self.provider, inst_type))
+        return matched_inst_types
 
     def list(self, limit=None, marker=None):
         inst_types = [GCEInstanceType(self.provider, inst_type)

+ 4 - 4
test/test_instance_types_service.py

@@ -79,13 +79,13 @@ class CloudInstanceTypesServiceTestCase(ProviderTestBase):
             self.provider,
             "instance_type")
         inst_type = self.provider.compute.instance_types.find(
-            name=instance_type_name)
+            name=instance_type_name)[0]
         self.assertTrue(isinstance(inst_type, InstanceType),
                         "Find must return an InstanceType object")
 
-        self.assertIsNone(self.provider.compute.instance_types.find(
+        self.assertFalse(self.provider.compute.instance_types.find(
             name="non_existent_instance_type"), "Searching for a non-existent"
-            " instance type must return None")
+            " instance type must return an empty list")
 
         with self.assertRaises(TypeError):
             self.provider.compute.instance_types.find(
@@ -102,7 +102,7 @@ class CloudInstanceTypesServiceTestCase(ProviderTestBase):
             self.provider,
             "instance_type")
         inst_type = self.provider.compute.instance_types.find(
-            name=instance_type_name)
+            name=instance_type_name)[0]
         self.assertEqual(inst_type,
                          compute_svc.instance_types.get(inst_type.id))
         self.assertIsNone(compute_svc.instance_types.get("non_existent_id"),

+ 1 - 1
tox.ini

@@ -8,7 +8,7 @@ envlist = py27, py35, pypy
 
 [testenv]
 commands = {envpython} -m coverage run --branch --source=cloudbridge --omit=cloudbridge/cloud/interfaces/* setup.py test
-passenv = AWS_ACCESS_KEY AWS_SECRET_KEY GCE_CLIENT_EMAIL GCE_PRIVATE_KEY GCE_PROJECT_NAME GCE_DEFAULT_ZONE GCE_SERVICE_CREDS_FILE OS_AUTH_URL OS_PASSWORD OS_TENANT_NAME OS_USERNAME OS_REGION_NAME NOVA_SERVICE_NAME CB_IMAGE_AWS CB_INSTANCE_TYPE_AWS CB_PLACEMENT_AWS CB_IMAGE_OS CB_INSTANCE_TYPE_OS CB_PLACEMENT_OS CB_TEST_PROVIDER CB_USE_MOCK_PROVIDERS
+passenv = AWS_ACCESS_KEY AWS_SECRET_KEY GCE_CLIENT_EMAIL GCE_PROJECT_NAME GCE_DEFAULT_ZONE GCE_SERVICE_CREDS_FILE OS_AUTH_URL OS_PASSWORD OS_TENANT_NAME OS_USERNAME OS_REGION_NAME NOVA_SERVICE_NAME CB_IMAGE_AWS CB_INSTANCE_TYPE_AWS CB_PLACEMENT_AWS CB_IMAGE_OS CB_INSTANCE_TYPE_OS CB_PLACEMENT_OS CB_TEST_PROVIDER CB_USE_MOCK_PROVIDERS
 deps =
     -rrequirements.txt
     coverage