فهرست منبع

Added get method + test to InstanceTypesService.

Nuwan Goonasekera 10 سال پیش
والد
کامیت
ce05f01ced
3فایلهای تغییر یافته به همراه40 افزوده شده و 0 حذف شده
  1. 4 0
      cloudbridge/cloud/base/services.py
  2. 18 0
      cloudbridge/cloud/interfaces/services.py
  3. 18 0
      test/test_instance_types_service.py

+ 4 - 0
cloudbridge/cloud/base/services.py

@@ -111,6 +111,10 @@ class BaseInstanceTypesService(
     def __init__(self, provider):
         super(BaseInstanceTypesService, self).__init__(provider)
 
+    def get(self, instance_type_id):
+        itype = (t for t in self.list() if t.id == instance_type_id)
+        return next(itype, None)
+
     def find(self, **kwargs):
         name = kwargs.get('name')
         if name:

+ 18 - 0
cloudbridge/cloud/interfaces/services.py

@@ -883,6 +883,24 @@ class SecurityGroupService(PageableObjectMixin, CloudService):
 class InstanceTypesService(PageableObjectMixin, CloudService):
     __metaclass__ = ABCMeta
 
+    @abstractmethod
+    def get(self, instance_type_id):
+        """
+        Returns an InstanceType given its ID. Returns ``None`` if the
+        InstanceType does not exist.
+
+        Example:
+
+        .. code-block:: python
+
+            itype = provider.compute.instance_types.get('my_itype_id')
+            print(itype.id, itype.name)
+
+        :rtype: :class:`.InstanceType`
+        :return:  an InstanceType instance
+        """
+        pass
+
     @abstractmethod
     def list(self, limit=None, marker=None):
         """

+ 18 - 0
test/test_instance_types_service.py

@@ -90,3 +90,21 @@ class CloudInstanceTypesServiceTestCase(ProviderTestBase):
         with self.assertRaises(TypeError):
             self.provider.compute.instance_types.find(
                 non_existent_param="random_value")
+
+    def test_instance_types_get(self):
+        """
+        Searching for an instance by id should return an
+        InstanceType object and searching for a non-existent
+        object should return None
+        """
+        compute_svc = self.provider.compute
+        instance_type_name = helpers.get_provider_test_data(
+            self.provider,
+            "instance_type")
+        inst_type = self.provider.compute.instance_types.find(
+            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"),
+                          "Searching for a non-existent instance id must"
+                          " return None")