blob: 7ba280b07def04f31670fb82d6238c8d1b767dfa [file] [log] [blame]
tierno7edb6752016-03-21 17:37:52 +01001# -*- 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'''
25osconnector implements all the methods to interact with openstack using the python-client.
26'''
montesmoreno0c8def02016-12-22 12:16:23 +000027__author__="Alfonso Tierno, Gerardo Garcia, Pablo Montes, xFlow Research"
28__date__ ="$22-jun-2014 11:19:29$"
tierno7edb6752016-03-21 17:37:52 +010029
30import vimconn
31import json
32import yaml
tiernoae4a8d12016-07-08 12:30:39 +020033import logging
garciadeblas9f8456e2016-09-05 05:02:59 +020034import netaddr
montesmoreno0c8def02016-12-22 12:16:23 +000035import time
tierno36c0b172017-01-12 18:32:28 +010036import yaml
garciadeblas2299e3b2017-01-26 14:35:55 +000037import random
tierno7edb6752016-03-21 17:37:52 +010038
ahmadsa96af9f42017-01-31 16:17:14 +050039from novaclient import client as nClient_v2, exceptions as nvExceptions
40from novaclient import api_versions
ahmadsa95baa272016-11-30 09:14:11 +050041import keystoneclient.v2_0.client as ksClient_v2
42from novaclient.v2.client import Client as nClient
43import keystoneclient.v3.client as ksClient
tierno7edb6752016-03-21 17:37:52 +010044import keystoneclient.exceptions as ksExceptions
45import glanceclient.v2.client as glClient
46import glanceclient.client as gl1Client
47import glanceclient.exc as gl1Exceptions
montesmoreno0c8def02016-12-22 12:16:23 +000048import cinderclient.v2.client as cClient_v2
tierno7edb6752016-03-21 17:37:52 +010049from httplib import HTTPException
ahmadsa95baa272016-11-30 09:14:11 +050050from neutronclient.neutron import client as neClient_v2
51from neutronclient.v2_0 import client as neClient
tierno7edb6752016-03-21 17:37:52 +010052from neutronclient.common import exceptions as neExceptions
53from requests.exceptions import ConnectionError
54
55'''contain the openstack virtual machine status to openmano status'''
56vmStatus2manoFormat={'ACTIVE':'ACTIVE',
57 'PAUSED':'PAUSED',
58 'SUSPENDED': 'SUSPENDED',
59 'SHUTOFF':'INACTIVE',
60 'BUILD':'BUILD',
61 'ERROR':'ERROR','DELETED':'DELETED'
62 }
63netStatus2manoFormat={'ACTIVE':'ACTIVE','PAUSED':'PAUSED','INACTIVE':'INACTIVE','BUILD':'BUILD','ERROR':'ERROR','DELETED':'DELETED'
64 }
65
montesmoreno0c8def02016-12-22 12:16:23 +000066#global var to have a timeout creating and deleting volumes
67volume_timeout = 60
montesmoreno2a1fc4e2017-01-09 16:46:04 +000068server_timeout = 60
montesmoreno0c8def02016-12-22 12:16:23 +000069
tierno7edb6752016-03-21 17:37:52 +010070class vimconnector(vimconn.vimconnector):
tiernofe789902016-09-29 14:20:44 +000071 def __init__(self, uuid, name, tenant_id, tenant_name, url, url_admin=None, user=None, passwd=None, log_level=None, config={}):
ahmadsa96af9f42017-01-31 16:17:14 +050072 '''using common constructor parameters. In this case
tierno7edb6752016-03-21 17:37:52 +010073 'url' is the keystone authorization url,
74 'url_admin' is not use
75 '''
ahmadsa95baa272016-11-30 09:14:11 +050076 self.osc_api_version = 'v2.0'
77 if config.get('APIversion') == 'v3.3':
78 self.osc_api_version = 'v3.3'
tiernoae4a8d12016-07-08 12:30:39 +020079 vimconn.vimconnector.__init__(self, uuid, name, tenant_id, tenant_name, url, url_admin, user, passwd, log_level, config)
tierno7edb6752016-03-21 17:37:52 +010080
81 self.k_creds={}
82 self.n_creds={}
tiernoc75a5dc2017-01-18 15:53:44 +010083 if self.config.get("insecure"):
84 self.k_creds["insecure"] = True
85 self.n_creds["insecure"] = True
tierno7edb6752016-03-21 17:37:52 +010086 if not url:
87 raise TypeError, 'url param can not be NoneType'
88 self.k_creds['auth_url'] = url
89 self.n_creds['auth_url'] = url
tierno392f2852016-05-13 12:28:55 +020090 if tenant_name:
91 self.k_creds['tenant_name'] = tenant_name
92 self.n_creds['project_id'] = tenant_name
93 if tenant_id:
94 self.k_creds['tenant_id'] = tenant_id
95 self.n_creds['tenant_id'] = tenant_id
tierno7edb6752016-03-21 17:37:52 +010096 if user:
97 self.k_creds['username'] = user
98 self.n_creds['username'] = user
99 if passwd:
100 self.k_creds['password'] = passwd
101 self.n_creds['api_key'] = passwd
ahmadsa95baa272016-11-30 09:14:11 +0500102 if self.osc_api_version == 'v3.3':
103 self.k_creds['project_name'] = tenant_name
104 self.k_creds['project_id'] = tenant_id
montesmorenocf227142017-01-12 12:24:21 +0000105 if config.get('region_name'):
106 self.k_creds['region_name'] = config.get('region_name')
107 self.n_creds['region_name'] = config.get('region_name')
montesmoreno0c8def02016-12-22 12:16:23 +0000108
tierno7edb6752016-03-21 17:37:52 +0100109 self.reload_client = True
tierno73ad9e42016-09-12 18:11:11 +0200110 self.logger = logging.getLogger('openmano.vim.openstack')
tiernofe789902016-09-29 14:20:44 +0000111 if log_level:
112 self.logger.setLevel( getattr(logging, log_level) )
tierno7edb6752016-03-21 17:37:52 +0100113
114 def __setitem__(self,index, value):
115 '''Set individuals parameters
116 Throw TypeError, KeyError
117 '''
tierno392f2852016-05-13 12:28:55 +0200118 if index=='tenant_id':
tierno7edb6752016-03-21 17:37:52 +0100119 self.reload_client=True
tierno392f2852016-05-13 12:28:55 +0200120 self.tenant_id = value
ahmadsa95baa272016-11-30 09:14:11 +0500121 if self.osc_api_version == 'v3.3':
122 if value:
123 self.k_creds['project_id'] = value
124 self.n_creds['project_id'] = value
125 else:
126 del self.k_creds['project_id']
127 del self.n_creds['project_id']
tierno392f2852016-05-13 12:28:55 +0200128 else:
ahmadsa95baa272016-11-30 09:14:11 +0500129 if value:
130 self.k_creds['tenant_id'] = value
131 self.n_creds['tenant_id'] = value
132 else:
133 del self.k_creds['tenant_id']
134 del self.n_creds['tenant_id']
tierno392f2852016-05-13 12:28:55 +0200135 elif index=='tenant_name':
136 self.reload_client=True
137 self.tenant_name = value
ahmadsa95baa272016-11-30 09:14:11 +0500138 if self.osc_api_version == 'v3.3':
139 if value:
140 self.k_creds['project_name'] = value
141 self.n_creds['project_name'] = value
142 else:
143 del self.k_creds['project_name']
144 del self.n_creds['project_name']
tierno7edb6752016-03-21 17:37:52 +0100145 else:
ahmadsa95baa272016-11-30 09:14:11 +0500146 if value:
147 self.k_creds['tenant_name'] = value
148 self.n_creds['project_id'] = value
149 else:
150 del self.k_creds['tenant_name']
151 del self.n_creds['project_id']
tierno7edb6752016-03-21 17:37:52 +0100152 elif index=='user':
153 self.reload_client=True
154 self.user = value
155 if value:
156 self.k_creds['username'] = value
157 self.n_creds['username'] = value
158 else:
159 del self.k_creds['username']
160 del self.n_creds['username']
161 elif index=='passwd':
162 self.reload_client=True
163 self.passwd = value
164 if value:
165 self.k_creds['password'] = value
166 self.n_creds['api_key'] = value
167 else:
168 del self.k_creds['password']
169 del self.n_creds['api_key']
170 elif index=='url':
171 self.reload_client=True
172 self.url = value
173 if value:
174 self.k_creds['auth_url'] = value
175 self.n_creds['auth_url'] = value
176 else:
177 raise TypeError, 'url param can not be NoneType'
178 else:
179 vimconn.vimconnector.__setitem__(self,index, value)
180
181 def _reload_connection(self):
182 '''Called before any operation, it check if credentials has changed
183 Throw keystoneclient.apiclient.exceptions.AuthorizationFailure
184 '''
185 #TODO control the timing and possible token timeout, but it seams that python client does this task for us :-)
186 if self.reload_client:
187 #test valid params
188 if len(self.n_creds) <4:
189 raise ksExceptions.ClientException("Not enough parameters to connect to openstack")
ahmadsa95baa272016-11-30 09:14:11 +0500190 if self.osc_api_version == 'v3.3':
ahmadsa96af9f42017-01-31 16:17:14 +0500191 self.nova = nClient(api_version=api_versions.APIVersion(version_str='2.0'), **self.n_creds)
montesmoreno0c8def02016-12-22 12:16:23 +0000192 #TODO To be updated for v3
193 #self.cinder = cClient.Client(**self.n_creds)
ahmadsa95baa272016-11-30 09:14:11 +0500194 self.keystone = ksClient.Client(**self.k_creds)
195 self.ne_endpoint=self.keystone.service_catalog.url_for(service_type='network', endpoint_type='publicURL')
ahmadsa96af9f42017-01-31 16:17:14 +0500196 self.neutron = neClient.Client(api_version=api_versions.APIVersion(version_str='2.0'), endpoint_url=self.ne_endpoint, token=self.keystone.auth_token, **self.k_creds)
ahmadsa95baa272016-11-30 09:14:11 +0500197 else:
ahmadsa96af9f42017-01-31 16:17:14 +0500198 self.nova = nClient_v2.Client(version='2', **self.n_creds)
montesmoreno0c8def02016-12-22 12:16:23 +0000199 self.cinder = cClient_v2.Client(**self.n_creds)
ahmadsa95baa272016-11-30 09:14:11 +0500200 self.keystone = ksClient_v2.Client(**self.k_creds)
201 self.ne_endpoint=self.keystone.service_catalog.url_for(service_type='network', endpoint_type='publicURL')
202 self.neutron = neClient_v2.Client('2.0', endpoint_url=self.ne_endpoint, token=self.keystone.auth_token, **self.k_creds)
tierno7edb6752016-03-21 17:37:52 +0100203 self.glance_endpoint = self.keystone.service_catalog.url_for(service_type='image', endpoint_type='publicURL')
204 self.glance = glClient.Client(self.glance_endpoint, token=self.keystone.auth_token, **self.k_creds) #TODO check k_creds vs n_creds
tierno7edb6752016-03-21 17:37:52 +0100205 self.reload_client = False
ahmadsa95baa272016-11-30 09:14:11 +0500206
tierno7edb6752016-03-21 17:37:52 +0100207 def __net_os2mano(self, net_list_dict):
208 '''Transform the net openstack format to mano format
209 net_list_dict can be a list of dict or a single dict'''
210 if type(net_list_dict) is dict:
211 net_list_=(net_list_dict,)
212 elif type(net_list_dict) is list:
213 net_list_=net_list_dict
214 else:
215 raise TypeError("param net_list_dict must be a list or a dictionary")
216 for net in net_list_:
217 if net.get('provider:network_type') == "vlan":
218 net['type']='data'
219 else:
220 net['type']='bridge'
tiernoae4a8d12016-07-08 12:30:39 +0200221
222
223
224 def _format_exception(self, exception):
225 '''Transform a keystone, nova, neutron exception into a vimconn exception'''
226 if isinstance(exception, (HTTPException, gl1Exceptions.HTTPException, gl1Exceptions.CommunicationError,
tierno8e995ce2016-09-22 08:13:00 +0000227 ConnectionError, ksExceptions.ConnectionError, neExceptions.ConnectionFailed
228 )):
tiernoae4a8d12016-07-08 12:30:39 +0200229 raise vimconn.vimconnConnectionException(type(exception).__name__ + ": " + str(exception))
230 elif isinstance(exception, (nvExceptions.ClientException, ksExceptions.ClientException,
231 neExceptions.NeutronException, nvExceptions.BadRequest)):
232 raise vimconn.vimconnUnexpectedResponse(type(exception).__name__ + ": " + str(exception))
233 elif isinstance(exception, (neExceptions.NetworkNotFoundClient, nvExceptions.NotFound)):
234 raise vimconn.vimconnNotFoundException(type(exception).__name__ + ": " + str(exception))
235 elif isinstance(exception, nvExceptions.Conflict):
236 raise vimconn.vimconnConflictException(type(exception).__name__ + ": " + str(exception))
237 else: # ()
238 raise vimconn.vimconnConnectionException(type(exception).__name__ + ": " + str(exception))
239
240 def get_tenant_list(self, filter_dict={}):
241 '''Obtain tenants of VIM
242 filter_dict can contain the following keys:
243 name: filter by tenant name
244 id: filter by tenant uuid/id
245 <other VIM specific>
246 Returns the tenant list of dictionaries: [{'name':'<name>, 'id':'<id>, ...}, ...]
247 '''
ahmadsa95baa272016-11-30 09:14:11 +0500248 self.logger.debug("Getting tenants from VIM filter: '%s'", str(filter_dict))
tiernoae4a8d12016-07-08 12:30:39 +0200249 try:
250 self._reload_connection()
montesmoreno0c8def02016-12-22 12:16:23 +0000251 if self.osc_api_version == 'v3.3':
ahmadsa95baa272016-11-30 09:14:11 +0500252 project_class_list=self.keystone.projects.findall(**filter_dict)
253 else:
254 project_class_list=self.keystone.tenants.findall(**filter_dict)
255 project_list=[]
256 for project in project_class_list:
257 project_list.append(project.to_dict())
258 return project_list
tierno8e995ce2016-09-22 08:13:00 +0000259 except (ksExceptions.ConnectionError, ksExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200260 self._format_exception(e)
261
262 def new_tenant(self, tenant_name, tenant_description):
263 '''Adds a new tenant to openstack VIM. Returns the tenant identifier'''
264 self.logger.debug("Adding a new tenant name: %s", tenant_name)
265 try:
266 self._reload_connection()
ahmadsa95baa272016-11-30 09:14:11 +0500267 if self.osc_api_version == 'v3.3':
268 project=self.keystone.projects.create(tenant_name, tenant_description)
269 else:
270 project=self.keystone.tenants.create(tenant_name, tenant_description)
271 return project.id
tierno8e995ce2016-09-22 08:13:00 +0000272 except (ksExceptions.ConnectionError, ksExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200273 self._format_exception(e)
274
275 def delete_tenant(self, tenant_id):
276 '''Delete a tenant from openstack VIM. Returns the old tenant identifier'''
277 self.logger.debug("Deleting tenant %s from VIM", tenant_id)
278 try:
279 self._reload_connection()
montesmoreno0c8def02016-12-22 12:16:23 +0000280 if self.osc_api_version == 'v3.3':
ahmadsa95baa272016-11-30 09:14:11 +0500281 self.keystone.projects.delete(tenant_id)
282 else:
283 self.keystone.tenants.delete(tenant_id)
tiernoae4a8d12016-07-08 12:30:39 +0200284 return tenant_id
tierno8e995ce2016-09-22 08:13:00 +0000285 except (ksExceptions.ConnectionError, ksExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200286 self._format_exception(e)
ahmadsa95baa272016-11-30 09:14:11 +0500287
garciadeblas9f8456e2016-09-05 05:02:59 +0200288 def new_network(self,net_name, net_type, ip_profile=None, shared=False, vlan=None):
tiernoae4a8d12016-07-08 12:30:39 +0200289 '''Adds a tenant network to VIM. Returns the network identifier'''
290 self.logger.debug("Adding a new network to VIM name '%s', type '%s'", net_name, net_type)
garciadeblasedca7b32016-09-29 14:01:52 +0000291 #self.logger.debug(">>>>>>>>>>>>>>>>>> IP profile %s", str(ip_profile))
tierno7edb6752016-03-21 17:37:52 +0100292 try:
garciadeblasedca7b32016-09-29 14:01:52 +0000293 new_net = None
tierno7edb6752016-03-21 17:37:52 +0100294 self._reload_connection()
295 network_dict = {'name': net_name, 'admin_state_up': True}
296 if net_type=="data" or net_type=="ptp":
297 if self.config.get('dataplane_physical_net') == None:
tiernoae4a8d12016-07-08 12:30:39 +0200298 raise vimconn.vimconnConflictException("You must provide a 'dataplane_physical_net' at config value before creating sriov network")
tierno7edb6752016-03-21 17:37:52 +0100299 network_dict["provider:physical_network"] = self.config['dataplane_physical_net'] #"physnet_sriov" #TODO physical
300 network_dict["provider:network_type"] = "vlan"
301 if vlan!=None:
302 network_dict["provider:network_type"] = vlan
tiernoae4a8d12016-07-08 12:30:39 +0200303 network_dict["shared"]=shared
tierno7edb6752016-03-21 17:37:52 +0100304 new_net=self.neutron.create_network({'network':network_dict})
305 #print new_net
garciadeblas9f8456e2016-09-05 05:02:59 +0200306 #create subnetwork, even if there is no profile
307 if not ip_profile:
308 ip_profile = {}
309 if 'subnet_address' not in ip_profile:
garciadeblas2299e3b2017-01-26 14:35:55 +0000310 #Fake subnet is required
311 subnet_rand = random.randint(0, 255)
312 ip_profile['subnet_address'] = "192.168.{}.0/24".format(subnet_rand)
garciadeblas9f8456e2016-09-05 05:02:59 +0200313 if 'ip_version' not in ip_profile:
314 ip_profile['ip_version'] = "IPv4"
tierno7edb6752016-03-21 17:37:52 +0100315 subnet={"name":net_name+"-subnet",
316 "network_id": new_net["network"]["id"],
garciadeblas9f8456e2016-09-05 05:02:59 +0200317 "ip_version": 4 if ip_profile['ip_version']=="IPv4" else 6,
318 "cidr": ip_profile['subnet_address']
tierno7edb6752016-03-21 17:37:52 +0100319 }
garciadeblas9f8456e2016-09-05 05:02:59 +0200320 if 'gateway_address' in ip_profile:
321 subnet['gateway_ip'] = ip_profile['gateway_address']
garciadeblasedca7b32016-09-29 14:01:52 +0000322 if ip_profile.get('dns_address'):
garciadeblas9f8456e2016-09-05 05:02:59 +0200323 #TODO: manage dns_address as a list of addresses separated by commas
324 subnet['dns_nameservers'] = []
325 subnet['dns_nameservers'].append(ip_profile['dns_address'])
326 if 'dhcp_enabled' in ip_profile:
327 subnet['enable_dhcp'] = False if ip_profile['dhcp_enabled']=="false" else True
328 if 'dhcp_start_address' in ip_profile:
329 subnet['allocation_pools']=[]
330 subnet['allocation_pools'].append(dict())
331 subnet['allocation_pools'][0]['start'] = ip_profile['dhcp_start_address']
332 if 'dhcp_count' in ip_profile:
333 #parts = ip_profile['dhcp_start_address'].split('.')
334 #ip_int = (int(parts[0]) << 24) + (int(parts[1]) << 16) + (int(parts[2]) << 8) + int(parts[3])
335 ip_int = int(netaddr.IPAddress(ip_profile['dhcp_start_address']))
garciadeblas21d795b2016-09-29 17:31:46 +0200336 ip_int += ip_profile['dhcp_count'] - 1
garciadeblas9f8456e2016-09-05 05:02:59 +0200337 ip_str = str(netaddr.IPAddress(ip_int))
338 subnet['allocation_pools'][0]['end'] = ip_str
garciadeblasedca7b32016-09-29 14:01:52 +0000339 #self.logger.debug(">>>>>>>>>>>>>>>>>> Subnet: %s", str(subnet))
tierno7edb6752016-03-21 17:37:52 +0100340 self.neutron.create_subnet({"subnet": subnet} )
tiernoae4a8d12016-07-08 12:30:39 +0200341 return new_net["network"]["id"]
tierno8e995ce2016-09-22 08:13:00 +0000342 except (neExceptions.ConnectionFailed, ksExceptions.ClientException, neExceptions.NeutronException, ConnectionError) as e:
garciadeblasedca7b32016-09-29 14:01:52 +0000343 if new_net:
344 self.neutron.delete_network(new_net['network']['id'])
tiernoae4a8d12016-07-08 12:30:39 +0200345 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100346
347 def get_network_list(self, filter_dict={}):
348 '''Obtain tenant networks of VIM
349 Filter_dict can be:
350 name: network name
351 id: network uuid
352 shared: boolean
353 tenant_id: tenant
354 admin_state_up: boolean
355 status: 'ACTIVE'
356 Returns the network list of dictionaries
357 '''
tiernoae4a8d12016-07-08 12:30:39 +0200358 self.logger.debug("Getting network from VIM filter: '%s'", str(filter_dict))
tierno7edb6752016-03-21 17:37:52 +0100359 try:
360 self._reload_connection()
montesmoreno0c8def02016-12-22 12:16:23 +0000361 if self.osc_api_version == 'v3.3' and "tenant_id" in filter_dict:
ahmadsa95baa272016-11-30 09:14:11 +0500362 filter_dict['project_id'] = filter_dict.pop('tenant_id')
tierno7edb6752016-03-21 17:37:52 +0100363 net_dict=self.neutron.list_networks(**filter_dict)
364 net_list=net_dict["networks"]
365 self.__net_os2mano(net_list)
tiernoae4a8d12016-07-08 12:30:39 +0200366 return net_list
tierno8e995ce2016-09-22 08:13:00 +0000367 except (neExceptions.ConnectionFailed, ksExceptions.ClientException, neExceptions.NeutronException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200368 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100369
tiernoae4a8d12016-07-08 12:30:39 +0200370 def get_network(self, net_id):
371 '''Obtain details of network from VIM
372 Returns the network information from a network id'''
373 self.logger.debug(" Getting tenant network %s from VIM", net_id)
tierno7edb6752016-03-21 17:37:52 +0100374 filter_dict={"id": net_id}
tiernoae4a8d12016-07-08 12:30:39 +0200375 net_list = self.get_network_list(filter_dict)
tierno7edb6752016-03-21 17:37:52 +0100376 if len(net_list)==0:
tiernoae4a8d12016-07-08 12:30:39 +0200377 raise vimconn.vimconnNotFoundException("Network '{}' not found".format(net_id))
tierno7edb6752016-03-21 17:37:52 +0100378 elif len(net_list)>1:
tiernoae4a8d12016-07-08 12:30:39 +0200379 raise vimconn.vimconnConflictException("Found more than one network with this criteria")
tierno7edb6752016-03-21 17:37:52 +0100380 net = net_list[0]
381 subnets=[]
382 for subnet_id in net.get("subnets", () ):
383 try:
384 subnet = self.neutron.show_subnet(subnet_id)
385 except Exception as e:
tiernoae4a8d12016-07-08 12:30:39 +0200386 self.logger.error("osconnector.get_network(): Error getting subnet %s %s" % (net_id, str(e)))
387 subnet = {"id": subnet_id, "fault": str(e)}
tierno7edb6752016-03-21 17:37:52 +0100388 subnets.append(subnet)
389 net["subnets"] = subnets
tiernoae4a8d12016-07-08 12:30:39 +0200390 return net
tierno7edb6752016-03-21 17:37:52 +0100391
tiernoae4a8d12016-07-08 12:30:39 +0200392 def delete_network(self, net_id):
393 '''Deletes a tenant network from VIM. Returns the old network identifier'''
394 self.logger.debug("Deleting network '%s' from VIM", net_id)
tierno7edb6752016-03-21 17:37:52 +0100395 try:
396 self._reload_connection()
397 #delete VM ports attached to this networks before the network
398 ports = self.neutron.list_ports(network_id=net_id)
399 for p in ports['ports']:
400 try:
401 self.neutron.delete_port(p["id"])
402 except Exception as e:
tiernoae4a8d12016-07-08 12:30:39 +0200403 self.logger.error("Error deleting port %s: %s", p["id"], str(e))
tierno7edb6752016-03-21 17:37:52 +0100404 self.neutron.delete_network(net_id)
tiernoae4a8d12016-07-08 12:30:39 +0200405 return net_id
406 except (neExceptions.ConnectionFailed, neExceptions.NetworkNotFoundClient, neExceptions.NeutronException,
tierno8e995ce2016-09-22 08:13:00 +0000407 ksExceptions.ClientException, neExceptions.NeutronException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200408 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100409
tiernoae4a8d12016-07-08 12:30:39 +0200410 def refresh_nets_status(self, net_list):
411 '''Get the status of the networks
412 Params: the list of network identifiers
413 Returns a dictionary with:
414 net_id: #VIM id of this network
415 status: #Mandatory. Text with one of:
416 # DELETED (not found at vim)
417 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
418 # OTHER (Vim reported other status not understood)
419 # ERROR (VIM indicates an ERROR status)
420 # ACTIVE, INACTIVE, DOWN (admin down),
421 # BUILD (on building process)
422 #
423 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
424 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
425
426 '''
427 net_dict={}
428 for net_id in net_list:
429 net = {}
430 try:
431 net_vim = self.get_network(net_id)
432 if net_vim['status'] in netStatus2manoFormat:
433 net["status"] = netStatus2manoFormat[ net_vim['status'] ]
434 else:
435 net["status"] = "OTHER"
436 net["error_msg"] = "VIM status reported " + net_vim['status']
437
tierno8e995ce2016-09-22 08:13:00 +0000438 if net['status'] == "ACTIVE" and not net_vim['admin_state_up']:
tiernoae4a8d12016-07-08 12:30:39 +0200439 net['status'] = 'DOWN'
tierno8e995ce2016-09-22 08:13:00 +0000440 try:
441 net['vim_info'] = yaml.safe_dump(net_vim, default_flow_style=True, width=256)
442 except yaml.representer.RepresenterError:
443 net['vim_info'] = str(net_vim)
tiernoae4a8d12016-07-08 12:30:39 +0200444 if net_vim.get('fault'): #TODO
445 net['error_msg'] = str(net_vim['fault'])
446 except vimconn.vimconnNotFoundException as e:
447 self.logger.error("Exception getting net status: %s", str(e))
448 net['status'] = "DELETED"
449 net['error_msg'] = str(e)
450 except vimconn.vimconnException as e:
451 self.logger.error("Exception getting net status: %s", str(e))
452 net['status'] = "VIM_ERROR"
453 net['error_msg'] = str(e)
454 net_dict[net_id] = net
455 return net_dict
456
457 def get_flavor(self, flavor_id):
458 '''Obtain flavor details from the VIM. Returns the flavor dict details'''
459 self.logger.debug("Getting flavor '%s'", flavor_id)
tierno7edb6752016-03-21 17:37:52 +0100460 try:
461 self._reload_connection()
462 flavor = self.nova.flavors.find(id=flavor_id)
463 #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
tiernoae4a8d12016-07-08 12:30:39 +0200464 return flavor.to_dict()
tierno8e995ce2016-09-22 08:13:00 +0000465 except (nvExceptions.NotFound, nvExceptions.ClientException, ksExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200466 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100467
tiernocf157a82017-01-30 14:07:06 +0100468 def get_flavor_id_from_data(self, flavor_dict):
469 """Obtain flavor id that match the flavor description
470 Returns the flavor_id or raises a vimconnNotFoundException
471 """
472 try:
473 self._reload_connection()
474 numa=None
475 numas = flavor_dict.get("extended",{}).get("numas")
476 if numas:
477 #TODO
478 raise vimconn.vimconnNotFoundException("Flavor with EPA still not implemted")
479 # if len(numas) > 1:
480 # raise vimconn.vimconnNotFoundException("Cannot find any flavor with more than one numa")
481 # numa=numas[0]
482 # numas = extended.get("numas")
483 for flavor in self.nova.flavors.list():
484 epa = flavor.get_keys()
485 if epa:
486 continue
487 #TODO
488 if flavor.ram != flavor_dict["ram"]:
489 continue
490 if flavor.vcpus != flavor_dict["vcpus"]:
491 continue
492 if flavor.disk != flavor_dict["disk"]:
493 continue
494 return flavor.id
495 raise vimconn.vimconnNotFoundException("Cannot find any flavor matching '{}'".format(str(flavor_dict)))
496 except (nvExceptions.NotFound, nvExceptions.ClientException, ksExceptions.ClientException, ConnectionError) as e:
497 self._format_exception(e)
498
499
tiernoae4a8d12016-07-08 12:30:39 +0200500 def new_flavor(self, flavor_data, change_name_if_used=True):
tierno7edb6752016-03-21 17:37:52 +0100501 '''Adds a tenant flavor to openstack VIM
tiernoae4a8d12016-07-08 12:30:39 +0200502 if change_name_if_used is True, it will change name in case of conflict, because it is not supported name repetition
tierno7edb6752016-03-21 17:37:52 +0100503 Returns the flavor identifier
504 '''
tiernoae4a8d12016-07-08 12:30:39 +0200505 self.logger.debug("Adding flavor '%s'", str(flavor_data))
tierno7edb6752016-03-21 17:37:52 +0100506 retry=0
tiernoae4a8d12016-07-08 12:30:39 +0200507 max_retries=3
tierno7edb6752016-03-21 17:37:52 +0100508 name_suffix = 0
tiernoae4a8d12016-07-08 12:30:39 +0200509 name=flavor_data['name']
510 while retry<max_retries:
tierno7edb6752016-03-21 17:37:52 +0100511 retry+=1
512 try:
513 self._reload_connection()
514 if change_name_if_used:
515 #get used names
516 fl_names=[]
517 fl=self.nova.flavors.list()
518 for f in fl:
519 fl_names.append(f.name)
520 while name in fl_names:
521 name_suffix += 1
tiernoae4a8d12016-07-08 12:30:39 +0200522 name = flavor_data['name']+"-" + str(name_suffix)
tierno7edb6752016-03-21 17:37:52 +0100523
tiernoae4a8d12016-07-08 12:30:39 +0200524 ram = flavor_data.get('ram',64)
525 vcpus = flavor_data.get('vcpus',1)
tierno7edb6752016-03-21 17:37:52 +0100526 numa_properties=None
527
tiernoae4a8d12016-07-08 12:30:39 +0200528 extended = flavor_data.get("extended")
tierno7edb6752016-03-21 17:37:52 +0100529 if extended:
530 numas=extended.get("numas")
531 if numas:
532 numa_nodes = len(numas)
533 if numa_nodes > 1:
534 return -1, "Can not add flavor with more than one numa"
535 numa_properties = {"hw:numa_nodes":str(numa_nodes)}
536 numa_properties["hw:mem_page_size"] = "large"
537 numa_properties["hw:cpu_policy"] = "dedicated"
538 numa_properties["hw:numa_mempolicy"] = "strict"
539 for numa in numas:
540 #overwrite ram and vcpus
541 ram = numa['memory']*1024
542 if 'paired-threads' in numa:
543 vcpus = numa['paired-threads']*2
544 numa_properties["hw:cpu_threads_policy"] = "prefer"
545 elif 'cores' in numa:
546 vcpus = numa['cores']
547 #numa_properties["hw:cpu_threads_policy"] = "prefer"
548 elif 'threads' in numa:
549 vcpus = numa['threads']
550 numa_properties["hw:cpu_policy"] = "isolated"
551 for interface in numa.get("interfaces",() ):
552 if interface["dedicated"]=="yes":
tierno809a7802016-07-08 13:31:24 +0200553 raise vimconn.vimconnException("Passthrough interfaces are not supported for the openstack connector", http_code=vimconn.HTTP_Service_Unavailable)
tierno7edb6752016-03-21 17:37:52 +0100554 #TODO, add the key 'pci_passthrough:alias"="<label at config>:<number ifaces>"' when a way to connect it is available
555
556 #create flavor
557 new_flavor=self.nova.flavors.create(name,
558 ram,
559 vcpus,
tiernoae4a8d12016-07-08 12:30:39 +0200560 flavor_data.get('disk',1),
561 is_public=flavor_data.get('is_public', True)
tierno7edb6752016-03-21 17:37:52 +0100562 )
563 #add metadata
564 if numa_properties:
565 new_flavor.set_keys(numa_properties)
tiernoae4a8d12016-07-08 12:30:39 +0200566 return new_flavor.id
tierno7edb6752016-03-21 17:37:52 +0100567 except nvExceptions.Conflict as e:
tiernoae4a8d12016-07-08 12:30:39 +0200568 if change_name_if_used and retry < max_retries:
tierno7edb6752016-03-21 17:37:52 +0100569 continue
tiernoae4a8d12016-07-08 12:30:39 +0200570 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100571 #except nvExceptions.BadRequest as e:
572 except (ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200573 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100574
tiernoae4a8d12016-07-08 12:30:39 +0200575 def delete_flavor(self,flavor_id):
576 '''Deletes a tenant flavor from openstack VIM. Returns the old flavor_id
tierno7edb6752016-03-21 17:37:52 +0100577 '''
tiernoae4a8d12016-07-08 12:30:39 +0200578 try:
579 self._reload_connection()
580 self.nova.flavors.delete(flavor_id)
581 return flavor_id
582 #except nvExceptions.BadRequest as e:
tierno8e995ce2016-09-22 08:13:00 +0000583 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200584 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100585
tiernoae4a8d12016-07-08 12:30:39 +0200586 def new_image(self,image_dict):
tierno7edb6752016-03-21 17:37:52 +0100587 '''
tiernoae4a8d12016-07-08 12:30:39 +0200588 Adds a tenant image to VIM. imge_dict is a dictionary with:
589 name: name
590 disk_format: qcow2, vhd, vmdk, raw (by default), ...
591 location: path or URI
592 public: "yes" or "no"
593 metadata: metadata of the image
594 Returns the image_id
tierno7edb6752016-03-21 17:37:52 +0100595 '''
tierno7edb6752016-03-21 17:37:52 +0100596 #using version 1 of glance client
597 glancev1 = gl1Client.Client('1',self.glance_endpoint, token=self.keystone.auth_token, **self.k_creds) #TODO check k_creds vs n_creds
tiernoae4a8d12016-07-08 12:30:39 +0200598 retry=0
599 max_retries=3
600 while retry<max_retries:
tierno7edb6752016-03-21 17:37:52 +0100601 retry+=1
602 try:
603 self._reload_connection()
604 #determine format http://docs.openstack.org/developer/glance/formats.html
605 if "disk_format" in image_dict:
606 disk_format=image_dict["disk_format"]
garciadeblas14480452017-01-10 13:08:07 +0100607 else: #autodiscover based on extension
tierno7edb6752016-03-21 17:37:52 +0100608 if image_dict['location'][-6:]==".qcow2":
609 disk_format="qcow2"
610 elif image_dict['location'][-4:]==".vhd":
611 disk_format="vhd"
612 elif image_dict['location'][-5:]==".vmdk":
613 disk_format="vmdk"
614 elif image_dict['location'][-4:]==".vdi":
615 disk_format="vdi"
616 elif image_dict['location'][-4:]==".iso":
617 disk_format="iso"
618 elif image_dict['location'][-4:]==".aki":
619 disk_format="aki"
620 elif image_dict['location'][-4:]==".ari":
621 disk_format="ari"
622 elif image_dict['location'][-4:]==".ami":
623 disk_format="ami"
624 else:
625 disk_format="raw"
tiernoae4a8d12016-07-08 12:30:39 +0200626 self.logger.debug("new_image: '%s' loading from '%s'", image_dict['name'], image_dict['location'])
tierno7edb6752016-03-21 17:37:52 +0100627 if image_dict['location'][0:4]=="http":
628 new_image = glancev1.images.create(name=image_dict['name'], is_public=image_dict.get('public',"yes")=="yes",
629 container_format="bare", location=image_dict['location'], disk_format=disk_format)
630 else: #local path
631 with open(image_dict['location']) as fimage:
632 new_image = glancev1.images.create(name=image_dict['name'], is_public=image_dict.get('public',"yes")=="yes",
633 container_format="bare", data=fimage, disk_format=disk_format)
634 #insert metadata. We cannot use 'new_image.properties.setdefault'
635 #because nova and glance are "INDEPENDENT" and we are using nova for reading metadata
636 new_image_nova=self.nova.images.find(id=new_image.id)
637 new_image_nova.metadata.setdefault('location',image_dict['location'])
638 metadata_to_load = image_dict.get('metadata')
639 if metadata_to_load:
640 for k,v in yaml.load(metadata_to_load).iteritems():
641 new_image_nova.metadata.setdefault(k,v)
tiernoae4a8d12016-07-08 12:30:39 +0200642 return new_image.id
643 except (nvExceptions.Conflict, ksExceptions.ClientException, nvExceptions.ClientException) as e:
644 self._format_exception(e)
tierno8e995ce2016-09-22 08:13:00 +0000645 except (HTTPException, gl1Exceptions.HTTPException, gl1Exceptions.CommunicationError, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200646 if retry==max_retries:
647 continue
648 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100649 except IOError as e: #can not open the file
tiernoae4a8d12016-07-08 12:30:39 +0200650 raise vimconn.vimconnConnectionException(type(e).__name__ + ": " + str(e)+ " for " + image_dict['location'],
651 http_code=vimconn.HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100652
tiernoae4a8d12016-07-08 12:30:39 +0200653 def delete_image(self, image_id):
654 '''Deletes a tenant image from openstack VIM. Returns the old id
tierno7edb6752016-03-21 17:37:52 +0100655 '''
tiernoae4a8d12016-07-08 12:30:39 +0200656 try:
657 self._reload_connection()
658 self.nova.images.delete(image_id)
659 return image_id
tierno8e995ce2016-09-22 08:13:00 +0000660 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, ConnectionError) as e: #TODO remove
tiernoae4a8d12016-07-08 12:30:39 +0200661 self._format_exception(e)
662
663 def get_image_id_from_path(self, path):
garciadeblasb69fa9f2016-09-28 12:04:10 +0200664 '''Get the image id from image path in the VIM database. Returns the image_id'''
tiernoae4a8d12016-07-08 12:30:39 +0200665 try:
666 self._reload_connection()
667 images = self.nova.images.list()
668 for image in images:
669 if image.metadata.get("location")==path:
670 return image.id
671 raise vimconn.vimconnNotFoundException("image with location '{}' not found".format( path))
tierno8e995ce2016-09-22 08:13:00 +0000672 except (ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200673 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +0100674
garciadeblasb69fa9f2016-09-28 12:04:10 +0200675 def get_image_list(self, filter_dict={}):
676 '''Obtain tenant images from VIM
677 Filter_dict can be:
678 id: image id
679 name: image name
680 checksum: image checksum
681 Returns the image list of dictionaries:
682 [{<the fields at Filter_dict plus some VIM specific>}, ...]
683 List can be empty
684 '''
685 self.logger.debug("Getting image list from VIM filter: '%s'", str(filter_dict))
686 try:
687 self._reload_connection()
688 filter_dict_os=filter_dict.copy()
689 #First we filter by the available filter fields: name, id. The others are removed.
690 filter_dict_os.pop('checksum',None)
691 image_list=self.nova.images.findall(**filter_dict_os)
692 if len(image_list)==0:
693 return []
694 #Then we filter by the rest of filter fields: checksum
695 filtered_list = []
696 for image in image_list:
tierno4540ea52017-01-18 17:44:32 +0100697 image_class=self.glance.images.get(image.id)
698 if 'checksum' not in filter_dict or image_class['checksum']==filter_dict.get('checksum'):
699 filtered_list.append(image_class.copy())
garciadeblasb69fa9f2016-09-28 12:04:10 +0200700 return filtered_list
701 except (ksExceptions.ClientException, nvExceptions.ClientException, gl1Exceptions.CommunicationError, ConnectionError) as e:
702 self._format_exception(e)
703
montesmoreno0c8def02016-12-22 12:16:23 +0000704 def new_vminstance(self,name,description,start,image_id,flavor_id,net_list,cloud_config=None,disk_list=None):
tierno7edb6752016-03-21 17:37:52 +0100705 '''Adds a VM instance to VIM
706 Params:
707 start: indicates if VM must start or boot in pause mode. Ignored
708 image_id,flavor_id: iamge and flavor uuid
709 net_list: list of interfaces, each one is a dictionary with:
710 name:
711 net_id: network uuid to connect
712 vpci: virtual vcpi to assign, ignored because openstack lack #TODO
713 model: interface model, ignored #TODO
714 mac_address: used for SR-IOV ifaces #TODO for other types
715 use: 'data', 'bridge', 'mgmt'
716 type: 'virtual', 'PF', 'VF', 'VFnotShared'
717 vim_id: filled/added by this function
ahmadsaf853d452016-12-22 11:33:47 +0500718 floating_ip: True/False (or it can be None)
tierno7edb6752016-03-21 17:37:52 +0100719 #TODO ip, security groups
tiernoae4a8d12016-07-08 12:30:39 +0200720 Returns the instance identifier
tierno7edb6752016-03-21 17:37:52 +0100721 '''
tiernofa51c202017-01-27 14:58:17 +0100722 self.logger.debug("new_vminstance input: image='%s' flavor='%s' nics='%s'",image_id, flavor_id,str(net_list))
tierno7edb6752016-03-21 17:37:52 +0100723 try:
tierno6e116232016-07-18 13:01:40 +0200724 metadata={}
tierno7edb6752016-03-21 17:37:52 +0100725 net_list_vim=[]
ahmadsaf853d452016-12-22 11:33:47 +0500726 external_network=[] #list of external networks to be connected to instance, later on used to create floating_ip
tierno7edb6752016-03-21 17:37:52 +0100727 self._reload_connection()
tiernoae4a8d12016-07-08 12:30:39 +0200728 metadata_vpci={} #For a specific neutron plugin
tierno7edb6752016-03-21 17:37:52 +0100729 for net in net_list:
730 if not net.get("net_id"): #skip non connected iface
731 continue
ahmadsaf853d452016-12-22 11:33:47 +0500732 if net["type"]=="virtual" or net["type"]=="VF":
tierno7edb6752016-03-21 17:37:52 +0100733 port_dict={
ahmadsaf853d452016-12-22 11:33:47 +0500734 "network_id": net["net_id"],
735 "name": net.get("name"),
736 "admin_state_up": True
737 }
738 if net["type"]=="virtual":
739 if "vpci" in net:
740 metadata_vpci[ net["net_id"] ] = [[ net["vpci"], "" ]]
741 else: # for VF
742 if "vpci" in net:
743 if "VF" not in metadata_vpci:
744 metadata_vpci["VF"]=[]
745 metadata_vpci["VF"].append([ net["vpci"], "" ])
746 port_dict["binding:vnic_type"]="direct"
tierno7edb6752016-03-21 17:37:52 +0100747 if not port_dict["name"]:
ahmadsaf853d452016-12-22 11:33:47 +0500748 port_dict["name"]=name
tierno7edb6752016-03-21 17:37:52 +0100749 if net.get("mac_address"):
750 port_dict["mac_address"]=net["mac_address"]
montesmorenocf227142017-01-12 12:24:21 +0000751 if net.get("port_security") == False:
montesmoreno2a1fc4e2017-01-09 16:46:04 +0000752 port_dict["port_security_enabled"]=net["port_security"]
tierno7edb6752016-03-21 17:37:52 +0100753 new_port = self.neutron.create_port({"port": port_dict })
754 net["mac_adress"] = new_port["port"]["mac_address"]
755 net["vim_id"] = new_port["port"]["id"]
ahmadsaf853d452016-12-22 11:33:47 +0500756 net["ip"] = new_port["port"].get("fixed_ips", [{}])[0].get("ip_address")
tierno7edb6752016-03-21 17:37:52 +0100757 net_list_vim.append({"port-id": new_port["port"]["id"]})
ahmadsaf853d452016-12-22 11:33:47 +0500758 else: # for PF
759 self.logger.warn("new_vminstance: Warning, can not connect a passthrough interface ")
760 #TODO insert this when openstack consider passthrough ports as openstack neutron ports
761 if net.get('floating_ip', False):
tiernof8383b82017-01-18 15:49:48 +0100762 net['exit_on_floating_ip_error'] = True
ahmadsaf853d452016-12-22 11:33:47 +0500763 external_network.append(net)
tiernof8383b82017-01-18 15:49:48 +0100764 elif net['use'] == 'mgmt' and self.config.get('use_floating_ip'):
765 net['exit_on_floating_ip_error'] = False
766 external_network.append(net)
767
tierno7edb6752016-03-21 17:37:52 +0100768 if metadata_vpci:
769 metadata = {"pci_assignement": json.dumps(metadata_vpci)}
tiernoafbced42016-07-23 01:43:53 +0200770 if len(metadata["pci_assignement"]) >255:
tierno6e116232016-07-18 13:01:40 +0200771 #limit the metadata size
772 #metadata["pci_assignement"] = metadata["pci_assignement"][0:255]
773 self.logger.warn("Metadata deleted since it exceeds the expected length (255) ")
774 metadata = {}
tierno7edb6752016-03-21 17:37:52 +0100775
tiernoae4a8d12016-07-08 12:30:39 +0200776 self.logger.debug("name '%s' image_id '%s'flavor_id '%s' net_list_vim '%s' description '%s' metadata %s",
777 name, image_id, flavor_id, str(net_list_vim), description, str(metadata))
tierno7edb6752016-03-21 17:37:52 +0100778
779 security_groups = self.config.get('security_groups')
780 if type(security_groups) is str:
781 security_groups = ( security_groups, )
tierno36c0b172017-01-12 18:32:28 +0100782 #cloud config
783 userdata=None
784 config_drive = None
tiernoa4e1a6e2016-08-31 14:19:40 +0200785 if isinstance(cloud_config, dict):
tierno36c0b172017-01-12 18:32:28 +0100786 if cloud_config.get("user-data"):
787 userdata=cloud_config["user-data"]
788 if cloud_config.get("boot-data-drive") != None:
789 config_drive = cloud_config["boot-data-drive"]
790 if cloud_config.get("config-files") or cloud_config.get("users") or cloud_config.get("key-pairs"):
791 if userdata:
792 raise vimconn.vimconnConflictException("Cloud-config cannot contain both 'userdata' and 'config-files'/'users'/'key-pairs'")
793 userdata_dict={}
794 #default user
795 if cloud_config.get("key-pairs"):
796 userdata_dict["ssh-authorized-keys"] = cloud_config["key-pairs"]
797 userdata_dict["users"] = [{"default": None, "ssh-authorized-keys": cloud_config["key-pairs"] }]
798 if cloud_config.get("users"):
tierno01d0bf52017-01-25 14:27:20 +0100799 if "users" not in userdata_dict:
tierno36c0b172017-01-12 18:32:28 +0100800 userdata_dict["users"] = [ "default" ]
801 for user in cloud_config["users"]:
802 user_info = {
803 "name" : user["name"],
804 "sudo": "ALL = (ALL)NOPASSWD:ALL"
805 }
806 if "user-info" in user:
807 user_info["gecos"] = user["user-info"]
808 if user.get("key-pairs"):
809 user_info["ssh-authorized-keys"] = user["key-pairs"]
810 userdata_dict["users"].append(user_info)
811
812 if cloud_config.get("config-files"):
813 userdata_dict["write_files"] = []
814 for file in cloud_config["config-files"]:
815 file_info = {
816 "path" : file["dest"],
817 "content": file["content"]
818 }
819 if file.get("encoding"):
820 file_info["encoding"] = file["encoding"]
821 if file.get("permissions"):
822 file_info["permissions"] = file["permissions"]
823 if file.get("owner"):
824 file_info["owner"] = file["owner"]
825 userdata_dict["write_files"].append(file_info)
826 userdata = "#cloud-config\n"
827 userdata += yaml.safe_dump(userdata_dict, indent=4, default_flow_style=False)
tiernoa4e1a6e2016-08-31 14:19:40 +0200828 self.logger.debug("userdata: %s", userdata)
829 elif isinstance(cloud_config, str):
830 userdata = cloud_config
montesmoreno0c8def02016-12-22 12:16:23 +0000831
832 #Create additional volumes in case these are present in disk_list
833 block_device_mapping = None
834 base_disk_index = ord('b')
835 if disk_list != None:
836 block_device_mapping = dict()
837 for disk in disk_list:
838 if 'image_id' in disk:
839 volume = self.cinder.volumes.create(size = disk['size'],name = name + '_vd' +
840 chr(base_disk_index), imageRef = disk['image_id'])
841 else:
842 volume = self.cinder.volumes.create(size=disk['size'], name=name + '_vd' +
843 chr(base_disk_index))
844 block_device_mapping['_vd' + chr(base_disk_index)] = volume.id
845 base_disk_index += 1
846
847 #wait until volumes are with status available
848 keep_waiting = True
849 elapsed_time = 0
850 while keep_waiting and elapsed_time < volume_timeout:
851 keep_waiting = False
852 for volume_id in block_device_mapping.itervalues():
853 if self.cinder.volumes.get(volume_id).status != 'available':
854 keep_waiting = True
855 if keep_waiting:
856 time.sleep(1)
857 elapsed_time += 1
858
859 #if we exceeded the timeout rollback
860 if elapsed_time >= volume_timeout:
861 #delete the volumes we just created
862 for volume_id in block_device_mapping.itervalues():
863 self.cinder.volumes.delete(volume_id)
864
865 #delete ports we just created
866 for net_item in net_list_vim:
867 if 'port-id' in net_item:
montesmorenocf227142017-01-12 12:24:21 +0000868 self.neutron.delete_port(net_item['port-id'])
montesmoreno0c8def02016-12-22 12:16:23 +0000869
870 raise vimconn.vimconnException('Timeout creating volumes for instance ' + name,
871 http_code=vimconn.HTTP_Request_Timeout)
872
tierno7edb6752016-03-21 17:37:52 +0100873 server = self.nova.servers.create(name, image_id, flavor_id, nics=net_list_vim, meta=metadata,
montesmoreno0c8def02016-12-22 12:16:23 +0000874 security_groups=security_groups,
875 availability_zone=self.config.get('availability_zone'),
876 key_name=self.config.get('keypair'),
877 userdata=userdata,
tierno36c0b172017-01-12 18:32:28 +0100878 config_drive = config_drive,
montesmoreno0c8def02016-12-22 12:16:23 +0000879 block_device_mapping = block_device_mapping
880 ) # , description=description)
tiernoae4a8d12016-07-08 12:30:39 +0200881 #print "DONE :-)", server
ahmadsaf853d452016-12-22 11:33:47 +0500882 pool_id = None
883 floating_ips = self.neutron.list_floatingips().get("floatingips", ())
884 for floating_network in external_network:
tiernof8383b82017-01-18 15:49:48 +0100885 try:
886 # wait until vm is active
887 elapsed_time = 0
888 while elapsed_time < server_timeout:
889 status = self.nova.servers.get(server.id).status
890 if status == 'ACTIVE':
891 break
892 time.sleep(1)
893 elapsed_time += 1
montesmoreno2a1fc4e2017-01-09 16:46:04 +0000894
tiernof8383b82017-01-18 15:49:48 +0100895 #if we exceeded the timeout rollback
896 if elapsed_time >= server_timeout:
897 raise vimconn.vimconnException('Timeout creating instance ' + name,
898 http_code=vimconn.HTTP_Request_Timeout)
montesmoreno2a1fc4e2017-01-09 16:46:04 +0000899
tiernof8383b82017-01-18 15:49:48 +0100900 assigned = False
901 while(assigned == False):
902 if floating_ips:
903 ip = floating_ips.pop(0)
904 if not ip.get("port_id", False) and ip.get('tenant_id') == server.tenant_id:
905 free_floating_ip = ip.get("floating_ip_address")
906 try:
907 fix_ip = floating_network.get('ip')
908 server.add_floating_ip(free_floating_ip, fix_ip)
909 assigned = True
910 except Exception as e:
911 raise vimconn.vimconnException(type(e).__name__ + ": Cannot create floating_ip "+ str(e), http_code=vimconn.HTTP_Conflict)
912 else:
913 #Find the external network
914 external_nets = list()
915 for net in self.neutron.list_networks()['networks']:
916 if net['router:external']:
917 external_nets.append(net)
918
919 if len(external_nets) == 0:
920 raise vimconn.vimconnException("Cannot create floating_ip automatically since no external "
921 "network is present",
922 http_code=vimconn.HTTP_Conflict)
923 if len(external_nets) > 1:
924 raise vimconn.vimconnException("Cannot create floating_ip automatically since multiple "
925 "external networks are present",
926 http_code=vimconn.HTTP_Conflict)
927
928 pool_id = external_nets[0].get('id')
929 param = {'floatingip': {'floating_network_id': pool_id, 'tenant_id': server.tenant_id}}
ahmadsaf853d452016-12-22 11:33:47 +0500930 try:
tiernof8383b82017-01-18 15:49:48 +0100931 #self.logger.debug("Creating floating IP")
932 new_floating_ip = self.neutron.create_floatingip(param)
933 free_floating_ip = new_floating_ip['floatingip']['floating_ip_address']
ahmadsaf853d452016-12-22 11:33:47 +0500934 fix_ip = floating_network.get('ip')
935 server.add_floating_ip(free_floating_ip, fix_ip)
tiernof8383b82017-01-18 15:49:48 +0100936 assigned=True
ahmadsaf853d452016-12-22 11:33:47 +0500937 except Exception as e:
tiernof8383b82017-01-18 15:49:48 +0100938 raise vimconn.vimconnException(type(e).__name__ + ": Cannot assign floating_ip "+ str(e), http_code=vimconn.HTTP_Conflict)
939 except Exception as e:
940 if not floating_network['exit_on_floating_ip_error']:
941 self.logger.warn("Cannot create floating_ip. %s", str(e))
942 continue
943 self.delete_vminstance(server.id)
944 raise
montesmoreno2a1fc4e2017-01-09 16:46:04 +0000945
tiernoae4a8d12016-07-08 12:30:39 +0200946 return server.id
tierno7edb6752016-03-21 17:37:52 +0100947# except nvExceptions.NotFound as e:
948# error_value=-vimconn.HTTP_Not_Found
949# error_text= "vm instance %s not found" % vm_id
tiernof8383b82017-01-18 15:49:48 +0100950 except (ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError) as e:
montesmoreno2a1fc4e2017-01-09 16:46:04 +0000951 # delete the volumes we just created
952 if block_device_mapping != None:
953 for volume_id in block_device_mapping.itervalues():
954 self.cinder.volumes.delete(volume_id)
955
956 # delete ports we just created
957 for net_item in net_list_vim:
958 if 'port-id' in net_item:
montesmorenocf227142017-01-12 12:24:21 +0000959 self.neutron.delete_port(net_item['port-id'])
tiernoae4a8d12016-07-08 12:30:39 +0200960 self._format_exception(e)
961 except TypeError as e:
962 raise vimconn.vimconnException(type(e).__name__ + ": "+ str(e), http_code=vimconn.HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +0100963
tiernoae4a8d12016-07-08 12:30:39 +0200964 def get_vminstance(self,vm_id):
tierno7edb6752016-03-21 17:37:52 +0100965 '''Returns the VM instance information from VIM'''
tiernoae4a8d12016-07-08 12:30:39 +0200966 #self.logger.debug("Getting VM from VIM")
tierno7edb6752016-03-21 17:37:52 +0100967 try:
968 self._reload_connection()
969 server = self.nova.servers.find(id=vm_id)
970 #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
tiernoae4a8d12016-07-08 12:30:39 +0200971 return server.to_dict()
tierno8e995ce2016-09-22 08:13:00 +0000972 except (ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.NotFound, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +0200973 self._format_exception(e)
974
975 def get_vminstance_console(self,vm_id, console_type="vnc"):
tierno7edb6752016-03-21 17:37:52 +0100976 '''
977 Get a console for the virtual machine
978 Params:
979 vm_id: uuid of the VM
980 console_type, can be:
981 "novnc" (by default), "xvpvnc" for VNC types,
982 "rdp-html5" for RDP types, "spice-html5" for SPICE types
tiernoae4a8d12016-07-08 12:30:39 +0200983 Returns dict with the console parameters:
984 protocol: ssh, ftp, http, https, ...
985 server: usually ip address
986 port: the http, ssh, ... port
987 suffix: extra text, e.g. the http path and query string
tierno7edb6752016-03-21 17:37:52 +0100988 '''
tiernoae4a8d12016-07-08 12:30:39 +0200989 self.logger.debug("Getting VM CONSOLE from VIM")
tierno7edb6752016-03-21 17:37:52 +0100990 try:
991 self._reload_connection()
992 server = self.nova.servers.find(id=vm_id)
993 if console_type == None or console_type == "novnc":
994 console_dict = server.get_vnc_console("novnc")
995 elif console_type == "xvpvnc":
996 console_dict = server.get_vnc_console(console_type)
997 elif console_type == "rdp-html5":
998 console_dict = server.get_rdp_console(console_type)
999 elif console_type == "spice-html5":
1000 console_dict = server.get_spice_console(console_type)
1001 else:
tiernoae4a8d12016-07-08 12:30:39 +02001002 raise vimconn.vimconnException("console type '{}' not allowed".format(console_type), http_code=vimconn.HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001003
1004 console_dict1 = console_dict.get("console")
1005 if console_dict1:
1006 console_url = console_dict1.get("url")
1007 if console_url:
1008 #parse console_url
1009 protocol_index = console_url.find("//")
1010 suffix_index = console_url[protocol_index+2:].find("/") + protocol_index+2
1011 port_index = console_url[protocol_index+2:suffix_index].find(":") + protocol_index+2
1012 if protocol_index < 0 or port_index<0 or suffix_index<0:
1013 return -vimconn.HTTP_Internal_Server_Error, "Unexpected response from VIM"
1014 console_dict={"protocol": console_url[0:protocol_index],
1015 "server": console_url[protocol_index+2:port_index],
1016 "port": console_url[port_index:suffix_index],
1017 "suffix": console_url[suffix_index+1:]
1018 }
1019 protocol_index += 2
tiernoae4a8d12016-07-08 12:30:39 +02001020 return console_dict
1021 raise vimconn.vimconnUnexpectedResponse("Unexpected response from VIM")
tierno7edb6752016-03-21 17:37:52 +01001022
tierno8e995ce2016-09-22 08:13:00 +00001023 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.BadRequest, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001024 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +01001025
tiernoae4a8d12016-07-08 12:30:39 +02001026 def delete_vminstance(self, vm_id):
1027 '''Removes a VM instance from VIM. Returns the old identifier
tierno7edb6752016-03-21 17:37:52 +01001028 '''
tiernoae4a8d12016-07-08 12:30:39 +02001029 #print "osconnector: Getting VM from VIM"
tierno7edb6752016-03-21 17:37:52 +01001030 try:
1031 self._reload_connection()
1032 #delete VM ports attached to this networks before the virtual machine
1033 ports = self.neutron.list_ports(device_id=vm_id)
1034 for p in ports['ports']:
1035 try:
1036 self.neutron.delete_port(p["id"])
1037 except Exception as e:
tiernoae4a8d12016-07-08 12:30:39 +02001038 self.logger.error("Error deleting port: " + type(e).__name__ + ": "+ str(e))
montesmoreno0c8def02016-12-22 12:16:23 +00001039
1040 #commented because detaching the volumes makes the servers.delete not work properly ?!?
1041 #dettach volumes attached
1042 server = self.nova.servers.get(vm_id)
1043 volumes_attached_dict = server._info['os-extended-volumes:volumes_attached']
1044 #for volume in volumes_attached_dict:
1045 # self.cinder.volumes.detach(volume['id'])
1046
tierno7edb6752016-03-21 17:37:52 +01001047 self.nova.servers.delete(vm_id)
montesmoreno0c8def02016-12-22 12:16:23 +00001048
1049 #delete volumes.
1050 #Although having detached them should have them in active status
1051 #we ensure in this loop
1052 keep_waiting = True
1053 elapsed_time = 0
1054 while keep_waiting and elapsed_time < volume_timeout:
1055 keep_waiting = False
1056 for volume in volumes_attached_dict:
1057 if self.cinder.volumes.get(volume['id']).status != 'available':
1058 keep_waiting = True
1059 else:
1060 self.cinder.volumes.delete(volume['id'])
1061 if keep_waiting:
1062 time.sleep(1)
1063 elapsed_time += 1
1064
tiernoae4a8d12016-07-08 12:30:39 +02001065 return vm_id
tierno8e995ce2016-09-22 08:13:00 +00001066 except (nvExceptions.NotFound, ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001067 self._format_exception(e)
tierno7edb6752016-03-21 17:37:52 +01001068 #TODO insert exception vimconn.HTTP_Unauthorized
1069 #if reaching here is because an exception
tierno7edb6752016-03-21 17:37:52 +01001070
tiernoae4a8d12016-07-08 12:30:39 +02001071 def refresh_vms_status(self, vm_list):
1072 '''Get the status of the virtual machines and their interfaces/ports
1073 Params: the list of VM identifiers
1074 Returns a dictionary with:
1075 vm_id: #VIM id of this Virtual Machine
1076 status: #Mandatory. Text with one of:
1077 # DELETED (not found at vim)
1078 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
1079 # OTHER (Vim reported other status not understood)
1080 # ERROR (VIM indicates an ERROR status)
1081 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
1082 # CREATING (on building process), ERROR
1083 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
1084 #
1085 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
1086 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1087 interfaces:
1088 - vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
1089 mac_address: #Text format XX:XX:XX:XX:XX:XX
1090 vim_net_id: #network id where this interface is connected
1091 vim_interface_id: #interface/port VIM id
1092 ip_address: #null, or text with IPv4, IPv6 address
tierno7edb6752016-03-21 17:37:52 +01001093 '''
tiernoae4a8d12016-07-08 12:30:39 +02001094 vm_dict={}
1095 self.logger.debug("refresh_vms status: Getting tenant VM instance information from VIM")
1096 for vm_id in vm_list:
1097 vm={}
1098 try:
1099 vm_vim = self.get_vminstance(vm_id)
1100 if vm_vim['status'] in vmStatus2manoFormat:
1101 vm['status'] = vmStatus2manoFormat[ vm_vim['status'] ]
tierno7edb6752016-03-21 17:37:52 +01001102 else:
tiernoae4a8d12016-07-08 12:30:39 +02001103 vm['status'] = "OTHER"
1104 vm['error_msg'] = "VIM status reported " + vm_vim['status']
tierno8e995ce2016-09-22 08:13:00 +00001105 try:
1106 vm['vim_info'] = yaml.safe_dump(vm_vim, default_flow_style=True, width=256)
1107 except yaml.representer.RepresenterError:
1108 vm['vim_info'] = str(vm_vim)
tiernoae4a8d12016-07-08 12:30:39 +02001109 vm["interfaces"] = []
1110 if vm_vim.get('fault'):
1111 vm['error_msg'] = str(vm_vim['fault'])
1112 #get interfaces
tierno7edb6752016-03-21 17:37:52 +01001113 try:
tiernoae4a8d12016-07-08 12:30:39 +02001114 self._reload_connection()
1115 port_dict=self.neutron.list_ports(device_id=vm_id)
1116 for port in port_dict["ports"]:
1117 interface={}
tierno8e995ce2016-09-22 08:13:00 +00001118 try:
1119 interface['vim_info'] = yaml.safe_dump(port, default_flow_style=True, width=256)
1120 except yaml.representer.RepresenterError:
1121 interface['vim_info'] = str(port)
tiernoae4a8d12016-07-08 12:30:39 +02001122 interface["mac_address"] = port.get("mac_address")
1123 interface["vim_net_id"] = port["network_id"]
1124 interface["vim_interface_id"] = port["id"]
1125 ips=[]
1126 #look for floating ip address
1127 floating_ip_dict = self.neutron.list_floatingips(port_id=port["id"])
1128 if floating_ip_dict.get("floatingips"):
1129 ips.append(floating_ip_dict["floatingips"][0].get("floating_ip_address") )
tierno7edb6752016-03-21 17:37:52 +01001130
tiernoae4a8d12016-07-08 12:30:39 +02001131 for subnet in port["fixed_ips"]:
1132 ips.append(subnet["ip_address"])
1133 interface["ip_address"] = ";".join(ips)
1134 vm["interfaces"].append(interface)
1135 except Exception as e:
1136 self.logger.error("Error getting vm interface information " + type(e).__name__ + ": "+ str(e))
1137 except vimconn.vimconnNotFoundException as e:
1138 self.logger.error("Exception getting vm status: %s", str(e))
1139 vm['status'] = "DELETED"
1140 vm['error_msg'] = str(e)
1141 except vimconn.vimconnException as e:
1142 self.logger.error("Exception getting vm status: %s", str(e))
1143 vm['status'] = "VIM_ERROR"
1144 vm['error_msg'] = str(e)
1145 vm_dict[vm_id] = vm
1146 return vm_dict
tierno7edb6752016-03-21 17:37:52 +01001147
tiernoae4a8d12016-07-08 12:30:39 +02001148 def action_vminstance(self, vm_id, action_dict):
tierno7edb6752016-03-21 17:37:52 +01001149 '''Send and action over a VM instance from VIM
tiernoae4a8d12016-07-08 12:30:39 +02001150 Returns the vm_id if the action was successfully sent to the VIM'''
1151 self.logger.debug("Action over VM '%s': %s", vm_id, str(action_dict))
tierno7edb6752016-03-21 17:37:52 +01001152 try:
1153 self._reload_connection()
1154 server = self.nova.servers.find(id=vm_id)
1155 if "start" in action_dict:
1156 if action_dict["start"]=="rebuild":
1157 server.rebuild()
1158 else:
1159 if server.status=="PAUSED":
1160 server.unpause()
1161 elif server.status=="SUSPENDED":
1162 server.resume()
1163 elif server.status=="SHUTOFF":
1164 server.start()
1165 elif "pause" in action_dict:
1166 server.pause()
1167 elif "resume" in action_dict:
1168 server.resume()
1169 elif "shutoff" in action_dict or "shutdown" in action_dict:
1170 server.stop()
1171 elif "forceOff" in action_dict:
1172 server.stop() #TODO
1173 elif "terminate" in action_dict:
1174 server.delete()
1175 elif "createImage" in action_dict:
1176 server.create_image()
1177 #"path":path_schema,
1178 #"description":description_schema,
1179 #"name":name_schema,
1180 #"metadata":metadata_schema,
1181 #"imageRef": id_schema,
1182 #"disk": {"oneOf":[{"type": "null"}, {"type":"string"}] },
1183 elif "rebuild" in action_dict:
1184 server.rebuild(server.image['id'])
1185 elif "reboot" in action_dict:
1186 server.reboot() #reboot_type='SOFT'
1187 elif "console" in action_dict:
1188 console_type = action_dict["console"]
1189 if console_type == None or console_type == "novnc":
1190 console_dict = server.get_vnc_console("novnc")
1191 elif console_type == "xvpvnc":
1192 console_dict = server.get_vnc_console(console_type)
1193 elif console_type == "rdp-html5":
1194 console_dict = server.get_rdp_console(console_type)
1195 elif console_type == "spice-html5":
1196 console_dict = server.get_spice_console(console_type)
1197 else:
tiernoae4a8d12016-07-08 12:30:39 +02001198 raise vimconn.vimconnException("console type '{}' not allowed".format(console_type),
1199 http_code=vimconn.HTTP_Bad_Request)
tierno7edb6752016-03-21 17:37:52 +01001200 try:
1201 console_url = console_dict["console"]["url"]
1202 #parse console_url
1203 protocol_index = console_url.find("//")
1204 suffix_index = console_url[protocol_index+2:].find("/") + protocol_index+2
1205 port_index = console_url[protocol_index+2:suffix_index].find(":") + protocol_index+2
1206 if protocol_index < 0 or port_index<0 or suffix_index<0:
tiernoae4a8d12016-07-08 12:30:39 +02001207 raise vimconn.vimconnException("Unexpected response from VIM " + str(console_dict))
tierno7edb6752016-03-21 17:37:52 +01001208 console_dict2={"protocol": console_url[0:protocol_index],
1209 "server": console_url[protocol_index+2 : port_index],
1210 "port": int(console_url[port_index+1 : suffix_index]),
1211 "suffix": console_url[suffix_index+1:]
1212 }
tiernoae4a8d12016-07-08 12:30:39 +02001213 return console_dict2
1214 except Exception as e:
1215 raise vimconn.vimconnException("Unexpected response from VIM " + str(console_dict))
tierno7edb6752016-03-21 17:37:52 +01001216
tiernoae4a8d12016-07-08 12:30:39 +02001217 return vm_id
tierno8e995ce2016-09-22 08:13:00 +00001218 except (ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.NotFound, ConnectionError) as e:
tiernoae4a8d12016-07-08 12:30:39 +02001219 self._format_exception(e)
1220 #TODO insert exception vimconn.HTTP_Unauthorized
1221
1222#NOT USED FUNCTIONS
1223
1224 def new_external_port(self, port_data):
1225 #TODO openstack if needed
1226 '''Adds a external port to VIM'''
1227 '''Returns the port identifier'''
1228 return -vimconn.HTTP_Internal_Server_Error, "osconnector.new_external_port() not implemented"
1229
1230 def connect_port_network(self, port_id, network_id, admin=False):
1231 #TODO openstack if needed
1232 '''Connects a external port to a network'''
1233 '''Returns status code of the VIM response'''
1234 return -vimconn.HTTP_Internal_Server_Error, "osconnector.connect_port_network() not implemented"
1235
1236 def new_user(self, user_name, user_passwd, tenant_id=None):
1237 '''Adds a new user to openstack VIM'''
1238 '''Returns the user identifier'''
1239 self.logger.debug("osconnector: Adding a new user to VIM")
1240 try:
1241 self._reload_connection()
1242 user=self.keystone.users.create(user_name, user_passwd, tenant_id=tenant_id)
1243 #self.keystone.tenants.add_user(self.k_creds["username"], #role)
1244 return user.id
1245 except ksExceptions.ConnectionError as e:
1246 error_value=-vimconn.HTTP_Bad_Request
1247 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1248 except ksExceptions.ClientException as e: #TODO remove
tierno7edb6752016-03-21 17:37:52 +01001249 error_value=-vimconn.HTTP_Bad_Request
1250 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1251 #TODO insert exception vimconn.HTTP_Unauthorized
1252 #if reaching here is because an exception
1253 if self.debug:
tiernoae4a8d12016-07-08 12:30:39 +02001254 self.logger.debug("new_user " + error_text)
tierno7edb6752016-03-21 17:37:52 +01001255 return error_value, error_text
tiernoae4a8d12016-07-08 12:30:39 +02001256
1257 def delete_user(self, user_id):
1258 '''Delete a user from openstack VIM'''
1259 '''Returns the user identifier'''
1260 if self.debug:
1261 print "osconnector: Deleting a user from VIM"
1262 try:
1263 self._reload_connection()
1264 self.keystone.users.delete(user_id)
1265 return 1, user_id
1266 except ksExceptions.ConnectionError as e:
1267 error_value=-vimconn.HTTP_Bad_Request
1268 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1269 except ksExceptions.NotFound as e:
1270 error_value=-vimconn.HTTP_Not_Found
1271 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1272 except ksExceptions.ClientException as e: #TODO remove
1273 error_value=-vimconn.HTTP_Bad_Request
1274 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1275 #TODO insert exception vimconn.HTTP_Unauthorized
1276 #if reaching here is because an exception
1277 if self.debug:
1278 print "delete_tenant " + error_text
1279 return error_value, error_text
1280
tierno7edb6752016-03-21 17:37:52 +01001281 def get_hosts_info(self):
1282 '''Get the information of deployed hosts
1283 Returns the hosts content'''
1284 if self.debug:
1285 print "osconnector: Getting Host info from VIM"
1286 try:
1287 h_list=[]
1288 self._reload_connection()
1289 hypervisors = self.nova.hypervisors.list()
1290 for hype in hypervisors:
1291 h_list.append( hype.to_dict() )
1292 return 1, {"hosts":h_list}
1293 except nvExceptions.NotFound as e:
1294 error_value=-vimconn.HTTP_Not_Found
1295 error_text= (str(e) if len(e.args)==0 else str(e.args[0]))
1296 except (ksExceptions.ClientException, nvExceptions.ClientException) as e:
1297 error_value=-vimconn.HTTP_Bad_Request
1298 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1299 #TODO insert exception vimconn.HTTP_Unauthorized
1300 #if reaching here is because an exception
1301 if self.debug:
1302 print "get_hosts_info " + error_text
1303 return error_value, error_text
1304
1305 def get_hosts(self, vim_tenant):
1306 '''Get the hosts and deployed instances
1307 Returns the hosts content'''
1308 r, hype_dict = self.get_hosts_info()
1309 if r<0:
1310 return r, hype_dict
1311 hypervisors = hype_dict["hosts"]
1312 try:
1313 servers = self.nova.servers.list()
1314 for hype in hypervisors:
1315 for server in servers:
1316 if server.to_dict()['OS-EXT-SRV-ATTR:hypervisor_hostname']==hype['hypervisor_hostname']:
1317 if 'vm' in hype:
1318 hype['vm'].append(server.id)
1319 else:
1320 hype['vm'] = [server.id]
1321 return 1, hype_dict
1322 except nvExceptions.NotFound as e:
1323 error_value=-vimconn.HTTP_Not_Found
1324 error_text= (str(e) if len(e.args)==0 else str(e.args[0]))
1325 except (ksExceptions.ClientException, nvExceptions.ClientException) as e:
1326 error_value=-vimconn.HTTP_Bad_Request
1327 error_text= type(e).__name__ + ": "+ (str(e) if len(e.args)==0 else str(e.args[0]))
1328 #TODO insert exception vimconn.HTTP_Unauthorized
1329 #if reaching here is because an exception
1330 if self.debug:
1331 print "get_hosts " + error_text
1332 return error_value, error_text
1333
tierno7edb6752016-03-21 17:37:52 +01001334