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