Bug 573 VCD: Ensure that more than 2 NICs can get connected to VCD VM
[osm/RO.git] / osm_ro / vimconn_vmware.py
1 # -*- coding: utf-8 -*-
2
3 ##
4 # Copyright 2016-2017 VMware Inc.
5 # This file is part of ETSI OSM
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: osslegalrouting@vmware.com
22 ##
23
24 """
25 vimconn_vmware implementation an Abstract class in order to interact with VMware vCloud Director.
26 mbayramov@vmware.com
27 """
28 from progressbar import Percentage, Bar, ETA, FileTransferSpeed, ProgressBar
29
30 import vimconn
31 import os
32 import traceback
33 import itertools
34 import requests
35 import ssl
36 import atexit
37
38 from pyVmomi import vim, vmodl
39 from pyVim.connect import SmartConnect, Disconnect
40
41 from xml.etree import ElementTree as XmlElementTree
42 from lxml import etree as lxmlElementTree
43
44 import yaml
45 from pyvcloud.vcd.client import BasicLoginCredentials,Client,VcdTaskException
46 from pyvcloud.vcd.vdc import VDC
47 from pyvcloud.vcd.org import Org
48 import re
49 from pyvcloud.vcd.vapp import VApp
50 from xml.sax.saxutils import escape
51 import logging
52 import json
53 import time
54 import uuid
55 import httplib
56 #For python3
57 #import http.client
58 import hashlib
59 import socket
60 import struct
61 import netaddr
62 import random
63
64 # global variable for vcd connector type
65 STANDALONE = 'standalone'
66
67 # key for flavor dicts
68 FLAVOR_RAM_KEY = 'ram'
69 FLAVOR_VCPUS_KEY = 'vcpus'
70 FLAVOR_DISK_KEY = 'disk'
71 DEFAULT_IP_PROFILE = {'dhcp_count':50,
72 'dhcp_enabled':True,
73 'ip_version':"IPv4"
74 }
75 # global variable for wait time
76 INTERVAL_TIME = 5
77 MAX_WAIT_TIME = 1800
78
79 API_VERSION = '5.9'
80
81 __author__ = "Mustafa Bayramov, Arpita Kate, Sachin Bhangare, Prakash Kasar"
82 __date__ = "$09-Mar-2018 11:09:29$"
83 __version__ = '0.2'
84
85 # -1: "Could not be created",
86 # 0: "Unresolved",
87 # 1: "Resolved",
88 # 2: "Deployed",
89 # 3: "Suspended",
90 # 4: "Powered on",
91 # 5: "Waiting for user input",
92 # 6: "Unknown state",
93 # 7: "Unrecognized state",
94 # 8: "Powered off",
95 # 9: "Inconsistent state",
96 # 10: "Children do not all have the same status",
97 # 11: "Upload initiated, OVF descriptor pending",
98 # 12: "Upload initiated, copying contents",
99 # 13: "Upload initiated , disk contents pending",
100 # 14: "Upload has been quarantined",
101 # 15: "Upload quarantine period has expired"
102
103 # mapping vCD status to MANO
104 vcdStatusCode2manoFormat = {4: 'ACTIVE',
105 7: 'PAUSED',
106 3: 'SUSPENDED',
107 8: 'INACTIVE',
108 12: 'BUILD',
109 -1: 'ERROR',
110 14: 'DELETED'}
111
112 #
113 netStatus2manoFormat = {'ACTIVE': 'ACTIVE', 'PAUSED': 'PAUSED', 'INACTIVE': 'INACTIVE', 'BUILD': 'BUILD',
114 'ERROR': 'ERROR', 'DELETED': 'DELETED'
115 }
116
117 class vimconnector(vimconn.vimconnector):
118 # dict used to store flavor in memory
119 flavorlist = {}
120
121 def __init__(self, uuid=None, name=None, tenant_id=None, tenant_name=None,
122 url=None, url_admin=None, user=None, passwd=None, log_level=None, config={}, persistent_info={}):
123 """
124 Constructor create vmware connector to vCloud director.
125
126 By default construct doesn't validate connection state. So client can create object with None arguments.
127 If client specified username , password and host and VDC name. Connector initialize other missing attributes.
128
129 a) It initialize organization UUID
130 b) Initialize tenant_id/vdc ID. (This information derived from tenant name)
131
132 Args:
133 uuid - is organization uuid.
134 name - is organization name that must be presented in vCloud director.
135 tenant_id - is VDC uuid it must be presented in vCloud director
136 tenant_name - is VDC name.
137 url - is hostname or ip address of vCloud director
138 url_admin - same as above.
139 user - is user that administrator for organization. Caller must make sure that
140 username has right privileges.
141
142 password - is password for a user.
143
144 VMware connector also requires PVDC administrative privileges and separate account.
145 This variables must be passed via config argument dict contains keys
146
147 dict['admin_username']
148 dict['admin_password']
149 config - Provide NSX and vCenter information
150
151 Returns:
152 Nothing.
153 """
154
155 vimconn.vimconnector.__init__(self, uuid, name, tenant_id, tenant_name, url,
156 url_admin, user, passwd, log_level, config)
157
158 self.logger = logging.getLogger('openmano.vim.vmware')
159 self.logger.setLevel(10)
160 self.persistent_info = persistent_info
161
162 self.name = name
163 self.id = uuid
164 self.url = url
165 self.url_admin = url_admin
166 self.tenant_id = tenant_id
167 self.tenant_name = tenant_name
168 self.user = user
169 self.passwd = passwd
170 self.config = config
171 self.admin_password = None
172 self.admin_user = None
173 self.org_name = ""
174 self.nsx_manager = None
175 self.nsx_user = None
176 self.nsx_password = None
177 self.availability_zone = None
178
179 # Disable warnings from self-signed certificates.
180 requests.packages.urllib3.disable_warnings()
181
182 if tenant_name is not None:
183 orgnameandtenant = tenant_name.split(":")
184 if len(orgnameandtenant) == 2:
185 self.tenant_name = orgnameandtenant[1]
186 self.org_name = orgnameandtenant[0]
187 else:
188 self.tenant_name = tenant_name
189 if "orgname" in config:
190 self.org_name = config['orgname']
191
192 if log_level:
193 self.logger.setLevel(getattr(logging, log_level))
194
195 try:
196 self.admin_user = config['admin_username']
197 self.admin_password = config['admin_password']
198 except KeyError:
199 raise vimconn.vimconnException(message="Error admin username or admin password is empty.")
200
201 try:
202 self.nsx_manager = config['nsx_manager']
203 self.nsx_user = config['nsx_user']
204 self.nsx_password = config['nsx_password']
205 except KeyError:
206 raise vimconn.vimconnException(message="Error: nsx manager or nsx user or nsx password is empty in Config")
207
208 self.vcenter_ip = config.get("vcenter_ip", None)
209 self.vcenter_port = config.get("vcenter_port", None)
210 self.vcenter_user = config.get("vcenter_user", None)
211 self.vcenter_password = config.get("vcenter_password", None)
212
213 #Set availability zone for Affinity rules
214 self.availability_zone = self.set_availability_zones()
215
216 # ############# Stub code for SRIOV #################
217 # try:
218 # self.dvs_name = config['dv_switch_name']
219 # except KeyError:
220 # raise vimconn.vimconnException(message="Error: distributed virtaul switch name is empty in Config")
221 #
222 # self.vlanID_range = config.get("vlanID_range", None)
223
224 self.org_uuid = None
225 self.client = None
226
227 if not url:
228 raise vimconn.vimconnException('url param can not be NoneType')
229
230 if not self.url_admin: # try to use normal url
231 self.url_admin = self.url
232
233 logging.debug("UUID: {} name: {} tenant_id: {} tenant name {}".format(self.id, self.org_name,
234 self.tenant_id, self.tenant_name))
235 logging.debug("vcd url {} vcd username: {} vcd password: {}".format(self.url, self.user, self.passwd))
236 logging.debug("vcd admin username {} vcd admin passowrd {}".format(self.admin_user, self.admin_password))
237
238 # initialize organization
239 if self.user is not None and self.passwd is not None and self.url:
240 self.init_organization()
241
242 def __getitem__(self, index):
243 if index == 'name':
244 return self.name
245 if index == 'tenant_id':
246 return self.tenant_id
247 if index == 'tenant_name':
248 return self.tenant_name
249 elif index == 'id':
250 return self.id
251 elif index == 'org_name':
252 return self.org_name
253 elif index == 'org_uuid':
254 return self.org_uuid
255 elif index == 'user':
256 return self.user
257 elif index == 'passwd':
258 return self.passwd
259 elif index == 'url':
260 return self.url
261 elif index == 'url_admin':
262 return self.url_admin
263 elif index == "config":
264 return self.config
265 else:
266 raise KeyError("Invalid key '%s'" % str(index))
267
268 def __setitem__(self, index, value):
269 if index == 'name':
270 self.name = value
271 if index == 'tenant_id':
272 self.tenant_id = value
273 if index == 'tenant_name':
274 self.tenant_name = value
275 elif index == 'id':
276 self.id = value
277 elif index == 'org_name':
278 self.org_name = value
279 elif index == 'org_uuid':
280 self.org_uuid = value
281 elif index == 'user':
282 self.user = value
283 elif index == 'passwd':
284 self.passwd = value
285 elif index == 'url':
286 self.url = value
287 elif index == 'url_admin':
288 self.url_admin = value
289 else:
290 raise KeyError("Invalid key '%s'" % str(index))
291
292 def connect_as_admin(self):
293 """ Method connect as pvdc admin user to vCloud director.
294 There are certain action that can be done only by provider vdc admin user.
295 Organization creation / provider network creation etc.
296
297 Returns:
298 The return client object that latter can be used to connect to vcloud director as admin for provider vdc
299 """
300
301 self.logger.debug("Logging into vCD {} as admin.".format(self.org_name))
302
303 try:
304 host = self.url
305 org = 'System'
306 client_as_admin = Client(host, verify_ssl_certs=False)
307 client_as_admin.set_credentials(BasicLoginCredentials(self.admin_user, org, self.admin_password))
308 except Exception as e:
309 raise vimconn.vimconnException(
310 "Can't connect to a vCloud director as: {} with exception {}".format(self.admin_user, e))
311
312 return client_as_admin
313
314 def connect(self):
315 """ Method connect as normal user to vCloud director.
316
317 Returns:
318 The return client object that latter can be used to connect to vCloud director as admin for VDC
319 """
320
321 try:
322 self.logger.debug("Logging into vCD {} as {} to datacenter {}.".format(self.org_name,
323 self.user,
324 self.org_name))
325 host = self.url
326 client = Client(host, verify_ssl_certs=False)
327 client.set_credentials(BasicLoginCredentials(self.user, self.org_name, self.passwd))
328 except:
329 raise vimconn.vimconnConnectionException("Can't connect to a vCloud director org: "
330 "{} as user: {}".format(self.org_name, self.user))
331
332 return client
333
334 def init_organization(self):
335 """ Method initialize organization UUID and VDC parameters.
336
337 At bare minimum client must provide organization name that present in vCloud director and VDC.
338
339 The VDC - UUID ( tenant_id) will be initialized at the run time if client didn't call constructor.
340 The Org - UUID will be initialized at the run time if data center present in vCloud director.
341
342 Returns:
343 The return vca object that letter can be used to connect to vcloud direct as admin
344 """
345 client = self.connect()
346 if not client:
347 raise vimconn.vimconnConnectionException("Failed to connect vCD.")
348
349 self.client = client
350 try:
351 if self.org_uuid is None:
352 org_list = client.get_org_list()
353 for org in org_list.Org:
354 # we set org UUID at the init phase but we can do it only when we have valid credential.
355 if org.get('name') == self.org_name:
356 self.org_uuid = org.get('href').split('/')[-1]
357 self.logger.debug("Setting organization UUID {}".format(self.org_uuid))
358 break
359 else:
360 raise vimconn.vimconnException("Vcloud director organization {} not found".format(self.org_name))
361
362 # if well good we require for org details
363 org_details_dict = self.get_org(org_uuid=self.org_uuid)
364
365 # we have two case if we want to initialize VDC ID or VDC name at run time
366 # tenant_name provided but no tenant id
367 if self.tenant_id is None and self.tenant_name is not None and 'vdcs' in org_details_dict:
368 vdcs_dict = org_details_dict['vdcs']
369 for vdc in vdcs_dict:
370 if vdcs_dict[vdc] == self.tenant_name:
371 self.tenant_id = vdc
372 self.logger.debug("Setting vdc uuid {} for organization UUID {}".format(self.tenant_id,
373 self.org_name))
374 break
375 else:
376 raise vimconn.vimconnException("Tenant name indicated but not present in vcloud director.")
377 # case two we have tenant_id but we don't have tenant name so we find and set it.
378 if self.tenant_id is not None and self.tenant_name is None and 'vdcs' in org_details_dict:
379 vdcs_dict = org_details_dict['vdcs']
380 for vdc in vdcs_dict:
381 if vdc == self.tenant_id:
382 self.tenant_name = vdcs_dict[vdc]
383 self.logger.debug("Setting vdc uuid {} for organization UUID {}".format(self.tenant_id,
384 self.org_name))
385 break
386 else:
387 raise vimconn.vimconnException("Tenant id indicated but not present in vcloud director")
388 self.logger.debug("Setting organization uuid {}".format(self.org_uuid))
389 except:
390 self.logger.debug("Failed initialize organization UUID for org {}".format(self.org_name))
391 self.logger.debug(traceback.format_exc())
392 self.org_uuid = None
393
394 def new_tenant(self, tenant_name=None, tenant_description=None):
395 """ Method adds a new tenant to VIM with this name.
396 This action requires access to create VDC action in vCloud director.
397
398 Args:
399 tenant_name is tenant_name to be created.
400 tenant_description not used for this call
401
402 Return:
403 returns the tenant identifier in UUID format.
404 If action is failed method will throw vimconn.vimconnException method
405 """
406 vdc_task = self.create_vdc(vdc_name=tenant_name)
407 if vdc_task is not None:
408 vdc_uuid, value = vdc_task.popitem()
409 self.logger.info("Created new vdc {} and uuid: {}".format(tenant_name, vdc_uuid))
410 return vdc_uuid
411 else:
412 raise vimconn.vimconnException("Failed create tenant {}".format(tenant_name))
413
414 def delete_tenant(self, tenant_id=None):
415 """ Delete a tenant from VIM
416 Args:
417 tenant_id is tenant_id to be deleted.
418
419 Return:
420 returns the tenant identifier in UUID format.
421 If action is failed method will throw exception
422 """
423 vca = self.connect_as_admin()
424 if not vca:
425 raise vimconn.vimconnConnectionException("Failed to connect vCD")
426
427 if tenant_id is not None:
428 if vca._session:
429 #Get OrgVDC
430 url_list = [self.url, '/api/vdc/', tenant_id]
431 orgvdc_herf = ''.join(url_list)
432
433 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
434 'x-vcloud-authorization': vca._session.headers['x-vcloud-authorization']}
435 response = self.perform_request(req_type='GET',
436 url=orgvdc_herf,
437 headers=headers)
438
439 if response.status_code != requests.codes.ok:
440 self.logger.debug("delete_tenant():GET REST API call {} failed. "\
441 "Return status code {}".format(orgvdc_herf,
442 response.status_code))
443 raise vimconn.vimconnNotFoundException("Fail to get tenant {}".format(tenant_id))
444
445 lxmlroot_respond = lxmlElementTree.fromstring(response.content)
446 namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.iteritems() if prefix}
447 #For python3
448 #namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.items() if prefix}
449 namespaces["xmlns"]= "http://www.vmware.com/vcloud/v1.5"
450 vdc_remove_href = lxmlroot_respond.find("xmlns:Link[@rel='remove']",namespaces).attrib['href']
451 vdc_remove_href = vdc_remove_href + '?recursive=true&force=true'
452
453 response = self.perform_request(req_type='DELETE',
454 url=vdc_remove_href,
455 headers=headers)
456
457 if response.status_code == 202:
458 time.sleep(5)
459 return tenant_id
460 else:
461 self.logger.debug("delete_tenant(): DELETE REST API call {} failed. "\
462 "Return status code {}".format(vdc_remove_href,
463 response.status_code))
464 raise vimconn.vimconnException("Fail to delete tenant with ID {}".format(tenant_id))
465 else:
466 self.logger.debug("delete_tenant():Incorrect tenant ID {}".format(tenant_id))
467 raise vimconn.vimconnNotFoundException("Fail to get tenant {}".format(tenant_id))
468
469
470 def get_tenant_list(self, filter_dict={}):
471 """Obtain tenants of VIM
472 filter_dict can contain the following keys:
473 name: filter by tenant name
474 id: filter by tenant uuid/id
475 <other VIM specific>
476 Returns the tenant list of dictionaries:
477 [{'name':'<name>, 'id':'<id>, ...}, ...]
478
479 """
480 org_dict = self.get_org(self.org_uuid)
481 vdcs_dict = org_dict['vdcs']
482
483 vdclist = []
484 try:
485 for k in vdcs_dict:
486 entry = {'name': vdcs_dict[k], 'id': k}
487 # if caller didn't specify dictionary we return all tenants.
488 if filter_dict is not None and filter_dict:
489 filtered_entry = entry.copy()
490 filtered_dict = set(entry.keys()) - set(filter_dict)
491 for unwanted_key in filtered_dict: del entry[unwanted_key]
492 if filter_dict == entry:
493 vdclist.append(filtered_entry)
494 else:
495 vdclist.append(entry)
496 except:
497 self.logger.debug("Error in get_tenant_list()")
498 self.logger.debug(traceback.format_exc())
499 raise vimconn.vimconnException("Incorrect state. {}")
500
501 return vdclist
502
503 def new_network(self, net_name, net_type, ip_profile=None, shared=False):
504 """Adds a tenant network to VIM
505 net_name is the name
506 net_type can be 'bridge','data'.'ptp'.
507 ip_profile is a dict containing the IP parameters of the network
508 shared is a boolean
509 Returns the network identifier"""
510
511 self.logger.debug("new_network tenant {} net_type {} ip_profile {} shared {}"
512 .format(net_name, net_type, ip_profile, shared))
513
514 isshared = 'false'
515 if shared:
516 isshared = 'true'
517
518 # ############# Stub code for SRIOV #################
519 # if net_type == "data" or net_type == "ptp":
520 # if self.config.get('dv_switch_name') == None:
521 # raise vimconn.vimconnConflictException("You must provide 'dv_switch_name' at config value")
522 # network_uuid = self.create_dvPort_group(net_name)
523
524 network_uuid = self.create_network(network_name=net_name, net_type=net_type,
525 ip_profile=ip_profile, isshared=isshared)
526 if network_uuid is not None:
527 return network_uuid
528 else:
529 raise vimconn.vimconnUnexpectedResponse("Failed create a new network {}".format(net_name))
530
531 def get_vcd_network_list(self):
532 """ Method available organization for a logged in tenant
533
534 Returns:
535 The return vca object that letter can be used to connect to vcloud direct as admin
536 """
537
538 self.logger.debug("get_vcd_network_list(): retrieving network list for vcd {}".format(self.tenant_name))
539
540 if not self.tenant_name:
541 raise vimconn.vimconnConnectionException("Tenant name is empty.")
542
543 org, vdc = self.get_vdc_details()
544 if vdc is None:
545 raise vimconn.vimconnConnectionException("Can't retrieve information for a VDC {}".format(self.tenant_name))
546
547 vdc_uuid = vdc.get('id').split(":")[3]
548 if self.client._session:
549 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
550 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
551 response = self.perform_request(req_type='GET',
552 url=vdc.get('href'),
553 headers=headers)
554 if response.status_code != 200:
555 self.logger.error("Failed to get vdc content")
556 raise vimconn.vimconnNotFoundException("Failed to get vdc content")
557 else:
558 content = XmlElementTree.fromstring(response.content)
559
560 network_list = []
561 try:
562 for item in content:
563 if item.tag.split('}')[-1] == 'AvailableNetworks':
564 for net in item:
565 response = self.perform_request(req_type='GET',
566 url=net.get('href'),
567 headers=headers)
568
569 if response.status_code != 200:
570 self.logger.error("Failed to get network content")
571 raise vimconn.vimconnNotFoundException("Failed to get network content")
572 else:
573 net_details = XmlElementTree.fromstring(response.content)
574
575 filter_dict = {}
576 net_uuid = net_details.get('id').split(":")
577 if len(net_uuid) != 4:
578 continue
579 else:
580 net_uuid = net_uuid[3]
581 # create dict entry
582 self.logger.debug("get_vcd_network_list(): Adding network {} "
583 "to a list vcd id {} network {}".format(net_uuid,
584 vdc_uuid,
585 net_details.get('name')))
586 filter_dict["name"] = net_details.get('name')
587 filter_dict["id"] = net_uuid
588 if [i.text for i in net_details if i.tag.split('}')[-1] == 'IsShared'][0] == 'true':
589 shared = True
590 else:
591 shared = False
592 filter_dict["shared"] = shared
593 filter_dict["tenant_id"] = vdc_uuid
594 if int(net_details.get('status')) == 1:
595 filter_dict["admin_state_up"] = True
596 else:
597 filter_dict["admin_state_up"] = False
598 filter_dict["status"] = "ACTIVE"
599 filter_dict["type"] = "bridge"
600 network_list.append(filter_dict)
601 self.logger.debug("get_vcd_network_list adding entry {}".format(filter_dict))
602 except:
603 self.logger.debug("Error in get_vcd_network_list", exc_info=True)
604 pass
605
606 self.logger.debug("get_vcd_network_list returning {}".format(network_list))
607 return network_list
608
609 def get_network_list(self, filter_dict={}):
610 """Obtain tenant networks of VIM
611 Filter_dict can be:
612 name: network name OR/AND
613 id: network uuid OR/AND
614 shared: boolean OR/AND
615 tenant_id: tenant OR/AND
616 admin_state_up: boolean
617 status: 'ACTIVE'
618
619 [{key : value , key : value}]
620
621 Returns the network list of dictionaries:
622 [{<the fields at Filter_dict plus some VIM specific>}, ...]
623 List can be empty
624 """
625
626 self.logger.debug("get_network_list(): retrieving network list for vcd {}".format(self.tenant_name))
627
628 if not self.tenant_name:
629 raise vimconn.vimconnConnectionException("Tenant name is empty.")
630
631 org, vdc = self.get_vdc_details()
632 if vdc is None:
633 raise vimconn.vimconnConnectionException("Can't retrieve information for a VDC {}.".format(self.tenant_name))
634
635 try:
636 vdcid = vdc.get('id').split(":")[3]
637
638 if self.client._session:
639 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
640 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
641 response = self.perform_request(req_type='GET',
642 url=vdc.get('href'),
643 headers=headers)
644 if response.status_code != 200:
645 self.logger.error("Failed to get vdc content")
646 raise vimconn.vimconnNotFoundException("Failed to get vdc content")
647 else:
648 content = XmlElementTree.fromstring(response.content)
649
650 network_list = []
651 for item in content:
652 if item.tag.split('}')[-1] == 'AvailableNetworks':
653 for net in item:
654 response = self.perform_request(req_type='GET',
655 url=net.get('href'),
656 headers=headers)
657
658 if response.status_code != 200:
659 self.logger.error("Failed to get network content")
660 raise vimconn.vimconnNotFoundException("Failed to get network content")
661 else:
662 net_details = XmlElementTree.fromstring(response.content)
663
664 filter_entry = {}
665 net_uuid = net_details.get('id').split(":")
666 if len(net_uuid) != 4:
667 continue
668 else:
669 net_uuid = net_uuid[3]
670 # create dict entry
671 self.logger.debug("get_network_list(): Adding net {}"
672 " to a list vcd id {} network {}".format(net_uuid,
673 vdcid,
674 net_details.get('name')))
675 filter_entry["name"] = net_details.get('name')
676 filter_entry["id"] = net_uuid
677 if [i.text for i in net_details if i.tag.split('}')[-1] == 'IsShared'][0] == 'true':
678 shared = True
679 else:
680 shared = False
681 filter_entry["shared"] = shared
682 filter_entry["tenant_id"] = vdcid
683 if int(net_details.get('status')) == 1:
684 filter_entry["admin_state_up"] = True
685 else:
686 filter_entry["admin_state_up"] = False
687 filter_entry["status"] = "ACTIVE"
688 filter_entry["type"] = "bridge"
689 filtered_entry = filter_entry.copy()
690
691 if filter_dict is not None and filter_dict:
692 # we remove all the key : value we don't care and match only
693 # respected field
694 filtered_dict = set(filter_entry.keys()) - set(filter_dict)
695 for unwanted_key in filtered_dict: del filter_entry[unwanted_key]
696 if filter_dict == filter_entry:
697 network_list.append(filtered_entry)
698 else:
699 network_list.append(filtered_entry)
700 except Exception as e:
701 self.logger.debug("Error in get_network_list",exc_info=True)
702 if isinstance(e, vimconn.vimconnException):
703 raise
704 else:
705 raise vimconn.vimconnNotFoundException("Failed : Networks list not found {} ".format(e))
706
707 self.logger.debug("Returning {}".format(network_list))
708 return network_list
709
710 def get_network(self, net_id):
711 """Method obtains network details of net_id VIM network
712 Return a dict with the fields at filter_dict (see get_network_list) plus some VIM specific>}, ...]"""
713
714 try:
715 org, vdc = self.get_vdc_details()
716 vdc_id = vdc.get('id').split(":")[3]
717 if self.client._session:
718 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
719 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
720 response = self.perform_request(req_type='GET',
721 url=vdc.get('href'),
722 headers=headers)
723 if response.status_code != 200:
724 self.logger.error("Failed to get vdc content")
725 raise vimconn.vimconnNotFoundException("Failed to get vdc content")
726 else:
727 content = XmlElementTree.fromstring(response.content)
728
729 filter_dict = {}
730
731 for item in content:
732 if item.tag.split('}')[-1] == 'AvailableNetworks':
733 for net in item:
734 response = self.perform_request(req_type='GET',
735 url=net.get('href'),
736 headers=headers)
737
738 if response.status_code != 200:
739 self.logger.error("Failed to get network content")
740 raise vimconn.vimconnNotFoundException("Failed to get network content")
741 else:
742 net_details = XmlElementTree.fromstring(response.content)
743
744 vdc_network_id = net_details.get('id').split(":")
745 if len(vdc_network_id) == 4 and vdc_network_id[3] == net_id:
746 filter_dict["name"] = net_details.get('name')
747 filter_dict["id"] = vdc_network_id[3]
748 if [i.text for i in net_details if i.tag.split('}')[-1] == 'IsShared'][0] == 'true':
749 shared = True
750 else:
751 shared = False
752 filter_dict["shared"] = shared
753 filter_dict["tenant_id"] = vdc_id
754 if int(net_details.get('status')) == 1:
755 filter_dict["admin_state_up"] = True
756 else:
757 filter_dict["admin_state_up"] = False
758 filter_dict["status"] = "ACTIVE"
759 filter_dict["type"] = "bridge"
760 self.logger.debug("Returning {}".format(filter_dict))
761 return filter_dict
762 else:
763 raise vimconn.vimconnNotFoundException("Network {} not found".format(net_id))
764 except Exception as e:
765 self.logger.debug("Error in get_network")
766 self.logger.debug(traceback.format_exc())
767 if isinstance(e, vimconn.vimconnException):
768 raise
769 else:
770 raise vimconn.vimconnNotFoundException("Failed : Network not found {} ".format(e))
771
772 return filter_dict
773
774 def delete_network(self, net_id):
775 """
776 Method Deletes a tenant network from VIM, provide the network id.
777
778 Returns the network identifier or raise an exception
779 """
780
781 # ############# Stub code for SRIOV #################
782 # dvport_group = self.get_dvport_group(net_id)
783 # if dvport_group:
784 # #delete portgroup
785 # status = self.destroy_dvport_group(net_id)
786 # if status:
787 # # Remove vlanID from persistent info
788 # if net_id in self.persistent_info["used_vlanIDs"]:
789 # del self.persistent_info["used_vlanIDs"][net_id]
790 #
791 # return net_id
792
793 vcd_network = self.get_vcd_network(network_uuid=net_id)
794 if vcd_network is not None and vcd_network:
795 if self.delete_network_action(network_uuid=net_id):
796 return net_id
797 else:
798 raise vimconn.vimconnNotFoundException("Network {} not found".format(net_id))
799
800 def refresh_nets_status(self, net_list):
801 """Get the status of the networks
802 Params: the list of network identifiers
803 Returns a dictionary with:
804 net_id: #VIM id of this network
805 status: #Mandatory. Text with one of:
806 # DELETED (not found at vim)
807 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
808 # OTHER (Vim reported other status not understood)
809 # ERROR (VIM indicates an ERROR status)
810 # ACTIVE, INACTIVE, DOWN (admin down),
811 # BUILD (on building process)
812 #
813 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
814 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
815
816 """
817
818 dict_entry = {}
819 try:
820 for net in net_list:
821 errormsg = ''
822 vcd_network = self.get_vcd_network(network_uuid=net)
823 if vcd_network is not None and vcd_network:
824 if vcd_network['status'] == '1':
825 status = 'ACTIVE'
826 else:
827 status = 'DOWN'
828 else:
829 status = 'DELETED'
830 errormsg = 'Network not found.'
831
832 dict_entry[net] = {'status': status, 'error_msg': errormsg,
833 'vim_info': yaml.safe_dump(vcd_network)}
834 except:
835 self.logger.debug("Error in refresh_nets_status")
836 self.logger.debug(traceback.format_exc())
837
838 return dict_entry
839
840 def get_flavor(self, flavor_id):
841 """Obtain flavor details from the VIM
842 Returns the flavor dict details {'id':<>, 'name':<>, other vim specific } #TODO to concrete
843 """
844 if flavor_id not in vimconnector.flavorlist:
845 raise vimconn.vimconnNotFoundException("Flavor not found.")
846 return vimconnector.flavorlist[flavor_id]
847
848 def new_flavor(self, flavor_data):
849 """Adds a tenant flavor to VIM
850 flavor_data contains a dictionary with information, keys:
851 name: flavor name
852 ram: memory (cloud type) in MBytes
853 vpcus: cpus (cloud type)
854 extended: EPA parameters
855 - numas: #items requested in same NUMA
856 memory: number of 1G huge pages memory
857 paired-threads|cores|threads: number of paired hyperthreads, complete cores OR individual threads
858 interfaces: # passthrough(PT) or SRIOV interfaces attached to this numa
859 - name: interface name
860 dedicated: yes|no|yes:sriov; for PT, SRIOV or only one SRIOV for the physical NIC
861 bandwidth: X Gbps; requested guarantee bandwidth
862 vpci: requested virtual PCI address
863 disk: disk size
864 is_public:
865 #TODO to concrete
866 Returns the flavor identifier"""
867
868 # generate a new uuid put to internal dict and return it.
869 self.logger.debug("Creating new flavor - flavor_data: {}".format(flavor_data))
870 new_flavor=flavor_data
871 ram = flavor_data.get(FLAVOR_RAM_KEY, 1024)
872 cpu = flavor_data.get(FLAVOR_VCPUS_KEY, 1)
873 disk = flavor_data.get(FLAVOR_DISK_KEY, 0)
874
875 if not isinstance(ram, int):
876 raise vimconn.vimconnException("Non-integer value for ram")
877 elif not isinstance(cpu, int):
878 raise vimconn.vimconnException("Non-integer value for cpu")
879 elif not isinstance(disk, int):
880 raise vimconn.vimconnException("Non-integer value for disk")
881
882 extended_flv = flavor_data.get("extended")
883 if extended_flv:
884 numas=extended_flv.get("numas")
885 if numas:
886 for numa in numas:
887 #overwrite ram and vcpus
888 if 'memory' in numa:
889 ram = numa['memory']*1024
890 if 'paired-threads' in numa:
891 cpu = numa['paired-threads']*2
892 elif 'cores' in numa:
893 cpu = numa['cores']
894 elif 'threads' in numa:
895 cpu = numa['threads']
896
897 new_flavor[FLAVOR_RAM_KEY] = ram
898 new_flavor[FLAVOR_VCPUS_KEY] = cpu
899 new_flavor[FLAVOR_DISK_KEY] = disk
900 # generate a new uuid put to internal dict and return it.
901 flavor_id = uuid.uuid4()
902 vimconnector.flavorlist[str(flavor_id)] = new_flavor
903 self.logger.debug("Created flavor - {} : {}".format(flavor_id, new_flavor))
904
905 return str(flavor_id)
906
907 def delete_flavor(self, flavor_id):
908 """Deletes a tenant flavor from VIM identify by its id
909
910 Returns the used id or raise an exception
911 """
912 if flavor_id not in vimconnector.flavorlist:
913 raise vimconn.vimconnNotFoundException("Flavor not found.")
914
915 vimconnector.flavorlist.pop(flavor_id, None)
916 return flavor_id
917
918 def new_image(self, image_dict):
919 """
920 Adds a tenant image to VIM
921 Returns:
922 200, image-id if the image is created
923 <0, message if there is an error
924 """
925
926 return self.get_image_id_from_path(image_dict['location'])
927
928 def delete_image(self, image_id):
929 """
930 Deletes a tenant image from VIM
931 Args:
932 image_id is ID of Image to be deleted
933 Return:
934 returns the image identifier in UUID format or raises an exception on error
935 """
936 conn = self.connect_as_admin()
937 if not conn:
938 raise vimconn.vimconnConnectionException("Failed to connect vCD")
939 # Get Catalog details
940 url_list = [self.url, '/api/catalog/', image_id]
941 catalog_herf = ''.join(url_list)
942
943 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
944 'x-vcloud-authorization': conn._session.headers['x-vcloud-authorization']}
945
946 response = self.perform_request(req_type='GET',
947 url=catalog_herf,
948 headers=headers)
949
950 if response.status_code != requests.codes.ok:
951 self.logger.debug("delete_image():GET REST API call {} failed. "\
952 "Return status code {}".format(catalog_herf,
953 response.status_code))
954 raise vimconn.vimconnNotFoundException("Fail to get image {}".format(image_id))
955
956 lxmlroot_respond = lxmlElementTree.fromstring(response.content)
957 namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.iteritems() if prefix}
958 #For python3
959 #namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.items() if prefix}
960 namespaces["xmlns"]= "http://www.vmware.com/vcloud/v1.5"
961
962 catalogItems_section = lxmlroot_respond.find("xmlns:CatalogItems",namespaces)
963 catalogItems = catalogItems_section.iterfind("xmlns:CatalogItem",namespaces)
964 for catalogItem in catalogItems:
965 catalogItem_href = catalogItem.attrib['href']
966
967 response = self.perform_request(req_type='GET',
968 url=catalogItem_href,
969 headers=headers)
970
971 if response.status_code != requests.codes.ok:
972 self.logger.debug("delete_image():GET REST API call {} failed. "\
973 "Return status code {}".format(catalog_herf,
974 response.status_code))
975 raise vimconn.vimconnNotFoundException("Fail to get catalogItem {} for catalog {}".format(
976 catalogItem,
977 image_id))
978
979 lxmlroot_respond = lxmlElementTree.fromstring(response.content)
980 namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.iteritems() if prefix}
981 #For python3
982 #namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.items() if prefix}
983 namespaces["xmlns"]= "http://www.vmware.com/vcloud/v1.5"
984 catalogitem_remove_href = lxmlroot_respond.find("xmlns:Link[@rel='remove']",namespaces).attrib['href']
985
986 #Remove catalogItem
987 response = self.perform_request(req_type='DELETE',
988 url=catalogitem_remove_href,
989 headers=headers)
990 if response.status_code == requests.codes.no_content:
991 self.logger.debug("Deleted Catalog item {}".format(catalogItem))
992 else:
993 raise vimconn.vimconnException("Fail to delete Catalog Item {}".format(catalogItem))
994
995 #Remove catalog
996 url_list = [self.url, '/api/admin/catalog/', image_id]
997 catalog_remove_herf = ''.join(url_list)
998 response = self.perform_request(req_type='DELETE',
999 url=catalog_remove_herf,
1000 headers=headers)
1001
1002 if response.status_code == requests.codes.no_content:
1003 self.logger.debug("Deleted Catalog {}".format(image_id))
1004 return image_id
1005 else:
1006 raise vimconn.vimconnException("Fail to delete Catalog {}".format(image_id))
1007
1008
1009 def catalog_exists(self, catalog_name, catalogs):
1010 """
1011
1012 :param catalog_name:
1013 :param catalogs:
1014 :return:
1015 """
1016 for catalog in catalogs:
1017 if catalog['name'] == catalog_name:
1018 return True
1019 return False
1020
1021 def create_vimcatalog(self, vca=None, catalog_name=None):
1022 """ Create new catalog entry in vCloud director.
1023
1024 Args
1025 vca: vCloud director.
1026 catalog_name catalog that client wish to create. Note no validation done for a name.
1027 Client must make sure that provide valid string representation.
1028
1029 Return (bool) True if catalog created.
1030
1031 """
1032 try:
1033 result = vca.create_catalog(catalog_name, catalog_name)
1034 if result is not None:
1035 return True
1036 catalogs = vca.list_catalogs()
1037 except:
1038 return False
1039 return self.catalog_exists(catalog_name, catalogs)
1040
1041 # noinspection PyIncorrectDocstring
1042 def upload_ovf(self, vca=None, catalog_name=None, image_name=None, media_file_name=None,
1043 description='', progress=False, chunk_bytes=128 * 1024):
1044 """
1045 Uploads a OVF file to a vCloud catalog
1046
1047 :param chunk_bytes:
1048 :param progress:
1049 :param description:
1050 :param image_name:
1051 :param vca:
1052 :param catalog_name: (str): The name of the catalog to upload the media.
1053 :param media_file_name: (str): The name of the local media file to upload.
1054 :return: (bool) True if the media file was successfully uploaded, false otherwise.
1055 """
1056 os.path.isfile(media_file_name)
1057 statinfo = os.stat(media_file_name)
1058
1059 # find a catalog entry where we upload OVF.
1060 # create vApp Template and check the status if vCD able to read OVF it will respond with appropirate
1061 # status change.
1062 # if VCD can parse OVF we upload VMDK file
1063 try:
1064 for catalog in vca.list_catalogs():
1065 if catalog_name != catalog['name']:
1066 continue
1067 catalog_href = "{}/api/catalog/{}/action/upload".format(self.url, catalog['id'])
1068 data = """
1069 <UploadVAppTemplateParams name="{}" xmlns="http://www.vmware.com/vcloud/v1.5" xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1"><Description>{} vApp Template</Description></UploadVAppTemplateParams>
1070 """.format(catalog_name, description)
1071
1072 if self.client:
1073 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
1074 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
1075 headers['Content-Type'] = 'application/vnd.vmware.vcloud.uploadVAppTemplateParams+xml'
1076
1077 response = self.perform_request(req_type='POST',
1078 url=catalog_href,
1079 headers=headers,
1080 data=data)
1081
1082 if response.status_code == requests.codes.created:
1083 catalogItem = XmlElementTree.fromstring(response.content)
1084 entity = [child for child in catalogItem if
1085 child.get("type") == "application/vnd.vmware.vcloud.vAppTemplate+xml"][0]
1086 href = entity.get('href')
1087 template = href
1088
1089 response = self.perform_request(req_type='GET',
1090 url=href,
1091 headers=headers)
1092
1093 if response.status_code == requests.codes.ok:
1094 headers['Content-Type'] = 'Content-Type text/xml'
1095 result = re.search('rel="upload:default"\shref="(.*?\/descriptor.ovf)"',response.content)
1096 if result:
1097 transfer_href = result.group(1)
1098
1099 response = self.perform_request(req_type='PUT',
1100 url=transfer_href,
1101 headers=headers,
1102 data=open(media_file_name, 'rb'))
1103 if response.status_code != requests.codes.ok:
1104 self.logger.debug(
1105 "Failed create vApp template for catalog name {} and image {}".format(catalog_name,
1106 media_file_name))
1107 return False
1108
1109 # TODO fix this with aync block
1110 time.sleep(5)
1111
1112 self.logger.debug("vApp template for catalog name {} and image {}".format(catalog_name, media_file_name))
1113
1114 # uploading VMDK file
1115 # check status of OVF upload and upload remaining files.
1116 response = self.perform_request(req_type='GET',
1117 url=template,
1118 headers=headers)
1119
1120 if response.status_code == requests.codes.ok:
1121 result = re.search('rel="upload:default"\s*href="(.*?vmdk)"',response.content)
1122 if result:
1123 link_href = result.group(1)
1124 # we skip ovf since it already uploaded.
1125 if 'ovf' in link_href:
1126 continue
1127 # The OVF file and VMDK must be in a same directory
1128 head, tail = os.path.split(media_file_name)
1129 file_vmdk = head + '/' + link_href.split("/")[-1]
1130 if not os.path.isfile(file_vmdk):
1131 return False
1132 statinfo = os.stat(file_vmdk)
1133 if statinfo.st_size == 0:
1134 return False
1135 hrefvmdk = link_href
1136
1137 if progress:
1138 widgets = ['Uploading file: ', Percentage(), ' ', Bar(), ' ', ETA(), ' ',
1139 FileTransferSpeed()]
1140 progress_bar = ProgressBar(widgets=widgets, maxval=statinfo.st_size).start()
1141
1142 bytes_transferred = 0
1143 f = open(file_vmdk, 'rb')
1144 while bytes_transferred < statinfo.st_size:
1145 my_bytes = f.read(chunk_bytes)
1146 if len(my_bytes) <= chunk_bytes:
1147 headers['Content-Range'] = 'bytes %s-%s/%s' % (
1148 bytes_transferred, len(my_bytes) - 1, statinfo.st_size)
1149 headers['Content-Length'] = str(len(my_bytes))
1150 response = requests.put(url=hrefvmdk,
1151 headers=headers,
1152 data=my_bytes,
1153 verify=False)
1154 if response.status_code == requests.codes.ok:
1155 bytes_transferred += len(my_bytes)
1156 if progress:
1157 progress_bar.update(bytes_transferred)
1158 else:
1159 self.logger.debug(
1160 'file upload failed with error: [%s] %s' % (response.status_code,
1161 response.content))
1162
1163 f.close()
1164 return False
1165 f.close()
1166 if progress:
1167 progress_bar.finish()
1168 time.sleep(10)
1169 return True
1170 else:
1171 self.logger.debug("Failed retrieve vApp template for catalog name {} for OVF {}".
1172 format(catalog_name, media_file_name))
1173 return False
1174 except Exception as exp:
1175 self.logger.debug("Failed while uploading OVF to catalog {} for OVF file {} with Exception {}"
1176 .format(catalog_name,media_file_name, exp))
1177 raise vimconn.vimconnException(
1178 "Failed while uploading OVF to catalog {} for OVF file {} with Exception {}"
1179 .format(catalog_name,media_file_name, exp))
1180
1181 self.logger.debug("Failed retrieve catalog name {} for OVF file {}".format(catalog_name, media_file_name))
1182 return False
1183
1184 def upload_vimimage(self, vca=None, catalog_name=None, media_name=None, medial_file_name=None, progress=False):
1185 """Upload media file"""
1186 # TODO add named parameters for readability
1187
1188 return self.upload_ovf(vca=vca, catalog_name=catalog_name, image_name=media_name.split(".")[0],
1189 media_file_name=medial_file_name, description='medial_file_name', progress=progress)
1190
1191 def validate_uuid4(self, uuid_string=None):
1192 """ Method validate correct format of UUID.
1193
1194 Return: true if string represent valid uuid
1195 """
1196 try:
1197 val = uuid.UUID(uuid_string, version=4)
1198 except ValueError:
1199 return False
1200 return True
1201
1202 def get_catalogid(self, catalog_name=None, catalogs=None):
1203 """ Method check catalog and return catalog ID in UUID format.
1204
1205 Args
1206 catalog_name: catalog name as string
1207 catalogs: list of catalogs.
1208
1209 Return: catalogs uuid
1210 """
1211
1212 for catalog in catalogs:
1213 if catalog['name'] == catalog_name:
1214 catalog_id = catalog['id']
1215 return catalog_id
1216 return None
1217
1218 def get_catalogbyid(self, catalog_uuid=None, catalogs=None):
1219 """ Method check catalog and return catalog name lookup done by catalog UUID.
1220
1221 Args
1222 catalog_name: catalog name as string
1223 catalogs: list of catalogs.
1224
1225 Return: catalogs name or None
1226 """
1227
1228 if not self.validate_uuid4(uuid_string=catalog_uuid):
1229 return None
1230
1231 for catalog in catalogs:
1232 catalog_id = catalog.get('id')
1233 if catalog_id == catalog_uuid:
1234 return catalog.get('name')
1235 return None
1236
1237 def get_catalog_obj(self, catalog_uuid=None, catalogs=None):
1238 """ Method check catalog and return catalog name lookup done by catalog UUID.
1239
1240 Args
1241 catalog_name: catalog name as string
1242 catalogs: list of catalogs.
1243
1244 Return: catalogs name or None
1245 """
1246
1247 if not self.validate_uuid4(uuid_string=catalog_uuid):
1248 return None
1249
1250 for catalog in catalogs:
1251 catalog_id = catalog.get('id')
1252 if catalog_id == catalog_uuid:
1253 return catalog
1254 return None
1255
1256 def get_image_id_from_path(self, path=None, progress=False):
1257 """ Method upload OVF image to vCloud director.
1258
1259 Each OVF image represented as single catalog entry in vcloud director.
1260 The method check for existing catalog entry. The check done by file name without file extension.
1261
1262 if given catalog name already present method will respond with existing catalog uuid otherwise
1263 it will create new catalog entry and upload OVF file to newly created catalog.
1264
1265 If method can't create catalog entry or upload a file it will throw exception.
1266
1267 Method accept boolean flag progress that will output progress bar. It useful method
1268 for standalone upload use case. In case to test large file upload.
1269
1270 Args
1271 path: - valid path to OVF file.
1272 progress - boolean progress bar show progress bar.
1273
1274 Return: if image uploaded correct method will provide image catalog UUID.
1275 """
1276
1277 if not path:
1278 raise vimconn.vimconnException("Image path can't be None.")
1279
1280 if not os.path.isfile(path):
1281 raise vimconn.vimconnException("Can't read file. File not found.")
1282
1283 if not os.access(path, os.R_OK):
1284 raise vimconn.vimconnException("Can't read file. Check file permission to read.")
1285
1286 self.logger.debug("get_image_id_from_path() client requesting {} ".format(path))
1287
1288 dirpath, filename = os.path.split(path)
1289 flname, file_extension = os.path.splitext(path)
1290 if file_extension != '.ovf':
1291 self.logger.debug("Wrong file extension {} connector support only OVF container.".format(file_extension))
1292 raise vimconn.vimconnException("Wrong container. vCloud director supports only OVF.")
1293
1294 catalog_name = os.path.splitext(filename)[0]
1295 catalog_md5_name = hashlib.md5(path).hexdigest()
1296 self.logger.debug("File name {} Catalog Name {} file path {} "
1297 "vdc catalog name {}".format(filename, catalog_name, path, catalog_md5_name))
1298
1299 try:
1300 org,vdc = self.get_vdc_details()
1301 catalogs = org.list_catalogs()
1302 except Exception as exp:
1303 self.logger.debug("Failed get catalogs() with Exception {} ".format(exp))
1304 raise vimconn.vimconnException("Failed get catalogs() with Exception {} ".format(exp))
1305
1306 if len(catalogs) == 0:
1307 self.logger.info("Creating a new catalog entry {} in vcloud director".format(catalog_name))
1308 result = self.create_vimcatalog(org, catalog_md5_name)
1309 if not result:
1310 raise vimconn.vimconnException("Failed create new catalog {} ".format(catalog_md5_name))
1311
1312 result = self.upload_vimimage(vca=org, catalog_name=catalog_md5_name,
1313 media_name=filename, medial_file_name=path, progress=progress)
1314 if not result:
1315 raise vimconn.vimconnException("Failed create vApp template for catalog {} ".format(catalog_name))
1316 return self.get_catalogid(catalog_name, catalogs)
1317 else:
1318 for catalog in catalogs:
1319 # search for existing catalog if we find same name we return ID
1320 # TODO optimize this
1321 if catalog['name'] == catalog_md5_name:
1322 self.logger.debug("Found existing catalog entry for {} "
1323 "catalog id {}".format(catalog_name,
1324 self.get_catalogid(catalog_md5_name, catalogs)))
1325 return self.get_catalogid(catalog_md5_name, catalogs)
1326
1327 # if we didn't find existing catalog we create a new one and upload image.
1328 self.logger.debug("Creating new catalog entry {} - {}".format(catalog_name, catalog_md5_name))
1329 result = self.create_vimcatalog(org, catalog_md5_name)
1330 if not result:
1331 raise vimconn.vimconnException("Failed create new catalog {} ".format(catalog_md5_name))
1332
1333 result = self.upload_vimimage(vca=org, catalog_name=catalog_md5_name,
1334 media_name=filename, medial_file_name=path, progress=progress)
1335 if not result:
1336 raise vimconn.vimconnException("Failed create vApp template for catalog {} ".format(catalog_md5_name))
1337
1338 return self.get_catalogid(catalog_md5_name, org.list_catalogs())
1339
1340 def get_image_list(self, filter_dict={}):
1341 '''Obtain tenant images from VIM
1342 Filter_dict can be:
1343 name: image name
1344 id: image uuid
1345 checksum: image checksum
1346 location: image path
1347 Returns the image list of dictionaries:
1348 [{<the fields at Filter_dict plus some VIM specific>}, ...]
1349 List can be empty
1350 '''
1351
1352 try:
1353 org, vdc = self.get_vdc_details()
1354 image_list = []
1355 catalogs = org.list_catalogs()
1356 if len(catalogs) == 0:
1357 return image_list
1358 else:
1359 for catalog in catalogs:
1360 catalog_uuid = catalog.get('id')
1361 name = catalog.get('name')
1362 filtered_dict = {}
1363 if filter_dict.get("name") and filter_dict["name"] != name:
1364 continue
1365 if filter_dict.get("id") and filter_dict["id"] != catalog_uuid:
1366 continue
1367 filtered_dict ["name"] = name
1368 filtered_dict ["id"] = catalog_uuid
1369 image_list.append(filtered_dict)
1370
1371 self.logger.debug("List of already created catalog items: {}".format(image_list))
1372 return image_list
1373 except Exception as exp:
1374 raise vimconn.vimconnException("Exception occured while retriving catalog items {}".format(exp))
1375
1376 def get_vappid(self, vdc=None, vapp_name=None):
1377 """ Method takes vdc object and vApp name and returns vapp uuid or None
1378
1379 Args:
1380 vdc: The VDC object.
1381 vapp_name: is application vappp name identifier
1382
1383 Returns:
1384 The return vApp name otherwise None
1385 """
1386 if vdc is None or vapp_name is None:
1387 return None
1388 # UUID has following format https://host/api/vApp/vapp-30da58a3-e7c7-4d09-8f68-d4c8201169cf
1389 try:
1390 refs = filter(lambda ref: ref.name == vapp_name and ref.type_ == 'application/vnd.vmware.vcloud.vApp+xml',
1391 vdc.ResourceEntities.ResourceEntity)
1392 #For python3
1393 #refs = [ref for ref in vdc.ResourceEntities.ResourceEntity\
1394 # if ref.name == vapp_name and ref.type_ == 'application/vnd.vmware.vcloud.vApp+xml']
1395 if len(refs) == 1:
1396 return refs[0].href.split("vapp")[1][1:]
1397 except Exception as e:
1398 self.logger.exception(e)
1399 return False
1400 return None
1401
1402 def check_vapp(self, vdc=None, vapp_uuid=None):
1403 """ Method Method returns True or False if vapp deployed in vCloud director
1404
1405 Args:
1406 vca: Connector to VCA
1407 vdc: The VDC object.
1408 vappid: vappid is application identifier
1409
1410 Returns:
1411 The return True if vApp deployed
1412 :param vdc:
1413 :param vapp_uuid:
1414 """
1415 try:
1416 refs = filter(lambda ref:
1417 ref.type_ == 'application/vnd.vmware.vcloud.vApp+xml',
1418 vdc.ResourceEntities.ResourceEntity)
1419 #For python3
1420 #refs = [ref for ref in vdc.ResourceEntities.ResourceEntity\
1421 # if ref.type_ == 'application/vnd.vmware.vcloud.vApp+xml']
1422 for ref in refs:
1423 vappid = ref.href.split("vapp")[1][1:]
1424 # find vapp with respected vapp uuid
1425 if vappid == vapp_uuid:
1426 return True
1427 except Exception as e:
1428 self.logger.exception(e)
1429 return False
1430 return False
1431
1432 def get_namebyvappid(self, vapp_uuid=None):
1433 """Method returns vApp name from vCD and lookup done by vapp_id.
1434
1435 Args:
1436 vapp_uuid: vappid is application identifier
1437
1438 Returns:
1439 The return vApp name otherwise None
1440 """
1441 try:
1442 if self.client and vapp_uuid:
1443 vapp_call = "{}/api/vApp/vapp-{}".format(self.url, vapp_uuid)
1444 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
1445 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
1446
1447 response = self.perform_request(req_type='GET',
1448 url=vapp_call,
1449 headers=headers)
1450 #Retry login if session expired & retry sending request
1451 if response.status_code == 403:
1452 response = self.retry_rest('GET', vapp_call)
1453
1454 tree = XmlElementTree.fromstring(response.content)
1455 return tree.attrib['name']
1456 except Exception as e:
1457 self.logger.exception(e)
1458 return None
1459 return None
1460
1461 def new_vminstance(self, name=None, description="", start=False, image_id=None, flavor_id=None, net_list=[],
1462 cloud_config=None, disk_list=None, availability_zone_index=None, availability_zone_list=None):
1463 """Adds a VM instance to VIM
1464 Params:
1465 'start': (boolean) indicates if VM must start or created in pause mode.
1466 'image_id','flavor_id': image and flavor VIM id to use for the VM
1467 'net_list': list of interfaces, each one is a dictionary with:
1468 'name': (optional) name for the interface.
1469 'net_id': VIM network id where this interface must be connect to. Mandatory for type==virtual
1470 'vpci': (optional) virtual vPCI address to assign at the VM. Can be ignored depending on VIM capabilities
1471 'model': (optional and only have sense for type==virtual) interface model: virtio, e1000, ...
1472 'mac_address': (optional) mac address to assign to this interface
1473 #TODO: CHECK if an optional 'vlan' parameter is needed for VIMs when type if VF and net_id is not provided,
1474 the VLAN tag to be used. In case net_id is provided, the internal network vlan is used for tagging VF
1475 'type': (mandatory) can be one of:
1476 'virtual', in this case always connected to a network of type 'net_type=bridge'
1477 'PCI-PASSTHROUGH' or 'PF' (passthrough): depending on VIM capabilities it can be connected to a data/ptp network ot it
1478 can created unconnected
1479 'SR-IOV' or 'VF' (SRIOV with VLAN tag): same as PF for network connectivity.
1480 'VFnotShared'(SRIOV without VLAN tag) same as PF for network connectivity. VF where no other VFs
1481 are allocated on the same physical NIC
1482 'bw': (optional) only for PF/VF/VFnotShared. Minimal Bandwidth required for the interface in GBPS
1483 'port_security': (optional) If False it must avoid any traffic filtering at this interface. If missing
1484 or True, it must apply the default VIM behaviour
1485 After execution the method will add the key:
1486 'vim_id': must be filled/added by this method with the VIM identifier generated by the VIM for this
1487 interface. 'net_list' is modified
1488 'cloud_config': (optional) dictionary with:
1489 'key-pairs': (optional) list of strings with the public key to be inserted to the default user
1490 'users': (optional) list of users to be inserted, each item is a dict with:
1491 'name': (mandatory) user name,
1492 'key-pairs': (optional) list of strings with the public key to be inserted to the user
1493 'user-data': (optional) can be a string with the text script to be passed directly to cloud-init,
1494 or a list of strings, each one contains a script to be passed, usually with a MIMEmultipart file
1495 'config-files': (optional). List of files to be transferred. Each item is a dict with:
1496 'dest': (mandatory) string with the destination absolute path
1497 'encoding': (optional, by default text). Can be one of:
1498 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
1499 'content' (mandatory): string with the content of the file
1500 'permissions': (optional) string with file permissions, typically octal notation '0644'
1501 'owner': (optional) file owner, string with the format 'owner:group'
1502 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk)
1503 'disk_list': (optional) list with additional disks to the VM. Each item is a dict with:
1504 'image_id': (optional). VIM id of an existing image. If not provided an empty disk must be mounted
1505 'size': (mandatory) string with the size of the disk in GB
1506 availability_zone_index: Index of availability_zone_list to use for this this VM. None if not AV required
1507 availability_zone_list: list of availability zones given by user in the VNFD descriptor. Ignore if
1508 availability_zone_index is None
1509 Returns a tuple with the instance identifier and created_items or raises an exception on error
1510 created_items can be None or a dictionary where this method can include key-values that will be passed to
1511 the method delete_vminstance and action_vminstance. Can be used to store created ports, volumes, etc.
1512 Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same
1513 as not present.
1514 """
1515 self.logger.info("Creating new instance for entry {}".format(name))
1516 self.logger.debug("desc {} boot {} image_id: {} flavor_id: {} net_list: {} cloud_config {} disk_list {} "\
1517 "availability_zone_index {} availability_zone_list {}"\
1518 .format(description, start, image_id, flavor_id, net_list, cloud_config, disk_list,\
1519 availability_zone_index, availability_zone_list))
1520
1521 #new vm name = vmname + tenant_id + uuid
1522 new_vm_name = [name, '-', str(uuid.uuid4())]
1523 vmname_andid = ''.join(new_vm_name)
1524
1525 for net in net_list:
1526 if net['type'] == "SR-IOV" or net['type'] == "PCI-PASSTHROUGH":
1527 raise vimconn.vimconnNotSupportedException(
1528 "Current vCD version does not support type : {}".format(net['type']))
1529
1530 if len(net_list) > 10:
1531 raise vimconn.vimconnNotSupportedException(
1532 "The VM hardware versions 7 and above support upto 10 NICs only")
1533
1534 # if vm already deployed we return existing uuid
1535 # we check for presence of VDC, Catalog entry and Flavor.
1536 org, vdc = self.get_vdc_details()
1537 if vdc is None:
1538 raise vimconn.vimconnNotFoundException(
1539 "new_vminstance(): Failed create vApp {}: (Failed retrieve VDC information)".format(name))
1540 catalogs = org.list_catalogs()
1541 if catalogs is None:
1542 #Retry once, if failed by refreshing token
1543 self.get_token()
1544 org = Org(self.client, resource=self.client.get_org())
1545 catalogs = org.list_catalogs()
1546 if catalogs is None:
1547 raise vimconn.vimconnNotFoundException(
1548 "new_vminstance(): Failed create vApp {}: (Failed retrieve catalogs list)".format(name))
1549
1550 catalog_hash_name = self.get_catalogbyid(catalog_uuid=image_id, catalogs=catalogs)
1551 if catalog_hash_name:
1552 self.logger.info("Found catalog entry {} for image id {}".format(catalog_hash_name, image_id))
1553 else:
1554 raise vimconn.vimconnNotFoundException("new_vminstance(): Failed create vApp {}: "
1555 "(Failed retrieve catalog information {})".format(name, image_id))
1556
1557
1558 # Set vCPU and Memory based on flavor.
1559 vm_cpus = None
1560 vm_memory = None
1561 vm_disk = None
1562 numas = None
1563
1564 if flavor_id is not None:
1565 if flavor_id not in vimconnector.flavorlist:
1566 raise vimconn.vimconnNotFoundException("new_vminstance(): Failed create vApp {}: "
1567 "Failed retrieve flavor information "
1568 "flavor id {}".format(name, flavor_id))
1569 else:
1570 try:
1571 flavor = vimconnector.flavorlist[flavor_id]
1572 vm_cpus = flavor[FLAVOR_VCPUS_KEY]
1573 vm_memory = flavor[FLAVOR_RAM_KEY]
1574 vm_disk = flavor[FLAVOR_DISK_KEY]
1575 extended = flavor.get("extended", None)
1576 if extended:
1577 numas=extended.get("numas", None)
1578
1579 except Exception as exp:
1580 raise vimconn.vimconnException("Corrupted flavor. {}.Exception: {}".format(flavor_id, exp))
1581
1582 # image upload creates template name as catalog name space Template.
1583 templateName = self.get_catalogbyid(catalog_uuid=image_id, catalogs=catalogs)
1584 power_on = 'false'
1585 if start:
1586 power_on = 'true'
1587
1588 # client must provide at least one entry in net_list if not we report error
1589 #If net type is mgmt, then configure it as primary net & use its NIC index as primary NIC
1590 #If no mgmt, then the 1st NN in netlist is considered as primary net.
1591 primary_net = None
1592 primary_netname = None
1593 primary_net_href = None
1594 network_mode = 'bridged'
1595 if net_list is not None and len(net_list) > 0:
1596 for net in net_list:
1597 if 'use' in net and net['use'] == 'mgmt' and not primary_net:
1598 primary_net = net
1599 if primary_net is None:
1600 primary_net = net_list[0]
1601
1602 try:
1603 primary_net_id = primary_net['net_id']
1604 url_list = [self.url, '/api/network/', primary_net_id]
1605 primary_net_href = ''.join(url_list)
1606 network_dict = self.get_vcd_network(network_uuid=primary_net_id)
1607 if 'name' in network_dict:
1608 primary_netname = network_dict['name']
1609
1610 except KeyError:
1611 raise vimconn.vimconnException("Corrupted flavor. {}".format(primary_net))
1612 else:
1613 raise vimconn.vimconnUnexpectedResponse("new_vminstance(): Failed network list is empty.".format(name))
1614
1615 # use: 'data', 'bridge', 'mgmt'
1616 # create vApp. Set vcpu and ram based on flavor id.
1617 try:
1618 vdc_obj = VDC(self.client, resource=org.get_vdc(self.tenant_name))
1619 if not vdc_obj:
1620 raise vimconn.vimconnNotFoundException("new_vminstance(): Failed to get VDC object")
1621
1622 for retry in (1,2):
1623 items = org.get_catalog_item(catalog_hash_name, catalog_hash_name)
1624 catalog_items = [items.attrib]
1625
1626 if len(catalog_items) == 1:
1627 if self.client:
1628 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
1629 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
1630
1631 response = self.perform_request(req_type='GET',
1632 url=catalog_items[0].get('href'),
1633 headers=headers)
1634 catalogItem = XmlElementTree.fromstring(response.content)
1635 entity = [child for child in catalogItem if child.get("type") == "application/vnd.vmware.vcloud.vAppTemplate+xml"][0]
1636 vapp_tempalte_href = entity.get("href")
1637
1638 response = self.perform_request(req_type='GET',
1639 url=vapp_tempalte_href,
1640 headers=headers)
1641 if response.status_code != requests.codes.ok:
1642 self.logger.debug("REST API call {} failed. Return status code {}".format(vapp_tempalte_href,
1643 response.status_code))
1644 else:
1645 result = (response.content).replace("\n"," ")
1646
1647 src = re.search('<Vm goldMaster="false"\sstatus="\d+"\sname="(.*?)"\s'
1648 'id="(\w+:\w+:vm:.*?)"\shref="(.*?)"\s'
1649 'type="application/vnd\.vmware\.vcloud\.vm\+xml',result)
1650 if src:
1651 vm_name = src.group(1)
1652 vm_id = src.group(2)
1653 vm_href = src.group(3)
1654
1655 cpus = re.search('<rasd:Description>Number of Virtual CPUs</.*?>(\d+)</rasd:VirtualQuantity>',result).group(1)
1656 memory_mb = re.search('<rasd:Description>Memory Size</.*?>(\d+)</rasd:VirtualQuantity>',result).group(1)
1657 cores = re.search('<vmw:CoresPerSocket ovf:required.*?>(\d+)</vmw:CoresPerSocket>',result).group(1)
1658
1659 headers['Content-Type'] = 'application/vnd.vmware.vcloud.instantiateVAppTemplateParams+xml'
1660 vdc_id = vdc.get('id').split(':')[-1]
1661 instantiate_vapp_href = "{}/api/vdc/{}/action/instantiateVAppTemplate".format(self.url,
1662 vdc_id)
1663 data = """<?xml version="1.0" encoding="UTF-8"?>
1664 <InstantiateVAppTemplateParams
1665 xmlns="http://www.vmware.com/vcloud/v1.5"
1666 name="{}"
1667 deploy="false"
1668 powerOn="false"
1669 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1670 xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1">
1671 <Description>Vapp instantiation</Description>
1672 <InstantiationParams>
1673 <NetworkConfigSection>
1674 <ovf:Info>Configuration parameters for logical networks</ovf:Info>
1675 <NetworkConfig networkName="{}">
1676 <Configuration>
1677 <ParentNetwork href="{}" />
1678 <FenceMode>bridged</FenceMode>
1679 </Configuration>
1680 </NetworkConfig>
1681 </NetworkConfigSection>
1682 <LeaseSettingsSection
1683 type="application/vnd.vmware.vcloud.leaseSettingsSection+xml">
1684 <ovf:Info>Lease Settings</ovf:Info>
1685 <StorageLeaseInSeconds>172800</StorageLeaseInSeconds>
1686 <StorageLeaseExpiration>2014-04-25T08:08:16.438-07:00</StorageLeaseExpiration>
1687 </LeaseSettingsSection>
1688 </InstantiationParams>
1689 <Source href="{}"/>
1690 <SourcedItem>
1691 <Source href="{}" id="{}" name="{}"
1692 type="application/vnd.vmware.vcloud.vm+xml"/>
1693 <VmGeneralParams>
1694 <NeedsCustomization>false</NeedsCustomization>
1695 </VmGeneralParams>
1696 <InstantiationParams>
1697 <NetworkConnectionSection>
1698 <ovf:Info>Specifies the available VM network connections</ovf:Info>
1699 <NetworkConnection network="{}">
1700 <NetworkConnectionIndex>0</NetworkConnectionIndex>
1701 <IsConnected>true</IsConnected>
1702 <IpAddressAllocationMode>DHCP</IpAddressAllocationMode>
1703 </NetworkConnection>
1704 </NetworkConnectionSection><ovf:VirtualHardwareSection>
1705 <ovf:Info>Virtual hardware requirements</ovf:Info>
1706 <ovf:Item xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData"
1707 xmlns:vmw="http://www.vmware.com/schema/ovf">
1708 <rasd:AllocationUnits>hertz * 10^6</rasd:AllocationUnits>
1709 <rasd:Description>Number of Virtual CPUs</rasd:Description>
1710 <rasd:ElementName xmlns:py="http://codespeak.net/lxml/objectify/pytype" py:pytype="str">{cpu} virtual CPU(s)</rasd:ElementName>
1711 <rasd:InstanceID>4</rasd:InstanceID>
1712 <rasd:Reservation>0</rasd:Reservation>
1713 <rasd:ResourceType>3</rasd:ResourceType>
1714 <rasd:VirtualQuantity xmlns:py="http://codespeak.net/lxml/objectify/pytype" py:pytype="int">{cpu}</rasd:VirtualQuantity>
1715 <rasd:Weight>0</rasd:Weight>
1716 <vmw:CoresPerSocket ovf:required="false">{core}</vmw:CoresPerSocket>
1717 </ovf:Item><ovf:Item xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData">
1718 <rasd:AllocationUnits>byte * 2^20</rasd:AllocationUnits>
1719 <rasd:Description>Memory Size</rasd:Description>
1720 <rasd:ElementName xmlns:py="http://codespeak.net/lxml/objectify/pytype" py:pytype="str">{memory} MB of memory</rasd:ElementName>
1721 <rasd:InstanceID>5</rasd:InstanceID>
1722 <rasd:Reservation>0</rasd:Reservation>
1723 <rasd:ResourceType>4</rasd:ResourceType>
1724 <rasd:VirtualQuantity xmlns:py="http://codespeak.net/lxml/objectify/pytype" py:pytype="int">{memory}</rasd:VirtualQuantity>
1725 <rasd:Weight>0</rasd:Weight>
1726 </ovf:Item>
1727 </ovf:VirtualHardwareSection>
1728 </InstantiationParams>
1729 </SourcedItem>
1730 <AllEULAsAccepted>false</AllEULAsAccepted>
1731 </InstantiateVAppTemplateParams>""".format(vmname_andid,
1732 primary_netname,
1733 primary_net_href,
1734 vapp_tempalte_href,
1735 vm_href,
1736 vm_id,
1737 vm_name,
1738 primary_netname,
1739 cpu=cpus,
1740 core=cores,
1741 memory=memory_mb)
1742
1743 response = self.perform_request(req_type='POST',
1744 url=instantiate_vapp_href,
1745 headers=headers,
1746 data=data)
1747
1748 if response.status_code != 201:
1749 self.logger.error("REST call {} failed reason : {}"\
1750 "status code : {}".format(instantiate_vapp_href,
1751 response.content,
1752 response.status_code))
1753 raise vimconn.vimconnException("new_vminstance(): Failed to create"\
1754 "vAapp {}".format(vmname_andid))
1755 else:
1756 vapptask = self.get_task_from_response(response.content)
1757
1758 if vapptask is None and retry==1:
1759 self.get_token() # Retry getting token
1760 continue
1761 else:
1762 break
1763
1764 if vapptask is None or vapptask is False:
1765 raise vimconn.vimconnUnexpectedResponse(
1766 "new_vminstance(): failed to create vApp {}".format(vmname_andid))
1767
1768 # wait for task to complete
1769 result = self.client.get_task_monitor().wait_for_success(task=vapptask)
1770
1771 if result.get('status') == 'success':
1772 self.logger.debug("new_vminstance(): Sucessfully created Vapp {}".format(vmname_andid))
1773 else:
1774 raise vimconn.vimconnUnexpectedResponse(
1775 "new_vminstance(): failed to create vApp {}".format(vmname_andid))
1776
1777 except Exception as exp:
1778 raise vimconn.vimconnUnexpectedResponse(
1779 "new_vminstance(): failed to create vApp {} with Exception:{}".format(vmname_andid, exp))
1780
1781 # we should have now vapp in undeployed state.
1782 try:
1783 vdc_obj = VDC(self.client, href=vdc.get('href'))
1784 vapp_resource = vdc_obj.get_vapp(vmname_andid)
1785 vapp_uuid = vapp_resource.get('id').split(':')[-1]
1786 vapp = VApp(self.client, resource=vapp_resource)
1787
1788 except Exception as exp:
1789 raise vimconn.vimconnUnexpectedResponse(
1790 "new_vminstance(): Failed to retrieve vApp {} after creation: Exception:{}"
1791 .format(vmname_andid, exp))
1792
1793 if vapp_uuid is None:
1794 raise vimconn.vimconnUnexpectedResponse(
1795 "new_vminstance(): Failed to retrieve vApp {} after creation".format(
1796 vmname_andid))
1797
1798 #Add PCI passthrough/SRIOV configrations
1799 vm_obj = None
1800 pci_devices_info = []
1801 sriov_net_info = []
1802 reserve_memory = False
1803
1804 for net in net_list:
1805 if net["type"] == "PF" or net["type"] == "PCI-PASSTHROUGH":
1806 pci_devices_info.append(net)
1807 elif (net["type"] == "VF" or net["type"] == "SR-IOV" or net["type"] == "VFnotShared") and 'net_id'in net:
1808 sriov_net_info.append(net)
1809
1810 #Add PCI
1811 if len(pci_devices_info) > 0:
1812 self.logger.info("Need to add PCI devices {} into VM {}".format(pci_devices_info,
1813 vmname_andid ))
1814 PCI_devices_status, vm_obj, vcenter_conect = self.add_pci_devices(vapp_uuid,
1815 pci_devices_info,
1816 vmname_andid)
1817 if PCI_devices_status:
1818 self.logger.info("Added PCI devives {} to VM {}".format(
1819 pci_devices_info,
1820 vmname_andid)
1821 )
1822 reserve_memory = True
1823 else:
1824 self.logger.info("Fail to add PCI devives {} to VM {}".format(
1825 pci_devices_info,
1826 vmname_andid)
1827 )
1828
1829 # Modify vm disk
1830 if vm_disk:
1831 #Assuming there is only one disk in ovf and fast provisioning in organization vDC is disabled
1832 result = self.modify_vm_disk(vapp_uuid, vm_disk)
1833 if result :
1834 self.logger.debug("Modified Disk size of VM {} ".format(vmname_andid))
1835
1836 #Add new or existing disks to vApp
1837 if disk_list:
1838 added_existing_disk = False
1839 for disk in disk_list:
1840 if 'device_type' in disk and disk['device_type'] == 'cdrom':
1841 image_id = disk['image_id']
1842 # Adding CD-ROM to VM
1843 # will revisit code once specification ready to support this feature
1844 self.insert_media_to_vm(vapp, image_id)
1845 elif "image_id" in disk and disk["image_id"] is not None:
1846 self.logger.debug("Adding existing disk from image {} to vm {} ".format(
1847 disk["image_id"] , vapp_uuid))
1848 self.add_existing_disk(catalogs=catalogs,
1849 image_id=disk["image_id"],
1850 size = disk["size"],
1851 template_name=templateName,
1852 vapp_uuid=vapp_uuid
1853 )
1854 added_existing_disk = True
1855 else:
1856 #Wait till added existing disk gets reflected into vCD database/API
1857 if added_existing_disk:
1858 time.sleep(5)
1859 added_existing_disk = False
1860 self.add_new_disk(vapp_uuid, disk['size'])
1861
1862 if numas:
1863 # Assigning numa affinity setting
1864 for numa in numas:
1865 if 'paired-threads-id' in numa:
1866 paired_threads_id = numa['paired-threads-id']
1867 self.set_numa_affinity(vapp_uuid, paired_threads_id)
1868
1869 # add NICs & connect to networks in netlist
1870 try:
1871 vdc_obj = VDC(self.client, href=vdc.get('href'))
1872 vapp_resource = vdc_obj.get_vapp(vmname_andid)
1873 vapp = VApp(self.client, resource=vapp_resource)
1874 vapp_id = vapp_resource.get('id').split(':')[-1]
1875
1876 self.logger.info("Removing primary NIC: ")
1877 # First remove all NICs so that NIC properties can be adjusted as needed
1878 self.remove_primary_network_adapter_from_all_vms(vapp)
1879
1880 self.logger.info("Request to connect VM to a network: {}".format(net_list))
1881 primary_nic_index = 0
1882 nicIndex = 0
1883 for net in net_list:
1884 # openmano uses network id in UUID format.
1885 # vCloud Director need a name so we do reverse operation from provided UUID we lookup a name
1886 # [{'use': 'bridge', 'net_id': '527d4bf7-566a-41e7-a9e7-ca3cdd9cef4f', 'type': 'virtual',
1887 # 'vpci': '0000:00:11.0', 'name': 'eth0'}]
1888
1889 if 'net_id' not in net:
1890 continue
1891
1892 #Using net_id as a vim_id i.e. vim interface id, as do not have saperate vim interface id
1893 #Same will be returned in refresh_vms_status() as vim_interface_id
1894 net['vim_id'] = net['net_id'] # Provide the same VIM identifier as the VIM network
1895
1896 interface_net_id = net['net_id']
1897 interface_net_name = self.get_network_name_by_id(network_uuid=interface_net_id)
1898 interface_network_mode = net['use']
1899
1900 if interface_network_mode == 'mgmt':
1901 primary_nic_index = nicIndex
1902
1903 """- POOL (A static IP address is allocated automatically from a pool of addresses.)
1904 - DHCP (The IP address is obtained from a DHCP service.)
1905 - MANUAL (The IP address is assigned manually in the IpAddress element.)
1906 - NONE (No IP addressing mode specified.)"""
1907
1908 if primary_netname is not None:
1909 self.logger.debug("new_vminstance(): Filtering by net name {}".format(interface_net_name))
1910 nets = filter(lambda n: n.get('name') == interface_net_name, self.get_network_list())
1911 #For python3
1912 #nets = [n for n in self.get_network_list() if n.get('name') == interface_net_name]
1913 if len(nets) == 1:
1914 self.logger.info("new_vminstance(): Found requested network: {}".format(nets[0].get('name')))
1915
1916 if interface_net_name != primary_netname:
1917 # connect network to VM - with all DHCP by default
1918 self.logger.info("new_vminstance(): Attaching net {} to vapp".format(interface_net_name))
1919 self.connect_vapp_to_org_vdc_network(vapp_id, nets[0].get('name'))
1920
1921 type_list = ('PF', 'PCI-PASSTHROUGH', 'VF', 'SR-IOV', 'VFnotShared')
1922 if 'type' in net and net['type'] not in type_list:
1923 # fetching nic type from vnf
1924 if 'model' in net:
1925 if net['model'] is not None:
1926 if net['model'].lower() == 'paravirt' or net['model'].lower() == 'virtio':
1927 nic_type = 'VMXNET3'
1928 else:
1929 nic_type = net['model']
1930
1931 self.logger.info("new_vminstance(): adding network adapter "\
1932 "to a network {}".format(nets[0].get('name')))
1933 self.add_network_adapter_to_vms(vapp, nets[0].get('name'),
1934 primary_nic_index,
1935 nicIndex,
1936 net,
1937 nic_type=nic_type)
1938 else:
1939 self.logger.info("new_vminstance(): adding network adapter "\
1940 "to a network {}".format(nets[0].get('name')))
1941 self.add_network_adapter_to_vms(vapp, nets[0].get('name'),
1942 primary_nic_index,
1943 nicIndex,
1944 net)
1945 nicIndex += 1
1946
1947 # cloud-init for ssh-key injection
1948 if cloud_config:
1949 self.cloud_init(vapp,cloud_config)
1950
1951 # ############# Stub code for SRIOV #################
1952 #Add SRIOV
1953 # if len(sriov_net_info) > 0:
1954 # self.logger.info("Need to add SRIOV adapters {} into VM {}".format(sriov_net_info,
1955 # vmname_andid ))
1956 # sriov_status, vm_obj, vcenter_conect = self.add_sriov(vapp_uuid,
1957 # sriov_net_info,
1958 # vmname_andid)
1959 # if sriov_status:
1960 # self.logger.info("Added SRIOV {} to VM {}".format(
1961 # sriov_net_info,
1962 # vmname_andid)
1963 # )
1964 # reserve_memory = True
1965 # else:
1966 # self.logger.info("Fail to add SRIOV {} to VM {}".format(
1967 # sriov_net_info,
1968 # vmname_andid)
1969 # )
1970
1971 # If VM has PCI devices or SRIOV reserve memory for VM
1972 if reserve_memory:
1973 memReserve = vm_obj.config.hardware.memoryMB
1974 spec = vim.vm.ConfigSpec()
1975 spec.memoryAllocation = vim.ResourceAllocationInfo(reservation=memReserve)
1976 task = vm_obj.ReconfigVM_Task(spec=spec)
1977 if task:
1978 result = self.wait_for_vcenter_task(task, vcenter_conect)
1979 self.logger.info("Reserved memory {} MB for "
1980 "VM VM status: {}".format(str(memReserve), result))
1981 else:
1982 self.logger.info("Fail to reserved memory {} to VM {}".format(
1983 str(memReserve), str(vm_obj)))
1984
1985 self.logger.debug("new_vminstance(): starting power on vApp {} ".format(vmname_andid))
1986
1987 poweron_task = self.power_on_vapp(vapp_id, vmname_andid)
1988 result = self.client.get_task_monitor().wait_for_success(task=poweron_task)
1989 if result.get('status') == 'success':
1990 self.logger.info("new_vminstance(): Successfully power on "\
1991 "vApp {}".format(vmname_andid))
1992 else:
1993 self.logger.error("new_vminstance(): failed to power on vApp "\
1994 "{}".format(vmname_andid))
1995
1996 except Exception as exp :
1997 # it might be a case if specific mandatory entry in dict is empty or some other pyVcloud exception
1998 self.logger.error("new_vminstance(): Failed create new vm instance {} with exception {}"
1999 .format(name, exp))
2000 raise vimconn.vimconnException("new_vminstance(): Failed create new vm instance {} with exception {}"
2001 .format(name, exp))
2002
2003 # check if vApp deployed and if that the case return vApp UUID otherwise -1
2004 wait_time = 0
2005 vapp_uuid = None
2006 while wait_time <= MAX_WAIT_TIME:
2007 try:
2008 vapp_resource = vdc_obj.get_vapp(vmname_andid)
2009 vapp = VApp(self.client, resource=vapp_resource)
2010 except Exception as exp:
2011 raise vimconn.vimconnUnexpectedResponse(
2012 "new_vminstance(): Failed to retrieve vApp {} after creation: Exception:{}"
2013 .format(vmname_andid, exp))
2014
2015 #if vapp and vapp.me.deployed:
2016 if vapp and vapp_resource.get('deployed') == 'true':
2017 vapp_uuid = vapp_resource.get('id').split(':')[-1]
2018 break
2019 else:
2020 self.logger.debug("new_vminstance(): Wait for vApp {} to deploy".format(name))
2021 time.sleep(INTERVAL_TIME)
2022
2023 wait_time +=INTERVAL_TIME
2024
2025 #SET Affinity Rule for VM
2026 #Pre-requisites: User has created Hosh Groups in vCenter with respective Hosts to be used
2027 #While creating VIM account user has to pass the Host Group names in availability_zone list
2028 #"availability_zone" is a part of VIM "config" parameters
2029 #For example, in VIM config: "availability_zone":["HG_170","HG_174","HG_175"]
2030 #Host groups are referred as availability zones
2031 #With following procedure, deployed VM will be added into a VM group.
2032 #Then A VM to Host Affinity rule will be created using the VM group & Host group.
2033 if(availability_zone_list):
2034 self.logger.debug("Existing Host Groups in VIM {}".format(self.config.get('availability_zone')))
2035 #Admin access required for creating Affinity rules
2036 client = self.connect_as_admin()
2037 if not client:
2038 raise vimconn.vimconnConnectionException("Failed to connect vCD as admin")
2039 else:
2040 self.client = client
2041 if self.client:
2042 headers = {'Accept':'application/*+xml;version=27.0',
2043 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
2044 #Step1: Get provider vdc details from organization
2045 pvdc_href = self.get_pvdc_for_org(self.tenant_name, headers)
2046 if pvdc_href is not None:
2047 #Step2: Found required pvdc, now get resource pool information
2048 respool_href = self.get_resource_pool_details(pvdc_href, headers)
2049 if respool_href is None:
2050 #Raise error if respool_href not found
2051 msg = "new_vminstance():Error in finding resource pool details in pvdc {}"\
2052 .format(pvdc_href)
2053 self.log_message(msg)
2054
2055 #Step3: Verify requested availability zone(hostGroup) is present in vCD
2056 # get availability Zone
2057 vm_az = self.get_vm_availability_zone(availability_zone_index, availability_zone_list)
2058 # check if provided av zone(hostGroup) is present in vCD VIM
2059 status = self.check_availibility_zone(vm_az, respool_href, headers)
2060 if status is False:
2061 msg = "new_vminstance(): Error in finding availability zone(Host Group): {} in "\
2062 "resource pool {} status: {}".format(vm_az,respool_href,status)
2063 self.log_message(msg)
2064 else:
2065 self.logger.debug ("new_vminstance(): Availability zone {} found in VIM".format(vm_az))
2066
2067 #Step4: Find VM group references to create vm group
2068 vmgrp_href = self.find_vmgroup_reference(respool_href, headers)
2069 if vmgrp_href == None:
2070 msg = "new_vminstance(): No reference to VmGroup found in resource pool"
2071 self.log_message(msg)
2072
2073 #Step5: Create a VmGroup with name az_VmGroup
2074 vmgrp_name = vm_az + "_" + name #Formed VM Group name = Host Group name + VM name
2075 status = self.create_vmgroup(vmgrp_name, vmgrp_href, headers)
2076 if status is not True:
2077 msg = "new_vminstance(): Error in creating VM group {}".format(vmgrp_name)
2078 self.log_message(msg)
2079
2080 #VM Group url to add vms to vm group
2081 vmgrpname_url = self.url + "/api/admin/extension/vmGroup/name/"+ vmgrp_name
2082
2083 #Step6: Add VM to VM Group
2084 #Find VM uuid from vapp_uuid
2085 vm_details = self.get_vapp_details_rest(vapp_uuid)
2086 vm_uuid = vm_details['vmuuid']
2087
2088 status = self.add_vm_to_vmgroup(vm_uuid, vmgrpname_url, vmgrp_name, headers)
2089 if status is not True:
2090 msg = "new_vminstance(): Error in adding VM to VM group {}".format(vmgrp_name)
2091 self.log_message(msg)
2092
2093 #Step7: Create VM to Host affinity rule
2094 addrule_href = self.get_add_rule_reference (respool_href, headers)
2095 if addrule_href is None:
2096 msg = "new_vminstance(): Error in finding href to add rule in resource pool: {}"\
2097 .format(respool_href)
2098 self.log_message(msg)
2099
2100 status = self.create_vm_to_host_affinity_rule(addrule_href, vmgrp_name, vm_az, "Affinity", headers)
2101 if status is False:
2102 msg = "new_vminstance(): Error in creating affinity rule for VM {} in Host group {}"\
2103 .format(name, vm_az)
2104 self.log_message(msg)
2105 else:
2106 self.logger.debug("new_vminstance(): Affinity rule created successfully. Added {} in Host group {}"\
2107 .format(name, vm_az))
2108 #Reset token to a normal user to perform other operations
2109 self.get_token()
2110
2111 if vapp_uuid is not None:
2112 return vapp_uuid, None
2113 else:
2114 raise vimconn.vimconnUnexpectedResponse("new_vminstance(): Failed create new vm instance {}".format(name))
2115
2116
2117 def get_vcd_availibility_zones(self,respool_href, headers):
2118 """ Method to find presence of av zone is VIM resource pool
2119
2120 Args:
2121 respool_href - resource pool href
2122 headers - header information
2123
2124 Returns:
2125 vcd_az - list of azone present in vCD
2126 """
2127 vcd_az = []
2128 url=respool_href
2129 resp = self.perform_request(req_type='GET',url=respool_href, headers=headers)
2130
2131 if resp.status_code != requests.codes.ok:
2132 self.logger.debug ("REST API call {} failed. Return status code {}".format(url, resp.status_code))
2133 else:
2134 #Get the href to hostGroups and find provided hostGroup is present in it
2135 resp_xml = XmlElementTree.fromstring(resp.content)
2136 for child in resp_xml:
2137 if 'VMWProviderVdcResourcePool' in child.tag:
2138 for schild in child:
2139 if 'Link' in schild.tag:
2140 if schild.attrib.get('type') == "application/vnd.vmware.admin.vmwHostGroupsType+xml":
2141 hostGroup = schild.attrib.get('href')
2142 hg_resp = self.perform_request(req_type='GET',url=hostGroup, headers=headers)
2143 if hg_resp.status_code != requests.codes.ok:
2144 self.logger.debug ("REST API call {} failed. Return status code {}".format(hostGroup, hg_resp.status_code))
2145 else:
2146 hg_resp_xml = XmlElementTree.fromstring(hg_resp.content)
2147 for hostGroup in hg_resp_xml:
2148 if 'HostGroup' in hostGroup.tag:
2149 #append host group name to the list
2150 vcd_az.append(hostGroup.attrib.get("name"))
2151 return vcd_az
2152
2153
2154 def set_availability_zones(self):
2155 """
2156 Set vim availability zone
2157 """
2158
2159 vim_availability_zones = None
2160 availability_zone = None
2161 if 'availability_zone' in self.config:
2162 vim_availability_zones = self.config.get('availability_zone')
2163 if isinstance(vim_availability_zones, str):
2164 availability_zone = [vim_availability_zones]
2165 elif isinstance(vim_availability_zones, list):
2166 availability_zone = vim_availability_zones
2167 else:
2168 return availability_zone
2169
2170 return availability_zone
2171
2172
2173 def get_vm_availability_zone(self, availability_zone_index, availability_zone_list):
2174 """
2175 Return the availability zone to be used by the created VM.
2176 returns: The VIM availability zone to be used or None
2177 """
2178 if availability_zone_index is None:
2179 if not self.config.get('availability_zone'):
2180 return None
2181 elif isinstance(self.config.get('availability_zone'), str):
2182 return self.config['availability_zone']
2183 else:
2184 return self.config['availability_zone'][0]
2185
2186 vim_availability_zones = self.availability_zone
2187
2188 # check if VIM offer enough availability zones describe in the VNFD
2189 if vim_availability_zones and len(availability_zone_list) <= len(vim_availability_zones):
2190 # check if all the names of NFV AV match VIM AV names
2191 match_by_index = False
2192 for av in availability_zone_list:
2193 if av not in vim_availability_zones:
2194 match_by_index = True
2195 break
2196 if match_by_index:
2197 self.logger.debug("Required Availability zone or Host Group not found in VIM config")
2198 self.logger.debug("Input Availability zone list: {}".format(availability_zone_list))
2199 self.logger.debug("VIM configured Availability zones: {}".format(vim_availability_zones))
2200 self.logger.debug("VIM Availability zones will be used by index")
2201 return vim_availability_zones[availability_zone_index]
2202 else:
2203 return availability_zone_list[availability_zone_index]
2204 else:
2205 raise vimconn.vimconnConflictException("No enough availability zones at VIM for this deployment")
2206
2207
2208 def create_vm_to_host_affinity_rule(self, addrule_href, vmgrpname, hostgrpname, polarity, headers):
2209 """ Method to create VM to Host Affinity rule in vCD
2210
2211 Args:
2212 addrule_href - href to make a POST request
2213 vmgrpname - name of the VM group created
2214 hostgrpnmae - name of the host group created earlier
2215 polarity - Affinity or Anti-affinity (default: Affinity)
2216 headers - headers to make REST call
2217
2218 Returns:
2219 True- if rule is created
2220 False- Failed to create rule due to some error
2221
2222 """
2223 task_status = False
2224 rule_name = polarity + "_" + vmgrpname
2225 payload = """<?xml version="1.0" encoding="UTF-8"?>
2226 <vmext:VMWVmHostAffinityRule
2227 xmlns:vmext="http://www.vmware.com/vcloud/extension/v1.5"
2228 xmlns:vcloud="http://www.vmware.com/vcloud/v1.5"
2229 type="application/vnd.vmware.admin.vmwVmHostAffinityRule+xml">
2230 <vcloud:Name>{}</vcloud:Name>
2231 <vcloud:IsEnabled>true</vcloud:IsEnabled>
2232 <vcloud:IsMandatory>true</vcloud:IsMandatory>
2233 <vcloud:Polarity>{}</vcloud:Polarity>
2234 <vmext:HostGroupName>{}</vmext:HostGroupName>
2235 <vmext:VmGroupName>{}</vmext:VmGroupName>
2236 </vmext:VMWVmHostAffinityRule>""".format(rule_name, polarity, hostgrpname, vmgrpname)
2237
2238 resp = self.perform_request(req_type='POST',url=addrule_href, headers=headers, data=payload)
2239
2240 if resp.status_code != requests.codes.accepted:
2241 self.logger.debug ("REST API call {} failed. Return status code {}".format(addrule_href, resp.status_code))
2242 task_status = False
2243 return task_status
2244 else:
2245 affinity_task = self.get_task_from_response(resp.content)
2246 self.logger.debug ("affinity_task: {}".format(affinity_task))
2247 if affinity_task is None or affinity_task is False:
2248 raise vimconn.vimconnUnexpectedResponse("failed to find affinity task")
2249 # wait for task to complete
2250 result = self.client.get_task_monitor().wait_for_success(task=affinity_task)
2251 if result.get('status') == 'success':
2252 self.logger.debug("Successfully created affinity rule {}".format(rule_name))
2253 return True
2254 else:
2255 raise vimconn.vimconnUnexpectedResponse(
2256 "failed to create affinity rule {}".format(rule_name))
2257
2258
2259 def get_add_rule_reference (self, respool_href, headers):
2260 """ This method finds href to add vm to host affinity rule to vCD
2261
2262 Args:
2263 respool_href- href to resource pool
2264 headers- header information to make REST call
2265
2266 Returns:
2267 None - if no valid href to add rule found or
2268 addrule_href - href to add vm to host affinity rule of resource pool
2269 """
2270 addrule_href = None
2271 resp = self.perform_request(req_type='GET',url=respool_href, headers=headers)
2272
2273 if resp.status_code != requests.codes.ok:
2274 self.logger.debug ("REST API call {} failed. Return status code {}".format(respool_href, resp.status_code))
2275 else:
2276
2277 resp_xml = XmlElementTree.fromstring(resp.content)
2278 for child in resp_xml:
2279 if 'VMWProviderVdcResourcePool' in child.tag:
2280 for schild in child:
2281 if 'Link' in schild.tag:
2282 if schild.attrib.get('type') == "application/vnd.vmware.admin.vmwVmHostAffinityRule+xml" and \
2283 schild.attrib.get('rel') == "add":
2284 addrule_href = schild.attrib.get('href')
2285 break
2286
2287 return addrule_href
2288
2289
2290 def add_vm_to_vmgroup(self, vm_uuid, vmGroupNameURL, vmGroup_name, headers):
2291 """ Method to add deployed VM to newly created VM Group.
2292 This is required to create VM to Host affinity in vCD
2293
2294 Args:
2295 vm_uuid- newly created vm uuid
2296 vmGroupNameURL- URL to VM Group name
2297 vmGroup_name- Name of VM group created
2298 headers- Headers for REST request
2299
2300 Returns:
2301 True- if VM added to VM group successfully
2302 False- if any error encounter
2303 """
2304
2305 addvm_resp = self.perform_request(req_type='GET',url=vmGroupNameURL, headers=headers)#, data=payload)
2306
2307 if addvm_resp.status_code != requests.codes.ok:
2308 self.logger.debug ("REST API call to get VM Group Name url {} failed. Return status code {}"\
2309 .format(vmGroupNameURL, addvm_resp.status_code))
2310 return False
2311 else:
2312 resp_xml = XmlElementTree.fromstring(addvm_resp.content)
2313 for child in resp_xml:
2314 if child.tag.split('}')[1] == 'Link':
2315 if child.attrib.get("rel") == "addVms":
2316 addvmtogrpURL = child.attrib.get("href")
2317
2318 #Get vm details
2319 url_list = [self.url, '/api/vApp/vm-',vm_uuid]
2320 vmdetailsURL = ''.join(url_list)
2321
2322 resp = self.perform_request(req_type='GET',url=vmdetailsURL, headers=headers)
2323
2324 if resp.status_code != requests.codes.ok:
2325 self.logger.debug ("REST API call {} failed. Return status code {}".format(vmdetailsURL, resp.status_code))
2326 return False
2327
2328 #Parse VM details
2329 resp_xml = XmlElementTree.fromstring(resp.content)
2330 if resp_xml.tag.split('}')[1] == "Vm":
2331 vm_id = resp_xml.attrib.get("id")
2332 vm_name = resp_xml.attrib.get("name")
2333 vm_href = resp_xml.attrib.get("href")
2334 #print vm_id, vm_name, vm_href
2335 #Add VM into VMgroup
2336 payload = """<?xml version="1.0" encoding="UTF-8"?>\
2337 <ns2:Vms xmlns:ns2="http://www.vmware.com/vcloud/v1.5" \
2338 xmlns="http://www.vmware.com/vcloud/versions" \
2339 xmlns:ns3="http://schemas.dmtf.org/ovf/envelope/1" \
2340 xmlns:ns4="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData" \
2341 xmlns:ns5="http://schemas.dmtf.org/wbem/wscim/1/common" \
2342 xmlns:ns6="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData" \
2343 xmlns:ns7="http://www.vmware.com/schema/ovf" \
2344 xmlns:ns8="http://schemas.dmtf.org/ovf/environment/1" \
2345 xmlns:ns9="http://www.vmware.com/vcloud/extension/v1.5">\
2346 <ns2:VmReference href="{}" id="{}" name="{}" \
2347 type="application/vnd.vmware.vcloud.vm+xml" />\
2348 </ns2:Vms>""".format(vm_href, vm_id, vm_name)
2349
2350 addvmtogrp_resp = self.perform_request(req_type='POST',url=addvmtogrpURL, headers=headers, data=payload)
2351
2352 if addvmtogrp_resp.status_code != requests.codes.accepted:
2353 self.logger.debug ("REST API call {} failed. Return status code {}".format(addvmtogrpURL, addvmtogrp_resp.status_code))
2354 return False
2355 else:
2356 self.logger.debug ("Done adding VM {} to VMgroup {}".format(vm_name, vmGroup_name))
2357 return True
2358
2359
2360 def create_vmgroup(self, vmgroup_name, vmgroup_href, headers):
2361 """Method to create a VM group in vCD
2362
2363 Args:
2364 vmgroup_name : Name of VM group to be created
2365 vmgroup_href : href for vmgroup
2366 headers- Headers for REST request
2367 """
2368 #POST to add URL with required data
2369 vmgroup_status = False
2370 payload = """<VMWVmGroup xmlns="http://www.vmware.com/vcloud/extension/v1.5" \
2371 xmlns:vcloud_v1.5="http://www.vmware.com/vcloud/v1.5" name="{}">\
2372 <vmCount>1</vmCount>\
2373 </VMWVmGroup>""".format(vmgroup_name)
2374 resp = self.perform_request(req_type='POST',url=vmgroup_href, headers=headers, data=payload)
2375
2376 if resp.status_code != requests.codes.accepted:
2377 self.logger.debug ("REST API call {} failed. Return status code {}".format(vmgroup_href, resp.status_code))
2378 return vmgroup_status
2379 else:
2380 vmgroup_task = self.get_task_from_response(resp.content)
2381 if vmgroup_task is None or vmgroup_task is False:
2382 raise vimconn.vimconnUnexpectedResponse(
2383 "create_vmgroup(): failed to create VM group {}".format(vmgroup_name))
2384
2385 # wait for task to complete
2386 result = self.client.get_task_monitor().wait_for_success(task=vmgroup_task)
2387
2388 if result.get('status') == 'success':
2389 self.logger.debug("create_vmgroup(): Successfully created VM group {}".format(vmgroup_name))
2390 #time.sleep(10)
2391 vmgroup_status = True
2392 return vmgroup_status
2393 else:
2394 raise vimconn.vimconnUnexpectedResponse(\
2395 "create_vmgroup(): failed to create VM group {}".format(vmgroup_name))
2396
2397
2398 def find_vmgroup_reference(self, url, headers):
2399 """ Method to create a new VMGroup which is required to add created VM
2400 Args:
2401 url- resource pool href
2402 headers- header information
2403
2404 Returns:
2405 returns href to VM group to create VM group
2406 """
2407 #Perform GET on resource pool to find 'add' link to create VMGroup
2408 #https://vcd-ip/api/admin/extension/providervdc/<providervdc id>/resourcePools
2409 vmgrp_href = None
2410 resp = self.perform_request(req_type='GET',url=url, headers=headers)
2411
2412 if resp.status_code != requests.codes.ok:
2413 self.logger.debug ("REST API call {} failed. Return status code {}".format(url, resp.status_code))
2414 else:
2415 #Get the href to add vmGroup to vCD
2416 resp_xml = XmlElementTree.fromstring(resp.content)
2417 for child in resp_xml:
2418 if 'VMWProviderVdcResourcePool' in child.tag:
2419 for schild in child:
2420 if 'Link' in schild.tag:
2421 #Find href with type VMGroup and rel with add
2422 if schild.attrib.get('type') == "application/vnd.vmware.admin.vmwVmGroupType+xml"\
2423 and schild.attrib.get('rel') == "add":
2424 vmgrp_href = schild.attrib.get('href')
2425 return vmgrp_href
2426
2427
2428 def check_availibility_zone(self, az, respool_href, headers):
2429 """ Method to verify requested av zone is present or not in provided
2430 resource pool
2431
2432 Args:
2433 az - name of hostgroup (availibility_zone)
2434 respool_href - Resource Pool href
2435 headers - Headers to make REST call
2436 Returns:
2437 az_found - True if availibility_zone is found else False
2438 """
2439 az_found = False
2440 headers['Accept']='application/*+xml;version=27.0'
2441 resp = self.perform_request(req_type='GET',url=respool_href, headers=headers)
2442
2443 if resp.status_code != requests.codes.ok:
2444 self.logger.debug ("REST API call {} failed. Return status code {}".format(respool_href, resp.status_code))
2445 else:
2446 #Get the href to hostGroups and find provided hostGroup is present in it
2447 resp_xml = XmlElementTree.fromstring(resp.content)
2448
2449 for child in resp_xml:
2450 if 'VMWProviderVdcResourcePool' in child.tag:
2451 for schild in child:
2452 if 'Link' in schild.tag:
2453 if schild.attrib.get('type') == "application/vnd.vmware.admin.vmwHostGroupsType+xml":
2454 hostGroup_href = schild.attrib.get('href')
2455 hg_resp = self.perform_request(req_type='GET',url=hostGroup_href, headers=headers)
2456 if hg_resp.status_code != requests.codes.ok:
2457 self.logger.debug ("REST API call {} failed. Return status code {}".format(hostGroup_href, hg_resp.status_code))
2458 else:
2459 hg_resp_xml = XmlElementTree.fromstring(hg_resp.content)
2460 for hostGroup in hg_resp_xml:
2461 if 'HostGroup' in hostGroup.tag:
2462 if hostGroup.attrib.get("name") == az:
2463 az_found = True
2464 break
2465 return az_found
2466
2467
2468 def get_pvdc_for_org(self, org_vdc, headers):
2469 """ This method gets provider vdc references from organisation
2470
2471 Args:
2472 org_vdc - name of the organisation VDC to find pvdc
2473 headers - headers to make REST call
2474
2475 Returns:
2476 None - if no pvdc href found else
2477 pvdc_href - href to pvdc
2478 """
2479
2480 #Get provider VDC references from vCD
2481 pvdc_href = None
2482 #url = '<vcd url>/api/admin/extension/providerVdcReferences'
2483 url_list = [self.url, '/api/admin/extension/providerVdcReferences']
2484 url = ''.join(url_list)
2485
2486 response = self.perform_request(req_type='GET',url=url, headers=headers)
2487 if response.status_code != requests.codes.ok:
2488 self.logger.debug ("REST API call {} failed. Return status code {}"\
2489 .format(url, response.status_code))
2490 else:
2491 xmlroot_response = XmlElementTree.fromstring(response.content)
2492 for child in xmlroot_response:
2493 if 'ProviderVdcReference' in child.tag:
2494 pvdc_href = child.attrib.get('href')
2495 #Get vdcReferences to find org
2496 pvdc_resp = self.perform_request(req_type='GET',url=pvdc_href, headers=headers)
2497 if pvdc_resp.status_code != requests.codes.ok:
2498 raise vimconn.vimconnException("REST API call {} failed. "\
2499 "Return status code {}"\
2500 .format(url, pvdc_resp.status_code))
2501
2502 pvdc_resp_xml = XmlElementTree.fromstring(pvdc_resp.content)
2503 for child in pvdc_resp_xml:
2504 if 'Link' in child.tag:
2505 if child.attrib.get('type') == "application/vnd.vmware.admin.vdcReferences+xml":
2506 vdc_href = child.attrib.get('href')
2507
2508 #Check if provided org is present in vdc
2509 vdc_resp = self.perform_request(req_type='GET',
2510 url=vdc_href,
2511 headers=headers)
2512 if vdc_resp.status_code != requests.codes.ok:
2513 raise vimconn.vimconnException("REST API call {} failed. "\
2514 "Return status code {}"\
2515 .format(url, vdc_resp.status_code))
2516 vdc_resp_xml = XmlElementTree.fromstring(vdc_resp.content)
2517 for child in vdc_resp_xml:
2518 if 'VdcReference' in child.tag:
2519 if child.attrib.get('name') == org_vdc:
2520 return pvdc_href
2521
2522
2523 def get_resource_pool_details(self, pvdc_href, headers):
2524 """ Method to get resource pool information.
2525 Host groups are property of resource group.
2526 To get host groups, we need to GET details of resource pool.
2527
2528 Args:
2529 pvdc_href: href to pvdc details
2530 headers: headers
2531
2532 Returns:
2533 respool_href - Returns href link reference to resource pool
2534 """
2535 respool_href = None
2536 resp = self.perform_request(req_type='GET',url=pvdc_href, headers=headers)
2537
2538 if resp.status_code != requests.codes.ok:
2539 self.logger.debug ("REST API call {} failed. Return status code {}"\
2540 .format(pvdc_href, resp.status_code))
2541 else:
2542 respool_resp_xml = XmlElementTree.fromstring(resp.content)
2543 for child in respool_resp_xml:
2544 if 'Link' in child.tag:
2545 if child.attrib.get('type') == "application/vnd.vmware.admin.vmwProviderVdcResourcePoolSet+xml":
2546 respool_href = child.attrib.get("href")
2547 break
2548 return respool_href
2549
2550
2551 def log_message(self, msg):
2552 """
2553 Method to log error messages related to Affinity rule creation
2554 in new_vminstance & raise Exception
2555 Args :
2556 msg - Error message to be logged
2557
2558 """
2559 #get token to connect vCD as a normal user
2560 self.get_token()
2561 self.logger.debug(msg)
2562 raise vimconn.vimconnException(msg)
2563
2564
2565 ##
2566 ##
2567 ## based on current discussion
2568 ##
2569 ##
2570 ## server:
2571 # created: '2016-09-08T11:51:58'
2572 # description: simple-instance.linux1.1
2573 # flavor: ddc6776e-75a9-11e6-ad5f-0800273e724c
2574 # hostId: e836c036-74e7-11e6-b249-0800273e724c
2575 # image: dde30fe6-75a9-11e6-ad5f-0800273e724c
2576 # status: ACTIVE
2577 # error_msg:
2578 # interfaces: …
2579 #
2580 def get_vminstance(self, vim_vm_uuid=None):
2581 """Returns the VM instance information from VIM"""
2582
2583 self.logger.debug("Client requesting vm instance {} ".format(vim_vm_uuid))
2584
2585 org, vdc = self.get_vdc_details()
2586 if vdc is None:
2587 raise vimconn.vimconnConnectionException(
2588 "Failed to get a reference of VDC for a tenant {}".format(self.tenant_name))
2589
2590 vm_info_dict = self.get_vapp_details_rest(vapp_uuid=vim_vm_uuid)
2591 if not vm_info_dict:
2592 self.logger.debug("get_vminstance(): Failed to get vApp name by UUID {}".format(vim_vm_uuid))
2593 raise vimconn.vimconnNotFoundException("Failed to get vApp name by UUID {}".format(vim_vm_uuid))
2594
2595 status_key = vm_info_dict['status']
2596 error = ''
2597 try:
2598 vm_dict = {'created': vm_info_dict['created'],
2599 'description': vm_info_dict['name'],
2600 'status': vcdStatusCode2manoFormat[int(status_key)],
2601 'hostId': vm_info_dict['vmuuid'],
2602 'error_msg': error,
2603 'vim_info': yaml.safe_dump(vm_info_dict), 'interfaces': []}
2604
2605 if 'interfaces' in vm_info_dict:
2606 vm_dict['interfaces'] = vm_info_dict['interfaces']
2607 else:
2608 vm_dict['interfaces'] = []
2609 except KeyError:
2610 vm_dict = {'created': '',
2611 'description': '',
2612 'status': vcdStatusCode2manoFormat[int(-1)],
2613 'hostId': vm_info_dict['vmuuid'],
2614 'error_msg': "Inconsistency state",
2615 'vim_info': yaml.safe_dump(vm_info_dict), 'interfaces': []}
2616
2617 return vm_dict
2618
2619 def delete_vminstance(self, vm__vim_uuid, created_items=None):
2620 """Method poweroff and remove VM instance from vcloud director network.
2621
2622 Args:
2623 vm__vim_uuid: VM UUID
2624
2625 Returns:
2626 Returns the instance identifier
2627 """
2628
2629 self.logger.debug("Client requesting delete vm instance {} ".format(vm__vim_uuid))
2630
2631 org, vdc = self.get_vdc_details()
2632 vdc_obj = VDC(self.client, href=vdc.get('href'))
2633 if vdc_obj is None:
2634 self.logger.debug("delete_vminstance(): Failed to get a reference of VDC for a tenant {}".format(
2635 self.tenant_name))
2636 raise vimconn.vimconnException(
2637 "delete_vminstance(): Failed to get a reference of VDC for a tenant {}".format(self.tenant_name))
2638
2639 try:
2640 vapp_name = self.get_namebyvappid(vm__vim_uuid)
2641 vapp_resource = vdc_obj.get_vapp(vapp_name)
2642 vapp = VApp(self.client, resource=vapp_resource)
2643 if vapp_name is None:
2644 self.logger.debug("delete_vminstance(): Failed to get vm by given {} vm uuid".format(vm__vim_uuid))
2645 return -1, "delete_vminstance(): Failed to get vm by given {} vm uuid".format(vm__vim_uuid)
2646 else:
2647 self.logger.info("Deleting vApp {} and UUID {}".format(vapp_name, vm__vim_uuid))
2648
2649 # Delete vApp and wait for status change if task executed and vApp is None.
2650
2651 if vapp:
2652 if vapp_resource.get('deployed') == 'true':
2653 self.logger.info("Powering off vApp {}".format(vapp_name))
2654 #Power off vApp
2655 powered_off = False
2656 wait_time = 0
2657 while wait_time <= MAX_WAIT_TIME:
2658 power_off_task = vapp.power_off()
2659 result = self.client.get_task_monitor().wait_for_success(task=power_off_task)
2660
2661 if result.get('status') == 'success':
2662 powered_off = True
2663 break
2664 else:
2665 self.logger.info("Wait for vApp {} to power off".format(vapp_name))
2666 time.sleep(INTERVAL_TIME)
2667
2668 wait_time +=INTERVAL_TIME
2669 if not powered_off:
2670 self.logger.debug("delete_vminstance(): Failed to power off VM instance {} ".format(vm__vim_uuid))
2671 else:
2672 self.logger.info("delete_vminstance(): Powered off VM instance {} ".format(vm__vim_uuid))
2673
2674 #Undeploy vApp
2675 self.logger.info("Undeploy vApp {}".format(vapp_name))
2676 wait_time = 0
2677 undeployed = False
2678 while wait_time <= MAX_WAIT_TIME:
2679 vapp = VApp(self.client, resource=vapp_resource)
2680 if not vapp:
2681 self.logger.debug("delete_vminstance(): Failed to get vm by given {} vm uuid".format(vm__vim_uuid))
2682 return -1, "delete_vminstance(): Failed to get vm by given {} vm uuid".format(vm__vim_uuid)
2683 undeploy_task = vapp.undeploy()
2684
2685 result = self.client.get_task_monitor().wait_for_success(task=undeploy_task)
2686 if result.get('status') == 'success':
2687 undeployed = True
2688 break
2689 else:
2690 self.logger.debug("Wait for vApp {} to undeploy".format(vapp_name))
2691 time.sleep(INTERVAL_TIME)
2692
2693 wait_time +=INTERVAL_TIME
2694
2695 if not undeployed:
2696 self.logger.debug("delete_vminstance(): Failed to undeploy vApp {} ".format(vm__vim_uuid))
2697
2698 # delete vapp
2699 self.logger.info("Start deletion of vApp {} ".format(vapp_name))
2700
2701 if vapp is not None:
2702 wait_time = 0
2703 result = False
2704
2705 while wait_time <= MAX_WAIT_TIME:
2706 vapp = VApp(self.client, resource=vapp_resource)
2707 if not vapp:
2708 self.logger.debug("delete_vminstance(): Failed to get vm by given {} vm uuid".format(vm__vim_uuid))
2709 return -1, "delete_vminstance(): Failed to get vm by given {} vm uuid".format(vm__vim_uuid)
2710
2711 delete_task = vdc_obj.delete_vapp(vapp.name, force=True)
2712
2713 result = self.client.get_task_monitor().wait_for_success(task=delete_task)
2714 if result.get('status') == 'success':
2715 break
2716 else:
2717 self.logger.debug("Wait for vApp {} to delete".format(vapp_name))
2718 time.sleep(INTERVAL_TIME)
2719
2720 wait_time +=INTERVAL_TIME
2721
2722 if result is None:
2723 self.logger.debug("delete_vminstance(): Failed delete uuid {} ".format(vm__vim_uuid))
2724 else:
2725 self.logger.info("Deleted vm instance {} sccessfully".format(vm__vim_uuid))
2726 return vm__vim_uuid
2727 except:
2728 self.logger.debug(traceback.format_exc())
2729 raise vimconn.vimconnException("delete_vminstance(): Failed delete vm instance {}".format(vm__vim_uuid))
2730
2731
2732 def refresh_vms_status(self, vm_list):
2733 """Get the status of the virtual machines and their interfaces/ports
2734 Params: the list of VM identifiers
2735 Returns a dictionary with:
2736 vm_id: #VIM id of this Virtual Machine
2737 status: #Mandatory. Text with one of:
2738 # DELETED (not found at vim)
2739 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
2740 # OTHER (Vim reported other status not understood)
2741 # ERROR (VIM indicates an ERROR status)
2742 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
2743 # CREATING (on building process), ERROR
2744 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
2745 #
2746 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
2747 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
2748 interfaces:
2749 - vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
2750 mac_address: #Text format XX:XX:XX:XX:XX:XX
2751 vim_net_id: #network id where this interface is connected
2752 vim_interface_id: #interface/port VIM id
2753 ip_address: #null, or text with IPv4, IPv6 address
2754 """
2755
2756 self.logger.debug("Client requesting refresh vm status for {} ".format(vm_list))
2757
2758 org,vdc = self.get_vdc_details()
2759 if vdc is None:
2760 raise vimconn.vimconnException("Failed to get a reference of VDC for a tenant {}".format(self.tenant_name))
2761
2762 vms_dict = {}
2763 nsx_edge_list = []
2764 for vmuuid in vm_list:
2765 vapp_name = self.get_namebyvappid(vmuuid)
2766 if vapp_name is not None:
2767
2768 try:
2769 vm_pci_details = self.get_vm_pci_details(vmuuid)
2770 vdc_obj = VDC(self.client, href=vdc.get('href'))
2771 vapp_resource = vdc_obj.get_vapp(vapp_name)
2772 the_vapp = VApp(self.client, resource=vapp_resource)
2773
2774 vm_details = {}
2775 for vm in the_vapp.get_all_vms():
2776 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
2777 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
2778 response = self.perform_request(req_type='GET',
2779 url=vm.get('href'),
2780 headers=headers)
2781
2782 if response.status_code != 200:
2783 self.logger.error("refresh_vms_status : REST call {} failed reason : {}"\
2784 "status code : {}".format(vm.get('href'),
2785 response.content,
2786 response.status_code))
2787 raise vimconn.vimconnException("refresh_vms_status : Failed to get "\
2788 "VM details")
2789 xmlroot = XmlElementTree.fromstring(response.content)
2790
2791
2792 result = response.content.replace("\n"," ")
2793 hdd_match = re.search('vcloud:capacity="(\d+)"\svcloud:storageProfileOverrideVmDefault=',result)
2794 if hdd_match:
2795 hdd_mb = hdd_match.group(1)
2796 vm_details['hdd_mb'] = int(hdd_mb) if hdd_mb else None
2797 cpus_match = re.search('<rasd:Description>Number of Virtual CPUs</.*?>(\d+)</rasd:VirtualQuantity>',result)
2798 if cpus_match:
2799 cpus = cpus_match.group(1)
2800 vm_details['cpus'] = int(cpus) if cpus else None
2801 memory_mb = re.search('<rasd:Description>Memory Size</.*?>(\d+)</rasd:VirtualQuantity>',result).group(1)
2802 vm_details['memory_mb'] = int(memory_mb) if memory_mb else None
2803 vm_details['status'] = vcdStatusCode2manoFormat[int(xmlroot.get('status'))]
2804 vm_details['id'] = xmlroot.get('id')
2805 vm_details['name'] = xmlroot.get('name')
2806 vm_info = [vm_details]
2807 if vm_pci_details:
2808 vm_info[0].update(vm_pci_details)
2809
2810 vm_dict = {'status': vcdStatusCode2manoFormat[int(vapp_resource.get('status'))],
2811 'error_msg': vcdStatusCode2manoFormat[int(vapp_resource.get('status'))],
2812 'vim_info': yaml.safe_dump(vm_info), 'interfaces': []}
2813
2814 # get networks
2815 vm_ip = None
2816 vm_mac = None
2817 networks = re.findall('<NetworkConnection needsCustomization=.*?</NetworkConnection>',result)
2818 for network in networks:
2819 mac_s = re.search('<MACAddress>(.*?)</MACAddress>',network)
2820 vm_mac = mac_s.group(1) if mac_s else None
2821 ip_s = re.search('<IpAddress>(.*?)</IpAddress>',network)
2822 vm_ip = ip_s.group(1) if ip_s else None
2823
2824 if vm_ip is None:
2825 if not nsx_edge_list:
2826 nsx_edge_list = self.get_edge_details()
2827 if nsx_edge_list is None:
2828 raise vimconn.vimconnException("refresh_vms_status:"\
2829 "Failed to get edge details from NSX Manager")
2830 if vm_mac is not None:
2831 vm_ip = self.get_ipaddr_from_NSXedge(nsx_edge_list, vm_mac)
2832
2833 net_s = re.search('network="(.*?)"',network)
2834 network_name = net_s.group(1) if net_s else None
2835
2836 vm_net_id = self.get_network_id_by_name(network_name)
2837 interface = {"mac_address": vm_mac,
2838 "vim_net_id": vm_net_id,
2839 "vim_interface_id": vm_net_id,
2840 "ip_address": vm_ip}
2841
2842 vm_dict["interfaces"].append(interface)
2843
2844 # add a vm to vm dict
2845 vms_dict.setdefault(vmuuid, vm_dict)
2846 self.logger.debug("refresh_vms_status : vm info {}".format(vm_dict))
2847 except Exception as exp:
2848 self.logger.debug("Error in response {}".format(exp))
2849 self.logger.debug(traceback.format_exc())
2850
2851 return vms_dict
2852
2853
2854 def get_edge_details(self):
2855 """Get the NSX edge list from NSX Manager
2856 Returns list of NSX edges
2857 """
2858 edge_list = []
2859 rheaders = {'Content-Type': 'application/xml'}
2860 nsx_api_url = '/api/4.0/edges'
2861
2862 self.logger.debug("Get edge details from NSX Manager {} {}".format(self.nsx_manager, nsx_api_url))
2863
2864 try:
2865 resp = requests.get(self.nsx_manager + nsx_api_url,
2866 auth = (self.nsx_user, self.nsx_password),
2867 verify = False, headers = rheaders)
2868 if resp.status_code == requests.codes.ok:
2869 paged_Edge_List = XmlElementTree.fromstring(resp.text)
2870 for edge_pages in paged_Edge_List:
2871 if edge_pages.tag == 'edgePage':
2872 for edge_summary in edge_pages:
2873 if edge_summary.tag == 'pagingInfo':
2874 for element in edge_summary:
2875 if element.tag == 'totalCount' and element.text == '0':
2876 raise vimconn.vimconnException("get_edge_details: No NSX edges details found: {}"
2877 .format(self.nsx_manager))
2878
2879 if edge_summary.tag == 'edgeSummary':
2880 for element in edge_summary:
2881 if element.tag == 'id':
2882 edge_list.append(element.text)
2883 else:
2884 raise vimconn.vimconnException("get_edge_details: No NSX edge details found: {}"
2885 .format(self.nsx_manager))
2886
2887 if not edge_list:
2888 raise vimconn.vimconnException("get_edge_details: "\
2889 "No NSX edge details found: {}"
2890 .format(self.nsx_manager))
2891 else:
2892 self.logger.debug("get_edge_details: Found NSX edges {}".format(edge_list))
2893 return edge_list
2894 else:
2895 self.logger.debug("get_edge_details: "
2896 "Failed to get NSX edge details from NSX Manager: {}"
2897 .format(resp.content))
2898 return None
2899
2900 except Exception as exp:
2901 self.logger.debug("get_edge_details: "\
2902 "Failed to get NSX edge details from NSX Manager: {}"
2903 .format(exp))
2904 raise vimconn.vimconnException("get_edge_details: "\
2905 "Failed to get NSX edge details from NSX Manager: {}"
2906 .format(exp))
2907
2908
2909 def get_ipaddr_from_NSXedge(self, nsx_edges, mac_address):
2910 """Get IP address details from NSX edges, using the MAC address
2911 PARAMS: nsx_edges : List of NSX edges
2912 mac_address : Find IP address corresponding to this MAC address
2913 Returns: IP address corrresponding to the provided MAC address
2914 """
2915
2916 ip_addr = None
2917 rheaders = {'Content-Type': 'application/xml'}
2918
2919 self.logger.debug("get_ipaddr_from_NSXedge: Finding IP addr from NSX edge")
2920
2921 try:
2922 for edge in nsx_edges:
2923 nsx_api_url = '/api/4.0/edges/'+ edge +'/dhcp/leaseInfo'
2924
2925 resp = requests.get(self.nsx_manager + nsx_api_url,
2926 auth = (self.nsx_user, self.nsx_password),
2927 verify = False, headers = rheaders)
2928
2929 if resp.status_code == requests.codes.ok:
2930 dhcp_leases = XmlElementTree.fromstring(resp.text)
2931 for child in dhcp_leases:
2932 if child.tag == 'dhcpLeaseInfo':
2933 dhcpLeaseInfo = child
2934 for leaseInfo in dhcpLeaseInfo:
2935 for elem in leaseInfo:
2936 if (elem.tag)=='macAddress':
2937 edge_mac_addr = elem.text
2938 if (elem.tag)=='ipAddress':
2939 ip_addr = elem.text
2940 if edge_mac_addr is not None:
2941 if edge_mac_addr == mac_address:
2942 self.logger.debug("Found ip addr {} for mac {} at NSX edge {}"
2943 .format(ip_addr, mac_address,edge))
2944 return ip_addr
2945 else:
2946 self.logger.debug("get_ipaddr_from_NSXedge: "\
2947 "Error occurred while getting DHCP lease info from NSX Manager: {}"
2948 .format(resp.content))
2949
2950 self.logger.debug("get_ipaddr_from_NSXedge: No IP addr found in any NSX edge")
2951 return None
2952
2953 except XmlElementTree.ParseError as Err:
2954 self.logger.debug("ParseError in response from NSX Manager {}".format(Err.message), exc_info=True)
2955
2956
2957 def action_vminstance(self, vm__vim_uuid=None, action_dict=None, created_items={}):
2958 """Send and action over a VM instance from VIM
2959 Returns the vm_id if the action was successfully sent to the VIM"""
2960
2961 self.logger.debug("Received action for vm {} and action dict {}".format(vm__vim_uuid, action_dict))
2962 if vm__vim_uuid is None or action_dict is None:
2963 raise vimconn.vimconnException("Invalid request. VM id or action is None.")
2964
2965 org, vdc = self.get_vdc_details()
2966 if vdc is None:
2967 raise vimconn.vimconnException("Failed to get a reference of VDC for a tenant {}".format(self.tenant_name))
2968
2969 vapp_name = self.get_namebyvappid(vm__vim_uuid)
2970 if vapp_name is None:
2971 self.logger.debug("action_vminstance(): Failed to get vm by given {} vm uuid".format(vm__vim_uuid))
2972 raise vimconn.vimconnException("Failed to get vm by given {} vm uuid".format(vm__vim_uuid))
2973 else:
2974 self.logger.info("Action_vminstance vApp {} and UUID {}".format(vapp_name, vm__vim_uuid))
2975
2976 try:
2977 vdc_obj = VDC(self.client, href=vdc.get('href'))
2978 vapp_resource = vdc_obj.get_vapp(vapp_name)
2979 vapp = VApp(self.client, resource=vapp_resource)
2980 if "start" in action_dict:
2981 self.logger.info("action_vminstance: Power on vApp: {}".format(vapp_name))
2982 poweron_task = self.power_on_vapp(vm__vim_uuid, vapp_name)
2983 result = self.client.get_task_monitor().wait_for_success(task=poweron_task)
2984 self.instance_actions_result("start", result, vapp_name)
2985 elif "rebuild" in action_dict:
2986 self.logger.info("action_vminstance: Rebuild vApp: {}".format(vapp_name))
2987 rebuild_task = vapp.deploy(power_on=True)
2988 result = self.client.get_task_monitor().wait_for_success(task=rebuild_task)
2989 self.instance_actions_result("rebuild", result, vapp_name)
2990 elif "pause" in action_dict:
2991 self.logger.info("action_vminstance: pause vApp: {}".format(vapp_name))
2992 pause_task = vapp.undeploy(action='suspend')
2993 result = self.client.get_task_monitor().wait_for_success(task=pause_task)
2994 self.instance_actions_result("pause", result, vapp_name)
2995 elif "resume" in action_dict:
2996 self.logger.info("action_vminstance: resume vApp: {}".format(vapp_name))
2997 poweron_task = self.power_on_vapp(vm__vim_uuid, vapp_name)
2998 result = self.client.get_task_monitor().wait_for_success(task=poweron_task)
2999 self.instance_actions_result("resume", result, vapp_name)
3000 elif "shutoff" in action_dict or "shutdown" in action_dict:
3001 action_name , value = action_dict.items()[0]
3002 #For python3
3003 #action_name , value = list(action_dict.items())[0]
3004 self.logger.info("action_vminstance: {} vApp: {}".format(action_name, vapp_name))
3005 shutdown_task = vapp.shutdown()
3006 result = self.client.get_task_monitor().wait_for_success(task=shutdown_task)
3007 if action_name == "shutdown":
3008 self.instance_actions_result("shutdown", result, vapp_name)
3009 else:
3010 self.instance_actions_result("shutoff", result, vapp_name)
3011 elif "forceOff" in action_dict:
3012 result = vapp.undeploy(action='powerOff')
3013 self.instance_actions_result("forceOff", result, vapp_name)
3014 elif "reboot" in action_dict:
3015 self.logger.info("action_vminstance: reboot vApp: {}".format(vapp_name))
3016 reboot_task = vapp.reboot()
3017 self.client.get_task_monitor().wait_for_success(task=reboot_task)
3018 else:
3019 raise vimconn.vimconnException("action_vminstance: Invalid action {} or action is None.".format(action_dict))
3020 return vm__vim_uuid
3021 except Exception as exp :
3022 self.logger.debug("action_vminstance: Failed with Exception {}".format(exp))
3023 raise vimconn.vimconnException("action_vminstance: Failed with Exception {}".format(exp))
3024
3025 def instance_actions_result(self, action, result, vapp_name):
3026 if result.get('status') == 'success':
3027 self.logger.info("action_vminstance: Sucessfully {} the vApp: {}".format(action, vapp_name))
3028 else:
3029 self.logger.error("action_vminstance: Failed to {} vApp: {}".format(action, vapp_name))
3030
3031 def get_vminstance_console(self, vm_id, console_type="vnc"):
3032 """
3033 Get a console for the virtual machine
3034 Params:
3035 vm_id: uuid of the VM
3036 console_type, can be:
3037 "novnc" (by default), "xvpvnc" for VNC types,
3038 "rdp-html5" for RDP types, "spice-html5" for SPICE types
3039 Returns dict with the console parameters:
3040 protocol: ssh, ftp, http, https, ...
3041 server: usually ip address
3042 port: the http, ssh, ... port
3043 suffix: extra text, e.g. the http path and query string
3044 """
3045 raise vimconn.vimconnNotImplemented("Should have implemented this")
3046
3047 # NOT USED METHODS in current version
3048
3049 def host_vim2gui(self, host, server_dict):
3050 """Transform host dictionary from VIM format to GUI format,
3051 and append to the server_dict
3052 """
3053 raise vimconn.vimconnNotImplemented("Should have implemented this")
3054
3055 def get_hosts_info(self):
3056 """Get the information of deployed hosts
3057 Returns the hosts content"""
3058 raise vimconn.vimconnNotImplemented("Should have implemented this")
3059
3060 def get_hosts(self, vim_tenant):
3061 """Get the hosts and deployed instances
3062 Returns the hosts content"""
3063 raise vimconn.vimconnNotImplemented("Should have implemented this")
3064
3065 def get_processor_rankings(self):
3066 """Get the processor rankings in the VIM database"""
3067 raise vimconn.vimconnNotImplemented("Should have implemented this")
3068
3069 def new_host(self, host_data):
3070 """Adds a new host to VIM"""
3071 '''Returns status code of the VIM response'''
3072 raise vimconn.vimconnNotImplemented("Should have implemented this")
3073
3074 def new_external_port(self, port_data):
3075 """Adds a external port to VIM"""
3076 '''Returns the port identifier'''
3077 raise vimconn.vimconnNotImplemented("Should have implemented this")
3078
3079 def new_external_network(self, net_name, net_type):
3080 """Adds a external network to VIM (shared)"""
3081 '''Returns the network identifier'''
3082 raise vimconn.vimconnNotImplemented("Should have implemented this")
3083
3084 def connect_port_network(self, port_id, network_id, admin=False):
3085 """Connects a external port to a network"""
3086 '''Returns status code of the VIM response'''
3087 raise vimconn.vimconnNotImplemented("Should have implemented this")
3088
3089 def new_vminstancefromJSON(self, vm_data):
3090 """Adds a VM instance to VIM"""
3091 '''Returns the instance identifier'''
3092 raise vimconn.vimconnNotImplemented("Should have implemented this")
3093
3094 def get_network_name_by_id(self, network_uuid=None):
3095 """Method gets vcloud director network named based on supplied uuid.
3096
3097 Args:
3098 network_uuid: network_id
3099
3100 Returns:
3101 The return network name.
3102 """
3103
3104 if not network_uuid:
3105 return None
3106
3107 try:
3108 org_dict = self.get_org(self.org_uuid)
3109 if 'networks' in org_dict:
3110 org_network_dict = org_dict['networks']
3111 for net_uuid in org_network_dict:
3112 if net_uuid == network_uuid:
3113 return org_network_dict[net_uuid]
3114 except:
3115 self.logger.debug("Exception in get_network_name_by_id")
3116 self.logger.debug(traceback.format_exc())
3117
3118 return None
3119
3120 def get_network_id_by_name(self, network_name=None):
3121 """Method gets vcloud director network uuid based on supplied name.
3122
3123 Args:
3124 network_name: network_name
3125 Returns:
3126 The return network uuid.
3127 network_uuid: network_id
3128 """
3129
3130 if not network_name:
3131 self.logger.debug("get_network_id_by_name() : Network name is empty")
3132 return None
3133
3134 try:
3135 org_dict = self.get_org(self.org_uuid)
3136 if org_dict and 'networks' in org_dict:
3137 org_network_dict = org_dict['networks']
3138 for net_uuid,net_name in org_network_dict.iteritems():
3139 #For python3
3140 #for net_uuid,net_name in org_network_dict.items():
3141 if net_name == network_name:
3142 return net_uuid
3143
3144 except KeyError as exp:
3145 self.logger.debug("get_network_id_by_name() : KeyError- {} ".format(exp))
3146
3147 return None
3148
3149 def list_org_action(self):
3150 """
3151 Method leverages vCloud director and query for available organization for particular user
3152
3153 Args:
3154 vca - is active VCA connection.
3155 vdc_name - is a vdc name that will be used to query vms action
3156
3157 Returns:
3158 The return XML respond
3159 """
3160 url_list = [self.url, '/api/org']
3161 vm_list_rest_call = ''.join(url_list)
3162
3163 if self.client._session:
3164 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
3165 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
3166
3167 response = self.perform_request(req_type='GET',
3168 url=vm_list_rest_call,
3169 headers=headers)
3170
3171 if response.status_code == 403:
3172 response = self.retry_rest('GET', vm_list_rest_call)
3173
3174 if response.status_code == requests.codes.ok:
3175 return response.content
3176
3177 return None
3178
3179 def get_org_action(self, org_uuid=None):
3180 """
3181 Method leverages vCloud director and retrieve available object for organization.
3182
3183 Args:
3184 org_uuid - vCD organization uuid
3185 self.client - is active connection.
3186
3187 Returns:
3188 The return XML respond
3189 """
3190
3191 if org_uuid is None:
3192 return None
3193
3194 url_list = [self.url, '/api/org/', org_uuid]
3195 vm_list_rest_call = ''.join(url_list)
3196
3197 if self.client._session:
3198 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
3199 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
3200
3201 #response = requests.get(vm_list_rest_call, headers=headers, verify=False)
3202 response = self.perform_request(req_type='GET',
3203 url=vm_list_rest_call,
3204 headers=headers)
3205 if response.status_code == 403:
3206 response = self.retry_rest('GET', vm_list_rest_call)
3207
3208 if response.status_code == requests.codes.ok:
3209 return response.content
3210 return None
3211
3212 def get_org(self, org_uuid=None):
3213 """
3214 Method retrieves available organization in vCloud Director
3215
3216 Args:
3217 org_uuid - is a organization uuid.
3218
3219 Returns:
3220 The return dictionary with following key
3221 "network" - for network list under the org
3222 "catalogs" - for network list under the org
3223 "vdcs" - for vdc list under org
3224 """
3225
3226 org_dict = {}
3227
3228 if org_uuid is None:
3229 return org_dict
3230
3231 content = self.get_org_action(org_uuid=org_uuid)
3232 try:
3233 vdc_list = {}
3234 network_list = {}
3235 catalog_list = {}
3236 vm_list_xmlroot = XmlElementTree.fromstring(content)
3237 for child in vm_list_xmlroot:
3238 if child.attrib['type'] == 'application/vnd.vmware.vcloud.vdc+xml':
3239 vdc_list[child.attrib['href'].split("/")[-1:][0]] = child.attrib['name']
3240 org_dict['vdcs'] = vdc_list
3241 if child.attrib['type'] == 'application/vnd.vmware.vcloud.orgNetwork+xml':
3242 network_list[child.attrib['href'].split("/")[-1:][0]] = child.attrib['name']
3243 org_dict['networks'] = network_list
3244 if child.attrib['type'] == 'application/vnd.vmware.vcloud.catalog+xml':
3245 catalog_list[child.attrib['href'].split("/")[-1:][0]] = child.attrib['name']
3246 org_dict['catalogs'] = catalog_list
3247 except:
3248 pass
3249
3250 return org_dict
3251
3252 def get_org_list(self):
3253 """
3254 Method retrieves available organization in vCloud Director
3255
3256 Args:
3257 vca - is active VCA connection.
3258
3259 Returns:
3260 The return dictionary and key for each entry VDC UUID
3261 """
3262
3263 org_dict = {}
3264
3265 content = self.list_org_action()
3266 try:
3267 vm_list_xmlroot = XmlElementTree.fromstring(content)
3268 for vm_xml in vm_list_xmlroot:
3269 if vm_xml.tag.split("}")[1] == 'Org':
3270 org_uuid = vm_xml.attrib['href'].split('/')[-1:]
3271 org_dict[org_uuid[0]] = vm_xml.attrib['name']
3272 except:
3273 pass
3274
3275 return org_dict
3276
3277 def vms_view_action(self, vdc_name=None):
3278 """ Method leverages vCloud director vms query call
3279
3280 Args:
3281 vca - is active VCA connection.
3282 vdc_name - is a vdc name that will be used to query vms action
3283
3284 Returns:
3285 The return XML respond
3286 """
3287 vca = self.connect()
3288 if vdc_name is None:
3289 return None
3290
3291 url_list = [vca.host, '/api/vms/query']
3292 vm_list_rest_call = ''.join(url_list)
3293
3294 if not (not vca.vcloud_session or not vca.vcloud_session.organization):
3295 refs = filter(lambda ref: ref.name == vdc_name and ref.type_ == 'application/vnd.vmware.vcloud.vdc+xml',
3296 vca.vcloud_session.organization.Link)
3297 #For python3
3298 #refs = [ref for ref in vca.vcloud_session.organization.Link if ref.name == vdc_name and\
3299 # ref.type_ == 'application/vnd.vmware.vcloud.vdc+xml']
3300 if len(refs) == 1:
3301 response = Http.get(url=vm_list_rest_call,
3302 headers=vca.vcloud_session.get_vcloud_headers(),
3303 verify=vca.verify,
3304 logger=vca.logger)
3305 if response.status_code == requests.codes.ok:
3306 return response.content
3307
3308 return None
3309
3310 def get_vapp_list(self, vdc_name=None):
3311 """
3312 Method retrieves vApp list deployed vCloud director and returns a dictionary
3313 contains a list of all vapp deployed for queried VDC.
3314 The key for a dictionary is vApp UUID
3315
3316
3317 Args:
3318 vca - is active VCA connection.
3319 vdc_name - is a vdc name that will be used to query vms action
3320
3321 Returns:
3322 The return dictionary and key for each entry vapp UUID
3323 """
3324
3325 vapp_dict = {}
3326 if vdc_name is None:
3327 return vapp_dict
3328
3329 content = self.vms_view_action(vdc_name=vdc_name)
3330 try:
3331 vm_list_xmlroot = XmlElementTree.fromstring(content)
3332 for vm_xml in vm_list_xmlroot:
3333 if vm_xml.tag.split("}")[1] == 'VMRecord':
3334 if vm_xml.attrib['isVAppTemplate'] == 'true':
3335 rawuuid = vm_xml.attrib['container'].split('/')[-1:]
3336 if 'vappTemplate-' in rawuuid[0]:
3337 # vm in format vappTemplate-e63d40e7-4ff5-4c6d-851f-96c1e4da86a5 we remove
3338 # vm and use raw UUID as key
3339 vapp_dict[rawuuid[0][13:]] = vm_xml.attrib
3340 except:
3341 pass
3342
3343 return vapp_dict
3344
3345 def get_vm_list(self, vdc_name=None):
3346 """
3347 Method retrieves VM's list deployed vCloud director. It returns a dictionary
3348 contains a list of all VM's deployed for queried VDC.
3349 The key for a dictionary is VM UUID
3350
3351
3352 Args:
3353 vca - is active VCA connection.
3354 vdc_name - is a vdc name that will be used to query vms action
3355
3356 Returns:
3357 The return dictionary and key for each entry vapp UUID
3358 """
3359 vm_dict = {}
3360
3361 if vdc_name is None:
3362 return vm_dict
3363
3364 content = self.vms_view_action(vdc_name=vdc_name)
3365 try:
3366 vm_list_xmlroot = XmlElementTree.fromstring(content)
3367 for vm_xml in vm_list_xmlroot:
3368 if vm_xml.tag.split("}")[1] == 'VMRecord':
3369 if vm_xml.attrib['isVAppTemplate'] == 'false':
3370 rawuuid = vm_xml.attrib['href'].split('/')[-1:]
3371 if 'vm-' in rawuuid[0]:
3372 # vm in format vm-e63d40e7-4ff5-4c6d-851f-96c1e4da86a5 we remove
3373 # vm and use raw UUID as key
3374 vm_dict[rawuuid[0][3:]] = vm_xml.attrib
3375 except:
3376 pass
3377
3378 return vm_dict
3379
3380 def get_vapp(self, vdc_name=None, vapp_name=None, isuuid=False):
3381 """
3382 Method retrieves VM deployed vCloud director. It returns VM attribute as dictionary
3383 contains a list of all VM's deployed for queried VDC.
3384 The key for a dictionary is VM UUID
3385
3386
3387 Args:
3388 vca - is active VCA connection.
3389 vdc_name - is a vdc name that will be used to query vms action
3390
3391 Returns:
3392 The return dictionary and key for each entry vapp UUID
3393 """
3394 vm_dict = {}
3395 vca = self.connect()
3396 if not vca:
3397 raise vimconn.vimconnConnectionException("self.connect() is failed")
3398
3399 if vdc_name is None:
3400 return vm_dict
3401
3402 content = self.vms_view_action(vdc_name=vdc_name)
3403 try:
3404 vm_list_xmlroot = XmlElementTree.fromstring(content)
3405 for vm_xml in vm_list_xmlroot:
3406 if vm_xml.tag.split("}")[1] == 'VMRecord' and vm_xml.attrib['isVAppTemplate'] == 'false':
3407 # lookup done by UUID
3408 if isuuid:
3409 if vapp_name in vm_xml.attrib['container']:
3410 rawuuid = vm_xml.attrib['href'].split('/')[-1:]
3411 if 'vm-' in rawuuid[0]:
3412 vm_dict[rawuuid[0][3:]] = vm_xml.attrib
3413 break
3414 # lookup done by Name
3415 else:
3416 if vapp_name in vm_xml.attrib['name']:
3417 rawuuid = vm_xml.attrib['href'].split('/')[-1:]
3418 if 'vm-' in rawuuid[0]:
3419 vm_dict[rawuuid[0][3:]] = vm_xml.attrib
3420 break
3421 except:
3422 pass
3423
3424 return vm_dict
3425
3426 def get_network_action(self, network_uuid=None):
3427 """
3428 Method leverages vCloud director and query network based on network uuid
3429
3430 Args:
3431 vca - is active VCA connection.
3432 network_uuid - is a network uuid
3433
3434 Returns:
3435 The return XML respond
3436 """
3437
3438 if network_uuid is None:
3439 return None
3440
3441 url_list = [self.url, '/api/network/', network_uuid]
3442 vm_list_rest_call = ''.join(url_list)
3443
3444 if self.client._session:
3445 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
3446 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
3447
3448 response = self.perform_request(req_type='GET',
3449 url=vm_list_rest_call,
3450 headers=headers)
3451 #Retry login if session expired & retry sending request
3452 if response.status_code == 403:
3453 response = self.retry_rest('GET', vm_list_rest_call)
3454
3455 if response.status_code == requests.codes.ok:
3456 return response.content
3457
3458 return None
3459
3460 def get_vcd_network(self, network_uuid=None):
3461 """
3462 Method retrieves available network from vCloud Director
3463
3464 Args:
3465 network_uuid - is VCD network UUID
3466
3467 Each element serialized as key : value pair
3468
3469 Following keys available for access. network_configuration['Gateway'}
3470 <Configuration>
3471 <IpScopes>
3472 <IpScope>
3473 <IsInherited>true</IsInherited>
3474 <Gateway>172.16.252.100</Gateway>
3475 <Netmask>255.255.255.0</Netmask>
3476 <Dns1>172.16.254.201</Dns1>
3477 <Dns2>172.16.254.202</Dns2>
3478 <DnsSuffix>vmwarelab.edu</DnsSuffix>
3479 <IsEnabled>true</IsEnabled>
3480 <IpRanges>
3481 <IpRange>
3482 <StartAddress>172.16.252.1</StartAddress>
3483 <EndAddress>172.16.252.99</EndAddress>
3484 </IpRange>
3485 </IpRanges>
3486 </IpScope>
3487 </IpScopes>
3488 <FenceMode>bridged</FenceMode>
3489
3490 Returns:
3491 The return dictionary and key for each entry vapp UUID
3492 """
3493
3494 network_configuration = {}
3495 if network_uuid is None:
3496 return network_uuid
3497
3498 try:
3499 content = self.get_network_action(network_uuid=network_uuid)
3500 vm_list_xmlroot = XmlElementTree.fromstring(content)
3501
3502 network_configuration['status'] = vm_list_xmlroot.get("status")
3503 network_configuration['name'] = vm_list_xmlroot.get("name")
3504 network_configuration['uuid'] = vm_list_xmlroot.get("id").split(":")[3]
3505
3506 for child in vm_list_xmlroot:
3507 if child.tag.split("}")[1] == 'IsShared':
3508 network_configuration['isShared'] = child.text.strip()
3509 if child.tag.split("}")[1] == 'Configuration':
3510 for configuration in child.iter():
3511 tagKey = configuration.tag.split("}")[1].strip()
3512 if tagKey != "":
3513 network_configuration[tagKey] = configuration.text.strip()
3514 return network_configuration
3515 except Exception as exp :
3516 self.logger.debug("get_vcd_network: Failed with Exception {}".format(exp))
3517 raise vimconn.vimconnException("get_vcd_network: Failed with Exception {}".format(exp))
3518
3519 return network_configuration
3520
3521 def delete_network_action(self, network_uuid=None):
3522 """
3523 Method delete given network from vCloud director
3524
3525 Args:
3526 network_uuid - is a network uuid that client wish to delete
3527
3528 Returns:
3529 The return None or XML respond or false
3530 """
3531 client = self.connect_as_admin()
3532 if not client:
3533 raise vimconn.vimconnConnectionException("Failed to connect vCD as admin")
3534 if network_uuid is None:
3535 return False
3536
3537 url_list = [self.url, '/api/admin/network/', network_uuid]
3538 vm_list_rest_call = ''.join(url_list)
3539
3540 if client._session:
3541 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
3542 'x-vcloud-authorization': client._session.headers['x-vcloud-authorization']}
3543 response = self.perform_request(req_type='DELETE',
3544 url=vm_list_rest_call,
3545 headers=headers)
3546 if response.status_code == 202:
3547 return True
3548
3549 return False
3550
3551 def create_network(self, network_name=None, net_type='bridge', parent_network_uuid=None,
3552 ip_profile=None, isshared='true'):
3553 """
3554 Method create network in vCloud director
3555
3556 Args:
3557 network_name - is network name to be created.
3558 net_type - can be 'bridge','data','ptp','mgmt'.
3559 ip_profile is a dict containing the IP parameters of the network
3560 isshared - is a boolean
3561 parent_network_uuid - is parent provider vdc network that will be used for mapping.
3562 It optional attribute. by default if no parent network indicate the first available will be used.
3563
3564 Returns:
3565 The return network uuid or return None
3566 """
3567
3568 new_network_name = [network_name, '-', str(uuid.uuid4())]
3569 content = self.create_network_rest(network_name=''.join(new_network_name),
3570 ip_profile=ip_profile,
3571 net_type=net_type,
3572 parent_network_uuid=parent_network_uuid,
3573 isshared=isshared)
3574 if content is None:
3575 self.logger.debug("Failed create network {}.".format(network_name))
3576 return None
3577
3578 try:
3579 vm_list_xmlroot = XmlElementTree.fromstring(content)
3580 vcd_uuid = vm_list_xmlroot.get('id').split(":")
3581 if len(vcd_uuid) == 4:
3582 self.logger.info("Created new network name: {} uuid: {}".format(network_name, vcd_uuid[3]))
3583 return vcd_uuid[3]
3584 except:
3585 self.logger.debug("Failed create network {}".format(network_name))
3586 return None
3587
3588 def create_network_rest(self, network_name=None, net_type='bridge', parent_network_uuid=None,
3589 ip_profile=None, isshared='true'):
3590 """
3591 Method create network in vCloud director
3592
3593 Args:
3594 network_name - is network name to be created.
3595 net_type - can be 'bridge','data','ptp','mgmt'.
3596 ip_profile is a dict containing the IP parameters of the network
3597 isshared - is a boolean
3598 parent_network_uuid - is parent provider vdc network that will be used for mapping.
3599 It optional attribute. by default if no parent network indicate the first available will be used.
3600
3601 Returns:
3602 The return network uuid or return None
3603 """
3604 client_as_admin = self.connect_as_admin()
3605 if not client_as_admin:
3606 raise vimconn.vimconnConnectionException("Failed to connect vCD.")
3607 if network_name is None:
3608 return None
3609
3610 url_list = [self.url, '/api/admin/vdc/', self.tenant_id]
3611 vm_list_rest_call = ''.join(url_list)
3612
3613 if client_as_admin._session:
3614 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
3615 'x-vcloud-authorization': client_as_admin._session.headers['x-vcloud-authorization']}
3616
3617 response = self.perform_request(req_type='GET',
3618 url=vm_list_rest_call,
3619 headers=headers)
3620
3621 provider_network = None
3622 available_networks = None
3623 add_vdc_rest_url = None
3624
3625 if response.status_code != requests.codes.ok:
3626 self.logger.debug("REST API call {} failed. Return status code {}".format(vm_list_rest_call,
3627 response.status_code))
3628 return None
3629 else:
3630 try:
3631 vm_list_xmlroot = XmlElementTree.fromstring(response.content)
3632 for child in vm_list_xmlroot:
3633 if child.tag.split("}")[1] == 'ProviderVdcReference':
3634 provider_network = child.attrib.get('href')
3635 # application/vnd.vmware.admin.providervdc+xml
3636 if child.tag.split("}")[1] == 'Link':
3637 if child.attrib.get('type') == 'application/vnd.vmware.vcloud.orgVdcNetwork+xml' \
3638 and child.attrib.get('rel') == 'add':
3639 add_vdc_rest_url = child.attrib.get('href')
3640 except:
3641 self.logger.debug("Failed parse respond for rest api call {}".format(vm_list_rest_call))
3642 self.logger.debug("Respond body {}".format(response.content))
3643 return None
3644
3645 # find pvdc provided available network
3646 response = self.perform_request(req_type='GET',
3647 url=provider_network,
3648 headers=headers)
3649 if response.status_code != requests.codes.ok:
3650 self.logger.debug("REST API call {} failed. Return status code {}".format(vm_list_rest_call,
3651 response.status_code))
3652 return None
3653
3654 if parent_network_uuid is None:
3655 try:
3656 vm_list_xmlroot = XmlElementTree.fromstring(response.content)
3657 for child in vm_list_xmlroot.iter():
3658 if child.tag.split("}")[1] == 'AvailableNetworks':
3659 for networks in child.iter():
3660 # application/vnd.vmware.admin.network+xml
3661 if networks.attrib.get('href') is not None:
3662 available_networks = networks.attrib.get('href')
3663 break
3664 except:
3665 return None
3666
3667 try:
3668 #Configure IP profile of the network
3669 ip_profile = ip_profile if ip_profile is not None else DEFAULT_IP_PROFILE
3670
3671 if 'subnet_address' not in ip_profile or ip_profile['subnet_address'] is None:
3672 subnet_rand = random.randint(0, 255)
3673 ip_base = "192.168.{}.".format(subnet_rand)
3674 ip_profile['subnet_address'] = ip_base + "0/24"
3675 else:
3676 ip_base = ip_profile['subnet_address'].rsplit('.',1)[0] + '.'
3677
3678 if 'gateway_address' not in ip_profile or ip_profile['gateway_address'] is None:
3679 ip_profile['gateway_address']=ip_base + "1"
3680 if 'dhcp_count' not in ip_profile or ip_profile['dhcp_count'] is None:
3681 ip_profile['dhcp_count']=DEFAULT_IP_PROFILE['dhcp_count']
3682 if 'dhcp_enabled' not in ip_profile or ip_profile['dhcp_enabled'] is None:
3683 ip_profile['dhcp_enabled']=DEFAULT_IP_PROFILE['dhcp_enabled']
3684 if 'dhcp_start_address' not in ip_profile or ip_profile['dhcp_start_address'] is None:
3685 ip_profile['dhcp_start_address']=ip_base + "3"
3686 if 'ip_version' not in ip_profile or ip_profile['ip_version'] is None:
3687 ip_profile['ip_version']=DEFAULT_IP_PROFILE['ip_version']
3688 if 'dns_address' not in ip_profile or ip_profile['dns_address'] is None:
3689 ip_profile['dns_address']=ip_base + "2"
3690
3691 gateway_address=ip_profile['gateway_address']
3692 dhcp_count=int(ip_profile['dhcp_count'])
3693 subnet_address=self.convert_cidr_to_netmask(ip_profile['subnet_address'])
3694
3695 if ip_profile['dhcp_enabled']==True:
3696 dhcp_enabled='true'
3697 else:
3698 dhcp_enabled='false'
3699 dhcp_start_address=ip_profile['dhcp_start_address']
3700
3701 #derive dhcp_end_address from dhcp_start_address & dhcp_count
3702 end_ip_int = int(netaddr.IPAddress(dhcp_start_address))
3703 end_ip_int += dhcp_count - 1
3704 dhcp_end_address = str(netaddr.IPAddress(end_ip_int))
3705
3706 ip_version=ip_profile['ip_version']
3707 dns_address=ip_profile['dns_address']
3708 except KeyError as exp:
3709 self.logger.debug("Create Network REST: Key error {}".format(exp))
3710 raise vimconn.vimconnException("Create Network REST: Key error{}".format(exp))
3711
3712 # either use client provided UUID or search for a first available
3713 # if both are not defined we return none
3714 if parent_network_uuid is not None:
3715 provider_network = None
3716 available_networks = None
3717 add_vdc_rest_url = None
3718
3719 url_list = [self.url, '/api/admin/vdc/', self.tenant_id, '/networks']
3720 add_vdc_rest_url = ''.join(url_list)
3721
3722 url_list = [self.url, '/api/admin/network/', parent_network_uuid]
3723 available_networks = ''.join(url_list)
3724
3725 #Creating all networks as Direct Org VDC type networks.
3726 #Unused in case of Underlay (data/ptp) network interface.
3727 fence_mode="bridged"
3728 is_inherited='false'
3729 dns_list = dns_address.split(";")
3730 dns1 = dns_list[0]
3731 dns2_text = ""
3732 if len(dns_list) >= 2:
3733 dns2_text = "\n <Dns2>{}</Dns2>\n".format(dns_list[1])
3734 data = """ <OrgVdcNetwork name="{0:s}" xmlns="http://www.vmware.com/vcloud/v1.5">
3735 <Description>Openmano created</Description>
3736 <Configuration>
3737 <IpScopes>
3738 <IpScope>
3739 <IsInherited>{1:s}</IsInherited>
3740 <Gateway>{2:s}</Gateway>
3741 <Netmask>{3:s}</Netmask>
3742 <Dns1>{4:s}</Dns1>{5:s}
3743 <IsEnabled>{6:s}</IsEnabled>
3744 <IpRanges>
3745 <IpRange>
3746 <StartAddress>{7:s}</StartAddress>
3747 <EndAddress>{8:s}</EndAddress>
3748 </IpRange>
3749 </IpRanges>
3750 </IpScope>
3751 </IpScopes>
3752 <ParentNetwork href="{9:s}"/>
3753 <FenceMode>{10:s}</FenceMode>
3754 </Configuration>
3755 <IsShared>{11:s}</IsShared>
3756 </OrgVdcNetwork> """.format(escape(network_name), is_inherited, gateway_address,
3757 subnet_address, dns1, dns2_text, dhcp_enabled,
3758 dhcp_start_address, dhcp_end_address, available_networks,
3759 fence_mode, isshared)
3760
3761 headers['Content-Type'] = 'application/vnd.vmware.vcloud.orgVdcNetwork+xml'
3762 try:
3763 response = self.perform_request(req_type='POST',
3764 url=add_vdc_rest_url,
3765 headers=headers,
3766 data=data)
3767
3768 if response.status_code != 201:
3769 self.logger.debug("Create Network POST REST API call failed. Return status code {}, Response content: {}"
3770 .format(response.status_code,response.content))
3771 else:
3772 network_task = self.get_task_from_response(response.content)
3773 self.logger.debug("Create Network REST : Waiting for Network creation complete")
3774 time.sleep(5)
3775 result = self.client.get_task_monitor().wait_for_success(task=network_task)
3776 if result.get('status') == 'success':
3777 return response.content
3778 else:
3779 self.logger.debug("create_network_rest task failed. Network Create response : {}"
3780 .format(response.content))
3781 except Exception as exp:
3782 self.logger.debug("create_network_rest : Exception : {} ".format(exp))
3783
3784 return None
3785
3786 def convert_cidr_to_netmask(self, cidr_ip=None):
3787 """
3788 Method sets convert CIDR netmask address to normal IP format
3789 Args:
3790 cidr_ip : CIDR IP address
3791 Returns:
3792 netmask : Converted netmask
3793 """
3794 if cidr_ip is not None:
3795 if '/' in cidr_ip:
3796 network, net_bits = cidr_ip.split('/')
3797 netmask = socket.inet_ntoa(struct.pack(">I", (0xffffffff << (32 - int(net_bits))) & 0xffffffff))
3798 else:
3799 netmask = cidr_ip
3800 return netmask
3801 return None
3802
3803 def get_provider_rest(self, vca=None):
3804 """
3805 Method gets provider vdc view from vcloud director
3806
3807 Args:
3808 network_name - is network name to be created.
3809 parent_network_uuid - is parent provider vdc network that will be used for mapping.
3810 It optional attribute. by default if no parent network indicate the first available will be used.
3811
3812 Returns:
3813 The return xml content of respond or None
3814 """
3815
3816 url_list = [self.url, '/api/admin']
3817 if vca:
3818 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
3819 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
3820 response = self.perform_request(req_type='GET',
3821 url=''.join(url_list),
3822 headers=headers)
3823
3824 if response.status_code == requests.codes.ok:
3825 return response.content
3826 return None
3827
3828 def create_vdc(self, vdc_name=None):
3829
3830 vdc_dict = {}
3831
3832 xml_content = self.create_vdc_from_tmpl_rest(vdc_name=vdc_name)
3833 if xml_content is not None:
3834 try:
3835 task_resp_xmlroot = XmlElementTree.fromstring(xml_content)
3836 for child in task_resp_xmlroot:
3837 if child.tag.split("}")[1] == 'Owner':
3838 vdc_id = child.attrib.get('href').split("/")[-1]
3839 vdc_dict[vdc_id] = task_resp_xmlroot.get('href')
3840 return vdc_dict
3841 except:
3842 self.logger.debug("Respond body {}".format(xml_content))
3843
3844 return None
3845
3846 def create_vdc_from_tmpl_rest(self, vdc_name=None):
3847 """
3848 Method create vdc in vCloud director based on VDC template.
3849 it uses pre-defined template.
3850
3851 Args:
3852 vdc_name - name of a new vdc.
3853
3854 Returns:
3855 The return xml content of respond or None
3856 """
3857 # pre-requesite atleast one vdc template should be available in vCD
3858 self.logger.info("Creating new vdc {}".format(vdc_name))
3859 vca = self.connect_as_admin()
3860 if not vca:
3861 raise vimconn.vimconnConnectionException("Failed to connect vCD")
3862 if vdc_name is None:
3863 return None
3864
3865 url_list = [self.url, '/api/vdcTemplates']
3866 vm_list_rest_call = ''.join(url_list)
3867
3868 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
3869 'x-vcloud-authorization': vca._session.headers['x-vcloud-authorization']}
3870 response = self.perform_request(req_type='GET',
3871 url=vm_list_rest_call,
3872 headers=headers)
3873
3874 # container url to a template
3875 vdc_template_ref = None
3876 try:
3877 vm_list_xmlroot = XmlElementTree.fromstring(response.content)
3878 for child in vm_list_xmlroot:
3879 # application/vnd.vmware.admin.providervdc+xml
3880 # we need find a template from witch we instantiate VDC
3881 if child.tag.split("}")[1] == 'VdcTemplate':
3882 if child.attrib.get('type') == 'application/vnd.vmware.admin.vdcTemplate+xml':
3883 vdc_template_ref = child.attrib.get('href')
3884 except:
3885 self.logger.debug("Failed parse respond for rest api call {}".format(vm_list_rest_call))
3886 self.logger.debug("Respond body {}".format(response.content))
3887 return None
3888
3889 # if we didn't found required pre defined template we return None
3890 if vdc_template_ref is None:
3891 return None
3892
3893 try:
3894 # instantiate vdc
3895 url_list = [self.url, '/api/org/', self.org_uuid, '/action/instantiate']
3896 vm_list_rest_call = ''.join(url_list)
3897 data = """<InstantiateVdcTemplateParams name="{0:s}" xmlns="http://www.vmware.com/vcloud/v1.5">
3898 <Source href="{1:s}"></Source>
3899 <Description>opnemano</Description>
3900 </InstantiateVdcTemplateParams>""".format(vdc_name, vdc_template_ref)
3901
3902 headers['Content-Type'] = 'application/vnd.vmware.vcloud.instantiateVdcTemplateParams+xml'
3903
3904 response = self.perform_request(req_type='POST',
3905 url=vm_list_rest_call,
3906 headers=headers,
3907 data=data)
3908
3909 vdc_task = self.get_task_from_response(response.content)
3910 self.client.get_task_monitor().wait_for_success(task=vdc_task)
3911
3912 # if we all ok we respond with content otherwise by default None
3913 if response.status_code >= 200 and response.status_code < 300:
3914 return response.content
3915 return None
3916 except:
3917 self.logger.debug("Failed parse respond for rest api call {}".format(vm_list_rest_call))
3918 self.logger.debug("Respond body {}".format(response.content))
3919
3920 return None
3921
3922 def create_vdc_rest(self, vdc_name=None):
3923 """
3924 Method create network in vCloud director
3925
3926 Args:
3927 vdc_name - vdc name to be created
3928 Returns:
3929 The return response
3930 """
3931
3932 self.logger.info("Creating new vdc {}".format(vdc_name))
3933
3934 vca = self.connect_as_admin()
3935 if not vca:
3936 raise vimconn.vimconnConnectionException("Failed to connect vCD")
3937 if vdc_name is None:
3938 return None
3939
3940 url_list = [self.url, '/api/admin/org/', self.org_uuid]
3941 vm_list_rest_call = ''.join(url_list)
3942
3943 if vca._session:
3944 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
3945 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
3946 response = self.perform_request(req_type='GET',
3947 url=vm_list_rest_call,
3948 headers=headers)
3949
3950 provider_vdc_ref = None
3951 add_vdc_rest_url = None
3952 available_networks = None
3953
3954 if response.status_code != requests.codes.ok:
3955 self.logger.debug("REST API call {} failed. Return status code {}".format(vm_list_rest_call,
3956 response.status_code))
3957 return None
3958 else:
3959 try:
3960 vm_list_xmlroot = XmlElementTree.fromstring(response.content)
3961 for child in vm_list_xmlroot:
3962 # application/vnd.vmware.admin.providervdc+xml
3963 if child.tag.split("}")[1] == 'Link':
3964 if child.attrib.get('type') == 'application/vnd.vmware.admin.createVdcParams+xml' \
3965 and child.attrib.get('rel') == 'add':
3966 add_vdc_rest_url = child.attrib.get('href')
3967 except:
3968 self.logger.debug("Failed parse respond for rest api call {}".format(vm_list_rest_call))
3969 self.logger.debug("Respond body {}".format(response.content))
3970 return None
3971
3972 response = self.get_provider_rest(vca=vca)
3973 try:
3974 vm_list_xmlroot = XmlElementTree.fromstring(response)
3975 for child in vm_list_xmlroot:
3976 if child.tag.split("}")[1] == 'ProviderVdcReferences':
3977 for sub_child in child:
3978 provider_vdc_ref = sub_child.attrib.get('href')
3979 except:
3980 self.logger.debug("Failed parse respond for rest api call {}".format(vm_list_rest_call))
3981 self.logger.debug("Respond body {}".format(response))
3982 return None
3983
3984 if add_vdc_rest_url is not None and provider_vdc_ref is not None:
3985 data = """ <CreateVdcParams name="{0:s}" xmlns="http://www.vmware.com/vcloud/v1.5"><Description>{1:s}</Description>
3986 <AllocationModel>ReservationPool</AllocationModel>
3987 <ComputeCapacity><Cpu><Units>MHz</Units><Allocated>2048</Allocated><Limit>2048</Limit></Cpu>
3988 <Memory><Units>MB</Units><Allocated>2048</Allocated><Limit>2048</Limit></Memory>
3989 </ComputeCapacity><NicQuota>0</NicQuota><NetworkQuota>100</NetworkQuota>
3990 <VdcStorageProfile><Enabled>true</Enabled><Units>MB</Units><Limit>20480</Limit><Default>true</Default></VdcStorageProfile>
3991 <ProviderVdcReference
3992 name="Main Provider"
3993 href="{2:s}" />
3994 <UsesFastProvisioning>true</UsesFastProvisioning></CreateVdcParams>""".format(escape(vdc_name),
3995 escape(vdc_name),
3996 provider_vdc_ref)
3997
3998 headers['Content-Type'] = 'application/vnd.vmware.admin.createVdcParams+xml'
3999
4000 response = self.perform_request(req_type='POST',
4001 url=add_vdc_rest_url,
4002 headers=headers,
4003 data=data)
4004
4005 # if we all ok we respond with content otherwise by default None
4006 if response.status_code == 201:
4007 return response.content
4008 return None
4009
4010 def get_vapp_details_rest(self, vapp_uuid=None, need_admin_access=False):
4011 """
4012 Method retrieve vapp detail from vCloud director
4013
4014 Args:
4015 vapp_uuid - is vapp identifier.
4016
4017 Returns:
4018 The return network uuid or return None
4019 """
4020
4021 parsed_respond = {}
4022 vca = None
4023
4024 if need_admin_access:
4025 vca = self.connect_as_admin()
4026 else:
4027 vca = self.client
4028
4029 if not vca:
4030 raise vimconn.vimconnConnectionException("Failed to connect vCD")
4031 if vapp_uuid is None:
4032 return None
4033
4034 url_list = [self.url, '/api/vApp/vapp-', vapp_uuid]
4035 get_vapp_restcall = ''.join(url_list)
4036
4037 if vca._session:
4038 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
4039 'x-vcloud-authorization': vca._session.headers['x-vcloud-authorization']}
4040 response = self.perform_request(req_type='GET',
4041 url=get_vapp_restcall,
4042 headers=headers)
4043
4044 if response.status_code == 403:
4045 if need_admin_access == False:
4046 response = self.retry_rest('GET', get_vapp_restcall)
4047
4048 if response.status_code != requests.codes.ok:
4049 self.logger.debug("REST API call {} failed. Return status code {}".format(get_vapp_restcall,
4050 response.status_code))
4051 return parsed_respond
4052
4053 try:
4054 xmlroot_respond = XmlElementTree.fromstring(response.content)
4055 parsed_respond['ovfDescriptorUploaded'] = xmlroot_respond.attrib['ovfDescriptorUploaded']
4056
4057 namespaces = {"vssd":"http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData" ,
4058 'ovf': 'http://schemas.dmtf.org/ovf/envelope/1',
4059 'vmw': 'http://www.vmware.com/schema/ovf',
4060 'vm': 'http://www.vmware.com/vcloud/v1.5',
4061 'rasd':"http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData",
4062 "vmext":"http://www.vmware.com/vcloud/extension/v1.5",
4063 "xmlns":"http://www.vmware.com/vcloud/v1.5"
4064 }
4065
4066 created_section = xmlroot_respond.find('vm:DateCreated', namespaces)
4067 if created_section is not None:
4068 parsed_respond['created'] = created_section.text
4069
4070 network_section = xmlroot_respond.find('vm:NetworkConfigSection/vm:NetworkConfig', namespaces)
4071 if network_section is not None and 'networkName' in network_section.attrib:
4072 parsed_respond['networkname'] = network_section.attrib['networkName']
4073
4074 ipscopes_section = \
4075 xmlroot_respond.find('vm:NetworkConfigSection/vm:NetworkConfig/vm:Configuration/vm:IpScopes',
4076 namespaces)
4077 if ipscopes_section is not None:
4078 for ipscope in ipscopes_section:
4079 for scope in ipscope:
4080 tag_key = scope.tag.split("}")[1]
4081 if tag_key == 'IpRanges':
4082 ip_ranges = scope.getchildren()
4083 for ipblock in ip_ranges:
4084 for block in ipblock:
4085 parsed_respond[block.tag.split("}")[1]] = block.text
4086 else:
4087 parsed_respond[tag_key] = scope.text
4088
4089 # parse children section for other attrib
4090 children_section = xmlroot_respond.find('vm:Children/', namespaces)
4091 if children_section is not None:
4092 parsed_respond['name'] = children_section.attrib['name']
4093 parsed_respond['nestedHypervisorEnabled'] = children_section.attrib['nestedHypervisorEnabled'] \
4094 if "nestedHypervisorEnabled" in children_section.attrib else None
4095 parsed_respond['deployed'] = children_section.attrib['deployed']
4096 parsed_respond['status'] = children_section.attrib['status']
4097 parsed_respond['vmuuid'] = children_section.attrib['id'].split(":")[-1]
4098 network_adapter = children_section.find('vm:NetworkConnectionSection', namespaces)
4099 nic_list = []
4100 for adapters in network_adapter:
4101 adapter_key = adapters.tag.split("}")[1]
4102 if adapter_key == 'PrimaryNetworkConnectionIndex':
4103 parsed_respond['primarynetwork'] = adapters.text
4104 if adapter_key == 'NetworkConnection':
4105 vnic = {}
4106 if 'network' in adapters.attrib:
4107 vnic['network'] = adapters.attrib['network']
4108 for adapter in adapters:
4109 setting_key = adapter.tag.split("}")[1]
4110 vnic[setting_key] = adapter.text
4111 nic_list.append(vnic)
4112
4113 for link in children_section:
4114 if link.tag.split("}")[1] == 'Link' and 'rel' in link.attrib:
4115 if link.attrib['rel'] == 'screen:acquireTicket':
4116 parsed_respond['acquireTicket'] = link.attrib
4117 if link.attrib['rel'] == 'screen:acquireMksTicket':
4118 parsed_respond['acquireMksTicket'] = link.attrib
4119
4120 parsed_respond['interfaces'] = nic_list
4121 vCloud_extension_section = children_section.find('xmlns:VCloudExtension', namespaces)
4122 if vCloud_extension_section is not None:
4123 vm_vcenter_info = {}
4124 vim_info = vCloud_extension_section.find('vmext:VmVimInfo', namespaces)
4125 vmext = vim_info.find('vmext:VmVimObjectRef', namespaces)
4126 if vmext is not None:
4127 vm_vcenter_info["vm_moref_id"] = vmext.find('vmext:MoRef', namespaces).text
4128 parsed_respond["vm_vcenter_info"]= vm_vcenter_info
4129
4130 virtual_hardware_section = children_section.find('ovf:VirtualHardwareSection', namespaces)
4131 vm_virtual_hardware_info = {}
4132 if virtual_hardware_section is not None:
4133 for item in virtual_hardware_section.iterfind('ovf:Item',namespaces):
4134 if item.find("rasd:Description",namespaces).text == "Hard disk":
4135 disk_size = item.find("rasd:HostResource" ,namespaces
4136 ).attrib["{"+namespaces['vm']+"}capacity"]
4137
4138 vm_virtual_hardware_info["disk_size"]= disk_size
4139 break
4140
4141 for link in virtual_hardware_section:
4142 if link.tag.split("}")[1] == 'Link' and 'rel' in link.attrib:
4143 if link.attrib['rel'] == 'edit' and link.attrib['href'].endswith("/disks"):
4144 vm_virtual_hardware_info["disk_edit_href"] = link.attrib['href']
4145 break
4146
4147 parsed_respond["vm_virtual_hardware"]= vm_virtual_hardware_info
4148 except Exception as exp :
4149 self.logger.info("Error occurred calling rest api for getting vApp details {}".format(exp))
4150 return parsed_respond
4151
4152 def acquire_console(self, vm_uuid=None):
4153
4154 if vm_uuid is None:
4155 return None
4156 if self.client._session:
4157 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
4158 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
4159 vm_dict = self.get_vapp_details_rest(vapp_uuid=vm_uuid)
4160 console_dict = vm_dict['acquireTicket']
4161 console_rest_call = console_dict['href']
4162
4163 response = self.perform_request(req_type='POST',
4164 url=console_rest_call,
4165 headers=headers)
4166
4167 if response.status_code == 403:
4168 response = self.retry_rest('POST', console_rest_call)
4169
4170 if response.status_code == requests.codes.ok:
4171 return response.content
4172
4173 return None
4174
4175 def modify_vm_disk(self, vapp_uuid, flavor_disk):
4176 """
4177 Method retrieve vm disk details
4178
4179 Args:
4180 vapp_uuid - is vapp identifier.
4181 flavor_disk - disk size as specified in VNFD (flavor)
4182
4183 Returns:
4184 The return network uuid or return None
4185 """
4186 status = None
4187 try:
4188 #Flavor disk is in GB convert it into MB
4189 flavor_disk = int(flavor_disk) * 1024
4190 vm_details = self.get_vapp_details_rest(vapp_uuid)
4191 if vm_details:
4192 vm_name = vm_details["name"]
4193 self.logger.info("VM: {} flavor_disk :{}".format(vm_name , flavor_disk))
4194
4195 if vm_details and "vm_virtual_hardware" in vm_details:
4196 vm_disk = int(vm_details["vm_virtual_hardware"]["disk_size"])
4197 disk_edit_href = vm_details["vm_virtual_hardware"]["disk_edit_href"]
4198
4199 self.logger.info("VM: {} VM_disk :{}".format(vm_name , vm_disk))
4200
4201 if flavor_disk > vm_disk:
4202 status = self.modify_vm_disk_rest(disk_edit_href ,flavor_disk)
4203 self.logger.info("Modify disk of VM {} from {} to {} MB".format(vm_name,
4204 vm_disk, flavor_disk ))
4205 else:
4206 status = True
4207 self.logger.info("No need to modify disk of VM {}".format(vm_name))
4208
4209 return status
4210 except Exception as exp:
4211 self.logger.info("Error occurred while modifing disk size {}".format(exp))
4212
4213
4214 def modify_vm_disk_rest(self, disk_href , disk_size):
4215 """
4216 Method retrieve modify vm disk size
4217
4218 Args:
4219 disk_href - vCD API URL to GET and PUT disk data
4220 disk_size - disk size as specified in VNFD (flavor)
4221
4222 Returns:
4223 The return network uuid or return None
4224 """
4225 if disk_href is None or disk_size is None:
4226 return None
4227
4228 if self.client._session:
4229 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
4230 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
4231 response = self.perform_request(req_type='GET',
4232 url=disk_href,
4233 headers=headers)
4234
4235 if response.status_code == 403:
4236 response = self.retry_rest('GET', disk_href)
4237
4238 if response.status_code != requests.codes.ok:
4239 self.logger.debug("GET REST API call {} failed. Return status code {}".format(disk_href,
4240 response.status_code))
4241 return None
4242 try:
4243 lxmlroot_respond = lxmlElementTree.fromstring(response.content)
4244 namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.iteritems() if prefix}
4245 #For python3
4246 #namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.items() if prefix}
4247 namespaces["xmlns"]= "http://www.vmware.com/vcloud/v1.5"
4248
4249 for item in lxmlroot_respond.iterfind('xmlns:Item',namespaces):
4250 if item.find("rasd:Description",namespaces).text == "Hard disk":
4251 disk_item = item.find("rasd:HostResource" ,namespaces )
4252 if disk_item is not None:
4253 disk_item.attrib["{"+namespaces['xmlns']+"}capacity"] = str(disk_size)
4254 break
4255
4256 data = lxmlElementTree.tostring(lxmlroot_respond, encoding='utf8', method='xml',
4257 xml_declaration=True)
4258
4259 #Send PUT request to modify disk size
4260 headers['Content-Type'] = 'application/vnd.vmware.vcloud.rasdItemsList+xml; charset=ISO-8859-1'
4261
4262 response = self.perform_request(req_type='PUT',
4263 url=disk_href,
4264 headers=headers,
4265 data=data)
4266 if response.status_code == 403:
4267 add_headers = {'Content-Type': headers['Content-Type']}
4268 response = self.retry_rest('PUT', disk_href, add_headers, data)
4269
4270 if response.status_code != 202:
4271 self.logger.debug("PUT REST API call {} failed. Return status code {}".format(disk_href,
4272 response.status_code))
4273 else:
4274 modify_disk_task = self.get_task_from_response(response.content)
4275 result = self.client.get_task_monitor().wait_for_success(task=modify_disk_task)
4276 if result.get('status') == 'success':
4277 return True
4278 else:
4279 return False
4280 return None
4281
4282 except Exception as exp :
4283 self.logger.info("Error occurred calling rest api for modifing disk size {}".format(exp))
4284 return None
4285
4286 def add_pci_devices(self, vapp_uuid , pci_devices , vmname_andid):
4287 """
4288 Method to attach pci devices to VM
4289
4290 Args:
4291 vapp_uuid - uuid of vApp/VM
4292 pci_devices - pci devices infromation as specified in VNFD (flavor)
4293
4294 Returns:
4295 The status of add pci device task , vm object and
4296 vcenter_conect object
4297 """
4298 vm_obj = None
4299 self.logger.info("Add pci devices {} into vApp {}".format(pci_devices , vapp_uuid))
4300 vcenter_conect, content = self.get_vcenter_content()
4301 vm_moref_id = self.get_vm_moref_id(vapp_uuid)
4302
4303 if vm_moref_id:
4304 try:
4305 no_of_pci_devices = len(pci_devices)
4306 if no_of_pci_devices > 0:
4307 #Get VM and its host
4308 host_obj, vm_obj = self.get_vm_obj(content, vm_moref_id)
4309 self.logger.info("VM {} is currently on host {}".format(vm_obj, host_obj))
4310 if host_obj and vm_obj:
4311 #get PCI devies from host on which vapp is currently installed
4312 avilable_pci_devices = self.get_pci_devices(host_obj, no_of_pci_devices)
4313
4314 if avilable_pci_devices is None:
4315 #find other hosts with active pci devices
4316 new_host_obj , avilable_pci_devices = self.get_host_and_PCIdevices(
4317 content,
4318 no_of_pci_devices
4319 )
4320
4321 if new_host_obj is not None and avilable_pci_devices is not None and len(avilable_pci_devices)> 0:
4322 #Migrate vm to the host where PCI devices are availble
4323 self.logger.info("Relocate VM {} on new host {}".format(vm_obj, new_host_obj))
4324 task = self.relocate_vm(new_host_obj, vm_obj)
4325 if task is not None:
4326 result = self.wait_for_vcenter_task(task, vcenter_conect)
4327 self.logger.info("Migrate VM status: {}".format(result))
4328 host_obj = new_host_obj
4329 else:
4330 self.logger.info("Fail to migrate VM : {}".format(result))
4331 raise vimconn.vimconnNotFoundException(
4332 "Fail to migrate VM : {} to host {}".format(
4333 vmname_andid,
4334 new_host_obj)
4335 )
4336
4337 if host_obj is not None and avilable_pci_devices is not None and len(avilable_pci_devices)> 0:
4338 #Add PCI devices one by one
4339 for pci_device in avilable_pci_devices:
4340 task = self.add_pci_to_vm(host_obj, vm_obj, pci_device)
4341 if task:
4342 status= self.wait_for_vcenter_task(task, vcenter_conect)
4343 if status:
4344 self.logger.info("Added PCI device {} to VM {}".format(pci_device,str(vm_obj)))
4345 else:
4346 self.logger.error("Fail to add PCI device {} to VM {}".format(pci_device,str(vm_obj)))
4347 return True, vm_obj, vcenter_conect
4348 else:
4349 self.logger.error("Currently there is no host with"\
4350 " {} number of avaialble PCI devices required for VM {}".format(
4351 no_of_pci_devices,
4352 vmname_andid)
4353 )
4354 raise vimconn.vimconnNotFoundException(
4355 "Currently there is no host with {} "\
4356 "number of avaialble PCI devices required for VM {}".format(
4357 no_of_pci_devices,
4358 vmname_andid))
4359 else:
4360 self.logger.debug("No infromation about PCI devices {} ",pci_devices)
4361
4362 except vmodl.MethodFault as error:
4363 self.logger.error("Error occurred while adding PCI devices {} ",error)
4364 return None, vm_obj, vcenter_conect
4365
4366 def get_vm_obj(self, content, mob_id):
4367 """
4368 Method to get the vsphere VM object associated with a given morf ID
4369 Args:
4370 vapp_uuid - uuid of vApp/VM
4371 content - vCenter content object
4372 mob_id - mob_id of VM
4373
4374 Returns:
4375 VM and host object
4376 """
4377 vm_obj = None
4378 host_obj = None
4379 try :
4380 container = content.viewManager.CreateContainerView(content.rootFolder,
4381 [vim.VirtualMachine], True
4382 )
4383 for vm in container.view:
4384 mobID = vm._GetMoId()
4385 if mobID == mob_id:
4386 vm_obj = vm
4387 host_obj = vm_obj.runtime.host
4388 break
4389 except Exception as exp:
4390 self.logger.error("Error occurred while finding VM object : {}".format(exp))
4391 return host_obj, vm_obj
4392
4393 def get_pci_devices(self, host, need_devices):
4394 """
4395 Method to get the details of pci devices on given host
4396 Args:
4397 host - vSphere host object
4398 need_devices - number of pci devices needed on host
4399
4400 Returns:
4401 array of pci devices
4402 """
4403 all_devices = []
4404 all_device_ids = []
4405 used_devices_ids = []
4406
4407 try:
4408 if host:
4409 pciPassthruInfo = host.config.pciPassthruInfo
4410 pciDevies = host.hardware.pciDevice
4411
4412 for pci_status in pciPassthruInfo:
4413 if pci_status.passthruActive:
4414 for device in pciDevies:
4415 if device.id == pci_status.id:
4416 all_device_ids.append(device.id)
4417 all_devices.append(device)
4418
4419 #check if devices are in use
4420 avalible_devices = all_devices
4421 for vm in host.vm:
4422 if vm.runtime.powerState == vim.VirtualMachinePowerState.poweredOn:
4423 vm_devices = vm.config.hardware.device
4424 for device in vm_devices:
4425 if type(device) is vim.vm.device.VirtualPCIPassthrough:
4426 if device.backing.id in all_device_ids:
4427 for use_device in avalible_devices:
4428 if use_device.id == device.backing.id:
4429 avalible_devices.remove(use_device)
4430 used_devices_ids.append(device.backing.id)
4431 self.logger.debug("Device {} from devices {}"\
4432 "is in use".format(device.backing.id,
4433 device)
4434 )
4435 if len(avalible_devices) < need_devices:
4436 self.logger.debug("Host {} don't have {} number of active devices".format(host,
4437 need_devices))
4438 self.logger.debug("found only {} devives {}".format(len(avalible_devices),
4439 avalible_devices))
4440 return None
4441 else:
4442 required_devices = avalible_devices[:need_devices]
4443 self.logger.info("Found {} PCI devivces on host {} but required only {}".format(
4444 len(avalible_devices),
4445 host,
4446 need_devices))
4447 self.logger.info("Retruning {} devices as {}".format(need_devices,
4448 required_devices ))
4449 return required_devices
4450
4451 except Exception as exp:
4452 self.logger.error("Error {} occurred while finding pci devices on host: {}".format(exp, host))
4453
4454 return None
4455
4456 def get_host_and_PCIdevices(self, content, need_devices):
4457 """
4458 Method to get the details of pci devices infromation on all hosts
4459
4460 Args:
4461 content - vSphere host object
4462 need_devices - number of pci devices needed on host
4463
4464 Returns:
4465 array of pci devices and host object
4466 """
4467 host_obj = None
4468 pci_device_objs = None
4469 try:
4470 if content:
4471 container = content.viewManager.CreateContainerView(content.rootFolder,
4472 [vim.HostSystem], True)
4473 for host in container.view:
4474 devices = self.get_pci_devices(host, need_devices)
4475 if devices:
4476 host_obj = host
4477 pci_device_objs = devices
4478 break
4479 except Exception as exp:
4480 self.logger.error("Error {} occurred while finding pci devices on host: {}".format(exp, host_obj))
4481
4482 return host_obj,pci_device_objs
4483
4484 def relocate_vm(self, dest_host, vm) :
4485 """
4486 Method to get the relocate VM to new host
4487
4488 Args:
4489 dest_host - vSphere host object
4490 vm - vSphere VM object
4491
4492 Returns:
4493 task object
4494 """
4495 task = None
4496 try:
4497 relocate_spec = vim.vm.RelocateSpec(host=dest_host)
4498 task = vm.Relocate(relocate_spec)
4499 self.logger.info("Migrating {} to destination host {}".format(vm, dest_host))
4500 except Exception as exp:
4501 self.logger.error("Error occurred while relocate VM {} to new host {}: {}".format(
4502 dest_host, vm, exp))
4503 return task
4504
4505 def wait_for_vcenter_task(self, task, actionName='job', hideResult=False):
4506 """
4507 Waits and provides updates on a vSphere task
4508 """
4509 while task.info.state == vim.TaskInfo.State.running:
4510 time.sleep(2)
4511
4512 if task.info.state == vim.TaskInfo.State.success:
4513 if task.info.result is not None and not hideResult:
4514 self.logger.info('{} completed successfully, result: {}'.format(
4515 actionName,
4516 task.info.result))
4517 else:
4518 self.logger.info('Task {} completed successfully.'.format(actionName))
4519 else:
4520 self.logger.error('{} did not complete successfully: {} '.format(
4521 actionName,
4522 task.info.error)
4523 )
4524
4525 return task.info.result
4526
4527 def add_pci_to_vm(self,host_object, vm_object, host_pci_dev):
4528 """
4529 Method to add pci device in given VM
4530
4531 Args:
4532 host_object - vSphere host object
4533 vm_object - vSphere VM object
4534 host_pci_dev - host_pci_dev must be one of the devices from the
4535 host_object.hardware.pciDevice list
4536 which is configured as a PCI passthrough device
4537
4538 Returns:
4539 task object
4540 """
4541 task = None
4542 if vm_object and host_object and host_pci_dev:
4543 try :
4544 #Add PCI device to VM
4545 pci_passthroughs = vm_object.environmentBrowser.QueryConfigTarget(host=None).pciPassthrough
4546 systemid_by_pciid = {item.pciDevice.id: item.systemId for item in pci_passthroughs}
4547
4548 if host_pci_dev.id not in systemid_by_pciid:
4549 self.logger.error("Device {} is not a passthrough device ".format(host_pci_dev))
4550 return None
4551
4552 deviceId = hex(host_pci_dev.deviceId % 2**16).lstrip('0x')
4553 backing = vim.VirtualPCIPassthroughDeviceBackingInfo(deviceId=deviceId,
4554 id=host_pci_dev.id,
4555 systemId=systemid_by_pciid[host_pci_dev.id],
4556 vendorId=host_pci_dev.vendorId,
4557 deviceName=host_pci_dev.deviceName)
4558
4559 hba_object = vim.VirtualPCIPassthrough(key=-100, backing=backing)
4560
4561 new_device_config = vim.VirtualDeviceConfigSpec(device=hba_object)
4562 new_device_config.operation = "add"
4563 vmConfigSpec = vim.vm.ConfigSpec()
4564 vmConfigSpec.deviceChange = [new_device_config]
4565
4566 task = vm_object.ReconfigVM_Task(spec=vmConfigSpec)
4567 self.logger.info("Adding PCI device {} into VM {} from host {} ".format(
4568 host_pci_dev, vm_object, host_object)
4569 )
4570 except Exception as exp:
4571 self.logger.error("Error occurred while adding pci devive {} to VM {}: {}".format(
4572 host_pci_dev,
4573 vm_object,
4574 exp))
4575 return task
4576
4577 def get_vm_vcenter_info(self):
4578 """
4579 Method to get details of vCenter and vm
4580
4581 Args:
4582 vapp_uuid - uuid of vApp or VM
4583
4584 Returns:
4585 Moref Id of VM and deails of vCenter
4586 """
4587 vm_vcenter_info = {}
4588
4589 if self.vcenter_ip is not None:
4590 vm_vcenter_info["vm_vcenter_ip"] = self.vcenter_ip
4591 else:
4592 raise vimconn.vimconnException(message="vCenter IP is not provided."\
4593 " Please provide vCenter IP while attaching datacenter to tenant in --config")
4594 if self.vcenter_port is not None:
4595 vm_vcenter_info["vm_vcenter_port"] = self.vcenter_port
4596 else:
4597 raise vimconn.vimconnException(message="vCenter port is not provided."\
4598 " Please provide vCenter port while attaching datacenter to tenant in --config")
4599 if self.vcenter_user is not None:
4600 vm_vcenter_info["vm_vcenter_user"] = self.vcenter_user
4601 else:
4602 raise vimconn.vimconnException(message="vCenter user is not provided."\
4603 " Please provide vCenter user while attaching datacenter to tenant in --config")
4604
4605 if self.vcenter_password is not None:
4606 vm_vcenter_info["vm_vcenter_password"] = self.vcenter_password
4607 else:
4608 raise vimconn.vimconnException(message="vCenter user password is not provided."\
4609 " Please provide vCenter user password while attaching datacenter to tenant in --config")
4610
4611 return vm_vcenter_info
4612
4613
4614 def get_vm_pci_details(self, vmuuid):
4615 """
4616 Method to get VM PCI device details from vCenter
4617
4618 Args:
4619 vm_obj - vSphere VM object
4620
4621 Returns:
4622 dict of PCI devives attached to VM
4623
4624 """
4625 vm_pci_devices_info = {}
4626 try:
4627 vcenter_conect, content = self.get_vcenter_content()
4628 vm_moref_id = self.get_vm_moref_id(vmuuid)
4629 if vm_moref_id:
4630 #Get VM and its host
4631 if content:
4632 host_obj, vm_obj = self.get_vm_obj(content, vm_moref_id)
4633 if host_obj and vm_obj:
4634 vm_pci_devices_info["host_name"]= host_obj.name
4635 vm_pci_devices_info["host_ip"]= host_obj.config.network.vnic[0].spec.ip.ipAddress
4636 for device in vm_obj.config.hardware.device:
4637 if type(device) == vim.vm.device.VirtualPCIPassthrough:
4638 device_details={'devide_id':device.backing.id,
4639 'pciSlotNumber':device.slotInfo.pciSlotNumber,
4640 }
4641 vm_pci_devices_info[device.deviceInfo.label] = device_details
4642 else:
4643 self.logger.error("Can not connect to vCenter while getting "\
4644 "PCI devices infromationn")
4645 return vm_pci_devices_info
4646 except Exception as exp:
4647 self.logger.error("Error occurred while getting VM infromationn"\
4648 " for VM : {}".format(exp))
4649 raise vimconn.vimconnException(message=exp)
4650
4651
4652 def reserve_memory_for_all_vms(self, vapp, memory_mb):
4653 """
4654 Method to reserve memory for all VMs
4655 Args :
4656 vapp - VApp
4657 memory_mb - Memory in MB
4658 Returns:
4659 None
4660 """
4661
4662 self.logger.info("Reserve memory for all VMs")
4663 for vms in vapp.get_all_vms():
4664 vm_id = vms.get('id').split(':')[-1]
4665
4666 url_rest_call = "{}/api/vApp/vm-{}/virtualHardwareSection/memory".format(self.url, vm_id)
4667
4668 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
4669 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
4670 headers['Content-Type'] = 'application/vnd.vmware.vcloud.rasdItem+xml'
4671 response = self.perform_request(req_type='GET',
4672 url=url_rest_call,
4673 headers=headers)
4674
4675 if response.status_code == 403:
4676 response = self.retry_rest('GET', url_rest_call)
4677
4678 if response.status_code != 200:
4679 self.logger.error("REST call {} failed reason : {}"\
4680 "status code : {}".format(url_rest_call,
4681 response.content,
4682 response.status_code))
4683 raise vimconn.vimconnException("reserve_memory_for_all_vms : Failed to get "\
4684 "memory")
4685
4686 bytexml = bytes(bytearray(response.content, encoding='utf-8'))
4687 contentelem = lxmlElementTree.XML(bytexml)
4688 namespaces = {prefix:uri for prefix,uri in contentelem.nsmap.iteritems() if prefix}
4689 namespaces["xmlns"]= "http://www.vmware.com/vcloud/v1.5"
4690
4691 # Find the reservation element in the response
4692 memelem_list = contentelem.findall(".//rasd:Reservation", namespaces)
4693 for memelem in memelem_list:
4694 memelem.text = str(memory_mb)
4695
4696 newdata = lxmlElementTree.tostring(contentelem, pretty_print=True)
4697
4698 response = self.perform_request(req_type='PUT',
4699 url=url_rest_call,
4700 headers=headers,
4701 data=newdata)
4702
4703 if response.status_code == 403:
4704 add_headers = {'Content-Type': headers['Content-Type']}
4705 response = self.retry_rest('PUT', url_rest_call, add_headers, newdata)
4706
4707 if response.status_code != 202:
4708 self.logger.error("REST call {} failed reason : {}"\
4709 "status code : {} ".format(url_rest_call,
4710 response.content,
4711 response.status_code))
4712 raise vimconn.vimconnException("reserve_memory_for_all_vms : Failed to update "\
4713 "virtual hardware memory section")
4714 else:
4715 mem_task = self.get_task_from_response(response.content)
4716 result = self.client.get_task_monitor().wait_for_success(task=mem_task)
4717 if result.get('status') == 'success':
4718 self.logger.info("reserve_memory_for_all_vms(): VM {} succeeded "\
4719 .format(vm_id))
4720 else:
4721 self.logger.error("reserve_memory_for_all_vms(): VM {} failed "\
4722 .format(vm_id))
4723
4724 def connect_vapp_to_org_vdc_network(self, vapp_id, net_name):
4725 """
4726 Configure VApp network config with org vdc network
4727 Args :
4728 vapp - VApp
4729 Returns:
4730 None
4731 """
4732
4733 self.logger.info("Connecting vapp {} to org vdc network {}".
4734 format(vapp_id, net_name))
4735
4736 url_rest_call = "{}/api/vApp/vapp-{}/networkConfigSection/".format(self.url, vapp_id)
4737
4738 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
4739 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
4740 response = self.perform_request(req_type='GET',
4741 url=url_rest_call,
4742 headers=headers)
4743
4744 if response.status_code == 403:
4745 response = self.retry_rest('GET', url_rest_call)
4746
4747 if response.status_code != 200:
4748 self.logger.error("REST call {} failed reason : {}"\
4749 "status code : {}".format(url_rest_call,
4750 response.content,
4751 response.status_code))
4752 raise vimconn.vimconnException("connect_vapp_to_org_vdc_network : Failed to get "\
4753 "network config section")
4754
4755 data = response.content
4756 headers['Content-Type'] = 'application/vnd.vmware.vcloud.networkConfigSection+xml'
4757 net_id = self.get_network_id_by_name(net_name)
4758 if not net_id:
4759 raise vimconn.vimconnException("connect_vapp_to_org_vdc_network : Failed to find "\
4760 "existing network")
4761
4762 bytexml = bytes(bytearray(data, encoding='utf-8'))
4763 newelem = lxmlElementTree.XML(bytexml)
4764 namespaces = {prefix: uri for prefix, uri in newelem.nsmap.iteritems() if prefix}
4765 namespaces["xmlns"] = "http://www.vmware.com/vcloud/v1.5"
4766 nwcfglist = newelem.findall(".//xmlns:NetworkConfig", namespaces)
4767
4768 newstr = """<NetworkConfig networkName="{}">
4769 <Configuration>
4770 <ParentNetwork href="{}/api/network/{}"/>
4771 <FenceMode>bridged</FenceMode>
4772 </Configuration>
4773 </NetworkConfig>
4774 """.format(net_name, self.url, net_id)
4775 newcfgelem = lxmlElementTree.fromstring(newstr)
4776 if nwcfglist:
4777 nwcfglist[0].addnext(newcfgelem)
4778
4779 newdata = lxmlElementTree.tostring(newelem, pretty_print=True)
4780
4781 response = self.perform_request(req_type='PUT',
4782 url=url_rest_call,
4783 headers=headers,
4784 data=newdata)
4785
4786 if response.status_code == 403:
4787 add_headers = {'Content-Type': headers['Content-Type']}
4788 response = self.retry_rest('PUT', url_rest_call, add_headers, newdata)
4789
4790 if response.status_code != 202:
4791 self.logger.error("REST call {} failed reason : {}"\
4792 "status code : {} ".format(url_rest_call,
4793 response.content,
4794 response.status_code))
4795 raise vimconn.vimconnException("connect_vapp_to_org_vdc_network : Failed to update "\
4796 "network config section")
4797 else:
4798 vapp_task = self.get_task_from_response(response.content)
4799 result = self.client.get_task_monitor().wait_for_success(task=vapp_task)
4800 if result.get('status') == 'success':
4801 self.logger.info("connect_vapp_to_org_vdc_network(): Vapp {} connected to "\
4802 "network {}".format(vapp_id, net_name))
4803 else:
4804 self.logger.error("connect_vapp_to_org_vdc_network(): Vapp {} failed to "\
4805 "connect to network {}".format(vapp_id, net_name))
4806
4807 def remove_primary_network_adapter_from_all_vms(self, vapp):
4808 """
4809 Method to remove network adapter type to vm
4810 Args :
4811 vapp - VApp
4812 Returns:
4813 None
4814 """
4815
4816 self.logger.info("Removing network adapter from all VMs")
4817 for vms in vapp.get_all_vms():
4818 vm_id = vms.get('id').split(':')[-1]
4819
4820 url_rest_call = "{}/api/vApp/vm-{}/networkConnectionSection/".format(self.url, vm_id)
4821
4822 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
4823 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
4824 response = self.perform_request(req_type='GET',
4825 url=url_rest_call,
4826 headers=headers)
4827
4828 if response.status_code == 403:
4829 response = self.retry_rest('GET', url_rest_call)
4830
4831 if response.status_code != 200:
4832 self.logger.error("REST call {} failed reason : {}"\
4833 "status code : {}".format(url_rest_call,
4834 response.content,
4835 response.status_code))
4836 raise vimconn.vimconnException("remove_primary_network_adapter : Failed to get "\
4837 "network connection section")
4838
4839 data = response.content
4840 data = data.split('<Link rel="edit"')[0]
4841
4842 headers['Content-Type'] = 'application/vnd.vmware.vcloud.networkConnectionSection+xml'
4843
4844 newdata = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
4845 <NetworkConnectionSection xmlns="http://www.vmware.com/vcloud/v1.5"
4846 xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1"
4847 xmlns:vssd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData"
4848 xmlns:common="http://schemas.dmtf.org/wbem/wscim/1/common"
4849 xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData"
4850 xmlns:vmw="http://www.vmware.com/schema/ovf"
4851 xmlns:ovfenv="http://schemas.dmtf.org/ovf/environment/1"
4852 xmlns:vmext="http://www.vmware.com/vcloud/extension/v1.5"
4853 xmlns:ns9="http://www.vmware.com/vcloud/versions"
4854 href="{url}" type="application/vnd.vmware.vcloud.networkConnectionSection+xml" ovf:required="false">
4855 <ovf:Info>Specifies the available VM network connections</ovf:Info>
4856 <PrimaryNetworkConnectionIndex>0</PrimaryNetworkConnectionIndex>
4857 <Link rel="edit" href="{url}" type="application/vnd.vmware.vcloud.networkConnectionSection+xml"/>
4858 </NetworkConnectionSection>""".format(url=url_rest_call)
4859 response = self.perform_request(req_type='PUT',
4860 url=url_rest_call,
4861 headers=headers,
4862 data=newdata)
4863
4864 if response.status_code == 403:
4865 add_headers = {'Content-Type': headers['Content-Type']}
4866 response = self.retry_rest('PUT', url_rest_call, add_headers, newdata)
4867
4868 if response.status_code != 202:
4869 self.logger.error("REST call {} failed reason : {}"\
4870 "status code : {} ".format(url_rest_call,
4871 response.content,
4872 response.status_code))
4873 raise vimconn.vimconnException("remove_primary_network_adapter : Failed to update "\
4874 "network connection section")
4875 else:
4876 nic_task = self.get_task_from_response(response.content)
4877 result = self.client.get_task_monitor().wait_for_success(task=nic_task)
4878 if result.get('status') == 'success':
4879 self.logger.info("remove_primary_network_adapter(): VM {} conneced to "\
4880 "default NIC type".format(vm_id))
4881 else:
4882 self.logger.error("remove_primary_network_adapter(): VM {} failed to "\
4883 "connect NIC type".format(vm_id))
4884
4885 def add_network_adapter_to_vms(self, vapp, network_name, primary_nic_index, nicIndex, net, nic_type=None):
4886 """
4887 Method to add network adapter type to vm
4888 Args :
4889 network_name - name of network
4890 primary_nic_index - int value for primary nic index
4891 nicIndex - int value for nic index
4892 nic_type - specify model name to which add to vm
4893 Returns:
4894 None
4895 """
4896
4897 self.logger.info("Add network adapter to VM: network_name {} nicIndex {} nic_type {}".\
4898 format(network_name, nicIndex, nic_type))
4899 try:
4900 ip_address = None
4901 floating_ip = False
4902 mac_address = None
4903 if 'floating_ip' in net: floating_ip = net['floating_ip']
4904
4905 # Stub for ip_address feature
4906 if 'ip_address' in net: ip_address = net['ip_address']
4907
4908 if 'mac_address' in net: mac_address = net['mac_address']
4909
4910 if floating_ip:
4911 allocation_mode = "POOL"
4912 elif ip_address:
4913 allocation_mode = "MANUAL"
4914 else:
4915 allocation_mode = "DHCP"
4916
4917 if not nic_type:
4918 for vms in vapp.get_all_vms():
4919 vm_id = vms.get('id').split(':')[-1]
4920
4921 url_rest_call = "{}/api/vApp/vm-{}/networkConnectionSection/".format(self.url, vm_id)
4922
4923 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
4924 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
4925 response = self.perform_request(req_type='GET',
4926 url=url_rest_call,
4927 headers=headers)
4928
4929 if response.status_code == 403:
4930 response = self.retry_rest('GET', url_rest_call)
4931
4932 if response.status_code != 200:
4933 self.logger.error("REST call {} failed reason : {}"\
4934 "status code : {}".format(url_rest_call,
4935 response.content,
4936 response.status_code))
4937 raise vimconn.vimconnException("add_network_adapter_to_vms : Failed to get "\
4938 "network connection section")
4939
4940 data = response.content
4941 data = data.split('<Link rel="edit"')[0]
4942 if '<PrimaryNetworkConnectionIndex>' not in data:
4943 self.logger.debug("add_network_adapter PrimaryNIC not in data")
4944 item = """<PrimaryNetworkConnectionIndex>{}</PrimaryNetworkConnectionIndex>
4945 <NetworkConnection network="{}">
4946 <NetworkConnectionIndex>{}</NetworkConnectionIndex>
4947 <IsConnected>true</IsConnected>
4948 <IpAddressAllocationMode>{}</IpAddressAllocationMode>
4949 </NetworkConnection>""".format(primary_nic_index, network_name, nicIndex,
4950 allocation_mode)
4951 # Stub for ip_address feature
4952 if ip_address:
4953 ip_tag = '<IpAddress>{}</IpAddress>'.format(ip_address)
4954 item = item.replace('</NetworkConnectionIndex>\n','</NetworkConnectionIndex>\n{}\n'.format(ip_tag))
4955
4956 if mac_address:
4957 mac_tag = '<MACAddress>{}</MACAddress>'.format(mac_address)
4958 item = item.replace('</IsConnected>\n','</IsConnected>\n{}\n'.format(mac_tag))
4959
4960 data = data.replace('</ovf:Info>\n','</ovf:Info>\n{}\n</NetworkConnectionSection>'.format(item))
4961 else:
4962 self.logger.debug("add_network_adapter PrimaryNIC in data")
4963 new_item = """<NetworkConnection network="{}">
4964 <NetworkConnectionIndex>{}</NetworkConnectionIndex>
4965 <IsConnected>true</IsConnected>
4966 <IpAddressAllocationMode>{}</IpAddressAllocationMode>
4967 </NetworkConnection>""".format(network_name, nicIndex,
4968 allocation_mode)
4969 # Stub for ip_address feature
4970 if ip_address:
4971 ip_tag = '<IpAddress>{}</IpAddress>'.format(ip_address)
4972 new_item = new_item.replace('</NetworkConnectionIndex>\n','</NetworkConnectionIndex>\n{}\n'.format(ip_tag))
4973
4974 if mac_address:
4975 mac_tag = '<MACAddress>{}</MACAddress>'.format(mac_address)
4976 new_item = new_item.replace('</IsConnected>\n','</IsConnected>\n{}\n'.format(mac_tag))
4977
4978 data = data + new_item + '</NetworkConnectionSection>'
4979
4980 headers['Content-Type'] = 'application/vnd.vmware.vcloud.networkConnectionSection+xml'
4981
4982 response = self.perform_request(req_type='PUT',
4983 url=url_rest_call,
4984 headers=headers,
4985 data=data)
4986
4987 if response.status_code == 403:
4988 add_headers = {'Content-Type': headers['Content-Type']}
4989 response = self.retry_rest('PUT', url_rest_call, add_headers, data)
4990
4991 if response.status_code != 202:
4992 self.logger.error("REST call {} failed reason : {}"\
4993 "status code : {} ".format(url_rest_call,
4994 response.content,
4995 response.status_code))
4996 raise vimconn.vimconnException("add_network_adapter_to_vms : Failed to update "\
4997 "network connection section")
4998 else:
4999 nic_task = self.get_task_from_response(response.content)
5000 result = self.client.get_task_monitor().wait_for_success(task=nic_task)
5001 if result.get('status') == 'success':
5002 self.logger.info("add_network_adapter_to_vms(): VM {} conneced to "\
5003 "default NIC type".format(vm_id))
5004 else:
5005 self.logger.error("add_network_adapter_to_vms(): VM {} failed to "\
5006 "connect NIC type".format(vm_id))
5007 else:
5008 for vms in vapp.get_all_vms():
5009 vm_id = vms.get('id').split(':')[-1]
5010
5011 url_rest_call = "{}/api/vApp/vm-{}/networkConnectionSection/".format(self.url, vm_id)
5012
5013 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
5014 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
5015 response = self.perform_request(req_type='GET',
5016 url=url_rest_call,
5017 headers=headers)
5018
5019 if response.status_code == 403:
5020 response = self.retry_rest('GET', url_rest_call)
5021
5022 if response.status_code != 200:
5023 self.logger.error("REST call {} failed reason : {}"\
5024 "status code : {}".format(url_rest_call,
5025 response.content,
5026 response.status_code))
5027 raise vimconn.vimconnException("add_network_adapter_to_vms : Failed to get "\
5028 "network connection section")
5029 data = response.content
5030 data = data.split('<Link rel="edit"')[0]
5031 if '<PrimaryNetworkConnectionIndex>' not in data:
5032 self.logger.debug("add_network_adapter PrimaryNIC not in data nic_type {}".format(nic_type))
5033 item = """<PrimaryNetworkConnectionIndex>{}</PrimaryNetworkConnectionIndex>
5034 <NetworkConnection network="{}">
5035 <NetworkConnectionIndex>{}</NetworkConnectionIndex>
5036 <IsConnected>true</IsConnected>
5037 <IpAddressAllocationMode>{}</IpAddressAllocationMode>
5038 <NetworkAdapterType>{}</NetworkAdapterType>
5039 </NetworkConnection>""".format(primary_nic_index, network_name, nicIndex,
5040 allocation_mode, nic_type)
5041 # Stub for ip_address feature
5042 if ip_address:
5043 ip_tag = '<IpAddress>{}</IpAddress>'.format(ip_address)
5044 item = item.replace('</NetworkConnectionIndex>\n','</NetworkConnectionIndex>\n{}\n'.format(ip_tag))
5045
5046 if mac_address:
5047 mac_tag = '<MACAddress>{}</MACAddress>'.format(mac_address)
5048 item = item.replace('</IsConnected>\n','</IsConnected>\n{}\n'.format(mac_tag))
5049
5050 data = data.replace('</ovf:Info>\n','</ovf:Info>\n{}\n</NetworkConnectionSection>'.format(item))
5051 else:
5052 self.logger.debug("add_network_adapter PrimaryNIC in data nic_type {}".format(nic_type))
5053 new_item = """<NetworkConnection network="{}">
5054 <NetworkConnectionIndex>{}</NetworkConnectionIndex>
5055 <IsConnected>true</IsConnected>
5056 <IpAddressAllocationMode>{}</IpAddressAllocationMode>
5057 <NetworkAdapterType>{}</NetworkAdapterType>
5058 </NetworkConnection>""".format(network_name, nicIndex,
5059 allocation_mode, nic_type)
5060 # Stub for ip_address feature
5061 if ip_address:
5062 ip_tag = '<IpAddress>{}</IpAddress>'.format(ip_address)
5063 new_item = new_item.replace('</NetworkConnectionIndex>\n','</NetworkConnectionIndex>\n{}\n'.format(ip_tag))
5064
5065 if mac_address:
5066 mac_tag = '<MACAddress>{}</MACAddress>'.format(mac_address)
5067 new_item = new_item.replace('</IsConnected>\n','</IsConnected>\n{}\n'.format(mac_tag))
5068
5069 data = data + new_item + '</NetworkConnectionSection>'
5070
5071 headers['Content-Type'] = 'application/vnd.vmware.vcloud.networkConnectionSection+xml'
5072
5073 response = self.perform_request(req_type='PUT',
5074 url=url_rest_call,
5075 headers=headers,
5076 data=data)
5077
5078 if response.status_code == 403:
5079 add_headers = {'Content-Type': headers['Content-Type']}
5080 response = self.retry_rest('PUT', url_rest_call, add_headers, data)
5081
5082 if response.status_code != 202:
5083 self.logger.error("REST call {} failed reason : {}"\
5084 "status code : {}".format(url_rest_call,
5085 response.content,
5086 response.status_code))
5087 raise vimconn.vimconnException("add_network_adapter_to_vms : Failed to update "\
5088 "network connection section")
5089 else:
5090 nic_task = self.get_task_from_response(response.content)
5091 result = self.client.get_task_monitor().wait_for_success(task=nic_task)
5092 if result.get('status') == 'success':
5093 self.logger.info("add_network_adapter_to_vms(): VM {} "\
5094 "conneced to NIC type {}".format(vm_id, nic_type))
5095 else:
5096 self.logger.error("add_network_adapter_to_vms(): VM {} "\
5097 "failed to connect NIC type {}".format(vm_id, nic_type))
5098 except Exception as exp:
5099 self.logger.error("add_network_adapter_to_vms() : exception occurred "\
5100 "while adding Network adapter")
5101 raise vimconn.vimconnException(message=exp)
5102
5103
5104 def set_numa_affinity(self, vmuuid, paired_threads_id):
5105 """
5106 Method to assign numa affinity in vm configuration parammeters
5107 Args :
5108 vmuuid - vm uuid
5109 paired_threads_id - one or more virtual processor
5110 numbers
5111 Returns:
5112 return if True
5113 """
5114 try:
5115 vcenter_conect, content = self.get_vcenter_content()
5116 vm_moref_id = self.get_vm_moref_id(vmuuid)
5117
5118 host_obj, vm_obj = self.get_vm_obj(content ,vm_moref_id)
5119 if vm_obj:
5120 config_spec = vim.vm.ConfigSpec()
5121 config_spec.extraConfig = []
5122 opt = vim.option.OptionValue()
5123 opt.key = 'numa.nodeAffinity'
5124 opt.value = str(paired_threads_id)
5125 config_spec.extraConfig.append(opt)
5126 task = vm_obj.ReconfigVM_Task(config_spec)
5127 if task:
5128 result = self.wait_for_vcenter_task(task, vcenter_conect)
5129 extra_config = vm_obj.config.extraConfig
5130 flag = False
5131 for opts in extra_config:
5132 if 'numa.nodeAffinity' in opts.key:
5133 flag = True
5134 self.logger.info("set_numa_affinity: Sucessfully assign numa affinity "\
5135 "value {} for vm {}".format(opt.value, vm_obj))
5136 if flag:
5137 return
5138 else:
5139 self.logger.error("set_numa_affinity: Failed to assign numa affinity")
5140 except Exception as exp:
5141 self.logger.error("set_numa_affinity : exception occurred while setting numa affinity "\
5142 "for VM {} : {}".format(vm_obj, vm_moref_id))
5143 raise vimconn.vimconnException("set_numa_affinity : Error {} failed to assign numa "\
5144 "affinity".format(exp))
5145
5146
5147 def cloud_init(self, vapp, cloud_config):
5148 """
5149 Method to inject ssh-key
5150 vapp - vapp object
5151 cloud_config a dictionary with:
5152 'key-pairs': (optional) list of strings with the public key to be inserted to the default user
5153 'users': (optional) list of users to be inserted, each item is a dict with:
5154 'name': (mandatory) user name,
5155 'key-pairs': (optional) list of strings with the public key to be inserted to the user
5156 'user-data': (optional) can be a string with the text script to be passed directly to cloud-init,
5157 or a list of strings, each one contains a script to be passed, usually with a MIMEmultipart file
5158 'config-files': (optional). List of files to be transferred. Each item is a dict with:
5159 'dest': (mandatory) string with the destination absolute path
5160 'encoding': (optional, by default text). Can be one of:
5161 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
5162 'content' (mandatory): string with the content of the file
5163 'permissions': (optional) string with file permissions, typically octal notation '0644'
5164 'owner': (optional) file owner, string with the format 'owner:group'
5165 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk
5166 """
5167 try:
5168 if not isinstance(cloud_config, dict):
5169 raise Exception("cloud_init : parameter cloud_config is not a dictionary")
5170 else:
5171 key_pairs = []
5172 userdata = []
5173 if "key-pairs" in cloud_config:
5174 key_pairs = cloud_config["key-pairs"]
5175
5176 if "users" in cloud_config:
5177 userdata = cloud_config["users"]
5178
5179 self.logger.debug("cloud_init : Guest os customization started..")
5180 customize_script = self.format_script(key_pairs=key_pairs, users_list=userdata)
5181 customize_script = customize_script.replace("&","&amp;")
5182 self.guest_customization(vapp, customize_script)
5183
5184 except Exception as exp:
5185 self.logger.error("cloud_init : exception occurred while injecting "\
5186 "ssh-key")
5187 raise vimconn.vimconnException("cloud_init : Error {} failed to inject "\
5188 "ssh-key".format(exp))
5189
5190 def format_script(self, key_pairs=[], users_list=[]):
5191 bash_script = """#!/bin/sh
5192 echo performing customization tasks with param $1 at `date "+DATE: %Y-%m-%d - TIME: %H:%M:%S"` >> /root/customization.log
5193 if [ "$1" = "precustomization" ];then
5194 echo performing precustomization tasks on `date "+DATE: %Y-%m-%d - TIME: %H:%M:%S"` >> /root/customization.log
5195 """
5196
5197 keys = "\n".join(key_pairs)
5198 if keys:
5199 keys_data = """
5200 if [ ! -d /root/.ssh ];then
5201 mkdir /root/.ssh
5202 chown root:root /root/.ssh
5203 chmod 700 /root/.ssh
5204 touch /root/.ssh/authorized_keys
5205 chown root:root /root/.ssh/authorized_keys
5206 chmod 600 /root/.ssh/authorized_keys
5207 # make centos with selinux happy
5208 which restorecon && restorecon -Rv /root/.ssh
5209 else
5210 touch /root/.ssh/authorized_keys
5211 chown root:root /root/.ssh/authorized_keys
5212 chmod 600 /root/.ssh/authorized_keys
5213 fi
5214 echo '{key}' >> /root/.ssh/authorized_keys
5215 """.format(key=keys)
5216
5217 bash_script+= keys_data
5218
5219 for user in users_list:
5220 if 'name' in user: user_name = user['name']
5221 if 'key-pairs' in user:
5222 user_keys = "\n".join(user['key-pairs'])
5223 else:
5224 user_keys = None
5225
5226 add_user_name = """
5227 useradd -d /home/{user_name} -m -g users -s /bin/bash {user_name}
5228 """.format(user_name=user_name)
5229
5230 bash_script+= add_user_name
5231
5232 if user_keys:
5233 user_keys_data = """
5234 mkdir /home/{user_name}/.ssh
5235 chown {user_name}:{user_name} /home/{user_name}/.ssh
5236 chmod 700 /home/{user_name}/.ssh
5237 touch /home/{user_name}/.ssh/authorized_keys
5238 chown {user_name}:{user_name} /home/{user_name}/.ssh/authorized_keys
5239 chmod 600 /home/{user_name}/.ssh/authorized_keys
5240 # make centos with selinux happy
5241 which restorecon && restorecon -Rv /home/{user_name}/.ssh
5242 echo '{user_key}' >> /home/{user_name}/.ssh/authorized_keys
5243 """.format(user_name=user_name,user_key=user_keys)
5244
5245 bash_script+= user_keys_data
5246
5247 return bash_script+"\n\tfi"
5248
5249 def guest_customization(self, vapp, customize_script):
5250 """
5251 Method to customize guest os
5252 vapp - Vapp object
5253 customize_script - Customize script to be run at first boot of VM.
5254 """
5255 for vm in vapp.get_all_vms():
5256 vm_id = vm.get('id').split(':')[-1]
5257 vm_name = vm.get('name')
5258 vm_name = vm_name.replace('_','-')
5259
5260 vm_customization_url = "{}/api/vApp/vm-{}/guestCustomizationSection/".format(self.url, vm_id)
5261 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
5262 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
5263
5264 headers['Content-Type'] = "application/vnd.vmware.vcloud.guestCustomizationSection+xml"
5265
5266 data = """<GuestCustomizationSection
5267 xmlns="http://www.vmware.com/vcloud/v1.5"
5268 xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1"
5269 ovf:required="false" href="{}" type="application/vnd.vmware.vcloud.guestCustomizationSection+xml">
5270 <ovf:Info>Specifies Guest OS Customization Settings</ovf:Info>
5271 <Enabled>true</Enabled>
5272 <ChangeSid>false</ChangeSid>
5273 <VirtualMachineId>{}</VirtualMachineId>
5274 <JoinDomainEnabled>false</JoinDomainEnabled>
5275 <UseOrgSettings>false</UseOrgSettings>
5276 <AdminPasswordEnabled>false</AdminPasswordEnabled>
5277 <AdminPasswordAuto>true</AdminPasswordAuto>
5278 <AdminAutoLogonEnabled>false</AdminAutoLogonEnabled>
5279 <AdminAutoLogonCount>0</AdminAutoLogonCount>
5280 <ResetPasswordRequired>false</ResetPasswordRequired>
5281 <CustomizationScript>{}</CustomizationScript>
5282 <ComputerName>{}</ComputerName>
5283 <Link href="{}" type="application/vnd.vmware.vcloud.guestCustomizationSection+xml" rel="edit"/>
5284 </GuestCustomizationSection>
5285 """.format(vm_customization_url,
5286 vm_id,
5287 customize_script,
5288 vm_name,
5289 vm_customization_url)
5290
5291 response = self.perform_request(req_type='PUT',
5292 url=vm_customization_url,
5293 headers=headers,
5294 data=data)
5295 if response.status_code == 202:
5296 guest_task = self.get_task_from_response(response.content)
5297 self.client.get_task_monitor().wait_for_success(task=guest_task)
5298 self.logger.info("guest_customization : customized guest os task "\
5299 "completed for VM {}".format(vm_name))
5300 else:
5301 self.logger.error("guest_customization : task for customized guest os"\
5302 "failed for VM {}".format(vm_name))
5303 raise vimconn.vimconnException("guest_customization : failed to perform"\
5304 "guest os customization on VM {}".format(vm_name))
5305
5306 def add_new_disk(self, vapp_uuid, disk_size):
5307 """
5308 Method to create an empty vm disk
5309
5310 Args:
5311 vapp_uuid - is vapp identifier.
5312 disk_size - size of disk to be created in GB
5313
5314 Returns:
5315 None
5316 """
5317 status = False
5318 vm_details = None
5319 try:
5320 #Disk size in GB, convert it into MB
5321 if disk_size is not None:
5322 disk_size_mb = int(disk_size) * 1024
5323 vm_details = self.get_vapp_details_rest(vapp_uuid)
5324
5325 if vm_details and "vm_virtual_hardware" in vm_details:
5326 self.logger.info("Adding disk to VM: {} disk size:{}GB".format(vm_details["name"], disk_size))
5327 disk_href = vm_details["vm_virtual_hardware"]["disk_edit_href"]
5328 status = self.add_new_disk_rest(disk_href, disk_size_mb)
5329
5330 except Exception as exp:
5331 msg = "Error occurred while creating new disk {}.".format(exp)
5332 self.rollback_newvm(vapp_uuid, msg)
5333
5334 if status:
5335 self.logger.info("Added new disk to VM: {} disk size:{}GB".format(vm_details["name"], disk_size))
5336 else:
5337 #If failed to add disk, delete VM
5338 msg = "add_new_disk: Failed to add new disk to {}".format(vm_details["name"])
5339 self.rollback_newvm(vapp_uuid, msg)
5340
5341
5342 def add_new_disk_rest(self, disk_href, disk_size_mb):
5343 """
5344 Retrives vApp Disks section & add new empty disk
5345
5346 Args:
5347 disk_href: Disk section href to addd disk
5348 disk_size_mb: Disk size in MB
5349
5350 Returns: Status of add new disk task
5351 """
5352 status = False
5353 if self.client._session:
5354 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
5355 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
5356 response = self.perform_request(req_type='GET',
5357 url=disk_href,
5358 headers=headers)
5359
5360 if response.status_code == 403:
5361 response = self.retry_rest('GET', disk_href)
5362
5363 if response.status_code != requests.codes.ok:
5364 self.logger.error("add_new_disk_rest: GET REST API call {} failed. Return status code {}"
5365 .format(disk_href, response.status_code))
5366 return status
5367 try:
5368 #Find but type & max of instance IDs assigned to disks
5369 lxmlroot_respond = lxmlElementTree.fromstring(response.content)
5370 namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.iteritems() if prefix}
5371 #For python3
5372 #namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.items() if prefix}
5373 namespaces["xmlns"]= "http://www.vmware.com/vcloud/v1.5"
5374 instance_id = 0
5375 for item in lxmlroot_respond.iterfind('xmlns:Item',namespaces):
5376 if item.find("rasd:Description",namespaces).text == "Hard disk":
5377 inst_id = int(item.find("rasd:InstanceID" ,namespaces).text)
5378 if inst_id > instance_id:
5379 instance_id = inst_id
5380 disk_item = item.find("rasd:HostResource" ,namespaces)
5381 bus_subtype = disk_item.attrib["{"+namespaces['xmlns']+"}busSubType"]
5382 bus_type = disk_item.attrib["{"+namespaces['xmlns']+"}busType"]
5383
5384 instance_id = instance_id + 1
5385 new_item = """<Item>
5386 <rasd:Description>Hard disk</rasd:Description>
5387 <rasd:ElementName>New disk</rasd:ElementName>
5388 <rasd:HostResource
5389 xmlns:vcloud="http://www.vmware.com/vcloud/v1.5"
5390 vcloud:capacity="{}"
5391 vcloud:busSubType="{}"
5392 vcloud:busType="{}"></rasd:HostResource>
5393 <rasd:InstanceID>{}</rasd:InstanceID>
5394 <rasd:ResourceType>17</rasd:ResourceType>
5395 </Item>""".format(disk_size_mb, bus_subtype, bus_type, instance_id)
5396
5397 new_data = response.content
5398 #Add new item at the bottom
5399 new_data = new_data.replace('</Item>\n</RasdItemsList>', '</Item>\n{}\n</RasdItemsList>'.format(new_item))
5400
5401 # Send PUT request to modify virtual hardware section with new disk
5402 headers['Content-Type'] = 'application/vnd.vmware.vcloud.rasdItemsList+xml; charset=ISO-8859-1'
5403
5404 response = self.perform_request(req_type='PUT',
5405 url=disk_href,
5406 data=new_data,
5407 headers=headers)
5408
5409 if response.status_code == 403:
5410 add_headers = {'Content-Type': headers['Content-Type']}
5411 response = self.retry_rest('PUT', disk_href, add_headers, new_data)
5412
5413 if response.status_code != 202:
5414 self.logger.error("PUT REST API call {} failed. Return status code {}. Response Content:{}"
5415 .format(disk_href, response.status_code, response.content))
5416 else:
5417 add_disk_task = self.get_task_from_response(response.content)
5418 result = self.client.get_task_monitor().wait_for_success(task=add_disk_task)
5419 if result.get('status') == 'success':
5420 status = True
5421 else:
5422 self.logger.error("Add new disk REST task failed to add {} MB disk".format(disk_size_mb))
5423
5424 except Exception as exp:
5425 self.logger.error("Error occurred calling rest api for creating new disk {}".format(exp))
5426
5427 return status
5428
5429
5430 def add_existing_disk(self, catalogs=None, image_id=None, size=None, template_name=None, vapp_uuid=None):
5431 """
5432 Method to add existing disk to vm
5433 Args :
5434 catalogs - List of VDC catalogs
5435 image_id - Catalog ID
5436 template_name - Name of template in catalog
5437 vapp_uuid - UUID of vApp
5438 Returns:
5439 None
5440 """
5441 disk_info = None
5442 vcenter_conect, content = self.get_vcenter_content()
5443 #find moref-id of vm in image
5444 catalog_vm_info = self.get_vapp_template_details(catalogs=catalogs,
5445 image_id=image_id,
5446 )
5447
5448 if catalog_vm_info and "vm_vcenter_info" in catalog_vm_info:
5449 if "vm_moref_id" in catalog_vm_info["vm_vcenter_info"]:
5450 catalog_vm_moref_id = catalog_vm_info["vm_vcenter_info"].get("vm_moref_id", None)
5451 if catalog_vm_moref_id:
5452 self.logger.info("Moref_id of VM in catalog : {}" .format(catalog_vm_moref_id))
5453 host, catalog_vm_obj = self.get_vm_obj(content, catalog_vm_moref_id)
5454 if catalog_vm_obj:
5455 #find existing disk
5456 disk_info = self.find_disk(catalog_vm_obj)
5457 else:
5458 exp_msg = "No VM with image id {} found".format(image_id)
5459 self.rollback_newvm(vapp_uuid, exp_msg, exp_type="NotFound")
5460 else:
5461 exp_msg = "No Image found with image ID {} ".format(image_id)
5462 self.rollback_newvm(vapp_uuid, exp_msg, exp_type="NotFound")
5463
5464 if disk_info:
5465 self.logger.info("Existing disk_info : {}".format(disk_info))
5466 #get VM
5467 vm_moref_id = self.get_vm_moref_id(vapp_uuid)
5468 host, vm_obj = self.get_vm_obj(content, vm_moref_id)
5469 if vm_obj:
5470 status = self.add_disk(vcenter_conect=vcenter_conect,
5471 vm=vm_obj,
5472 disk_info=disk_info,
5473 size=size,
5474 vapp_uuid=vapp_uuid
5475 )
5476 if status:
5477 self.logger.info("Disk from image id {} added to {}".format(image_id,
5478 vm_obj.config.name)
5479 )
5480 else:
5481 msg = "No disk found with image id {} to add in VM {}".format(
5482 image_id,
5483 vm_obj.config.name)
5484 self.rollback_newvm(vapp_uuid, msg, exp_type="NotFound")
5485
5486
5487 def find_disk(self, vm_obj):
5488 """
5489 Method to find details of existing disk in VM
5490 Args :
5491 vm_obj - vCenter object of VM
5492 image_id - Catalog ID
5493 Returns:
5494 disk_info : dict of disk details
5495 """
5496 disk_info = {}
5497 if vm_obj:
5498 try:
5499 devices = vm_obj.config.hardware.device
5500 for device in devices:
5501 if type(device) is vim.vm.device.VirtualDisk:
5502 if isinstance(device.backing,vim.vm.device.VirtualDisk.FlatVer2BackingInfo) and hasattr(device.backing, 'fileName'):
5503 disk_info["full_path"] = device.backing.fileName
5504 disk_info["datastore"] = device.backing.datastore
5505 disk_info["capacityKB"] = device.capacityInKB
5506 break
5507 except Exception as exp:
5508 self.logger.error("find_disk() : exception occurred while "\
5509 "getting existing disk details :{}".format(exp))
5510 return disk_info
5511
5512
5513 def add_disk(self, vcenter_conect=None, vm=None, size=None, vapp_uuid=None, disk_info={}):
5514 """
5515 Method to add existing disk in VM
5516 Args :
5517 vcenter_conect - vCenter content object
5518 vm - vCenter vm object
5519 disk_info : dict of disk details
5520 Returns:
5521 status : status of add disk task
5522 """
5523 datastore = disk_info["datastore"] if "datastore" in disk_info else None
5524 fullpath = disk_info["full_path"] if "full_path" in disk_info else None
5525 capacityKB = disk_info["capacityKB"] if "capacityKB" in disk_info else None
5526 if size is not None:
5527 #Convert size from GB to KB
5528 sizeKB = int(size) * 1024 * 1024
5529 #compare size of existing disk and user given size.Assign whicherver is greater
5530 self.logger.info("Add Existing disk : sizeKB {} , capacityKB {}".format(
5531 sizeKB, capacityKB))
5532 if sizeKB > capacityKB:
5533 capacityKB = sizeKB
5534
5535 if datastore and fullpath and capacityKB:
5536 try:
5537 spec = vim.vm.ConfigSpec()
5538 # get all disks on a VM, set unit_number to the next available
5539 unit_number = 0
5540 for dev in vm.config.hardware.device:
5541 if hasattr(dev.backing, 'fileName'):
5542 unit_number = int(dev.unitNumber) + 1
5543 # unit_number 7 reserved for scsi controller
5544 if unit_number == 7:
5545 unit_number += 1
5546 if isinstance(dev, vim.vm.device.VirtualDisk):
5547 #vim.vm.device.VirtualSCSIController
5548 controller_key = dev.controllerKey
5549
5550 self.logger.info("Add Existing disk : unit number {} , controller key {}".format(
5551 unit_number, controller_key))
5552 # add disk here
5553 dev_changes = []
5554 disk_spec = vim.vm.device.VirtualDeviceSpec()
5555 disk_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add
5556 disk_spec.device = vim.vm.device.VirtualDisk()
5557 disk_spec.device.backing = \
5558 vim.vm.device.VirtualDisk.FlatVer2BackingInfo()
5559 disk_spec.device.backing.thinProvisioned = True
5560 disk_spec.device.backing.diskMode = 'persistent'
5561 disk_spec.device.backing.datastore = datastore
5562 disk_spec.device.backing.fileName = fullpath
5563
5564 disk_spec.device.unitNumber = unit_number
5565 disk_spec.device.capacityInKB = capacityKB
5566 disk_spec.device.controllerKey = controller_key
5567 dev_changes.append(disk_spec)
5568 spec.deviceChange = dev_changes
5569 task = vm.ReconfigVM_Task(spec=spec)
5570 status = self.wait_for_vcenter_task(task, vcenter_conect)
5571 return status
5572 except Exception as exp:
5573 exp_msg = "add_disk() : exception {} occurred while adding disk "\
5574 "{} to vm {}".format(exp,
5575 fullpath,
5576 vm.config.name)
5577 self.rollback_newvm(vapp_uuid, exp_msg)
5578 else:
5579 msg = "add_disk() : Can not add disk to VM with disk info {} ".format(disk_info)
5580 self.rollback_newvm(vapp_uuid, msg)
5581
5582
5583 def get_vcenter_content(self):
5584 """
5585 Get the vsphere content object
5586 """
5587 try:
5588 vm_vcenter_info = self.get_vm_vcenter_info()
5589 except Exception as exp:
5590 self.logger.error("Error occurred while getting vCenter infromationn"\
5591 " for VM : {}".format(exp))
5592 raise vimconn.vimconnException(message=exp)
5593
5594 context = None
5595 if hasattr(ssl, '_create_unverified_context'):
5596 context = ssl._create_unverified_context()
5597
5598 vcenter_conect = SmartConnect(
5599 host=vm_vcenter_info["vm_vcenter_ip"],
5600 user=vm_vcenter_info["vm_vcenter_user"],
5601 pwd=vm_vcenter_info["vm_vcenter_password"],
5602 port=int(vm_vcenter_info["vm_vcenter_port"]),
5603 sslContext=context
5604 )
5605 atexit.register(Disconnect, vcenter_conect)
5606 content = vcenter_conect.RetrieveContent()
5607 return vcenter_conect, content
5608
5609
5610 def get_vm_moref_id(self, vapp_uuid):
5611 """
5612 Get the moref_id of given VM
5613 """
5614 try:
5615 if vapp_uuid:
5616 vm_details = self.get_vapp_details_rest(vapp_uuid, need_admin_access=True)
5617 if vm_details and "vm_vcenter_info" in vm_details:
5618 vm_moref_id = vm_details["vm_vcenter_info"].get("vm_moref_id", None)
5619 return vm_moref_id
5620
5621 except Exception as exp:
5622 self.logger.error("Error occurred while getting VM moref ID "\
5623 " for VM : {}".format(exp))
5624 return None
5625
5626
5627 def get_vapp_template_details(self, catalogs=None, image_id=None , template_name=None):
5628 """
5629 Method to get vApp template details
5630 Args :
5631 catalogs - list of VDC catalogs
5632 image_id - Catalog ID to find
5633 template_name : template name in catalog
5634 Returns:
5635 parsed_respond : dict of vApp tempalte details
5636 """
5637 parsed_response = {}
5638
5639 vca = self.connect_as_admin()
5640 if not vca:
5641 raise vimconn.vimconnConnectionException("Failed to connect vCD")
5642
5643 try:
5644 org, vdc = self.get_vdc_details()
5645 catalog = self.get_catalog_obj(image_id, catalogs)
5646 if catalog:
5647 items = org.get_catalog_item(catalog.get('name'), catalog.get('name'))
5648 catalog_items = [items.attrib]
5649
5650 if len(catalog_items) == 1:
5651 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
5652 'x-vcloud-authorization': vca._session.headers['x-vcloud-authorization']}
5653
5654 response = self.perform_request(req_type='GET',
5655 url=catalog_items[0].get('href'),
5656 headers=headers)
5657 catalogItem = XmlElementTree.fromstring(response.content)
5658 entity = [child for child in catalogItem if child.get("type") == "application/vnd.vmware.vcloud.vAppTemplate+xml"][0]
5659 vapp_tempalte_href = entity.get("href")
5660 #get vapp details and parse moref id
5661
5662 namespaces = {"vssd":"http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData" ,
5663 'ovf': 'http://schemas.dmtf.org/ovf/envelope/1',
5664 'vmw': 'http://www.vmware.com/schema/ovf',
5665 'vm': 'http://www.vmware.com/vcloud/v1.5',
5666 'rasd':"http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData",
5667 'vmext':"http://www.vmware.com/vcloud/extension/v1.5",
5668 'xmlns':"http://www.vmware.com/vcloud/v1.5"
5669 }
5670
5671 if vca._session:
5672 response = self.perform_request(req_type='GET',
5673 url=vapp_tempalte_href,
5674 headers=headers)
5675
5676 if response.status_code != requests.codes.ok:
5677 self.logger.debug("REST API call {} failed. Return status code {}".format(
5678 vapp_tempalte_href, response.status_code))
5679
5680 else:
5681 xmlroot_respond = XmlElementTree.fromstring(response.content)
5682 children_section = xmlroot_respond.find('vm:Children/', namespaces)
5683 if children_section is not None:
5684 vCloud_extension_section = children_section.find('xmlns:VCloudExtension', namespaces)
5685 if vCloud_extension_section is not None:
5686 vm_vcenter_info = {}
5687 vim_info = vCloud_extension_section.find('vmext:VmVimInfo', namespaces)
5688 vmext = vim_info.find('vmext:VmVimObjectRef', namespaces)
5689 if vmext is not None:
5690 vm_vcenter_info["vm_moref_id"] = vmext.find('vmext:MoRef', namespaces).text
5691 parsed_response["vm_vcenter_info"]= vm_vcenter_info
5692
5693 except Exception as exp :
5694 self.logger.info("Error occurred calling rest api for getting vApp details {}".format(exp))
5695
5696 return parsed_response
5697
5698
5699 def rollback_newvm(self, vapp_uuid, msg , exp_type="Genric"):
5700 """
5701 Method to delete vApp
5702 Args :
5703 vapp_uuid - vApp UUID
5704 msg - Error message to be logged
5705 exp_type : Exception type
5706 Returns:
5707 None
5708 """
5709 if vapp_uuid:
5710 status = self.delete_vminstance(vapp_uuid)
5711 else:
5712 msg = "No vApp ID"
5713 self.logger.error(msg)
5714 if exp_type == "Genric":
5715 raise vimconn.vimconnException(msg)
5716 elif exp_type == "NotFound":
5717 raise vimconn.vimconnNotFoundException(message=msg)
5718
5719 def add_sriov(self, vapp_uuid, sriov_nets, vmname_andid):
5720 """
5721 Method to attach SRIOV adapters to VM
5722
5723 Args:
5724 vapp_uuid - uuid of vApp/VM
5725 sriov_nets - SRIOV devices infromation as specified in VNFD (flavor)
5726 vmname_andid - vmname
5727
5728 Returns:
5729 The status of add SRIOV adapter task , vm object and
5730 vcenter_conect object
5731 """
5732 vm_obj = None
5733 vcenter_conect, content = self.get_vcenter_content()
5734 vm_moref_id = self.get_vm_moref_id(vapp_uuid)
5735
5736 if vm_moref_id:
5737 try:
5738 no_of_sriov_devices = len(sriov_nets)
5739 if no_of_sriov_devices > 0:
5740 #Get VM and its host
5741 host_obj, vm_obj = self.get_vm_obj(content, vm_moref_id)
5742 self.logger.info("VM {} is currently on host {}".format(vm_obj, host_obj))
5743 if host_obj and vm_obj:
5744 #get SRIOV devies from host on which vapp is currently installed
5745 avilable_sriov_devices = self.get_sriov_devices(host_obj,
5746 no_of_sriov_devices,
5747 )
5748
5749 if len(avilable_sriov_devices) == 0:
5750 #find other hosts with active pci devices
5751 new_host_obj , avilable_sriov_devices = self.get_host_and_sriov_devices(
5752 content,
5753 no_of_sriov_devices,
5754 )
5755
5756 if new_host_obj is not None and len(avilable_sriov_devices)> 0:
5757 #Migrate vm to the host where SRIOV devices are available
5758 self.logger.info("Relocate VM {} on new host {}".format(vm_obj,
5759 new_host_obj))
5760 task = self.relocate_vm(new_host_obj, vm_obj)
5761 if task is not None:
5762 result = self.wait_for_vcenter_task(task, vcenter_conect)
5763 self.logger.info("Migrate VM status: {}".format(result))
5764 host_obj = new_host_obj
5765 else:
5766 self.logger.info("Fail to migrate VM : {}".format(result))
5767 raise vimconn.vimconnNotFoundException(
5768 "Fail to migrate VM : {} to host {}".format(
5769 vmname_andid,
5770 new_host_obj)
5771 )
5772
5773 if host_obj is not None and avilable_sriov_devices is not None and len(avilable_sriov_devices)> 0:
5774 #Add SRIOV devices one by one
5775 for sriov_net in sriov_nets:
5776 network_name = sriov_net.get('net_id')
5777 dvs_portgr_name = self.create_dvPort_group(network_name)
5778 if sriov_net.get('type') == "VF" or sriov_net.get('type') == "SR-IOV":
5779 #add vlan ID ,Modify portgroup for vlan ID
5780 self.configure_vlanID(content, vcenter_conect, network_name)
5781
5782 task = self.add_sriov_to_vm(content,
5783 vm_obj,
5784 host_obj,
5785 network_name,
5786 avilable_sriov_devices[0]
5787 )
5788 if task:
5789 status= self.wait_for_vcenter_task(task, vcenter_conect)
5790 if status:
5791 self.logger.info("Added SRIOV {} to VM {}".format(
5792 no_of_sriov_devices,
5793 str(vm_obj)))
5794 else:
5795 self.logger.error("Fail to add SRIOV {} to VM {}".format(
5796 no_of_sriov_devices,
5797 str(vm_obj)))
5798 raise vimconn.vimconnUnexpectedResponse(
5799 "Fail to add SRIOV adapter in VM ".format(str(vm_obj))
5800 )
5801 return True, vm_obj, vcenter_conect
5802 else:
5803 self.logger.error("Currently there is no host with"\
5804 " {} number of avaialble SRIOV "\
5805 "VFs required for VM {}".format(
5806 no_of_sriov_devices,
5807 vmname_andid)
5808 )
5809 raise vimconn.vimconnNotFoundException(
5810 "Currently there is no host with {} "\
5811 "number of avaialble SRIOV devices required for VM {}".format(
5812 no_of_sriov_devices,
5813 vmname_andid))
5814 else:
5815 self.logger.debug("No infromation about SRIOV devices {} ",sriov_nets)
5816
5817 except vmodl.MethodFault as error:
5818 self.logger.error("Error occurred while adding SRIOV {} ",error)
5819 return None, vm_obj, vcenter_conect
5820
5821
5822 def get_sriov_devices(self,host, no_of_vfs):
5823 """
5824 Method to get the details of SRIOV devices on given host
5825 Args:
5826 host - vSphere host object
5827 no_of_vfs - number of VFs needed on host
5828
5829 Returns:
5830 array of SRIOV devices
5831 """
5832 sriovInfo=[]
5833 if host:
5834 for device in host.config.pciPassthruInfo:
5835 if isinstance(device,vim.host.SriovInfo) and device.sriovActive:
5836 if device.numVirtualFunction >= no_of_vfs:
5837 sriovInfo.append(device)
5838 break
5839 return sriovInfo
5840
5841
5842 def get_host_and_sriov_devices(self, content, no_of_vfs):
5843 """
5844 Method to get the details of SRIOV devices infromation on all hosts
5845
5846 Args:
5847 content - vSphere host object
5848 no_of_vfs - number of pci VFs needed on host
5849
5850 Returns:
5851 array of SRIOV devices and host object
5852 """
5853 host_obj = None
5854 sriov_device_objs = None
5855 try:
5856 if content:
5857 container = content.viewManager.CreateContainerView(content.rootFolder,
5858 [vim.HostSystem], True)
5859 for host in container.view:
5860 devices = self.get_sriov_devices(host, no_of_vfs)
5861 if devices:
5862 host_obj = host
5863 sriov_device_objs = devices
5864 break
5865 except Exception as exp:
5866 self.logger.error("Error {} occurred while finding SRIOV devices on host: {}".format(exp, host_obj))
5867
5868 return host_obj,sriov_device_objs
5869
5870
5871 def add_sriov_to_vm(self,content, vm_obj, host_obj, network_name, sriov_device):
5872 """
5873 Method to add SRIOV adapter to vm
5874
5875 Args:
5876 host_obj - vSphere host object
5877 vm_obj - vSphere vm object
5878 content - vCenter content object
5879 network_name - name of distributed virtaul portgroup
5880 sriov_device - SRIOV device info
5881
5882 Returns:
5883 task object
5884 """
5885 devices = []
5886 vnic_label = "sriov nic"
5887 try:
5888 dvs_portgr = self.get_dvport_group(network_name)
5889 network_name = dvs_portgr.name
5890 nic = vim.vm.device.VirtualDeviceSpec()
5891 # VM device
5892 nic.operation = vim.vm.device.VirtualDeviceSpec.Operation.add
5893 nic.device = vim.vm.device.VirtualSriovEthernetCard()
5894 nic.device.addressType = 'assigned'
5895 #nic.device.key = 13016
5896 nic.device.deviceInfo = vim.Description()
5897 nic.device.deviceInfo.label = vnic_label
5898 nic.device.deviceInfo.summary = network_name
5899 nic.device.backing = vim.vm.device.VirtualEthernetCard.NetworkBackingInfo()
5900
5901 nic.device.backing.network = self.get_obj(content, [vim.Network], network_name)
5902 nic.device.backing.deviceName = network_name
5903 nic.device.backing.useAutoDetect = False
5904 nic.device.connectable = vim.vm.device.VirtualDevice.ConnectInfo()
5905 nic.device.connectable.startConnected = True
5906 nic.device.connectable.allowGuestControl = True
5907
5908 nic.device.sriovBacking = vim.vm.device.VirtualSriovEthernetCard.SriovBackingInfo()
5909 nic.device.sriovBacking.physicalFunctionBacking = vim.vm.device.VirtualPCIPassthrough.DeviceBackingInfo()
5910 nic.device.sriovBacking.physicalFunctionBacking.id = sriov_device.id
5911
5912 devices.append(nic)
5913 vmconf = vim.vm.ConfigSpec(deviceChange=devices)
5914 task = vm_obj.ReconfigVM_Task(vmconf)
5915 return task
5916 except Exception as exp:
5917 self.logger.error("Error {} occurred while adding SRIOV adapter in VM: {}".format(exp, vm_obj))
5918 return None
5919
5920
5921 def create_dvPort_group(self, network_name):
5922 """
5923 Method to create disributed virtual portgroup
5924
5925 Args:
5926 network_name - name of network/portgroup
5927
5928 Returns:
5929 portgroup key
5930 """
5931 try:
5932 new_network_name = [network_name, '-', str(uuid.uuid4())]
5933 network_name=''.join(new_network_name)
5934 vcenter_conect, content = self.get_vcenter_content()
5935
5936 dv_switch = self.get_obj(content, [vim.DistributedVirtualSwitch], self.dvs_name)
5937 if dv_switch:
5938 dv_pg_spec = vim.dvs.DistributedVirtualPortgroup.ConfigSpec()
5939 dv_pg_spec.name = network_name
5940
5941 dv_pg_spec.type = vim.dvs.DistributedVirtualPortgroup.PortgroupType.earlyBinding
5942 dv_pg_spec.defaultPortConfig = vim.dvs.VmwareDistributedVirtualSwitch.VmwarePortConfigPolicy()
5943 dv_pg_spec.defaultPortConfig.securityPolicy = vim.dvs.VmwareDistributedVirtualSwitch.SecurityPolicy()
5944 dv_pg_spec.defaultPortConfig.securityPolicy.allowPromiscuous = vim.BoolPolicy(value=False)
5945 dv_pg_spec.defaultPortConfig.securityPolicy.forgedTransmits = vim.BoolPolicy(value=False)
5946 dv_pg_spec.defaultPortConfig.securityPolicy.macChanges = vim.BoolPolicy(value=False)
5947
5948 task = dv_switch.AddDVPortgroup_Task([dv_pg_spec])
5949 self.wait_for_vcenter_task(task, vcenter_conect)
5950
5951 dvPort_group = self.get_obj(content, [vim.dvs.DistributedVirtualPortgroup], network_name)
5952 if dvPort_group:
5953 self.logger.info("Created disributed virtaul port group: {}".format(dvPort_group))
5954 return dvPort_group.key
5955 else:
5956 self.logger.debug("No disributed virtual switch found with name {}".format(network_name))
5957
5958 except Exception as exp:
5959 self.logger.error("Error occurred while creating disributed virtaul port group {}"\
5960 " : {}".format(network_name, exp))
5961 return None
5962
5963 def reconfig_portgroup(self, content, dvPort_group_name , config_info={}):
5964 """
5965 Method to reconfigure disributed virtual portgroup
5966
5967 Args:
5968 dvPort_group_name - name of disributed virtual portgroup
5969 content - vCenter content object
5970 config_info - disributed virtual portgroup configuration
5971
5972 Returns:
5973 task object
5974 """
5975 try:
5976 dvPort_group = self.get_dvport_group(dvPort_group_name)
5977 if dvPort_group:
5978 dv_pg_spec = vim.dvs.DistributedVirtualPortgroup.ConfigSpec()
5979 dv_pg_spec.configVersion = dvPort_group.config.configVersion
5980 dv_pg_spec.defaultPortConfig = vim.dvs.VmwareDistributedVirtualSwitch.VmwarePortConfigPolicy()
5981 if "vlanID" in config_info:
5982 dv_pg_spec.defaultPortConfig.vlan = vim.dvs.VmwareDistributedVirtualSwitch.VlanIdSpec()
5983 dv_pg_spec.defaultPortConfig.vlan.vlanId = config_info.get('vlanID')
5984
5985 task = dvPort_group.ReconfigureDVPortgroup_Task(spec=dv_pg_spec)
5986 return task
5987 else:
5988 return None
5989 except Exception as exp:
5990 self.logger.error("Error occurred while reconfiguraing disributed virtaul port group {}"\
5991 " : {}".format(dvPort_group_name, exp))
5992 return None
5993
5994
5995 def destroy_dvport_group(self , dvPort_group_name):
5996 """
5997 Method to destroy disributed virtual portgroup
5998
5999 Args:
6000 network_name - name of network/portgroup
6001
6002 Returns:
6003 True if portgroup successfully got deleted else false
6004 """
6005 vcenter_conect, content = self.get_vcenter_content()
6006 try:
6007 status = None
6008 dvPort_group = self.get_dvport_group(dvPort_group_name)
6009 if dvPort_group:
6010 task = dvPort_group.Destroy_Task()
6011 status = self.wait_for_vcenter_task(task, vcenter_conect)
6012 return status
6013 except vmodl.MethodFault as exp:
6014 self.logger.error("Caught vmodl fault {} while deleting disributed virtaul port group {}".format(
6015 exp, dvPort_group_name))
6016 return None
6017
6018
6019 def get_dvport_group(self, dvPort_group_name):
6020 """
6021 Method to get disributed virtual portgroup
6022
6023 Args:
6024 network_name - name of network/portgroup
6025
6026 Returns:
6027 portgroup object
6028 """
6029 vcenter_conect, content = self.get_vcenter_content()
6030 dvPort_group = None
6031 try:
6032 container = content.viewManager.CreateContainerView(content.rootFolder, [vim.dvs.DistributedVirtualPortgroup], True)
6033 for item in container.view:
6034 if item.key == dvPort_group_name:
6035 dvPort_group = item
6036 break
6037 return dvPort_group
6038 except vmodl.MethodFault as exp:
6039 self.logger.error("Caught vmodl fault {} for disributed virtaul port group {}".format(
6040 exp, dvPort_group_name))
6041 return None
6042
6043 def get_vlanID_from_dvs_portgr(self, dvPort_group_name):
6044 """
6045 Method to get disributed virtual portgroup vlanID
6046
6047 Args:
6048 network_name - name of network/portgroup
6049
6050 Returns:
6051 vlan ID
6052 """
6053 vlanId = None
6054 try:
6055 dvPort_group = self.get_dvport_group(dvPort_group_name)
6056 if dvPort_group:
6057 vlanId = dvPort_group.config.defaultPortConfig.vlan.vlanId
6058 except vmodl.MethodFault as exp:
6059 self.logger.error("Caught vmodl fault {} for disributed virtaul port group {}".format(
6060 exp, dvPort_group_name))
6061 return vlanId
6062
6063
6064 def configure_vlanID(self, content, vcenter_conect, dvPort_group_name):
6065 """
6066 Method to configure vlanID in disributed virtual portgroup vlanID
6067
6068 Args:
6069 network_name - name of network/portgroup
6070
6071 Returns:
6072 None
6073 """
6074 vlanID = self.get_vlanID_from_dvs_portgr(dvPort_group_name)
6075 if vlanID == 0:
6076 #configure vlanID
6077 vlanID = self.genrate_vlanID(dvPort_group_name)
6078 config = {"vlanID":vlanID}
6079 task = self.reconfig_portgroup(content, dvPort_group_name,
6080 config_info=config)
6081 if task:
6082 status= self.wait_for_vcenter_task(task, vcenter_conect)
6083 if status:
6084 self.logger.info("Reconfigured Port group {} for vlan ID {}".format(
6085 dvPort_group_name,vlanID))
6086 else:
6087 self.logger.error("Fail reconfigure portgroup {} for vlanID{}".format(
6088 dvPort_group_name, vlanID))
6089
6090
6091 def genrate_vlanID(self, network_name):
6092 """
6093 Method to get unused vlanID
6094 Args:
6095 network_name - name of network/portgroup
6096 Returns:
6097 vlanID
6098 """
6099 vlan_id = None
6100 used_ids = []
6101 if self.config.get('vlanID_range') == None:
6102 raise vimconn.vimconnConflictException("You must provide a 'vlanID_range' "\
6103 "at config value before creating sriov network with vlan tag")
6104 if "used_vlanIDs" not in self.persistent_info:
6105 self.persistent_info["used_vlanIDs"] = {}
6106 else:
6107 used_ids = self.persistent_info["used_vlanIDs"].values()
6108 #For python3
6109 #used_ids = list(self.persistent_info["used_vlanIDs"].values())
6110
6111 for vlanID_range in self.config.get('vlanID_range'):
6112 start_vlanid , end_vlanid = vlanID_range.split("-")
6113 if start_vlanid > end_vlanid:
6114 raise vimconn.vimconnConflictException("Invalid vlan ID range {}".format(
6115 vlanID_range))
6116
6117 for id in xrange(int(start_vlanid), int(end_vlanid) + 1):
6118 #For python3
6119 #for id in range(int(start_vlanid), int(end_vlanid) + 1):
6120 if id not in used_ids:
6121 vlan_id = id
6122 self.persistent_info["used_vlanIDs"][network_name] = vlan_id
6123 return vlan_id
6124 if vlan_id is None:
6125 raise vimconn.vimconnConflictException("All Vlan IDs are in use")
6126
6127
6128 def get_obj(self, content, vimtype, name):
6129 """
6130 Get the vsphere object associated with a given text name
6131 """
6132 obj = None
6133 container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True)
6134 for item in container.view:
6135 if item.name == name:
6136 obj = item
6137 break
6138 return obj
6139
6140
6141 def insert_media_to_vm(self, vapp, image_id):
6142 """
6143 Method to insert media CD-ROM (ISO image) from catalog to vm.
6144 vapp - vapp object to get vm id
6145 Image_id - image id for cdrom to be inerted to vm
6146 """
6147 # create connection object
6148 vca = self.connect()
6149 try:
6150 # fetching catalog details
6151 rest_url = "{}/api/catalog/{}".format(self.url, image_id)
6152 if vca._session:
6153 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
6154 'x-vcloud-authorization': vca._session.headers['x-vcloud-authorization']}
6155 response = self.perform_request(req_type='GET',
6156 url=rest_url,
6157 headers=headers)
6158
6159 if response.status_code != 200:
6160 self.logger.error("REST call {} failed reason : {}"\
6161 "status code : {}".format(url_rest_call,
6162 response.content,
6163 response.status_code))
6164 raise vimconn.vimconnException("insert_media_to_vm(): Failed to get "\
6165 "catalog details")
6166 # searching iso name and id
6167 iso_name,media_id = self.get_media_details(vca, response.content)
6168
6169 if iso_name and media_id:
6170 data ="""<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
6171 <ns6:MediaInsertOrEjectParams
6172 xmlns="http://www.vmware.com/vcloud/versions" xmlns:ns2="http://schemas.dmtf.org/ovf/envelope/1" xmlns:ns3="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData" xmlns:ns4="http://schemas.dmtf.org/wbem/wscim/1/common" xmlns:ns5="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData" xmlns:ns6="http://www.vmware.com/vcloud/v1.5" xmlns:ns7="http://www.vmware.com/schema/ovf" xmlns:ns8="http://schemas.dmtf.org/ovf/environment/1" xmlns:ns9="http://www.vmware.com/vcloud/extension/v1.5">
6173 <ns6:Media
6174 type="application/vnd.vmware.vcloud.media+xml"
6175 name="{}.iso"
6176 id="urn:vcloud:media:{}"
6177 href="https://{}/api/media/{}"/>
6178 </ns6:MediaInsertOrEjectParams>""".format(iso_name, media_id,
6179 self.url,media_id)
6180
6181 for vms in vapp.get_all_vms():
6182 vm_id = vms.get('id').split(':')[-1]
6183
6184 headers['Content-Type'] = 'application/vnd.vmware.vcloud.mediaInsertOrEjectParams+xml'
6185 rest_url = "{}/api/vApp/vm-{}/media/action/insertMedia".format(self.url,vm_id)
6186
6187 response = self.perform_request(req_type='POST',
6188 url=rest_url,
6189 data=data,
6190 headers=headers)
6191
6192 if response.status_code != 202:
6193 self.logger.error("Failed to insert CD-ROM to vm")
6194 raise vimconn.vimconnException("insert_media_to_vm() : Failed to insert"\
6195 "ISO image to vm")
6196 else:
6197 task = self.get_task_from_response(response.content)
6198 result = self.client.get_task_monitor().wait_for_success(task=task)
6199 if result.get('status') == 'success':
6200 self.logger.info("insert_media_to_vm(): Sucessfully inserted media ISO"\
6201 " image to vm {}".format(vm_id))
6202
6203 except Exception as exp:
6204 self.logger.error("insert_media_to_vm() : exception occurred "\
6205 "while inserting media CD-ROM")
6206 raise vimconn.vimconnException(message=exp)
6207
6208
6209 def get_media_details(self, vca, content):
6210 """
6211 Method to get catalog item details
6212 vca - connection object
6213 content - Catalog details
6214 Return - Media name, media id
6215 """
6216 cataloghref_list = []
6217 try:
6218 if content:
6219 vm_list_xmlroot = XmlElementTree.fromstring(content)
6220 for child in vm_list_xmlroot.iter():
6221 if 'CatalogItem' in child.tag:
6222 cataloghref_list.append(child.attrib.get('href'))
6223 if cataloghref_list is not None:
6224 for href in cataloghref_list:
6225 if href:
6226 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
6227 'x-vcloud-authorization': vca._session.headers['x-vcloud-authorization']}
6228 response = self.perform_request(req_type='GET',
6229 url=href,
6230 headers=headers)
6231 if response.status_code != 200:
6232 self.logger.error("REST call {} failed reason : {}"\
6233 "status code : {}".format(href,
6234 response.content,
6235 response.status_code))
6236 raise vimconn.vimconnException("get_media_details : Failed to get "\
6237 "catalogitem details")
6238 list_xmlroot = XmlElementTree.fromstring(response.content)
6239 for child in list_xmlroot.iter():
6240 if 'Entity' in child.tag:
6241 if 'media' in child.attrib.get('href'):
6242 name = child.attrib.get('name')
6243 media_id = child.attrib.get('href').split('/').pop()
6244 return name,media_id
6245 else:
6246 self.logger.debug("Media name and id not found")
6247 return False,False
6248 except Exception as exp:
6249 self.logger.error("get_media_details : exception occurred "\
6250 "getting media details")
6251 raise vimconn.vimconnException(message=exp)
6252
6253
6254 def retry_rest(self, method, url, add_headers=None, data=None):
6255 """ Method to get Token & retry respective REST request
6256 Args:
6257 api - REST API - Can be one of 'GET' or 'PUT' or 'POST'
6258 url - request url to be used
6259 add_headers - Additional headers (optional)
6260 data - Request payload data to be passed in request
6261 Returns:
6262 response - Response of request
6263 """
6264 response = None
6265
6266 #Get token
6267 self.get_token()
6268
6269 if self.client._session:
6270 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
6271 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
6272
6273 if add_headers:
6274 headers.update(add_headers)
6275
6276 if method == 'GET':
6277 response = self.perform_request(req_type='GET',
6278 url=url,
6279 headers=headers)
6280 elif method == 'PUT':
6281 response = self.perform_request(req_type='PUT',
6282 url=url,
6283 headers=headers,
6284 data=data)
6285 elif method == 'POST':
6286 response = self.perform_request(req_type='POST',
6287 url=url,
6288 headers=headers,
6289 data=data)
6290 elif method == 'DELETE':
6291 response = self.perform_request(req_type='DELETE',
6292 url=url,
6293 headers=headers)
6294 return response
6295
6296
6297 def get_token(self):
6298 """ Generate a new token if expired
6299
6300 Returns:
6301 The return client object that letter can be used to connect to vCloud director as admin for VDC
6302 """
6303 try:
6304 self.logger.debug("Generate token for vca {} as {} to datacenter {}.".format(self.org_name,
6305 self.user,
6306 self.org_name))
6307 host = self.url
6308 client = Client(host, verify_ssl_certs=False)
6309 client.set_credentials(BasicLoginCredentials(self.user, self.org_name, self.passwd))
6310 # connection object
6311 self.client = client
6312
6313 except:
6314 raise vimconn.vimconnConnectionException("Can't connect to a vCloud director org: "
6315 "{} as user: {}".format(self.org_name, self.user))
6316
6317 if not client:
6318 raise vimconn.vimconnConnectionException("Failed while reconnecting vCD")
6319
6320
6321 def get_vdc_details(self):
6322 """ Get VDC details using pyVcloud Lib
6323
6324 Returns org and vdc object
6325 """
6326 org = Org(self.client, resource=self.client.get_org())
6327 vdc = org.get_vdc(self.tenant_name)
6328
6329 #Retry once, if failed by refreshing token
6330 if vdc is None:
6331 self.get_token()
6332 vdc = org.get_vdc(self.tenant_name)
6333
6334 return org, vdc
6335
6336
6337 def perform_request(self, req_type, url, headers=None, data=None):
6338 """Perform the POST/PUT/GET/DELETE request."""
6339
6340 #Log REST request details
6341 self.log_request(req_type, url=url, headers=headers, data=data)
6342 # perform request and return its result
6343 if req_type == 'GET':
6344 response = requests.get(url=url,
6345 headers=headers,
6346 verify=False)
6347 elif req_type == 'PUT':
6348 response = requests.put(url=url,
6349 headers=headers,
6350 data=data,
6351 verify=False)
6352 elif req_type == 'POST':
6353 response = requests.post(url=url,
6354 headers=headers,
6355 data=data,
6356 verify=False)
6357 elif req_type == 'DELETE':
6358 response = requests.delete(url=url,
6359 headers=headers,
6360 verify=False)
6361 #Log the REST response
6362 self.log_response(response)
6363
6364 return response
6365
6366
6367 def log_request(self, req_type, url=None, headers=None, data=None):
6368 """Logs REST request details"""
6369
6370 if req_type is not None:
6371 self.logger.debug("Request type: {}".format(req_type))
6372
6373 if url is not None:
6374 self.logger.debug("Request url: {}".format(url))
6375
6376 if headers is not None:
6377 for header in headers:
6378 self.logger.debug("Request header: {}: {}".format(header, headers[header]))
6379
6380 if data is not None:
6381 self.logger.debug("Request data: {}".format(data))
6382
6383
6384 def log_response(self, response):
6385 """Logs REST response details"""
6386
6387 self.logger.debug("Response status code: {} ".format(response.status_code))
6388
6389
6390 def get_task_from_response(self, content):
6391 """
6392 content - API response content(response.content)
6393 return task object
6394 """
6395 xmlroot = XmlElementTree.fromstring(content)
6396 if xmlroot.tag.split('}')[1] == "Task":
6397 return xmlroot
6398 else:
6399 for ele in xmlroot:
6400 if ele.tag.split("}")[1] == "Tasks":
6401 task = ele[0]
6402 break
6403 return task
6404
6405
6406 def power_on_vapp(self,vapp_id, vapp_name):
6407 """
6408 vapp_id - vApp uuid
6409 vapp_name - vAapp name
6410 return - Task object
6411 """
6412 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
6413 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
6414
6415 poweron_href = "{}/api/vApp/vapp-{}/power/action/powerOn".format(self.url,
6416 vapp_id)
6417 response = self.perform_request(req_type='POST',
6418 url=poweron_href,
6419 headers=headers)
6420
6421 if response.status_code != 202:
6422 self.logger.error("REST call {} failed reason : {}"\
6423 "status code : {} ".format(poweron_href,
6424 response.content,
6425 response.status_code))
6426 raise vimconn.vimconnException("power_on_vapp() : Failed to power on "\
6427 "vApp {}".format(vapp_name))
6428 else:
6429 poweron_task = self.get_task_from_response(response.content)
6430 return poweron_task
6431
6432