Răsfoiți Sursa

Added travis timeout setting and implemented initial changes for import security rules

vikramdoda 9 ani în urmă
părinte
comite
95f8345fad

+ 0 - 3
cloudbridge/cloud/base/resources.py

@@ -9,8 +9,6 @@ import re
 import shutil
 import time
 
-import cloudbridge
-
 from cloudbridge.cloud.interfaces.exceptions \
     import InvalidConfigurationException
 from cloudbridge.cloud.interfaces.exceptions import InvalidNameException
@@ -50,7 +48,6 @@ from cloudbridge.cloud.interfaces.resources import VolumeState
 
 import six
 
-cloudbridge.set_stream_logger(__name__)
 log = logging.getLogger(__name__)
 
 

+ 68 - 43
cloudbridge/cloud/providers/azure/resources.py

@@ -158,6 +158,20 @@ class AzureSecurityGroup(BaseSecurityGroup):
         :rtype: :class:``.SecurityGroupRule``
         :return: Rule object if successful or ``None``.
         """
+        if ip_protocol and from_port and to_port:
+            return self._create_rule(ip_protocol, from_port, to_port, cidr_ip)
+        elif src_group:
+            result = None
+            sg = (self._provicer.security.security_groups.get(src_group)
+                  if isinstance(src_group, str) else src_group)
+            for rule in sg.rules:
+                result = self._create_rule(rule.ip_protocol, rule.from_port,
+                                           rule.to_port, rule.cidr_ip)
+            return result
+        else:
+            return None
+
+    def _create_rule(self, ip_protocol, from_port, to_port, cidr_ip):
 
         # If cidr_ip is None, default values is set as 0.0.0.0/0
         if not cidr_ip:
@@ -165,33 +179,32 @@ class AzureSecurityGroup(BaseSecurityGroup):
 
         # If the SG with same parameters exist already,
         # then, it is returned instead of creating a new one.
-        rule = self.get_rule(ip_protocol, from_port,
-                             to_port, cidr_ip, src_group)
-        if not rule:
-            # resource_group = self._provider.resource_group
-            count = len(self.rules) + 1
-            rule_name = "Rule - " + str(count)
-            priority = count * 100
-            destination_port_range = "*"
-            destination_address_prefix = "*"
-            access = "Allow"
-            direction = "Inbound"
-            parameters = {"protocol": ip_protocol,
-                          "source_port_range":
-                              str(from_port) + "-" + str(to_port),
-                          "destination_port_range": destination_port_range,
-                          "priority": priority,
-                          "source_address_prefix": cidr_ip,
-                          "destination_address_prefix":
-                              destination_address_prefix,
-                          "access": access, "direction": direction}
-            result = self._provider.azure_client. \
-                create_security_group_rule(self.id,
-                                           rule_name, parameters)
-            self._security_group.security_rules.append(result)
-            return AzureSecurityGroupRule(self._provider, result, self)
-
-        return rule
+        rule = self.get_rule(ip_protocol, from_port, to_port, cidr_ip)
+
+        if rule:
+            return rule
+
+        count = len(self.rules) + 1
+        rule_name = "Rule - " + str(count)
+        priority = count * 100
+        destination_port_range = str(from_port) + "-" + str(to_port)
+        source_port_range = '*'
+        destination_address_prefix = "*"
+        access = "Allow"
+        direction = "Inbound"
+        parameters = {"protocol": ip_protocol,
+                      "source_port_range": source_port_range,
+                      "destination_port_range": destination_port_range,
+                      "priority": priority,
+                      "source_address_prefix": cidr_ip,
+                      "destination_address_prefix":
+                          destination_address_prefix,
+                      "access": access, "direction": direction}
+        result = self._provider.azure_client. \
+            create_security_group_rule(self.id,
+                                       rule_name, parameters)
+        self._security_group.security_rules.append(result)
+        return AzureSecurityGroupRule(self._provider, result, self)
 
     def get_rule(self, ip_protocol=None, from_port=None, to_port=None,
                  cidr_ip=None, src_group=None):
@@ -1071,7 +1084,7 @@ class AzureSubnet(BaseSubnet):
 
     @property
     def id(self):
-        return self._subnet.name
+        return self.network_id + '|$|' + self._subnet.name
 
     @property
     def resource_id(self):
@@ -1421,30 +1434,42 @@ class AzureInstance(BaseInstance):
         if not associated any security group to NIC
         else replacing the existing security group.
         '''
-
+        sg = (self._provicer.security.security_groups.get(sg)
+              if isinstance(sg, str) else sg)
         nic = self._provider.azure_client.get_nic(self._nic_ids[0])
         if not nic.network_security_group:
             nic.network_security_group = NetworkSecurityGroup()
-
-        sg = (self._provicer.security.security_groups.get(sg)
-              if isinstance(sg, str) else sg)
-        nic.network_security_group.id = sg.resource_id
+            nic.network_security_group.id = sg.resource_id
+        else:
+            sg_url_params = azure_helpers.\
+                parse_url(SECURITY_GROUP_RESOURCE_ID,
+                          nic.network_security_group.id)
+            existing_sg = self._provider.security.\
+                security_groups.get(sg_url_params.get(SECURITY_GROUP_NAME))
+
+            new_sg = self._provider.security.security_groups.\
+                create('{0}-{1}'.format(sg.name, existing_sg.name),
+                       'Merged security groups {0} and {1}'.
+                       format(sg.name, existing_sg.name))
+            new_sg.add_rule(src_group=sg)
+            new_sg.add_rule(src_group=existing_sg)
+            nic.network_security_group.id = new_sg.resource_id
 
         self._provider.azure_client.create_nic(self._nic_ids[0], nic)
 
     def remove_security_group(self, sg):
 
         '''
-                :param sg:
-                :return: None
-
-                This method removes the security group to VM instance.
-                In Azure, security group added to Network interface.
-                Azure supports to add only one security group to
-                network interface, we are removing the provided security group
-                if it associated to NIC
-                else we are ignoring.
-                '''
+        :param sg:
+        :return: None
+
+        This method removes the security group to VM instance.
+        In Azure, security group added to Network interface.
+        Azure supports to add only one security group to
+        network interface, we are removing the provided security group
+        if it associated to NIC
+        else we are ignoring.
+        '''
 
         nic = self._provider.azure_client.get_nic(self._nic_ids[0])
         sg = (self._provicer.security.security_groups.get(sg)

+ 39 - 28
cloudbridge/cloud/providers/azure/services.py

@@ -445,6 +445,8 @@ class AzureInstanceService(BaseInstanceService):
                key_pair=None, security_groups=None, user_data=None,
                launch_config=None, **kwargs):
 
+        instance_name = "{0}-{1}".format(name, uuid.uuid4().hex[:6])
+
         # Key_pair is mandatory in azure and it should not be None.
         if key_pair:
             key_pair = (self.provider.security.key_pairs.get(key_pair)
@@ -467,8 +469,9 @@ class AzureInstanceService(BaseInstanceService):
 
         zone_id = zone.id if isinstance(zone, PlacementZone) else zone
 
-        subnet_id, zone_id, security_group_ids = \
-            self._resolve_launch_options(subnet, zone_id, security_groups)
+        subnet_id, zone_id, security_group_id = \
+            self._resolve_launch_options(instance_name,
+                                         subnet, zone_id, security_groups)
 
         if launch_config:
             disks, root_disk_size = \
@@ -478,8 +481,6 @@ class AzureInstanceService(BaseInstanceService):
             disks = None
             root_disk_size = None
 
-        instance_name = "{0}-{1}".format(name, uuid.uuid4().hex[:6])
-
         nic_params = {
                 'location': self._provider.region_name,
                 'ip_configurations': [{
@@ -491,9 +492,9 @@ class AzureInstanceService(BaseInstanceService):
                 }]
             }
 
-        if security_group_ids and len(security_group_ids) > 0:
+        if security_group_id:
             nic_params['network_security_group'] = {
-                'id': security_group_ids[0]
+                'id': security_group_id
             }
         nic_info = self.provider.azure_client.create_nic(
             instance_name + '_nic',
@@ -549,7 +550,7 @@ class AzureInstanceService(BaseInstanceService):
         vm = self._provider.azure_client.get_vm(instance_name)
         return AzureInstance(self.provider, vm)
 
-    def _resolve_launch_options(self, subnet=None, zone_id=None,
+    def _resolve_launch_options(self, name, subnet=None, zone_id=None,
                                 security_groups=None):
         """
         Work out interdependent launch options.
@@ -574,18 +575,30 @@ class AzureInstanceService(BaseInstanceService):
         if subnet:
             # subnet's zone takes precedence
             zone_id = subnet.zone.id
-        security_group_ids = None
-        if isinstance(security_groups, list):
+        security_group_id = None
+
+        if isinstance(security_groups, list) and len(security_groups) > 0:
+
             if isinstance(security_groups[0], SecurityGroup):
-                security_group_ids = [sg.resource_id for sg in security_groups]
+                security_groups_ids = [sg.id for sg in security_groups]
+                security_group_id = security_groups[0].resource_id
             else:
-                groups = []
+                security_groups_ids = security_groups
+                seuciry_group = self.provider.security.\
+                    security_groups.get(security_groups[0])
+                security_group_id = seuciry_group.resource_id
+
+            if len(security_groups) > 1:
+                new_sg = self.provider.security.security_groups.\
+                    create('{0}-sg'.format(name), 'Merge security groups {0}'.
+                           format(','.join(security_groups_ids)))
+
                 for sg in security_groups:
-                    sg_obj = self.provider.security.security_groups.get(sg)
-                    groups.append(sg_obj)
-                security_group_ids = [sg.resource_id for sg in groups]
+                    new_sg.add_rule(src_group=sg)
+
+                security_group_id = new_sg.resource_id
 
-        return subnet.resource_id, zone_id, security_group_ids
+        return subnet.resource_id, zone_id, security_group_id
 
     def _process_block_device_mappings(self, launch_config,
                                        vm_name, zone=None):
@@ -890,9 +903,11 @@ class AzureSubnetService(BaseSubnetService):
         :param subnet_id:
         :return:
         """
-        subnets = [subnet for subnet in self._list_subnets()
-                   if subnet.id == subnet_id]
-        return subnets[0] if len(subnets) else None
+        subnet_id_parts = subnet_id.split('|$|')
+        azure_subnet = self.provider.azure_client.\
+            get_subnet(subnet_id_parts[0], subnet_id_parts[1])
+        return AzureSubnet(self.provider,
+                           azure_subnet) if azure_subnet else None
 
     def list(self, network=None, limit=None, marker=None):
         """
@@ -928,7 +943,7 @@ class AzureSubnetService(BaseSubnetService):
         if not name:
             subnet_name = AzureSubnet.CB_DEFAULT_SUBNET_NAME
         else:
-            subnet_name = "{0}-{1}".format(name, uuid.uuid4().hex[:6])
+            subnet_name = name
 
         subnet_info = self.provider.azure_client\
             .create_subnet(
@@ -984,15 +999,11 @@ class AzureSubnetService(BaseSubnetService):
             # It also requires network id. To get the network id
             # code is doing an explicit get and retrieving the network id
 
-            if not isinstance(subnet, Subnet):
-                subnet = self.get(subnet)
-            if subnet:
-                self.provider.azure_client.delete_subnet(
-                    subnet.network_id,
-                    subnet.id
-                )
-                return True
-            return False
+            subnet_id = subnet.id if isinstance(subnet, Subnet) else subnet
+            subnet_id_parts = subnet_id.split('|$|')
+            self.provider.azure_client.\
+                delete_subnet(subnet_id_parts[0], subnet_id_parts[1])
+            return True
         except CloudError as cloudError:
             # Azure raises the cloud error if the resource not available
             log.exception(cloudError.message)