+
+ try:
+
+ resName = self._get_resource_name_from_resource_id(vm_id)
+ vm = self.conn_compute.virtual_machines.get(self.resource_group, resName)
+
+ # Shuts down the virtual machine and releases the compute resources
+ #vm_stop = self.conn_compute.virtual_machines.power_off(self.resource_group, resName)
+ #vm_stop.wait()
+
+ vm_delete = self.conn_compute.virtual_machines.delete(self.resource_group, resName)
+ vm_delete.wait()
+
+ # Delete OS Disk
+ os_disk_name = vm.storage_profile.os_disk.name
+ self.logger.debug('Delete OS DISK - ' + os_disk_name)
+ self.conn_compute.disks.delete(self.resource_group, os_disk_name)
+
+ # After deletting VM, it is necessary delete NIC, because if is not deleted delete_network
+ # does not work because Azure says that is in use the subnet
+ network_interfaces = vm.network_profile.network_interfaces
+
+ for network_interface in network_interfaces:
+
+ #self.logger.debug('nic - {}'.format(network_interface))
+
+ nic_name = self._get_resource_name_from_resource_id(network_interface.id)
+
+ #self.logger.debug('nic_name - {}'.format(nic_name))
+
+ nic_data = self.conn_vnet.network_interfaces.get(
+ self.resource_group,
+ nic_name)
+
+ exist_public_ip = nic_data.ip_configurations[0].public_ip_address
+ if exist_public_ip:
+ public_ip_id = nic_data.ip_configurations[0].public_ip_address.id
+ self.logger.debug('Public ip id - ' + public_ip_id)
+
+ self.logger.debug('Delete NIC - ' + nic_name)
+ nic_delete = self.conn_vnet.network_interfaces.delete(self.resource_group, nic_name)
+ nic_delete.wait()
+
+ # Delete public_ip
+ public_ip_name = self._get_resource_name_from_resource_id(public_ip_id)
+
+ self.logger.debug('Delete PUBLIC IP - ' + public_ip_name)
+ public_ip = self.conn_vnet.public_ip_addresses.delete(self.resource_group, public_ip_name)
+ except CloudError as e:
+ if e.error.error == "ResourceNotFound":
+ raise vimconn.vimconnNotFoundException("No vminstance found '{}'".format(vm_id))
+ else:
+ raise
+ except Exception as e:
+ self.format_vimconn_exception(e)
+
+ def action_vminstance(self, vm_id, action_dict, created_items={}):
+ """Send and action over a VM instance from VIM
+ Returns the vm_id if the action was successfully sent to the VIM"""
+
+ self.logger.debug("Action over VM '%s': %s", vm_id, str(action_dict))
+ try:
+ self._reload_connection()
+ resName = self._get_resource_name_from_resource_id(vm_id)
+ if "start" in action_dict:
+ self.conn_compute.virtual_machines.start(self.resource_group,resName)
+ elif "stop" in action_dict or "shutdown" in action_dict or "shutoff" in action_dict:
+ self.conn_compute.virtual_machines.power_off(self.resource_group,resName)
+ elif "terminate" in action_dict:
+ self.conn_compute.virtual_machines.delete(self.resource_group,resName)
+ elif "reboot" in action_dict:
+ self.conn_compute.virtual_machines.restart(self.resource_group,resName)
+ return None
+ except CloudError as e:
+ if e.error.error == "ResourceNotFound":
+ raise vimconn.vimconnNotFoundException("No vm found '{}'".format(vm_id))
+ else:
+ raise
+ except Exception as e:
+ self.format_vimconn_exception(e)
+
+ def delete_flavor(self, flavor_id):
+
+ raise vimconn.vimconnAuthException("It is not possible to delete a FLAVOR in AZURE")
+
+ def delete_tenant(self,tenant_id,):
+
+ raise vimconn.vimconnAuthException("It is not possible to delete a TENANT in AZURE")
+
+ def delete_image(self, image_id):
+
+ raise vimconn.vimconnAuthException("It is not possible to delete a IMAGE in AZURE")