blob: c65482bcd70ff6b58efa31806d03ab6fe5298ca3 [file] [log] [blame]
bayramov325fa1c2016-09-08 01:42:46 -07001# -*- coding: utf-8 -*-
2
3##
bhangare1a0b97c2017-06-21 02:20:15 -07004# Copyright 2016-2017 VMware Inc.
5# This file is part of ETSI OSM
bayramov325fa1c2016-09-08 01:42:46 -07006# 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
bhangare1a0b97c2017-06-21 02:20:15 -070021# contact: osslegalrouting@vmware.com
bayramov325fa1c2016-09-08 01:42:46 -070022##
23
bayramov5761ad12016-10-04 09:00:30 +040024"""
bayramov325fa1c2016-09-08 01:42:46 -070025vimconn_vmware implementation an Abstract class in order to interact with VMware vCloud Director.
26mbayramov@vmware.com
bayramov5761ad12016-10-04 09:00:30 +040027"""
bayramovfe3f3c92016-10-04 07:53:41 +040028from progressbar import Percentage, Bar, ETA, FileTransferSpeed, ProgressBar
bayramovbd6160f2016-09-28 04:12:05 +040029
30import vimconn
bayramov325fa1c2016-09-08 01:42:46 -070031import os
bayramovef390722016-09-27 03:34:46 -070032import traceback
bayramovef390722016-09-27 03:34:46 -070033import itertools
bayramov325fa1c2016-09-08 01:42:46 -070034import requests
bhangarefda5f7c2017-01-12 23:50:34 -080035import ssl
36import atexit
37
38from pyVmomi import vim, vmodl
39from pyVim.connect import SmartConnect, Disconnect
bayramov325fa1c2016-09-08 01:42:46 -070040
bayramovef390722016-09-27 03:34:46 -070041from xml.etree import ElementTree as XmlElementTree
bhangarea92ae392017-01-12 22:30:29 -080042from lxml import etree as lxmlElementTree
bayramov325fa1c2016-09-08 01:42:46 -070043
bayramovef390722016-09-27 03:34:46 -070044import yaml
kasarc5bf2932018-03-09 04:15:22 -080045from pyvcloud.vcd.client import BasicLoginCredentials,Client,VcdTaskException
46from pyvcloud.vcd.vdc import VDC
47from pyvcloud.vcd.org import Org
48import re
49from pyvcloud.vcd.vapp import VApp
bayramov325fa1c2016-09-08 01:42:46 -070050from xml.sax.saxutils import escape
bayramov325fa1c2016-09-08 01:42:46 -070051import logging
52import json
bayramov325fa1c2016-09-08 01:42:46 -070053import time
54import uuid
55import httplib
kasarc5bf2932018-03-09 04:15:22 -080056#For python3
57#import http.client
kate15f1c382016-12-15 01:12:40 -080058import hashlib
bhangare0e571a92017-01-12 04:02:23 -080059import socket
60import struct
61import netaddr
kasarde691232017-03-25 03:37:31 -070062import random
bayramov325fa1c2016-09-08 01:42:46 -070063
bayramovbd6160f2016-09-28 04:12:05 +040064# global variable for vcd connector type
65STANDALONE = 'standalone'
66
kate15f1c382016-12-15 01:12:40 -080067# key for flavor dicts
68FLAVOR_RAM_KEY = 'ram'
69FLAVOR_VCPUS_KEY = 'vcpus'
bhangarea92ae392017-01-12 22:30:29 -080070FLAVOR_DISK_KEY = 'disk'
kasarde691232017-03-25 03:37:31 -070071DEFAULT_IP_PROFILE = {'dhcp_count':50,
bhangare0e571a92017-01-12 04:02:23 -080072 'dhcp_enabled':True,
kasarde691232017-03-25 03:37:31 -070073 'ip_version':"IPv4"
bhangare0e571a92017-01-12 04:02:23 -080074 }
75# global variable for wait time
kate13ab2c42016-12-23 01:34:24 -080076INTERVAL_TIME = 5
77MAX_WAIT_TIME = 1800
bayramov325fa1c2016-09-08 01:42:46 -070078
kasarc5bf2932018-03-09 04:15:22 -080079API_VERSION = '5.9'
bayramovbd6160f2016-09-28 04:12:05 +040080
kasarc5bf2932018-03-09 04:15:22 -080081__author__ = "Mustafa Bayramov, Arpita Kate, Sachin Bhangare, Prakash Kasar"
82__date__ = "$09-Mar-2018 11:09:29$"
83__version__ = '0.2'
bayramov325fa1c2016-09-08 01:42:46 -070084
bayramovef390722016-09-27 03:34:46 -070085# -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
104vcdStatusCode2manoFormat = {4: 'ACTIVE',
105 7: 'PAUSED',
106 3: 'SUSPENDED',
107 8: 'INACTIVE',
108 12: 'BUILD',
109 -1: 'ERROR',
110 14: 'DELETED'}
111
112#
113netStatus2manoFormat = {'ACTIVE': 'ACTIVE', 'PAUSED': 'PAUSED', 'INACTIVE': 'INACTIVE', 'BUILD': 'BUILD',
114 'ERROR': 'ERROR', 'DELETED': 'DELETED'
115 }
116
bayramovbd6160f2016-09-28 04:12:05 +0400117class vimconnector(vimconn.vimconnector):
kateeb044522017-03-06 23:54:39 -0800118 # dict used to store flavor in memory
119 flavorlist = {}
120
bayramovbd6160f2016-09-28 04:12:05 +0400121 def __init__(self, uuid=None, name=None, tenant_id=None, tenant_name=None,
tiernob3d36742017-03-03 23:51:05 +0100122 url=None, url_admin=None, user=None, passwd=None, log_level=None, config={}, persistent_info={}):
bayramovb6ffe792016-09-28 11:50:56 +0400123 """
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']
kateeb044522017-03-06 23:54:39 -0800149 config - Provide NSX and vCenter information
bayramovb6ffe792016-09-28 11:50:56 +0400150
151 Returns:
152 Nothing.
153 """
154
bayramovbd6160f2016-09-28 04:12:05 +0400155 vimconn.vimconnector.__init__(self, uuid, name, tenant_id, tenant_name, url,
156 url_admin, user, passwd, log_level, config)
kate15f1c382016-12-15 01:12:40 -0800157
158 self.logger = logging.getLogger('openmano.vim.vmware')
159 self.logger.setLevel(10)
tiernob3d36742017-03-03 23:51:05 +0100160 self.persistent_info = persistent_info
kate15f1c382016-12-15 01:12:40 -0800161
bayramovef390722016-09-27 03:34:46 -0700162 self.name = name
kate15f1c382016-12-15 01:12:40 -0800163 self.id = uuid
bayramovef390722016-09-27 03:34:46 -0700164 self.url = url
bayramov325fa1c2016-09-08 01:42:46 -0700165 self.url_admin = url_admin
166 self.tenant_id = tenant_id
167 self.tenant_name = tenant_name
bayramovef390722016-09-27 03:34:46 -0700168 self.user = user
169 self.passwd = passwd
170 self.config = config
171 self.admin_password = None
172 self.admin_user = None
kate15f1c382016-12-15 01:12:40 -0800173 self.org_name = ""
bhangare985a1fd2017-01-31 01:53:21 -0800174 self.nsx_manager = None
175 self.nsx_user = None
176 self.nsx_password = None
sbhangarea8e5b782018-06-21 02:10:03 -0700177 self.availability_zone = None
bayramovef390722016-09-27 03:34:46 -0700178
kasarc5bf2932018-03-09 04:15:22 -0800179 # Disable warnings from self-signed certificates.
180 requests.packages.urllib3.disable_warnings()
181
kate15f1c382016-12-15 01:12:40 -0800182 if tenant_name is not None:
183 orgnameandtenant = tenant_name.split(":")
184 if len(orgnameandtenant) == 2:
katec324e002016-12-23 00:54:47 -0800185 self.tenant_name = orgnameandtenant[1]
186 self.org_name = orgnameandtenant[0]
kate15f1c382016-12-15 01:12:40 -0800187 else:
188 self.tenant_name = tenant_name
bhangarea92ae392017-01-12 22:30:29 -0800189 if "orgname" in config:
kate15f1c382016-12-15 01:12:40 -0800190 self.org_name = config['orgname']
katec324e002016-12-23 00:54:47 -0800191
tiernofe789902016-09-29 14:20:44 +0000192 if log_level:
bayramov5761ad12016-10-04 09:00:30 +0400193 self.logger.setLevel(getattr(logging, log_level))
bayramov325fa1c2016-09-08 01:42:46 -0700194
bayramovef390722016-09-27 03:34:46 -0700195 try:
196 self.admin_user = config['admin_username']
197 self.admin_password = config['admin_password']
198 except KeyError:
bayramovbd6160f2016-09-28 04:12:05 +0400199 raise vimconn.vimconnException(message="Error admin username or admin password is empty.")
bayramov325fa1c2016-09-08 01:42:46 -0700200
bhangare985a1fd2017-01-31 01:53:21 -0800201 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
kateeb044522017-03-06 23:54:39 -0800208 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
sbhangarea8e5b782018-06-21 02:10:03 -0700213 #Set availability zone for Affinity rules
214 self.availability_zone = self.set_availability_zones()
215
kateac1e3792017-04-01 02:16:39 -0700216# ############# 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
bayramovef390722016-09-27 03:34:46 -0700224 self.org_uuid = None
kasarc5bf2932018-03-09 04:15:22 -0800225 self.client = None
bayramov325fa1c2016-09-08 01:42:46 -0700226
227 if not url:
bayramov5761ad12016-10-04 09:00:30 +0400228 raise vimconn.vimconnException('url param can not be NoneType')
bayramov325fa1c2016-09-08 01:42:46 -0700229
bayramovef390722016-09-27 03:34:46 -0700230 if not self.url_admin: # try to use normal url
bayramov325fa1c2016-09-08 01:42:46 -0700231 self.url_admin = self.url
232
kate15f1c382016-12-15 01:12:40 -0800233 logging.debug("UUID: {} name: {} tenant_id: {} tenant name {}".format(self.id, self.org_name,
bayramovef390722016-09-27 03:34:46 -0700234 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))
bayramov325fa1c2016-09-08 01:42:46 -0700237
bayramovef390722016-09-27 03:34:46 -0700238 # initialize organization
bayramovbd6160f2016-09-28 04:12:05 +0400239 if self.user is not None and self.passwd is not None and self.url:
240 self.init_organization()
bayramovef390722016-09-27 03:34:46 -0700241
242 def __getitem__(self, index):
kate15f1c382016-12-15 01:12:40 -0800243 if index == 'name':
244 return self.name
bayramovef390722016-09-27 03:34:46 -0700245 if index == 'tenant_id':
bayramov325fa1c2016-09-08 01:42:46 -0700246 return self.tenant_id
bayramovef390722016-09-27 03:34:46 -0700247 if index == 'tenant_name':
bayramov325fa1c2016-09-08 01:42:46 -0700248 return self.tenant_name
bayramovef390722016-09-27 03:34:46 -0700249 elif index == 'id':
bayramov325fa1c2016-09-08 01:42:46 -0700250 return self.id
bayramovef390722016-09-27 03:34:46 -0700251 elif index == 'org_name':
252 return self.org_name
253 elif index == 'org_uuid':
254 return self.org_uuid
255 elif index == 'user':
bayramov325fa1c2016-09-08 01:42:46 -0700256 return self.user
bayramovef390722016-09-27 03:34:46 -0700257 elif index == 'passwd':
bayramov325fa1c2016-09-08 01:42:46 -0700258 return self.passwd
bayramovef390722016-09-27 03:34:46 -0700259 elif index == 'url':
bayramov325fa1c2016-09-08 01:42:46 -0700260 return self.url
bayramovef390722016-09-27 03:34:46 -0700261 elif index == 'url_admin':
bayramov325fa1c2016-09-08 01:42:46 -0700262 return self.url_admin
bayramovef390722016-09-27 03:34:46 -0700263 elif index == "config":
bayramov325fa1c2016-09-08 01:42:46 -0700264 return self.config
265 else:
bayramovef390722016-09-27 03:34:46 -0700266 raise KeyError("Invalid key '%s'" % str(index))
bayramov325fa1c2016-09-08 01:42:46 -0700267
bayramovef390722016-09-27 03:34:46 -0700268 def __setitem__(self, index, value):
kate15f1c382016-12-15 01:12:40 -0800269 if index == 'name':
270 self.name = value
bayramovef390722016-09-27 03:34:46 -0700271 if index == 'tenant_id':
bayramov325fa1c2016-09-08 01:42:46 -0700272 self.tenant_id = value
bayramovef390722016-09-27 03:34:46 -0700273 if index == 'tenant_name':
bayramov325fa1c2016-09-08 01:42:46 -0700274 self.tenant_name = value
bayramovef390722016-09-27 03:34:46 -0700275 elif index == 'id':
bayramov325fa1c2016-09-08 01:42:46 -0700276 self.id = value
bayramovef390722016-09-27 03:34:46 -0700277 elif index == 'org_name':
278 self.org_name = value
bayramovef390722016-09-27 03:34:46 -0700279 elif index == 'org_uuid':
kate15f1c382016-12-15 01:12:40 -0800280 self.org_uuid = value
bayramovef390722016-09-27 03:34:46 -0700281 elif index == 'user':
bayramov325fa1c2016-09-08 01:42:46 -0700282 self.user = value
bayramovef390722016-09-27 03:34:46 -0700283 elif index == 'passwd':
bayramov325fa1c2016-09-08 01:42:46 -0700284 self.passwd = value
bayramovef390722016-09-27 03:34:46 -0700285 elif index == 'url':
bayramov325fa1c2016-09-08 01:42:46 -0700286 self.url = value
bayramovef390722016-09-27 03:34:46 -0700287 elif index == 'url_admin':
bayramov325fa1c2016-09-08 01:42:46 -0700288 self.url_admin = value
289 else:
bayramovef390722016-09-27 03:34:46 -0700290 raise KeyError("Invalid key '%s'" % str(index))
bayramov325fa1c2016-09-08 01:42:46 -0700291
bayramovef390722016-09-27 03:34:46 -0700292 def connect_as_admin(self):
bayramovb6ffe792016-09-28 11:50:56 +0400293 """ 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.
bayramovef390722016-09-27 03:34:46 -0700296
297 Returns:
kasarc5bf2932018-03-09 04:15:22 -0800298 The return client object that latter can be used to connect to vcloud director as admin for provider vdc
bayramovef390722016-09-27 03:34:46 -0700299 """
300
kasarc5bf2932018-03-09 04:15:22 -0800301 self.logger.debug("Logging into vCD {} as admin.".format(self.org_name))
bayramov325fa1c2016-09-08 01:42:46 -0700302
kasarc5bf2932018-03-09 04:15:22 -0800303 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))
bayramov325fa1c2016-09-08 01:42:46 -0700311
kasarc5bf2932018-03-09 04:15:22 -0800312 return client_as_admin
bayramov325fa1c2016-09-08 01:42:46 -0700313
bayramovef390722016-09-27 03:34:46 -0700314 def connect(self):
315 """ Method connect as normal user to vCloud director.
316
317 Returns:
kasarc5bf2932018-03-09 04:15:22 -0800318 The return client object that latter can be used to connect to vCloud director as admin for VDC
bayramovef390722016-09-27 03:34:46 -0700319 """
320
bayramovb6ffe792016-09-28 11:50:56 +0400321 try:
kasarc5bf2932018-03-09 04:15:22 -0800322 self.logger.debug("Logging into vCD {} as {} to datacenter {}.".format(self.org_name,
kate15f1c382016-12-15 01:12:40 -0800323 self.user,
324 self.org_name))
kasarc5bf2932018-03-09 04:15:22 -0800325 host = self.url
326 client = Client(host, verify_ssl_certs=False)
327 client.set_credentials(BasicLoginCredentials(self.user, self.org_name, self.passwd))
bayramovb6ffe792016-09-28 11:50:56 +0400328 except:
kate15f1c382016-12-15 01:12:40 -0800329 raise vimconn.vimconnConnectionException("Can't connect to a vCloud director org: "
330 "{} as user: {}".format(self.org_name, self.user))
bayramov325fa1c2016-09-08 01:42:46 -0700331
kasarc5bf2932018-03-09 04:15:22 -0800332 return client
bayramov325fa1c2016-09-08 01:42:46 -0700333
bayramovbd6160f2016-09-28 04:12:05 +0400334 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.
bayramov325fa1c2016-09-08 01:42:46 -0700341
bayramovef390722016-09-27 03:34:46 -0700342 Returns:
343 The return vca object that letter can be used to connect to vcloud direct as admin
344 """
kasarc5bf2932018-03-09 04:15:22 -0800345 client = self.connect()
346 if not client:
347 raise vimconn.vimconnConnectionException("Failed to connect vCD.")
bhangare1a0b97c2017-06-21 02:20:15 -0700348
kasarc5bf2932018-03-09 04:15:22 -0800349 self.client = client
bayramovef390722016-09-27 03:34:46 -0700350 try:
351 if self.org_uuid is None:
kasarc5bf2932018-03-09 04:15:22 -0800352 org_list = client.get_org_list()
353 for org in org_list.Org:
bayramovbd6160f2016-09-28 04:12:05 +0400354 # we set org UUID at the init phase but we can do it only when we have valid credential.
kasarc5bf2932018-03-09 04:15:22 -0800355 if org.get('name') == self.org_name:
356 self.org_uuid = org.get('href').split('/')[-1]
bayramovbd6160f2016-09-28 04:12:05 +0400357 self.logger.debug("Setting organization UUID {}".format(self.org_uuid))
358 break
bayramovbd6160f2016-09-28 04:12:05 +0400359 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
bayramov5761ad12016-10-04 09:00:30 +0400367 if self.tenant_id is None and self.tenant_name is not None and 'vdcs' in org_details_dict:
bayramovbd6160f2016-09-28 04:12:05 +0400368 vdcs_dict = org_details_dict['vdcs']
bayramovbd6160f2016-09-28 04:12:05 +0400369 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,
kate15f1c382016-12-15 01:12:40 -0800373 self.org_name))
bayramovbd6160f2016-09-28 04:12:05 +0400374 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.
bayramov5761ad12016-10-04 09:00:30 +0400378 if self.tenant_id is not None and self.tenant_name is None and 'vdcs' in org_details_dict:
bayramovbd6160f2016-09-28 04:12:05 +0400379 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,
kate15f1c382016-12-15 01:12:40 -0800384 self.org_name))
bayramovbd6160f2016-09-28 04:12:05 +0400385 break
386 else:
387 raise vimconn.vimconnException("Tenant id indicated but not present in vcloud director")
bayramovef390722016-09-27 03:34:46 -0700388 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
bayramov325fa1c2016-09-08 01:42:46 -0700393
bayramovef390722016-09-27 03:34:46 -0700394 def new_tenant(self, tenant_name=None, tenant_description=None):
bayramovb6ffe792016-09-28 11:50:56 +0400395 """ Method adds a new tenant to VIM with this name.
396 This action requires access to create VDC action in vCloud director.
bayramovef390722016-09-27 03:34:46 -0700397
bayramovb6ffe792016-09-28 11:50:56 +0400398 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
bayramovbd6160f2016-09-28 04:12:05 +0400405 """
bayramovef390722016-09-27 03:34:46 -0700406 vdc_task = self.create_vdc(vdc_name=tenant_name)
407 if vdc_task is not None:
408 vdc_uuid, value = vdc_task.popitem()
kasarc5bf2932018-03-09 04:15:22 -0800409 self.logger.info("Created new vdc {} and uuid: {}".format(tenant_name, vdc_uuid))
bayramovef390722016-09-27 03:34:46 -0700410 return vdc_uuid
411 else:
bayramovbd6160f2016-09-28 04:12:05 +0400412 raise vimconn.vimconnException("Failed create tenant {}".format(tenant_name))
bayramovef390722016-09-27 03:34:46 -0700413
bayramov163f1ae2016-09-28 17:16:55 +0400414 def delete_tenant(self, tenant_id=None):
kated47ad5f2017-08-03 02:16:13 -0700415 """ 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:
kasarc5bf2932018-03-09 04:15:22 -0800425 raise vimconn.vimconnConnectionException("Failed to connect vCD")
kated47ad5f2017-08-03 02:16:13 -0700426
427 if tenant_id is not None:
kasarc5bf2932018-03-09 04:15:22 -0800428 if vca._session:
kated47ad5f2017-08-03 02:16:13 -0700429 #Get OrgVDC
kasarc5bf2932018-03-09 04:15:22 -0800430 url_list = [self.url, '/api/vdc/', tenant_id]
kated47ad5f2017-08-03 02:16:13 -0700431 orgvdc_herf = ''.join(url_list)
kasarc5bf2932018-03-09 04:15:22 -0800432
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)
kated47ad5f2017-08-03 02:16:13 -0700438
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}
kasarc5bf2932018-03-09 04:15:22 -0800447 #For python3
448 #namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.items() if prefix}
kated47ad5f2017-08-03 02:16:13 -0700449 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
kasarc5bf2932018-03-09 04:15:22 -0800453 response = self.perform_request(req_type='DELETE',
454 url=vdc_remove_href,
455 headers=headers)
kated47ad5f2017-08-03 02:16:13 -0700456
457 if response.status_code == 202:
kasarc5bf2932018-03-09 04:15:22 -0800458 time.sleep(5)
459 return tenant_id
kated47ad5f2017-08-03 02:16:13 -0700460 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
bayramov325fa1c2016-09-08 01:42:46 -0700469
470 def get_tenant_list(self, filter_dict={}):
bayramovb6ffe792016-09-28 11:50:56 +0400471 """Obtain tenants of VIM
bayramov325fa1c2016-09-08 01:42:46 -0700472 filter_dict can contain the following keys:
473 name: filter by tenant name
474 id: filter by tenant uuid/id
475 <other VIM specific>
bayramovb6ffe792016-09-28 11:50:56 +0400476 Returns the tenant list of dictionaries:
bayramov325fa1c2016-09-08 01:42:46 -0700477 [{'name':'<name>, 'id':'<id>, ...}, ...]
bayramov325fa1c2016-09-08 01:42:46 -0700478
bayramovb6ffe792016-09-28 11:50:56 +0400479 """
bayramovef390722016-09-27 03:34:46 -0700480 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}
bayramovb6ffe792016-09-28 11:50:56 +0400487 # 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)
bayramovef390722016-09-27 03:34:46 -0700496 except:
497 self.logger.debug("Error in get_tenant_list()")
498 self.logger.debug(traceback.format_exc())
bayramovb6ffe792016-09-28 11:50:56 +0400499 raise vimconn.vimconnException("Incorrect state. {}")
bayramovef390722016-09-27 03:34:46 -0700500
501 return vdclist
502
503 def new_network(self, net_name, net_type, ip_profile=None, shared=False):
bayramovb6ffe792016-09-28 11:50:56 +0400504 """Adds a tenant network to VIM
bayramov325fa1c2016-09-08 01:42:46 -0700505 net_name is the name
bhangare0e571a92017-01-12 04:02:23 -0800506 net_type can be 'bridge','data'.'ptp'.
bayramovb6ffe792016-09-28 11:50:56 +0400507 ip_profile is a dict containing the IP parameters of the network
bayramov325fa1c2016-09-08 01:42:46 -0700508 shared is a boolean
bayramovb6ffe792016-09-28 11:50:56 +0400509 Returns the network identifier"""
bayramov325fa1c2016-09-08 01:42:46 -0700510
bhangare0e571a92017-01-12 04:02:23 -0800511 self.logger.debug("new_network tenant {} net_type {} ip_profile {} shared {}"
512 .format(net_name, net_type, ip_profile, shared))
bayramov325fa1c2016-09-08 01:42:46 -0700513
bayramovef390722016-09-27 03:34:46 -0700514 isshared = 'false'
515 if shared:
516 isshared = 'true'
517
kateac1e3792017-04-01 02:16:39 -0700518# ############# 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
bhangare0e571a92017-01-12 04:02:23 -0800524 network_uuid = self.create_network(network_name=net_name, net_type=net_type,
525 ip_profile=ip_profile, isshared=isshared)
bayramovef390722016-09-27 03:34:46 -0700526 if network_uuid is not None:
527 return network_uuid
528 else:
bayramovbd6160f2016-09-28 04:12:05 +0400529 raise vimconn.vimconnUnexpectedResponse("Failed create a new network {}".format(net_name))
bayramovef390722016-09-27 03:34:46 -0700530
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
kate15f1c382016-12-15 01:12:40 -0800538 self.logger.debug("get_vcd_network_list(): retrieving network list for vcd {}".format(self.tenant_name))
bayramovef390722016-09-27 03:34:46 -0700539
kate15f1c382016-12-15 01:12:40 -0800540 if not self.tenant_name:
541 raise vimconn.vimconnConnectionException("Tenant name is empty.")
542
kasarc5bf2932018-03-09 04:15:22 -0800543 org, vdc = self.get_vdc_details()
kate15f1c382016-12-15 01:12:40 -0800544 if vdc is None:
545 raise vimconn.vimconnConnectionException("Can't retrieve information for a VDC {}".format(self.tenant_name))
546
kasarc5bf2932018-03-09 04:15:22 -0800547 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)
sbhangarea8e5b782018-06-21 02:10:03 -0700559
bayramovef390722016-09-27 03:34:46 -0700560 network_list = []
561 try:
kasarc5bf2932018-03-09 04:15:22 -0800562 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)
bayramovef390722016-09-27 03:34:46 -0700568
kasarc5bf2932018-03-09 04:15:22 -0800569 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
Ravi Chamarty5b2d5c12018-10-22 23:59:10 +0000582 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')))
kasarc5bf2932018-03-09 04:15:22 -0800586 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
kasar40d97802018-05-02 05:58:07 -0700594 if int(net_details.get('status')) == 1:
kasarc5bf2932018-03-09 04:15:22 -0800595 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))
bayramovef390722016-09-27 03:34:46 -0700602 except:
kasarc5bf2932018-03-09 04:15:22 -0800603 self.logger.debug("Error in get_vcd_network_list", exc_info=True)
bayramovef390722016-09-27 03:34:46 -0700604 pass
605
606 self.logger.debug("get_vcd_network_list returning {}".format(network_list))
607 return network_list
bayramov325fa1c2016-09-08 01:42:46 -0700608
609 def get_network_list(self, filter_dict={}):
bayramovb6ffe792016-09-28 11:50:56 +0400610 """Obtain tenant networks of VIM
bayramov325fa1c2016-09-08 01:42:46 -0700611 Filter_dict can be:
bayramovef390722016-09-27 03:34:46 -0700612 name: network name OR/AND
613 id: network uuid OR/AND
614 shared: boolean OR/AND
615 tenant_id: tenant OR/AND
bayramov325fa1c2016-09-08 01:42:46 -0700616 admin_state_up: boolean
617 status: 'ACTIVE'
bayramovef390722016-09-27 03:34:46 -0700618
619 [{key : value , key : value}]
620
bayramov325fa1c2016-09-08 01:42:46 -0700621 Returns the network list of dictionaries:
622 [{<the fields at Filter_dict plus some VIM specific>}, ...]
623 List can be empty
bayramovb6ffe792016-09-28 11:50:56 +0400624 """
bayramov325fa1c2016-09-08 01:42:46 -0700625
bhangare1a0b97c2017-06-21 02:20:15 -0700626 self.logger.debug("get_network_list(): retrieving network list for vcd {}".format(self.tenant_name))
kate15f1c382016-12-15 01:12:40 -0800627
628 if not self.tenant_name:
629 raise vimconn.vimconnConnectionException("Tenant name is empty.")
bayramov325fa1c2016-09-08 01:42:46 -0700630
kasarc5bf2932018-03-09 04:15:22 -0800631 org, vdc = self.get_vdc_details()
kate15f1c382016-12-15 01:12:40 -0800632 if vdc is None:
633 raise vimconn.vimconnConnectionException("Can't retrieve information for a VDC {}.".format(self.tenant_name))
bayramov325fa1c2016-09-08 01:42:46 -0700634
bayramovef390722016-09-27 03:34:46 -0700635 try:
kasarc5bf2932018-03-09 04:15:22 -0800636 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
bhangarebfdca492017-03-11 01:32:46 -0800650 network_list = []
kasarc5bf2932018-03-09 04:15:22 -0800651 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)
bhangarebfdca492017-03-11 01:32:46 -0800657
kasarc5bf2932018-03-09 04:15:22 -0800658 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)
bayramovef390722016-09-27 03:34:46 -0700663
kasarc5bf2932018-03-09 04:15:22 -0800664 filter_entry = {}
665 net_uuid = net_details.get('id').split(":")
666 if len(net_uuid) != 4:
667 continue
668 else:
sbhangarea8e5b782018-06-21 02:10:03 -0700669 net_uuid = net_uuid[3]
kasarc5bf2932018-03-09 04:15:22 -0800670 # create dict entry
Ravi Chamarty5b2d5c12018-10-22 23:59:10 +0000671 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')))
kasarc5bf2932018-03-09 04:15:22 -0800675 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
kasar40d97802018-05-02 05:58:07 -0700683 if int(net_details.get('status')) == 1:
kasarc5bf2932018-03-09 04:15:22 -0800684 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))
bayramov325fa1c2016-09-08 01:42:46 -0700706
707 self.logger.debug("Returning {}".format(network_list))
708 return network_list
709
710 def get_network(self, net_id):
bayramovfe3f3c92016-10-04 07:53:41 +0400711 """Method obtains network details of net_id VIM network
bayramovef390722016-09-27 03:34:46 -0700712 Return a dict with the fields at filter_dict (see get_network_list) plus some VIM specific>}, ...]"""
713
bayramovef390722016-09-27 03:34:46 -0700714 try:
kasarc5bf2932018-03-09 04:15:22 -0800715 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)
bhangarebfdca492017-03-11 01:32:46 -0800728
bhangarebfdca492017-03-11 01:32:46 -0800729 filter_dict = {}
730
kasarc5bf2932018-03-09 04:15:22 -0800731 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)
kasarc30a04e2017-08-24 05:58:18 -0700737
kasarc5bf2932018-03-09 04:15:22 -0800738 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
kasar40d97802018-05-02 05:58:07 -0700754 if int(net_details.get('status')) == 1:
kasarc5bf2932018-03-09 04:15:22 -0800755 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
bayramovef390722016-09-27 03:34:46 -0700762 else:
kasarc5bf2932018-03-09 04:15:22 -0800763 raise vimconn.vimconnNotFoundException("Network {} not found".format(net_id))
kasarc30a04e2017-08-24 05:58:18 -0700764 except Exception as e:
bayramovef390722016-09-27 03:34:46 -0700765 self.logger.debug("Error in get_network")
766 self.logger.debug(traceback.format_exc())
kasarc30a04e2017-08-24 05:58:18 -0700767 if isinstance(e, vimconn.vimconnException):
768 raise
769 else:
770 raise vimconn.vimconnNotFoundException("Failed : Network not found {} ".format(e))
bayramovef390722016-09-27 03:34:46 -0700771
772 return filter_dict
bayramov325fa1c2016-09-08 01:42:46 -0700773
774 def delete_network(self, net_id):
bayramovef390722016-09-27 03:34:46 -0700775 """
776 Method Deletes a tenant network from VIM, provide the network id.
777
778 Returns the network identifier or raise an exception
779 """
780
kateac1e3792017-04-01 02:16:39 -0700781 # ############# 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
bayramovfe3f3c92016-10-04 07:53:41 +0400793 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
bayramovef390722016-09-27 03:34:46 -0700797 else:
798 raise vimconn.vimconnNotFoundException("Network {} not found".format(net_id))
bayramov325fa1c2016-09-08 01:42:46 -0700799
800 def refresh_nets_status(self, net_list):
bayramovbd6160f2016-09-28 04:12:05 +0400801 """Get the status of the networks
bayramov325fa1c2016-09-08 01:42:46 -0700802 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)
bayramovbd6160f2016-09-28 04:12:05 +0400807 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
bayramov325fa1c2016-09-08 01:42:46 -0700808 # OTHER (Vim reported other status not understood)
809 # ERROR (VIM indicates an ERROR status)
bayramovbd6160f2016-09-28 04:12:05 +0400810 # ACTIVE, INACTIVE, DOWN (admin down),
bayramov325fa1c2016-09-08 01:42:46 -0700811 # BUILD (on building process)
812 #
bayramovbd6160f2016-09-28 04:12:05 +0400813 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
bayramov325fa1c2016-09-08 01:42:46 -0700814 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
815
bayramovbd6160f2016-09-28 04:12:05 +0400816 """
bayramov325fa1c2016-09-08 01:42:46 -0700817
bayramovef390722016-09-27 03:34:46 -0700818 dict_entry = {}
819 try:
820 for net in net_list:
bayramovef390722016-09-27 03:34:46 -0700821 errormsg = ''
822 vcd_network = self.get_vcd_network(network_uuid=net)
bayramovfe3f3c92016-10-04 07:53:41 +0400823 if vcd_network is not None and vcd_network:
bhangare92d4af32016-12-24 02:54:51 -0800824 if vcd_network['status'] == '1':
bayramovef390722016-09-27 03:34:46 -0700825 status = 'ACTIVE'
826 else:
827 status = 'DOWN'
828 else:
829 status = 'DELETED'
bayramovfe3f3c92016-10-04 07:53:41 +0400830 errormsg = 'Network not found.'
831
832 dict_entry[net] = {'status': status, 'error_msg': errormsg,
bhangare92d4af32016-12-24 02:54:51 -0800833 'vim_info': yaml.safe_dump(vcd_network)}
bayramovef390722016-09-27 03:34:46 -0700834 except:
835 self.logger.debug("Error in refresh_nets_status")
836 self.logger.debug(traceback.format_exc())
837
838 return dict_entry
839
bayramovbd6160f2016-09-28 04:12:05 +0400840 def get_flavor(self, flavor_id):
bayramovef390722016-09-27 03:34:46 -0700841 """Obtain flavor details from the VIM
842 Returns the flavor dict details {'id':<>, 'name':<>, other vim specific } #TODO to concrete
843 """
kateeb044522017-03-06 23:54:39 -0800844 if flavor_id not in vimconnector.flavorlist:
bayramovfe3f3c92016-10-04 07:53:41 +0400845 raise vimconn.vimconnNotFoundException("Flavor not found.")
kateeb044522017-03-06 23:54:39 -0800846 return vimconnector.flavorlist[flavor_id]
bayramov325fa1c2016-09-08 01:42:46 -0700847
848 def new_flavor(self, flavor_data):
bayramovef390722016-09-27 03:34:46 -0700849 """Adds a tenant flavor to VIM
bayramov325fa1c2016-09-08 01:42:46 -0700850 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
bayramovef390722016-09-27 03:34:46 -0700862 vpci: requested virtual PCI address
bayramov325fa1c2016-09-08 01:42:46 -0700863 disk: disk size
864 is_public:
bayramov325fa1c2016-09-08 01:42:46 -0700865 #TODO to concrete
bayramovef390722016-09-27 03:34:46 -0700866 Returns the flavor identifier"""
bayramov325fa1c2016-09-08 01:42:46 -0700867
bayramovef390722016-09-27 03:34:46 -0700868 # generate a new uuid put to internal dict and return it.
bhangarea92ae392017-01-12 22:30:29 -0800869 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)
garciadeblas79d1a1a2017-12-11 16:07:07 +0100873 disk = flavor_data.get(FLAVOR_DISK_KEY, 0)
bhangarea92ae392017-01-12 22:30:29 -0800874
kasarfeaaa052017-06-08 03:46:18 -0700875 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
bhangarea92ae392017-01-12 22:30:29 -0800882 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
sbhangarea8e5b782018-06-21 02:10:03 -0700888 if 'memory' in numa:
889 ram = numa['memory']*1024
bhangarea92ae392017-01-12 22:30:29 -0800890 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.
bayramovef390722016-09-27 03:34:46 -0700901 flavor_id = uuid.uuid4()
kateeb044522017-03-06 23:54:39 -0800902 vimconnector.flavorlist[str(flavor_id)] = new_flavor
bhangarea92ae392017-01-12 22:30:29 -0800903 self.logger.debug("Created flavor - {} : {}".format(flavor_id, new_flavor))
bayramov325fa1c2016-09-08 01:42:46 -0700904
bayramovef390722016-09-27 03:34:46 -0700905 return str(flavor_id)
bayramov325fa1c2016-09-08 01:42:46 -0700906
907 def delete_flavor(self, flavor_id):
bayramovef390722016-09-27 03:34:46 -0700908 """Deletes a tenant flavor from VIM identify by its id
bayramov325fa1c2016-09-08 01:42:46 -0700909
bayramovfe3f3c92016-10-04 07:53:41 +0400910 Returns the used id or raise an exception
bayramovef390722016-09-27 03:34:46 -0700911 """
kateeb044522017-03-06 23:54:39 -0800912 if flavor_id not in vimconnector.flavorlist:
bayramovfe3f3c92016-10-04 07:53:41 +0400913 raise vimconn.vimconnNotFoundException("Flavor not found.")
bayramovef390722016-09-27 03:34:46 -0700914
kateeb044522017-03-06 23:54:39 -0800915 vimconnector.flavorlist.pop(flavor_id, None)
bayramovef390722016-09-27 03:34:46 -0700916 return flavor_id
917
918 def new_image(self, image_dict):
bayramov5761ad12016-10-04 09:00:30 +0400919 """
bayramov325fa1c2016-09-08 01:42:46 -0700920 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
bayramov5761ad12016-10-04 09:00:30 +0400924 """
bayramov325fa1c2016-09-08 01:42:46 -0700925
bayramovef390722016-09-27 03:34:46 -0700926 return self.get_image_id_from_path(image_dict['location'])
bayramov325fa1c2016-09-08 01:42:46 -0700927
928 def delete_image(self, image_id):
bayramovfe3f3c92016-10-04 07:53:41 +0400929 """
kated47ad5f2017-08-03 02:16:13 -0700930 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
bayramovfe3f3c92016-10-04 07:53:41 +0400935 """
kasarc5bf2932018-03-09 04:15:22 -0800936 conn = self.connect_as_admin()
937 if not conn:
938 raise vimconn.vimconnConnectionException("Failed to connect vCD")
kated47ad5f2017-08-03 02:16:13 -0700939 # Get Catalog details
kasarc5bf2932018-03-09 04:15:22 -0800940 url_list = [self.url, '/api/catalog/', image_id]
kated47ad5f2017-08-03 02:16:13 -0700941 catalog_herf = ''.join(url_list)
kasarc5bf2932018-03-09 04:15:22 -0800942
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,
sbhangarea8e5b782018-06-21 02:10:03 -0700948 headers=headers)
bayramovfe3f3c92016-10-04 07:53:41 +0400949
kated47ad5f2017-08-03 02:16:13 -0700950 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}
kasarc5bf2932018-03-09 04:15:22 -0800958 #For python3
959 #namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.items() if prefix}
kated47ad5f2017-08-03 02:16:13 -0700960 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
kasarc5bf2932018-03-09 04:15:22 -0800967 response = self.perform_request(req_type='GET',
968 url=catalogItem_href,
969 headers=headers)
kated47ad5f2017-08-03 02:16:13 -0700970
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}
kasarc5bf2932018-03-09 04:15:22 -0800981 #For python3
982 #namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.items() if prefix}
kated47ad5f2017-08-03 02:16:13 -0700983 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
kasarc5bf2932018-03-09 04:15:22 -0800987 response = self.perform_request(req_type='DELETE',
988 url=catalogitem_remove_href,
sbhangarea8e5b782018-06-21 02:10:03 -0700989 headers=headers)
kated47ad5f2017-08-03 02:16:13 -0700990 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
kasarc5bf2932018-03-09 04:15:22 -0800996 url_list = [self.url, '/api/admin/catalog/', image_id]
kated47ad5f2017-08-03 02:16:13 -0700997 catalog_remove_herf = ''.join(url_list)
kasarc5bf2932018-03-09 04:15:22 -0800998 response = self.perform_request(req_type='DELETE',
999 url=catalog_remove_herf,
1000 headers=headers)
kated47ad5f2017-08-03 02:16:13 -07001001
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
bayramov325fa1c2016-09-08 01:42:46 -07001008
1009 def catalog_exists(self, catalog_name, catalogs):
bayramovfe3f3c92016-10-04 07:53:41 +04001010 """
1011
1012 :param catalog_name:
1013 :param catalogs:
1014 :return:
1015 """
bayramov325fa1c2016-09-08 01:42:46 -07001016 for catalog in catalogs:
kasarc5bf2932018-03-09 04:15:22 -08001017 if catalog['name'] == catalog_name:
bayramov325fa1c2016-09-08 01:42:46 -07001018 return True
1019 return False
1020
bayramovb6ffe792016-09-28 11:50:56 +04001021 def create_vimcatalog(self, vca=None, catalog_name=None):
bayramovfe3f3c92016-10-04 07:53:41 +04001022 """ Create new catalog entry in vCloud director.
bayramovb6ffe792016-09-28 11:50:56 +04001023
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:
kasarc5bf2932018-03-09 04:15:22 -08001033 result = vca.create_catalog(catalog_name, catalog_name)
1034 if result is not None:
sbhangarea8e5b782018-06-21 02:10:03 -07001035 return True
kasarc5bf2932018-03-09 04:15:22 -08001036 catalogs = vca.list_catalogs()
bayramovb6ffe792016-09-28 11:50:56 +04001037 except:
bayramov325fa1c2016-09-08 01:42:46 -07001038 return False
bayramov325fa1c2016-09-08 01:42:46 -07001039 return self.catalog_exists(catalog_name, catalogs)
1040
bayramov5761ad12016-10-04 09:00:30 +04001041 # noinspection PyIncorrectDocstring
bayramovfe3f3c92016-10-04 07:53:41 +04001042 def upload_ovf(self, vca=None, catalog_name=None, image_name=None, media_file_name=None,
1043 description='', progress=False, chunk_bytes=128 * 1024):
bayramov325fa1c2016-09-08 01:42:46 -07001044 """
1045 Uploads a OVF file to a vCloud catalog
1046
bayramov5761ad12016-10-04 09:00:30 +04001047 :param chunk_bytes:
1048 :param progress:
1049 :param description:
1050 :param image_name:
1051 :param vca:
bayramov325fa1c2016-09-08 01:42:46 -07001052 :param catalog_name: (str): The name of the catalog to upload the media.
bayramov325fa1c2016-09-08 01:42:46 -07001053 :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)
bayramov325fa1c2016-09-08 01:42:46 -07001058
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
bhangarebfdca492017-03-11 01:32:46 -08001063 try:
kasarc5bf2932018-03-09 04:15:22 -08001064 for catalog in vca.list_catalogs():
1065 if catalog_name != catalog['name']:
bhangarebfdca492017-03-11 01:32:46 -08001066 continue
kasarc5bf2932018-03-09 04:15:22 -08001067 catalog_href = "{}/api/catalog/{}/action/upload".format(self.url, catalog['id'])
bhangarebfdca492017-03-11 01:32:46 -08001068 data = """
kasarc5bf2932018-03-09 04:15:22 -08001069 <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
bhangarebfdca492017-03-11 01:32:46 -08001082 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
kasarc5bf2932018-03-09 04:15:22 -08001088
1089 response = self.perform_request(req_type='GET',
1090 url=href,
1091 headers=headers)
bhangarebfdca492017-03-11 01:32:46 -08001092
1093 if response.status_code == requests.codes.ok:
bhangarebfdca492017-03-11 01:32:46 -08001094 headers['Content-Type'] = 'Content-Type text/xml'
kasarc5bf2932018-03-09 04:15:22 -08001095 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'))
bhangarebfdca492017-03-11 01:32:46 -08001103 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.
kasarc5bf2932018-03-09 04:15:22 -08001116 response = self.perform_request(req_type='GET',
1117 url=template,
1118 headers=headers)
bhangarebfdca492017-03-11 01:32:46 -08001119
1120 if response.status_code == requests.codes.ok:
kasarc5bf2932018-03-09 04:15:22 -08001121 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()
bhangarebfdca492017-03-11 01:32:46 -08001164 return False
kasarc5bf2932018-03-09 04:15:22 -08001165 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
bhangarebfdca492017-03-11 01:32:46 -08001174 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))
bayramov325fa1c2016-09-08 01:42:46 -07001180
1181 self.logger.debug("Failed retrieve catalog name {} for OVF file {}".format(catalog_name, media_file_name))
1182 return False
1183
bayramovfe3f3c92016-10-04 07:53:41 +04001184 def upload_vimimage(self, vca=None, catalog_name=None, media_name=None, medial_file_name=None, progress=False):
bayramov325fa1c2016-09-08 01:42:46 -07001185 """Upload media file"""
bayramovfe3f3c92016-10-04 07:53:41 +04001186 # 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)
bayramov325fa1c2016-09-08 01:42:46 -07001190
bayramovb6ffe792016-09-28 11:50:56 +04001191 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
bayramov325fa1c2016-09-08 01:42:46 -07001212 for catalog in catalogs:
kasarc5bf2932018-03-09 04:15:22 -08001213 if catalog['name'] == catalog_name:
1214 catalog_id = catalog['id']
1215 return catalog_id
bayramov325fa1c2016-09-08 01:42:46 -07001216 return None
1217
bayramovb6ffe792016-09-28 11:50:56 +04001218 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
bayramov325fa1c2016-09-08 01:42:46 -07001231 for catalog in catalogs:
kasarc5bf2932018-03-09 04:15:22 -08001232 catalog_id = catalog.get('id')
bayramovb6ffe792016-09-28 11:50:56 +04001233 if catalog_id == catalog_uuid:
kasarc5bf2932018-03-09 04:15:22 -08001234 return catalog.get('name')
bayramov325fa1c2016-09-08 01:42:46 -07001235 return None
1236
bhangare06312472017-03-30 05:49:07 -07001237 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:
kasarc5bf2932018-03-09 04:15:22 -08001251 catalog_id = catalog.get('id')
bhangare06312472017-03-30 05:49:07 -07001252 if catalog_id == catalog_uuid:
1253 return catalog
1254 return None
1255
bayramovfe3f3c92016-10-04 07:53:41 +04001256 def get_image_id_from_path(self, path=None, progress=False):
bayramovb6ffe792016-09-28 11:50:56 +04001257 """ Method upload OVF image to vCloud director.
bayramov325fa1c2016-09-08 01:42:46 -07001258
bayramovb6ffe792016-09-28 11:50:56 +04001259 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
bayramovfe3f3c92016-10-04 07:53:41 +04001267 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
bayramovb6ffe792016-09-28 11:50:56 +04001270 Args
bayramovfe3f3c92016-10-04 07:53:41 +04001271 path: - valid path to OVF file.
1272 progress - boolean progress bar show progress bar.
bayramovb6ffe792016-09-28 11:50:56 +04001273
1274 Return: if image uploaded correct method will provide image catalog UUID.
1275 """
bayramov325fa1c2016-09-08 01:42:46 -07001276
kate15f1c382016-12-15 01:12:40 -08001277 if not path:
bayramovfe3f3c92016-10-04 07:53:41 +04001278 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))
bayramov325fa1c2016-09-08 01:42:46 -07001287
1288 dirpath, filename = os.path.split(path)
1289 flname, file_extension = os.path.splitext(path)
1290 if file_extension != '.ovf':
bayramovfe3f3c92016-10-04 07:53:41 +04001291 self.logger.debug("Wrong file extension {} connector support only OVF container.".format(file_extension))
bayramovb6ffe792016-09-28 11:50:56 +04001292 raise vimconn.vimconnException("Wrong container. vCloud director supports only OVF.")
kate15f1c382016-12-15 01:12:40 -08001293
bayramov325fa1c2016-09-08 01:42:46 -07001294 catalog_name = os.path.splitext(filename)[0]
kate15f1c382016-12-15 01:12:40 -08001295 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))
bayramov325fa1c2016-09-08 01:42:46 -07001298
bhangarebfdca492017-03-11 01:32:46 -08001299 try:
kasarc5bf2932018-03-09 04:15:22 -08001300 org,vdc = self.get_vdc_details()
1301 catalogs = org.list_catalogs()
bhangarebfdca492017-03-11 01:32:46 -08001302 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
bayramov325fa1c2016-09-08 01:42:46 -07001306 if len(catalogs) == 0:
bayramovfe3f3c92016-10-04 07:53:41 +04001307 self.logger.info("Creating a new catalog entry {} in vcloud director".format(catalog_name))
kasarc5bf2932018-03-09 04:15:22 -08001308 result = self.create_vimcatalog(org, catalog_md5_name)
bayramov325fa1c2016-09-08 01:42:46 -07001309 if not result:
kate15f1c382016-12-15 01:12:40 -08001310 raise vimconn.vimconnException("Failed create new catalog {} ".format(catalog_md5_name))
kasarc5bf2932018-03-09 04:15:22 -08001311
1312 result = self.upload_vimimage(vca=org, catalog_name=catalog_md5_name,
bayramovfe3f3c92016-10-04 07:53:41 +04001313 media_name=filename, medial_file_name=path, progress=progress)
bayramov325fa1c2016-09-08 01:42:46 -07001314 if not result:
bayramovb6ffe792016-09-28 11:50:56 +04001315 raise vimconn.vimconnException("Failed create vApp template for catalog {} ".format(catalog_name))
kasarc5bf2932018-03-09 04:15:22 -08001316 return self.get_catalogid(catalog_name, catalogs)
bayramov325fa1c2016-09-08 01:42:46 -07001317 else:
1318 for catalog in catalogs:
1319 # search for existing catalog if we find same name we return ID
1320 # TODO optimize this
kasarc5bf2932018-03-09 04:15:22 -08001321 if catalog['name'] == catalog_md5_name:
kate15f1c382016-12-15 01:12:40 -08001322 self.logger.debug("Found existing catalog entry for {} "
1323 "catalog id {}".format(catalog_name,
1324 self.get_catalogid(catalog_md5_name, catalogs)))
kasarc5bf2932018-03-09 04:15:22 -08001325 return self.get_catalogid(catalog_md5_name, catalogs)
bayramov325fa1c2016-09-08 01:42:46 -07001326
bayramovfe3f3c92016-10-04 07:53:41 +04001327 # if we didn't find existing catalog we create a new one and upload image.
kate15f1c382016-12-15 01:12:40 -08001328 self.logger.debug("Creating new catalog entry {} - {}".format(catalog_name, catalog_md5_name))
kasarc5bf2932018-03-09 04:15:22 -08001329 result = self.create_vimcatalog(org, catalog_md5_name)
bayramov325fa1c2016-09-08 01:42:46 -07001330 if not result:
kate15f1c382016-12-15 01:12:40 -08001331 raise vimconn.vimconnException("Failed create new catalog {} ".format(catalog_md5_name))
bayramovfe3f3c92016-10-04 07:53:41 +04001332
kasarc5bf2932018-03-09 04:15:22 -08001333 result = self.upload_vimimage(vca=org, catalog_name=catalog_md5_name,
bayramovfe3f3c92016-10-04 07:53:41 +04001334 media_name=filename, medial_file_name=path, progress=progress)
bayramov325fa1c2016-09-08 01:42:46 -07001335 if not result:
kate15f1c382016-12-15 01:12:40 -08001336 raise vimconn.vimconnException("Failed create vApp template for catalog {} ".format(catalog_md5_name))
bayramov325fa1c2016-09-08 01:42:46 -07001337
kasarc5bf2932018-03-09 04:15:22 -08001338 return self.get_catalogid(catalog_md5_name, org.list_catalogs())
bayramov325fa1c2016-09-08 01:42:46 -07001339
kate8fc61fc2017-01-23 19:57:06 -08001340 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 '''
bhangare1a0b97c2017-06-21 02:20:15 -07001351
kate8fc61fc2017-01-23 19:57:06 -08001352 try:
kasarc5bf2932018-03-09 04:15:22 -08001353 org, vdc = self.get_vdc_details()
kate8fc61fc2017-01-23 19:57:06 -08001354 image_list = []
kasarc5bf2932018-03-09 04:15:22 -08001355 catalogs = org.list_catalogs()
kate8fc61fc2017-01-23 19:57:06 -08001356 if len(catalogs) == 0:
1357 return image_list
1358 else:
1359 for catalog in catalogs:
kasarc5bf2932018-03-09 04:15:22 -08001360 catalog_uuid = catalog.get('id')
1361 name = catalog.get('name')
kate8fc61fc2017-01-23 19:57:06 -08001362 filtered_dict = {}
kate34718682017-01-24 03:20:43 -08001363 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)
kate8fc61fc2017-01-23 19:57:06 -08001370
1371 self.logger.debug("List of already created catalog items: {}".format(image_list))
1372 return image_list
1373 except Exception as exp:
kated63062f2017-01-24 07:26:52 -08001374 raise vimconn.vimconnException("Exception occured while retriving catalog items {}".format(exp))
kate8fc61fc2017-01-23 19:57:06 -08001375
bayramovef390722016-09-27 03:34:46 -07001376 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:
bayramovef390722016-09-27 03:34:46 -07001380 vdc: The VDC object.
1381 vapp_name: is application vappp name identifier
1382
bayramovb6ffe792016-09-28 11:50:56 +04001383 Returns:
bayramovef390722016-09-27 03:34:46 -07001384 The return vApp name otherwise None
1385 """
bayramovef390722016-09-27 03:34:46 -07001386 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
bayramov325fa1c2016-09-08 01:42:46 -07001389 try:
1390 refs = filter(lambda ref: ref.name == vapp_name and ref.type_ == 'application/vnd.vmware.vcloud.vApp+xml',
bayramovef390722016-09-27 03:34:46 -07001391 vdc.ResourceEntities.ResourceEntity)
kasarc5bf2932018-03-09 04:15:22 -08001392 #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']
bayramov325fa1c2016-09-08 01:42:46 -07001395 if len(refs) == 1:
1396 return refs[0].href.split("vapp")[1][1:]
bayramovef390722016-09-27 03:34:46 -07001397 except Exception as e:
1398 self.logger.exception(e)
1399 return False
1400 return None
1401
bayramovfe3f3c92016-10-04 07:53:41 +04001402 def check_vapp(self, vdc=None, vapp_uuid=None):
1403 """ Method Method returns True or False if vapp deployed in vCloud director
bayramovef390722016-09-27 03:34:46 -07001404
1405 Args:
1406 vca: Connector to VCA
1407 vdc: The VDC object.
1408 vappid: vappid is application identifier
1409
1410 Returns:
bayramovfe3f3c92016-10-04 07:53:41 +04001411 The return True if vApp deployed
bayramov5761ad12016-10-04 09:00:30 +04001412 :param vdc:
1413 :param vapp_uuid:
bayramovef390722016-09-27 03:34:46 -07001414 """
1415 try:
1416 refs = filter(lambda ref:
1417 ref.type_ == 'application/vnd.vmware.vcloud.vApp+xml',
1418 vdc.ResourceEntities.ResourceEntity)
kasarc5bf2932018-03-09 04:15:22 -08001419 #For python3
1420 #refs = [ref for ref in vdc.ResourceEntities.ResourceEntity\
1421 # if ref.type_ == 'application/vnd.vmware.vcloud.vApp+xml']
bayramovef390722016-09-27 03:34:46 -07001422 for ref in refs:
1423 vappid = ref.href.split("vapp")[1][1:]
1424 # find vapp with respected vapp uuid
bayramovfe3f3c92016-10-04 07:53:41 +04001425 if vappid == vapp_uuid:
bayramovef390722016-09-27 03:34:46 -07001426 return True
1427 except Exception as e:
1428 self.logger.exception(e)
1429 return False
1430 return False
1431
kasarc5bf2932018-03-09 04:15:22 -08001432 def get_namebyvappid(self, vapp_uuid=None):
bayramovef390722016-09-27 03:34:46 -07001433 """Method returns vApp name from vCD and lookup done by vapp_id.
1434
1435 Args:
bayramovfe3f3c92016-10-04 07:53:41 +04001436 vapp_uuid: vappid is application identifier
bayramovef390722016-09-27 03:34:46 -07001437
1438 Returns:
1439 The return vApp name otherwise None
1440 """
bayramovef390722016-09-27 03:34:46 -07001441 try:
kasarc5bf2932018-03-09 04:15:22 -08001442 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']}
bhangare1a0b97c2017-06-21 02:20:15 -07001446
kasarc5bf2932018-03-09 04:15:22 -08001447 response = self.perform_request(req_type='GET',
1448 url=vapp_call,
sbhangarea8e5b782018-06-21 02:10:03 -07001449 headers=headers)
kasarc5bf2932018-03-09 04:15:22 -08001450 #Retry login if session expired & retry sending request
1451 if response.status_code == 403:
1452 response = self.retry_rest('GET', vapp_call)
bhangare1a0b97c2017-06-21 02:20:15 -07001453
kasarc5bf2932018-03-09 04:15:22 -08001454 tree = XmlElementTree.fromstring(response.content)
1455 return tree.attrib['name']
bayramovef390722016-09-27 03:34:46 -07001456 except Exception as e:
1457 self.logger.exception(e)
bayramov325fa1c2016-09-08 01:42:46 -07001458 return None
1459 return None
1460
tierno19860412017-10-03 10:46:46 +02001461 def new_vminstance(self, name=None, description="", start=False, image_id=None, flavor_id=None, net_list=[],
tierno5a3273c2017-08-29 11:43:46 +02001462 cloud_config=None, disk_list=None, availability_zone_index=None, availability_zone_list=None):
bayramov325fa1c2016-09-08 01:42:46 -07001463 """Adds a VM instance to VIM
1464 Params:
tierno19860412017-10-03 10:46:46 +02001465 '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
garciadeblasc4f4d732018-10-25 18:17:24 +02001471 'model': (optional and only have sense for type==virtual) interface model: virtio, e1000, ...
tierno19860412017-10-03 10:46:46 +02001472 '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'
tierno66eba6e2017-11-10 17:09:18 +01001477 'PCI-PASSTHROUGH' or 'PF' (passthrough): depending on VIM capabilities it can be connected to a data/ptp network ot it
tierno19860412017-10-03 10:46:46 +02001478 can created unconnected
tierno66eba6e2017-11-10 17:09:18 +01001479 'SR-IOV' or 'VF' (SRIOV with VLAN tag): same as PF for network connectivity.
tierno19860412017-10-03 10:46:46 +02001480 '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
tierno98e909c2017-10-14 13:27:03 +02001509 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.
bayramov325fa1c2016-09-08 01:42:46 -07001514 """
kate15f1c382016-12-15 01:12:40 -08001515 self.logger.info("Creating new instance for entry {}".format(name))
sbhangarea8e5b782018-06-21 02:10:03 -07001516 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))
bayramov325fa1c2016-09-08 01:42:46 -07001520
bayramov5761ad12016-10-04 09:00:30 +04001521 #new vm name = vmname + tenant_id + uuid
1522 new_vm_name = [name, '-', str(uuid.uuid4())]
kate15f1c382016-12-15 01:12:40 -08001523 vmname_andid = ''.join(new_vm_name)
bayramov5761ad12016-10-04 09:00:30 +04001524
kasarc5bf2932018-03-09 04:15:22 -08001525 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']))
bayramov325fa1c2016-09-08 01:42:46 -07001529
kasarc5bf2932018-03-09 04:15:22 -08001530 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
bayramovef390722016-09-27 03:34:46 -07001535 # we check for presence of VDC, Catalog entry and Flavor.
kasarc5bf2932018-03-09 04:15:22 -08001536 org, vdc = self.get_vdc_details()
bayramovef390722016-09-27 03:34:46 -07001537 if vdc is None:
bayramovfe3f3c92016-10-04 07:53:41 +04001538 raise vimconn.vimconnNotFoundException(
bayramovb6ffe792016-09-28 11:50:56 +04001539 "new_vminstance(): Failed create vApp {}: (Failed retrieve VDC information)".format(name))
kasarc5bf2932018-03-09 04:15:22 -08001540 catalogs = org.list_catalogs()
bhangare1a0b97c2017-06-21 02:20:15 -07001541 if catalogs is None:
1542 #Retry once, if failed by refreshing token
1543 self.get_token()
kasarc5bf2932018-03-09 04:15:22 -08001544 org = Org(self.client, resource=self.client.get_org())
1545 catalogs = org.list_catalogs()
bayramovef390722016-09-27 03:34:46 -07001546 if catalogs is None:
bayramovfe3f3c92016-10-04 07:53:41 +04001547 raise vimconn.vimconnNotFoundException(
kate15f1c382016-12-15 01:12:40 -08001548 "new_vminstance(): Failed create vApp {}: (Failed retrieve catalogs list)".format(name))
bayramovbd6160f2016-09-28 04:12:05 +04001549
kate15f1c382016-12-15 01:12:40 -08001550 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.
bayramovb6ffe792016-09-28 11:50:56 +04001559 vm_cpus = None
1560 vm_memory = None
bhangarea92ae392017-01-12 22:30:29 -08001561 vm_disk = None
bhangare68e73e62017-07-04 22:44:01 -07001562 numas = None
kateac1e3792017-04-01 02:16:39 -07001563
bayramovb6ffe792016-09-28 11:50:56 +04001564 if flavor_id is not None:
kateeb044522017-03-06 23:54:39 -08001565 if flavor_id not in vimconnector.flavorlist:
kate15f1c382016-12-15 01:12:40 -08001566 raise vimconn.vimconnNotFoundException("new_vminstance(): Failed create vApp {}: "
1567 "Failed retrieve flavor information "
1568 "flavor id {}".format(name, flavor_id))
bayramovb6ffe792016-09-28 11:50:56 +04001569 else:
1570 try:
kateeb044522017-03-06 23:54:39 -08001571 flavor = vimconnector.flavorlist[flavor_id]
kate15f1c382016-12-15 01:12:40 -08001572 vm_cpus = flavor[FLAVOR_VCPUS_KEY]
1573 vm_memory = flavor[FLAVOR_RAM_KEY]
bhangarea92ae392017-01-12 22:30:29 -08001574 vm_disk = flavor[FLAVOR_DISK_KEY]
bhangarefda5f7c2017-01-12 23:50:34 -08001575 extended = flavor.get("extended", None)
1576 if extended:
1577 numas=extended.get("numas", None)
kateac1e3792017-04-01 02:16:39 -07001578
kateeb044522017-03-06 23:54:39 -08001579 except Exception as exp:
1580 raise vimconn.vimconnException("Corrupted flavor. {}.Exception: {}".format(flavor_id, exp))
bayramov325fa1c2016-09-08 01:42:46 -07001581
bayramovef390722016-09-27 03:34:46 -07001582 # image upload creates template name as catalog name space Template.
kate15f1c382016-12-15 01:12:40 -08001583 templateName = self.get_catalogbyid(catalog_uuid=image_id, catalogs=catalogs)
bayramovef390722016-09-27 03:34:46 -07001584 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
bhangare0e571a92017-01-12 04:02:23 -08001589 #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
kate15f1c382016-12-15 01:12:40 -08001592 primary_netname = None
Ravi Chamarty1fdf9992018-10-07 15:45:44 +00001593 primary_net_href = None
kate15f1c382016-12-15 01:12:40 -08001594 network_mode = 'bridged'
bayramovb6ffe792016-09-28 11:50:56 +04001595 if net_list is not None and len(net_list) > 0:
bhangare0e571a92017-01-12 04:02:23 -08001596 for net in net_list:
tierno19860412017-10-03 10:46:46 +02001597 if 'use' in net and net['use'] == 'mgmt' and not primary_net:
bhangare0e571a92017-01-12 04:02:23 -08001598 primary_net = net
bayramovb6ffe792016-09-28 11:50:56 +04001599 if primary_net is None:
bhangare0e571a92017-01-12 04:02:23 -08001600 primary_net = net_list[0]
1601
1602 try:
1603 primary_net_id = primary_net['net_id']
Ravi Chamarty1fdf9992018-10-07 15:45:44 +00001604 url_list = [self.url, '/api/network/', primary_net_id]
1605 primary_net_href = ''.join(url_list)
bhangare0e571a92017-01-12 04:02:23 -08001606 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))
bayramovef390722016-09-27 03:34:46 -07001614
1615 # use: 'data', 'bridge', 'mgmt'
1616 # create vApp. Set vcpu and ram based on flavor id.
bhangarebfdca492017-03-11 01:32:46 -08001617 try:
kasarc5bf2932018-03-09 04:15:22 -08001618 vdc_obj = VDC(self.client, resource=org.get_vdc(self.tenant_name))
1619 if not vdc_obj:
sbhangarea8e5b782018-06-21 02:10:03 -07001620 raise vimconn.vimconnNotFoundException("new_vminstance(): Failed to get VDC object")
bhangare1a0b97c2017-06-21 02:20:15 -07001621
kasarc5bf2932018-03-09 04:15:22 -08001622 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")
sbhangarea8e5b782018-06-21 02:10:03 -07001637
kasarc5bf2932018-03-09 04:15:22 -08001638 response = self.perform_request(req_type='GET',
1639 url=vapp_tempalte_href,
sbhangarea8e5b782018-06-21 02:10:03 -07001640 headers=headers)
kasarc5bf2932018-03-09 04:15:22 -08001641 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
sbhangarea8e5b782018-06-21 02:10:03 -07001659 headers['Content-Type'] = 'application/vnd.vmware.vcloud.instantiateVAppTemplateParams+xml'
kasarc5bf2932018-03-09 04:15:22 -08001660 vdc_id = vdc.get('id').split(':')[-1]
1661 instantiate_vapp_href = "{}/api/vdc/{}/action/instantiateVAppTemplate".format(self.url,
sbhangarea8e5b782018-06-21 02:10:03 -07001662 vdc_id)
kasarc5bf2932018-03-09 04:15:22 -08001663 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>
Ravi Chamarty1fdf9992018-10-07 15:45:44 +00001675 <NetworkConfig networkName="{}">
kasarc5bf2932018-03-09 04:15:22 -08001676 <Configuration>
Ravi Chamarty1fdf9992018-10-07 15:45:44 +00001677 <ParentNetwork href="{}" />
kasarc5bf2932018-03-09 04:15:22 -08001678 <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>
sbhangarea8e5b782018-06-21 02:10:03 -07001689 <Source href="{}"/>
kasarc5bf2932018-03-09 04:15:22 -08001690 <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>
sbhangarea8e5b782018-06-21 02:10:03 -07001702 <IpAddressAllocationMode>DHCP</IpAddressAllocationMode>
1703 </NetworkConnection>
kasarc5bf2932018-03-09 04:15:22 -08001704 </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>
sbhangarea8e5b782018-06-21 02:10:03 -07001711 <rasd:InstanceID>4</rasd:InstanceID>
kasarc5bf2932018-03-09 04:15:22 -08001712 <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>
sbhangarea8e5b782018-06-21 02:10:03 -07001727 </ovf:VirtualHardwareSection>
kasarc5bf2932018-03-09 04:15:22 -08001728 </InstantiationParams>
1729 </SourcedItem>
1730 <AllEULAsAccepted>false</AllEULAsAccepted>
1731 </InstantiateVAppTemplateParams>""".format(vmname_andid,
Ravi Chamarty1fdf9992018-10-07 15:45:44 +00001732 primary_netname,
1733 primary_net_href,
kasarc5bf2932018-03-09 04:15:22 -08001734 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:
bhangare68e73e62017-07-04 22:44:01 -07001759 self.get_token() # Retry getting token
1760 continue
1761 else:
1762 break
1763
bhangare1a0b97c2017-06-21 02:20:15 -07001764 if vapptask is None or vapptask is False:
bhangarebfdca492017-03-11 01:32:46 -08001765 raise vimconn.vimconnUnexpectedResponse(
1766 "new_vminstance(): failed to create vApp {}".format(vmname_andid))
kasarc5bf2932018-03-09 04:15:22 -08001767
sbhangarea8e5b782018-06-21 02:10:03 -07001768 # wait for task to complete
kasarc5bf2932018-03-09 04:15:22 -08001769 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))
bhangarebfdca492017-03-11 01:32:46 -08001776
1777 except Exception as exp:
1778 raise vimconn.vimconnUnexpectedResponse(
1779 "new_vminstance(): failed to create vApp {} with Exception:{}".format(vmname_andid, exp))
bayramovef390722016-09-27 03:34:46 -07001780
1781 # we should have now vapp in undeployed state.
bhangarebfdca492017-03-11 01:32:46 -08001782 try:
kasarc5bf2932018-03-09 04:15:22 -08001783 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)
bhangare1a0b97c2017-06-21 02:20:15 -07001787
bhangarebfdca492017-03-11 01:32:46 -08001788 except Exception as exp:
1789 raise vimconn.vimconnUnexpectedResponse(
1790 "new_vminstance(): Failed to retrieve vApp {} after creation: Exception:{}"
1791 .format(vmname_andid, exp))
1792
bhangare1a0b97c2017-06-21 02:20:15 -07001793 if vapp_uuid is None:
bayramovbd6160f2016-09-28 04:12:05 +04001794 raise vimconn.vimconnUnexpectedResponse(
bhangarebfdca492017-03-11 01:32:46 -08001795 "new_vminstance(): Failed to retrieve vApp {} after creation".format(
bhangarea92ae392017-01-12 22:30:29 -08001796 vmname_andid))
1797
kateac1e3792017-04-01 02:16:39 -07001798 #Add PCI passthrough/SRIOV configrations
bhangarefda5f7c2017-01-12 23:50:34 -08001799 vm_obj = None
kateac1e3792017-04-01 02:16:39 -07001800 pci_devices_info = []
1801 sriov_net_info = []
1802 reserve_memory = False
1803
1804 for net in net_list:
tierno66eba6e2017-11-10 17:09:18 +01001805 if net["type"] == "PF" or net["type"] == "PCI-PASSTHROUGH":
kateac1e3792017-04-01 02:16:39 -07001806 pci_devices_info.append(net)
tierno66eba6e2017-11-10 17:09:18 +01001807 elif (net["type"] == "VF" or net["type"] == "SR-IOV" or net["type"] == "VFnotShared") and 'net_id'in net:
kateac1e3792017-04-01 02:16:39 -07001808 sriov_net_info.append(net)
1809
1810 #Add PCI
bhangarefda5f7c2017-01-12 23:50:34 -08001811 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 )
kateac1e3792017-04-01 02:16:39 -07001822 reserve_memory = True
bhangarefda5f7c2017-01-12 23:50:34 -08001823 else:
1824 self.logger.info("Fail to add PCI devives {} to VM {}".format(
1825 pci_devices_info,
1826 vmname_andid)
1827 )
bhangare1a0b97c2017-06-21 02:20:15 -07001828
bhangare06312472017-03-30 05:49:07 -07001829 # Modify vm disk
bhangarea92ae392017-01-12 22:30:29 -08001830 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))
bayramovef390722016-09-27 03:34:46 -07001835
bhangare06312472017-03-30 05:49:07 -07001836 #Add new or existing disks to vApp
1837 if disk_list:
1838 added_existing_disk = False
1839 for disk in disk_list:
kasar0c007d62017-05-19 03:13:57 -07001840 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:
bhangare06312472017-03-30 05:49:07 -07001846 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
bhangare1a0b97c2017-06-21 02:20:15 -07001860 self.add_new_disk(vapp_uuid, disk['size'])
bhangare06312472017-03-30 05:49:07 -07001861
kasarde691232017-03-25 03:37:31 -07001862 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
bhangare0e571a92017-01-12 04:02:23 -08001869 # add NICs & connect to networks in netlist
bayramovef390722016-09-27 03:34:46 -07001870 try:
Ravi Chamarty5b2d5c12018-10-22 23:59:10 +00001871 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)
Ravi Chamartyb0ab2c02018-10-26 01:40:50 +00001874 vapp_id = vapp_resource.get('id').split(':')[-1]
Ravi Chamarty5b2d5c12018-10-22 23:59:10 +00001875
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
kate15f1c382016-12-15 01:12:40 -08001880 self.logger.info("Request to connect VM to a network: {}".format(net_list))
bhangare0e571a92017-01-12 04:02:23 -08001881 primary_nic_index = 0
Ravi Chamarty1fdf9992018-10-07 15:45:44 +00001882 nicIndex = 0
bayramovef390722016-09-27 03:34:46 -07001883 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
kate15f1c382016-12-15 01:12:40 -08001886 # [{'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
bhangare97b192d2017-10-05 00:51:15 -07001892 #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
tierno19860412017-10-03 10:46:46 +02001895
bayramovef390722016-09-27 03:34:46 -07001896 interface_net_id = net['net_id']
kate15f1c382016-12-15 01:12:40 -08001897 interface_net_name = self.get_network_name_by_id(network_uuid=interface_net_id)
bayramovef390722016-09-27 03:34:46 -07001898 interface_network_mode = net['use']
1899
bhangare0e571a92017-01-12 04:02:23 -08001900 if interface_network_mode == 'mgmt':
1901 primary_nic_index = nicIndex
1902
kate15f1c382016-12-15 01:12:40 -08001903 """- 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:
Ravi Chamarty5b2d5c12018-10-22 23:59:10 +00001909 self.logger.debug("new_vminstance(): Filtering by net name {}".format(interface_net_name))
kasarc5bf2932018-03-09 04:15:22 -08001910 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]
bayramovef390722016-09-27 03:34:46 -07001913 if len(nets) == 1:
kasarc5bf2932018-03-09 04:15:22 -08001914 self.logger.info("new_vminstance(): Found requested network: {}".format(nets[0].get('name')))
bhangare1a0b97c2017-06-21 02:20:15 -07001915
Ravi Chamarty5b2d5c12018-10-22 23:59:10 +00001916 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))
Ravi Chamartyb0ab2c02018-10-26 01:40:50 +00001919 self.connect_vapp_to_org_vdc_network(vapp_id, nets[0].get('name'))
kasar3ac5dc42017-03-15 06:28:22 -07001920
tierno66eba6e2017-11-10 17:09:18 +01001921 type_list = ('PF', 'PCI-PASSTHROUGH', 'VF', 'SR-IOV', 'VFnotShared')
kasar3ac5dc42017-03-15 06:28:22 -07001922 if 'type' in net and net['type'] not in type_list:
1923 # fetching nic type from vnf
1924 if 'model' in net:
garciadeblas31e141b2018-10-25 18:33:19 +02001925 if net['model'] is not None:
1926 if net['model'].lower() == 'paravirt' or net['model'].lower() == 'virtio':
1927 nic_type = 'VMXNET3'
kasar204e39e2018-01-25 00:57:02 -08001928 else:
1929 nic_type = net['model']
1930
kasar3ac5dc42017-03-15 06:28:22 -07001931 self.logger.info("new_vminstance(): adding network adapter "\
kasarc5bf2932018-03-09 04:15:22 -08001932 "to a network {}".format(nets[0].get('name')))
1933 self.add_network_adapter_to_vms(vapp, nets[0].get('name'),
kasar3ac5dc42017-03-15 06:28:22 -07001934 primary_nic_index,
1935 nicIndex,
kasardc1f02e2017-03-25 07:20:30 -07001936 net,
kasar3ac5dc42017-03-15 06:28:22 -07001937 nic_type=nic_type)
1938 else:
1939 self.logger.info("new_vminstance(): adding network adapter "\
kasarc5bf2932018-03-09 04:15:22 -08001940 "to a network {}".format(nets[0].get('name')))
Ravi Chamarty5063a982018-10-26 13:47:14 +00001941 nic_type = 'VMXNET3'
1942 if net['type'] in ['SR-IOV', 'VF']:
1943 nic_type = net['type']
kasarc5bf2932018-03-09 04:15:22 -08001944 self.add_network_adapter_to_vms(vapp, nets[0].get('name'),
kasar3ac5dc42017-03-15 06:28:22 -07001945 primary_nic_index,
kasardc1f02e2017-03-25 07:20:30 -07001946 nicIndex,
Ravi Chamarty5063a982018-10-26 13:47:14 +00001947 net,
1948 nic_type=nic_type)
bhangare0e571a92017-01-12 04:02:23 -08001949 nicIndex += 1
bayramovef390722016-09-27 03:34:46 -07001950
kasardc1f02e2017-03-25 07:20:30 -07001951 # cloud-init for ssh-key injection
1952 if cloud_config:
1953 self.cloud_init(vapp,cloud_config)
1954
kateac1e3792017-04-01 02:16:39 -07001955 # ############# Stub code for SRIOV #################
1956 #Add SRIOV
1957# if len(sriov_net_info) > 0:
1958# self.logger.info("Need to add SRIOV adapters {} into VM {}".format(sriov_net_info,
1959# vmname_andid ))
1960# sriov_status, vm_obj, vcenter_conect = self.add_sriov(vapp_uuid,
1961# sriov_net_info,
1962# vmname_andid)
1963# if sriov_status:
1964# self.logger.info("Added SRIOV {} to VM {}".format(
1965# sriov_net_info,
1966# vmname_andid)
1967# )
1968# reserve_memory = True
1969# else:
1970# self.logger.info("Fail to add SRIOV {} to VM {}".format(
1971# sriov_net_info,
1972# vmname_andid)
1973# )
1974
1975 # If VM has PCI devices or SRIOV reserve memory for VM
1976 if reserve_memory:
bhangarebfdca492017-03-11 01:32:46 -08001977 memReserve = vm_obj.config.hardware.memoryMB
1978 spec = vim.vm.ConfigSpec()
1979 spec.memoryAllocation = vim.ResourceAllocationInfo(reservation=memReserve)
1980 task = vm_obj.ReconfigVM_Task(spec=spec)
1981 if task:
1982 result = self.wait_for_vcenter_task(task, vcenter_conect)
tierno19860412017-10-03 10:46:46 +02001983 self.logger.info("Reserved memory {} MB for "
1984 "VM VM status: {}".format(str(memReserve), result))
bhangarebfdca492017-03-11 01:32:46 -08001985 else:
tierno19860412017-10-03 10:46:46 +02001986 self.logger.info("Fail to reserved memory {} to VM {}".format(
1987 str(memReserve), str(vm_obj)))
bhangarefda5f7c2017-01-12 23:50:34 -08001988
kasarc5bf2932018-03-09 04:15:22 -08001989 self.logger.debug("new_vminstance(): starting power on vApp {} ".format(vmname_andid))
bhangare1a0b97c2017-06-21 02:20:15 -07001990
kasarc5bf2932018-03-09 04:15:22 -08001991 poweron_task = self.power_on_vapp(vapp_id, vmname_andid)
1992 result = self.client.get_task_monitor().wait_for_success(task=poweron_task)
1993 if result.get('status') == 'success':
1994 self.logger.info("new_vminstance(): Successfully power on "\
1995 "vApp {}".format(vmname_andid))
1996 else:
1997 self.logger.error("new_vminstance(): failed to power on vApp "\
1998 "{}".format(vmname_andid))
bhangarebfdca492017-03-11 01:32:46 -08001999
2000 except Exception as exp :
2001 # it might be a case if specific mandatory entry in dict is empty or some other pyVcloud exception
kasarc5bf2932018-03-09 04:15:22 -08002002 self.logger.error("new_vminstance(): Failed create new vm instance {} with exception {}"
bhangare1a0b97c2017-06-21 02:20:15 -07002003 .format(name, exp))
2004 raise vimconn.vimconnException("new_vminstance(): Failed create new vm instance {} with exception {}"
2005 .format(name, exp))
bhangarefda5f7c2017-01-12 23:50:34 -08002006
bayramovef390722016-09-27 03:34:46 -07002007 # check if vApp deployed and if that the case return vApp UUID otherwise -1
kate13ab2c42016-12-23 01:34:24 -08002008 wait_time = 0
2009 vapp_uuid = None
2010 while wait_time <= MAX_WAIT_TIME:
bhangarebfdca492017-03-11 01:32:46 -08002011 try:
kasarc5bf2932018-03-09 04:15:22 -08002012 vapp_resource = vdc_obj.get_vapp(vmname_andid)
sbhangarea8e5b782018-06-21 02:10:03 -07002013 vapp = VApp(self.client, resource=vapp_resource)
bhangarebfdca492017-03-11 01:32:46 -08002014 except Exception as exp:
2015 raise vimconn.vimconnUnexpectedResponse(
2016 "new_vminstance(): Failed to retrieve vApp {} after creation: Exception:{}"
2017 .format(vmname_andid, exp))
2018
kasarc5bf2932018-03-09 04:15:22 -08002019 #if vapp and vapp.me.deployed:
2020 if vapp and vapp_resource.get('deployed') == 'true':
2021 vapp_uuid = vapp_resource.get('id').split(':')[-1]
kate13ab2c42016-12-23 01:34:24 -08002022 break
2023 else:
2024 self.logger.debug("new_vminstance(): Wait for vApp {} to deploy".format(name))
2025 time.sleep(INTERVAL_TIME)
2026
2027 wait_time +=INTERVAL_TIME
2028
sbhangarea8e5b782018-06-21 02:10:03 -07002029 #SET Affinity Rule for VM
2030 #Pre-requisites: User has created Hosh Groups in vCenter with respective Hosts to be used
2031 #While creating VIM account user has to pass the Host Group names in availability_zone list
2032 #"availability_zone" is a part of VIM "config" parameters
2033 #For example, in VIM config: "availability_zone":["HG_170","HG_174","HG_175"]
2034 #Host groups are referred as availability zones
2035 #With following procedure, deployed VM will be added into a VM group.
2036 #Then A VM to Host Affinity rule will be created using the VM group & Host group.
2037 if(availability_zone_list):
2038 self.logger.debug("Existing Host Groups in VIM {}".format(self.config.get('availability_zone')))
2039 #Admin access required for creating Affinity rules
2040 client = self.connect_as_admin()
2041 if not client:
2042 raise vimconn.vimconnConnectionException("Failed to connect vCD as admin")
2043 else:
2044 self.client = client
2045 if self.client:
2046 headers = {'Accept':'application/*+xml;version=27.0',
2047 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
2048 #Step1: Get provider vdc details from organization
2049 pvdc_href = self.get_pvdc_for_org(self.tenant_name, headers)
2050 if pvdc_href is not None:
2051 #Step2: Found required pvdc, now get resource pool information
2052 respool_href = self.get_resource_pool_details(pvdc_href, headers)
2053 if respool_href is None:
2054 #Raise error if respool_href not found
2055 msg = "new_vminstance():Error in finding resource pool details in pvdc {}"\
2056 .format(pvdc_href)
2057 self.log_message(msg)
2058
2059 #Step3: Verify requested availability zone(hostGroup) is present in vCD
2060 # get availability Zone
2061 vm_az = self.get_vm_availability_zone(availability_zone_index, availability_zone_list)
2062 # check if provided av zone(hostGroup) is present in vCD VIM
2063 status = self.check_availibility_zone(vm_az, respool_href, headers)
2064 if status is False:
2065 msg = "new_vminstance(): Error in finding availability zone(Host Group): {} in "\
2066 "resource pool {} status: {}".format(vm_az,respool_href,status)
2067 self.log_message(msg)
2068 else:
2069 self.logger.debug ("new_vminstance(): Availability zone {} found in VIM".format(vm_az))
2070
2071 #Step4: Find VM group references to create vm group
2072 vmgrp_href = self.find_vmgroup_reference(respool_href, headers)
2073 if vmgrp_href == None:
2074 msg = "new_vminstance(): No reference to VmGroup found in resource pool"
2075 self.log_message(msg)
2076
2077 #Step5: Create a VmGroup with name az_VmGroup
2078 vmgrp_name = vm_az + "_" + name #Formed VM Group name = Host Group name + VM name
2079 status = self.create_vmgroup(vmgrp_name, vmgrp_href, headers)
2080 if status is not True:
2081 msg = "new_vminstance(): Error in creating VM group {}".format(vmgrp_name)
2082 self.log_message(msg)
2083
2084 #VM Group url to add vms to vm group
2085 vmgrpname_url = self.url + "/api/admin/extension/vmGroup/name/"+ vmgrp_name
2086
2087 #Step6: Add VM to VM Group
2088 #Find VM uuid from vapp_uuid
2089 vm_details = self.get_vapp_details_rest(vapp_uuid)
2090 vm_uuid = vm_details['vmuuid']
2091
2092 status = self.add_vm_to_vmgroup(vm_uuid, vmgrpname_url, vmgrp_name, headers)
2093 if status is not True:
2094 msg = "new_vminstance(): Error in adding VM to VM group {}".format(vmgrp_name)
2095 self.log_message(msg)
2096
2097 #Step7: Create VM to Host affinity rule
2098 addrule_href = self.get_add_rule_reference (respool_href, headers)
2099 if addrule_href is None:
2100 msg = "new_vminstance(): Error in finding href to add rule in resource pool: {}"\
2101 .format(respool_href)
2102 self.log_message(msg)
2103
2104 status = self.create_vm_to_host_affinity_rule(addrule_href, vmgrp_name, vm_az, "Affinity", headers)
2105 if status is False:
2106 msg = "new_vminstance(): Error in creating affinity rule for VM {} in Host group {}"\
2107 .format(name, vm_az)
2108 self.log_message(msg)
2109 else:
2110 self.logger.debug("new_vminstance(): Affinity rule created successfully. Added {} in Host group {}"\
2111 .format(name, vm_az))
2112 #Reset token to a normal user to perform other operations
2113 self.get_token()
2114
bayramovef390722016-09-27 03:34:46 -07002115 if vapp_uuid is not None:
tierno98e909c2017-10-14 13:27:03 +02002116 return vapp_uuid, None
bayramovbd6160f2016-09-28 04:12:05 +04002117 else:
2118 raise vimconn.vimconnUnexpectedResponse("new_vminstance(): Failed create new vm instance {}".format(name))
bayramov325fa1c2016-09-08 01:42:46 -07002119
sbhangarea8e5b782018-06-21 02:10:03 -07002120
2121 def get_vcd_availibility_zones(self,respool_href, headers):
2122 """ Method to find presence of av zone is VIM resource pool
2123
2124 Args:
2125 respool_href - resource pool href
2126 headers - header information
2127
2128 Returns:
2129 vcd_az - list of azone present in vCD
2130 """
2131 vcd_az = []
2132 url=respool_href
2133 resp = self.perform_request(req_type='GET',url=respool_href, headers=headers)
2134
2135 if resp.status_code != requests.codes.ok:
2136 self.logger.debug ("REST API call {} failed. Return status code {}".format(url, resp.status_code))
2137 else:
2138 #Get the href to hostGroups and find provided hostGroup is present in it
2139 resp_xml = XmlElementTree.fromstring(resp.content)
2140 for child in resp_xml:
2141 if 'VMWProviderVdcResourcePool' in child.tag:
2142 for schild in child:
2143 if 'Link' in schild.tag:
2144 if schild.attrib.get('type') == "application/vnd.vmware.admin.vmwHostGroupsType+xml":
2145 hostGroup = schild.attrib.get('href')
2146 hg_resp = self.perform_request(req_type='GET',url=hostGroup, headers=headers)
2147 if hg_resp.status_code != requests.codes.ok:
2148 self.logger.debug ("REST API call {} failed. Return status code {}".format(hostGroup, hg_resp.status_code))
2149 else:
2150 hg_resp_xml = XmlElementTree.fromstring(hg_resp.content)
2151 for hostGroup in hg_resp_xml:
2152 if 'HostGroup' in hostGroup.tag:
2153 #append host group name to the list
2154 vcd_az.append(hostGroup.attrib.get("name"))
2155 return vcd_az
2156
2157
2158 def set_availability_zones(self):
2159 """
2160 Set vim availability zone
2161 """
2162
2163 vim_availability_zones = None
2164 availability_zone = None
2165 if 'availability_zone' in self.config:
2166 vim_availability_zones = self.config.get('availability_zone')
2167 if isinstance(vim_availability_zones, str):
2168 availability_zone = [vim_availability_zones]
2169 elif isinstance(vim_availability_zones, list):
2170 availability_zone = vim_availability_zones
2171 else:
2172 return availability_zone
2173
2174 return availability_zone
2175
2176
2177 def get_vm_availability_zone(self, availability_zone_index, availability_zone_list):
2178 """
2179 Return the availability zone to be used by the created VM.
2180 returns: The VIM availability zone to be used or None
2181 """
2182 if availability_zone_index is None:
2183 if not self.config.get('availability_zone'):
2184 return None
2185 elif isinstance(self.config.get('availability_zone'), str):
2186 return self.config['availability_zone']
2187 else:
2188 return self.config['availability_zone'][0]
2189
2190 vim_availability_zones = self.availability_zone
2191
2192 # check if VIM offer enough availability zones describe in the VNFD
2193 if vim_availability_zones and len(availability_zone_list) <= len(vim_availability_zones):
2194 # check if all the names of NFV AV match VIM AV names
2195 match_by_index = False
2196 for av in availability_zone_list:
2197 if av not in vim_availability_zones:
2198 match_by_index = True
2199 break
2200 if match_by_index:
2201 self.logger.debug("Required Availability zone or Host Group not found in VIM config")
2202 self.logger.debug("Input Availability zone list: {}".format(availability_zone_list))
2203 self.logger.debug("VIM configured Availability zones: {}".format(vim_availability_zones))
2204 self.logger.debug("VIM Availability zones will be used by index")
2205 return vim_availability_zones[availability_zone_index]
2206 else:
2207 return availability_zone_list[availability_zone_index]
2208 else:
2209 raise vimconn.vimconnConflictException("No enough availability zones at VIM for this deployment")
2210
2211
2212 def create_vm_to_host_affinity_rule(self, addrule_href, vmgrpname, hostgrpname, polarity, headers):
2213 """ Method to create VM to Host Affinity rule in vCD
2214
2215 Args:
2216 addrule_href - href to make a POST request
2217 vmgrpname - name of the VM group created
2218 hostgrpnmae - name of the host group created earlier
2219 polarity - Affinity or Anti-affinity (default: Affinity)
2220 headers - headers to make REST call
2221
2222 Returns:
2223 True- if rule is created
2224 False- Failed to create rule due to some error
2225
2226 """
2227 task_status = False
2228 rule_name = polarity + "_" + vmgrpname
2229 payload = """<?xml version="1.0" encoding="UTF-8"?>
2230 <vmext:VMWVmHostAffinityRule
2231 xmlns:vmext="http://www.vmware.com/vcloud/extension/v1.5"
2232 xmlns:vcloud="http://www.vmware.com/vcloud/v1.5"
2233 type="application/vnd.vmware.admin.vmwVmHostAffinityRule+xml">
2234 <vcloud:Name>{}</vcloud:Name>
2235 <vcloud:IsEnabled>true</vcloud:IsEnabled>
2236 <vcloud:IsMandatory>true</vcloud:IsMandatory>
2237 <vcloud:Polarity>{}</vcloud:Polarity>
2238 <vmext:HostGroupName>{}</vmext:HostGroupName>
2239 <vmext:VmGroupName>{}</vmext:VmGroupName>
2240 </vmext:VMWVmHostAffinityRule>""".format(rule_name, polarity, hostgrpname, vmgrpname)
2241
2242 resp = self.perform_request(req_type='POST',url=addrule_href, headers=headers, data=payload)
2243
2244 if resp.status_code != requests.codes.accepted:
2245 self.logger.debug ("REST API call {} failed. Return status code {}".format(addrule_href, resp.status_code))
2246 task_status = False
2247 return task_status
2248 else:
2249 affinity_task = self.get_task_from_response(resp.content)
2250 self.logger.debug ("affinity_task: {}".format(affinity_task))
2251 if affinity_task is None or affinity_task is False:
2252 raise vimconn.vimconnUnexpectedResponse("failed to find affinity task")
2253 # wait for task to complete
2254 result = self.client.get_task_monitor().wait_for_success(task=affinity_task)
2255 if result.get('status') == 'success':
2256 self.logger.debug("Successfully created affinity rule {}".format(rule_name))
2257 return True
2258 else:
2259 raise vimconn.vimconnUnexpectedResponse(
2260 "failed to create affinity rule {}".format(rule_name))
2261
2262
2263 def get_add_rule_reference (self, respool_href, headers):
2264 """ This method finds href to add vm to host affinity rule to vCD
2265
2266 Args:
2267 respool_href- href to resource pool
2268 headers- header information to make REST call
2269
2270 Returns:
2271 None - if no valid href to add rule found or
2272 addrule_href - href to add vm to host affinity rule of resource pool
2273 """
2274 addrule_href = None
2275 resp = self.perform_request(req_type='GET',url=respool_href, headers=headers)
2276
2277 if resp.status_code != requests.codes.ok:
2278 self.logger.debug ("REST API call {} failed. Return status code {}".format(respool_href, resp.status_code))
2279 else:
2280
2281 resp_xml = XmlElementTree.fromstring(resp.content)
2282 for child in resp_xml:
2283 if 'VMWProviderVdcResourcePool' in child.tag:
2284 for schild in child:
2285 if 'Link' in schild.tag:
2286 if schild.attrib.get('type') == "application/vnd.vmware.admin.vmwVmHostAffinityRule+xml" and \
2287 schild.attrib.get('rel') == "add":
2288 addrule_href = schild.attrib.get('href')
2289 break
2290
2291 return addrule_href
2292
2293
2294 def add_vm_to_vmgroup(self, vm_uuid, vmGroupNameURL, vmGroup_name, headers):
2295 """ Method to add deployed VM to newly created VM Group.
2296 This is required to create VM to Host affinity in vCD
2297
2298 Args:
2299 vm_uuid- newly created vm uuid
2300 vmGroupNameURL- URL to VM Group name
2301 vmGroup_name- Name of VM group created
2302 headers- Headers for REST request
2303
2304 Returns:
2305 True- if VM added to VM group successfully
2306 False- if any error encounter
2307 """
2308
2309 addvm_resp = self.perform_request(req_type='GET',url=vmGroupNameURL, headers=headers)#, data=payload)
2310
2311 if addvm_resp.status_code != requests.codes.ok:
2312 self.logger.debug ("REST API call to get VM Group Name url {} failed. Return status code {}"\
2313 .format(vmGroupNameURL, addvm_resp.status_code))
2314 return False
2315 else:
2316 resp_xml = XmlElementTree.fromstring(addvm_resp.content)
2317 for child in resp_xml:
2318 if child.tag.split('}')[1] == 'Link':
2319 if child.attrib.get("rel") == "addVms":
2320 addvmtogrpURL = child.attrib.get("href")
2321
2322 #Get vm details
2323 url_list = [self.url, '/api/vApp/vm-',vm_uuid]
2324 vmdetailsURL = ''.join(url_list)
2325
2326 resp = self.perform_request(req_type='GET',url=vmdetailsURL, headers=headers)
2327
2328 if resp.status_code != requests.codes.ok:
2329 self.logger.debug ("REST API call {} failed. Return status code {}".format(vmdetailsURL, resp.status_code))
2330 return False
2331
2332 #Parse VM details
2333 resp_xml = XmlElementTree.fromstring(resp.content)
2334 if resp_xml.tag.split('}')[1] == "Vm":
2335 vm_id = resp_xml.attrib.get("id")
2336 vm_name = resp_xml.attrib.get("name")
2337 vm_href = resp_xml.attrib.get("href")
2338 #print vm_id, vm_name, vm_href
2339 #Add VM into VMgroup
2340 payload = """<?xml version="1.0" encoding="UTF-8"?>\
2341 <ns2:Vms xmlns:ns2="http://www.vmware.com/vcloud/v1.5" \
2342 xmlns="http://www.vmware.com/vcloud/versions" \
2343 xmlns:ns3="http://schemas.dmtf.org/ovf/envelope/1" \
2344 xmlns:ns4="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData" \
2345 xmlns:ns5="http://schemas.dmtf.org/wbem/wscim/1/common" \
2346 xmlns:ns6="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData" \
2347 xmlns:ns7="http://www.vmware.com/schema/ovf" \
2348 xmlns:ns8="http://schemas.dmtf.org/ovf/environment/1" \
2349 xmlns:ns9="http://www.vmware.com/vcloud/extension/v1.5">\
2350 <ns2:VmReference href="{}" id="{}" name="{}" \
2351 type="application/vnd.vmware.vcloud.vm+xml" />\
2352 </ns2:Vms>""".format(vm_href, vm_id, vm_name)
2353
2354 addvmtogrp_resp = self.perform_request(req_type='POST',url=addvmtogrpURL, headers=headers, data=payload)
2355
2356 if addvmtogrp_resp.status_code != requests.codes.accepted:
2357 self.logger.debug ("REST API call {} failed. Return status code {}".format(addvmtogrpURL, addvmtogrp_resp.status_code))
2358 return False
2359 else:
2360 self.logger.debug ("Done adding VM {} to VMgroup {}".format(vm_name, vmGroup_name))
2361 return True
2362
2363
2364 def create_vmgroup(self, vmgroup_name, vmgroup_href, headers):
2365 """Method to create a VM group in vCD
2366
2367 Args:
2368 vmgroup_name : Name of VM group to be created
2369 vmgroup_href : href for vmgroup
2370 headers- Headers for REST request
2371 """
2372 #POST to add URL with required data
2373 vmgroup_status = False
2374 payload = """<VMWVmGroup xmlns="http://www.vmware.com/vcloud/extension/v1.5" \
2375 xmlns:vcloud_v1.5="http://www.vmware.com/vcloud/v1.5" name="{}">\
2376 <vmCount>1</vmCount>\
2377 </VMWVmGroup>""".format(vmgroup_name)
2378 resp = self.perform_request(req_type='POST',url=vmgroup_href, headers=headers, data=payload)
2379
2380 if resp.status_code != requests.codes.accepted:
2381 self.logger.debug ("REST API call {} failed. Return status code {}".format(vmgroup_href, resp.status_code))
2382 return vmgroup_status
2383 else:
2384 vmgroup_task = self.get_task_from_response(resp.content)
2385 if vmgroup_task is None or vmgroup_task is False:
2386 raise vimconn.vimconnUnexpectedResponse(
2387 "create_vmgroup(): failed to create VM group {}".format(vmgroup_name))
2388
2389 # wait for task to complete
2390 result = self.client.get_task_monitor().wait_for_success(task=vmgroup_task)
2391
2392 if result.get('status') == 'success':
2393 self.logger.debug("create_vmgroup(): Successfully created VM group {}".format(vmgroup_name))
2394 #time.sleep(10)
2395 vmgroup_status = True
2396 return vmgroup_status
2397 else:
2398 raise vimconn.vimconnUnexpectedResponse(\
2399 "create_vmgroup(): failed to create VM group {}".format(vmgroup_name))
2400
2401
2402 def find_vmgroup_reference(self, url, headers):
2403 """ Method to create a new VMGroup which is required to add created VM
2404 Args:
2405 url- resource pool href
2406 headers- header information
2407
2408 Returns:
2409 returns href to VM group to create VM group
2410 """
2411 #Perform GET on resource pool to find 'add' link to create VMGroup
2412 #https://vcd-ip/api/admin/extension/providervdc/<providervdc id>/resourcePools
2413 vmgrp_href = None
2414 resp = self.perform_request(req_type='GET',url=url, headers=headers)
2415
2416 if resp.status_code != requests.codes.ok:
2417 self.logger.debug ("REST API call {} failed. Return status code {}".format(url, resp.status_code))
2418 else:
2419 #Get the href to add vmGroup to vCD
2420 resp_xml = XmlElementTree.fromstring(resp.content)
2421 for child in resp_xml:
2422 if 'VMWProviderVdcResourcePool' in child.tag:
2423 for schild in child:
2424 if 'Link' in schild.tag:
2425 #Find href with type VMGroup and rel with add
2426 if schild.attrib.get('type') == "application/vnd.vmware.admin.vmwVmGroupType+xml"\
2427 and schild.attrib.get('rel') == "add":
2428 vmgrp_href = schild.attrib.get('href')
2429 return vmgrp_href
2430
2431
2432 def check_availibility_zone(self, az, respool_href, headers):
2433 """ Method to verify requested av zone is present or not in provided
2434 resource pool
2435
2436 Args:
2437 az - name of hostgroup (availibility_zone)
2438 respool_href - Resource Pool href
2439 headers - Headers to make REST call
2440 Returns:
2441 az_found - True if availibility_zone is found else False
2442 """
2443 az_found = False
2444 headers['Accept']='application/*+xml;version=27.0'
2445 resp = self.perform_request(req_type='GET',url=respool_href, headers=headers)
2446
2447 if resp.status_code != requests.codes.ok:
2448 self.logger.debug ("REST API call {} failed. Return status code {}".format(respool_href, resp.status_code))
2449 else:
2450 #Get the href to hostGroups and find provided hostGroup is present in it
2451 resp_xml = XmlElementTree.fromstring(resp.content)
2452
2453 for child in resp_xml:
2454 if 'VMWProviderVdcResourcePool' in child.tag:
2455 for schild in child:
2456 if 'Link' in schild.tag:
2457 if schild.attrib.get('type') == "application/vnd.vmware.admin.vmwHostGroupsType+xml":
2458 hostGroup_href = schild.attrib.get('href')
2459 hg_resp = self.perform_request(req_type='GET',url=hostGroup_href, headers=headers)
2460 if hg_resp.status_code != requests.codes.ok:
2461 self.logger.debug ("REST API call {} failed. Return status code {}".format(hostGroup_href, hg_resp.status_code))
2462 else:
2463 hg_resp_xml = XmlElementTree.fromstring(hg_resp.content)
2464 for hostGroup in hg_resp_xml:
2465 if 'HostGroup' in hostGroup.tag:
2466 if hostGroup.attrib.get("name") == az:
2467 az_found = True
2468 break
2469 return az_found
2470
2471
2472 def get_pvdc_for_org(self, org_vdc, headers):
2473 """ This method gets provider vdc references from organisation
2474
2475 Args:
2476 org_vdc - name of the organisation VDC to find pvdc
2477 headers - headers to make REST call
2478
2479 Returns:
2480 None - if no pvdc href found else
2481 pvdc_href - href to pvdc
2482 """
2483
2484 #Get provider VDC references from vCD
2485 pvdc_href = None
2486 #url = '<vcd url>/api/admin/extension/providerVdcReferences'
2487 url_list = [self.url, '/api/admin/extension/providerVdcReferences']
2488 url = ''.join(url_list)
2489
2490 response = self.perform_request(req_type='GET',url=url, headers=headers)
2491 if response.status_code != requests.codes.ok:
2492 self.logger.debug ("REST API call {} failed. Return status code {}"\
2493 .format(url, response.status_code))
2494 else:
2495 xmlroot_response = XmlElementTree.fromstring(response.content)
2496 for child in xmlroot_response:
2497 if 'ProviderVdcReference' in child.tag:
2498 pvdc_href = child.attrib.get('href')
2499 #Get vdcReferences to find org
2500 pvdc_resp = self.perform_request(req_type='GET',url=pvdc_href, headers=headers)
2501 if pvdc_resp.status_code != requests.codes.ok:
2502 raise vimconn.vimconnException("REST API call {} failed. "\
2503 "Return status code {}"\
2504 .format(url, pvdc_resp.status_code))
2505
2506 pvdc_resp_xml = XmlElementTree.fromstring(pvdc_resp.content)
2507 for child in pvdc_resp_xml:
2508 if 'Link' in child.tag:
2509 if child.attrib.get('type') == "application/vnd.vmware.admin.vdcReferences+xml":
2510 vdc_href = child.attrib.get('href')
2511
2512 #Check if provided org is present in vdc
2513 vdc_resp = self.perform_request(req_type='GET',
2514 url=vdc_href,
2515 headers=headers)
2516 if vdc_resp.status_code != requests.codes.ok:
2517 raise vimconn.vimconnException("REST API call {} failed. "\
2518 "Return status code {}"\
2519 .format(url, vdc_resp.status_code))
2520 vdc_resp_xml = XmlElementTree.fromstring(vdc_resp.content)
2521 for child in vdc_resp_xml:
2522 if 'VdcReference' in child.tag:
2523 if child.attrib.get('name') == org_vdc:
2524 return pvdc_href
2525
2526
2527 def get_resource_pool_details(self, pvdc_href, headers):
2528 """ Method to get resource pool information.
2529 Host groups are property of resource group.
2530 To get host groups, we need to GET details of resource pool.
2531
2532 Args:
2533 pvdc_href: href to pvdc details
2534 headers: headers
2535
2536 Returns:
2537 respool_href - Returns href link reference to resource pool
2538 """
2539 respool_href = None
2540 resp = self.perform_request(req_type='GET',url=pvdc_href, headers=headers)
2541
2542 if resp.status_code != requests.codes.ok:
2543 self.logger.debug ("REST API call {} failed. Return status code {}"\
2544 .format(pvdc_href, resp.status_code))
2545 else:
2546 respool_resp_xml = XmlElementTree.fromstring(resp.content)
2547 for child in respool_resp_xml:
2548 if 'Link' in child.tag:
2549 if child.attrib.get('type') == "application/vnd.vmware.admin.vmwProviderVdcResourcePoolSet+xml":
2550 respool_href = child.attrib.get("href")
2551 break
2552 return respool_href
2553
2554
2555 def log_message(self, msg):
2556 """
2557 Method to log error messages related to Affinity rule creation
2558 in new_vminstance & raise Exception
2559 Args :
2560 msg - Error message to be logged
2561
2562 """
2563 #get token to connect vCD as a normal user
2564 self.get_token()
2565 self.logger.debug(msg)
2566 raise vimconn.vimconnException(msg)
2567
2568
bayramovef390722016-09-27 03:34:46 -07002569 ##
2570 ##
2571 ## based on current discussion
2572 ##
2573 ##
2574 ## server:
2575 # created: '2016-09-08T11:51:58'
2576 # description: simple-instance.linux1.1
2577 # flavor: ddc6776e-75a9-11e6-ad5f-0800273e724c
2578 # hostId: e836c036-74e7-11e6-b249-0800273e724c
2579 # image: dde30fe6-75a9-11e6-ad5f-0800273e724c
2580 # status: ACTIVE
2581 # error_msg:
2582 # interfaces: …
2583 #
bayramovfe3f3c92016-10-04 07:53:41 +04002584 def get_vminstance(self, vim_vm_uuid=None):
bayramov5761ad12016-10-04 09:00:30 +04002585 """Returns the VM instance information from VIM"""
bayramov325fa1c2016-09-08 01:42:46 -07002586
bayramovef390722016-09-27 03:34:46 -07002587 self.logger.debug("Client requesting vm instance {} ".format(vim_vm_uuid))
bayramov325fa1c2016-09-08 01:42:46 -07002588
kasarc5bf2932018-03-09 04:15:22 -08002589 org, vdc = self.get_vdc_details()
bayramovef390722016-09-27 03:34:46 -07002590 if vdc is None:
bayramovfe3f3c92016-10-04 07:53:41 +04002591 raise vimconn.vimconnConnectionException(
2592 "Failed to get a reference of VDC for a tenant {}".format(self.tenant_name))
bayramov325fa1c2016-09-08 01:42:46 -07002593
bayramovfe3f3c92016-10-04 07:53:41 +04002594 vm_info_dict = self.get_vapp_details_rest(vapp_uuid=vim_vm_uuid)
2595 if not vm_info_dict:
bayramovef390722016-09-27 03:34:46 -07002596 self.logger.debug("get_vminstance(): Failed to get vApp name by UUID {}".format(vim_vm_uuid))
bayramovfe3f3c92016-10-04 07:53:41 +04002597 raise vimconn.vimconnNotFoundException("Failed to get vApp name by UUID {}".format(vim_vm_uuid))
bayramov325fa1c2016-09-08 01:42:46 -07002598
bayramovfe3f3c92016-10-04 07:53:41 +04002599 status_key = vm_info_dict['status']
2600 error = ''
bayramovef390722016-09-27 03:34:46 -07002601 try:
bayramovfe3f3c92016-10-04 07:53:41 +04002602 vm_dict = {'created': vm_info_dict['created'],
2603 'description': vm_info_dict['name'],
2604 'status': vcdStatusCode2manoFormat[int(status_key)],
2605 'hostId': vm_info_dict['vmuuid'],
2606 'error_msg': error,
2607 'vim_info': yaml.safe_dump(vm_info_dict), 'interfaces': []}
bayramovef390722016-09-27 03:34:46 -07002608
bayramov5761ad12016-10-04 09:00:30 +04002609 if 'interfaces' in vm_info_dict:
bayramovfe3f3c92016-10-04 07:53:41 +04002610 vm_dict['interfaces'] = vm_info_dict['interfaces']
2611 else:
2612 vm_dict['interfaces'] = []
2613 except KeyError:
2614 vm_dict = {'created': '',
2615 'description': '',
2616 'status': vcdStatusCode2manoFormat[int(-1)],
2617 'hostId': vm_info_dict['vmuuid'],
2618 'error_msg': "Inconsistency state",
2619 'vim_info': yaml.safe_dump(vm_info_dict), 'interfaces': []}
bayramovef390722016-09-27 03:34:46 -07002620
2621 return vm_dict
2622
tierno98e909c2017-10-14 13:27:03 +02002623 def delete_vminstance(self, vm__vim_uuid, created_items=None):
bayramovef390722016-09-27 03:34:46 -07002624 """Method poweroff and remove VM instance from vcloud director network.
2625
2626 Args:
2627 vm__vim_uuid: VM UUID
2628
2629 Returns:
2630 Returns the instance identifier
2631 """
2632
2633 self.logger.debug("Client requesting delete vm instance {} ".format(vm__vim_uuid))
bayramov325fa1c2016-09-08 01:42:46 -07002634
kasarc5bf2932018-03-09 04:15:22 -08002635 org, vdc = self.get_vdc_details()
sbhangarea8e5b782018-06-21 02:10:03 -07002636 vdc_obj = VDC(self.client, href=vdc.get('href'))
kasarc5bf2932018-03-09 04:15:22 -08002637 if vdc_obj is None:
bayramovef390722016-09-27 03:34:46 -07002638 self.logger.debug("delete_vminstance(): Failed to get a reference of VDC for a tenant {}".format(
2639 self.tenant_name))
bayramovbd6160f2016-09-28 04:12:05 +04002640 raise vimconn.vimconnException(
2641 "delete_vminstance(): Failed to get a reference of VDC for a tenant {}".format(self.tenant_name))
bayramov325fa1c2016-09-08 01:42:46 -07002642
bayramovef390722016-09-27 03:34:46 -07002643 try:
kasarc5bf2932018-03-09 04:15:22 -08002644 vapp_name = self.get_namebyvappid(vm__vim_uuid)
2645 vapp_resource = vdc_obj.get_vapp(vapp_name)
2646 vapp = VApp(self.client, resource=vapp_resource)
bayramovef390722016-09-27 03:34:46 -07002647 if vapp_name is None:
2648 self.logger.debug("delete_vminstance(): Failed to get vm by given {} vm uuid".format(vm__vim_uuid))
2649 return -1, "delete_vminstance(): Failed to get vm by given {} vm uuid".format(vm__vim_uuid)
2650 else:
2651 self.logger.info("Deleting vApp {} and UUID {}".format(vapp_name, vm__vim_uuid))
bayramov325fa1c2016-09-08 01:42:46 -07002652
bayramovef390722016-09-27 03:34:46 -07002653 # Delete vApp and wait for status change if task executed and vApp is None.
bayramov325fa1c2016-09-08 01:42:46 -07002654
kate13ab2c42016-12-23 01:34:24 -08002655 if vapp:
kasarc5bf2932018-03-09 04:15:22 -08002656 if vapp_resource.get('deployed') == 'true':
kate13ab2c42016-12-23 01:34:24 -08002657 self.logger.info("Powering off vApp {}".format(vapp_name))
2658 #Power off vApp
2659 powered_off = False
2660 wait_time = 0
2661 while wait_time <= MAX_WAIT_TIME:
kasarc5bf2932018-03-09 04:15:22 -08002662 power_off_task = vapp.power_off()
2663 result = self.client.get_task_monitor().wait_for_success(task=power_off_task)
bayramovef390722016-09-27 03:34:46 -07002664
kasarc5bf2932018-03-09 04:15:22 -08002665 if result.get('status') == 'success':
2666 powered_off = True
2667 break
kate13ab2c42016-12-23 01:34:24 -08002668 else:
2669 self.logger.info("Wait for vApp {} to power off".format(vapp_name))
2670 time.sleep(INTERVAL_TIME)
2671
2672 wait_time +=INTERVAL_TIME
2673 if not powered_off:
2674 self.logger.debug("delete_vminstance(): Failed to power off VM instance {} ".format(vm__vim_uuid))
2675 else:
2676 self.logger.info("delete_vminstance(): Powered off VM instance {} ".format(vm__vim_uuid))
2677
2678 #Undeploy vApp
2679 self.logger.info("Undeploy vApp {}".format(vapp_name))
2680 wait_time = 0
2681 undeployed = False
2682 while wait_time <= MAX_WAIT_TIME:
sbhangarea8e5b782018-06-21 02:10:03 -07002683 vapp = VApp(self.client, resource=vapp_resource)
kate13ab2c42016-12-23 01:34:24 -08002684 if not vapp:
2685 self.logger.debug("delete_vminstance(): Failed to get vm by given {} vm uuid".format(vm__vim_uuid))
2686 return -1, "delete_vminstance(): Failed to get vm by given {} vm uuid".format(vm__vim_uuid)
kasarc5bf2932018-03-09 04:15:22 -08002687 undeploy_task = vapp.undeploy()
kate13ab2c42016-12-23 01:34:24 -08002688
kasarc5bf2932018-03-09 04:15:22 -08002689 result = self.client.get_task_monitor().wait_for_success(task=undeploy_task)
2690 if result.get('status') == 'success':
2691 undeployed = True
2692 break
kate13ab2c42016-12-23 01:34:24 -08002693 else:
2694 self.logger.debug("Wait for vApp {} to undeploy".format(vapp_name))
2695 time.sleep(INTERVAL_TIME)
2696
2697 wait_time +=INTERVAL_TIME
2698
2699 if not undeployed:
sbhangarea8e5b782018-06-21 02:10:03 -07002700 self.logger.debug("delete_vminstance(): Failed to undeploy vApp {} ".format(vm__vim_uuid))
kate13ab2c42016-12-23 01:34:24 -08002701
2702 # delete vapp
2703 self.logger.info("Start deletion of vApp {} ".format(vapp_name))
kate13ab2c42016-12-23 01:34:24 -08002704
2705 if vapp is not None:
2706 wait_time = 0
2707 result = False
2708
2709 while wait_time <= MAX_WAIT_TIME:
kasarc5bf2932018-03-09 04:15:22 -08002710 vapp = VApp(self.client, resource=vapp_resource)
kate13ab2c42016-12-23 01:34:24 -08002711 if not vapp:
2712 self.logger.debug("delete_vminstance(): Failed to get vm by given {} vm uuid".format(vm__vim_uuid))
2713 return -1, "delete_vminstance(): Failed to get vm by given {} vm uuid".format(vm__vim_uuid)
2714
kasarc5bf2932018-03-09 04:15:22 -08002715 delete_task = vdc_obj.delete_vapp(vapp.name, force=True)
kate13ab2c42016-12-23 01:34:24 -08002716
kasarc5bf2932018-03-09 04:15:22 -08002717 result = self.client.get_task_monitor().wait_for_success(task=delete_task)
sbhangarea8e5b782018-06-21 02:10:03 -07002718 if result.get('status') == 'success':
kasarc5bf2932018-03-09 04:15:22 -08002719 break
kate13ab2c42016-12-23 01:34:24 -08002720 else:
2721 self.logger.debug("Wait for vApp {} to delete".format(vapp_name))
2722 time.sleep(INTERVAL_TIME)
2723
2724 wait_time +=INTERVAL_TIME
2725
kasarc5bf2932018-03-09 04:15:22 -08002726 if result is None:
bayramovbd6160f2016-09-28 04:12:05 +04002727 self.logger.debug("delete_vminstance(): Failed delete uuid {} ".format(vm__vim_uuid))
kasarc5bf2932018-03-09 04:15:22 -08002728 else:
2729 self.logger.info("Deleted vm instance {} sccessfully".format(vm__vim_uuid))
2730 return vm__vim_uuid
bayramovef390722016-09-27 03:34:46 -07002731 except:
2732 self.logger.debug(traceback.format_exc())
bayramovbd6160f2016-09-28 04:12:05 +04002733 raise vimconn.vimconnException("delete_vminstance(): Failed delete vm instance {}".format(vm__vim_uuid))
bayramovef390722016-09-27 03:34:46 -07002734
bayramov325fa1c2016-09-08 01:42:46 -07002735
2736 def refresh_vms_status(self, vm_list):
bayramovef390722016-09-27 03:34:46 -07002737 """Get the status of the virtual machines and their interfaces/ports
bayramov325fa1c2016-09-08 01:42:46 -07002738 Params: the list of VM identifiers
2739 Returns a dictionary with:
2740 vm_id: #VIM id of this Virtual Machine
2741 status: #Mandatory. Text with one of:
2742 # DELETED (not found at vim)
bayramovef390722016-09-27 03:34:46 -07002743 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
bayramov325fa1c2016-09-08 01:42:46 -07002744 # OTHER (Vim reported other status not understood)
2745 # ERROR (VIM indicates an ERROR status)
bayramovef390722016-09-27 03:34:46 -07002746 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
bayramov325fa1c2016-09-08 01:42:46 -07002747 # CREATING (on building process), ERROR
2748 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
2749 #
bayramovef390722016-09-27 03:34:46 -07002750 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
bayramov325fa1c2016-09-08 01:42:46 -07002751 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
2752 interfaces:
2753 - vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
2754 mac_address: #Text format XX:XX:XX:XX:XX:XX
2755 vim_net_id: #network id where this interface is connected
2756 vim_interface_id: #interface/port VIM id
2757 ip_address: #null, or text with IPv4, IPv6 address
bayramovef390722016-09-27 03:34:46 -07002758 """
bayramov325fa1c2016-09-08 01:42:46 -07002759
bayramovef390722016-09-27 03:34:46 -07002760 self.logger.debug("Client requesting refresh vm status for {} ".format(vm_list))
bhangare985a1fd2017-01-31 01:53:21 -08002761
kasarc5bf2932018-03-09 04:15:22 -08002762 org,vdc = self.get_vdc_details()
bayramovef390722016-09-27 03:34:46 -07002763 if vdc is None:
bayramovbd6160f2016-09-28 04:12:05 +04002764 raise vimconn.vimconnException("Failed to get a reference of VDC for a tenant {}".format(self.tenant_name))
bayramovef390722016-09-27 03:34:46 -07002765
2766 vms_dict = {}
kasarde691232017-03-25 03:37:31 -07002767 nsx_edge_list = []
bayramovef390722016-09-27 03:34:46 -07002768 for vmuuid in vm_list:
kasarc5bf2932018-03-09 04:15:22 -08002769 vapp_name = self.get_namebyvappid(vmuuid)
2770 if vapp_name is not None:
bayramovef390722016-09-27 03:34:46 -07002771
bayramovef390722016-09-27 03:34:46 -07002772 try:
bhangare1a0b97c2017-06-21 02:20:15 -07002773 vm_pci_details = self.get_vm_pci_details(vmuuid)
kasarc5bf2932018-03-09 04:15:22 -08002774 vdc_obj = VDC(self.client, href=vdc.get('href'))
2775 vapp_resource = vdc_obj.get_vapp(vapp_name)
2776 the_vapp = VApp(self.client, resource=vapp_resource)
bhangarebfdca492017-03-11 01:32:46 -08002777
kasarc5bf2932018-03-09 04:15:22 -08002778 vm_details = {}
2779 for vm in the_vapp.get_all_vms():
2780 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
sbhangarea8e5b782018-06-21 02:10:03 -07002781 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
kasarc5bf2932018-03-09 04:15:22 -08002782 response = self.perform_request(req_type='GET',
2783 url=vm.get('href'),
2784 headers=headers)
bhangarebfdca492017-03-11 01:32:46 -08002785
kasarc5bf2932018-03-09 04:15:22 -08002786 if response.status_code != 200:
2787 self.logger.error("refresh_vms_status : REST call {} failed reason : {}"\
2788 "status code : {}".format(vm.get('href'),
2789 response.content,
2790 response.status_code))
2791 raise vimconn.vimconnException("refresh_vms_status : Failed to get "\
2792 "VM details")
2793 xmlroot = XmlElementTree.fromstring(response.content)
kasarde691232017-03-25 03:37:31 -07002794
Ravi Chamarty1fdf9992018-10-07 15:45:44 +00002795
kasarc5bf2932018-03-09 04:15:22 -08002796 result = response.content.replace("\n"," ")
Ravi Chamarty1fdf9992018-10-07 15:45:44 +00002797 hdd_match = re.search('vcloud:capacity="(\d+)"\svcloud:storageProfileOverrideVmDefault=',result)
2798 if hdd_match:
2799 hdd_mb = hdd_match.group(1)
2800 vm_details['hdd_mb'] = int(hdd_mb) if hdd_mb else None
2801 cpus_match = re.search('<rasd:Description>Number of Virtual CPUs</.*?>(\d+)</rasd:VirtualQuantity>',result)
2802 if cpus_match:
2803 cpus = cpus_match.group(1)
2804 vm_details['cpus'] = int(cpus) if cpus else None
kasarc5bf2932018-03-09 04:15:22 -08002805 memory_mb = re.search('<rasd:Description>Memory Size</.*?>(\d+)</rasd:VirtualQuantity>',result).group(1)
2806 vm_details['memory_mb'] = int(memory_mb) if memory_mb else None
2807 vm_details['status'] = vcdStatusCode2manoFormat[int(xmlroot.get('status'))]
2808 vm_details['id'] = xmlroot.get('id')
2809 vm_details['name'] = xmlroot.get('name')
2810 vm_info = [vm_details]
2811 if vm_pci_details:
sbhangarea8e5b782018-06-21 02:10:03 -07002812 vm_info[0].update(vm_pci_details)
kasarc5bf2932018-03-09 04:15:22 -08002813
2814 vm_dict = {'status': vcdStatusCode2manoFormat[int(vapp_resource.get('status'))],
2815 'error_msg': vcdStatusCode2manoFormat[int(vapp_resource.get('status'))],
2816 'vim_info': yaml.safe_dump(vm_info), 'interfaces': []}
2817
2818 # get networks
sbhangarea8e5b782018-06-21 02:10:03 -07002819 vm_ip = None
kasar1b7b9522018-05-15 06:15:07 -07002820 vm_mac = None
2821 networks = re.findall('<NetworkConnection needsCustomization=.*?</NetworkConnection>',result)
2822 for network in networks:
2823 mac_s = re.search('<MACAddress>(.*?)</MACAddress>',network)
2824 vm_mac = mac_s.group(1) if mac_s else None
2825 ip_s = re.search('<IpAddress>(.*?)</IpAddress>',network)
2826 vm_ip = ip_s.group(1) if ip_s else None
kasarc5bf2932018-03-09 04:15:22 -08002827
kasar1b7b9522018-05-15 06:15:07 -07002828 if vm_ip is None:
2829 if not nsx_edge_list:
2830 nsx_edge_list = self.get_edge_details()
2831 if nsx_edge_list is None:
2832 raise vimconn.vimconnException("refresh_vms_status:"\
2833 "Failed to get edge details from NSX Manager")
2834 if vm_mac is not None:
2835 vm_ip = self.get_ipaddr_from_NSXedge(nsx_edge_list, vm_mac)
kasarc5bf2932018-03-09 04:15:22 -08002836
kasarabac1e22018-05-17 02:44:39 -07002837 net_s = re.search('network="(.*?)"',network)
2838 network_name = net_s.group(1) if net_s else None
2839
kasar1b7b9522018-05-15 06:15:07 -07002840 vm_net_id = self.get_network_id_by_name(network_name)
2841 interface = {"mac_address": vm_mac,
2842 "vim_net_id": vm_net_id,
2843 "vim_interface_id": vm_net_id,
2844 "ip_address": vm_ip}
2845
2846 vm_dict["interfaces"].append(interface)
kasarc5bf2932018-03-09 04:15:22 -08002847
bayramovef390722016-09-27 03:34:46 -07002848 # add a vm to vm dict
2849 vms_dict.setdefault(vmuuid, vm_dict)
kasarc5bf2932018-03-09 04:15:22 -08002850 self.logger.debug("refresh_vms_status : vm info {}".format(vm_dict))
bhangarebfdca492017-03-11 01:32:46 -08002851 except Exception as exp:
2852 self.logger.debug("Error in response {}".format(exp))
bayramovef390722016-09-27 03:34:46 -07002853 self.logger.debug(traceback.format_exc())
2854
2855 return vms_dict
2856
kasarde691232017-03-25 03:37:31 -07002857
2858 def get_edge_details(self):
2859 """Get the NSX edge list from NSX Manager
2860 Returns list of NSX edges
2861 """
2862 edge_list = []
2863 rheaders = {'Content-Type': 'application/xml'}
2864 nsx_api_url = '/api/4.0/edges'
2865
2866 self.logger.debug("Get edge details from NSX Manager {} {}".format(self.nsx_manager, nsx_api_url))
2867
2868 try:
2869 resp = requests.get(self.nsx_manager + nsx_api_url,
2870 auth = (self.nsx_user, self.nsx_password),
2871 verify = False, headers = rheaders)
2872 if resp.status_code == requests.codes.ok:
2873 paged_Edge_List = XmlElementTree.fromstring(resp.text)
2874 for edge_pages in paged_Edge_List:
2875 if edge_pages.tag == 'edgePage':
2876 for edge_summary in edge_pages:
2877 if edge_summary.tag == 'pagingInfo':
2878 for element in edge_summary:
2879 if element.tag == 'totalCount' and element.text == '0':
2880 raise vimconn.vimconnException("get_edge_details: No NSX edges details found: {}"
2881 .format(self.nsx_manager))
2882
2883 if edge_summary.tag == 'edgeSummary':
2884 for element in edge_summary:
2885 if element.tag == 'id':
2886 edge_list.append(element.text)
2887 else:
2888 raise vimconn.vimconnException("get_edge_details: No NSX edge details found: {}"
2889 .format(self.nsx_manager))
2890
2891 if not edge_list:
2892 raise vimconn.vimconnException("get_edge_details: "\
2893 "No NSX edge details found: {}"
2894 .format(self.nsx_manager))
2895 else:
2896 self.logger.debug("get_edge_details: Found NSX edges {}".format(edge_list))
2897 return edge_list
2898 else:
2899 self.logger.debug("get_edge_details: "
2900 "Failed to get NSX edge details from NSX Manager: {}"
2901 .format(resp.content))
2902 return None
2903
2904 except Exception as exp:
2905 self.logger.debug("get_edge_details: "\
2906 "Failed to get NSX edge details from NSX Manager: {}"
2907 .format(exp))
2908 raise vimconn.vimconnException("get_edge_details: "\
2909 "Failed to get NSX edge details from NSX Manager: {}"
2910 .format(exp))
2911
2912
2913 def get_ipaddr_from_NSXedge(self, nsx_edges, mac_address):
2914 """Get IP address details from NSX edges, using the MAC address
2915 PARAMS: nsx_edges : List of NSX edges
2916 mac_address : Find IP address corresponding to this MAC address
2917 Returns: IP address corrresponding to the provided MAC address
2918 """
2919
2920 ip_addr = None
2921 rheaders = {'Content-Type': 'application/xml'}
2922
2923 self.logger.debug("get_ipaddr_from_NSXedge: Finding IP addr from NSX edge")
2924
2925 try:
2926 for edge in nsx_edges:
2927 nsx_api_url = '/api/4.0/edges/'+ edge +'/dhcp/leaseInfo'
2928
2929 resp = requests.get(self.nsx_manager + nsx_api_url,
2930 auth = (self.nsx_user, self.nsx_password),
2931 verify = False, headers = rheaders)
2932
2933 if resp.status_code == requests.codes.ok:
2934 dhcp_leases = XmlElementTree.fromstring(resp.text)
2935 for child in dhcp_leases:
2936 if child.tag == 'dhcpLeaseInfo':
2937 dhcpLeaseInfo = child
2938 for leaseInfo in dhcpLeaseInfo:
2939 for elem in leaseInfo:
2940 if (elem.tag)=='macAddress':
2941 edge_mac_addr = elem.text
2942 if (elem.tag)=='ipAddress':
2943 ip_addr = elem.text
2944 if edge_mac_addr is not None:
2945 if edge_mac_addr == mac_address:
2946 self.logger.debug("Found ip addr {} for mac {} at NSX edge {}"
2947 .format(ip_addr, mac_address,edge))
2948 return ip_addr
2949 else:
2950 self.logger.debug("get_ipaddr_from_NSXedge: "\
2951 "Error occurred while getting DHCP lease info from NSX Manager: {}"
2952 .format(resp.content))
2953
2954 self.logger.debug("get_ipaddr_from_NSXedge: No IP addr found in any NSX edge")
2955 return None
2956
2957 except XmlElementTree.ParseError as Err:
2958 self.logger.debug("ParseError in response from NSX Manager {}".format(Err.message), exc_info=True)
2959
2960
tierno98e909c2017-10-14 13:27:03 +02002961 def action_vminstance(self, vm__vim_uuid=None, action_dict=None, created_items={}):
bayramovef390722016-09-27 03:34:46 -07002962 """Send and action over a VM instance from VIM
2963 Returns the vm_id if the action was successfully sent to the VIM"""
2964
2965 self.logger.debug("Received action for vm {} and action dict {}".format(vm__vim_uuid, action_dict))
2966 if vm__vim_uuid is None or action_dict is None:
bayramovbd6160f2016-09-28 04:12:05 +04002967 raise vimconn.vimconnException("Invalid request. VM id or action is None.")
bayramovef390722016-09-27 03:34:46 -07002968
kasarc5bf2932018-03-09 04:15:22 -08002969 org, vdc = self.get_vdc_details()
bayramovef390722016-09-27 03:34:46 -07002970 if vdc is None:
tierno98e909c2017-10-14 13:27:03 +02002971 raise vimconn.vimconnException("Failed to get a reference of VDC for a tenant {}".format(self.tenant_name))
bayramovef390722016-09-27 03:34:46 -07002972
kasarc5bf2932018-03-09 04:15:22 -08002973 vapp_name = self.get_namebyvappid(vm__vim_uuid)
bayramovef390722016-09-27 03:34:46 -07002974 if vapp_name is None:
2975 self.logger.debug("action_vminstance(): Failed to get vm by given {} vm uuid".format(vm__vim_uuid))
bayramovbd6160f2016-09-28 04:12:05 +04002976 raise vimconn.vimconnException("Failed to get vm by given {} vm uuid".format(vm__vim_uuid))
bayramovef390722016-09-27 03:34:46 -07002977 else:
2978 self.logger.info("Action_vminstance vApp {} and UUID {}".format(vapp_name, vm__vim_uuid))
2979
2980 try:
kasarc5bf2932018-03-09 04:15:22 -08002981 vdc_obj = VDC(self.client, href=vdc.get('href'))
2982 vapp_resource = vdc_obj.get_vapp(vapp_name)
sbhangarea8e5b782018-06-21 02:10:03 -07002983 vapp = VApp(self.client, resource=vapp_resource)
bayramovef390722016-09-27 03:34:46 -07002984 if "start" in action_dict:
kasar3ac5dc42017-03-15 06:28:22 -07002985 self.logger.info("action_vminstance: Power on vApp: {}".format(vapp_name))
sbhangarea8e5b782018-06-21 02:10:03 -07002986 poweron_task = self.power_on_vapp(vm__vim_uuid, vapp_name)
2987 result = self.client.get_task_monitor().wait_for_success(task=poweron_task)
kasarc5bf2932018-03-09 04:15:22 -08002988 self.instance_actions_result("start", result, vapp_name)
kasar3ac5dc42017-03-15 06:28:22 -07002989 elif "rebuild" in action_dict:
2990 self.logger.info("action_vminstance: Rebuild vApp: {}".format(vapp_name))
kasarc5bf2932018-03-09 04:15:22 -08002991 rebuild_task = vapp.deploy(power_on=True)
sbhangarea8e5b782018-06-21 02:10:03 -07002992 result = self.client.get_task_monitor().wait_for_success(task=rebuild_task)
kasar3ac5dc42017-03-15 06:28:22 -07002993 self.instance_actions_result("rebuild", result, vapp_name)
bayramovef390722016-09-27 03:34:46 -07002994 elif "pause" in action_dict:
kasar3ac5dc42017-03-15 06:28:22 -07002995 self.logger.info("action_vminstance: pause vApp: {}".format(vapp_name))
kasarc5bf2932018-03-09 04:15:22 -08002996 pause_task = vapp.undeploy(action='suspend')
sbhangarea8e5b782018-06-21 02:10:03 -07002997 result = self.client.get_task_monitor().wait_for_success(task=pause_task)
kasar3ac5dc42017-03-15 06:28:22 -07002998 self.instance_actions_result("pause", result, vapp_name)
bayramovef390722016-09-27 03:34:46 -07002999 elif "resume" in action_dict:
kasar3ac5dc42017-03-15 06:28:22 -07003000 self.logger.info("action_vminstance: resume vApp: {}".format(vapp_name))
kasarc5bf2932018-03-09 04:15:22 -08003001 poweron_task = self.power_on_vapp(vm__vim_uuid, vapp_name)
3002 result = self.client.get_task_monitor().wait_for_success(task=poweron_task)
kasar3ac5dc42017-03-15 06:28:22 -07003003 self.instance_actions_result("resume", result, vapp_name)
bayramovef390722016-09-27 03:34:46 -07003004 elif "shutoff" in action_dict or "shutdown" in action_dict:
kasar3ac5dc42017-03-15 06:28:22 -07003005 action_name , value = action_dict.items()[0]
kasarc5bf2932018-03-09 04:15:22 -08003006 #For python3
3007 #action_name , value = list(action_dict.items())[0]
kasar3ac5dc42017-03-15 06:28:22 -07003008 self.logger.info("action_vminstance: {} vApp: {}".format(action_name, vapp_name))
kasarc5bf2932018-03-09 04:15:22 -08003009 shutdown_task = vapp.shutdown()
3010 result = self.client.get_task_monitor().wait_for_success(task=shutdown_task)
kasar3ac5dc42017-03-15 06:28:22 -07003011 if action_name == "shutdown":
3012 self.instance_actions_result("shutdown", result, vapp_name)
bhangare985a1fd2017-01-31 01:53:21 -08003013 else:
kasar3ac5dc42017-03-15 06:28:22 -07003014 self.instance_actions_result("shutoff", result, vapp_name)
bayramovef390722016-09-27 03:34:46 -07003015 elif "forceOff" in action_dict:
kasarc5bf2932018-03-09 04:15:22 -08003016 result = vapp.undeploy(action='powerOff')
kasar3ac5dc42017-03-15 06:28:22 -07003017 self.instance_actions_result("forceOff", result, vapp_name)
3018 elif "reboot" in action_dict:
3019 self.logger.info("action_vminstance: reboot vApp: {}".format(vapp_name))
kasarc5bf2932018-03-09 04:15:22 -08003020 reboot_task = vapp.reboot()
sbhangarea8e5b782018-06-21 02:10:03 -07003021 self.client.get_task_monitor().wait_for_success(task=reboot_task)
bayramovef390722016-09-27 03:34:46 -07003022 else:
kasar3ac5dc42017-03-15 06:28:22 -07003023 raise vimconn.vimconnException("action_vminstance: Invalid action {} or action is None.".format(action_dict))
kasarc5bf2932018-03-09 04:15:22 -08003024 return vm__vim_uuid
bhangarebfdca492017-03-11 01:32:46 -08003025 except Exception as exp :
3026 self.logger.debug("action_vminstance: Failed with Exception {}".format(exp))
3027 raise vimconn.vimconnException("action_vminstance: Failed with Exception {}".format(exp))
bayramovef390722016-09-27 03:34:46 -07003028
kasar3ac5dc42017-03-15 06:28:22 -07003029 def instance_actions_result(self, action, result, vapp_name):
kasarc5bf2932018-03-09 04:15:22 -08003030 if result.get('status') == 'success':
kasar3ac5dc42017-03-15 06:28:22 -07003031 self.logger.info("action_vminstance: Sucessfully {} the vApp: {}".format(action, vapp_name))
3032 else:
3033 self.logger.error("action_vminstance: Failed to {} vApp: {}".format(action, vapp_name))
3034
bayramovef390722016-09-27 03:34:46 -07003035 def get_vminstance_console(self, vm_id, console_type="vnc"):
3036 """
bayramov325fa1c2016-09-08 01:42:46 -07003037 Get a console for the virtual machine
3038 Params:
3039 vm_id: uuid of the VM
3040 console_type, can be:
bayramovef390722016-09-27 03:34:46 -07003041 "novnc" (by default), "xvpvnc" for VNC types,
bayramov325fa1c2016-09-08 01:42:46 -07003042 "rdp-html5" for RDP types, "spice-html5" for SPICE types
3043 Returns dict with the console parameters:
3044 protocol: ssh, ftp, http, https, ...
bayramovef390722016-09-27 03:34:46 -07003045 server: usually ip address
3046 port: the http, ssh, ... port
3047 suffix: extra text, e.g. the http path and query string
3048 """
bayramovbd6160f2016-09-28 04:12:05 +04003049 raise vimconn.vimconnNotImplemented("Should have implemented this")
bayramov325fa1c2016-09-08 01:42:46 -07003050
bayramovef390722016-09-27 03:34:46 -07003051 # NOT USED METHODS in current version
bayramov325fa1c2016-09-08 01:42:46 -07003052
3053 def host_vim2gui(self, host, server_dict):
bayramov5761ad12016-10-04 09:00:30 +04003054 """Transform host dictionary from VIM format to GUI format,
bayramov325fa1c2016-09-08 01:42:46 -07003055 and append to the server_dict
bayramov5761ad12016-10-04 09:00:30 +04003056 """
bayramovbd6160f2016-09-28 04:12:05 +04003057 raise vimconn.vimconnNotImplemented("Should have implemented this")
bayramov325fa1c2016-09-08 01:42:46 -07003058
3059 def get_hosts_info(self):
bayramov5761ad12016-10-04 09:00:30 +04003060 """Get the information of deployed hosts
3061 Returns the hosts content"""
bayramovbd6160f2016-09-28 04:12:05 +04003062 raise vimconn.vimconnNotImplemented("Should have implemented this")
bayramov325fa1c2016-09-08 01:42:46 -07003063
3064 def get_hosts(self, vim_tenant):
bayramov5761ad12016-10-04 09:00:30 +04003065 """Get the hosts and deployed instances
3066 Returns the hosts content"""
bayramovbd6160f2016-09-28 04:12:05 +04003067 raise vimconn.vimconnNotImplemented("Should have implemented this")
bayramov325fa1c2016-09-08 01:42:46 -07003068
3069 def get_processor_rankings(self):
bayramov5761ad12016-10-04 09:00:30 +04003070 """Get the processor rankings in the VIM database"""
bayramovbd6160f2016-09-28 04:12:05 +04003071 raise vimconn.vimconnNotImplemented("Should have implemented this")
bayramov325fa1c2016-09-08 01:42:46 -07003072
3073 def new_host(self, host_data):
bayramov5761ad12016-10-04 09:00:30 +04003074 """Adds a new host to VIM"""
bayramov325fa1c2016-09-08 01:42:46 -07003075 '''Returns status code of the VIM response'''
bayramovbd6160f2016-09-28 04:12:05 +04003076 raise vimconn.vimconnNotImplemented("Should have implemented this")
bayramov325fa1c2016-09-08 01:42:46 -07003077
3078 def new_external_port(self, port_data):
bayramov5761ad12016-10-04 09:00:30 +04003079 """Adds a external port to VIM"""
bayramov325fa1c2016-09-08 01:42:46 -07003080 '''Returns the port identifier'''
bayramovbd6160f2016-09-28 04:12:05 +04003081 raise vimconn.vimconnNotImplemented("Should have implemented this")
bayramov325fa1c2016-09-08 01:42:46 -07003082
bayramovef390722016-09-27 03:34:46 -07003083 def new_external_network(self, net_name, net_type):
bayramov5761ad12016-10-04 09:00:30 +04003084 """Adds a external network to VIM (shared)"""
bayramov325fa1c2016-09-08 01:42:46 -07003085 '''Returns the network identifier'''
bayramovbd6160f2016-09-28 04:12:05 +04003086 raise vimconn.vimconnNotImplemented("Should have implemented this")
bayramov325fa1c2016-09-08 01:42:46 -07003087
3088 def connect_port_network(self, port_id, network_id, admin=False):
bayramov5761ad12016-10-04 09:00:30 +04003089 """Connects a external port to a network"""
bayramov325fa1c2016-09-08 01:42:46 -07003090 '''Returns status code of the VIM response'''
bayramovbd6160f2016-09-28 04:12:05 +04003091 raise vimconn.vimconnNotImplemented("Should have implemented this")
bayramov325fa1c2016-09-08 01:42:46 -07003092
3093 def new_vminstancefromJSON(self, vm_data):
bayramov5761ad12016-10-04 09:00:30 +04003094 """Adds a VM instance to VIM"""
bayramov325fa1c2016-09-08 01:42:46 -07003095 '''Returns the instance identifier'''
bayramovbd6160f2016-09-28 04:12:05 +04003096 raise vimconn.vimconnNotImplemented("Should have implemented this")
bayramov325fa1c2016-09-08 01:42:46 -07003097
kate15f1c382016-12-15 01:12:40 -08003098 def get_network_name_by_id(self, network_uuid=None):
bayramovef390722016-09-27 03:34:46 -07003099 """Method gets vcloud director network named based on supplied uuid.
3100
3101 Args:
kate15f1c382016-12-15 01:12:40 -08003102 network_uuid: network_id
bayramovef390722016-09-27 03:34:46 -07003103
3104 Returns:
3105 The return network name.
3106 """
3107
kate15f1c382016-12-15 01:12:40 -08003108 if not network_uuid:
bayramovef390722016-09-27 03:34:46 -07003109 return None
3110
3111 try:
kate15f1c382016-12-15 01:12:40 -08003112 org_dict = self.get_org(self.org_uuid)
3113 if 'networks' in org_dict:
3114 org_network_dict = org_dict['networks']
3115 for net_uuid in org_network_dict:
3116 if net_uuid == network_uuid:
3117 return org_network_dict[net_uuid]
bayramovef390722016-09-27 03:34:46 -07003118 except:
3119 self.logger.debug("Exception in get_network_name_by_id")
3120 self.logger.debug(traceback.format_exc())
3121
3122 return None
3123
bhangare2c855072016-12-27 01:41:28 -08003124 def get_network_id_by_name(self, network_name=None):
3125 """Method gets vcloud director network uuid based on supplied name.
3126
3127 Args:
3128 network_name: network_name
3129 Returns:
3130 The return network uuid.
3131 network_uuid: network_id
3132 """
3133
bhangare2c855072016-12-27 01:41:28 -08003134 if not network_name:
3135 self.logger.debug("get_network_id_by_name() : Network name is empty")
3136 return None
3137
3138 try:
3139 org_dict = self.get_org(self.org_uuid)
3140 if org_dict and 'networks' in org_dict:
3141 org_network_dict = org_dict['networks']
3142 for net_uuid,net_name in org_network_dict.iteritems():
kasarc5bf2932018-03-09 04:15:22 -08003143 #For python3
3144 #for net_uuid,net_name in org_network_dict.items():
bhangare2c855072016-12-27 01:41:28 -08003145 if net_name == network_name:
3146 return net_uuid
3147
3148 except KeyError as exp:
3149 self.logger.debug("get_network_id_by_name() : KeyError- {} ".format(exp))
3150
3151 return None
3152
bayramovef390722016-09-27 03:34:46 -07003153 def list_org_action(self):
3154 """
3155 Method leverages vCloud director and query for available organization for particular user
3156
3157 Args:
3158 vca - is active VCA connection.
3159 vdc_name - is a vdc name that will be used to query vms action
3160
3161 Returns:
3162 The return XML respond
3163 """
kasarc5bf2932018-03-09 04:15:22 -08003164 url_list = [self.url, '/api/org']
bayramovef390722016-09-27 03:34:46 -07003165 vm_list_rest_call = ''.join(url_list)
3166
kasarc5bf2932018-03-09 04:15:22 -08003167 if self.client._session:
3168 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
3169 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
3170
3171 response = self.perform_request(req_type='GET',
3172 url=vm_list_rest_call,
3173 headers=headers)
bhangare1a0b97c2017-06-21 02:20:15 -07003174
3175 if response.status_code == 403:
3176 response = self.retry_rest('GET', vm_list_rest_call)
3177
bayramovef390722016-09-27 03:34:46 -07003178 if response.status_code == requests.codes.ok:
3179 return response.content
3180
3181 return None
3182
3183 def get_org_action(self, org_uuid=None):
3184 """
kasarc5bf2932018-03-09 04:15:22 -08003185 Method leverages vCloud director and retrieve available object for organization.
bayramovef390722016-09-27 03:34:46 -07003186
3187 Args:
kasarc5bf2932018-03-09 04:15:22 -08003188 org_uuid - vCD organization uuid
3189 self.client - is active connection.
bayramovef390722016-09-27 03:34:46 -07003190
3191 Returns:
3192 The return XML respond
3193 """
3194
bayramovef390722016-09-27 03:34:46 -07003195 if org_uuid is None:
3196 return None
3197
kasarc5bf2932018-03-09 04:15:22 -08003198 url_list = [self.url, '/api/org/', org_uuid]
bayramovef390722016-09-27 03:34:46 -07003199 vm_list_rest_call = ''.join(url_list)
3200
sbhangarea8e5b782018-06-21 02:10:03 -07003201 if self.client._session:
kasarc5bf2932018-03-09 04:15:22 -08003202 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
sbhangarea8e5b782018-06-21 02:10:03 -07003203 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
bhangare1a0b97c2017-06-21 02:20:15 -07003204
kasarc5bf2932018-03-09 04:15:22 -08003205 #response = requests.get(vm_list_rest_call, headers=headers, verify=False)
3206 response = self.perform_request(req_type='GET',
3207 url=vm_list_rest_call,
3208 headers=headers)
bhangare1a0b97c2017-06-21 02:20:15 -07003209 if response.status_code == 403:
3210 response = self.retry_rest('GET', vm_list_rest_call)
3211
bayramovef390722016-09-27 03:34:46 -07003212 if response.status_code == requests.codes.ok:
sbhangarea8e5b782018-06-21 02:10:03 -07003213 return response.content
bayramovef390722016-09-27 03:34:46 -07003214 return None
3215
3216 def get_org(self, org_uuid=None):
3217 """
3218 Method retrieves available organization in vCloud Director
3219
3220 Args:
bayramovb6ffe792016-09-28 11:50:56 +04003221 org_uuid - is a organization uuid.
bayramovef390722016-09-27 03:34:46 -07003222
3223 Returns:
bayramovb6ffe792016-09-28 11:50:56 +04003224 The return dictionary with following key
3225 "network" - for network list under the org
3226 "catalogs" - for network list under the org
3227 "vdcs" - for vdc list under org
bayramovef390722016-09-27 03:34:46 -07003228 """
3229
3230 org_dict = {}
bayramovef390722016-09-27 03:34:46 -07003231
3232 if org_uuid is None:
3233 return org_dict
3234
3235 content = self.get_org_action(org_uuid=org_uuid)
3236 try:
3237 vdc_list = {}
3238 network_list = {}
3239 catalog_list = {}
3240 vm_list_xmlroot = XmlElementTree.fromstring(content)
3241 for child in vm_list_xmlroot:
3242 if child.attrib['type'] == 'application/vnd.vmware.vcloud.vdc+xml':
3243 vdc_list[child.attrib['href'].split("/")[-1:][0]] = child.attrib['name']
3244 org_dict['vdcs'] = vdc_list
3245 if child.attrib['type'] == 'application/vnd.vmware.vcloud.orgNetwork+xml':
3246 network_list[child.attrib['href'].split("/")[-1:][0]] = child.attrib['name']
3247 org_dict['networks'] = network_list
3248 if child.attrib['type'] == 'application/vnd.vmware.vcloud.catalog+xml':
3249 catalog_list[child.attrib['href'].split("/")[-1:][0]] = child.attrib['name']
3250 org_dict['catalogs'] = catalog_list
3251 except:
3252 pass
3253
3254 return org_dict
3255
3256 def get_org_list(self):
3257 """
3258 Method retrieves available organization in vCloud Director
3259
3260 Args:
3261 vca - is active VCA connection.
3262
3263 Returns:
3264 The return dictionary and key for each entry VDC UUID
3265 """
3266
3267 org_dict = {}
bayramovef390722016-09-27 03:34:46 -07003268
3269 content = self.list_org_action()
3270 try:
3271 vm_list_xmlroot = XmlElementTree.fromstring(content)
3272 for vm_xml in vm_list_xmlroot:
3273 if vm_xml.tag.split("}")[1] == 'Org':
3274 org_uuid = vm_xml.attrib['href'].split('/')[-1:]
3275 org_dict[org_uuid[0]] = vm_xml.attrib['name']
3276 except:
3277 pass
3278
3279 return org_dict
3280
3281 def vms_view_action(self, vdc_name=None):
3282 """ Method leverages vCloud director vms query call
3283
3284 Args:
3285 vca - is active VCA connection.
3286 vdc_name - is a vdc name that will be used to query vms action
3287
3288 Returns:
3289 The return XML respond
3290 """
3291 vca = self.connect()
3292 if vdc_name is None:
3293 return None
3294
3295 url_list = [vca.host, '/api/vms/query']
3296 vm_list_rest_call = ''.join(url_list)
3297
3298 if not (not vca.vcloud_session or not vca.vcloud_session.organization):
3299 refs = filter(lambda ref: ref.name == vdc_name and ref.type_ == 'application/vnd.vmware.vcloud.vdc+xml',
3300 vca.vcloud_session.organization.Link)
kasarc5bf2932018-03-09 04:15:22 -08003301 #For python3
3302 #refs = [ref for ref in vca.vcloud_session.organization.Link if ref.name == vdc_name and\
3303 # ref.type_ == 'application/vnd.vmware.vcloud.vdc+xml']
bayramovef390722016-09-27 03:34:46 -07003304 if len(refs) == 1:
3305 response = Http.get(url=vm_list_rest_call,
3306 headers=vca.vcloud_session.get_vcloud_headers(),
3307 verify=vca.verify,
3308 logger=vca.logger)
3309 if response.status_code == requests.codes.ok:
3310 return response.content
3311
3312 return None
3313
3314 def get_vapp_list(self, vdc_name=None):
3315 """
3316 Method retrieves vApp list deployed vCloud director and returns a dictionary
3317 contains a list of all vapp deployed for queried VDC.
3318 The key for a dictionary is vApp UUID
3319
3320
3321 Args:
3322 vca - is active VCA connection.
3323 vdc_name - is a vdc name that will be used to query vms action
3324
3325 Returns:
3326 The return dictionary and key for each entry vapp UUID
3327 """
3328
3329 vapp_dict = {}
3330 if vdc_name is None:
3331 return vapp_dict
3332
3333 content = self.vms_view_action(vdc_name=vdc_name)
3334 try:
3335 vm_list_xmlroot = XmlElementTree.fromstring(content)
3336 for vm_xml in vm_list_xmlroot:
3337 if vm_xml.tag.split("}")[1] == 'VMRecord':
3338 if vm_xml.attrib['isVAppTemplate'] == 'true':
3339 rawuuid = vm_xml.attrib['container'].split('/')[-1:]
3340 if 'vappTemplate-' in rawuuid[0]:
3341 # vm in format vappTemplate-e63d40e7-4ff5-4c6d-851f-96c1e4da86a5 we remove
3342 # vm and use raw UUID as key
3343 vapp_dict[rawuuid[0][13:]] = vm_xml.attrib
3344 except:
3345 pass
3346
3347 return vapp_dict
3348
3349 def get_vm_list(self, vdc_name=None):
3350 """
3351 Method retrieves VM's list deployed vCloud director. It returns a dictionary
3352 contains a list of all VM's deployed for queried VDC.
3353 The key for a dictionary is VM UUID
3354
3355
3356 Args:
3357 vca - is active VCA connection.
3358 vdc_name - is a vdc name that will be used to query vms action
3359
3360 Returns:
3361 The return dictionary and key for each entry vapp UUID
3362 """
3363 vm_dict = {}
3364
3365 if vdc_name is None:
3366 return vm_dict
3367
3368 content = self.vms_view_action(vdc_name=vdc_name)
3369 try:
3370 vm_list_xmlroot = XmlElementTree.fromstring(content)
3371 for vm_xml in vm_list_xmlroot:
3372 if vm_xml.tag.split("}")[1] == 'VMRecord':
3373 if vm_xml.attrib['isVAppTemplate'] == 'false':
3374 rawuuid = vm_xml.attrib['href'].split('/')[-1:]
3375 if 'vm-' in rawuuid[0]:
3376 # vm in format vm-e63d40e7-4ff5-4c6d-851f-96c1e4da86a5 we remove
3377 # vm and use raw UUID as key
3378 vm_dict[rawuuid[0][3:]] = vm_xml.attrib
3379 except:
3380 pass
3381
3382 return vm_dict
3383
3384 def get_vapp(self, vdc_name=None, vapp_name=None, isuuid=False):
3385 """
bayramovb6ffe792016-09-28 11:50:56 +04003386 Method retrieves VM deployed vCloud director. It returns VM attribute as dictionary
bayramovef390722016-09-27 03:34:46 -07003387 contains a list of all VM's deployed for queried VDC.
3388 The key for a dictionary is VM UUID
3389
3390
3391 Args:
3392 vca - is active VCA connection.
3393 vdc_name - is a vdc name that will be used to query vms action
3394
3395 Returns:
3396 The return dictionary and key for each entry vapp UUID
3397 """
3398 vm_dict = {}
3399 vca = self.connect()
3400 if not vca:
3401 raise vimconn.vimconnConnectionException("self.connect() is failed")
3402
3403 if vdc_name is None:
3404 return vm_dict
3405
3406 content = self.vms_view_action(vdc_name=vdc_name)
3407 try:
3408 vm_list_xmlroot = XmlElementTree.fromstring(content)
3409 for vm_xml in vm_list_xmlroot:
bayramovb6ffe792016-09-28 11:50:56 +04003410 if vm_xml.tag.split("}")[1] == 'VMRecord' and vm_xml.attrib['isVAppTemplate'] == 'false':
3411 # lookup done by UUID
bayramovef390722016-09-27 03:34:46 -07003412 if isuuid:
bayramovef390722016-09-27 03:34:46 -07003413 if vapp_name in vm_xml.attrib['container']:
3414 rawuuid = vm_xml.attrib['href'].split('/')[-1:]
3415 if 'vm-' in rawuuid[0]:
bayramovef390722016-09-27 03:34:46 -07003416 vm_dict[rawuuid[0][3:]] = vm_xml.attrib
bayramovb6ffe792016-09-28 11:50:56 +04003417 break
3418 # lookup done by Name
3419 else:
3420 if vapp_name in vm_xml.attrib['name']:
3421 rawuuid = vm_xml.attrib['href'].split('/')[-1:]
3422 if 'vm-' in rawuuid[0]:
3423 vm_dict[rawuuid[0][3:]] = vm_xml.attrib
3424 break
bayramovef390722016-09-27 03:34:46 -07003425 except:
3426 pass
3427
3428 return vm_dict
3429
3430 def get_network_action(self, network_uuid=None):
3431 """
3432 Method leverages vCloud director and query network based on network uuid
3433
3434 Args:
3435 vca - is active VCA connection.
3436 network_uuid - is a network uuid
3437
3438 Returns:
3439 The return XML respond
3440 """
3441
bayramovef390722016-09-27 03:34:46 -07003442 if network_uuid is None:
3443 return None
3444
kasarc5bf2932018-03-09 04:15:22 -08003445 url_list = [self.url, '/api/network/', network_uuid]
bayramovef390722016-09-27 03:34:46 -07003446 vm_list_rest_call = ''.join(url_list)
3447
kasarc5bf2932018-03-09 04:15:22 -08003448 if self.client._session:
3449 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
3450 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
bhangare1a0b97c2017-06-21 02:20:15 -07003451
kasarc5bf2932018-03-09 04:15:22 -08003452 response = self.perform_request(req_type='GET',
3453 url=vm_list_rest_call,
3454 headers=headers)
bhangare1a0b97c2017-06-21 02:20:15 -07003455 #Retry login if session expired & retry sending request
3456 if response.status_code == 403:
3457 response = self.retry_rest('GET', vm_list_rest_call)
3458
bayramovef390722016-09-27 03:34:46 -07003459 if response.status_code == requests.codes.ok:
3460 return response.content
3461
3462 return None
3463
3464 def get_vcd_network(self, network_uuid=None):
3465 """
3466 Method retrieves available network from vCloud Director
3467
3468 Args:
3469 network_uuid - is VCD network UUID
3470
3471 Each element serialized as key : value pair
3472
3473 Following keys available for access. network_configuration['Gateway'}
3474 <Configuration>
3475 <IpScopes>
3476 <IpScope>
3477 <IsInherited>true</IsInherited>
3478 <Gateway>172.16.252.100</Gateway>
3479 <Netmask>255.255.255.0</Netmask>
3480 <Dns1>172.16.254.201</Dns1>
3481 <Dns2>172.16.254.202</Dns2>
3482 <DnsSuffix>vmwarelab.edu</DnsSuffix>
3483 <IsEnabled>true</IsEnabled>
3484 <IpRanges>
3485 <IpRange>
3486 <StartAddress>172.16.252.1</StartAddress>
3487 <EndAddress>172.16.252.99</EndAddress>
3488 </IpRange>
3489 </IpRanges>
3490 </IpScope>
3491 </IpScopes>
3492 <FenceMode>bridged</FenceMode>
3493
3494 Returns:
3495 The return dictionary and key for each entry vapp UUID
3496 """
3497
3498 network_configuration = {}
3499 if network_uuid is None:
3500 return network_uuid
3501
bayramovef390722016-09-27 03:34:46 -07003502 try:
bhangarebfdca492017-03-11 01:32:46 -08003503 content = self.get_network_action(network_uuid=network_uuid)
bayramovef390722016-09-27 03:34:46 -07003504 vm_list_xmlroot = XmlElementTree.fromstring(content)
3505
3506 network_configuration['status'] = vm_list_xmlroot.get("status")
3507 network_configuration['name'] = vm_list_xmlroot.get("name")
3508 network_configuration['uuid'] = vm_list_xmlroot.get("id").split(":")[3]
3509
3510 for child in vm_list_xmlroot:
3511 if child.tag.split("}")[1] == 'IsShared':
3512 network_configuration['isShared'] = child.text.strip()
3513 if child.tag.split("}")[1] == 'Configuration':
3514 for configuration in child.iter():
3515 tagKey = configuration.tag.split("}")[1].strip()
3516 if tagKey != "":
3517 network_configuration[tagKey] = configuration.text.strip()
3518 return network_configuration
bhangarebfdca492017-03-11 01:32:46 -08003519 except Exception as exp :
3520 self.logger.debug("get_vcd_network: Failed with Exception {}".format(exp))
3521 raise vimconn.vimconnException("get_vcd_network: Failed with Exception {}".format(exp))
bayramovef390722016-09-27 03:34:46 -07003522
3523 return network_configuration
3524
3525 def delete_network_action(self, network_uuid=None):
3526 """
3527 Method delete given network from vCloud director
3528
3529 Args:
3530 network_uuid - is a network uuid that client wish to delete
3531
3532 Returns:
3533 The return None or XML respond or false
3534 """
kasarc5bf2932018-03-09 04:15:22 -08003535 client = self.connect_as_admin()
3536 if not client:
3537 raise vimconn.vimconnConnectionException("Failed to connect vCD as admin")
bayramovef390722016-09-27 03:34:46 -07003538 if network_uuid is None:
3539 return False
3540
kasarc5bf2932018-03-09 04:15:22 -08003541 url_list = [self.url, '/api/admin/network/', network_uuid]
bayramovef390722016-09-27 03:34:46 -07003542 vm_list_rest_call = ''.join(url_list)
3543
kasarc5bf2932018-03-09 04:15:22 -08003544 if client._session:
3545 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
sbhangarea8e5b782018-06-21 02:10:03 -07003546 'x-vcloud-authorization': client._session.headers['x-vcloud-authorization']}
kasarc5bf2932018-03-09 04:15:22 -08003547 response = self.perform_request(req_type='DELETE',
3548 url=vm_list_rest_call,
3549 headers=headers)
bayramovef390722016-09-27 03:34:46 -07003550 if response.status_code == 202:
3551 return True
3552
3553 return False
3554
bhangare0e571a92017-01-12 04:02:23 -08003555 def create_network(self, network_name=None, net_type='bridge', parent_network_uuid=None,
3556 ip_profile=None, isshared='true'):
bayramovef390722016-09-27 03:34:46 -07003557 """
3558 Method create network in vCloud director
3559
3560 Args:
3561 network_name - is network name to be created.
bhangare0e571a92017-01-12 04:02:23 -08003562 net_type - can be 'bridge','data','ptp','mgmt'.
3563 ip_profile is a dict containing the IP parameters of the network
3564 isshared - is a boolean
bayramovef390722016-09-27 03:34:46 -07003565 parent_network_uuid - is parent provider vdc network that will be used for mapping.
3566 It optional attribute. by default if no parent network indicate the first available will be used.
3567
3568 Returns:
3569 The return network uuid or return None
3570 """
3571
bayramov5761ad12016-10-04 09:00:30 +04003572 new_network_name = [network_name, '-', str(uuid.uuid4())]
3573 content = self.create_network_rest(network_name=''.join(new_network_name),
bhangare0e571a92017-01-12 04:02:23 -08003574 ip_profile=ip_profile,
3575 net_type=net_type,
bayramovef390722016-09-27 03:34:46 -07003576 parent_network_uuid=parent_network_uuid,
3577 isshared=isshared)
3578 if content is None:
3579 self.logger.debug("Failed create network {}.".format(network_name))
3580 return None
3581
3582 try:
3583 vm_list_xmlroot = XmlElementTree.fromstring(content)
3584 vcd_uuid = vm_list_xmlroot.get('id').split(":")
3585 if len(vcd_uuid) == 4:
bhangarebfdca492017-03-11 01:32:46 -08003586 self.logger.info("Created new network name: {} uuid: {}".format(network_name, vcd_uuid[3]))
bayramovef390722016-09-27 03:34:46 -07003587 return vcd_uuid[3]
3588 except:
3589 self.logger.debug("Failed create network {}".format(network_name))
3590 return None
3591
bhangare0e571a92017-01-12 04:02:23 -08003592 def create_network_rest(self, network_name=None, net_type='bridge', parent_network_uuid=None,
3593 ip_profile=None, isshared='true'):
bayramovef390722016-09-27 03:34:46 -07003594 """
3595 Method create network in vCloud director
3596
3597 Args:
3598 network_name - is network name to be created.
bhangare0e571a92017-01-12 04:02:23 -08003599 net_type - can be 'bridge','data','ptp','mgmt'.
3600 ip_profile is a dict containing the IP parameters of the network
3601 isshared - is a boolean
bayramovef390722016-09-27 03:34:46 -07003602 parent_network_uuid - is parent provider vdc network that will be used for mapping.
3603 It optional attribute. by default if no parent network indicate the first available will be used.
3604
3605 Returns:
3606 The return network uuid or return None
3607 """
kasarc5bf2932018-03-09 04:15:22 -08003608 client_as_admin = self.connect_as_admin()
3609 if not client_as_admin:
3610 raise vimconn.vimconnConnectionException("Failed to connect vCD.")
bayramovef390722016-09-27 03:34:46 -07003611 if network_name is None:
3612 return None
3613
kasarc5bf2932018-03-09 04:15:22 -08003614 url_list = [self.url, '/api/admin/vdc/', self.tenant_id]
bayramovef390722016-09-27 03:34:46 -07003615 vm_list_rest_call = ''.join(url_list)
kasarc5bf2932018-03-09 04:15:22 -08003616
3617 if client_as_admin._session:
3618 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
3619 'x-vcloud-authorization': client_as_admin._session.headers['x-vcloud-authorization']}
3620
3621 response = self.perform_request(req_type='GET',
3622 url=vm_list_rest_call,
3623 headers=headers)
bayramovef390722016-09-27 03:34:46 -07003624
3625 provider_network = None
3626 available_networks = None
3627 add_vdc_rest_url = None
3628
3629 if response.status_code != requests.codes.ok:
3630 self.logger.debug("REST API call {} failed. Return status code {}".format(vm_list_rest_call,
3631 response.status_code))
3632 return None
3633 else:
3634 try:
3635 vm_list_xmlroot = XmlElementTree.fromstring(response.content)
3636 for child in vm_list_xmlroot:
3637 if child.tag.split("}")[1] == 'ProviderVdcReference':
3638 provider_network = child.attrib.get('href')
3639 # application/vnd.vmware.admin.providervdc+xml
3640 if child.tag.split("}")[1] == 'Link':
3641 if child.attrib.get('type') == 'application/vnd.vmware.vcloud.orgVdcNetwork+xml' \
3642 and child.attrib.get('rel') == 'add':
3643 add_vdc_rest_url = child.attrib.get('href')
3644 except:
3645 self.logger.debug("Failed parse respond for rest api call {}".format(vm_list_rest_call))
3646 self.logger.debug("Respond body {}".format(response.content))
3647 return None
3648
3649 # find pvdc provided available network
kasarc5bf2932018-03-09 04:15:22 -08003650 response = self.perform_request(req_type='GET',
3651 url=provider_network,
3652 headers=headers)
bayramovef390722016-09-27 03:34:46 -07003653 if response.status_code != requests.codes.ok:
3654 self.logger.debug("REST API call {} failed. Return status code {}".format(vm_list_rest_call,
3655 response.status_code))
3656 return None
3657
bayramovef390722016-09-27 03:34:46 -07003658 if parent_network_uuid is None:
3659 try:
3660 vm_list_xmlroot = XmlElementTree.fromstring(response.content)
3661 for child in vm_list_xmlroot.iter():
3662 if child.tag.split("}")[1] == 'AvailableNetworks':
3663 for networks in child.iter():
3664 # application/vnd.vmware.admin.network+xml
3665 if networks.attrib.get('href') is not None:
3666 available_networks = networks.attrib.get('href')
3667 break
3668 except:
3669 return None
3670
bhangarebfdca492017-03-11 01:32:46 -08003671 try:
3672 #Configure IP profile of the network
3673 ip_profile = ip_profile if ip_profile is not None else DEFAULT_IP_PROFILE
bhangare0e571a92017-01-12 04:02:23 -08003674
kasarde691232017-03-25 03:37:31 -07003675 if 'subnet_address' not in ip_profile or ip_profile['subnet_address'] is None:
3676 subnet_rand = random.randint(0, 255)
3677 ip_base = "192.168.{}.".format(subnet_rand)
3678 ip_profile['subnet_address'] = ip_base + "0/24"
3679 else:
3680 ip_base = ip_profile['subnet_address'].rsplit('.',1)[0] + '.'
3681
bhangarebfdca492017-03-11 01:32:46 -08003682 if 'gateway_address' not in ip_profile or ip_profile['gateway_address'] is None:
kasarde691232017-03-25 03:37:31 -07003683 ip_profile['gateway_address']=ip_base + "1"
bhangarebfdca492017-03-11 01:32:46 -08003684 if 'dhcp_count' not in ip_profile or ip_profile['dhcp_count'] is None:
3685 ip_profile['dhcp_count']=DEFAULT_IP_PROFILE['dhcp_count']
bhangarebfdca492017-03-11 01:32:46 -08003686 if 'dhcp_enabled' not in ip_profile or ip_profile['dhcp_enabled'] is None:
3687 ip_profile['dhcp_enabled']=DEFAULT_IP_PROFILE['dhcp_enabled']
3688 if 'dhcp_start_address' not in ip_profile or ip_profile['dhcp_start_address'] is None:
kasarde691232017-03-25 03:37:31 -07003689 ip_profile['dhcp_start_address']=ip_base + "3"
bhangarebfdca492017-03-11 01:32:46 -08003690 if 'ip_version' not in ip_profile or ip_profile['ip_version'] is None:
3691 ip_profile['ip_version']=DEFAULT_IP_PROFILE['ip_version']
3692 if 'dns_address' not in ip_profile or ip_profile['dns_address'] is None:
kasarde691232017-03-25 03:37:31 -07003693 ip_profile['dns_address']=ip_base + "2"
bhangare0e571a92017-01-12 04:02:23 -08003694
bhangarebfdca492017-03-11 01:32:46 -08003695 gateway_address=ip_profile['gateway_address']
3696 dhcp_count=int(ip_profile['dhcp_count'])
3697 subnet_address=self.convert_cidr_to_netmask(ip_profile['subnet_address'])
bhangare0e571a92017-01-12 04:02:23 -08003698
bhangarebfdca492017-03-11 01:32:46 -08003699 if ip_profile['dhcp_enabled']==True:
3700 dhcp_enabled='true'
3701 else:
3702 dhcp_enabled='false'
3703 dhcp_start_address=ip_profile['dhcp_start_address']
bhangare0e571a92017-01-12 04:02:23 -08003704
bhangarebfdca492017-03-11 01:32:46 -08003705 #derive dhcp_end_address from dhcp_start_address & dhcp_count
3706 end_ip_int = int(netaddr.IPAddress(dhcp_start_address))
3707 end_ip_int += dhcp_count - 1
3708 dhcp_end_address = str(netaddr.IPAddress(end_ip_int))
3709
3710 ip_version=ip_profile['ip_version']
3711 dns_address=ip_profile['dns_address']
3712 except KeyError as exp:
3713 self.logger.debug("Create Network REST: Key error {}".format(exp))
3714 raise vimconn.vimconnException("Create Network REST: Key error{}".format(exp))
bhangare0e571a92017-01-12 04:02:23 -08003715
bayramovef390722016-09-27 03:34:46 -07003716 # either use client provided UUID or search for a first available
3717 # if both are not defined we return none
3718 if parent_network_uuid is not None:
Ravi Chamartyb0ab2c02018-10-26 01:40:50 +00003719 provider_network = None
3720 available_networks = None
3721 add_vdc_rest_url = None
3722
3723 url_list = [self.url, '/api/admin/vdc/', self.tenant_id, '/networks']
bayramovef390722016-09-27 03:34:46 -07003724 add_vdc_rest_url = ''.join(url_list)
3725
Ravi Chamartyb0ab2c02018-10-26 01:40:50 +00003726 url_list = [self.url, '/api/admin/network/', parent_network_uuid]
3727 available_networks = ''.join(url_list)
3728
bhangare06312472017-03-30 05:49:07 -07003729 #Creating all networks as Direct Org VDC type networks.
3730 #Unused in case of Underlay (data/ptp) network interface.
3731 fence_mode="bridged"
3732 is_inherited='false'
tierno455612d2017-05-30 16:40:10 +02003733 dns_list = dns_address.split(";")
3734 dns1 = dns_list[0]
3735 dns2_text = ""
3736 if len(dns_list) >= 2:
3737 dns2_text = "\n <Dns2>{}</Dns2>\n".format(dns_list[1])
bhangare06312472017-03-30 05:49:07 -07003738 data = """ <OrgVdcNetwork name="{0:s}" xmlns="http://www.vmware.com/vcloud/v1.5">
3739 <Description>Openmano created</Description>
3740 <Configuration>
3741 <IpScopes>
3742 <IpScope>
3743 <IsInherited>{1:s}</IsInherited>
3744 <Gateway>{2:s}</Gateway>
3745 <Netmask>{3:s}</Netmask>
tierno455612d2017-05-30 16:40:10 +02003746 <Dns1>{4:s}</Dns1>{5:s}
3747 <IsEnabled>{6:s}</IsEnabled>
bhangare06312472017-03-30 05:49:07 -07003748 <IpRanges>
3749 <IpRange>
tierno455612d2017-05-30 16:40:10 +02003750 <StartAddress>{7:s}</StartAddress>
3751 <EndAddress>{8:s}</EndAddress>
bhangare06312472017-03-30 05:49:07 -07003752 </IpRange>
3753 </IpRanges>
3754 </IpScope>
3755 </IpScopes>
tierno455612d2017-05-30 16:40:10 +02003756 <ParentNetwork href="{9:s}"/>
3757 <FenceMode>{10:s}</FenceMode>
bhangare06312472017-03-30 05:49:07 -07003758 </Configuration>
tierno455612d2017-05-30 16:40:10 +02003759 <IsShared>{11:s}</IsShared>
bhangare06312472017-03-30 05:49:07 -07003760 </OrgVdcNetwork> """.format(escape(network_name), is_inherited, gateway_address,
tierno455612d2017-05-30 16:40:10 +02003761 subnet_address, dns1, dns2_text, dhcp_enabled,
bhangare06312472017-03-30 05:49:07 -07003762 dhcp_start_address, dhcp_end_address, available_networks,
3763 fence_mode, isshared)
bayramovef390722016-09-27 03:34:46 -07003764
bayramovef390722016-09-27 03:34:46 -07003765 headers['Content-Type'] = 'application/vnd.vmware.vcloud.orgVdcNetwork+xml'
bhangare0e571a92017-01-12 04:02:23 -08003766 try:
kasarc5bf2932018-03-09 04:15:22 -08003767 response = self.perform_request(req_type='POST',
3768 url=add_vdc_rest_url,
3769 headers=headers,
3770 data=data)
bayramovef390722016-09-27 03:34:46 -07003771
bhangare0e571a92017-01-12 04:02:23 -08003772 if response.status_code != 201:
bhangarebfdca492017-03-11 01:32:46 -08003773 self.logger.debug("Create Network POST REST API call failed. Return status code {}, Response content: {}"
3774 .format(response.status_code,response.content))
bhangare0e571a92017-01-12 04:02:23 -08003775 else:
kasarc5bf2932018-03-09 04:15:22 -08003776 network_task = self.get_task_from_response(response.content)
3777 self.logger.debug("Create Network REST : Waiting for Network creation complete")
3778 time.sleep(5)
3779 result = self.client.get_task_monitor().wait_for_success(task=network_task)
sbhangarea8e5b782018-06-21 02:10:03 -07003780 if result.get('status') == 'success':
kasarc5bf2932018-03-09 04:15:22 -08003781 return response.content
3782 else:
3783 self.logger.debug("create_network_rest task failed. Network Create response : {}"
3784 .format(response.content))
bhangare0e571a92017-01-12 04:02:23 -08003785 except Exception as exp:
3786 self.logger.debug("create_network_rest : Exception : {} ".format(exp))
3787
3788 return None
3789
3790 def convert_cidr_to_netmask(self, cidr_ip=None):
3791 """
3792 Method sets convert CIDR netmask address to normal IP format
3793 Args:
3794 cidr_ip : CIDR IP address
3795 Returns:
3796 netmask : Converted netmask
3797 """
3798 if cidr_ip is not None:
3799 if '/' in cidr_ip:
3800 network, net_bits = cidr_ip.split('/')
3801 netmask = socket.inet_ntoa(struct.pack(">I", (0xffffffff << (32 - int(net_bits))) & 0xffffffff))
3802 else:
3803 netmask = cidr_ip
3804 return netmask
bayramovef390722016-09-27 03:34:46 -07003805 return None
3806
3807 def get_provider_rest(self, vca=None):
3808 """
3809 Method gets provider vdc view from vcloud director
3810
3811 Args:
3812 network_name - is network name to be created.
3813 parent_network_uuid - is parent provider vdc network that will be used for mapping.
3814 It optional attribute. by default if no parent network indicate the first available will be used.
3815
3816 Returns:
3817 The return xml content of respond or None
3818 """
3819
kasarc5bf2932018-03-09 04:15:22 -08003820 url_list = [self.url, '/api/admin']
3821 if vca:
3822 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
3823 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
3824 response = self.perform_request(req_type='GET',
3825 url=''.join(url_list),
3826 headers=headers)
bayramovef390722016-09-27 03:34:46 -07003827
3828 if response.status_code == requests.codes.ok:
3829 return response.content
3830 return None
3831
3832 def create_vdc(self, vdc_name=None):
3833
3834 vdc_dict = {}
3835
3836 xml_content = self.create_vdc_from_tmpl_rest(vdc_name=vdc_name)
3837 if xml_content is not None:
bayramovef390722016-09-27 03:34:46 -07003838 try:
3839 task_resp_xmlroot = XmlElementTree.fromstring(xml_content)
3840 for child in task_resp_xmlroot:
3841 if child.tag.split("}")[1] == 'Owner':
3842 vdc_id = child.attrib.get('href').split("/")[-1]
3843 vdc_dict[vdc_id] = task_resp_xmlroot.get('href')
3844 return vdc_dict
3845 except:
3846 self.logger.debug("Respond body {}".format(xml_content))
3847
3848 return None
3849
3850 def create_vdc_from_tmpl_rest(self, vdc_name=None):
3851 """
3852 Method create vdc in vCloud director based on VDC template.
kasarc5bf2932018-03-09 04:15:22 -08003853 it uses pre-defined template.
bayramovef390722016-09-27 03:34:46 -07003854
3855 Args:
3856 vdc_name - name of a new vdc.
3857
3858 Returns:
3859 The return xml content of respond or None
3860 """
kasarc5bf2932018-03-09 04:15:22 -08003861 # pre-requesite atleast one vdc template should be available in vCD
bayramovef390722016-09-27 03:34:46 -07003862 self.logger.info("Creating new vdc {}".format(vdc_name))
kasarc5bf2932018-03-09 04:15:22 -08003863 vca = self.connect_as_admin()
bayramovef390722016-09-27 03:34:46 -07003864 if not vca:
kasarc5bf2932018-03-09 04:15:22 -08003865 raise vimconn.vimconnConnectionException("Failed to connect vCD")
bayramovef390722016-09-27 03:34:46 -07003866 if vdc_name is None:
3867 return None
3868
kasarc5bf2932018-03-09 04:15:22 -08003869 url_list = [self.url, '/api/vdcTemplates']
bayramovef390722016-09-27 03:34:46 -07003870 vm_list_rest_call = ''.join(url_list)
kasarc5bf2932018-03-09 04:15:22 -08003871
3872 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
3873 'x-vcloud-authorization': vca._session.headers['x-vcloud-authorization']}
3874 response = self.perform_request(req_type='GET',
3875 url=vm_list_rest_call,
3876 headers=headers)
bayramovef390722016-09-27 03:34:46 -07003877
3878 # container url to a template
3879 vdc_template_ref = None
3880 try:
3881 vm_list_xmlroot = XmlElementTree.fromstring(response.content)
3882 for child in vm_list_xmlroot:
3883 # application/vnd.vmware.admin.providervdc+xml
3884 # we need find a template from witch we instantiate VDC
3885 if child.tag.split("}")[1] == 'VdcTemplate':
kated47ad5f2017-08-03 02:16:13 -07003886 if child.attrib.get('type') == 'application/vnd.vmware.admin.vdcTemplate+xml':
bayramovef390722016-09-27 03:34:46 -07003887 vdc_template_ref = child.attrib.get('href')
3888 except:
3889 self.logger.debug("Failed parse respond for rest api call {}".format(vm_list_rest_call))
3890 self.logger.debug("Respond body {}".format(response.content))
3891 return None
3892
3893 # if we didn't found required pre defined template we return None
3894 if vdc_template_ref is None:
3895 return None
3896
3897 try:
3898 # instantiate vdc
kasarc5bf2932018-03-09 04:15:22 -08003899 url_list = [self.url, '/api/org/', self.org_uuid, '/action/instantiate']
bayramovef390722016-09-27 03:34:46 -07003900 vm_list_rest_call = ''.join(url_list)
3901 data = """<InstantiateVdcTemplateParams name="{0:s}" xmlns="http://www.vmware.com/vcloud/v1.5">
3902 <Source href="{1:s}"></Source>
3903 <Description>opnemano</Description>
3904 </InstantiateVdcTemplateParams>""".format(vdc_name, vdc_template_ref)
kated47ad5f2017-08-03 02:16:13 -07003905
kasarc5bf2932018-03-09 04:15:22 -08003906 headers['Content-Type'] = 'application/vnd.vmware.vcloud.instantiateVdcTemplateParams+xml'
3907
3908 response = self.perform_request(req_type='POST',
3909 url=vm_list_rest_call,
3910 headers=headers,
3911 data=data)
3912
3913 vdc_task = self.get_task_from_response(response.content)
3914 self.client.get_task_monitor().wait_for_success(task=vdc_task)
kated47ad5f2017-08-03 02:16:13 -07003915
bayramovef390722016-09-27 03:34:46 -07003916 # if we all ok we respond with content otherwise by default None
3917 if response.status_code >= 200 and response.status_code < 300:
3918 return response.content
3919 return None
3920 except:
3921 self.logger.debug("Failed parse respond for rest api call {}".format(vm_list_rest_call))
3922 self.logger.debug("Respond body {}".format(response.content))
3923
3924 return None
3925
3926 def create_vdc_rest(self, vdc_name=None):
3927 """
3928 Method create network in vCloud director
3929
3930 Args:
kasarc5bf2932018-03-09 04:15:22 -08003931 vdc_name - vdc name to be created
bayramovef390722016-09-27 03:34:46 -07003932 Returns:
kasarc5bf2932018-03-09 04:15:22 -08003933 The return response
bayramovef390722016-09-27 03:34:46 -07003934 """
3935
3936 self.logger.info("Creating new vdc {}".format(vdc_name))
bayramovef390722016-09-27 03:34:46 -07003937
3938 vca = self.connect_as_admin()
3939 if not vca:
kasarc5bf2932018-03-09 04:15:22 -08003940 raise vimconn.vimconnConnectionException("Failed to connect vCD")
bayramovef390722016-09-27 03:34:46 -07003941 if vdc_name is None:
3942 return None
3943
kasarc5bf2932018-03-09 04:15:22 -08003944 url_list = [self.url, '/api/admin/org/', self.org_uuid]
bayramovef390722016-09-27 03:34:46 -07003945 vm_list_rest_call = ''.join(url_list)
kasarc5bf2932018-03-09 04:15:22 -08003946
3947 if vca._session:
3948 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
3949 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
3950 response = self.perform_request(req_type='GET',
3951 url=vm_list_rest_call,
3952 headers=headers)
bayramovef390722016-09-27 03:34:46 -07003953
3954 provider_vdc_ref = None
3955 add_vdc_rest_url = None
3956 available_networks = None
3957
3958 if response.status_code != requests.codes.ok:
3959 self.logger.debug("REST API call {} failed. Return status code {}".format(vm_list_rest_call,
3960 response.status_code))
3961 return None
3962 else:
3963 try:
3964 vm_list_xmlroot = XmlElementTree.fromstring(response.content)
3965 for child in vm_list_xmlroot:
3966 # application/vnd.vmware.admin.providervdc+xml
3967 if child.tag.split("}")[1] == 'Link':
3968 if child.attrib.get('type') == 'application/vnd.vmware.admin.createVdcParams+xml' \
3969 and child.attrib.get('rel') == 'add':
3970 add_vdc_rest_url = child.attrib.get('href')
3971 except:
3972 self.logger.debug("Failed parse respond for rest api call {}".format(vm_list_rest_call))
3973 self.logger.debug("Respond body {}".format(response.content))
3974 return None
3975
3976 response = self.get_provider_rest(vca=vca)
bayramovef390722016-09-27 03:34:46 -07003977 try:
3978 vm_list_xmlroot = XmlElementTree.fromstring(response)
3979 for child in vm_list_xmlroot:
3980 if child.tag.split("}")[1] == 'ProviderVdcReferences':
3981 for sub_child in child:
3982 provider_vdc_ref = sub_child.attrib.get('href')
3983 except:
3984 self.logger.debug("Failed parse respond for rest api call {}".format(vm_list_rest_call))
3985 self.logger.debug("Respond body {}".format(response))
3986 return None
3987
bayramovef390722016-09-27 03:34:46 -07003988 if add_vdc_rest_url is not None and provider_vdc_ref is not None:
3989 data = """ <CreateVdcParams name="{0:s}" xmlns="http://www.vmware.com/vcloud/v1.5"><Description>{1:s}</Description>
3990 <AllocationModel>ReservationPool</AllocationModel>
3991 <ComputeCapacity><Cpu><Units>MHz</Units><Allocated>2048</Allocated><Limit>2048</Limit></Cpu>
3992 <Memory><Units>MB</Units><Allocated>2048</Allocated><Limit>2048</Limit></Memory>
3993 </ComputeCapacity><NicQuota>0</NicQuota><NetworkQuota>100</NetworkQuota>
3994 <VdcStorageProfile><Enabled>true</Enabled><Units>MB</Units><Limit>20480</Limit><Default>true</Default></VdcStorageProfile>
3995 <ProviderVdcReference
3996 name="Main Provider"
3997 href="{2:s}" />
3998 <UsesFastProvisioning>true</UsesFastProvisioning></CreateVdcParams>""".format(escape(vdc_name),
3999 escape(vdc_name),
4000 provider_vdc_ref)
4001
bayramovef390722016-09-27 03:34:46 -07004002 headers['Content-Type'] = 'application/vnd.vmware.admin.createVdcParams+xml'
kasarc5bf2932018-03-09 04:15:22 -08004003
4004 response = self.perform_request(req_type='POST',
4005 url=add_vdc_rest_url,
4006 headers=headers,
4007 data=data)
bayramovef390722016-09-27 03:34:46 -07004008
bayramovef390722016-09-27 03:34:46 -07004009 # if we all ok we respond with content otherwise by default None
4010 if response.status_code == 201:
4011 return response.content
4012 return None
bayramovfe3f3c92016-10-04 07:53:41 +04004013
bhangarefda5f7c2017-01-12 23:50:34 -08004014 def get_vapp_details_rest(self, vapp_uuid=None, need_admin_access=False):
bayramovfe3f3c92016-10-04 07:53:41 +04004015 """
4016 Method retrieve vapp detail from vCloud director
4017
4018 Args:
4019 vapp_uuid - is vapp identifier.
4020
4021 Returns:
4022 The return network uuid or return None
4023 """
4024
4025 parsed_respond = {}
bhangarefda5f7c2017-01-12 23:50:34 -08004026 vca = None
bayramovfe3f3c92016-10-04 07:53:41 +04004027
bhangarefda5f7c2017-01-12 23:50:34 -08004028 if need_admin_access:
4029 vca = self.connect_as_admin()
4030 else:
sbhangarea8e5b782018-06-21 02:10:03 -07004031 vca = self.client
bhangarefda5f7c2017-01-12 23:50:34 -08004032
bayramovfe3f3c92016-10-04 07:53:41 +04004033 if not vca:
kasarc5bf2932018-03-09 04:15:22 -08004034 raise vimconn.vimconnConnectionException("Failed to connect vCD")
bayramovfe3f3c92016-10-04 07:53:41 +04004035 if vapp_uuid is None:
4036 return None
4037
kasarc5bf2932018-03-09 04:15:22 -08004038 url_list = [self.url, '/api/vApp/vapp-', vapp_uuid]
bayramovfe3f3c92016-10-04 07:53:41 +04004039 get_vapp_restcall = ''.join(url_list)
bhangarefda5f7c2017-01-12 23:50:34 -08004040
kasarc5bf2932018-03-09 04:15:22 -08004041 if vca._session:
4042 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
sbhangarea8e5b782018-06-21 02:10:03 -07004043 'x-vcloud-authorization': vca._session.headers['x-vcloud-authorization']}
kasarc5bf2932018-03-09 04:15:22 -08004044 response = self.perform_request(req_type='GET',
4045 url=get_vapp_restcall,
4046 headers=headers)
bayramovfe3f3c92016-10-04 07:53:41 +04004047
bhangare1a0b97c2017-06-21 02:20:15 -07004048 if response.status_code == 403:
4049 if need_admin_access == False:
4050 response = self.retry_rest('GET', get_vapp_restcall)
4051
bayramovfe3f3c92016-10-04 07:53:41 +04004052 if response.status_code != requests.codes.ok:
4053 self.logger.debug("REST API call {} failed. Return status code {}".format(get_vapp_restcall,
4054 response.status_code))
4055 return parsed_respond
4056
4057 try:
4058 xmlroot_respond = XmlElementTree.fromstring(response.content)
4059 parsed_respond['ovfDescriptorUploaded'] = xmlroot_respond.attrib['ovfDescriptorUploaded']
4060
bhangarea92ae392017-01-12 22:30:29 -08004061 namespaces = {"vssd":"http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData" ,
4062 'ovf': 'http://schemas.dmtf.org/ovf/envelope/1',
4063 'vmw': 'http://www.vmware.com/schema/ovf',
4064 'vm': 'http://www.vmware.com/vcloud/v1.5',
4065 'rasd':"http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData",
4066 "vmext":"http://www.vmware.com/vcloud/extension/v1.5",
4067 "xmlns":"http://www.vmware.com/vcloud/v1.5"
4068 }
bayramovfe3f3c92016-10-04 07:53:41 +04004069
bhangarea92ae392017-01-12 22:30:29 -08004070 created_section = xmlroot_respond.find('vm:DateCreated', namespaces)
bayramovfe3f3c92016-10-04 07:53:41 +04004071 if created_section is not None:
4072 parsed_respond['created'] = created_section.text
4073
bhangarea92ae392017-01-12 22:30:29 -08004074 network_section = xmlroot_respond.find('vm:NetworkConfigSection/vm:NetworkConfig', namespaces)
bayramov5761ad12016-10-04 09:00:30 +04004075 if network_section is not None and 'networkName' in network_section.attrib:
bayramovfe3f3c92016-10-04 07:53:41 +04004076 parsed_respond['networkname'] = network_section.attrib['networkName']
4077
4078 ipscopes_section = \
4079 xmlroot_respond.find('vm:NetworkConfigSection/vm:NetworkConfig/vm:Configuration/vm:IpScopes',
bhangarea92ae392017-01-12 22:30:29 -08004080 namespaces)
bayramovfe3f3c92016-10-04 07:53:41 +04004081 if ipscopes_section is not None:
4082 for ipscope in ipscopes_section:
4083 for scope in ipscope:
4084 tag_key = scope.tag.split("}")[1]
4085 if tag_key == 'IpRanges':
4086 ip_ranges = scope.getchildren()
4087 for ipblock in ip_ranges:
4088 for block in ipblock:
4089 parsed_respond[block.tag.split("}")[1]] = block.text
4090 else:
4091 parsed_respond[tag_key] = scope.text
4092
4093 # parse children section for other attrib
bhangarea92ae392017-01-12 22:30:29 -08004094 children_section = xmlroot_respond.find('vm:Children/', namespaces)
bayramovfe3f3c92016-10-04 07:53:41 +04004095 if children_section is not None:
4096 parsed_respond['name'] = children_section.attrib['name']
bhangarea92ae392017-01-12 22:30:29 -08004097 parsed_respond['nestedHypervisorEnabled'] = children_section.attrib['nestedHypervisorEnabled'] \
4098 if "nestedHypervisorEnabled" in children_section.attrib else None
bayramovfe3f3c92016-10-04 07:53:41 +04004099 parsed_respond['deployed'] = children_section.attrib['deployed']
4100 parsed_respond['status'] = children_section.attrib['status']
4101 parsed_respond['vmuuid'] = children_section.attrib['id'].split(":")[-1]
bhangarea92ae392017-01-12 22:30:29 -08004102 network_adapter = children_section.find('vm:NetworkConnectionSection', namespaces)
bayramovfe3f3c92016-10-04 07:53:41 +04004103 nic_list = []
4104 for adapters in network_adapter:
4105 adapter_key = adapters.tag.split("}")[1]
4106 if adapter_key == 'PrimaryNetworkConnectionIndex':
4107 parsed_respond['primarynetwork'] = adapters.text
4108 if adapter_key == 'NetworkConnection':
4109 vnic = {}
bayramov5761ad12016-10-04 09:00:30 +04004110 if 'network' in adapters.attrib:
bayramovfe3f3c92016-10-04 07:53:41 +04004111 vnic['network'] = adapters.attrib['network']
4112 for adapter in adapters:
4113 setting_key = adapter.tag.split("}")[1]
4114 vnic[setting_key] = adapter.text
4115 nic_list.append(vnic)
4116
4117 for link in children_section:
bayramov5761ad12016-10-04 09:00:30 +04004118 if link.tag.split("}")[1] == 'Link' and 'rel' in link.attrib:
bayramovfe3f3c92016-10-04 07:53:41 +04004119 if link.attrib['rel'] == 'screen:acquireTicket':
4120 parsed_respond['acquireTicket'] = link.attrib
4121 if link.attrib['rel'] == 'screen:acquireMksTicket':
4122 parsed_respond['acquireMksTicket'] = link.attrib
4123
4124 parsed_respond['interfaces'] = nic_list
bhangarefda5f7c2017-01-12 23:50:34 -08004125 vCloud_extension_section = children_section.find('xmlns:VCloudExtension', namespaces)
4126 if vCloud_extension_section is not None:
4127 vm_vcenter_info = {}
4128 vim_info = vCloud_extension_section.find('vmext:VmVimInfo', namespaces)
4129 vmext = vim_info.find('vmext:VmVimObjectRef', namespaces)
4130 if vmext is not None:
4131 vm_vcenter_info["vm_moref_id"] = vmext.find('vmext:MoRef', namespaces).text
bhangarefda5f7c2017-01-12 23:50:34 -08004132 parsed_respond["vm_vcenter_info"]= vm_vcenter_info
bayramovfe3f3c92016-10-04 07:53:41 +04004133
bhangarea92ae392017-01-12 22:30:29 -08004134 virtual_hardware_section = children_section.find('ovf:VirtualHardwareSection', namespaces)
4135 vm_virtual_hardware_info = {}
4136 if virtual_hardware_section is not None:
4137 for item in virtual_hardware_section.iterfind('ovf:Item',namespaces):
4138 if item.find("rasd:Description",namespaces).text == "Hard disk":
4139 disk_size = item.find("rasd:HostResource" ,namespaces
4140 ).attrib["{"+namespaces['vm']+"}capacity"]
4141
4142 vm_virtual_hardware_info["disk_size"]= disk_size
4143 break
4144
4145 for link in virtual_hardware_section:
4146 if link.tag.split("}")[1] == 'Link' and 'rel' in link.attrib:
4147 if link.attrib['rel'] == 'edit' and link.attrib['href'].endswith("/disks"):
4148 vm_virtual_hardware_info["disk_edit_href"] = link.attrib['href']
4149 break
4150
4151 parsed_respond["vm_virtual_hardware"]= vm_virtual_hardware_info
4152 except Exception as exp :
4153 self.logger.info("Error occurred calling rest api for getting vApp details {}".format(exp))
bayramovfe3f3c92016-10-04 07:53:41 +04004154 return parsed_respond
4155
kasarc5bf2932018-03-09 04:15:22 -08004156 def acquire_console(self, vm_uuid=None):
bayramovfe3f3c92016-10-04 07:53:41 +04004157
bayramovfe3f3c92016-10-04 07:53:41 +04004158 if vm_uuid is None:
4159 return None
kasarc5bf2932018-03-09 04:15:22 -08004160 if self.client._session:
4161 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
4162 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
4163 vm_dict = self.get_vapp_details_rest(vapp_uuid=vm_uuid)
bayramovfe3f3c92016-10-04 07:53:41 +04004164 console_dict = vm_dict['acquireTicket']
4165 console_rest_call = console_dict['href']
4166
kasarc5bf2932018-03-09 04:15:22 -08004167 response = self.perform_request(req_type='POST',
4168 url=console_rest_call,
4169 headers=headers)
4170
bhangare1a0b97c2017-06-21 02:20:15 -07004171 if response.status_code == 403:
4172 response = self.retry_rest('POST', console_rest_call)
bayramovfe3f3c92016-10-04 07:53:41 +04004173
bayramov5761ad12016-10-04 09:00:30 +04004174 if response.status_code == requests.codes.ok:
4175 return response.content
bayramovfe3f3c92016-10-04 07:53:41 +04004176
kate15f1c382016-12-15 01:12:40 -08004177 return None
kate13ab2c42016-12-23 01:34:24 -08004178
bhangarea92ae392017-01-12 22:30:29 -08004179 def modify_vm_disk(self, vapp_uuid, flavor_disk):
4180 """
4181 Method retrieve vm disk details
4182
4183 Args:
4184 vapp_uuid - is vapp identifier.
4185 flavor_disk - disk size as specified in VNFD (flavor)
4186
4187 Returns:
4188 The return network uuid or return None
4189 """
4190 status = None
4191 try:
4192 #Flavor disk is in GB convert it into MB
4193 flavor_disk = int(flavor_disk) * 1024
4194 vm_details = self.get_vapp_details_rest(vapp_uuid)
4195 if vm_details:
4196 vm_name = vm_details["name"]
4197 self.logger.info("VM: {} flavor_disk :{}".format(vm_name , flavor_disk))
4198
4199 if vm_details and "vm_virtual_hardware" in vm_details:
4200 vm_disk = int(vm_details["vm_virtual_hardware"]["disk_size"])
4201 disk_edit_href = vm_details["vm_virtual_hardware"]["disk_edit_href"]
4202
4203 self.logger.info("VM: {} VM_disk :{}".format(vm_name , vm_disk))
4204
4205 if flavor_disk > vm_disk:
4206 status = self.modify_vm_disk_rest(disk_edit_href ,flavor_disk)
4207 self.logger.info("Modify disk of VM {} from {} to {} MB".format(vm_name,
4208 vm_disk, flavor_disk ))
4209 else:
4210 status = True
4211 self.logger.info("No need to modify disk of VM {}".format(vm_name))
4212
4213 return status
4214 except Exception as exp:
4215 self.logger.info("Error occurred while modifing disk size {}".format(exp))
4216
4217
4218 def modify_vm_disk_rest(self, disk_href , disk_size):
4219 """
4220 Method retrieve modify vm disk size
4221
4222 Args:
4223 disk_href - vCD API URL to GET and PUT disk data
4224 disk_size - disk size as specified in VNFD (flavor)
4225
4226 Returns:
4227 The return network uuid or return None
4228 """
bhangarea92ae392017-01-12 22:30:29 -08004229 if disk_href is None or disk_size is None:
4230 return None
4231
kasarc5bf2932018-03-09 04:15:22 -08004232 if self.client._session:
4233 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
sbhangarea8e5b782018-06-21 02:10:03 -07004234 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
kasarc5bf2932018-03-09 04:15:22 -08004235 response = self.perform_request(req_type='GET',
4236 url=disk_href,
4237 headers=headers)
bhangare1a0b97c2017-06-21 02:20:15 -07004238
4239 if response.status_code == 403:
4240 response = self.retry_rest('GET', disk_href)
bhangarea92ae392017-01-12 22:30:29 -08004241
4242 if response.status_code != requests.codes.ok:
4243 self.logger.debug("GET REST API call {} failed. Return status code {}".format(disk_href,
4244 response.status_code))
4245 return None
4246 try:
4247 lxmlroot_respond = lxmlElementTree.fromstring(response.content)
4248 namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.iteritems() if prefix}
kasarc5bf2932018-03-09 04:15:22 -08004249 #For python3
4250 #namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.items() if prefix}
bhangarea92ae392017-01-12 22:30:29 -08004251 namespaces["xmlns"]= "http://www.vmware.com/vcloud/v1.5"
4252
4253 for item in lxmlroot_respond.iterfind('xmlns:Item',namespaces):
4254 if item.find("rasd:Description",namespaces).text == "Hard disk":
4255 disk_item = item.find("rasd:HostResource" ,namespaces )
4256 if disk_item is not None:
4257 disk_item.attrib["{"+namespaces['xmlns']+"}capacity"] = str(disk_size)
4258 break
4259
4260 data = lxmlElementTree.tostring(lxmlroot_respond, encoding='utf8', method='xml',
4261 xml_declaration=True)
4262
4263 #Send PUT request to modify disk size
bhangarea92ae392017-01-12 22:30:29 -08004264 headers['Content-Type'] = 'application/vnd.vmware.vcloud.rasdItemsList+xml; charset=ISO-8859-1'
4265
kasarc5bf2932018-03-09 04:15:22 -08004266 response = self.perform_request(req_type='PUT',
4267 url=disk_href,
4268 headers=headers,
4269 data=data)
bhangare1a0b97c2017-06-21 02:20:15 -07004270 if response.status_code == 403:
4271 add_headers = {'Content-Type': headers['Content-Type']}
4272 response = self.retry_rest('PUT', disk_href, add_headers, data)
bhangarea92ae392017-01-12 22:30:29 -08004273
4274 if response.status_code != 202:
4275 self.logger.debug("PUT REST API call {} failed. Return status code {}".format(disk_href,
4276 response.status_code))
4277 else:
kasarc5bf2932018-03-09 04:15:22 -08004278 modify_disk_task = self.get_task_from_response(response.content)
4279 result = self.client.get_task_monitor().wait_for_success(task=modify_disk_task)
4280 if result.get('status') == 'success':
4281 return True
4282 else:
sbhangarea8e5b782018-06-21 02:10:03 -07004283 return False
bhangarea92ae392017-01-12 22:30:29 -08004284 return None
4285
4286 except Exception as exp :
4287 self.logger.info("Error occurred calling rest api for modifing disk size {}".format(exp))
4288 return None
4289
bhangarefda5f7c2017-01-12 23:50:34 -08004290 def add_pci_devices(self, vapp_uuid , pci_devices , vmname_andid):
4291 """
4292 Method to attach pci devices to VM
4293
4294 Args:
4295 vapp_uuid - uuid of vApp/VM
4296 pci_devices - pci devices infromation as specified in VNFD (flavor)
4297
4298 Returns:
4299 The status of add pci device task , vm object and
4300 vcenter_conect object
4301 """
4302 vm_obj = None
bhangarefda5f7c2017-01-12 23:50:34 -08004303 self.logger.info("Add pci devices {} into vApp {}".format(pci_devices , vapp_uuid))
bhangare06312472017-03-30 05:49:07 -07004304 vcenter_conect, content = self.get_vcenter_content()
4305 vm_moref_id = self.get_vm_moref_id(vapp_uuid)
kateeb044522017-03-06 23:54:39 -08004306
bhangare06312472017-03-30 05:49:07 -07004307 if vm_moref_id:
bhangarefda5f7c2017-01-12 23:50:34 -08004308 try:
4309 no_of_pci_devices = len(pci_devices)
4310 if no_of_pci_devices > 0:
bhangarefda5f7c2017-01-12 23:50:34 -08004311 #Get VM and its host
bhangare06312472017-03-30 05:49:07 -07004312 host_obj, vm_obj = self.get_vm_obj(content, vm_moref_id)
bhangarefda5f7c2017-01-12 23:50:34 -08004313 self.logger.info("VM {} is currently on host {}".format(vm_obj, host_obj))
4314 if host_obj and vm_obj:
4315 #get PCI devies from host on which vapp is currently installed
4316 avilable_pci_devices = self.get_pci_devices(host_obj, no_of_pci_devices)
4317
4318 if avilable_pci_devices is None:
4319 #find other hosts with active pci devices
4320 new_host_obj , avilable_pci_devices = self.get_host_and_PCIdevices(
4321 content,
4322 no_of_pci_devices
4323 )
4324
4325 if new_host_obj is not None and avilable_pci_devices is not None and len(avilable_pci_devices)> 0:
4326 #Migrate vm to the host where PCI devices are availble
4327 self.logger.info("Relocate VM {} on new host {}".format(vm_obj, new_host_obj))
4328 task = self.relocate_vm(new_host_obj, vm_obj)
4329 if task is not None:
4330 result = self.wait_for_vcenter_task(task, vcenter_conect)
4331 self.logger.info("Migrate VM status: {}".format(result))
4332 host_obj = new_host_obj
4333 else:
4334 self.logger.info("Fail to migrate VM : {}".format(result))
4335 raise vimconn.vimconnNotFoundException(
4336 "Fail to migrate VM : {} to host {}".format(
4337 vmname_andid,
4338 new_host_obj)
4339 )
4340
4341 if host_obj is not None and avilable_pci_devices is not None and len(avilable_pci_devices)> 0:
4342 #Add PCI devices one by one
4343 for pci_device in avilable_pci_devices:
4344 task = self.add_pci_to_vm(host_obj, vm_obj, pci_device)
4345 if task:
4346 status= self.wait_for_vcenter_task(task, vcenter_conect)
4347 if status:
4348 self.logger.info("Added PCI device {} to VM {}".format(pci_device,str(vm_obj)))
4349 else:
kateeb044522017-03-06 23:54:39 -08004350 self.logger.error("Fail to add PCI device {} to VM {}".format(pci_device,str(vm_obj)))
bhangarefda5f7c2017-01-12 23:50:34 -08004351 return True, vm_obj, vcenter_conect
4352 else:
4353 self.logger.error("Currently there is no host with"\
4354 " {} number of avaialble PCI devices required for VM {}".format(
4355 no_of_pci_devices,
4356 vmname_andid)
4357 )
4358 raise vimconn.vimconnNotFoundException(
4359 "Currently there is no host with {} "\
4360 "number of avaialble PCI devices required for VM {}".format(
4361 no_of_pci_devices,
4362 vmname_andid))
4363 else:
4364 self.logger.debug("No infromation about PCI devices {} ",pci_devices)
4365
4366 except vmodl.MethodFault as error:
4367 self.logger.error("Error occurred while adding PCI devices {} ",error)
4368 return None, vm_obj, vcenter_conect
4369
4370 def get_vm_obj(self, content, mob_id):
4371 """
4372 Method to get the vsphere VM object associated with a given morf ID
4373 Args:
4374 vapp_uuid - uuid of vApp/VM
4375 content - vCenter content object
4376 mob_id - mob_id of VM
4377
4378 Returns:
4379 VM and host object
4380 """
4381 vm_obj = None
4382 host_obj = None
4383 try :
4384 container = content.viewManager.CreateContainerView(content.rootFolder,
4385 [vim.VirtualMachine], True
4386 )
4387 for vm in container.view:
4388 mobID = vm._GetMoId()
4389 if mobID == mob_id:
4390 vm_obj = vm
4391 host_obj = vm_obj.runtime.host
4392 break
4393 except Exception as exp:
4394 self.logger.error("Error occurred while finding VM object : {}".format(exp))
4395 return host_obj, vm_obj
4396
4397 def get_pci_devices(self, host, need_devices):
4398 """
4399 Method to get the details of pci devices on given host
4400 Args:
4401 host - vSphere host object
4402 need_devices - number of pci devices needed on host
4403
4404 Returns:
4405 array of pci devices
4406 """
4407 all_devices = []
4408 all_device_ids = []
4409 used_devices_ids = []
4410
4411 try:
4412 if host:
4413 pciPassthruInfo = host.config.pciPassthruInfo
4414 pciDevies = host.hardware.pciDevice
4415
4416 for pci_status in pciPassthruInfo:
4417 if pci_status.passthruActive:
4418 for device in pciDevies:
4419 if device.id == pci_status.id:
4420 all_device_ids.append(device.id)
4421 all_devices.append(device)
4422
4423 #check if devices are in use
4424 avalible_devices = all_devices
4425 for vm in host.vm:
4426 if vm.runtime.powerState == vim.VirtualMachinePowerState.poweredOn:
4427 vm_devices = vm.config.hardware.device
4428 for device in vm_devices:
4429 if type(device) is vim.vm.device.VirtualPCIPassthrough:
4430 if device.backing.id in all_device_ids:
4431 for use_device in avalible_devices:
4432 if use_device.id == device.backing.id:
4433 avalible_devices.remove(use_device)
4434 used_devices_ids.append(device.backing.id)
4435 self.logger.debug("Device {} from devices {}"\
4436 "is in use".format(device.backing.id,
4437 device)
4438 )
4439 if len(avalible_devices) < need_devices:
4440 self.logger.debug("Host {} don't have {} number of active devices".format(host,
4441 need_devices))
4442 self.logger.debug("found only {} devives {}".format(len(avalible_devices),
4443 avalible_devices))
4444 return None
4445 else:
4446 required_devices = avalible_devices[:need_devices]
4447 self.logger.info("Found {} PCI devivces on host {} but required only {}".format(
4448 len(avalible_devices),
4449 host,
4450 need_devices))
4451 self.logger.info("Retruning {} devices as {}".format(need_devices,
4452 required_devices ))
4453 return required_devices
4454
4455 except Exception as exp:
4456 self.logger.error("Error {} occurred while finding pci devices on host: {}".format(exp, host))
4457
4458 return None
4459
4460 def get_host_and_PCIdevices(self, content, need_devices):
4461 """
4462 Method to get the details of pci devices infromation on all hosts
4463
4464 Args:
4465 content - vSphere host object
4466 need_devices - number of pci devices needed on host
4467
4468 Returns:
4469 array of pci devices and host object
4470 """
4471 host_obj = None
4472 pci_device_objs = None
4473 try:
4474 if content:
4475 container = content.viewManager.CreateContainerView(content.rootFolder,
4476 [vim.HostSystem], True)
4477 for host in container.view:
4478 devices = self.get_pci_devices(host, need_devices)
4479 if devices:
4480 host_obj = host
4481 pci_device_objs = devices
4482 break
4483 except Exception as exp:
4484 self.logger.error("Error {} occurred while finding pci devices on host: {}".format(exp, host_obj))
4485
4486 return host_obj,pci_device_objs
4487
4488 def relocate_vm(self, dest_host, vm) :
4489 """
4490 Method to get the relocate VM to new host
4491
4492 Args:
4493 dest_host - vSphere host object
4494 vm - vSphere VM object
4495
4496 Returns:
4497 task object
4498 """
4499 task = None
4500 try:
4501 relocate_spec = vim.vm.RelocateSpec(host=dest_host)
4502 task = vm.Relocate(relocate_spec)
4503 self.logger.info("Migrating {} to destination host {}".format(vm, dest_host))
4504 except Exception as exp:
4505 self.logger.error("Error occurred while relocate VM {} to new host {}: {}".format(
4506 dest_host, vm, exp))
4507 return task
4508
4509 def wait_for_vcenter_task(self, task, actionName='job', hideResult=False):
4510 """
4511 Waits and provides updates on a vSphere task
4512 """
4513 while task.info.state == vim.TaskInfo.State.running:
4514 time.sleep(2)
4515
4516 if task.info.state == vim.TaskInfo.State.success:
4517 if task.info.result is not None and not hideResult:
4518 self.logger.info('{} completed successfully, result: {}'.format(
4519 actionName,
4520 task.info.result))
4521 else:
4522 self.logger.info('Task {} completed successfully.'.format(actionName))
4523 else:
4524 self.logger.error('{} did not complete successfully: {} '.format(
4525 actionName,
4526 task.info.error)
4527 )
4528
4529 return task.info.result
4530
4531 def add_pci_to_vm(self,host_object, vm_object, host_pci_dev):
4532 """
4533 Method to add pci device in given VM
4534
4535 Args:
4536 host_object - vSphere host object
4537 vm_object - vSphere VM object
4538 host_pci_dev - host_pci_dev must be one of the devices from the
4539 host_object.hardware.pciDevice list
4540 which is configured as a PCI passthrough device
4541
4542 Returns:
4543 task object
4544 """
4545 task = None
4546 if vm_object and host_object and host_pci_dev:
4547 try :
4548 #Add PCI device to VM
4549 pci_passthroughs = vm_object.environmentBrowser.QueryConfigTarget(host=None).pciPassthrough
4550 systemid_by_pciid = {item.pciDevice.id: item.systemId for item in pci_passthroughs}
4551
4552 if host_pci_dev.id not in systemid_by_pciid:
4553 self.logger.error("Device {} is not a passthrough device ".format(host_pci_dev))
4554 return None
4555
4556 deviceId = hex(host_pci_dev.deviceId % 2**16).lstrip('0x')
4557 backing = vim.VirtualPCIPassthroughDeviceBackingInfo(deviceId=deviceId,
4558 id=host_pci_dev.id,
4559 systemId=systemid_by_pciid[host_pci_dev.id],
4560 vendorId=host_pci_dev.vendorId,
4561 deviceName=host_pci_dev.deviceName)
4562
4563 hba_object = vim.VirtualPCIPassthrough(key=-100, backing=backing)
4564
4565 new_device_config = vim.VirtualDeviceConfigSpec(device=hba_object)
4566 new_device_config.operation = "add"
4567 vmConfigSpec = vim.vm.ConfigSpec()
4568 vmConfigSpec.deviceChange = [new_device_config]
4569
4570 task = vm_object.ReconfigVM_Task(spec=vmConfigSpec)
4571 self.logger.info("Adding PCI device {} into VM {} from host {} ".format(
4572 host_pci_dev, vm_object, host_object)
4573 )
4574 except Exception as exp:
4575 self.logger.error("Error occurred while adding pci devive {} to VM {}: {}".format(
4576 host_pci_dev,
4577 vm_object,
4578 exp))
4579 return task
4580
bhangare06312472017-03-30 05:49:07 -07004581 def get_vm_vcenter_info(self):
bhangarefda5f7c2017-01-12 23:50:34 -08004582 """
kateeb044522017-03-06 23:54:39 -08004583 Method to get details of vCenter and vm
bhangarefda5f7c2017-01-12 23:50:34 -08004584
4585 Args:
4586 vapp_uuid - uuid of vApp or VM
4587
4588 Returns:
4589 Moref Id of VM and deails of vCenter
4590 """
kateeb044522017-03-06 23:54:39 -08004591 vm_vcenter_info = {}
bhangarefda5f7c2017-01-12 23:50:34 -08004592
kateeb044522017-03-06 23:54:39 -08004593 if self.vcenter_ip is not None:
4594 vm_vcenter_info["vm_vcenter_ip"] = self.vcenter_ip
4595 else:
4596 raise vimconn.vimconnException(message="vCenter IP is not provided."\
4597 " Please provide vCenter IP while attaching datacenter to tenant in --config")
4598 if self.vcenter_port is not None:
4599 vm_vcenter_info["vm_vcenter_port"] = self.vcenter_port
4600 else:
4601 raise vimconn.vimconnException(message="vCenter port is not provided."\
4602 " Please provide vCenter port while attaching datacenter to tenant in --config")
4603 if self.vcenter_user is not None:
4604 vm_vcenter_info["vm_vcenter_user"] = self.vcenter_user
4605 else:
4606 raise vimconn.vimconnException(message="vCenter user is not provided."\
4607 " Please provide vCenter user while attaching datacenter to tenant in --config")
bhangarefda5f7c2017-01-12 23:50:34 -08004608
kateeb044522017-03-06 23:54:39 -08004609 if self.vcenter_password is not None:
4610 vm_vcenter_info["vm_vcenter_password"] = self.vcenter_password
4611 else:
4612 raise vimconn.vimconnException(message="vCenter user password is not provided."\
4613 " Please provide vCenter user password while attaching datacenter to tenant in --config")
bhangarefda5f7c2017-01-12 23:50:34 -08004614
bhangare06312472017-03-30 05:49:07 -07004615 return vm_vcenter_info
bhangarefda5f7c2017-01-12 23:50:34 -08004616
4617
4618 def get_vm_pci_details(self, vmuuid):
4619 """
4620 Method to get VM PCI device details from vCenter
4621
4622 Args:
4623 vm_obj - vSphere VM object
4624
4625 Returns:
4626 dict of PCI devives attached to VM
4627
4628 """
4629 vm_pci_devices_info = {}
4630 try:
bhangare06312472017-03-30 05:49:07 -07004631 vcenter_conect, content = self.get_vcenter_content()
4632 vm_moref_id = self.get_vm_moref_id(vmuuid)
4633 if vm_moref_id:
bhangarefda5f7c2017-01-12 23:50:34 -08004634 #Get VM and its host
kateeb044522017-03-06 23:54:39 -08004635 if content:
bhangare06312472017-03-30 05:49:07 -07004636 host_obj, vm_obj = self.get_vm_obj(content, vm_moref_id)
kateeb044522017-03-06 23:54:39 -08004637 if host_obj and vm_obj:
4638 vm_pci_devices_info["host_name"]= host_obj.name
4639 vm_pci_devices_info["host_ip"]= host_obj.config.network.vnic[0].spec.ip.ipAddress
4640 for device in vm_obj.config.hardware.device:
4641 if type(device) == vim.vm.device.VirtualPCIPassthrough:
4642 device_details={'devide_id':device.backing.id,
4643 'pciSlotNumber':device.slotInfo.pciSlotNumber,
4644 }
4645 vm_pci_devices_info[device.deviceInfo.label] = device_details
4646 else:
4647 self.logger.error("Can not connect to vCenter while getting "\
4648 "PCI devices infromationn")
4649 return vm_pci_devices_info
bhangarefda5f7c2017-01-12 23:50:34 -08004650 except Exception as exp:
kateeb044522017-03-06 23:54:39 -08004651 self.logger.error("Error occurred while getting VM infromationn"\
4652 " for VM : {}".format(exp))
4653 raise vimconn.vimconnException(message=exp)
bhangare0e571a92017-01-12 04:02:23 -08004654
Ravi Chamarty5b2d5c12018-10-22 23:59:10 +00004655
Ravi Chamartyb0ab2c02018-10-26 01:40:50 +00004656 def reserve_memory_for_all_vms(self, vapp, memory_mb):
4657 """
4658 Method to reserve memory for all VMs
4659 Args :
4660 vapp - VApp
4661 memory_mb - Memory in MB
4662 Returns:
4663 None
4664 """
4665
4666 self.logger.info("Reserve memory for all VMs")
4667 for vms in vapp.get_all_vms():
4668 vm_id = vms.get('id').split(':')[-1]
4669
4670 url_rest_call = "{}/api/vApp/vm-{}/virtualHardwareSection/memory".format(self.url, vm_id)
4671
4672 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
4673 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
4674 headers['Content-Type'] = 'application/vnd.vmware.vcloud.rasdItem+xml'
4675 response = self.perform_request(req_type='GET',
4676 url=url_rest_call,
4677 headers=headers)
4678
4679 if response.status_code == 403:
4680 response = self.retry_rest('GET', url_rest_call)
4681
4682 if response.status_code != 200:
4683 self.logger.error("REST call {} failed reason : {}"\
4684 "status code : {}".format(url_rest_call,
4685 response.content,
4686 response.status_code))
4687 raise vimconn.vimconnException("reserve_memory_for_all_vms : Failed to get "\
4688 "memory")
4689
4690 bytexml = bytes(bytearray(response.content, encoding='utf-8'))
4691 contentelem = lxmlElementTree.XML(bytexml)
4692 namespaces = {prefix:uri for prefix,uri in contentelem.nsmap.iteritems() if prefix}
4693 namespaces["xmlns"]= "http://www.vmware.com/vcloud/v1.5"
4694
4695 # Find the reservation element in the response
4696 memelem_list = contentelem.findall(".//rasd:Reservation", namespaces)
4697 for memelem in memelem_list:
4698 memelem.text = str(memory_mb)
4699
4700 newdata = lxmlElementTree.tostring(contentelem, pretty_print=True)
4701
4702 response = self.perform_request(req_type='PUT',
4703 url=url_rest_call,
4704 headers=headers,
4705 data=newdata)
4706
4707 if response.status_code == 403:
4708 add_headers = {'Content-Type': headers['Content-Type']}
4709 response = self.retry_rest('PUT', url_rest_call, add_headers, newdata)
4710
4711 if response.status_code != 202:
4712 self.logger.error("REST call {} failed reason : {}"\
4713 "status code : {} ".format(url_rest_call,
4714 response.content,
4715 response.status_code))
4716 raise vimconn.vimconnException("reserve_memory_for_all_vms : Failed to update "\
4717 "virtual hardware memory section")
4718 else:
4719 mem_task = self.get_task_from_response(response.content)
4720 result = self.client.get_task_monitor().wait_for_success(task=mem_task)
4721 if result.get('status') == 'success':
4722 self.logger.info("reserve_memory_for_all_vms(): VM {} succeeded "\
4723 .format(vm_id))
4724 else:
4725 self.logger.error("reserve_memory_for_all_vms(): VM {} failed "\
4726 .format(vm_id))
4727
4728 def connect_vapp_to_org_vdc_network(self, vapp_id, net_name):
4729 """
4730 Configure VApp network config with org vdc network
4731 Args :
4732 vapp - VApp
4733 Returns:
4734 None
4735 """
4736
4737 self.logger.info("Connecting vapp {} to org vdc network {}".
4738 format(vapp_id, net_name))
4739
4740 url_rest_call = "{}/api/vApp/vapp-{}/networkConfigSection/".format(self.url, vapp_id)
4741
4742 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
4743 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
4744 response = self.perform_request(req_type='GET',
4745 url=url_rest_call,
4746 headers=headers)
4747
4748 if response.status_code == 403:
4749 response = self.retry_rest('GET', url_rest_call)
4750
4751 if response.status_code != 200:
4752 self.logger.error("REST call {} failed reason : {}"\
4753 "status code : {}".format(url_rest_call,
4754 response.content,
4755 response.status_code))
4756 raise vimconn.vimconnException("connect_vapp_to_org_vdc_network : Failed to get "\
4757 "network config section")
4758
4759 data = response.content
4760 headers['Content-Type'] = 'application/vnd.vmware.vcloud.networkConfigSection+xml'
4761 net_id = self.get_network_id_by_name(net_name)
4762 if not net_id:
4763 raise vimconn.vimconnException("connect_vapp_to_org_vdc_network : Failed to find "\
4764 "existing network")
4765
4766 bytexml = bytes(bytearray(data, encoding='utf-8'))
4767 newelem = lxmlElementTree.XML(bytexml)
4768 namespaces = {prefix: uri for prefix, uri in newelem.nsmap.iteritems() if prefix}
4769 namespaces["xmlns"] = "http://www.vmware.com/vcloud/v1.5"
4770 nwcfglist = newelem.findall(".//xmlns:NetworkConfig", namespaces)
4771
4772 newstr = """<NetworkConfig networkName="{}">
4773 <Configuration>
4774 <ParentNetwork href="{}/api/network/{}"/>
4775 <FenceMode>bridged</FenceMode>
4776 </Configuration>
4777 </NetworkConfig>
4778 """.format(net_name, self.url, net_id)
4779 newcfgelem = lxmlElementTree.fromstring(newstr)
4780 if nwcfglist:
4781 nwcfglist[0].addnext(newcfgelem)
4782
4783 newdata = lxmlElementTree.tostring(newelem, pretty_print=True)
4784
4785 response = self.perform_request(req_type='PUT',
4786 url=url_rest_call,
4787 headers=headers,
4788 data=newdata)
4789
4790 if response.status_code == 403:
4791 add_headers = {'Content-Type': headers['Content-Type']}
4792 response = self.retry_rest('PUT', url_rest_call, add_headers, newdata)
4793
4794 if response.status_code != 202:
4795 self.logger.error("REST call {} failed reason : {}"\
4796 "status code : {} ".format(url_rest_call,
4797 response.content,
4798 response.status_code))
4799 raise vimconn.vimconnException("connect_vapp_to_org_vdc_network : Failed to update "\
4800 "network config section")
4801 else:
4802 vapp_task = self.get_task_from_response(response.content)
4803 result = self.client.get_task_monitor().wait_for_success(task=vapp_task)
4804 if result.get('status') == 'success':
4805 self.logger.info("connect_vapp_to_org_vdc_network(): Vapp {} connected to "\
4806 "network {}".format(vapp_id, net_name))
4807 else:
4808 self.logger.error("connect_vapp_to_org_vdc_network(): Vapp {} failed to "\
4809 "connect to network {}".format(vapp_id, net_name))
4810
Ravi Chamarty5b2d5c12018-10-22 23:59:10 +00004811 def remove_primary_network_adapter_from_all_vms(self, vapp):
4812 """
4813 Method to remove network adapter type to vm
4814 Args :
4815 vapp - VApp
4816 Returns:
4817 None
4818 """
4819
4820 self.logger.info("Removing network adapter from all VMs")
4821 for vms in vapp.get_all_vms():
4822 vm_id = vms.get('id').split(':')[-1]
4823
4824 url_rest_call = "{}/api/vApp/vm-{}/networkConnectionSection/".format(self.url, vm_id)
4825
4826 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
4827 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
4828 response = self.perform_request(req_type='GET',
4829 url=url_rest_call,
4830 headers=headers)
4831
4832 if response.status_code == 403:
4833 response = self.retry_rest('GET', url_rest_call)
4834
4835 if response.status_code != 200:
4836 self.logger.error("REST call {} failed reason : {}"\
4837 "status code : {}".format(url_rest_call,
4838 response.content,
4839 response.status_code))
4840 raise vimconn.vimconnException("remove_primary_network_adapter : Failed to get "\
4841 "network connection section")
4842
4843 data = response.content
4844 data = data.split('<Link rel="edit"')[0]
4845
4846 headers['Content-Type'] = 'application/vnd.vmware.vcloud.networkConnectionSection+xml'
4847
4848 newdata = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
4849 <NetworkConnectionSection xmlns="http://www.vmware.com/vcloud/v1.5"
4850 xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1"
4851 xmlns:vssd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData"
4852 xmlns:common="http://schemas.dmtf.org/wbem/wscim/1/common"
4853 xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData"
4854 xmlns:vmw="http://www.vmware.com/schema/ovf"
4855 xmlns:ovfenv="http://schemas.dmtf.org/ovf/environment/1"
4856 xmlns:vmext="http://www.vmware.com/vcloud/extension/v1.5"
4857 xmlns:ns9="http://www.vmware.com/vcloud/versions"
4858 href="{url}" type="application/vnd.vmware.vcloud.networkConnectionSection+xml" ovf:required="false">
4859 <ovf:Info>Specifies the available VM network connections</ovf:Info>
4860 <PrimaryNetworkConnectionIndex>0</PrimaryNetworkConnectionIndex>
4861 <Link rel="edit" href="{url}" type="application/vnd.vmware.vcloud.networkConnectionSection+xml"/>
4862 </NetworkConnectionSection>""".format(url=url_rest_call)
4863 response = self.perform_request(req_type='PUT',
4864 url=url_rest_call,
4865 headers=headers,
4866 data=newdata)
4867
4868 if response.status_code == 403:
4869 add_headers = {'Content-Type': headers['Content-Type']}
4870 response = self.retry_rest('PUT', url_rest_call, add_headers, newdata)
4871
4872 if response.status_code != 202:
4873 self.logger.error("REST call {} failed reason : {}"\
4874 "status code : {} ".format(url_rest_call,
4875 response.content,
4876 response.status_code))
4877 raise vimconn.vimconnException("remove_primary_network_adapter : Failed to update "\
4878 "network connection section")
4879 else:
4880 nic_task = self.get_task_from_response(response.content)
4881 result = self.client.get_task_monitor().wait_for_success(task=nic_task)
4882 if result.get('status') == 'success':
4883 self.logger.info("remove_primary_network_adapter(): VM {} conneced to "\
4884 "default NIC type".format(vm_id))
4885 else:
4886 self.logger.error("remove_primary_network_adapter(): VM {} failed to "\
4887 "connect NIC type".format(vm_id))
4888
kasardc1f02e2017-03-25 07:20:30 -07004889 def add_network_adapter_to_vms(self, vapp, network_name, primary_nic_index, nicIndex, net, nic_type=None):
kasar3ac5dc42017-03-15 06:28:22 -07004890 """
4891 Method to add network adapter type to vm
4892 Args :
4893 network_name - name of network
4894 primary_nic_index - int value for primary nic index
4895 nicIndex - int value for nic index
4896 nic_type - specify model name to which add to vm
4897 Returns:
4898 None
4899 """
kasar3ac5dc42017-03-15 06:28:22 -07004900
Ravi Chamarty5b2d5c12018-10-22 23:59:10 +00004901 self.logger.info("Add network adapter to VM: network_name {} nicIndex {} nic_type {}".\
4902 format(network_name, nicIndex, nic_type))
kasar4cb6e902017-03-18 00:17:27 -07004903 try:
kasard2963622017-03-31 05:53:17 -07004904 ip_address = None
kasardc1f02e2017-03-25 07:20:30 -07004905 floating_ip = False
kasarc5bf2932018-03-09 04:15:22 -08004906 mac_address = None
kasardc1f02e2017-03-25 07:20:30 -07004907 if 'floating_ip' in net: floating_ip = net['floating_ip']
kasard2963622017-03-31 05:53:17 -07004908
4909 # Stub for ip_address feature
4910 if 'ip_address' in net: ip_address = net['ip_address']
4911
kasarc5bf2932018-03-09 04:15:22 -08004912 if 'mac_address' in net: mac_address = net['mac_address']
4913
kasard2963622017-03-31 05:53:17 -07004914 if floating_ip:
4915 allocation_mode = "POOL"
4916 elif ip_address:
4917 allocation_mode = "MANUAL"
4918 else:
4919 allocation_mode = "DHCP"
kasardc1f02e2017-03-25 07:20:30 -07004920
kasar3ac5dc42017-03-15 06:28:22 -07004921 if not nic_type:
kasarc5bf2932018-03-09 04:15:22 -08004922 for vms in vapp.get_all_vms():
4923 vm_id = vms.get('id').split(':')[-1]
kasar3ac5dc42017-03-15 06:28:22 -07004924
kasarc5bf2932018-03-09 04:15:22 -08004925 url_rest_call = "{}/api/vApp/vm-{}/networkConnectionSection/".format(self.url, vm_id)
kasar3ac5dc42017-03-15 06:28:22 -07004926
kasarc5bf2932018-03-09 04:15:22 -08004927 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
sbhangarea8e5b782018-06-21 02:10:03 -07004928 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
kasarc5bf2932018-03-09 04:15:22 -08004929 response = self.perform_request(req_type='GET',
4930 url=url_rest_call,
4931 headers=headers)
bhangare1a0b97c2017-06-21 02:20:15 -07004932
4933 if response.status_code == 403:
4934 response = self.retry_rest('GET', url_rest_call)
4935
kasar3ac5dc42017-03-15 06:28:22 -07004936 if response.status_code != 200:
kasar4cb6e902017-03-18 00:17:27 -07004937 self.logger.error("REST call {} failed reason : {}"\
4938 "status code : {}".format(url_rest_call,
4939 response.content,
4940 response.status_code))
4941 raise vimconn.vimconnException("add_network_adapter_to_vms : Failed to get "\
4942 "network connection section")
kasar3ac5dc42017-03-15 06:28:22 -07004943
4944 data = response.content
kasarc5bf2932018-03-09 04:15:22 -08004945 data = data.split('<Link rel="edit"')[0]
kasar3ac5dc42017-03-15 06:28:22 -07004946 if '<PrimaryNetworkConnectionIndex>' not in data:
Ravi Chamarty5b2d5c12018-10-22 23:59:10 +00004947 self.logger.debug("add_network_adapter PrimaryNIC not in data")
kasar3ac5dc42017-03-15 06:28:22 -07004948 item = """<PrimaryNetworkConnectionIndex>{}</PrimaryNetworkConnectionIndex>
4949 <NetworkConnection network="{}">
4950 <NetworkConnectionIndex>{}</NetworkConnectionIndex>
4951 <IsConnected>true</IsConnected>
kasardc1f02e2017-03-25 07:20:30 -07004952 <IpAddressAllocationMode>{}</IpAddressAllocationMode>
4953 </NetworkConnection>""".format(primary_nic_index, network_name, nicIndex,
4954 allocation_mode)
kasard2963622017-03-31 05:53:17 -07004955 # Stub for ip_address feature
4956 if ip_address:
4957 ip_tag = '<IpAddress>{}</IpAddress>'.format(ip_address)
4958 item = item.replace('</NetworkConnectionIndex>\n','</NetworkConnectionIndex>\n{}\n'.format(ip_tag))
kasardc1f02e2017-03-25 07:20:30 -07004959
kasarc5bf2932018-03-09 04:15:22 -08004960 if mac_address:
4961 mac_tag = '<MACAddress>{}</MACAddress>'.format(mac_address)
4962 item = item.replace('</IsConnected>\n','</IsConnected>\n{}\n'.format(mac_tag))
4963
4964 data = data.replace('</ovf:Info>\n','</ovf:Info>\n{}\n</NetworkConnectionSection>'.format(item))
kasar3ac5dc42017-03-15 06:28:22 -07004965 else:
Ravi Chamarty5b2d5c12018-10-22 23:59:10 +00004966 self.logger.debug("add_network_adapter PrimaryNIC in data")
kasar3ac5dc42017-03-15 06:28:22 -07004967 new_item = """<NetworkConnection network="{}">
4968 <NetworkConnectionIndex>{}</NetworkConnectionIndex>
4969 <IsConnected>true</IsConnected>
kasardc1f02e2017-03-25 07:20:30 -07004970 <IpAddressAllocationMode>{}</IpAddressAllocationMode>
4971 </NetworkConnection>""".format(network_name, nicIndex,
4972 allocation_mode)
kasard2963622017-03-31 05:53:17 -07004973 # Stub for ip_address feature
4974 if ip_address:
4975 ip_tag = '<IpAddress>{}</IpAddress>'.format(ip_address)
4976 new_item = new_item.replace('</NetworkConnectionIndex>\n','</NetworkConnectionIndex>\n{}\n'.format(ip_tag))
kasardc1f02e2017-03-25 07:20:30 -07004977
kasarc5bf2932018-03-09 04:15:22 -08004978 if mac_address:
4979 mac_tag = '<MACAddress>{}</MACAddress>'.format(mac_address)
4980 new_item = new_item.replace('</IsConnected>\n','</IsConnected>\n{}\n'.format(mac_tag))
kasar3ac5dc42017-03-15 06:28:22 -07004981
kasarc5bf2932018-03-09 04:15:22 -08004982 data = data + new_item + '</NetworkConnectionSection>'
4983
kasar3ac5dc42017-03-15 06:28:22 -07004984 headers['Content-Type'] = 'application/vnd.vmware.vcloud.networkConnectionSection+xml'
kasarc5bf2932018-03-09 04:15:22 -08004985
4986 response = self.perform_request(req_type='PUT',
4987 url=url_rest_call,
4988 headers=headers,
4989 data=data)
bhangare1a0b97c2017-06-21 02:20:15 -07004990
4991 if response.status_code == 403:
4992 add_headers = {'Content-Type': headers['Content-Type']}
4993 response = self.retry_rest('PUT', url_rest_call, add_headers, data)
4994
kasar3ac5dc42017-03-15 06:28:22 -07004995 if response.status_code != 202:
kasar4cb6e902017-03-18 00:17:27 -07004996 self.logger.error("REST call {} failed reason : {}"\
4997 "status code : {} ".format(url_rest_call,
4998 response.content,
4999 response.status_code))
5000 raise vimconn.vimconnException("add_network_adapter_to_vms : Failed to update "\
5001 "network connection section")
kasar3ac5dc42017-03-15 06:28:22 -07005002 else:
kasarc5bf2932018-03-09 04:15:22 -08005003 nic_task = self.get_task_from_response(response.content)
sbhangarea8e5b782018-06-21 02:10:03 -07005004 result = self.client.get_task_monitor().wait_for_success(task=nic_task)
kasarc5bf2932018-03-09 04:15:22 -08005005 if result.get('status') == 'success':
kasar3ac5dc42017-03-15 06:28:22 -07005006 self.logger.info("add_network_adapter_to_vms(): VM {} conneced to "\
5007 "default NIC type".format(vm_id))
5008 else:
5009 self.logger.error("add_network_adapter_to_vms(): VM {} failed to "\
5010 "connect NIC type".format(vm_id))
5011 else:
kasarc5bf2932018-03-09 04:15:22 -08005012 for vms in vapp.get_all_vms():
5013 vm_id = vms.get('id').split(':')[-1]
kasar3ac5dc42017-03-15 06:28:22 -07005014
kasarc5bf2932018-03-09 04:15:22 -08005015 url_rest_call = "{}/api/vApp/vm-{}/networkConnectionSection/".format(self.url, vm_id)
5016
5017 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
5018 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
5019 response = self.perform_request(req_type='GET',
5020 url=url_rest_call,
5021 headers=headers)
bhangare1a0b97c2017-06-21 02:20:15 -07005022
5023 if response.status_code == 403:
5024 response = self.retry_rest('GET', url_rest_call)
5025
kasar3ac5dc42017-03-15 06:28:22 -07005026 if response.status_code != 200:
kasar4cb6e902017-03-18 00:17:27 -07005027 self.logger.error("REST call {} failed reason : {}"\
5028 "status code : {}".format(url_rest_call,
5029 response.content,
5030 response.status_code))
5031 raise vimconn.vimconnException("add_network_adapter_to_vms : Failed to get "\
5032 "network connection section")
kasar3ac5dc42017-03-15 06:28:22 -07005033 data = response.content
kasarc5bf2932018-03-09 04:15:22 -08005034 data = data.split('<Link rel="edit"')[0]
kasar3ac5dc42017-03-15 06:28:22 -07005035 if '<PrimaryNetworkConnectionIndex>' not in data:
Ravi Chamarty5b2d5c12018-10-22 23:59:10 +00005036 self.logger.debug("add_network_adapter PrimaryNIC not in data nic_type {}".format(nic_type))
kasar3ac5dc42017-03-15 06:28:22 -07005037 item = """<PrimaryNetworkConnectionIndex>{}</PrimaryNetworkConnectionIndex>
5038 <NetworkConnection network="{}">
5039 <NetworkConnectionIndex>{}</NetworkConnectionIndex>
5040 <IsConnected>true</IsConnected>
kasardc1f02e2017-03-25 07:20:30 -07005041 <IpAddressAllocationMode>{}</IpAddressAllocationMode>
kasar3ac5dc42017-03-15 06:28:22 -07005042 <NetworkAdapterType>{}</NetworkAdapterType>
kasardc1f02e2017-03-25 07:20:30 -07005043 </NetworkConnection>""".format(primary_nic_index, network_name, nicIndex,
5044 allocation_mode, nic_type)
kasard2963622017-03-31 05:53:17 -07005045 # Stub for ip_address feature
5046 if ip_address:
5047 ip_tag = '<IpAddress>{}</IpAddress>'.format(ip_address)
5048 item = item.replace('</NetworkConnectionIndex>\n','</NetworkConnectionIndex>\n{}\n'.format(ip_tag))
kasardc1f02e2017-03-25 07:20:30 -07005049
kasarc5bf2932018-03-09 04:15:22 -08005050 if mac_address:
5051 mac_tag = '<MACAddress>{}</MACAddress>'.format(mac_address)
sbhangarea8e5b782018-06-21 02:10:03 -07005052 item = item.replace('</IsConnected>\n','</IsConnected>\n{}\n'.format(mac_tag))
kasarc5bf2932018-03-09 04:15:22 -08005053
5054 data = data.replace('</ovf:Info>\n','</ovf:Info>\n{}\n</NetworkConnectionSection>'.format(item))
kasar3ac5dc42017-03-15 06:28:22 -07005055 else:
Ravi Chamarty5b2d5c12018-10-22 23:59:10 +00005056 self.logger.debug("add_network_adapter PrimaryNIC in data nic_type {}".format(nic_type))
kasar3ac5dc42017-03-15 06:28:22 -07005057 new_item = """<NetworkConnection network="{}">
5058 <NetworkConnectionIndex>{}</NetworkConnectionIndex>
5059 <IsConnected>true</IsConnected>
kasardc1f02e2017-03-25 07:20:30 -07005060 <IpAddressAllocationMode>{}</IpAddressAllocationMode>
kasar3ac5dc42017-03-15 06:28:22 -07005061 <NetworkAdapterType>{}</NetworkAdapterType>
kasardc1f02e2017-03-25 07:20:30 -07005062 </NetworkConnection>""".format(network_name, nicIndex,
5063 allocation_mode, nic_type)
kasard2963622017-03-31 05:53:17 -07005064 # Stub for ip_address feature
5065 if ip_address:
5066 ip_tag = '<IpAddress>{}</IpAddress>'.format(ip_address)
5067 new_item = new_item.replace('</NetworkConnectionIndex>\n','</NetworkConnectionIndex>\n{}\n'.format(ip_tag))
kasardc1f02e2017-03-25 07:20:30 -07005068
kasarc5bf2932018-03-09 04:15:22 -08005069 if mac_address:
5070 mac_tag = '<MACAddress>{}</MACAddress>'.format(mac_address)
5071 new_item = new_item.replace('</IsConnected>\n','</IsConnected>\n{}\n'.format(mac_tag))
kasar3ac5dc42017-03-15 06:28:22 -07005072
kasarc5bf2932018-03-09 04:15:22 -08005073 data = data + new_item + '</NetworkConnectionSection>'
5074
kasar3ac5dc42017-03-15 06:28:22 -07005075 headers['Content-Type'] = 'application/vnd.vmware.vcloud.networkConnectionSection+xml'
kasarc5bf2932018-03-09 04:15:22 -08005076
5077 response = self.perform_request(req_type='PUT',
5078 url=url_rest_call,
5079 headers=headers,
5080 data=data)
bhangare1a0b97c2017-06-21 02:20:15 -07005081
5082 if response.status_code == 403:
5083 add_headers = {'Content-Type': headers['Content-Type']}
5084 response = self.retry_rest('PUT', url_rest_call, add_headers, data)
kasar3ac5dc42017-03-15 06:28:22 -07005085
5086 if response.status_code != 202:
kasar4cb6e902017-03-18 00:17:27 -07005087 self.logger.error("REST call {} failed reason : {}"\
5088 "status code : {}".format(url_rest_call,
5089 response.content,
5090 response.status_code))
5091 raise vimconn.vimconnException("add_network_adapter_to_vms : Failed to update "\
5092 "network connection section")
kasar3ac5dc42017-03-15 06:28:22 -07005093 else:
kasarc5bf2932018-03-09 04:15:22 -08005094 nic_task = self.get_task_from_response(response.content)
5095 result = self.client.get_task_monitor().wait_for_success(task=nic_task)
5096 if result.get('status') == 'success':
kasar3ac5dc42017-03-15 06:28:22 -07005097 self.logger.info("add_network_adapter_to_vms(): VM {} "\
5098 "conneced to NIC type {}".format(vm_id, nic_type))
5099 else:
5100 self.logger.error("add_network_adapter_to_vms(): VM {} "\
5101 "failed to connect NIC type {}".format(vm_id, nic_type))
5102 except Exception as exp:
kasar4cb6e902017-03-18 00:17:27 -07005103 self.logger.error("add_network_adapter_to_vms() : exception occurred "\
5104 "while adding Network adapter")
5105 raise vimconn.vimconnException(message=exp)
kasarde691232017-03-25 03:37:31 -07005106
5107
5108 def set_numa_affinity(self, vmuuid, paired_threads_id):
5109 """
5110 Method to assign numa affinity in vm configuration parammeters
5111 Args :
5112 vmuuid - vm uuid
5113 paired_threads_id - one or more virtual processor
5114 numbers
5115 Returns:
5116 return if True
5117 """
5118 try:
kasar204e39e2018-01-25 00:57:02 -08005119 vcenter_conect, content = self.get_vcenter_content()
5120 vm_moref_id = self.get_vm_moref_id(vmuuid)
kasarde691232017-03-25 03:37:31 -07005121
kasar204e39e2018-01-25 00:57:02 -08005122 host_obj, vm_obj = self.get_vm_obj(content ,vm_moref_id)
5123 if vm_obj:
5124 config_spec = vim.vm.ConfigSpec()
5125 config_spec.extraConfig = []
5126 opt = vim.option.OptionValue()
5127 opt.key = 'numa.nodeAffinity'
5128 opt.value = str(paired_threads_id)
5129 config_spec.extraConfig.append(opt)
5130 task = vm_obj.ReconfigVM_Task(config_spec)
5131 if task:
5132 result = self.wait_for_vcenter_task(task, vcenter_conect)
5133 extra_config = vm_obj.config.extraConfig
5134 flag = False
5135 for opts in extra_config:
5136 if 'numa.nodeAffinity' in opts.key:
5137 flag = True
5138 self.logger.info("set_numa_affinity: Sucessfully assign numa affinity "\
5139 "value {} for vm {}".format(opt.value, vm_obj))
5140 if flag:
5141 return
5142 else:
5143 self.logger.error("set_numa_affinity: Failed to assign numa affinity")
kasarde691232017-03-25 03:37:31 -07005144 except Exception as exp:
5145 self.logger.error("set_numa_affinity : exception occurred while setting numa affinity "\
5146 "for VM {} : {}".format(vm_obj, vm_moref_id))
5147 raise vimconn.vimconnException("set_numa_affinity : Error {} failed to assign numa "\
5148 "affinity".format(exp))
kasardc1f02e2017-03-25 07:20:30 -07005149
5150
5151 def cloud_init(self, vapp, cloud_config):
5152 """
5153 Method to inject ssh-key
5154 vapp - vapp object
5155 cloud_config a dictionary with:
5156 'key-pairs': (optional) list of strings with the public key to be inserted to the default user
5157 'users': (optional) list of users to be inserted, each item is a dict with:
5158 'name': (mandatory) user name,
5159 'key-pairs': (optional) list of strings with the public key to be inserted to the user
tierno40e1bce2017-08-09 09:12:04 +02005160 'user-data': (optional) can be a string with the text script to be passed directly to cloud-init,
5161 or a list of strings, each one contains a script to be passed, usually with a MIMEmultipart file
kasardc1f02e2017-03-25 07:20:30 -07005162 'config-files': (optional). List of files to be transferred. Each item is a dict with:
5163 'dest': (mandatory) string with the destination absolute path
5164 'encoding': (optional, by default text). Can be one of:
5165 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
5166 'content' (mandatory): string with the content of the file
5167 'permissions': (optional) string with file permissions, typically octal notation '0644'
5168 'owner': (optional) file owner, string with the format 'owner:group'
5169 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk
5170 """
kasardc1f02e2017-03-25 07:20:30 -07005171 try:
kasar2aa50742017-08-08 02:11:22 -07005172 if not isinstance(cloud_config, dict):
5173 raise Exception("cloud_init : parameter cloud_config is not a dictionary")
5174 else:
kasardc1f02e2017-03-25 07:20:30 -07005175 key_pairs = []
5176 userdata = []
5177 if "key-pairs" in cloud_config:
5178 key_pairs = cloud_config["key-pairs"]
5179
5180 if "users" in cloud_config:
5181 userdata = cloud_config["users"]
5182
kasar2aa50742017-08-08 02:11:22 -07005183 self.logger.debug("cloud_init : Guest os customization started..")
5184 customize_script = self.format_script(key_pairs=key_pairs, users_list=userdata)
kasarc5bf2932018-03-09 04:15:22 -08005185 customize_script = customize_script.replace("&","&amp;")
kasar2aa50742017-08-08 02:11:22 -07005186 self.guest_customization(vapp, customize_script)
kasardc1f02e2017-03-25 07:20:30 -07005187
kasardc1f02e2017-03-25 07:20:30 -07005188 except Exception as exp:
5189 self.logger.error("cloud_init : exception occurred while injecting "\
5190 "ssh-key")
5191 raise vimconn.vimconnException("cloud_init : Error {} failed to inject "\
5192 "ssh-key".format(exp))
bhangare06312472017-03-30 05:49:07 -07005193
kasar2aa50742017-08-08 02:11:22 -07005194 def format_script(self, key_pairs=[], users_list=[]):
kasarc5bf2932018-03-09 04:15:22 -08005195 bash_script = """#!/bin/sh
kasar2aa50742017-08-08 02:11:22 -07005196 echo performing customization tasks with param $1 at `date "+DATE: %Y-%m-%d - TIME: %H:%M:%S"` >> /root/customization.log
5197 if [ "$1" = "precustomization" ];then
5198 echo performing precustomization tasks on `date "+DATE: %Y-%m-%d - TIME: %H:%M:%S"` >> /root/customization.log
5199 """
5200
5201 keys = "\n".join(key_pairs)
5202 if keys:
5203 keys_data = """
5204 if [ ! -d /root/.ssh ];then
5205 mkdir /root/.ssh
5206 chown root:root /root/.ssh
5207 chmod 700 /root/.ssh
5208 touch /root/.ssh/authorized_keys
5209 chown root:root /root/.ssh/authorized_keys
5210 chmod 600 /root/.ssh/authorized_keys
5211 # make centos with selinux happy
5212 which restorecon && restorecon -Rv /root/.ssh
5213 else
5214 touch /root/.ssh/authorized_keys
5215 chown root:root /root/.ssh/authorized_keys
5216 chmod 600 /root/.ssh/authorized_keys
5217 fi
5218 echo '{key}' >> /root/.ssh/authorized_keys
5219 """.format(key=keys)
5220
5221 bash_script+= keys_data
5222
5223 for user in users_list:
5224 if 'name' in user: user_name = user['name']
5225 if 'key-pairs' in user:
5226 user_keys = "\n".join(user['key-pairs'])
5227 else:
5228 user_keys = None
5229
5230 add_user_name = """
5231 useradd -d /home/{user_name} -m -g users -s /bin/bash {user_name}
5232 """.format(user_name=user_name)
5233
5234 bash_script+= add_user_name
5235
5236 if user_keys:
5237 user_keys_data = """
5238 mkdir /home/{user_name}/.ssh
5239 chown {user_name}:{user_name} /home/{user_name}/.ssh
5240 chmod 700 /home/{user_name}/.ssh
5241 touch /home/{user_name}/.ssh/authorized_keys
5242 chown {user_name}:{user_name} /home/{user_name}/.ssh/authorized_keys
5243 chmod 600 /home/{user_name}/.ssh/authorized_keys
5244 # make centos with selinux happy
5245 which restorecon && restorecon -Rv /home/{user_name}/.ssh
5246 echo '{user_key}' >> /home/{user_name}/.ssh/authorized_keys
5247 """.format(user_name=user_name,user_key=user_keys)
5248
5249 bash_script+= user_keys_data
5250
5251 return bash_script+"\n\tfi"
5252
5253 def guest_customization(self, vapp, customize_script):
5254 """
5255 Method to customize guest os
5256 vapp - Vapp object
5257 customize_script - Customize script to be run at first boot of VM.
5258 """
kasarc5bf2932018-03-09 04:15:22 -08005259 for vm in vapp.get_all_vms():
5260 vm_id = vm.get('id').split(':')[-1]
sbhangarea8e5b782018-06-21 02:10:03 -07005261 vm_name = vm.get('name')
5262 vm_name = vm_name.replace('_','-')
5263
kasarc5bf2932018-03-09 04:15:22 -08005264 vm_customization_url = "{}/api/vApp/vm-{}/guestCustomizationSection/".format(self.url, vm_id)
5265 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
5266 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
5267
5268 headers['Content-Type'] = "application/vnd.vmware.vcloud.guestCustomizationSection+xml"
5269
5270 data = """<GuestCustomizationSection
5271 xmlns="http://www.vmware.com/vcloud/v1.5"
5272 xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1"
5273 ovf:required="false" href="{}" type="application/vnd.vmware.vcloud.guestCustomizationSection+xml">
5274 <ovf:Info>Specifies Guest OS Customization Settings</ovf:Info>
5275 <Enabled>true</Enabled>
5276 <ChangeSid>false</ChangeSid>
5277 <VirtualMachineId>{}</VirtualMachineId>
5278 <JoinDomainEnabled>false</JoinDomainEnabled>
5279 <UseOrgSettings>false</UseOrgSettings>
5280 <AdminPasswordEnabled>false</AdminPasswordEnabled>
5281 <AdminPasswordAuto>true</AdminPasswordAuto>
5282 <AdminAutoLogonEnabled>false</AdminAutoLogonEnabled>
5283 <AdminAutoLogonCount>0</AdminAutoLogonCount>
5284 <ResetPasswordRequired>false</ResetPasswordRequired>
5285 <CustomizationScript>{}</CustomizationScript>
5286 <ComputerName>{}</ComputerName>
5287 <Link href="{}" type="application/vnd.vmware.vcloud.guestCustomizationSection+xml" rel="edit"/>
sbhangarea8e5b782018-06-21 02:10:03 -07005288 </GuestCustomizationSection>
kasarc5bf2932018-03-09 04:15:22 -08005289 """.format(vm_customization_url,
5290 vm_id,
5291 customize_script,
5292 vm_name,
sbhangarea8e5b782018-06-21 02:10:03 -07005293 vm_customization_url)
kasarc5bf2932018-03-09 04:15:22 -08005294
5295 response = self.perform_request(req_type='PUT',
5296 url=vm_customization_url,
5297 headers=headers,
5298 data=data)
5299 if response.status_code == 202:
5300 guest_task = self.get_task_from_response(response.content)
5301 self.client.get_task_monitor().wait_for_success(task=guest_task)
kasar2aa50742017-08-08 02:11:22 -07005302 self.logger.info("guest_customization : customized guest os task "\
5303 "completed for VM {}".format(vm_name))
5304 else:
5305 self.logger.error("guest_customization : task for customized guest os"\
5306 "failed for VM {}".format(vm_name))
5307 raise vimconn.vimconnException("guest_customization : failed to perform"\
5308 "guest os customization on VM {}".format(vm_name))
bhangare06312472017-03-30 05:49:07 -07005309
bhangare1a0b97c2017-06-21 02:20:15 -07005310 def add_new_disk(self, vapp_uuid, disk_size):
bhangare06312472017-03-30 05:49:07 -07005311 """
5312 Method to create an empty vm disk
5313
5314 Args:
5315 vapp_uuid - is vapp identifier.
5316 disk_size - size of disk to be created in GB
5317
5318 Returns:
5319 None
5320 """
5321 status = False
5322 vm_details = None
5323 try:
5324 #Disk size in GB, convert it into MB
5325 if disk_size is not None:
5326 disk_size_mb = int(disk_size) * 1024
5327 vm_details = self.get_vapp_details_rest(vapp_uuid)
5328
5329 if vm_details and "vm_virtual_hardware" in vm_details:
5330 self.logger.info("Adding disk to VM: {} disk size:{}GB".format(vm_details["name"], disk_size))
5331 disk_href = vm_details["vm_virtual_hardware"]["disk_edit_href"]
bhangare1a0b97c2017-06-21 02:20:15 -07005332 status = self.add_new_disk_rest(disk_href, disk_size_mb)
bhangare06312472017-03-30 05:49:07 -07005333
5334 except Exception as exp:
5335 msg = "Error occurred while creating new disk {}.".format(exp)
5336 self.rollback_newvm(vapp_uuid, msg)
5337
5338 if status:
5339 self.logger.info("Added new disk to VM: {} disk size:{}GB".format(vm_details["name"], disk_size))
5340 else:
5341 #If failed to add disk, delete VM
5342 msg = "add_new_disk: Failed to add new disk to {}".format(vm_details["name"])
5343 self.rollback_newvm(vapp_uuid, msg)
5344
5345
bhangare1a0b97c2017-06-21 02:20:15 -07005346 def add_new_disk_rest(self, disk_href, disk_size_mb):
bhangare06312472017-03-30 05:49:07 -07005347 """
5348 Retrives vApp Disks section & add new empty disk
5349
5350 Args:
5351 disk_href: Disk section href to addd disk
5352 disk_size_mb: Disk size in MB
5353
5354 Returns: Status of add new disk task
5355 """
5356 status = False
kasarc5bf2932018-03-09 04:15:22 -08005357 if self.client._session:
5358 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
sbhangarea8e5b782018-06-21 02:10:03 -07005359 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
kasarc5bf2932018-03-09 04:15:22 -08005360 response = self.perform_request(req_type='GET',
5361 url=disk_href,
5362 headers=headers)
bhangare1a0b97c2017-06-21 02:20:15 -07005363
5364 if response.status_code == 403:
5365 response = self.retry_rest('GET', disk_href)
bhangare06312472017-03-30 05:49:07 -07005366
5367 if response.status_code != requests.codes.ok:
5368 self.logger.error("add_new_disk_rest: GET REST API call {} failed. Return status code {}"
5369 .format(disk_href, response.status_code))
5370 return status
5371 try:
5372 #Find but type & max of instance IDs assigned to disks
5373 lxmlroot_respond = lxmlElementTree.fromstring(response.content)
5374 namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.iteritems() if prefix}
kasarc5bf2932018-03-09 04:15:22 -08005375 #For python3
5376 #namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.items() if prefix}
bhangare06312472017-03-30 05:49:07 -07005377 namespaces["xmlns"]= "http://www.vmware.com/vcloud/v1.5"
5378 instance_id = 0
5379 for item in lxmlroot_respond.iterfind('xmlns:Item',namespaces):
5380 if item.find("rasd:Description",namespaces).text == "Hard disk":
5381 inst_id = int(item.find("rasd:InstanceID" ,namespaces).text)
5382 if inst_id > instance_id:
5383 instance_id = inst_id
5384 disk_item = item.find("rasd:HostResource" ,namespaces)
5385 bus_subtype = disk_item.attrib["{"+namespaces['xmlns']+"}busSubType"]
5386 bus_type = disk_item.attrib["{"+namespaces['xmlns']+"}busType"]
5387
5388 instance_id = instance_id + 1
5389 new_item = """<Item>
5390 <rasd:Description>Hard disk</rasd:Description>
5391 <rasd:ElementName>New disk</rasd:ElementName>
5392 <rasd:HostResource
5393 xmlns:vcloud="http://www.vmware.com/vcloud/v1.5"
5394 vcloud:capacity="{}"
5395 vcloud:busSubType="{}"
5396 vcloud:busType="{}"></rasd:HostResource>
5397 <rasd:InstanceID>{}</rasd:InstanceID>
5398 <rasd:ResourceType>17</rasd:ResourceType>
5399 </Item>""".format(disk_size_mb, bus_subtype, bus_type, instance_id)
5400
5401 new_data = response.content
5402 #Add new item at the bottom
5403 new_data = new_data.replace('</Item>\n</RasdItemsList>', '</Item>\n{}\n</RasdItemsList>'.format(new_item))
5404
5405 # Send PUT request to modify virtual hardware section with new disk
bhangare06312472017-03-30 05:49:07 -07005406 headers['Content-Type'] = 'application/vnd.vmware.vcloud.rasdItemsList+xml; charset=ISO-8859-1'
5407
kasarc5bf2932018-03-09 04:15:22 -08005408 response = self.perform_request(req_type='PUT',
5409 url=disk_href,
5410 data=new_data,
5411 headers=headers)
bhangare1a0b97c2017-06-21 02:20:15 -07005412
5413 if response.status_code == 403:
5414 add_headers = {'Content-Type': headers['Content-Type']}
5415 response = self.retry_rest('PUT', disk_href, add_headers, new_data)
bhangare06312472017-03-30 05:49:07 -07005416
5417 if response.status_code != 202:
5418 self.logger.error("PUT REST API call {} failed. Return status code {}. Response Content:{}"
5419 .format(disk_href, response.status_code, response.content))
5420 else:
kasarc5bf2932018-03-09 04:15:22 -08005421 add_disk_task = self.get_task_from_response(response.content)
5422 result = self.client.get_task_monitor().wait_for_success(task=add_disk_task)
sbhangarea8e5b782018-06-21 02:10:03 -07005423 if result.get('status') == 'success':
kasarc5bf2932018-03-09 04:15:22 -08005424 status = True
5425 else:
sbhangarea8e5b782018-06-21 02:10:03 -07005426 self.logger.error("Add new disk REST task failed to add {} MB disk".format(disk_size_mb))
bhangare06312472017-03-30 05:49:07 -07005427
5428 except Exception as exp:
5429 self.logger.error("Error occurred calling rest api for creating new disk {}".format(exp))
5430
5431 return status
5432
5433
5434 def add_existing_disk(self, catalogs=None, image_id=None, size=None, template_name=None, vapp_uuid=None):
5435 """
5436 Method to add existing disk to vm
5437 Args :
5438 catalogs - List of VDC catalogs
5439 image_id - Catalog ID
5440 template_name - Name of template in catalog
5441 vapp_uuid - UUID of vApp
5442 Returns:
5443 None
5444 """
5445 disk_info = None
5446 vcenter_conect, content = self.get_vcenter_content()
5447 #find moref-id of vm in image
5448 catalog_vm_info = self.get_vapp_template_details(catalogs=catalogs,
5449 image_id=image_id,
5450 )
5451
5452 if catalog_vm_info and "vm_vcenter_info" in catalog_vm_info:
5453 if "vm_moref_id" in catalog_vm_info["vm_vcenter_info"]:
5454 catalog_vm_moref_id = catalog_vm_info["vm_vcenter_info"].get("vm_moref_id", None)
5455 if catalog_vm_moref_id:
5456 self.logger.info("Moref_id of VM in catalog : {}" .format(catalog_vm_moref_id))
5457 host, catalog_vm_obj = self.get_vm_obj(content, catalog_vm_moref_id)
5458 if catalog_vm_obj:
5459 #find existing disk
5460 disk_info = self.find_disk(catalog_vm_obj)
5461 else:
5462 exp_msg = "No VM with image id {} found".format(image_id)
5463 self.rollback_newvm(vapp_uuid, exp_msg, exp_type="NotFound")
5464 else:
5465 exp_msg = "No Image found with image ID {} ".format(image_id)
5466 self.rollback_newvm(vapp_uuid, exp_msg, exp_type="NotFound")
5467
5468 if disk_info:
5469 self.logger.info("Existing disk_info : {}".format(disk_info))
5470 #get VM
5471 vm_moref_id = self.get_vm_moref_id(vapp_uuid)
5472 host, vm_obj = self.get_vm_obj(content, vm_moref_id)
5473 if vm_obj:
5474 status = self.add_disk(vcenter_conect=vcenter_conect,
5475 vm=vm_obj,
5476 disk_info=disk_info,
5477 size=size,
5478 vapp_uuid=vapp_uuid
5479 )
5480 if status:
5481 self.logger.info("Disk from image id {} added to {}".format(image_id,
5482 vm_obj.config.name)
5483 )
5484 else:
5485 msg = "No disk found with image id {} to add in VM {}".format(
5486 image_id,
5487 vm_obj.config.name)
5488 self.rollback_newvm(vapp_uuid, msg, exp_type="NotFound")
5489
5490
5491 def find_disk(self, vm_obj):
5492 """
5493 Method to find details of existing disk in VM
5494 Args :
5495 vm_obj - vCenter object of VM
5496 image_id - Catalog ID
5497 Returns:
5498 disk_info : dict of disk details
5499 """
5500 disk_info = {}
5501 if vm_obj:
5502 try:
5503 devices = vm_obj.config.hardware.device
5504 for device in devices:
5505 if type(device) is vim.vm.device.VirtualDisk:
5506 if isinstance(device.backing,vim.vm.device.VirtualDisk.FlatVer2BackingInfo) and hasattr(device.backing, 'fileName'):
5507 disk_info["full_path"] = device.backing.fileName
5508 disk_info["datastore"] = device.backing.datastore
5509 disk_info["capacityKB"] = device.capacityInKB
5510 break
5511 except Exception as exp:
5512 self.logger.error("find_disk() : exception occurred while "\
5513 "getting existing disk details :{}".format(exp))
5514 return disk_info
5515
5516
5517 def add_disk(self, vcenter_conect=None, vm=None, size=None, vapp_uuid=None, disk_info={}):
5518 """
5519 Method to add existing disk in VM
5520 Args :
5521 vcenter_conect - vCenter content object
5522 vm - vCenter vm object
5523 disk_info : dict of disk details
5524 Returns:
5525 status : status of add disk task
5526 """
5527 datastore = disk_info["datastore"] if "datastore" in disk_info else None
5528 fullpath = disk_info["full_path"] if "full_path" in disk_info else None
5529 capacityKB = disk_info["capacityKB"] if "capacityKB" in disk_info else None
5530 if size is not None:
5531 #Convert size from GB to KB
5532 sizeKB = int(size) * 1024 * 1024
5533 #compare size of existing disk and user given size.Assign whicherver is greater
5534 self.logger.info("Add Existing disk : sizeKB {} , capacityKB {}".format(
5535 sizeKB, capacityKB))
5536 if sizeKB > capacityKB:
5537 capacityKB = sizeKB
5538
5539 if datastore and fullpath and capacityKB:
5540 try:
5541 spec = vim.vm.ConfigSpec()
5542 # get all disks on a VM, set unit_number to the next available
5543 unit_number = 0
5544 for dev in vm.config.hardware.device:
5545 if hasattr(dev.backing, 'fileName'):
5546 unit_number = int(dev.unitNumber) + 1
5547 # unit_number 7 reserved for scsi controller
5548 if unit_number == 7:
5549 unit_number += 1
5550 if isinstance(dev, vim.vm.device.VirtualDisk):
5551 #vim.vm.device.VirtualSCSIController
5552 controller_key = dev.controllerKey
5553
5554 self.logger.info("Add Existing disk : unit number {} , controller key {}".format(
5555 unit_number, controller_key))
5556 # add disk here
5557 dev_changes = []
5558 disk_spec = vim.vm.device.VirtualDeviceSpec()
5559 disk_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add
5560 disk_spec.device = vim.vm.device.VirtualDisk()
5561 disk_spec.device.backing = \
5562 vim.vm.device.VirtualDisk.FlatVer2BackingInfo()
5563 disk_spec.device.backing.thinProvisioned = True
5564 disk_spec.device.backing.diskMode = 'persistent'
5565 disk_spec.device.backing.datastore = datastore
5566 disk_spec.device.backing.fileName = fullpath
5567
5568 disk_spec.device.unitNumber = unit_number
5569 disk_spec.device.capacityInKB = capacityKB
5570 disk_spec.device.controllerKey = controller_key
5571 dev_changes.append(disk_spec)
5572 spec.deviceChange = dev_changes
5573 task = vm.ReconfigVM_Task(spec=spec)
5574 status = self.wait_for_vcenter_task(task, vcenter_conect)
5575 return status
5576 except Exception as exp:
5577 exp_msg = "add_disk() : exception {} occurred while adding disk "\
5578 "{} to vm {}".format(exp,
5579 fullpath,
5580 vm.config.name)
5581 self.rollback_newvm(vapp_uuid, exp_msg)
5582 else:
5583 msg = "add_disk() : Can not add disk to VM with disk info {} ".format(disk_info)
5584 self.rollback_newvm(vapp_uuid, msg)
5585
5586
5587 def get_vcenter_content(self):
5588 """
5589 Get the vsphere content object
5590 """
5591 try:
5592 vm_vcenter_info = self.get_vm_vcenter_info()
5593 except Exception as exp:
5594 self.logger.error("Error occurred while getting vCenter infromationn"\
5595 " for VM : {}".format(exp))
5596 raise vimconn.vimconnException(message=exp)
5597
5598 context = None
5599 if hasattr(ssl, '_create_unverified_context'):
5600 context = ssl._create_unverified_context()
5601
5602 vcenter_conect = SmartConnect(
5603 host=vm_vcenter_info["vm_vcenter_ip"],
5604 user=vm_vcenter_info["vm_vcenter_user"],
5605 pwd=vm_vcenter_info["vm_vcenter_password"],
5606 port=int(vm_vcenter_info["vm_vcenter_port"]),
5607 sslContext=context
5608 )
5609 atexit.register(Disconnect, vcenter_conect)
5610 content = vcenter_conect.RetrieveContent()
5611 return vcenter_conect, content
5612
5613
5614 def get_vm_moref_id(self, vapp_uuid):
5615 """
5616 Get the moref_id of given VM
5617 """
5618 try:
5619 if vapp_uuid:
5620 vm_details = self.get_vapp_details_rest(vapp_uuid, need_admin_access=True)
5621 if vm_details and "vm_vcenter_info" in vm_details:
5622 vm_moref_id = vm_details["vm_vcenter_info"].get("vm_moref_id", None)
bhangare06312472017-03-30 05:49:07 -07005623 return vm_moref_id
5624
5625 except Exception as exp:
5626 self.logger.error("Error occurred while getting VM moref ID "\
5627 " for VM : {}".format(exp))
5628 return None
5629
5630
5631 def get_vapp_template_details(self, catalogs=None, image_id=None , template_name=None):
5632 """
5633 Method to get vApp template details
5634 Args :
5635 catalogs - list of VDC catalogs
5636 image_id - Catalog ID to find
5637 template_name : template name in catalog
5638 Returns:
5639 parsed_respond : dict of vApp tempalte details
5640 """
5641 parsed_response = {}
5642
5643 vca = self.connect_as_admin()
5644 if not vca:
kasarc5bf2932018-03-09 04:15:22 -08005645 raise vimconn.vimconnConnectionException("Failed to connect vCD")
bhangare06312472017-03-30 05:49:07 -07005646
5647 try:
kasarc5bf2932018-03-09 04:15:22 -08005648 org, vdc = self.get_vdc_details()
bhangare06312472017-03-30 05:49:07 -07005649 catalog = self.get_catalog_obj(image_id, catalogs)
5650 if catalog:
kasarc5bf2932018-03-09 04:15:22 -08005651 items = org.get_catalog_item(catalog.get('name'), catalog.get('name'))
5652 catalog_items = [items.attrib]
5653
bhangare06312472017-03-30 05:49:07 -07005654 if len(catalog_items) == 1:
kasarc5bf2932018-03-09 04:15:22 -08005655 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
sbhangarea8e5b782018-06-21 02:10:03 -07005656 'x-vcloud-authorization': vca._session.headers['x-vcloud-authorization']}
kasarc5bf2932018-03-09 04:15:22 -08005657
5658 response = self.perform_request(req_type='GET',
5659 url=catalog_items[0].get('href'),
5660 headers=headers)
bhangare06312472017-03-30 05:49:07 -07005661 catalogItem = XmlElementTree.fromstring(response.content)
5662 entity = [child for child in catalogItem if child.get("type") == "application/vnd.vmware.vcloud.vAppTemplate+xml"][0]
5663 vapp_tempalte_href = entity.get("href")
5664 #get vapp details and parse moref id
5665
5666 namespaces = {"vssd":"http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData" ,
5667 'ovf': 'http://schemas.dmtf.org/ovf/envelope/1',
5668 'vmw': 'http://www.vmware.com/schema/ovf',
5669 'vm': 'http://www.vmware.com/vcloud/v1.5',
5670 'rasd':"http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData",
5671 'vmext':"http://www.vmware.com/vcloud/extension/v1.5",
5672 'xmlns':"http://www.vmware.com/vcloud/v1.5"
5673 }
5674
kasarc5bf2932018-03-09 04:15:22 -08005675 if vca._session:
5676 response = self.perform_request(req_type='GET',
5677 url=vapp_tempalte_href,
5678 headers=headers)
bhangare06312472017-03-30 05:49:07 -07005679
5680 if response.status_code != requests.codes.ok:
5681 self.logger.debug("REST API call {} failed. Return status code {}".format(
5682 vapp_tempalte_href, response.status_code))
5683
5684 else:
5685 xmlroot_respond = XmlElementTree.fromstring(response.content)
5686 children_section = xmlroot_respond.find('vm:Children/', namespaces)
5687 if children_section is not None:
5688 vCloud_extension_section = children_section.find('xmlns:VCloudExtension', namespaces)
5689 if vCloud_extension_section is not None:
5690 vm_vcenter_info = {}
5691 vim_info = vCloud_extension_section.find('vmext:VmVimInfo', namespaces)
5692 vmext = vim_info.find('vmext:VmVimObjectRef', namespaces)
5693 if vmext is not None:
5694 vm_vcenter_info["vm_moref_id"] = vmext.find('vmext:MoRef', namespaces).text
5695 parsed_response["vm_vcenter_info"]= vm_vcenter_info
5696
5697 except Exception as exp :
5698 self.logger.info("Error occurred calling rest api for getting vApp details {}".format(exp))
5699
5700 return parsed_response
5701
5702
5703 def rollback_newvm(self, vapp_uuid, msg , exp_type="Genric"):
5704 """
5705 Method to delete vApp
5706 Args :
5707 vapp_uuid - vApp UUID
5708 msg - Error message to be logged
5709 exp_type : Exception type
5710 Returns:
5711 None
5712 """
5713 if vapp_uuid:
5714 status = self.delete_vminstance(vapp_uuid)
5715 else:
5716 msg = "No vApp ID"
5717 self.logger.error(msg)
5718 if exp_type == "Genric":
5719 raise vimconn.vimconnException(msg)
5720 elif exp_type == "NotFound":
5721 raise vimconn.vimconnNotFoundException(message=msg)
5722
kateac1e3792017-04-01 02:16:39 -07005723 def add_sriov(self, vapp_uuid, sriov_nets, vmname_andid):
5724 """
5725 Method to attach SRIOV adapters to VM
5726
5727 Args:
5728 vapp_uuid - uuid of vApp/VM
5729 sriov_nets - SRIOV devices infromation as specified in VNFD (flavor)
5730 vmname_andid - vmname
5731
5732 Returns:
5733 The status of add SRIOV adapter task , vm object and
5734 vcenter_conect object
5735 """
5736 vm_obj = None
5737 vcenter_conect, content = self.get_vcenter_content()
5738 vm_moref_id = self.get_vm_moref_id(vapp_uuid)
5739
5740 if vm_moref_id:
5741 try:
5742 no_of_sriov_devices = len(sriov_nets)
5743 if no_of_sriov_devices > 0:
5744 #Get VM and its host
5745 host_obj, vm_obj = self.get_vm_obj(content, vm_moref_id)
5746 self.logger.info("VM {} is currently on host {}".format(vm_obj, host_obj))
5747 if host_obj and vm_obj:
5748 #get SRIOV devies from host on which vapp is currently installed
5749 avilable_sriov_devices = self.get_sriov_devices(host_obj,
5750 no_of_sriov_devices,
5751 )
5752
5753 if len(avilable_sriov_devices) == 0:
5754 #find other hosts with active pci devices
5755 new_host_obj , avilable_sriov_devices = self.get_host_and_sriov_devices(
5756 content,
5757 no_of_sriov_devices,
5758 )
5759
5760 if new_host_obj is not None and len(avilable_sriov_devices)> 0:
5761 #Migrate vm to the host where SRIOV devices are available
5762 self.logger.info("Relocate VM {} on new host {}".format(vm_obj,
5763 new_host_obj))
5764 task = self.relocate_vm(new_host_obj, vm_obj)
5765 if task is not None:
5766 result = self.wait_for_vcenter_task(task, vcenter_conect)
5767 self.logger.info("Migrate VM status: {}".format(result))
5768 host_obj = new_host_obj
5769 else:
5770 self.logger.info("Fail to migrate VM : {}".format(result))
5771 raise vimconn.vimconnNotFoundException(
5772 "Fail to migrate VM : {} to host {}".format(
5773 vmname_andid,
5774 new_host_obj)
5775 )
5776
5777 if host_obj is not None and avilable_sriov_devices is not None and len(avilable_sriov_devices)> 0:
5778 #Add SRIOV devices one by one
5779 for sriov_net in sriov_nets:
5780 network_name = sriov_net.get('net_id')
5781 dvs_portgr_name = self.create_dvPort_group(network_name)
tierno66eba6e2017-11-10 17:09:18 +01005782 if sriov_net.get('type') == "VF" or sriov_net.get('type') == "SR-IOV":
kateac1e3792017-04-01 02:16:39 -07005783 #add vlan ID ,Modify portgroup for vlan ID
5784 self.configure_vlanID(content, vcenter_conect, network_name)
5785
5786 task = self.add_sriov_to_vm(content,
5787 vm_obj,
5788 host_obj,
5789 network_name,
5790 avilable_sriov_devices[0]
5791 )
5792 if task:
5793 status= self.wait_for_vcenter_task(task, vcenter_conect)
5794 if status:
5795 self.logger.info("Added SRIOV {} to VM {}".format(
5796 no_of_sriov_devices,
5797 str(vm_obj)))
5798 else:
5799 self.logger.error("Fail to add SRIOV {} to VM {}".format(
5800 no_of_sriov_devices,
5801 str(vm_obj)))
5802 raise vimconn.vimconnUnexpectedResponse(
5803 "Fail to add SRIOV adapter in VM ".format(str(vm_obj))
5804 )
5805 return True, vm_obj, vcenter_conect
5806 else:
5807 self.logger.error("Currently there is no host with"\
5808 " {} number of avaialble SRIOV "\
5809 "VFs required for VM {}".format(
5810 no_of_sriov_devices,
5811 vmname_andid)
5812 )
5813 raise vimconn.vimconnNotFoundException(
5814 "Currently there is no host with {} "\
5815 "number of avaialble SRIOV devices required for VM {}".format(
5816 no_of_sriov_devices,
5817 vmname_andid))
5818 else:
5819 self.logger.debug("No infromation about SRIOV devices {} ",sriov_nets)
5820
5821 except vmodl.MethodFault as error:
5822 self.logger.error("Error occurred while adding SRIOV {} ",error)
5823 return None, vm_obj, vcenter_conect
5824
5825
5826 def get_sriov_devices(self,host, no_of_vfs):
5827 """
5828 Method to get the details of SRIOV devices on given host
5829 Args:
5830 host - vSphere host object
5831 no_of_vfs - number of VFs needed on host
5832
5833 Returns:
5834 array of SRIOV devices
5835 """
5836 sriovInfo=[]
5837 if host:
5838 for device in host.config.pciPassthruInfo:
5839 if isinstance(device,vim.host.SriovInfo) and device.sriovActive:
5840 if device.numVirtualFunction >= no_of_vfs:
5841 sriovInfo.append(device)
5842 break
5843 return sriovInfo
5844
5845
5846 def get_host_and_sriov_devices(self, content, no_of_vfs):
5847 """
5848 Method to get the details of SRIOV devices infromation on all hosts
5849
5850 Args:
5851 content - vSphere host object
5852 no_of_vfs - number of pci VFs needed on host
5853
5854 Returns:
5855 array of SRIOV devices and host object
5856 """
5857 host_obj = None
5858 sriov_device_objs = None
5859 try:
5860 if content:
5861 container = content.viewManager.CreateContainerView(content.rootFolder,
5862 [vim.HostSystem], True)
5863 for host in container.view:
5864 devices = self.get_sriov_devices(host, no_of_vfs)
5865 if devices:
5866 host_obj = host
5867 sriov_device_objs = devices
5868 break
5869 except Exception as exp:
5870 self.logger.error("Error {} occurred while finding SRIOV devices on host: {}".format(exp, host_obj))
5871
5872 return host_obj,sriov_device_objs
5873
5874
5875 def add_sriov_to_vm(self,content, vm_obj, host_obj, network_name, sriov_device):
5876 """
5877 Method to add SRIOV adapter to vm
5878
5879 Args:
5880 host_obj - vSphere host object
5881 vm_obj - vSphere vm object
5882 content - vCenter content object
5883 network_name - name of distributed virtaul portgroup
5884 sriov_device - SRIOV device info
5885
5886 Returns:
5887 task object
5888 """
5889 devices = []
5890 vnic_label = "sriov nic"
5891 try:
5892 dvs_portgr = self.get_dvport_group(network_name)
5893 network_name = dvs_portgr.name
5894 nic = vim.vm.device.VirtualDeviceSpec()
5895 # VM device
5896 nic.operation = vim.vm.device.VirtualDeviceSpec.Operation.add
5897 nic.device = vim.vm.device.VirtualSriovEthernetCard()
5898 nic.device.addressType = 'assigned'
5899 #nic.device.key = 13016
5900 nic.device.deviceInfo = vim.Description()
5901 nic.device.deviceInfo.label = vnic_label
5902 nic.device.deviceInfo.summary = network_name
5903 nic.device.backing = vim.vm.device.VirtualEthernetCard.NetworkBackingInfo()
5904
5905 nic.device.backing.network = self.get_obj(content, [vim.Network], network_name)
5906 nic.device.backing.deviceName = network_name
5907 nic.device.backing.useAutoDetect = False
5908 nic.device.connectable = vim.vm.device.VirtualDevice.ConnectInfo()
5909 nic.device.connectable.startConnected = True
5910 nic.device.connectable.allowGuestControl = True
5911
5912 nic.device.sriovBacking = vim.vm.device.VirtualSriovEthernetCard.SriovBackingInfo()
5913 nic.device.sriovBacking.physicalFunctionBacking = vim.vm.device.VirtualPCIPassthrough.DeviceBackingInfo()
5914 nic.device.sriovBacking.physicalFunctionBacking.id = sriov_device.id
5915
5916 devices.append(nic)
5917 vmconf = vim.vm.ConfigSpec(deviceChange=devices)
5918 task = vm_obj.ReconfigVM_Task(vmconf)
5919 return task
5920 except Exception as exp:
5921 self.logger.error("Error {} occurred while adding SRIOV adapter in VM: {}".format(exp, vm_obj))
5922 return None
5923
5924
5925 def create_dvPort_group(self, network_name):
5926 """
5927 Method to create disributed virtual portgroup
5928
5929 Args:
5930 network_name - name of network/portgroup
5931
5932 Returns:
5933 portgroup key
5934 """
5935 try:
5936 new_network_name = [network_name, '-', str(uuid.uuid4())]
5937 network_name=''.join(new_network_name)
5938 vcenter_conect, content = self.get_vcenter_content()
5939
5940 dv_switch = self.get_obj(content, [vim.DistributedVirtualSwitch], self.dvs_name)
5941 if dv_switch:
5942 dv_pg_spec = vim.dvs.DistributedVirtualPortgroup.ConfigSpec()
5943 dv_pg_spec.name = network_name
5944
5945 dv_pg_spec.type = vim.dvs.DistributedVirtualPortgroup.PortgroupType.earlyBinding
5946 dv_pg_spec.defaultPortConfig = vim.dvs.VmwareDistributedVirtualSwitch.VmwarePortConfigPolicy()
5947 dv_pg_spec.defaultPortConfig.securityPolicy = vim.dvs.VmwareDistributedVirtualSwitch.SecurityPolicy()
5948 dv_pg_spec.defaultPortConfig.securityPolicy.allowPromiscuous = vim.BoolPolicy(value=False)
5949 dv_pg_spec.defaultPortConfig.securityPolicy.forgedTransmits = vim.BoolPolicy(value=False)
5950 dv_pg_spec.defaultPortConfig.securityPolicy.macChanges = vim.BoolPolicy(value=False)
5951
5952 task = dv_switch.AddDVPortgroup_Task([dv_pg_spec])
5953 self.wait_for_vcenter_task(task, vcenter_conect)
5954
5955 dvPort_group = self.get_obj(content, [vim.dvs.DistributedVirtualPortgroup], network_name)
5956 if dvPort_group:
5957 self.logger.info("Created disributed virtaul port group: {}".format(dvPort_group))
5958 return dvPort_group.key
5959 else:
5960 self.logger.debug("No disributed virtual switch found with name {}".format(network_name))
5961
5962 except Exception as exp:
5963 self.logger.error("Error occurred while creating disributed virtaul port group {}"\
5964 " : {}".format(network_name, exp))
5965 return None
5966
5967 def reconfig_portgroup(self, content, dvPort_group_name , config_info={}):
5968 """
5969 Method to reconfigure disributed virtual portgroup
5970
5971 Args:
5972 dvPort_group_name - name of disributed virtual portgroup
5973 content - vCenter content object
5974 config_info - disributed virtual portgroup configuration
5975
5976 Returns:
5977 task object
5978 """
5979 try:
5980 dvPort_group = self.get_dvport_group(dvPort_group_name)
5981 if dvPort_group:
5982 dv_pg_spec = vim.dvs.DistributedVirtualPortgroup.ConfigSpec()
5983 dv_pg_spec.configVersion = dvPort_group.config.configVersion
5984 dv_pg_spec.defaultPortConfig = vim.dvs.VmwareDistributedVirtualSwitch.VmwarePortConfigPolicy()
5985 if "vlanID" in config_info:
5986 dv_pg_spec.defaultPortConfig.vlan = vim.dvs.VmwareDistributedVirtualSwitch.VlanIdSpec()
5987 dv_pg_spec.defaultPortConfig.vlan.vlanId = config_info.get('vlanID')
5988
5989 task = dvPort_group.ReconfigureDVPortgroup_Task(spec=dv_pg_spec)
5990 return task
5991 else:
5992 return None
5993 except Exception as exp:
5994 self.logger.error("Error occurred while reconfiguraing disributed virtaul port group {}"\
5995 " : {}".format(dvPort_group_name, exp))
5996 return None
5997
5998
5999 def destroy_dvport_group(self , dvPort_group_name):
6000 """
6001 Method to destroy disributed virtual portgroup
6002
6003 Args:
6004 network_name - name of network/portgroup
6005
6006 Returns:
6007 True if portgroup successfully got deleted else false
6008 """
6009 vcenter_conect, content = self.get_vcenter_content()
6010 try:
6011 status = None
6012 dvPort_group = self.get_dvport_group(dvPort_group_name)
6013 if dvPort_group:
6014 task = dvPort_group.Destroy_Task()
6015 status = self.wait_for_vcenter_task(task, vcenter_conect)
6016 return status
6017 except vmodl.MethodFault as exp:
6018 self.logger.error("Caught vmodl fault {} while deleting disributed virtaul port group {}".format(
6019 exp, dvPort_group_name))
6020 return None
6021
6022
6023 def get_dvport_group(self, dvPort_group_name):
6024 """
6025 Method to get disributed virtual portgroup
6026
6027 Args:
6028 network_name - name of network/portgroup
6029
6030 Returns:
6031 portgroup object
6032 """
6033 vcenter_conect, content = self.get_vcenter_content()
6034 dvPort_group = None
6035 try:
6036 container = content.viewManager.CreateContainerView(content.rootFolder, [vim.dvs.DistributedVirtualPortgroup], True)
6037 for item in container.view:
6038 if item.key == dvPort_group_name:
6039 dvPort_group = item
6040 break
6041 return dvPort_group
6042 except vmodl.MethodFault as exp:
6043 self.logger.error("Caught vmodl fault {} for disributed virtaul port group {}".format(
6044 exp, dvPort_group_name))
6045 return None
6046
6047 def get_vlanID_from_dvs_portgr(self, dvPort_group_name):
6048 """
6049 Method to get disributed virtual portgroup vlanID
6050
6051 Args:
6052 network_name - name of network/portgroup
6053
6054 Returns:
6055 vlan ID
6056 """
6057 vlanId = None
6058 try:
6059 dvPort_group = self.get_dvport_group(dvPort_group_name)
6060 if dvPort_group:
6061 vlanId = dvPort_group.config.defaultPortConfig.vlan.vlanId
6062 except vmodl.MethodFault as exp:
6063 self.logger.error("Caught vmodl fault {} for disributed virtaul port group {}".format(
6064 exp, dvPort_group_name))
6065 return vlanId
6066
6067
6068 def configure_vlanID(self, content, vcenter_conect, dvPort_group_name):
6069 """
6070 Method to configure vlanID in disributed virtual portgroup vlanID
6071
6072 Args:
6073 network_name - name of network/portgroup
6074
6075 Returns:
6076 None
6077 """
6078 vlanID = self.get_vlanID_from_dvs_portgr(dvPort_group_name)
6079 if vlanID == 0:
6080 #configure vlanID
6081 vlanID = self.genrate_vlanID(dvPort_group_name)
6082 config = {"vlanID":vlanID}
6083 task = self.reconfig_portgroup(content, dvPort_group_name,
6084 config_info=config)
6085 if task:
6086 status= self.wait_for_vcenter_task(task, vcenter_conect)
6087 if status:
6088 self.logger.info("Reconfigured Port group {} for vlan ID {}".format(
6089 dvPort_group_name,vlanID))
6090 else:
6091 self.logger.error("Fail reconfigure portgroup {} for vlanID{}".format(
6092 dvPort_group_name, vlanID))
6093
6094
6095 def genrate_vlanID(self, network_name):
6096 """
6097 Method to get unused vlanID
6098 Args:
6099 network_name - name of network/portgroup
6100 Returns:
6101 vlanID
6102 """
6103 vlan_id = None
6104 used_ids = []
6105 if self.config.get('vlanID_range') == None:
6106 raise vimconn.vimconnConflictException("You must provide a 'vlanID_range' "\
6107 "at config value before creating sriov network with vlan tag")
6108 if "used_vlanIDs" not in self.persistent_info:
6109 self.persistent_info["used_vlanIDs"] = {}
6110 else:
6111 used_ids = self.persistent_info["used_vlanIDs"].values()
kasarc5bf2932018-03-09 04:15:22 -08006112 #For python3
6113 #used_ids = list(self.persistent_info["used_vlanIDs"].values())
kateac1e3792017-04-01 02:16:39 -07006114
6115 for vlanID_range in self.config.get('vlanID_range'):
6116 start_vlanid , end_vlanid = vlanID_range.split("-")
6117 if start_vlanid > end_vlanid:
6118 raise vimconn.vimconnConflictException("Invalid vlan ID range {}".format(
6119 vlanID_range))
6120
6121 for id in xrange(int(start_vlanid), int(end_vlanid) + 1):
kasarc5bf2932018-03-09 04:15:22 -08006122 #For python3
6123 #for id in range(int(start_vlanid), int(end_vlanid) + 1):
kateac1e3792017-04-01 02:16:39 -07006124 if id not in used_ids:
6125 vlan_id = id
6126 self.persistent_info["used_vlanIDs"][network_name] = vlan_id
6127 return vlan_id
6128 if vlan_id is None:
6129 raise vimconn.vimconnConflictException("All Vlan IDs are in use")
6130
6131
6132 def get_obj(self, content, vimtype, name):
6133 """
6134 Get the vsphere object associated with a given text name
6135 """
6136 obj = None
6137 container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True)
6138 for item in container.view:
6139 if item.name == name:
6140 obj = item
6141 break
6142 return obj
6143
kasar0c007d62017-05-19 03:13:57 -07006144
6145 def insert_media_to_vm(self, vapp, image_id):
6146 """
6147 Method to insert media CD-ROM (ISO image) from catalog to vm.
6148 vapp - vapp object to get vm id
6149 Image_id - image id for cdrom to be inerted to vm
6150 """
6151 # create connection object
6152 vca = self.connect()
6153 try:
6154 # fetching catalog details
kasarc5bf2932018-03-09 04:15:22 -08006155 rest_url = "{}/api/catalog/{}".format(self.url, image_id)
6156 if vca._session:
6157 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
sbhangarea8e5b782018-06-21 02:10:03 -07006158 'x-vcloud-authorization': vca._session.headers['x-vcloud-authorization']}
kasarc5bf2932018-03-09 04:15:22 -08006159 response = self.perform_request(req_type='GET',
6160 url=rest_url,
6161 headers=headers)
kasar0c007d62017-05-19 03:13:57 -07006162
6163 if response.status_code != 200:
6164 self.logger.error("REST call {} failed reason : {}"\
6165 "status code : {}".format(url_rest_call,
6166 response.content,
6167 response.status_code))
6168 raise vimconn.vimconnException("insert_media_to_vm(): Failed to get "\
6169 "catalog details")
6170 # searching iso name and id
6171 iso_name,media_id = self.get_media_details(vca, response.content)
6172
6173 if iso_name and media_id:
6174 data ="""<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
6175 <ns6:MediaInsertOrEjectParams
6176 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">
6177 <ns6:Media
6178 type="application/vnd.vmware.vcloud.media+xml"
6179 name="{}.iso"
6180 id="urn:vcloud:media:{}"
6181 href="https://{}/api/media/{}"/>
6182 </ns6:MediaInsertOrEjectParams>""".format(iso_name, media_id,
kasarc5bf2932018-03-09 04:15:22 -08006183 self.url,media_id)
kasar0c007d62017-05-19 03:13:57 -07006184
kasarc5bf2932018-03-09 04:15:22 -08006185 for vms in vapp.get_all_vms():
6186 vm_id = vms.get('id').split(':')[-1]
kasar0c007d62017-05-19 03:13:57 -07006187
kasar0c007d62017-05-19 03:13:57 -07006188 headers['Content-Type'] = 'application/vnd.vmware.vcloud.mediaInsertOrEjectParams+xml'
kasarc5bf2932018-03-09 04:15:22 -08006189 rest_url = "{}/api/vApp/vm-{}/media/action/insertMedia".format(self.url,vm_id)
kasar0c007d62017-05-19 03:13:57 -07006190
kasarc5bf2932018-03-09 04:15:22 -08006191 response = self.perform_request(req_type='POST',
6192 url=rest_url,
6193 data=data,
6194 headers=headers)
kasar0c007d62017-05-19 03:13:57 -07006195
6196 if response.status_code != 202:
6197 self.logger.error("Failed to insert CD-ROM to vm")
6198 raise vimconn.vimconnException("insert_media_to_vm() : Failed to insert"\
6199 "ISO image to vm")
6200 else:
kasarc5bf2932018-03-09 04:15:22 -08006201 task = self.get_task_from_response(response.content)
6202 result = self.client.get_task_monitor().wait_for_success(task=task)
6203 if result.get('status') == 'success':
kasar0c007d62017-05-19 03:13:57 -07006204 self.logger.info("insert_media_to_vm(): Sucessfully inserted media ISO"\
6205 " image to vm {}".format(vm_id))
kasarc5bf2932018-03-09 04:15:22 -08006206
kasar0c007d62017-05-19 03:13:57 -07006207 except Exception as exp:
6208 self.logger.error("insert_media_to_vm() : exception occurred "\
6209 "while inserting media CD-ROM")
6210 raise vimconn.vimconnException(message=exp)
6211
6212
6213 def get_media_details(self, vca, content):
6214 """
6215 Method to get catalog item details
6216 vca - connection object
6217 content - Catalog details
6218 Return - Media name, media id
6219 """
6220 cataloghref_list = []
6221 try:
6222 if content:
6223 vm_list_xmlroot = XmlElementTree.fromstring(content)
6224 for child in vm_list_xmlroot.iter():
6225 if 'CatalogItem' in child.tag:
6226 cataloghref_list.append(child.attrib.get('href'))
6227 if cataloghref_list is not None:
6228 for href in cataloghref_list:
6229 if href:
kasarc5bf2932018-03-09 04:15:22 -08006230 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
sbhangarea8e5b782018-06-21 02:10:03 -07006231 'x-vcloud-authorization': vca._session.headers['x-vcloud-authorization']}
kasarc5bf2932018-03-09 04:15:22 -08006232 response = self.perform_request(req_type='GET',
6233 url=href,
6234 headers=headers)
kasar0c007d62017-05-19 03:13:57 -07006235 if response.status_code != 200:
6236 self.logger.error("REST call {} failed reason : {}"\
6237 "status code : {}".format(href,
6238 response.content,
6239 response.status_code))
6240 raise vimconn.vimconnException("get_media_details : Failed to get "\
6241 "catalogitem details")
6242 list_xmlroot = XmlElementTree.fromstring(response.content)
6243 for child in list_xmlroot.iter():
6244 if 'Entity' in child.tag:
6245 if 'media' in child.attrib.get('href'):
6246 name = child.attrib.get('name')
6247 media_id = child.attrib.get('href').split('/').pop()
6248 return name,media_id
6249 else:
6250 self.logger.debug("Media name and id not found")
6251 return False,False
6252 except Exception as exp:
6253 self.logger.error("get_media_details : exception occurred "\
6254 "getting media details")
6255 raise vimconn.vimconnException(message=exp)
6256
bhangare1a0b97c2017-06-21 02:20:15 -07006257
kated47ad5f2017-08-03 02:16:13 -07006258 def retry_rest(self, method, url, add_headers=None, data=None):
bhangare1a0b97c2017-06-21 02:20:15 -07006259 """ Method to get Token & retry respective REST request
6260 Args:
6261 api - REST API - Can be one of 'GET' or 'PUT' or 'POST'
6262 url - request url to be used
6263 add_headers - Additional headers (optional)
6264 data - Request payload data to be passed in request
6265 Returns:
6266 response - Response of request
6267 """
6268 response = None
6269
6270 #Get token
6271 self.get_token()
6272
kasarc5bf2932018-03-09 04:15:22 -08006273 if self.client._session:
6274 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
sbhangarea8e5b782018-06-21 02:10:03 -07006275 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
bhangare1a0b97c2017-06-21 02:20:15 -07006276
6277 if add_headers:
6278 headers.update(add_headers)
6279
kated47ad5f2017-08-03 02:16:13 -07006280 if method == 'GET':
kasarc5bf2932018-03-09 04:15:22 -08006281 response = self.perform_request(req_type='GET',
6282 url=url,
6283 headers=headers)
kated47ad5f2017-08-03 02:16:13 -07006284 elif method == 'PUT':
kasarc5bf2932018-03-09 04:15:22 -08006285 response = self.perform_request(req_type='PUT',
6286 url=url,
6287 headers=headers,
sbhangarea8e5b782018-06-21 02:10:03 -07006288 data=data)
kated47ad5f2017-08-03 02:16:13 -07006289 elif method == 'POST':
kasarc5bf2932018-03-09 04:15:22 -08006290 response = self.perform_request(req_type='POST',
6291 url=url,
6292 headers=headers,
sbhangarea8e5b782018-06-21 02:10:03 -07006293 data=data)
kated47ad5f2017-08-03 02:16:13 -07006294 elif method == 'DELETE':
kasarc5bf2932018-03-09 04:15:22 -08006295 response = self.perform_request(req_type='DELETE',
6296 url=url,
6297 headers=headers)
kated47ad5f2017-08-03 02:16:13 -07006298 return response
6299
bhangare1a0b97c2017-06-21 02:20:15 -07006300
6301 def get_token(self):
6302 """ Generate a new token if expired
6303
6304 Returns:
kasarc5bf2932018-03-09 04:15:22 -08006305 The return client object that letter can be used to connect to vCloud director as admin for VDC
bhangare1a0b97c2017-06-21 02:20:15 -07006306 """
bhangare1a0b97c2017-06-21 02:20:15 -07006307 try:
6308 self.logger.debug("Generate token for vca {} as {} to datacenter {}.".format(self.org_name,
6309 self.user,
6310 self.org_name))
kasarc5bf2932018-03-09 04:15:22 -08006311 host = self.url
6312 client = Client(host, verify_ssl_certs=False)
6313 client.set_credentials(BasicLoginCredentials(self.user, self.org_name, self.passwd))
6314 # connection object
sbhangarea8e5b782018-06-21 02:10:03 -07006315 self.client = client
bhangare1a0b97c2017-06-21 02:20:15 -07006316
6317 except:
6318 raise vimconn.vimconnConnectionException("Can't connect to a vCloud director org: "
6319 "{} as user: {}".format(self.org_name, self.user))
6320
kasarc5bf2932018-03-09 04:15:22 -08006321 if not client:
6322 raise vimconn.vimconnConnectionException("Failed while reconnecting vCD")
bhangare1a0b97c2017-06-21 02:20:15 -07006323
bhangare1a0b97c2017-06-21 02:20:15 -07006324
6325 def get_vdc_details(self):
6326 """ Get VDC details using pyVcloud Lib
6327
kasarc5bf2932018-03-09 04:15:22 -08006328 Returns org and vdc object
bhangare1a0b97c2017-06-21 02:20:15 -07006329 """
Ravi Chamartyb7ff3552018-10-30 19:51:23 +00006330 vdc = None
6331 try:
6332 org = Org(self.client, resource=self.client.get_org())
6333 vdc = org.get_vdc(self.tenant_name)
6334 except Exception as e:
6335 # pyvcloud not giving a specific exception, Refresh nevertheless
6336 self.logger.debug("Received exception {}, refreshing token ".format(str(e)))
bhangare1a0b97c2017-06-21 02:20:15 -07006337
6338 #Retry once, if failed by refreshing token
6339 if vdc is None:
6340 self.get_token()
Ravi Chamartyb7ff3552018-10-30 19:51:23 +00006341 org = Org(self.client, resource=self.client.get_org())
kasarc5bf2932018-03-09 04:15:22 -08006342 vdc = org.get_vdc(self.tenant_name)
bhangare1a0b97c2017-06-21 02:20:15 -07006343
kasarc5bf2932018-03-09 04:15:22 -08006344 return org, vdc
6345
6346
6347 def perform_request(self, req_type, url, headers=None, data=None):
6348 """Perform the POST/PUT/GET/DELETE request."""
6349
6350 #Log REST request details
6351 self.log_request(req_type, url=url, headers=headers, data=data)
6352 # perform request and return its result
6353 if req_type == 'GET':
6354 response = requests.get(url=url,
6355 headers=headers,
6356 verify=False)
6357 elif req_type == 'PUT':
6358 response = requests.put(url=url,
6359 headers=headers,
6360 data=data,
6361 verify=False)
6362 elif req_type == 'POST':
6363 response = requests.post(url=url,
6364 headers=headers,
6365 data=data,
6366 verify=False)
6367 elif req_type == 'DELETE':
6368 response = requests.delete(url=url,
6369 headers=headers,
6370 verify=False)
6371 #Log the REST response
6372 self.log_response(response)
6373
6374 return response
6375
6376
6377 def log_request(self, req_type, url=None, headers=None, data=None):
6378 """Logs REST request details"""
6379
6380 if req_type is not None:
6381 self.logger.debug("Request type: {}".format(req_type))
6382
6383 if url is not None:
6384 self.logger.debug("Request url: {}".format(url))
6385
6386 if headers is not None:
6387 for header in headers:
6388 self.logger.debug("Request header: {}: {}".format(header, headers[header]))
6389
6390 if data is not None:
6391 self.logger.debug("Request data: {}".format(data))
6392
6393
6394 def log_response(self, response):
6395 """Logs REST response details"""
6396
6397 self.logger.debug("Response status code: {} ".format(response.status_code))
6398
6399
6400 def get_task_from_response(self, content):
6401 """
6402 content - API response content(response.content)
sbhangarea8e5b782018-06-21 02:10:03 -07006403 return task object
kasarc5bf2932018-03-09 04:15:22 -08006404 """
6405 xmlroot = XmlElementTree.fromstring(content)
6406 if xmlroot.tag.split('}')[1] == "Task":
6407 return xmlroot
sbhangarea8e5b782018-06-21 02:10:03 -07006408 else:
kasarc5bf2932018-03-09 04:15:22 -08006409 for ele in xmlroot:
6410 if ele.tag.split("}")[1] == "Tasks":
6411 task = ele[0]
sbhangarea8e5b782018-06-21 02:10:03 -07006412 break
kasarc5bf2932018-03-09 04:15:22 -08006413 return task
6414
6415
6416 def power_on_vapp(self,vapp_id, vapp_name):
6417 """
6418 vapp_id - vApp uuid
6419 vapp_name - vAapp name
sbhangarea8e5b782018-06-21 02:10:03 -07006420 return - Task object
kasarc5bf2932018-03-09 04:15:22 -08006421 """
6422 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
6423 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
sbhangarea8e5b782018-06-21 02:10:03 -07006424
kasarc5bf2932018-03-09 04:15:22 -08006425 poweron_href = "{}/api/vApp/vapp-{}/power/action/powerOn".format(self.url,
6426 vapp_id)
6427 response = self.perform_request(req_type='POST',
6428 url=poweron_href,
6429 headers=headers)
6430
6431 if response.status_code != 202:
6432 self.logger.error("REST call {} failed reason : {}"\
6433 "status code : {} ".format(poweron_href,
6434 response.content,
6435 response.status_code))
6436 raise vimconn.vimconnException("power_on_vapp() : Failed to power on "\
6437 "vApp {}".format(vapp_name))
6438 else:
6439 poweron_task = self.get_task_from_response(response.content)
6440 return poweron_task
bhangare1a0b97c2017-06-21 02:20:15 -07006441
kated47ad5f2017-08-03 02:16:13 -07006442