X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=RO-VIM-azure%2Fosm_rovim_azure%2Fvimconn_azure.py;h=332a1ead6870de940fd4f5407802f1b54ec0d323;hb=80c9dd0040eeccccd6abc2ec5b131b1ba672359e;hp=a1d3aa827a9ea8aeeffd38e1a7ed83034bebe97f;hpb=66af69485ee8c6921ba901b8b594565290d41a24;p=osm%2FRO.git diff --git a/RO-VIM-azure/osm_rovim_azure/vimconn_azure.py b/RO-VIM-azure/osm_rovim_azure/vimconn_azure.py index a1d3aa82..332a1ead 100755 --- a/RO-VIM-azure/osm_rovim_azure/vimconn_azure.py +++ b/RO-VIM-azure/osm_rovim_azure/vimconn_azure.py @@ -14,28 +14,27 @@ ## import base64 -from osm_ro_plugin import vimconn import logging -import netaddr +from os import getenv import re -from os import getenv +from azure.core.exceptions import ResourceNotFoundError from azure.identity import ClientSecretCredential -from azure.mgmt.resource import ResourceManagementClient -from azure.mgmt.network import NetworkManagementClient from azure.mgmt.compute import ComputeManagementClient from azure.mgmt.compute.models import DiskCreateOption -from azure.core.exceptions import ResourceNotFoundError +from azure.mgmt.network import NetworkManagementClient +from azure.mgmt.resource import ResourceManagementClient from azure.profiles import ProfileDefinition -from msrestazure.azure_exceptions import CloudError +from cryptography.hazmat.backends import default_backend as crypto_default_backend +from cryptography.hazmat.primitives import serialization as crypto_serialization +from cryptography.hazmat.primitives.asymmetric import rsa from msrest.exceptions import AuthenticationError +from msrestazure.azure_exceptions import CloudError import msrestazure.tools as azure_tools +import netaddr +from osm_ro_plugin import vimconn from requests.exceptions import ConnectionError -from cryptography.hazmat.primitives import serialization as crypto_serialization -from cryptography.hazmat.primitives.asymmetric import rsa -from cryptography.hazmat.backends import default_backend as crypto_default_backend - __author__ = "Isabel Lloret, Sergio Gonzalez, Alfonso Tierno, Gerardo Garcia" __date__ = "$18-apr-2019 23:59:59$" @@ -58,7 +57,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 @@ -249,6 +247,9 @@ class vimconnector(vimconn.VimConnector): if "vnet_name" in config: self.vnet_name = config["vnet_name"] + # VNET_RESOURCE_GROUP + self.vnet_resource_group = config.get("vnet_resource_group") + # TODO - not used, do anything about it? # public ssh key self.pub_key = config.get("pub_key") @@ -405,7 +406,7 @@ class vimconnector(vimconn.VimConnector): """ try: vnet = self.conn_vnet.virtual_networks.get( - self.resource_group, self.vnet_name + self.vnet_resource_group or self.resource_group, self.vnet_name ) self.vnet_address_space = vnet.address_space.address_prefixes[0] self.vnet_id = vnet.id @@ -413,7 +414,7 @@ class vimconnector(vimconn.VimConnector): return except CloudError as e: if e.error.error and "notfound" in e.error.error.lower(): - pass + self.logger.exception("CloudError Exception occured.") # continue and create it else: self._format_vimconn_exception(e) @@ -428,10 +429,12 @@ class vimconnector(vimconn.VimConnector): self.logger.debug("create base vnet: %s", self.vnet_name) self.conn_vnet.virtual_networks.begin_create_or_update( - self.resource_group, self.vnet_name, vnet_params + self.vnet_resource_group or self.resource_group, + self.vnet_name, + vnet_params, ) vnet = self.conn_vnet.virtual_networks.get( - self.resource_group, self.vnet_name + self.vnet_resource_group or self.resource_group, self.vnet_name ) self.vnet_id = vnet.id except Exception as e: @@ -511,7 +514,10 @@ class vimconnector(vimconn.VimConnector): self.logger.debug("creating subnet_name: {}".format(subnet_name)) async_creation = self.conn_vnet.subnets.begin_create_or_update( - self.resource_group, self.vnet_name, subnet_name, subnet_params + self.vnet_resource_group or self.resource_group, + self.vnet_name, + subnet_name, + subnet_params, ) async_creation.wait() # TODO - do not wait here, check where it is used @@ -526,7 +532,9 @@ class vimconnector(vimconn.VimConnector): Adds a prefix to the subnet_name with a number in case the indicated name is repeated Checks subnets with the indicated name (without suffix) and adds a suffix with a number """ - all_subnets = self.conn_vnet.subnets.list(self.resource_group, self.vnet_name) + all_subnets = self.conn_vnet.subnets.list( + self.vnet_resource_group or self.resource_group, self.vnet_name + ) # Filter to subnets starting with the indicated name subnets = list( filter(lambda subnet: (subnet.name.startswith(subnet_name)), all_subnets) @@ -548,7 +556,10 @@ class vimconnector(vimconn.VimConnector): self._reload_connection() subnet_id = net["net_id"] - location = self.region or self._get_location_from_resource_group(self.resource_group) + location = self.region or self._get_location_from_resource_group( + self.resource_group + ) + try: net_ifz = {"location": location} net_ip_config = { @@ -814,7 +825,7 @@ class vimconnector(vimconn.VimConnector): self._reload_connection() vnet = self.conn_vnet.virtual_networks.get( - self.resource_group, self.vnet_name + self.vnet_resource_group or self.resource_group, self.vnet_name ) subnet_list = [] @@ -853,6 +864,7 @@ class vimconnector(vimconn.VimConnector): start, image_id, flavor_id, + affinity_group_list, net_list, cloud_config=None, disk_list=None, @@ -982,7 +994,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} @@ -1358,7 +1369,9 @@ class vimconnector(vimconn.VimConnector): def delete_network(self, net_id, created_items=None): self.logger.debug( - "deleting network {} - {}".format(self.resource_group, net_id) + "deleting network {} - {}".format( + self.vnet_resource_group or self.resource_group, net_id + ) ) self._reload_connection() @@ -1367,7 +1380,9 @@ class vimconnector(vimconn.VimConnector): try: # Obtain subnets ant try to delete nic first subnet = self.conn_vnet.subnets.get( - self.resource_group, self.vnet_name, res_name + self.vnet_resource_group or self.resource_group, + self.vnet_name, + res_name, ) if not subnet: raise vimconn.VimConnNotFoundException( @@ -1386,7 +1401,9 @@ class vimconnector(vimconn.VimConnector): # Subnet API fails (CloudError: Azure Error: ResourceNotFound) # Put the initial virtual_network API async_delete = self.conn_vnet.subnets.begin_delete( - self.resource_group, self.vnet_name, res_name + self.vnet_resource_group or self.resource_group, + self.vnet_name, + res_name, ) async_delete.wait() @@ -1407,7 +1424,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) @@ -1430,7 +1446,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( @@ -1462,7 +1477,7 @@ class vimconnector(vimconn.VimConnector): nic_delete.wait() self.logger.debug("deleted NIC name: %s", nic_name) - def delete_vminstance(self, vm_id, created_items=None): + def delete_vminstance(self, vm_id, created_items=None, volumes_to_hold=None): """Deletes a vm instance from the vim.""" self.logger.debug( "deleting VM instance {} - {}".format(self.resource_group, vm_id) @@ -1799,7 +1814,9 @@ class vimconnector(vimconn.VimConnector): netName = self._get_net_name_from_resource_id(net_id) resName = self._get_resource_name_from_resource_id(net_id) - net = self.conn_vnet.subnets.get(self.resource_group, netName, resName) + net = self.conn_vnet.subnets.get( + self.vnet_resource_group or self.resource_group, netName, resName + ) out_nets[net_id] = { "status": self.provision_state2osm[net.provisioning_state], @@ -1979,6 +1996,26 @@ class vimconnector(vimconn.VimConnector): else: return self._default_admin_user + def migrate_instance(self, vm_id, compute_host=None): + """ + Migrate a vdu + param: + vm_id: ID of an instance + compute_host: Host to migrate the vdu to + """ + # TODO: Add support for migration + raise vimconn.VimConnNotImplemented("Not implemented") + + def resize_instance(self, vm_id, flavor_id=None): + """ + resize a vdu + param: + vm_id: ID of an instance + flavor_id: flavor id to resize the vdu + """ + # TODO: Add support for resize + raise vimconn.VimConnNotImplemented("Not implemented") + if __name__ == "__main__": # Init logger @@ -2065,7 +2102,7 @@ if __name__ == "__main__": logger.debug("List networks") network_list = azure.get_network_list({"name": "internal"}) logger.debug("Network_list: {}".format(network_list)) - + logger.debug("Show machine isabelvm") vmachine = azure.get_vminstance( ("/subscriptions/5c1a2458-dfde-4adf-a4e3-08fa0e21d171/resourceGroups/{}" "/providers/Microsoft.Compute/virtualMachines/isabelVM" @@ -2089,7 +2126,7 @@ if __name__ == "__main__": "/Skus/18.04-LTS/Versions/18.04.201809110") """ """ - + network_id = ("subscriptions/5c1a2458-dfde-4adf-a4e3-08fa0e21d171/resourceGroups/{} "/providers/Microsoft.Network/virtualNetworks/osm_vnet/subnets/internal" ).format(test_params["resource_group"])