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

Merge branch 'local_azure_dev_v2' of D:\cloudbridge_dev with conflicts.

jatin 9 лет назад
Родитель
Сommit
8ae19c3f2d

+ 20 - 0
azure_integration_test/test_integration_azure_instance_type_service.py

@@ -0,0 +1,20 @@
+import azure_integration_test.helpers as helpers
+
+from azure_integration_test.helpers import ProviderTestBase
+
+
+class AzureIntegrationInstanceTypeServiceTestCase(ProviderTestBase):
+    @helpers.skipIfNoService(['compute.images'])
+    def test_azure_instance_type_service(self):
+
+        instance_type_list = self.provider.compute.instance_types.list()
+        print("List Instance Types - " + str(instance_type_list))
+        print("List Instance Type Properties - ")
+        print("Name - " + str(instance_type_list[0].name))
+        print("vcpus - " + str(instance_type_list[0].vcpus))
+        print("ram - " + str(instance_type_list[0].ram))
+        print("size_ephemeral_disks - " +
+              str(instance_type_list[0].size_ephemeral_disks))
+        print("num_ephemeral_disks - " +
+              str(instance_type_list[0].num_ephemeral_disks))
+        self.assertTrue(instance_type_list.total_results > 0)

+ 19 - 0
azure_test/test_azure_instance_type_service.py

@@ -0,0 +1,19 @@
+import azure_test.helpers as helpers
+from azure_test.helpers import ProviderTestBase
+
+
+class AzureInstanceTypeServiceTestCase(ProviderTestBase):
+
+    @helpers.skipIfNoService(['compute.instance_types'])
+    def test_azure_instance_type_list(self):
+        instance_type_list = self.provider.compute.instance_types.list()
+        print("List Instance Types - " + str(instance_type_list))
+        print("List Instance Type Properties - ")
+        print("Name - " + str(instance_type_list[0].name))
+        print("vcpus - " + str(instance_type_list[0].vcpus))
+        print("ram - " + str(instance_type_list[0].ram))
+        print("size_ephemeral_disks - " +
+              str(instance_type_list[0].size_ephemeral_disks))
+        print("num_ephemeral_disks - " +
+              str(instance_type_list[0].num_ephemeral_disks))
+        self.assertTrue(instance_type_list.total_results > 0)

+ 4 - 0
cloudbridge/cloud/providers/azure/azure_client.py

@@ -292,3 +292,7 @@ class AzureClient(object):
     def get_image(self, image_name):
         return self.compute_client.images. \
             get(self.resource_group_name, image_name)
+
+    def list_instance_types(self):
+        return self.compute_client.virtual_machine_sizes. \
+            list(self.region_name)

+ 22 - 1
cloudbridge/cloud/providers/azure/mock_azure_client.py

@@ -6,7 +6,9 @@ from io import BytesIO
 from azure.common import AzureException
 from azure.mgmt.compute.models import CreationData, DataDisk, \
     Disk, DiskCreateOption, Image, ManagedDiskParameters, \
-    Snapshot, StorageProfile, VirtualMachine
+    Snapshot, StorageProfile, VirtualMachine, \
+    VirtualMachineSize
+
 from azure.mgmt.network.models import NetworkSecurityGroup
 from azure.mgmt.network.models import SecurityRule
 from azure.mgmt.network.models import VirtualNetwork
@@ -209,6 +211,22 @@ class MockAzureClient:
 
     images = [image1, image2]
 
+    instance_type1 = VirtualMachineSize()
+    instance_type1.name = "instance_type1"
+    instance_type1.number_of_cores = 1
+    instance_type1.os_disk_size_in_mb = 100
+    instance_type1.memory_in_mb = 1000
+    instance_type1.max_data_disk_count = 1
+
+    instance_type2 = VirtualMachineSize()
+    instance_type2.name = "instance_type2"
+    instance_type2.number_of_cores = 2
+    instance_type2.os_disk_size_in_mb = 200
+    instance_type2.memory_in_mb = 2000
+    instance_type2.max_data_disk_count = 2
+
+    instance_types = [instance_type1, instance_type2]
+
     def __init__(self, provider):
         self._provider = provider
 
@@ -523,3 +541,6 @@ class MockAzureClient:
     def delete_image(self, name):
         img = self.get_image(name)
         self.images.remove(img)
+
+    def list_instance_types(self):
+        return self.instance_types

+ 46 - 1
cloudbridge/cloud/providers/azure/resources.py

@@ -8,7 +8,8 @@ from azure.common import AzureException
 
 
 from cloudbridge.cloud.base.resources import BaseAttachmentInfo, \
-    BaseBucket, BaseBucketObject, BaseMachineImage, BaseNetwork, \
+    BaseBucket, BaseBucketObject, BaseMachineImage, BaseNetwork,\
+    BaseInstanceType, BaseMachineImage, \
     BaseSecurityGroup, BaseSecurityGroupRule, BaseSnapshot, BaseVolume, \
     ClientPagedResultList
 from cloudbridge.cloud.interfaces import VolumeState
@@ -860,3 +861,47 @@ class AzureNetwork(BaseNetwork):
             return True if network else False
         except CloudError:
             return False
+
+
+class AzureInstanceType(BaseInstanceType):
+
+    def __init__(self, provider, instance_type):
+        super(AzureInstanceType, self).__init__(provider)
+        self._inst_type = instance_type
+
+    # @property
+    # def id(self):
+    #     return str(self._inst_dict['instance_type'])
+
+    @property
+    def name(self):
+        return self._inst_type.name
+
+    # @property
+    # def family(self):
+    #     return self._inst_type.get('family')
+
+    @property
+    def vcpus(self):
+        return self._inst_type.number_of_cores
+
+    @property
+    def ram(self):
+        return self._inst_type.memory_in_mb
+
+    @property
+    def size_root_disk(self):
+        return 0
+
+    @property
+    def size_ephemeral_disks(self):
+        return self._inst_type.os_disk_size_in_mb/1024
+
+    @property
+    def num_ephemeral_disks(self):
+        return self._inst_type.max_data_disk_count
+
+    # @property
+    # def extra_data(self):
+    #     return {key: val for key, val in enumerate(self._inst_type)
+    #             if key not in ["instance_type", "family", "vCPU", "memory"]}

+ 42 - 21
cloudbridge/cloud/providers/azure/services.py

@@ -5,8 +5,9 @@ from azure.common import AzureException
 
 from cloudbridge.cloud.base.resources import ClientPagedResultList
 from cloudbridge.cloud.base.services import BaseBlockStoreService, \
-    BaseComputeService, BaseImageService, BaseNetworkService, \
-    BaseObjectStoreService, BaseSecurityGroupService, BaseSecurityService, \
+    BaseComputeService, BaseImageService, BaseInstanceTypesService, \
+    BaseNetworkService, BaseObjectStoreService, \
+    BaseSecurityGroupService, BaseSecurityService, \
     BaseSnapshotService, BaseVolumeService
 from cloudbridge.cloud.interfaces.resources import PlacementZone, \
     Snapshot, Volume
@@ -14,8 +15,9 @@ from cloudbridge.cloud.providers.azure import helpers as azure_helpers
 
 from msrestazure.azure_exceptions import CloudError
 
-from .resources import AzureBucket, AzureMachineImage, \
-    AzureNetwork, AzureSecurityGroup, \
+from .resources import AzureBucket, AzureInstanceType,\
+    AzureMachineImage, AzureNetwork, \
+    AzureSecurityGroup, \
     AzureSnapshot, AzureVolume, \
     IMAGE_NAME, IMAGE_RESOURCE_ID, \
     NETWORK_NAME, NETWORK_RESOURCE_ID, \
@@ -225,8 +227,8 @@ class AzureVolumeService(BaseVolumeService):
                 'location': zone_id or self.provider.azure_client.region_name,
                 'disk_size_gb': size,
                 'creation_data': {
-                    'create_option': 'empty'
-                },
+                  'create_option': 'empty'
+                  },
                 'tags': tags}
 
             self.provider.azure_client.create_empty_disk(disk_name, params)
@@ -301,7 +303,7 @@ class AzureSnapshotService(BaseSnapshotService):
 class AzureComputeService(BaseComputeService):
     def __init__(self, provider):
         super(AzureComputeService, self).__init__(provider)
-        # self._instance_type_svc = AzureInstanceTypesService(self.provider)
+        self._instance_type_svc = AzureInstanceTypesService(self.provider)
         # self._instance_svc = AzureInstanceService(self.provider)
         # self._region_svc = AzureRegionService(self.provider)
         self._images_svc = AzureImageService(self.provider)
@@ -312,8 +314,7 @@ class AzureComputeService(BaseComputeService):
 
     @property
     def instance_types(self):
-        raise NotImplementedError('AzureComputeService not '
-                                  'implemented this method')
+        return self._instance_type_svc
 
     @property
     def instances(self):
@@ -352,6 +353,26 @@ class AzureImageService(BaseImageService):
                                      limit=limit, marker=marker)
 
 
+class AzureInstanceTypesService(BaseInstanceTypesService):
+
+    def __init__(self, provider):
+        super(AzureInstanceTypesService, self).__init__(provider)
+
+    @property
+    def instance_data(self):
+        """
+        Fetch info about the available instances.
+        """
+        r = self.provider.azure_client.list_instance_types()
+        return r
+
+    def list(self, limit=None, marker=None):
+        inst_types = [AzureInstanceType(self.provider, inst_type)
+                      for inst_type in self.instance_data]
+        return ClientPagedResultList(self.provider, inst_types,
+                                     limit=limit, marker=marker)
+
+
 class AzureNetworkService(BaseNetworkService):
     def __init__(self, provider):
         super(AzureNetworkService, self).__init__(provider)
@@ -415,16 +436,16 @@ class AzureNetworkService(BaseNetworkService):
         raise NotImplementedError('AzureNetworkService '
                                   'not implemented this method')
 
-
-def delete(self, network_id):
-    """
-        Delete an existing network.
+    def delete(self, network_id):
         """
-    try:
-        params = azure_helpers.parse_url(NETWORK_RESOURCE_ID, network_id)
-        network = self.provider.azure_client. \
-            delete_network(params.get(NETWORK_NAME))
-        return True if network else False
-    except CloudError as cloudError:
-        log.exception(cloudError.message)
-        return False
+                Delete an existing network.
+                """
+        try:
+            params = azure_helpers.parse_url(NETWORK_RESOURCE_ID, network_id)
+            network = self.provider.azure_client. \
+                delete_network(params.get(NETWORK_NAME))
+            return True if network else False
+        except CloudError as cloudError:
+            log.exception(cloudError.message)
+            return False
+

+ 2 - 3
test/test_azure_provider.py

@@ -13,9 +13,8 @@ class AzureProviderTestCase(ProviderTestBase):
             self.assertTrue(compute.instances is not None,
                             'Instances should not be none')
 
-        with self.assertRaises(NotImplementedError):
-            self.assertTrue(compute.instance_types is not None,
-                            'Instance types should not be none')
+        self.assertTrue(compute.instance_types is not None,
+                        'Instance types should not be none')
 
         with self.assertRaises(NotImplementedError):
             self.assertTrue(compute.regions is not None,