X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=RO-VIM-azure%2Fosm_rovim_azure%2Fvimconn_azure.py;h=0b31d0294a26badc2542c1a27c4c93bfd07dfa52;hb=7b521f73dd996a279f23b2f512cd89a42c1c79f6;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..0b31d029 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$" @@ -249,6 +248,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 +407,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 +415,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 +430,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 +515,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 +533,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 +557,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 +826,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 +865,7 @@ class vimconnector(vimconn.VimConnector): start, image_id, flavor_id, + affinity_group_list, net_list, cloud_config=None, disk_list=None, @@ -1358,7 +1371,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 +1382,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 +1403,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() @@ -1462,7 +1481,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 +1818,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 +2000,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 +2106,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 +2130,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"])