| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | ## |
| 4 | # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U. |
| 5 | # This file is part of openmano |
| 6 | # All Rights Reserved. |
| 7 | # |
| 8 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | # not use this file except in compliance with the License. You may obtain |
| 10 | # a copy of the License at |
| 11 | # |
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | # |
| 14 | # Unless required by applicable law or agreed to in writing, software |
| 15 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 17 | # License for the specific language governing permissions and limitations |
| 18 | # under the License. |
| 19 | # |
| 20 | # For those usages not covered by the Apache License, Version 2.0 please |
| 21 | # contact with: nfvlabs@tid.es |
| 22 | ## |
| 23 | |
| 24 | ''' |
| 25 | osconnector implements all the methods to interact with openstack using the python-client. |
| 26 | ''' |
| 27 | __author__="Alfonso Tierno, Gerardo Garcia" |
| 28 | __date__ ="$22-jun-2014 11:19:29$" |
| 29 | |
| 30 | import vimconn |
| 31 | import json |
| 32 | import yaml |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 33 | import logging |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 34 | import netaddr |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 35 | |
| 36 | from novaclient import client as nClient, exceptions as nvExceptions |
| 37 | import keystoneclient.v2_0.client as ksClient |
| 38 | import keystoneclient.exceptions as ksExceptions |
| 39 | import glanceclient.v2.client as glClient |
| 40 | import glanceclient.client as gl1Client |
| 41 | import glanceclient.exc as gl1Exceptions |
| 42 | from httplib import HTTPException |
| 43 | from neutronclient.neutron import client as neClient |
| 44 | from neutronclient.common import exceptions as neExceptions |
| 45 | from requests.exceptions import ConnectionError |
| 46 | |
| 47 | '''contain the openstack virtual machine status to openmano status''' |
| 48 | vmStatus2manoFormat={'ACTIVE':'ACTIVE', |
| 49 | 'PAUSED':'PAUSED', |
| 50 | 'SUSPENDED': 'SUSPENDED', |
| 51 | 'SHUTOFF':'INACTIVE', |
| 52 | 'BUILD':'BUILD', |
| 53 | 'ERROR':'ERROR','DELETED':'DELETED' |
| 54 | } |
| 55 | netStatus2manoFormat={'ACTIVE':'ACTIVE','PAUSED':'PAUSED','INACTIVE':'INACTIVE','BUILD':'BUILD','ERROR':'ERROR','DELETED':'DELETED' |
| 56 | } |
| 57 | |
| 58 | class vimconnector(vimconn.vimconnector): |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 59 | def __init__(self, uuid, name, tenant_id, tenant_name, url, url_admin=None, user=None, passwd=None, log_level="DEBUG", config={}): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 60 | '''using common constructor parameters. In this case |
| 61 | 'url' is the keystone authorization url, |
| 62 | 'url_admin' is not use |
| 63 | ''' |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 64 | vimconn.vimconnector.__init__(self, uuid, name, tenant_id, tenant_name, url, url_admin, user, passwd, log_level, config) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 65 | |
| 66 | self.k_creds={} |
| 67 | self.n_creds={} |
| 68 | if not url: |
| 69 | raise TypeError, 'url param can not be NoneType' |
| 70 | self.k_creds['auth_url'] = url |
| 71 | self.n_creds['auth_url'] = url |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 72 | if tenant_name: |
| 73 | self.k_creds['tenant_name'] = tenant_name |
| 74 | self.n_creds['project_id'] = tenant_name |
| 75 | if tenant_id: |
| 76 | self.k_creds['tenant_id'] = tenant_id |
| 77 | self.n_creds['tenant_id'] = tenant_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 78 | if user: |
| 79 | self.k_creds['username'] = user |
| 80 | self.n_creds['username'] = user |
| 81 | if passwd: |
| 82 | self.k_creds['password'] = passwd |
| 83 | self.n_creds['api_key'] = passwd |
| 84 | self.reload_client = True |
| tierno | 73ad9e4 | 2016-09-12 18:11:11 +0200 | [diff] [blame] | 85 | self.logger = logging.getLogger('openmano.vim.openstack') |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 86 | |
| 87 | def __setitem__(self,index, value): |
| 88 | '''Set individuals parameters |
| 89 | Throw TypeError, KeyError |
| 90 | ''' |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 91 | if index=='tenant_id': |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 92 | self.reload_client=True |
| tierno | 392f285 | 2016-05-13 12:28:55 +0200 | [diff] [blame] | 93 | self.tenant_id = value |
| 94 | if value: |
| 95 | self.k_creds['tenant_id'] = value |
| 96 | self.n_creds['tenant_id'] = value |
| 97 | else: |
| 98 | del self.k_creds['tenant_name'] |
| 99 | del self.n_creds['project_id'] |
| 100 | elif index=='tenant_name': |
| 101 | self.reload_client=True |
| 102 | self.tenant_name = value |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 103 | if value: |
| 104 | self.k_creds['tenant_name'] = value |
| 105 | self.n_creds['project_id'] = value |
| 106 | else: |
| 107 | del self.k_creds['tenant_name'] |
| 108 | del self.n_creds['project_id'] |
| 109 | elif index=='user': |
| 110 | self.reload_client=True |
| 111 | self.user = value |
| 112 | if value: |
| 113 | self.k_creds['username'] = value |
| 114 | self.n_creds['username'] = value |
| 115 | else: |
| 116 | del self.k_creds['username'] |
| 117 | del self.n_creds['username'] |
| 118 | elif index=='passwd': |
| 119 | self.reload_client=True |
| 120 | self.passwd = value |
| 121 | if value: |
| 122 | self.k_creds['password'] = value |
| 123 | self.n_creds['api_key'] = value |
| 124 | else: |
| 125 | del self.k_creds['password'] |
| 126 | del self.n_creds['api_key'] |
| 127 | elif index=='url': |
| 128 | self.reload_client=True |
| 129 | self.url = value |
| 130 | if value: |
| 131 | self.k_creds['auth_url'] = value |
| 132 | self.n_creds['auth_url'] = value |
| 133 | else: |
| 134 | raise TypeError, 'url param can not be NoneType' |
| 135 | else: |
| 136 | vimconn.vimconnector.__setitem__(self,index, value) |
| 137 | |
| 138 | def _reload_connection(self): |
| 139 | '''Called before any operation, it check if credentials has changed |
| 140 | Throw keystoneclient.apiclient.exceptions.AuthorizationFailure |
| 141 | ''' |
| 142 | #TODO control the timing and possible token timeout, but it seams that python client does this task for us :-) |
| 143 | if self.reload_client: |
| 144 | #test valid params |
| 145 | if len(self.n_creds) <4: |
| 146 | raise ksExceptions.ClientException("Not enough parameters to connect to openstack") |
| 147 | self.nova = nClient.Client(2, **self.n_creds) |
| 148 | self.keystone = ksClient.Client(**self.k_creds) |
| 149 | self.glance_endpoint = self.keystone.service_catalog.url_for(service_type='image', endpoint_type='publicURL') |
| 150 | self.glance = glClient.Client(self.glance_endpoint, token=self.keystone.auth_token, **self.k_creds) #TODO check k_creds vs n_creds |
| 151 | self.ne_endpoint=self.keystone.service_catalog.url_for(service_type='network', endpoint_type='publicURL') |
| 152 | self.neutron = neClient.Client('2.0', endpoint_url=self.ne_endpoint, token=self.keystone.auth_token, **self.k_creds) |
| 153 | self.reload_client = False |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 154 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 155 | def __net_os2mano(self, net_list_dict): |
| 156 | '''Transform the net openstack format to mano format |
| 157 | net_list_dict can be a list of dict or a single dict''' |
| 158 | if type(net_list_dict) is dict: |
| 159 | net_list_=(net_list_dict,) |
| 160 | elif type(net_list_dict) is list: |
| 161 | net_list_=net_list_dict |
| 162 | else: |
| 163 | raise TypeError("param net_list_dict must be a list or a dictionary") |
| 164 | for net in net_list_: |
| 165 | if net.get('provider:network_type') == "vlan": |
| 166 | net['type']='data' |
| 167 | else: |
| 168 | net['type']='bridge' |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 169 | |
| 170 | |
| 171 | |
| 172 | def _format_exception(self, exception): |
| 173 | '''Transform a keystone, nova, neutron exception into a vimconn exception''' |
| 174 | if isinstance(exception, (HTTPException, gl1Exceptions.HTTPException, gl1Exceptions.CommunicationError, |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 175 | ConnectionError, ksExceptions.ConnectionError, neExceptions.ConnectionFailed |
| 176 | )): |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 177 | raise vimconn.vimconnConnectionException(type(exception).__name__ + ": " + str(exception)) |
| 178 | elif isinstance(exception, (nvExceptions.ClientException, ksExceptions.ClientException, |
| 179 | neExceptions.NeutronException, nvExceptions.BadRequest)): |
| 180 | raise vimconn.vimconnUnexpectedResponse(type(exception).__name__ + ": " + str(exception)) |
| 181 | elif isinstance(exception, (neExceptions.NetworkNotFoundClient, nvExceptions.NotFound)): |
| 182 | raise vimconn.vimconnNotFoundException(type(exception).__name__ + ": " + str(exception)) |
| 183 | elif isinstance(exception, nvExceptions.Conflict): |
| 184 | raise vimconn.vimconnConflictException(type(exception).__name__ + ": " + str(exception)) |
| 185 | else: # () |
| 186 | raise vimconn.vimconnConnectionException(type(exception).__name__ + ": " + str(exception)) |
| 187 | |
| 188 | def get_tenant_list(self, filter_dict={}): |
| 189 | '''Obtain tenants of VIM |
| 190 | filter_dict can contain the following keys: |
| 191 | name: filter by tenant name |
| 192 | id: filter by tenant uuid/id |
| 193 | <other VIM specific> |
| 194 | Returns the tenant list of dictionaries: [{'name':'<name>, 'id':'<id>, ...}, ...] |
| 195 | ''' |
| 196 | self.logger.debug("Getting tenant from VIM filter: '%s'", str(filter_dict)) |
| 197 | try: |
| 198 | self._reload_connection() |
| 199 | tenant_class_list=self.keystone.tenants.findall(**filter_dict) |
| 200 | tenant_list=[] |
| 201 | for tenant in tenant_class_list: |
| 202 | tenant_list.append(tenant.to_dict()) |
| 203 | return tenant_list |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 204 | except (ksExceptions.ConnectionError, ksExceptions.ClientException, ConnectionError) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 205 | self._format_exception(e) |
| 206 | |
| 207 | def new_tenant(self, tenant_name, tenant_description): |
| 208 | '''Adds a new tenant to openstack VIM. Returns the tenant identifier''' |
| 209 | self.logger.debug("Adding a new tenant name: %s", tenant_name) |
| 210 | try: |
| 211 | self._reload_connection() |
| 212 | tenant=self.keystone.tenants.create(tenant_name, tenant_description) |
| 213 | return tenant.id |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 214 | except (ksExceptions.ConnectionError, ksExceptions.ClientException, ConnectionError) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 215 | self._format_exception(e) |
| 216 | |
| 217 | def delete_tenant(self, tenant_id): |
| 218 | '''Delete a tenant from openstack VIM. Returns the old tenant identifier''' |
| 219 | self.logger.debug("Deleting tenant %s from VIM", tenant_id) |
| 220 | try: |
| 221 | self._reload_connection() |
| 222 | self.keystone.tenants.delete(tenant_id) |
| 223 | return tenant_id |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 224 | except (ksExceptions.ConnectionError, ksExceptions.ClientException, ConnectionError) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 225 | self._format_exception(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 226 | |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 227 | def new_network(self,net_name, net_type, ip_profile=None, shared=False, vlan=None): |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 228 | '''Adds a tenant network to VIM. Returns the network identifier''' |
| 229 | self.logger.debug("Adding a new network to VIM name '%s', type '%s'", net_name, net_type) |
| garciadeblas | edca7b3 | 2016-09-29 14:01:52 +0000 | [diff] [blame^] | 230 | #self.logger.debug(">>>>>>>>>>>>>>>>>> IP profile %s", str(ip_profile)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 231 | try: |
| garciadeblas | edca7b3 | 2016-09-29 14:01:52 +0000 | [diff] [blame^] | 232 | new_net = None |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 233 | self._reload_connection() |
| 234 | network_dict = {'name': net_name, 'admin_state_up': True} |
| 235 | if net_type=="data" or net_type=="ptp": |
| 236 | if self.config.get('dataplane_physical_net') == None: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 237 | raise vimconn.vimconnConflictException("You must provide a 'dataplane_physical_net' at config value before creating sriov network") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 238 | network_dict["provider:physical_network"] = self.config['dataplane_physical_net'] #"physnet_sriov" #TODO physical |
| 239 | network_dict["provider:network_type"] = "vlan" |
| 240 | if vlan!=None: |
| 241 | network_dict["provider:network_type"] = vlan |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 242 | network_dict["shared"]=shared |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 243 | new_net=self.neutron.create_network({'network':network_dict}) |
| 244 | #print new_net |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 245 | #create subnetwork, even if there is no profile |
| 246 | if not ip_profile: |
| 247 | ip_profile = {} |
| 248 | if 'subnet_address' not in ip_profile: |
| 249 | #Fake subnet is required |
| 250 | ip_profile['subnet_address'] = "192.168.111.0/24" |
| 251 | if 'ip_version' not in ip_profile: |
| 252 | ip_profile['ip_version'] = "IPv4" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 253 | subnet={"name":net_name+"-subnet", |
| 254 | "network_id": new_net["network"]["id"], |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 255 | "ip_version": 4 if ip_profile['ip_version']=="IPv4" else 6, |
| 256 | "cidr": ip_profile['subnet_address'] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 257 | } |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 258 | if 'gateway_address' in ip_profile: |
| 259 | subnet['gateway_ip'] = ip_profile['gateway_address'] |
| garciadeblas | edca7b3 | 2016-09-29 14:01:52 +0000 | [diff] [blame^] | 260 | if ip_profile.get('dns_address'): |
| garciadeblas | 9f8456e | 2016-09-05 05:02:59 +0200 | [diff] [blame] | 261 | #TODO: manage dns_address as a list of addresses separated by commas |
| 262 | subnet['dns_nameservers'] = [] |
| 263 | subnet['dns_nameservers'].append(ip_profile['dns_address']) |
| 264 | if 'dhcp_enabled' in ip_profile: |
| 265 | subnet['enable_dhcp'] = False if ip_profile['dhcp_enabled']=="false" else True |
| 266 | if 'dhcp_start_address' in ip_profile: |
| 267 | subnet['allocation_pools']=[] |
| 268 | subnet['allocation_pools'].append(dict()) |
| 269 | subnet['allocation_pools'][0]['start'] = ip_profile['dhcp_start_address'] |
| 270 | if 'dhcp_count' in ip_profile: |
| 271 | #parts = ip_profile['dhcp_start_address'].split('.') |
| 272 | #ip_int = (int(parts[0]) << 24) + (int(parts[1]) << 16) + (int(parts[2]) << 8) + int(parts[3]) |
| 273 | ip_int = int(netaddr.IPAddress(ip_profile['dhcp_start_address'])) |
| 274 | ip_int += ip_profile['dhcp_count'] |
| 275 | ip_str = str(netaddr.IPAddress(ip_int)) |
| 276 | subnet['allocation_pools'][0]['end'] = ip_str |
| garciadeblas | edca7b3 | 2016-09-29 14:01:52 +0000 | [diff] [blame^] | 277 | #self.logger.debug(">>>>>>>>>>>>>>>>>> Subnet: %s", str(subnet)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 278 | self.neutron.create_subnet({"subnet": subnet} ) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 279 | return new_net["network"]["id"] |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 280 | except (neExceptions.ConnectionFailed, ksExceptions.ClientException, neExceptions.NeutronException, ConnectionError) as e: |
| garciadeblas | edca7b3 | 2016-09-29 14:01:52 +0000 | [diff] [blame^] | 281 | if new_net: |
| 282 | self.neutron.delete_network(new_net['network']['id']) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 283 | self._format_exception(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 284 | |
| 285 | def get_network_list(self, filter_dict={}): |
| 286 | '''Obtain tenant networks of VIM |
| 287 | Filter_dict can be: |
| 288 | name: network name |
| 289 | id: network uuid |
| 290 | shared: boolean |
| 291 | tenant_id: tenant |
| 292 | admin_state_up: boolean |
| 293 | status: 'ACTIVE' |
| 294 | Returns the network list of dictionaries |
| 295 | ''' |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 296 | self.logger.debug("Getting network from VIM filter: '%s'", str(filter_dict)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 297 | try: |
| 298 | self._reload_connection() |
| 299 | net_dict=self.neutron.list_networks(**filter_dict) |
| 300 | net_list=net_dict["networks"] |
| 301 | self.__net_os2mano(net_list) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 302 | return net_list |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 303 | except (neExceptions.ConnectionFailed, ksExceptions.ClientException, neExceptions.NeutronException, ConnectionError) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 304 | self._format_exception(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 305 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 306 | def get_network(self, net_id): |
| 307 | '''Obtain details of network from VIM |
| 308 | Returns the network information from a network id''' |
| 309 | self.logger.debug(" Getting tenant network %s from VIM", net_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 310 | filter_dict={"id": net_id} |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 311 | net_list = self.get_network_list(filter_dict) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 312 | if len(net_list)==0: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 313 | raise vimconn.vimconnNotFoundException("Network '{}' not found".format(net_id)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 314 | elif len(net_list)>1: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 315 | raise vimconn.vimconnConflictException("Found more than one network with this criteria") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 316 | net = net_list[0] |
| 317 | subnets=[] |
| 318 | for subnet_id in net.get("subnets", () ): |
| 319 | try: |
| 320 | subnet = self.neutron.show_subnet(subnet_id) |
| 321 | except Exception as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 322 | self.logger.error("osconnector.get_network(): Error getting subnet %s %s" % (net_id, str(e))) |
| 323 | subnet = {"id": subnet_id, "fault": str(e)} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 324 | subnets.append(subnet) |
| 325 | net["subnets"] = subnets |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 326 | return net |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 327 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 328 | def delete_network(self, net_id): |
| 329 | '''Deletes a tenant network from VIM. Returns the old network identifier''' |
| 330 | self.logger.debug("Deleting network '%s' from VIM", net_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 331 | try: |
| 332 | self._reload_connection() |
| 333 | #delete VM ports attached to this networks before the network |
| 334 | ports = self.neutron.list_ports(network_id=net_id) |
| 335 | for p in ports['ports']: |
| 336 | try: |
| 337 | self.neutron.delete_port(p["id"]) |
| 338 | except Exception as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 339 | self.logger.error("Error deleting port %s: %s", p["id"], str(e)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 340 | self.neutron.delete_network(net_id) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 341 | return net_id |
| 342 | except (neExceptions.ConnectionFailed, neExceptions.NetworkNotFoundClient, neExceptions.NeutronException, |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 343 | ksExceptions.ClientException, neExceptions.NeutronException, ConnectionError) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 344 | self._format_exception(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 345 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 346 | def refresh_nets_status(self, net_list): |
| 347 | '''Get the status of the networks |
| 348 | Params: the list of network identifiers |
| 349 | Returns a dictionary with: |
| 350 | net_id: #VIM id of this network |
| 351 | status: #Mandatory. Text with one of: |
| 352 | # DELETED (not found at vim) |
| 353 | # VIM_ERROR (Cannot connect to VIM, VIM response error, ...) |
| 354 | # OTHER (Vim reported other status not understood) |
| 355 | # ERROR (VIM indicates an ERROR status) |
| 356 | # ACTIVE, INACTIVE, DOWN (admin down), |
| 357 | # BUILD (on building process) |
| 358 | # |
| 359 | error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR |
| 360 | vim_info: #Text with plain information obtained from vim (yaml.safe_dump) |
| 361 | |
| 362 | ''' |
| 363 | net_dict={} |
| 364 | for net_id in net_list: |
| 365 | net = {} |
| 366 | try: |
| 367 | net_vim = self.get_network(net_id) |
| 368 | if net_vim['status'] in netStatus2manoFormat: |
| 369 | net["status"] = netStatus2manoFormat[ net_vim['status'] ] |
| 370 | else: |
| 371 | net["status"] = "OTHER" |
| 372 | net["error_msg"] = "VIM status reported " + net_vim['status'] |
| 373 | |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 374 | if net['status'] == "ACTIVE" and not net_vim['admin_state_up']: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 375 | net['status'] = 'DOWN' |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 376 | try: |
| 377 | net['vim_info'] = yaml.safe_dump(net_vim, default_flow_style=True, width=256) |
| 378 | except yaml.representer.RepresenterError: |
| 379 | net['vim_info'] = str(net_vim) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 380 | if net_vim.get('fault'): #TODO |
| 381 | net['error_msg'] = str(net_vim['fault']) |
| 382 | except vimconn.vimconnNotFoundException as e: |
| 383 | self.logger.error("Exception getting net status: %s", str(e)) |
| 384 | net['status'] = "DELETED" |
| 385 | net['error_msg'] = str(e) |
| 386 | except vimconn.vimconnException as e: |
| 387 | self.logger.error("Exception getting net status: %s", str(e)) |
| 388 | net['status'] = "VIM_ERROR" |
| 389 | net['error_msg'] = str(e) |
| 390 | net_dict[net_id] = net |
| 391 | return net_dict |
| 392 | |
| 393 | def get_flavor(self, flavor_id): |
| 394 | '''Obtain flavor details from the VIM. Returns the flavor dict details''' |
| 395 | self.logger.debug("Getting flavor '%s'", flavor_id) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 396 | try: |
| 397 | self._reload_connection() |
| 398 | flavor = self.nova.flavors.find(id=flavor_id) |
| 399 | #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 400 | return flavor.to_dict() |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 401 | except (nvExceptions.NotFound, nvExceptions.ClientException, ksExceptions.ClientException, ConnectionError) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 402 | self._format_exception(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 403 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 404 | def new_flavor(self, flavor_data, change_name_if_used=True): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 405 | '''Adds a tenant flavor to openstack VIM |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 406 | if change_name_if_used is True, it will change name in case of conflict, because it is not supported name repetition |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 407 | Returns the flavor identifier |
| 408 | ''' |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 409 | self.logger.debug("Adding flavor '%s'", str(flavor_data)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 410 | retry=0 |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 411 | max_retries=3 |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 412 | name_suffix = 0 |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 413 | name=flavor_data['name'] |
| 414 | while retry<max_retries: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 415 | retry+=1 |
| 416 | try: |
| 417 | self._reload_connection() |
| 418 | if change_name_if_used: |
| 419 | #get used names |
| 420 | fl_names=[] |
| 421 | fl=self.nova.flavors.list() |
| 422 | for f in fl: |
| 423 | fl_names.append(f.name) |
| 424 | while name in fl_names: |
| 425 | name_suffix += 1 |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 426 | name = flavor_data['name']+"-" + str(name_suffix) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 427 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 428 | ram = flavor_data.get('ram',64) |
| 429 | vcpus = flavor_data.get('vcpus',1) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 430 | numa_properties=None |
| 431 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 432 | extended = flavor_data.get("extended") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 433 | if extended: |
| 434 | numas=extended.get("numas") |
| 435 | if numas: |
| 436 | numa_nodes = len(numas) |
| 437 | if numa_nodes > 1: |
| 438 | return -1, "Can not add flavor with more than one numa" |
| 439 | numa_properties = {"hw:numa_nodes":str(numa_nodes)} |
| 440 | numa_properties["hw:mem_page_size"] = "large" |
| 441 | numa_properties["hw:cpu_policy"] = "dedicated" |
| 442 | numa_properties["hw:numa_mempolicy"] = "strict" |
| 443 | for numa in numas: |
| 444 | #overwrite ram and vcpus |
| 445 | ram = numa['memory']*1024 |
| 446 | if 'paired-threads' in numa: |
| 447 | vcpus = numa['paired-threads']*2 |
| 448 | numa_properties["hw:cpu_threads_policy"] = "prefer" |
| 449 | elif 'cores' in numa: |
| 450 | vcpus = numa['cores'] |
| 451 | #numa_properties["hw:cpu_threads_policy"] = "prefer" |
| 452 | elif 'threads' in numa: |
| 453 | vcpus = numa['threads'] |
| 454 | numa_properties["hw:cpu_policy"] = "isolated" |
| 455 | for interface in numa.get("interfaces",() ): |
| 456 | if interface["dedicated"]=="yes": |
| tierno | 809a780 | 2016-07-08 13:31:24 +0200 | [diff] [blame] | 457 | raise vimconn.vimconnException("Passthrough interfaces are not supported for the openstack connector", http_code=vimconn.HTTP_Service_Unavailable) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 458 | #TODO, add the key 'pci_passthrough:alias"="<label at config>:<number ifaces>"' when a way to connect it is available |
| 459 | |
| 460 | #create flavor |
| 461 | new_flavor=self.nova.flavors.create(name, |
| 462 | ram, |
| 463 | vcpus, |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 464 | flavor_data.get('disk',1), |
| 465 | is_public=flavor_data.get('is_public', True) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 466 | ) |
| 467 | #add metadata |
| 468 | if numa_properties: |
| 469 | new_flavor.set_keys(numa_properties) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 470 | return new_flavor.id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 471 | except nvExceptions.Conflict as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 472 | if change_name_if_used and retry < max_retries: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 473 | continue |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 474 | self._format_exception(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 475 | #except nvExceptions.BadRequest as e: |
| 476 | except (ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 477 | self._format_exception(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 478 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 479 | def delete_flavor(self,flavor_id): |
| 480 | '''Deletes a tenant flavor from openstack VIM. Returns the old flavor_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 481 | ''' |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 482 | try: |
| 483 | self._reload_connection() |
| 484 | self.nova.flavors.delete(flavor_id) |
| 485 | return flavor_id |
| 486 | #except nvExceptions.BadRequest as e: |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 487 | except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 488 | self._format_exception(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 489 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 490 | def new_image(self,image_dict): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 491 | ''' |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 492 | Adds a tenant image to VIM. imge_dict is a dictionary with: |
| 493 | name: name |
| 494 | disk_format: qcow2, vhd, vmdk, raw (by default), ... |
| 495 | location: path or URI |
| 496 | public: "yes" or "no" |
| 497 | metadata: metadata of the image |
| 498 | Returns the image_id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 499 | ''' |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 500 | #using version 1 of glance client |
| 501 | glancev1 = gl1Client.Client('1',self.glance_endpoint, token=self.keystone.auth_token, **self.k_creds) #TODO check k_creds vs n_creds |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 502 | retry=0 |
| 503 | max_retries=3 |
| 504 | while retry<max_retries: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 505 | retry+=1 |
| 506 | try: |
| 507 | self._reload_connection() |
| 508 | #determine format http://docs.openstack.org/developer/glance/formats.html |
| 509 | if "disk_format" in image_dict: |
| 510 | disk_format=image_dict["disk_format"] |
| 511 | else: #autodiscover base on extention |
| 512 | if image_dict['location'][-6:]==".qcow2": |
| 513 | disk_format="qcow2" |
| 514 | elif image_dict['location'][-4:]==".vhd": |
| 515 | disk_format="vhd" |
| 516 | elif image_dict['location'][-5:]==".vmdk": |
| 517 | disk_format="vmdk" |
| 518 | elif image_dict['location'][-4:]==".vdi": |
| 519 | disk_format="vdi" |
| 520 | elif image_dict['location'][-4:]==".iso": |
| 521 | disk_format="iso" |
| 522 | elif image_dict['location'][-4:]==".aki": |
| 523 | disk_format="aki" |
| 524 | elif image_dict['location'][-4:]==".ari": |
| 525 | disk_format="ari" |
| 526 | elif image_dict['location'][-4:]==".ami": |
| 527 | disk_format="ami" |
| 528 | else: |
| 529 | disk_format="raw" |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 530 | self.logger.debug("new_image: '%s' loading from '%s'", image_dict['name'], image_dict['location']) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 531 | if image_dict['location'][0:4]=="http": |
| 532 | new_image = glancev1.images.create(name=image_dict['name'], is_public=image_dict.get('public',"yes")=="yes", |
| 533 | container_format="bare", location=image_dict['location'], disk_format=disk_format) |
| 534 | else: #local path |
| 535 | with open(image_dict['location']) as fimage: |
| 536 | new_image = glancev1.images.create(name=image_dict['name'], is_public=image_dict.get('public',"yes")=="yes", |
| 537 | container_format="bare", data=fimage, disk_format=disk_format) |
| 538 | #insert metadata. We cannot use 'new_image.properties.setdefault' |
| 539 | #because nova and glance are "INDEPENDENT" and we are using nova for reading metadata |
| 540 | new_image_nova=self.nova.images.find(id=new_image.id) |
| 541 | new_image_nova.metadata.setdefault('location',image_dict['location']) |
| 542 | metadata_to_load = image_dict.get('metadata') |
| 543 | if metadata_to_load: |
| 544 | for k,v in yaml.load(metadata_to_load).iteritems(): |
| 545 | new_image_nova.metadata.setdefault(k,v) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 546 | return new_image.id |
| 547 | except (nvExceptions.Conflict, ksExceptions.ClientException, nvExceptions.ClientException) as e: |
| 548 | self._format_exception(e) |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 549 | except (HTTPException, gl1Exceptions.HTTPException, gl1Exceptions.CommunicationError, ConnectionError) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 550 | if retry==max_retries: |
| 551 | continue |
| 552 | self._format_exception(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 553 | except IOError as e: #can not open the file |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 554 | raise vimconn.vimconnConnectionException(type(e).__name__ + ": " + str(e)+ " for " + image_dict['location'], |
| 555 | http_code=vimconn.HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 556 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 557 | def delete_image(self, image_id): |
| 558 | '''Deletes a tenant image from openstack VIM. Returns the old id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 559 | ''' |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 560 | try: |
| 561 | self._reload_connection() |
| 562 | self.nova.images.delete(image_id) |
| 563 | return image_id |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 564 | except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, ConnectionError) as e: #TODO remove |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 565 | self._format_exception(e) |
| 566 | |
| 567 | def get_image_id_from_path(self, path): |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 568 | '''Get the image id from image path in the VIM database. Returns the image_id''' |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 569 | try: |
| 570 | self._reload_connection() |
| 571 | images = self.nova.images.list() |
| 572 | for image in images: |
| 573 | if image.metadata.get("location")==path: |
| 574 | return image.id |
| 575 | raise vimconn.vimconnNotFoundException("image with location '{}' not found".format( path)) |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 576 | except (ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, ConnectionError) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 577 | self._format_exception(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 578 | |
| garciadeblas | b69fa9f | 2016-09-28 12:04:10 +0200 | [diff] [blame] | 579 | def get_image_list(self, filter_dict={}): |
| 580 | '''Obtain tenant images from VIM |
| 581 | Filter_dict can be: |
| 582 | id: image id |
| 583 | name: image name |
| 584 | checksum: image checksum |
| 585 | Returns the image list of dictionaries: |
| 586 | [{<the fields at Filter_dict plus some VIM specific>}, ...] |
| 587 | List can be empty |
| 588 | ''' |
| 589 | self.logger.debug("Getting image list from VIM filter: '%s'", str(filter_dict)) |
| 590 | try: |
| 591 | self._reload_connection() |
| 592 | filter_dict_os=filter_dict.copy() |
| 593 | #First we filter by the available filter fields: name, id. The others are removed. |
| 594 | filter_dict_os.pop('checksum',None) |
| 595 | image_list=self.nova.images.findall(**filter_dict_os) |
| 596 | if len(image_list)==0: |
| 597 | return [] |
| 598 | #Then we filter by the rest of filter fields: checksum |
| 599 | filtered_list = [] |
| 600 | for image in image_list: |
| 601 | image_dict=glance.images.get(image.id) |
| 602 | if image_dict['checksum']==filter_dict.get('checksum'): |
| 603 | filtered_list.append(image) |
| 604 | return filtered_list |
| 605 | except (ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, ConnectionError) as e: |
| 606 | self._format_exception(e) |
| 607 | |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame] | 608 | def new_vminstance(self,name,description,start,image_id,flavor_id,net_list,cloud_config=None): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 609 | '''Adds a VM instance to VIM |
| 610 | Params: |
| 611 | start: indicates if VM must start or boot in pause mode. Ignored |
| 612 | image_id,flavor_id: iamge and flavor uuid |
| 613 | net_list: list of interfaces, each one is a dictionary with: |
| 614 | name: |
| 615 | net_id: network uuid to connect |
| 616 | vpci: virtual vcpi to assign, ignored because openstack lack #TODO |
| 617 | model: interface model, ignored #TODO |
| 618 | mac_address: used for SR-IOV ifaces #TODO for other types |
| 619 | use: 'data', 'bridge', 'mgmt' |
| 620 | type: 'virtual', 'PF', 'VF', 'VFnotShared' |
| 621 | vim_id: filled/added by this function |
| 622 | #TODO ip, security groups |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 623 | Returns the instance identifier |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 624 | ''' |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 625 | self.logger.debug("Creating VM image '%s' flavor '%s' nics='%s'",image_id, flavor_id,str(net_list)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 626 | try: |
| tierno | 6e11623 | 2016-07-18 13:01:40 +0200 | [diff] [blame] | 627 | metadata={} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 628 | net_list_vim=[] |
| 629 | self._reload_connection() |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 630 | metadata_vpci={} #For a specific neutron plugin |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 631 | for net in net_list: |
| 632 | if not net.get("net_id"): #skip non connected iface |
| 633 | continue |
| 634 | if net["type"]=="virtual": |
| 635 | net_list_vim.append({'net-id': net["net_id"]}) |
| 636 | if "vpci" in net: |
| 637 | metadata_vpci[ net["net_id"] ] = [[ net["vpci"], "" ]] |
| 638 | elif net["type"]=="PF": |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 639 | self.logger.warn("new_vminstance: Warning, can not connect a passthrough interface ") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 640 | #TODO insert this when openstack consider passthrough ports as openstack neutron ports |
| 641 | else: #VF |
| 642 | if "vpci" in net: |
| 643 | if "VF" not in metadata_vpci: |
| 644 | metadata_vpci["VF"]=[] |
| 645 | metadata_vpci["VF"].append([ net["vpci"], "" ]) |
| 646 | port_dict={ |
| 647 | "network_id": net["net_id"], |
| 648 | "name": net.get("name"), |
| 649 | "binding:vnic_type": "direct", |
| 650 | "admin_state_up": True |
| 651 | } |
| 652 | if not port_dict["name"]: |
| 653 | port_dict["name"] = name |
| 654 | if net.get("mac_address"): |
| 655 | port_dict["mac_address"]=net["mac_address"] |
| 656 | #TODO: manage having SRIOV without vlan tag |
| 657 | #if net["type"] == "VFnotShared" |
| 658 | # port_dict["vlan"]=0 |
| 659 | new_port = self.neutron.create_port({"port": port_dict }) |
| 660 | net["mac_adress"] = new_port["port"]["mac_address"] |
| 661 | net["vim_id"] = new_port["port"]["id"] |
| 662 | net["ip"] = new_port["port"].get("fixed_ips",[{}])[0].get("ip_address") |
| 663 | net_list_vim.append({"port-id": new_port["port"]["id"]}) |
| 664 | if metadata_vpci: |
| 665 | metadata = {"pci_assignement": json.dumps(metadata_vpci)} |
| tierno | afbced4 | 2016-07-23 01:43:53 +0200 | [diff] [blame] | 666 | if len(metadata["pci_assignement"]) >255: |
| tierno | 6e11623 | 2016-07-18 13:01:40 +0200 | [diff] [blame] | 667 | #limit the metadata size |
| 668 | #metadata["pci_assignement"] = metadata["pci_assignement"][0:255] |
| 669 | self.logger.warn("Metadata deleted since it exceeds the expected length (255) ") |
| 670 | metadata = {} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 671 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 672 | self.logger.debug("name '%s' image_id '%s'flavor_id '%s' net_list_vim '%s' description '%s' metadata %s", |
| 673 | name, image_id, flavor_id, str(net_list_vim), description, str(metadata)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 674 | |
| 675 | security_groups = self.config.get('security_groups') |
| 676 | if type(security_groups) is str: |
| 677 | security_groups = ( security_groups, ) |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame] | 678 | if isinstance(cloud_config, dict): |
| 679 | userdata="#cloud-config\nusers:\n" |
| 680 | #default user |
| 681 | if "key-pairs" in cloud_config: |
| 682 | userdata += " - default:\n ssh-authorized-keys:\n" |
| 683 | for key in cloud_config["key-pairs"]: |
| 684 | userdata += " - '{key}'\n".format(key=key) |
| 685 | for user in cloud_config.get("users",[]): |
| 686 | userdata += " - name: {name}\n sudo: ALL=(ALL) NOPASSWD:ALL\n".format(name=user["name"]) |
| 687 | if "user-info" in user: |
| 688 | userdata += " gecos: {}'\n".format(user["user-info"]) |
| 689 | if user.get("key-pairs"): |
| 690 | userdata += " ssh-authorized-keys:\n" |
| 691 | for key in user["key-pairs"]: |
| 692 | userdata += " - '{key}'\n".format(key=key) |
| 693 | self.logger.debug("userdata: %s", userdata) |
| 694 | elif isinstance(cloud_config, str): |
| 695 | userdata = cloud_config |
| 696 | else: |
| 697 | userdata=None |
| 698 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 699 | server = self.nova.servers.create(name, image_id, flavor_id, nics=net_list_vim, meta=metadata, |
| 700 | security_groups = security_groups, |
| 701 | availability_zone = self.config.get('availability_zone'), |
| 702 | key_name = self.config.get('keypair'), |
| tierno | a4e1a6e | 2016-08-31 14:19:40 +0200 | [diff] [blame] | 703 | userdata=userdata |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 704 | ) #, description=description) |
| 705 | |
| 706 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 707 | #print "DONE :-)", server |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 708 | |
| 709 | # #TODO server.add_floating_ip("10.95.87.209") |
| 710 | # #To look for a free floating_ip |
| 711 | # free_floating_ip = None |
| 712 | # for floating_ip in self.neutron.list_floatingips().get("floatingips", () ): |
| 713 | # if not floating_ip["port_id"]: |
| 714 | # free_floating_ip = floating_ip["floating_ip_address"] |
| 715 | # break |
| 716 | # if free_floating_ip: |
| 717 | # server.add_floating_ip(free_floating_ip) |
| 718 | |
| 719 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 720 | return server.id |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 721 | # except nvExceptions.NotFound as e: |
| 722 | # error_value=-vimconn.HTTP_Not_Found |
| 723 | # error_text= "vm instance %s not found" % vm_id |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 724 | except (ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError |
| 725 | ) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 726 | self._format_exception(e) |
| 727 | except TypeError as e: |
| 728 | raise vimconn.vimconnException(type(e).__name__ + ": "+ str(e), http_code=vimconn.HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 729 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 730 | def get_vminstance(self,vm_id): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 731 | '''Returns the VM instance information from VIM''' |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 732 | #self.logger.debug("Getting VM from VIM") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 733 | try: |
| 734 | self._reload_connection() |
| 735 | server = self.nova.servers.find(id=vm_id) |
| 736 | #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 737 | return server.to_dict() |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 738 | except (ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.NotFound, ConnectionError) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 739 | self._format_exception(e) |
| 740 | |
| 741 | def get_vminstance_console(self,vm_id, console_type="vnc"): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 742 | ''' |
| 743 | Get a console for the virtual machine |
| 744 | Params: |
| 745 | vm_id: uuid of the VM |
| 746 | console_type, can be: |
| 747 | "novnc" (by default), "xvpvnc" for VNC types, |
| 748 | "rdp-html5" for RDP types, "spice-html5" for SPICE types |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 749 | Returns dict with the console parameters: |
| 750 | protocol: ssh, ftp, http, https, ... |
| 751 | server: usually ip address |
| 752 | port: the http, ssh, ... port |
| 753 | suffix: extra text, e.g. the http path and query string |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 754 | ''' |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 755 | self.logger.debug("Getting VM CONSOLE from VIM") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 756 | try: |
| 757 | self._reload_connection() |
| 758 | server = self.nova.servers.find(id=vm_id) |
| 759 | if console_type == None or console_type == "novnc": |
| 760 | console_dict = server.get_vnc_console("novnc") |
| 761 | elif console_type == "xvpvnc": |
| 762 | console_dict = server.get_vnc_console(console_type) |
| 763 | elif console_type == "rdp-html5": |
| 764 | console_dict = server.get_rdp_console(console_type) |
| 765 | elif console_type == "spice-html5": |
| 766 | console_dict = server.get_spice_console(console_type) |
| 767 | else: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 768 | raise vimconn.vimconnException("console type '{}' not allowed".format(console_type), http_code=vimconn.HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 769 | |
| 770 | console_dict1 = console_dict.get("console") |
| 771 | if console_dict1: |
| 772 | console_url = console_dict1.get("url") |
| 773 | if console_url: |
| 774 | #parse console_url |
| 775 | protocol_index = console_url.find("//") |
| 776 | suffix_index = console_url[protocol_index+2:].find("/") + protocol_index+2 |
| 777 | port_index = console_url[protocol_index+2:suffix_index].find(":") + protocol_index+2 |
| 778 | if protocol_index < 0 or port_index<0 or suffix_index<0: |
| 779 | return -vimconn.HTTP_Internal_Server_Error, "Unexpected response from VIM" |
| 780 | console_dict={"protocol": console_url[0:protocol_index], |
| 781 | "server": console_url[protocol_index+2:port_index], |
| 782 | "port": console_url[port_index:suffix_index], |
| 783 | "suffix": console_url[suffix_index+1:] |
| 784 | } |
| 785 | protocol_index += 2 |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 786 | return console_dict |
| 787 | raise vimconn.vimconnUnexpectedResponse("Unexpected response from VIM") |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 788 | |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 789 | except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.BadRequest, ConnectionError) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 790 | self._format_exception(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 791 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 792 | def delete_vminstance(self, vm_id): |
| 793 | '''Removes a VM instance from VIM. Returns the old identifier |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 794 | ''' |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 795 | #print "osconnector: Getting VM from VIM" |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 796 | try: |
| 797 | self._reload_connection() |
| 798 | #delete VM ports attached to this networks before the virtual machine |
| 799 | ports = self.neutron.list_ports(device_id=vm_id) |
| 800 | for p in ports['ports']: |
| 801 | try: |
| 802 | self.neutron.delete_port(p["id"]) |
| 803 | except Exception as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 804 | self.logger.error("Error deleting port: " + type(e).__name__ + ": "+ str(e)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 805 | self.nova.servers.delete(vm_id) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 806 | return vm_id |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 807 | except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 808 | self._format_exception(e) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 809 | #TODO insert exception vimconn.HTTP_Unauthorized |
| 810 | #if reaching here is because an exception |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 811 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 812 | def refresh_vms_status(self, vm_list): |
| 813 | '''Get the status of the virtual machines and their interfaces/ports |
| 814 | Params: the list of VM identifiers |
| 815 | Returns a dictionary with: |
| 816 | vm_id: #VIM id of this Virtual Machine |
| 817 | status: #Mandatory. Text with one of: |
| 818 | # DELETED (not found at vim) |
| 819 | # VIM_ERROR (Cannot connect to VIM, VIM response error, ...) |
| 820 | # OTHER (Vim reported other status not understood) |
| 821 | # ERROR (VIM indicates an ERROR status) |
| 822 | # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running), |
| 823 | # CREATING (on building process), ERROR |
| 824 | # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address |
| 825 | # |
| 826 | error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR |
| 827 | vim_info: #Text with plain information obtained from vim (yaml.safe_dump) |
| 828 | interfaces: |
| 829 | - vim_info: #Text with plain information obtained from vim (yaml.safe_dump) |
| 830 | mac_address: #Text format XX:XX:XX:XX:XX:XX |
| 831 | vim_net_id: #network id where this interface is connected |
| 832 | vim_interface_id: #interface/port VIM id |
| 833 | ip_address: #null, or text with IPv4, IPv6 address |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 834 | ''' |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 835 | vm_dict={} |
| 836 | self.logger.debug("refresh_vms status: Getting tenant VM instance information from VIM") |
| 837 | for vm_id in vm_list: |
| 838 | vm={} |
| 839 | try: |
| 840 | vm_vim = self.get_vminstance(vm_id) |
| 841 | if vm_vim['status'] in vmStatus2manoFormat: |
| 842 | vm['status'] = vmStatus2manoFormat[ vm_vim['status'] ] |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 843 | else: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 844 | vm['status'] = "OTHER" |
| 845 | vm['error_msg'] = "VIM status reported " + vm_vim['status'] |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 846 | try: |
| 847 | vm['vim_info'] = yaml.safe_dump(vm_vim, default_flow_style=True, width=256) |
| 848 | except yaml.representer.RepresenterError: |
| 849 | vm['vim_info'] = str(vm_vim) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 850 | vm["interfaces"] = [] |
| 851 | if vm_vim.get('fault'): |
| 852 | vm['error_msg'] = str(vm_vim['fault']) |
| 853 | #get interfaces |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 854 | try: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 855 | self._reload_connection() |
| 856 | port_dict=self.neutron.list_ports(device_id=vm_id) |
| 857 | for port in port_dict["ports"]: |
| 858 | interface={} |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 859 | try: |
| 860 | interface['vim_info'] = yaml.safe_dump(port, default_flow_style=True, width=256) |
| 861 | except yaml.representer.RepresenterError: |
| 862 | interface['vim_info'] = str(port) |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 863 | interface["mac_address"] = port.get("mac_address") |
| 864 | interface["vim_net_id"] = port["network_id"] |
| 865 | interface["vim_interface_id"] = port["id"] |
| 866 | ips=[] |
| 867 | #look for floating ip address |
| 868 | floating_ip_dict = self.neutron.list_floatingips(port_id=port["id"]) |
| 869 | if floating_ip_dict.get("floatingips"): |
| 870 | ips.append(floating_ip_dict["floatingips"][0].get("floating_ip_address") ) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 871 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 872 | for subnet in port["fixed_ips"]: |
| 873 | ips.append(subnet["ip_address"]) |
| 874 | interface["ip_address"] = ";".join(ips) |
| 875 | vm["interfaces"].append(interface) |
| 876 | except Exception as e: |
| 877 | self.logger.error("Error getting vm interface information " + type(e).__name__ + ": "+ str(e)) |
| 878 | except vimconn.vimconnNotFoundException as e: |
| 879 | self.logger.error("Exception getting vm status: %s", str(e)) |
| 880 | vm['status'] = "DELETED" |
| 881 | vm['error_msg'] = str(e) |
| 882 | except vimconn.vimconnException as e: |
| 883 | self.logger.error("Exception getting vm status: %s", str(e)) |
| 884 | vm['status'] = "VIM_ERROR" |
| 885 | vm['error_msg'] = str(e) |
| 886 | vm_dict[vm_id] = vm |
| 887 | return vm_dict |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 888 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 889 | def action_vminstance(self, vm_id, action_dict): |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 890 | '''Send and action over a VM instance from VIM |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 891 | Returns the vm_id if the action was successfully sent to the VIM''' |
| 892 | self.logger.debug("Action over VM '%s': %s", vm_id, str(action_dict)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 893 | try: |
| 894 | self._reload_connection() |
| 895 | server = self.nova.servers.find(id=vm_id) |
| 896 | if "start" in action_dict: |
| 897 | if action_dict["start"]=="rebuild": |
| 898 | server.rebuild() |
| 899 | else: |
| 900 | if server.status=="PAUSED": |
| 901 | server.unpause() |
| 902 | elif server.status=="SUSPENDED": |
| 903 | server.resume() |
| 904 | elif server.status=="SHUTOFF": |
| 905 | server.start() |
| 906 | elif "pause" in action_dict: |
| 907 | server.pause() |
| 908 | elif "resume" in action_dict: |
| 909 | server.resume() |
| 910 | elif "shutoff" in action_dict or "shutdown" in action_dict: |
| 911 | server.stop() |
| 912 | elif "forceOff" in action_dict: |
| 913 | server.stop() #TODO |
| 914 | elif "terminate" in action_dict: |
| 915 | server.delete() |
| 916 | elif "createImage" in action_dict: |
| 917 | server.create_image() |
| 918 | #"path":path_schema, |
| 919 | #"description":description_schema, |
| 920 | #"name":name_schema, |
| 921 | #"metadata":metadata_schema, |
| 922 | #"imageRef": id_schema, |
| 923 | #"disk": {"oneOf":[{"type": "null"}, {"type":"string"}] }, |
| 924 | elif "rebuild" in action_dict: |
| 925 | server.rebuild(server.image['id']) |
| 926 | elif "reboot" in action_dict: |
| 927 | server.reboot() #reboot_type='SOFT' |
| 928 | elif "console" in action_dict: |
| 929 | console_type = action_dict["console"] |
| 930 | if console_type == None or console_type == "novnc": |
| 931 | console_dict = server.get_vnc_console("novnc") |
| 932 | elif console_type == "xvpvnc": |
| 933 | console_dict = server.get_vnc_console(console_type) |
| 934 | elif console_type == "rdp-html5": |
| 935 | console_dict = server.get_rdp_console(console_type) |
| 936 | elif console_type == "spice-html5": |
| 937 | console_dict = server.get_spice_console(console_type) |
| 938 | else: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 939 | raise vimconn.vimconnException("console type '{}' not allowed".format(console_type), |
| 940 | http_code=vimconn.HTTP_Bad_Request) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 941 | try: |
| 942 | console_url = console_dict["console"]["url"] |
| 943 | #parse console_url |
| 944 | protocol_index = console_url.find("//") |
| 945 | suffix_index = console_url[protocol_index+2:].find("/") + protocol_index+2 |
| 946 | port_index = console_url[protocol_index+2:suffix_index].find(":") + protocol_index+2 |
| 947 | if protocol_index < 0 or port_index<0 or suffix_index<0: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 948 | raise vimconn.vimconnException("Unexpected response from VIM " + str(console_dict)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 949 | console_dict2={"protocol": console_url[0:protocol_index], |
| 950 | "server": console_url[protocol_index+2 : port_index], |
| 951 | "port": int(console_url[port_index+1 : suffix_index]), |
| 952 | "suffix": console_url[suffix_index+1:] |
| 953 | } |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 954 | return console_dict2 |
| 955 | except Exception as e: |
| 956 | raise vimconn.vimconnException("Unexpected response from VIM " + str(console_dict)) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 957 | |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 958 | return vm_id |
| tierno | 8e995ce | 2016-09-22 08:13:00 +0000 | [diff] [blame] | 959 | except (ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.NotFound, ConnectionError) as e: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 960 | self._format_exception(e) |
| 961 | #TODO insert exception vimconn.HTTP_Unauthorized |
| 962 | |
| 963 | #NOT USED FUNCTIONS |
| 964 | |
| 965 | def new_external_port(self, port_data): |
| 966 | #TODO openstack if needed |
| 967 | '''Adds a external port to VIM''' |
| 968 | '''Returns the port identifier''' |
| 969 | return -vimconn.HTTP_Internal_Server_Error, "osconnector.new_external_port() not implemented" |
| 970 | |
| 971 | def connect_port_network(self, port_id, network_id, admin=False): |
| 972 | #TODO openstack if needed |
| 973 | '''Connects a external port to a network''' |
| 974 | '''Returns status code of the VIM response''' |
| 975 | return -vimconn.HTTP_Internal_Server_Error, "osconnector.connect_port_network() not implemented" |
| 976 | |
| 977 | def new_user(self, user_name, user_passwd, tenant_id=None): |
| 978 | '''Adds a new user to openstack VIM''' |
| 979 | '''Returns the user identifier''' |
| 980 | self.logger.debug("osconnector: Adding a new user to VIM") |
| 981 | try: |
| 982 | self._reload_connection() |
| 983 | user=self.keystone.users.create(user_name, user_passwd, tenant_id=tenant_id) |
| 984 | #self.keystone.tenants.add_user(self.k_creds["username"], #role) |
| 985 | return user.id |
| 986 | except ksExceptions.ConnectionError as e: |
| 987 | error_value=-vimconn.HTTP_Bad_Request |
| 988 | error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0])) |
| 989 | except ksExceptions.ClientException as e: #TODO remove |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 990 | error_value=-vimconn.HTTP_Bad_Request |
| 991 | error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0])) |
| 992 | #TODO insert exception vimconn.HTTP_Unauthorized |
| 993 | #if reaching here is because an exception |
| 994 | if self.debug: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 995 | self.logger.debug("new_user " + error_text) |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 996 | return error_value, error_text |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 997 | |
| 998 | def delete_user(self, user_id): |
| 999 | '''Delete a user from openstack VIM''' |
| 1000 | '''Returns the user identifier''' |
| 1001 | if self.debug: |
| 1002 | print "osconnector: Deleting a user from VIM" |
| 1003 | try: |
| 1004 | self._reload_connection() |
| 1005 | self.keystone.users.delete(user_id) |
| 1006 | return 1, user_id |
| 1007 | except ksExceptions.ConnectionError as e: |
| 1008 | error_value=-vimconn.HTTP_Bad_Request |
| 1009 | error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0])) |
| 1010 | except ksExceptions.NotFound as e: |
| 1011 | error_value=-vimconn.HTTP_Not_Found |
| 1012 | error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0])) |
| 1013 | except ksExceptions.ClientException as e: #TODO remove |
| 1014 | error_value=-vimconn.HTTP_Bad_Request |
| 1015 | error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0])) |
| 1016 | #TODO insert exception vimconn.HTTP_Unauthorized |
| 1017 | #if reaching here is because an exception |
| 1018 | if self.debug: |
| 1019 | print "delete_tenant " + error_text |
| 1020 | return error_value, error_text |
| 1021 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1022 | def get_hosts_info(self): |
| 1023 | '''Get the information of deployed hosts |
| 1024 | Returns the hosts content''' |
| 1025 | if self.debug: |
| 1026 | print "osconnector: Getting Host info from VIM" |
| 1027 | try: |
| 1028 | h_list=[] |
| 1029 | self._reload_connection() |
| 1030 | hypervisors = self.nova.hypervisors.list() |
| 1031 | for hype in hypervisors: |
| 1032 | h_list.append( hype.to_dict() ) |
| 1033 | return 1, {"hosts":h_list} |
| 1034 | except nvExceptions.NotFound as e: |
| 1035 | error_value=-vimconn.HTTP_Not_Found |
| 1036 | error_text= (str(e) if len(e.args)==0 else str(e.args[0])) |
| 1037 | except (ksExceptions.ClientException, nvExceptions.ClientException) as e: |
| 1038 | error_value=-vimconn.HTTP_Bad_Request |
| 1039 | error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0])) |
| 1040 | #TODO insert exception vimconn.HTTP_Unauthorized |
| 1041 | #if reaching here is because an exception |
| 1042 | if self.debug: |
| 1043 | print "get_hosts_info " + error_text |
| 1044 | return error_value, error_text |
| 1045 | |
| 1046 | def get_hosts(self, vim_tenant): |
| 1047 | '''Get the hosts and deployed instances |
| 1048 | Returns the hosts content''' |
| 1049 | r, hype_dict = self.get_hosts_info() |
| 1050 | if r<0: |
| 1051 | return r, hype_dict |
| 1052 | hypervisors = hype_dict["hosts"] |
| 1053 | try: |
| 1054 | servers = self.nova.servers.list() |
| 1055 | for hype in hypervisors: |
| 1056 | for server in servers: |
| 1057 | if server.to_dict()['OS-EXT-SRV-ATTR:hypervisor_hostname']==hype['hypervisor_hostname']: |
| 1058 | if 'vm' in hype: |
| 1059 | hype['vm'].append(server.id) |
| 1060 | else: |
| 1061 | hype['vm'] = [server.id] |
| 1062 | return 1, hype_dict |
| 1063 | except nvExceptions.NotFound as e: |
| 1064 | error_value=-vimconn.HTTP_Not_Found |
| 1065 | error_text= (str(e) if len(e.args)==0 else str(e.args[0])) |
| 1066 | except (ksExceptions.ClientException, nvExceptions.ClientException) as e: |
| 1067 | error_value=-vimconn.HTTP_Bad_Request |
| 1068 | error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0])) |
| 1069 | #TODO insert exception vimconn.HTTP_Unauthorized |
| 1070 | #if reaching here is because an exception |
| 1071 | if self.debug: |
| 1072 | print "get_hosts " + error_text |
| 1073 | return error_value, error_text |
| 1074 | |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1075 | |