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

Refactored the list method of security groups to do the filtering in the service.

madhugilla 9 лет назад
Родитель
Сommit
b80c558abb

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

@@ -78,10 +78,11 @@ class AzureClient(object):
         return self.subscription_client.subscriptions.list_locations(self.subscription_id)
         return self.subscription_client.subscriptions.list_locations(self.subscription_id)
 
 
     def list_security_group(self, filters=None):
     def list_security_group(self, filters=None):
-        security_groups = FilterList(
-            self.network_management_client.network_security_groups.list(self.resource_group_name))
-        security_groups.filter(filters)
-        return security_groups
+        # security_groups = FilterList(
+        #     self.network_management_client.network_security_groups.list(self.resource_group_name))
+        # security_groups.filter(filters)
+        # return security_groups
+        return self.network_management_client.network_security_groups.list(self.resource_group_name)
 
 
     def create_security_group(self, name, parameters):
     def create_security_group(self, name, parameters):
         sg_create = self.network_management_client.network_security_groups.create_or_update(self.resource_group_name,
         sg_create = self.network_management_client.network_security_groups.create_or_update(self.resource_group_name,

+ 11 - 16
cloudbridge/cloud/providers/azure/mock_azure_client.py

@@ -23,7 +23,6 @@ class MockAzureClient:
     sg_rule2.destination_port_range = "*"
     sg_rule2.destination_port_range = "*"
     sg_rule2.source_port_range = "*"
     sg_rule2.source_port_range = "*"
 
 
-
     sec_gr1 = NetworkSecurityGroup()
     sec_gr1 = NetworkSecurityGroup()
     sec_gr1.name = "sg1"
     sec_gr1.name = "sg1"
     sec_gr1.id = "/subscriptions/7904d702-e01c-4826-8519-f5a25c866a96/resourceGroups/CloudBridge-Azure/providers/Microsoft.Network/networkSecurityGroups/sg1"
     sec_gr1.id = "/subscriptions/7904d702-e01c-4826-8519-f5a25c866a96/resourceGroups/CloudBridge-Azure/providers/Microsoft.Network/networkSecurityGroups/sg1"
@@ -44,7 +43,6 @@ class MockAzureClient:
 
 
     security_groups = [sec_gr1, sec_gr2, sec_gr3]
     security_groups = [sec_gr1, sec_gr2, sec_gr3]
 
 
-
     container1 = Container()
     container1 = Container()
     container1.name = "container1"
     container1.name = "container1"
 
 
@@ -91,7 +89,6 @@ class MockAzureClient:
     def __init__(self, provider):
     def __init__(self, provider):
         self._provider = provider
         self._provider = provider
 
 
-
     def create_security_group(self, name, parameters):
     def create_security_group(self, name, parameters):
         sg_create = NetworkSecurityGroup()
         sg_create = NetworkSecurityGroup()
         sg_create.name = name
         sg_create.name = name
@@ -102,9 +99,8 @@ class MockAzureClient:
         return sg_create
         return sg_create
 
 
     def list_security_group(self, filters=None):
     def list_security_group(self, filters=None):
-        security_groups = FilterList(self.security_groups)
-        security_groups.filter(filters)
-        return security_groups
+
+        return self.security_groups
 
 
     def delete_security_group(self, name):
     def delete_security_group(self, name):
         for item in self.security_groups:
         for item in self.security_groups:
@@ -120,7 +116,8 @@ class MockAzureClient:
         return None
         return None
 
 
     def create_security_group_rule(self, security_group, rule_name, parameters):
     def create_security_group_rule(self, security_group, rule_name, parameters):
-        new_sg_rule = SecurityRule(protocol='*',source_address_prefix='100',destination_address_prefix="*",access="Allow",direction = "Inbound")
+        new_sg_rule = SecurityRule(protocol='*', source_address_prefix='100', destination_address_prefix="*",
+                                   access="Allow", direction="Inbound")
         new_sg_rule.name = "rule1"
         new_sg_rule.name = "rule1"
         new_sg_rule.id = "r1"
         new_sg_rule.id = "r1"
         new_sg_rule.destination_port_range = "*"
         new_sg_rule.destination_port_range = "*"
@@ -146,7 +143,7 @@ class MockAzureClient:
                 return container
                 return container
         return None
         return None
 
 
-    def list_containers(self, filters = None):
+    def list_containers(self, filters=None):
         containers = FilterList(self.containers)
         containers = FilterList(self.containers)
         containers.filter(filters)
         containers.filter(filters)
         return containers
         return containers
@@ -170,14 +167,14 @@ class MockAzureClient:
                 return blob
                 return blob
         return None
         return None
 
 
-    def list_blobs(self,container_name):
+    def list_blobs(self, container_name):
         return self.blocks
         return self.blocks
 
 
     def get_blob_content(self, container_name, blob_name):
     def get_blob_content(self, container_name, blob_name):
         blob = self.get_blob(container_name, blob_name)
         blob = self.get_blob(container_name, blob_name)
         return blob
         return blob
 
 
-    def delete_blob(self,container_name, blob_name):
+    def delete_blob(self, container_name, blob_name):
         for blob in self.blocks:
         for blob in self.blocks:
             if blob.name == blob_name:
             if blob.name == blob_name:
                 self.blocks.remove(blob)
                 self.blocks.remove(blob)
@@ -190,13 +187,13 @@ class MockAzureClient:
         return 'https://cloudbridgeazure.blob.core.windows.net/vhds/block1'
         return 'https://cloudbridgeazure.blob.core.windows.net/vhds/block1'
 
 
     def create_empty_disk(self, disk_name, size, region=None, snapshot_id=None):
     def create_empty_disk(self, disk_name, size, region=None, snapshot_id=None):
-        volume = Disk(location='eastus',creation_data=None)
-        volume.id='/subscriptions/7904d702-e01c-4826-8519-f5a25c866a96/resourceGroups/cloudbridge-azure/providers/Microsoft.Compute/disks/SampleVolume'
+        volume = Disk(location='eastus', creation_data=None)
+        volume.id = '/subscriptions/7904d702-e01c-4826-8519-f5a25c866a96/resourceGroups/cloudbridge-azure/providers/Microsoft.Compute/disks/SampleVolume'
         volume.name = disk_name
         volume.name = disk_name
-        volume.disk_size_gb= size
+        volume.disk_size_gb = size
         volume.creation_data = CreationData(create_option=DiskCreateOption.empty)
         volume.creation_data = CreationData(create_option=DiskCreateOption.empty)
         volume.time_created = '01-01-2017'
         volume.time_created = '01-01-2017'
-        volume.owner_id ='/subscriptions/7904d702-e01c-4826-8519-f5a25c866a96/resourceGroups/CloudBridge-Azure/providers/Microsoft.Compute/virtualMachines/ubuntu-intro1'
+        volume.owner_id = '/subscriptions/7904d702-e01c-4826-8519-f5a25c866a96/resourceGroups/CloudBridge-Azure/providers/Microsoft.Compute/virtualMachines/ubuntu-intro1'
         self.volumes.append(volume)
         self.volumes.append(volume)
         return volume
         return volume
 
 
@@ -205,5 +202,3 @@ class MockAzureClient:
             if volume.name == disk_name:
             if volume.name == disk_name:
                 return volume
                 return volume
         return None
         return None
-
-

+ 6 - 1
cloudbridge/cloud/providers/azure/services.py

@@ -4,6 +4,7 @@ from azure.common import AzureMissingResourceHttpError
 from msrestazure.azure_exceptions import CloudError
 from msrestazure.azure_exceptions import CloudError
 
 
 from cloudbridge.cloud.interfaces.resources import PlacementZone, Snapshot
 from cloudbridge.cloud.interfaces.resources import PlacementZone, Snapshot
+from cloudbridge.cloud.providers.azure.azure_client import FilterList
 from .resources import SUBNET_RESOURCE_ID, NETWORK_NAME, SUBNET_NAME, NETWORK_RESOURCE_ID, \
 from .resources import SUBNET_RESOURCE_ID, NETWORK_NAME, SUBNET_NAME, NETWORK_RESOURCE_ID, \
     INSTANCE_RESOURCE_ID, VM_NAME, IMAGE_RESOURCE_ID, RESOURCE_GROUP_NAME, IMAGE_NAME, SNAPSHOT_RESOURCE_ID, \
     INSTANCE_RESOURCE_ID, VM_NAME, IMAGE_RESOURCE_ID, RESOURCE_GROUP_NAME, IMAGE_NAME, SNAPSHOT_RESOURCE_ID, \
     SNAPSHOT_NAME, VOLUME_RESOURCE_ID, VOLUME_NAME, NETWORK_SECURITY_GROUP_RESOURCE_ID, SECURITY_GROUP_NAME, \
     SNAPSHOT_NAME, VOLUME_RESOURCE_ID, VOLUME_NAME, NETWORK_SECURITY_GROUP_RESOURCE_ID, SECURITY_GROUP_NAME, \
@@ -76,8 +77,12 @@ class AzureSecurityGroupService(BaseSecurityGroupService):
         """
         """
         Searches for a security group by a given list of attributes.
         Searches for a security group by a given list of attributes.
         """
         """
+        filter = {'name': name}
+        filtered_security_groups = FilterList(self.provider.azure_client.list_security_group())
+        filtered_security_groups.filter(filter)
         security_groups = [AzureSecurityGroup(self.provider, security_group)
         security_groups = [AzureSecurityGroup(self.provider, security_group)
-                           for security_group in self.provider.azure_client.list_security_group({'name': name})]
+                           for security_group in filtered_security_groups]
+
         return ClientPagedResultList(self.provider, security_groups,
         return ClientPagedResultList(self.provider, security_groups,
                                      limit=limit, marker=marker)
                                      limit=limit, marker=marker)
 
 

+ 1 - 1
test/test_azure_security_service.py

@@ -28,7 +28,7 @@ class AzureSecurityServiceTestCase(ProviderTestBase):
         sgl = self.provider.security.security_groups.find("sg")
         sgl = self.provider.security.security_groups.find("sg")
         for sg in sgl:
         for sg in sgl:
             self.assertTrue("sg" in sg.name)
             self.assertTrue("sg" in sg.name)
-        self.assertTrue(sgl.total_results > 1)
+        self.assertTrue(sgl.total_results > 0)
 
 
     @helpers.skipIfNoService(['security.security_groups'])
     @helpers.skipIfNoService(['security.security_groups'])
     def test_azure_security_group_find_not_exists(self):
     def test_azure_security_group_find_not_exists(self):