Fix bug 2302: IP ranges management in Azure not working properly
[osm/RO.git] / RO-VIM-azure / osm_rovim_azure / vimconn_azure.py
index d93ff5c..06bc970 100755 (executable)
@@ -56,7 +56,6 @@ def find_in_list(the_list, condition_lambda):
 
 
 class vimconnector(vimconn.VimConnector):
-
     # Translate azure provisioning state to OSM provision state
     # The first three ones are the transitional status once a user initiated action has been requested
     # Once the operation is complete, it will transition into the states Succeeded or Failed
@@ -204,7 +203,7 @@ class vimconnector(vimconn.VimConnector):
         # Variable that indicates if client must be reloaded or initialized
         self.reload_client = True
 
-        self.vnet_address_space = None
+        self.vnet_address_space = []
 
         # LOGGER
         self.logger = logging.getLogger("ro.vim.azure")
@@ -408,7 +407,7 @@ class vimconnector(vimconn.VimConnector):
             vnet = self.conn_vnet.virtual_networks.get(
                 self.vnet_resource_group or self.resource_group, self.vnet_name
             )
-            self.vnet_address_space = vnet.address_space.address_prefixes[0]
+            self.vnet_address_space = vnet.address_space.address_prefixes
             self.vnet_id = vnet.id
 
             return
@@ -425,7 +424,7 @@ class vimconnector(vimconn.VimConnector):
                 "location": self.region,
                 "address_space": {"address_prefixes": ["10.0.0.0/8"]},
             }
-            self.vnet_address_space = "10.0.0.0/8"
+            self.vnet_address_space = ["10.0.0.0/8"]
 
             self.logger.debug("create base vnet: %s", self.vnet_name)
             self.conn_vnet.virtual_networks.begin_create_or_update(
@@ -486,16 +485,21 @@ class vimconnector(vimconn.VimConnector):
         if ip_profile is None:
             # get a non used vnet ip range /24 and allocate automatically inside the range self.vnet_address_space
             used_subnets = self.get_network_list()
-            for ip_range in netaddr.IPNetwork(self.vnet_address_space).subnet(24):
-                for used_subnet in used_subnets:
-                    subnet_range = netaddr.IPNetwork(used_subnet["cidr_block"])
-
-                    if subnet_range in ip_range or ip_range in subnet_range:
-                        # this range overlaps with an existing subnet ip range. Breaks and look for another
+            for space in self.vnet_address_space:
+                for ip_range in netaddr.IPNetwork(space).subnet(24):
+                    for used_subnet in used_subnets:
+                        subnet_range = netaddr.IPNetwork(used_subnet["cidr_block"])
+
+                        if subnet_range in ip_range or ip_range in subnet_range:
+                            # this range overlaps with an existing subnet ip range. Breaks and look for another
+                            break
+                    else:
+                        ip_profile = {"subnet_address": str(ip_range)}
+                        self.logger.debug(
+                            "dinamically obtained ip_profile: %s", ip_range
+                        )
                         break
-                else:
-                    ip_profile = {"subnet_address": str(ip_range)}
-                    self.logger.debug("dinamically obtained ip_profile: %s", ip_range)
+                if ip_profile is not None:
                     break
             else:
                 raise vimconn.VimConnException(
@@ -970,7 +974,6 @@ class vimconnector(vimconn.VimConnector):
             self._format_vimconn_exception(e)
 
     def _build_os_profile(self, vm_name, cloud_config, image_id):
-
         # initial os_profile
         os_profile = {"computer_name": vm_name}
 
@@ -1142,7 +1145,7 @@ class vimconnector(vimconn.VimConnector):
             vm_sizes_list = [
                 vm_size.as_dict()
                 for vm_size in self.conn_compute.resource_skus.list(
-                    "location eq '{}'".format(self.region)
+                    filter="location eq '{}'".format(self.region)
                 )
             ]
 
@@ -1220,7 +1223,7 @@ class vimconnector(vimconn.VimConnector):
             vm_sizes_list = [
                 vm_size.as_dict()
                 for vm_size in self.conn_compute.resource_skus.list(
-                    "location eq '{}'".format(self.region)
+                    filter="location eq '{}'".format(self.region)
                 )
             ]
 
@@ -1315,7 +1318,6 @@ class vimconnector(vimconn.VimConnector):
             self._format_vimconn_exception(e)
 
     def delete_inuse_nic(self, nic_name):
-
         # Obtain nic data
         nic_data = self.conn_vnet.network_interfaces.get(self.resource_group, nic_name)
 
@@ -1338,7 +1340,6 @@ class vimconnector(vimconn.VimConnector):
 
             # TODO - check if there is a public ip to delete and delete it
             if network_interfaces:
-
                 # Deallocate the vm
                 async_vm_deallocate = (
                     self.conn_compute.virtual_machines.begin_deallocate(
@@ -1786,7 +1787,21 @@ class vimconnector(vimconn.VimConnector):
                 vm = self.conn_compute.virtual_machines.get(
                     self.resource_group, res_name
                 )
-                out_vm["vim_info"] = str(vm)
+                img = vm.storage_profile.image_reference
+                images = self._get_version_image_list(
+                    img.publisher, img.offer, img.sku, img.version
+                )
+                vim_info = {
+                    "id": vm.id,
+                    "name": vm.name,
+                    "location": vm.location,
+                    "provisioning_state": vm.provisioning_state,
+                    "vm_id": vm.vm_id,
+                    "type": vm.type,
+                    "flavor": {"id": vm.hardware_profile.vm_size},
+                    "image": images[0],
+                }
+                out_vm["vim_info"] = str(vim_info)
                 out_vm["status"] = self.provision_state2osm.get(
                     vm.provisioning_state, "OTHER"
                 )
@@ -1866,6 +1881,10 @@ class vimconnector(vimconn.VimConnector):
                     self.logger.debug("Public ip address is: %s", public_ip.ip_address)
                     ips.append(public_ip.ip_address)
 
+                subnet = nic_data.ip_configurations[0].subnet.id
+                if subnet:
+                    interface_dict["vim_net_id"] = subnet
+
                 private_ip = nic_data.ip_configurations[0].private_ip_address
                 ips.append(private_ip)