self._reload_connection()
network_dict = {"name": net_name, "admin_state_up": True}
- if net_type in ("data", "ptp"):
+ if net_type in ("data", "ptp") or provider_network_profile:
provider_physical_network = None
if provider_network_profile and provider_network_profile.get(
# For VF
elif net["type"] == "VF" or net["type"] == "SR-IOV":
-
port_dict["binding:vnic_type"] = "direct"
# VIO specific Changes
key_id = "vim_volume_id" if "vim_volume_id" in disk.keys() else "vim_id"
if disk.get(key_id):
-
block_device_mapping["vd" + chr(base_disk_index)] = disk[key_id]
existing_vim_volumes.append({"id": disk[key_id]})
availability_zone=vm_av_zone,
)
boot_volume_id = volume.id
- created_items["volume:" + str(volume.id)] = True
- block_device_mapping["vd" + chr(base_disk_index)] = volume.id
+ self.update_block_device_mapping(
+ volume=volume,
+ block_device_mapping=block_device_mapping,
+ base_disk_index=base_disk_index,
+ disk=disk,
+ created_items=created_items,
+ )
return boot_volume_id
+ @staticmethod
+ def update_block_device_mapping(
+ volume: object,
+ block_device_mapping: dict,
+ base_disk_index: int,
+ disk: dict,
+ created_items: dict,
+ ) -> None:
+ """Add volume information to block device mapping dict.
+ Args:
+ volume (object): Created volume object
+ block_device_mapping (dict): Block device details
+ base_disk_index (int): Disk index
+ disk (dict): Disk details
+ created_items (dict): All created items belongs to VM
+ """
+ if not volume:
+ raise vimconn.VimConnException("Volume is empty.")
+
+ if not hasattr(volume, "id"):
+ raise vimconn.VimConnException(
+ "Created volume is not valid, does not have id attribute."
+ )
+
+ volume_txt = "volume:" + str(volume.id)
+ if disk.get("keep"):
+ volume_txt += ":keep"
+ created_items[volume_txt] = True
+ block_device_mapping["vd" + chr(base_disk_index)] = volume.id
+
def _prepare_non_root_persistent_volumes(
self,
name: str,
key_id = "vim_volume_id" if "vim_volume_id" in disk.keys() else "vim_id"
if disk.get(key_id):
-
# Use existing persistent volume
block_device_mapping["vd" + chr(base_disk_index)] = disk[key_id]
existing_vim_volumes.append({"id": disk[key_id]})
# Make sure volume is in the same AZ as the VM to be attached to
availability_zone=vm_av_zone,
)
- created_items["volume:" + str(volume.id)] = True
- block_device_mapping["vd" + chr(base_disk_index)] = volume.id
+ self.update_block_device_mapping(
+ volume=volume,
+ block_device_mapping=block_device_mapping,
+ base_disk_index=base_disk_index,
+ disk=disk,
+ created_items=created_items,
+ )
def _wait_for_created_volumes_availability(
self, elapsed_time: int, created_items: dict
while elapsed_time < volume_timeout:
for created_item in created_items:
- v, _, volume_id = created_item.partition(":")
+ v, volume_id = (
+ created_item.split(":")[0],
+ created_item.split(":")[1],
+ )
if v == "volume":
if self.cinder.volumes.get(volume_id).status != "available":
break
return self.neutron.show_floatingip(free_floating_ip)
def _get_free_floating_ip(
- self, server: object, floating_network: dict, created_items: dict
+ self, server: object, floating_network: dict
) -> Optional[str]:
"""Get the free floating IP address.
Args:
server (object): Server Object
floating_network (dict): Floating network details
- created_items (dict): All created items belongs to new VM instance
Returns:
free_floating_ip (str): Free floating ip addr
# Randomize
random.shuffle(floating_ips)
- return self._find_floating_ip(
- server, floating_ips, floating_network, created_items
- )
+ return self._find_floating_ip(server, floating_ips, floating_network)
def _prepare_external_network_for_vminstance(
self,
# In case of RO in HA there can be conflicts, two RO trying to assign same floating IP, so retry
# several times
while not assigned:
-
free_floating_ip = self._get_free_floating_ip(
- server, floating_network, created_items
+ server, floating_network
)
if not free_floating_ip:
self.neutron.update_port(port[0], port_update)
except Exception:
-
raise vimconn.VimConnException(
"It was not possible to disable port security for port {}".format(
port[0]
the method delete_vminstance and action_vminstance. Can be used to store created ports, volumes, etc.
Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same
as not present.
+
"""
self.logger.debug(
"new_vminstance input: image='%s' flavor='%s' nics='%s'",
server_id = server.id
try:
+ created_items = self.remove_keep_tag_from_persistent_volumes(
+ created_items
+ )
+
self.delete_vminstance(server_id, created_items)
except Exception as e2:
self._format_exception(e)
+ @staticmethod
+ def remove_keep_tag_from_persistent_volumes(created_items: Dict) -> Dict:
+ """Removes the keep flag from persistent volumes. So, those volumes could be removed.
+
+ Args:
+ created_items (dict): All created items belongs to VM
+
+ Returns:
+ updated_created_items (dict): Dict which does not include keep flag for volumes.
+
+ """
+ return {
+ key.replace(":keep", ""): value for (key, value) in created_items.items()
+ }
+
def get_vminstance(self, vm_id):
"""Returns the VM instance information from VIM"""
# self.logger.debug("Getting VM from VIM")
k_id (str): Port id in the VIM
"""
try:
-
port_dict = self.neutron.list_ports()
existing_ports = [port["id"] for port in port_dict["ports"] if port_dict]
self.neutron.delete_port(k_id)
except Exception as e:
-
self.logger.error("Error deleting port: {}: {}".format(type(e).__name__, e))
def _delete_volumes_by_id_wth_cinder(
k_item, k_id = self._get_item_name_id(k)
if k_item == "volume":
-
unavailable_vol = self._delete_volumes_by_id_wth_cinder(
k, k_id, volumes_to_hold, created_items
)
keep_waiting = True
elif k_item == "floating_ip":
-
self._delete_floating_ip_by_id(k, k_id, created_items)
except Exception as e:
return keep_waiting
+ @staticmethod
+ def _extract_items_wth_keep_flag_from_created_items(created_items: dict) -> dict:
+ """Remove the volumes which has key flag from created_items
+
+ Args:
+ created_items (dict): All created items belongs to VM
+
+ Returns:
+ created_items (dict): Persistent volumes eliminated created_items
+ """
+ return {
+ key: value
+ for (key, value) in created_items.items()
+ if len(key.split(":")) == 2
+ }
+
def delete_vminstance(
self, vm_id: str, created_items: dict = None, volumes_to_hold: list = None
) -> None:
volumes_to_hold = []
try:
+ created_items = self._extract_items_wth_keep_flag_from_created_items(
+ created_items
+ )
+
self._reload_connection()
# Delete VM ports attached to the networks before the virtual machine
def action_vminstance(self, vm_id, action_dict, created_items={}):
"""Send and action over a VM instance from VIM
- Returns None or the console dict if the action was successfully sent to the VIM"""
+ Returns None or the console dict if the action was successfully sent to the VIM
+ """
self.logger.debug("Action over VM '%s': %s", vm_id, str(action_dict))
try: