blob: f343eeac92e06e32d579d3edb8ae7d7e84cc4bf7 [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
Ananda Baitharu319c26f2019-03-05 17:34:31 +000032import shutil
33import subprocess
34import tempfile
bayramovef390722016-09-27 03:34:46 -070035import traceback
bayramovef390722016-09-27 03:34:46 -070036import itertools
bayramov325fa1c2016-09-08 01:42:46 -070037import requests
bhangarefda5f7c2017-01-12 23:50:34 -080038import ssl
39import atexit
40
41from pyVmomi import vim, vmodl
42from pyVim.connect import SmartConnect, Disconnect
bayramov325fa1c2016-09-08 01:42:46 -070043
bayramovef390722016-09-27 03:34:46 -070044from xml.etree import ElementTree as XmlElementTree
bhangarea92ae392017-01-12 22:30:29 -080045from lxml import etree as lxmlElementTree
bayramov325fa1c2016-09-08 01:42:46 -070046
bayramovef390722016-09-27 03:34:46 -070047import yaml
kasarc5bf2932018-03-09 04:15:22 -080048from pyvcloud.vcd.client import BasicLoginCredentials,Client,VcdTaskException
49from pyvcloud.vcd.vdc import VDC
50from pyvcloud.vcd.org import Org
51import re
52from pyvcloud.vcd.vapp import VApp
bayramov325fa1c2016-09-08 01:42:46 -070053from xml.sax.saxutils import escape
bayramov325fa1c2016-09-08 01:42:46 -070054import logging
55import json
bayramov325fa1c2016-09-08 01:42:46 -070056import time
57import uuid
58import httplib
kasarc5bf2932018-03-09 04:15:22 -080059#For python3
60#import http.client
kate15f1c382016-12-15 01:12:40 -080061import hashlib
bhangare0e571a92017-01-12 04:02:23 -080062import socket
63import struct
64import netaddr
kasarde691232017-03-25 03:37:31 -070065import random
bayramov325fa1c2016-09-08 01:42:46 -070066
bayramovbd6160f2016-09-28 04:12:05 +040067# global variable for vcd connector type
68STANDALONE = 'standalone'
69
kate15f1c382016-12-15 01:12:40 -080070# key for flavor dicts
71FLAVOR_RAM_KEY = 'ram'
72FLAVOR_VCPUS_KEY = 'vcpus'
bhangarea92ae392017-01-12 22:30:29 -080073FLAVOR_DISK_KEY = 'disk'
kasarde691232017-03-25 03:37:31 -070074DEFAULT_IP_PROFILE = {'dhcp_count':50,
bhangare0e571a92017-01-12 04:02:23 -080075 'dhcp_enabled':True,
kasarde691232017-03-25 03:37:31 -070076 'ip_version':"IPv4"
bhangare0e571a92017-01-12 04:02:23 -080077 }
78# global variable for wait time
kate13ab2c42016-12-23 01:34:24 -080079INTERVAL_TIME = 5
80MAX_WAIT_TIME = 1800
bayramov325fa1c2016-09-08 01:42:46 -070081
Ananda Baitharu5f65b782019-06-10 09:00:27 +000082API_VERSION = '27.0'
bayramovbd6160f2016-09-28 04:12:05 +040083
kasarc5bf2932018-03-09 04:15:22 -080084__author__ = "Mustafa Bayramov, Arpita Kate, Sachin Bhangare, Prakash Kasar"
85__date__ = "$09-Mar-2018 11:09:29$"
86__version__ = '0.2'
bayramov325fa1c2016-09-08 01:42:46 -070087
bayramovef390722016-09-27 03:34:46 -070088# -1: "Could not be created",
89# 0: "Unresolved",
90# 1: "Resolved",
91# 2: "Deployed",
92# 3: "Suspended",
93# 4: "Powered on",
94# 5: "Waiting for user input",
95# 6: "Unknown state",
96# 7: "Unrecognized state",
97# 8: "Powered off",
98# 9: "Inconsistent state",
99# 10: "Children do not all have the same status",
100# 11: "Upload initiated, OVF descriptor pending",
101# 12: "Upload initiated, copying contents",
102# 13: "Upload initiated , disk contents pending",
103# 14: "Upload has been quarantined",
104# 15: "Upload quarantine period has expired"
105
106# mapping vCD status to MANO
107vcdStatusCode2manoFormat = {4: 'ACTIVE',
108 7: 'PAUSED',
109 3: 'SUSPENDED',
110 8: 'INACTIVE',
111 12: 'BUILD',
112 -1: 'ERROR',
113 14: 'DELETED'}
114
115#
116netStatus2manoFormat = {'ACTIVE': 'ACTIVE', 'PAUSED': 'PAUSED', 'INACTIVE': 'INACTIVE', 'BUILD': 'BUILD',
117 'ERROR': 'ERROR', 'DELETED': 'DELETED'
118 }
119
bayramovbd6160f2016-09-28 04:12:05 +0400120class vimconnector(vimconn.vimconnector):
kateeb044522017-03-06 23:54:39 -0800121 # dict used to store flavor in memory
122 flavorlist = {}
123
bayramovbd6160f2016-09-28 04:12:05 +0400124 def __init__(self, uuid=None, name=None, tenant_id=None, tenant_name=None,
tiernob3d36742017-03-03 23:51:05 +0100125 url=None, url_admin=None, user=None, passwd=None, log_level=None, config={}, persistent_info={}):
bayramovb6ffe792016-09-28 11:50:56 +0400126 """
127 Constructor create vmware connector to vCloud director.
128
129 By default construct doesn't validate connection state. So client can create object with None arguments.
130 If client specified username , password and host and VDC name. Connector initialize other missing attributes.
131
132 a) It initialize organization UUID
133 b) Initialize tenant_id/vdc ID. (This information derived from tenant name)
134
135 Args:
136 uuid - is organization uuid.
137 name - is organization name that must be presented in vCloud director.
138 tenant_id - is VDC uuid it must be presented in vCloud director
139 tenant_name - is VDC name.
140 url - is hostname or ip address of vCloud director
141 url_admin - same as above.
142 user - is user that administrator for organization. Caller must make sure that
143 username has right privileges.
144
145 password - is password for a user.
146
147 VMware connector also requires PVDC administrative privileges and separate account.
148 This variables must be passed via config argument dict contains keys
149
150 dict['admin_username']
151 dict['admin_password']
kateeb044522017-03-06 23:54:39 -0800152 config - Provide NSX and vCenter information
bayramovb6ffe792016-09-28 11:50:56 +0400153
154 Returns:
155 Nothing.
156 """
157
bayramovbd6160f2016-09-28 04:12:05 +0400158 vimconn.vimconnector.__init__(self, uuid, name, tenant_id, tenant_name, url,
159 url_admin, user, passwd, log_level, config)
kate15f1c382016-12-15 01:12:40 -0800160
161 self.logger = logging.getLogger('openmano.vim.vmware')
162 self.logger.setLevel(10)
tiernob3d36742017-03-03 23:51:05 +0100163 self.persistent_info = persistent_info
kate15f1c382016-12-15 01:12:40 -0800164
bayramovef390722016-09-27 03:34:46 -0700165 self.name = name
kate15f1c382016-12-15 01:12:40 -0800166 self.id = uuid
bayramovef390722016-09-27 03:34:46 -0700167 self.url = url
bayramov325fa1c2016-09-08 01:42:46 -0700168 self.url_admin = url_admin
169 self.tenant_id = tenant_id
170 self.tenant_name = tenant_name
bayramovef390722016-09-27 03:34:46 -0700171 self.user = user
172 self.passwd = passwd
173 self.config = config
174 self.admin_password = None
175 self.admin_user = None
kate15f1c382016-12-15 01:12:40 -0800176 self.org_name = ""
bhangare985a1fd2017-01-31 01:53:21 -0800177 self.nsx_manager = None
178 self.nsx_user = None
179 self.nsx_password = None
sbhangarea8e5b782018-06-21 02:10:03 -0700180 self.availability_zone = None
bayramovef390722016-09-27 03:34:46 -0700181
kasarc5bf2932018-03-09 04:15:22 -0800182 # Disable warnings from self-signed certificates.
183 requests.packages.urllib3.disable_warnings()
184
kate15f1c382016-12-15 01:12:40 -0800185 if tenant_name is not None:
186 orgnameandtenant = tenant_name.split(":")
187 if len(orgnameandtenant) == 2:
katec324e002016-12-23 00:54:47 -0800188 self.tenant_name = orgnameandtenant[1]
189 self.org_name = orgnameandtenant[0]
kate15f1c382016-12-15 01:12:40 -0800190 else:
191 self.tenant_name = tenant_name
bhangarea92ae392017-01-12 22:30:29 -0800192 if "orgname" in config:
kate15f1c382016-12-15 01:12:40 -0800193 self.org_name = config['orgname']
katec324e002016-12-23 00:54:47 -0800194
tiernofe789902016-09-29 14:20:44 +0000195 if log_level:
bayramov5761ad12016-10-04 09:00:30 +0400196 self.logger.setLevel(getattr(logging, log_level))
bayramov325fa1c2016-09-08 01:42:46 -0700197
bayramovef390722016-09-27 03:34:46 -0700198 try:
199 self.admin_user = config['admin_username']
200 self.admin_password = config['admin_password']
201 except KeyError:
bayramovbd6160f2016-09-28 04:12:05 +0400202 raise vimconn.vimconnException(message="Error admin username or admin password is empty.")
bayramov325fa1c2016-09-08 01:42:46 -0700203
bhangare985a1fd2017-01-31 01:53:21 -0800204 try:
205 self.nsx_manager = config['nsx_manager']
206 self.nsx_user = config['nsx_user']
207 self.nsx_password = config['nsx_password']
208 except KeyError:
209 raise vimconn.vimconnException(message="Error: nsx manager or nsx user or nsx password is empty in Config")
210
kateeb044522017-03-06 23:54:39 -0800211 self.vcenter_ip = config.get("vcenter_ip", None)
212 self.vcenter_port = config.get("vcenter_port", None)
213 self.vcenter_user = config.get("vcenter_user", None)
214 self.vcenter_password = config.get("vcenter_password", None)
215
sbhangarea8e5b782018-06-21 02:10:03 -0700216 #Set availability zone for Affinity rules
217 self.availability_zone = self.set_availability_zones()
218
kateac1e3792017-04-01 02:16:39 -0700219# ############# Stub code for SRIOV #################
220# try:
221# self.dvs_name = config['dv_switch_name']
222# except KeyError:
223# raise vimconn.vimconnException(message="Error: distributed virtaul switch name is empty in Config")
224#
225# self.vlanID_range = config.get("vlanID_range", None)
226
bayramovef390722016-09-27 03:34:46 -0700227 self.org_uuid = None
kasarc5bf2932018-03-09 04:15:22 -0800228 self.client = None
bayramov325fa1c2016-09-08 01:42:46 -0700229
230 if not url:
bayramov5761ad12016-10-04 09:00:30 +0400231 raise vimconn.vimconnException('url param can not be NoneType')
bayramov325fa1c2016-09-08 01:42:46 -0700232
bayramovef390722016-09-27 03:34:46 -0700233 if not self.url_admin: # try to use normal url
bayramov325fa1c2016-09-08 01:42:46 -0700234 self.url_admin = self.url
235
kate15f1c382016-12-15 01:12:40 -0800236 logging.debug("UUID: {} name: {} tenant_id: {} tenant name {}".format(self.id, self.org_name,
bayramovef390722016-09-27 03:34:46 -0700237 self.tenant_id, self.tenant_name))
238 logging.debug("vcd url {} vcd username: {} vcd password: {}".format(self.url, self.user, self.passwd))
239 logging.debug("vcd admin username {} vcd admin passowrd {}".format(self.admin_user, self.admin_password))
bayramov325fa1c2016-09-08 01:42:46 -0700240
bayramovef390722016-09-27 03:34:46 -0700241 # initialize organization
bayramovbd6160f2016-09-28 04:12:05 +0400242 if self.user is not None and self.passwd is not None and self.url:
243 self.init_organization()
bayramovef390722016-09-27 03:34:46 -0700244
245 def __getitem__(self, index):
kate15f1c382016-12-15 01:12:40 -0800246 if index == 'name':
247 return self.name
bayramovef390722016-09-27 03:34:46 -0700248 if index == 'tenant_id':
bayramov325fa1c2016-09-08 01:42:46 -0700249 return self.tenant_id
bayramovef390722016-09-27 03:34:46 -0700250 if index == 'tenant_name':
bayramov325fa1c2016-09-08 01:42:46 -0700251 return self.tenant_name
bayramovef390722016-09-27 03:34:46 -0700252 elif index == 'id':
bayramov325fa1c2016-09-08 01:42:46 -0700253 return self.id
bayramovef390722016-09-27 03:34:46 -0700254 elif index == 'org_name':
255 return self.org_name
256 elif index == 'org_uuid':
257 return self.org_uuid
258 elif index == 'user':
bayramov325fa1c2016-09-08 01:42:46 -0700259 return self.user
bayramovef390722016-09-27 03:34:46 -0700260 elif index == 'passwd':
bayramov325fa1c2016-09-08 01:42:46 -0700261 return self.passwd
bayramovef390722016-09-27 03:34:46 -0700262 elif index == 'url':
bayramov325fa1c2016-09-08 01:42:46 -0700263 return self.url
bayramovef390722016-09-27 03:34:46 -0700264 elif index == 'url_admin':
bayramov325fa1c2016-09-08 01:42:46 -0700265 return self.url_admin
bayramovef390722016-09-27 03:34:46 -0700266 elif index == "config":
bayramov325fa1c2016-09-08 01:42:46 -0700267 return self.config
268 else:
bayramovef390722016-09-27 03:34:46 -0700269 raise KeyError("Invalid key '%s'" % str(index))
bayramov325fa1c2016-09-08 01:42:46 -0700270
bayramovef390722016-09-27 03:34:46 -0700271 def __setitem__(self, index, value):
kate15f1c382016-12-15 01:12:40 -0800272 if index == 'name':
273 self.name = value
bayramovef390722016-09-27 03:34:46 -0700274 if index == 'tenant_id':
bayramov325fa1c2016-09-08 01:42:46 -0700275 self.tenant_id = value
bayramovef390722016-09-27 03:34:46 -0700276 if index == 'tenant_name':
bayramov325fa1c2016-09-08 01:42:46 -0700277 self.tenant_name = value
bayramovef390722016-09-27 03:34:46 -0700278 elif index == 'id':
bayramov325fa1c2016-09-08 01:42:46 -0700279 self.id = value
bayramovef390722016-09-27 03:34:46 -0700280 elif index == 'org_name':
281 self.org_name = value
bayramovef390722016-09-27 03:34:46 -0700282 elif index == 'org_uuid':
kate15f1c382016-12-15 01:12:40 -0800283 self.org_uuid = value
bayramovef390722016-09-27 03:34:46 -0700284 elif index == 'user':
bayramov325fa1c2016-09-08 01:42:46 -0700285 self.user = value
bayramovef390722016-09-27 03:34:46 -0700286 elif index == 'passwd':
bayramov325fa1c2016-09-08 01:42:46 -0700287 self.passwd = value
bayramovef390722016-09-27 03:34:46 -0700288 elif index == 'url':
bayramov325fa1c2016-09-08 01:42:46 -0700289 self.url = value
bayramovef390722016-09-27 03:34:46 -0700290 elif index == 'url_admin':
bayramov325fa1c2016-09-08 01:42:46 -0700291 self.url_admin = value
292 else:
bayramovef390722016-09-27 03:34:46 -0700293 raise KeyError("Invalid key '%s'" % str(index))
bayramov325fa1c2016-09-08 01:42:46 -0700294
bayramovef390722016-09-27 03:34:46 -0700295 def connect_as_admin(self):
bayramovb6ffe792016-09-28 11:50:56 +0400296 """ Method connect as pvdc admin user to vCloud director.
297 There are certain action that can be done only by provider vdc admin user.
298 Organization creation / provider network creation etc.
bayramovef390722016-09-27 03:34:46 -0700299
300 Returns:
kasarc5bf2932018-03-09 04:15:22 -0800301 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 -0700302 """
kasarc5bf2932018-03-09 04:15:22 -0800303 self.logger.debug("Logging into vCD {} as admin.".format(self.org_name))
bayramov325fa1c2016-09-08 01:42:46 -0700304
kasarc5bf2932018-03-09 04:15:22 -0800305 try:
306 host = self.url
307 org = 'System'
308 client_as_admin = Client(host, verify_ssl_certs=False)
kasare3449912019-03-13 02:21:06 -0700309 client_as_admin.set_highest_supported_version()
kasarc5bf2932018-03-09 04:15:22 -0800310 client_as_admin.set_credentials(BasicLoginCredentials(self.admin_user, org, self.admin_password))
311 except Exception as e:
312 raise vimconn.vimconnException(
313 "Can't connect to a vCloud director as: {} with exception {}".format(self.admin_user, e))
bayramov325fa1c2016-09-08 01:42:46 -0700314
kasarc5bf2932018-03-09 04:15:22 -0800315 return client_as_admin
bayramov325fa1c2016-09-08 01:42:46 -0700316
bayramovef390722016-09-27 03:34:46 -0700317 def connect(self):
318 """ Method connect as normal user to vCloud director.
319
320 Returns:
kasarc5bf2932018-03-09 04:15:22 -0800321 The return client object that latter can be used to connect to vCloud director as admin for VDC
bayramovef390722016-09-27 03:34:46 -0700322 """
bayramovb6ffe792016-09-28 11:50:56 +0400323 try:
kasarc5bf2932018-03-09 04:15:22 -0800324 self.logger.debug("Logging into vCD {} as {} to datacenter {}.".format(self.org_name,
kate15f1c382016-12-15 01:12:40 -0800325 self.user,
326 self.org_name))
kasarc5bf2932018-03-09 04:15:22 -0800327 host = self.url
328 client = Client(host, verify_ssl_certs=False)
kasare3449912019-03-13 02:21:06 -0700329 client.set_highest_supported_version()
kasarc5bf2932018-03-09 04:15:22 -0800330 client.set_credentials(BasicLoginCredentials(self.user, self.org_name, self.passwd))
bayramovb6ffe792016-09-28 11:50:56 +0400331 except:
kate15f1c382016-12-15 01:12:40 -0800332 raise vimconn.vimconnConnectionException("Can't connect to a vCloud director org: "
333 "{} as user: {}".format(self.org_name, self.user))
bayramov325fa1c2016-09-08 01:42:46 -0700334
kasarc5bf2932018-03-09 04:15:22 -0800335 return client
bayramov325fa1c2016-09-08 01:42:46 -0700336
bayramovbd6160f2016-09-28 04:12:05 +0400337 def init_organization(self):
338 """ Method initialize organization UUID and VDC parameters.
339
340 At bare minimum client must provide organization name that present in vCloud director and VDC.
341
342 The VDC - UUID ( tenant_id) will be initialized at the run time if client didn't call constructor.
343 The Org - UUID will be initialized at the run time if data center present in vCloud director.
bayramov325fa1c2016-09-08 01:42:46 -0700344
bayramovef390722016-09-27 03:34:46 -0700345 Returns:
346 The return vca object that letter can be used to connect to vcloud direct as admin
347 """
kasarc5bf2932018-03-09 04:15:22 -0800348 client = self.connect()
349 if not client:
350 raise vimconn.vimconnConnectionException("Failed to connect vCD.")
bhangare1a0b97c2017-06-21 02:20:15 -0700351
kasarc5bf2932018-03-09 04:15:22 -0800352 self.client = client
bayramovef390722016-09-27 03:34:46 -0700353 try:
354 if self.org_uuid is None:
kasarc5bf2932018-03-09 04:15:22 -0800355 org_list = client.get_org_list()
356 for org in org_list.Org:
bayramovbd6160f2016-09-28 04:12:05 +0400357 # 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 -0800358 if org.get('name') == self.org_name:
359 self.org_uuid = org.get('href').split('/')[-1]
bayramovbd6160f2016-09-28 04:12:05 +0400360 self.logger.debug("Setting organization UUID {}".format(self.org_uuid))
361 break
bayramovbd6160f2016-09-28 04:12:05 +0400362 else:
363 raise vimconn.vimconnException("Vcloud director organization {} not found".format(self.org_name))
364
365 # if well good we require for org details
366 org_details_dict = self.get_org(org_uuid=self.org_uuid)
367
368 # we have two case if we want to initialize VDC ID or VDC name at run time
369 # tenant_name provided but no tenant id
bayramov5761ad12016-10-04 09:00:30 +0400370 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 +0400371 vdcs_dict = org_details_dict['vdcs']
bayramovbd6160f2016-09-28 04:12:05 +0400372 for vdc in vdcs_dict:
373 if vdcs_dict[vdc] == self.tenant_name:
374 self.tenant_id = vdc
375 self.logger.debug("Setting vdc uuid {} for organization UUID {}".format(self.tenant_id,
kate15f1c382016-12-15 01:12:40 -0800376 self.org_name))
bayramovbd6160f2016-09-28 04:12:05 +0400377 break
378 else:
379 raise vimconn.vimconnException("Tenant name indicated but not present in vcloud director.")
380 # 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 +0400381 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 +0400382 vdcs_dict = org_details_dict['vdcs']
383 for vdc in vdcs_dict:
384 if vdc == self.tenant_id:
385 self.tenant_name = vdcs_dict[vdc]
386 self.logger.debug("Setting vdc uuid {} for organization UUID {}".format(self.tenant_id,
kate15f1c382016-12-15 01:12:40 -0800387 self.org_name))
bayramovbd6160f2016-09-28 04:12:05 +0400388 break
389 else:
390 raise vimconn.vimconnException("Tenant id indicated but not present in vcloud director")
bayramovef390722016-09-27 03:34:46 -0700391 self.logger.debug("Setting organization uuid {}".format(self.org_uuid))
392 except:
393 self.logger.debug("Failed initialize organization UUID for org {}".format(self.org_name))
394 self.logger.debug(traceback.format_exc())
395 self.org_uuid = None
bayramov325fa1c2016-09-08 01:42:46 -0700396
bayramovef390722016-09-27 03:34:46 -0700397 def new_tenant(self, tenant_name=None, tenant_description=None):
bayramovb6ffe792016-09-28 11:50:56 +0400398 """ Method adds a new tenant to VIM with this name.
399 This action requires access to create VDC action in vCloud director.
bayramovef390722016-09-27 03:34:46 -0700400
bayramovb6ffe792016-09-28 11:50:56 +0400401 Args:
402 tenant_name is tenant_name to be created.
403 tenant_description not used for this call
404
405 Return:
406 returns the tenant identifier in UUID format.
407 If action is failed method will throw vimconn.vimconnException method
bayramovbd6160f2016-09-28 04:12:05 +0400408 """
bayramovef390722016-09-27 03:34:46 -0700409 vdc_task = self.create_vdc(vdc_name=tenant_name)
410 if vdc_task is not None:
411 vdc_uuid, value = vdc_task.popitem()
kasarc5bf2932018-03-09 04:15:22 -0800412 self.logger.info("Created new vdc {} and uuid: {}".format(tenant_name, vdc_uuid))
bayramovef390722016-09-27 03:34:46 -0700413 return vdc_uuid
414 else:
bayramovbd6160f2016-09-28 04:12:05 +0400415 raise vimconn.vimconnException("Failed create tenant {}".format(tenant_name))
bayramovef390722016-09-27 03:34:46 -0700416
bayramov163f1ae2016-09-28 17:16:55 +0400417 def delete_tenant(self, tenant_id=None):
kated47ad5f2017-08-03 02:16:13 -0700418 """ Delete a tenant from VIM
419 Args:
420 tenant_id is tenant_id to be deleted.
421
422 Return:
423 returns the tenant identifier in UUID format.
424 If action is failed method will throw exception
425 """
426 vca = self.connect_as_admin()
427 if not vca:
kasarc5bf2932018-03-09 04:15:22 -0800428 raise vimconn.vimconnConnectionException("Failed to connect vCD")
kated47ad5f2017-08-03 02:16:13 -0700429
430 if tenant_id is not None:
kasarc5bf2932018-03-09 04:15:22 -0800431 if vca._session:
kated47ad5f2017-08-03 02:16:13 -0700432 #Get OrgVDC
kasarc5bf2932018-03-09 04:15:22 -0800433 url_list = [self.url, '/api/vdc/', tenant_id]
kated47ad5f2017-08-03 02:16:13 -0700434 orgvdc_herf = ''.join(url_list)
kasarc5bf2932018-03-09 04:15:22 -0800435
436 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
437 'x-vcloud-authorization': vca._session.headers['x-vcloud-authorization']}
438 response = self.perform_request(req_type='GET',
439 url=orgvdc_herf,
440 headers=headers)
kated47ad5f2017-08-03 02:16:13 -0700441
442 if response.status_code != requests.codes.ok:
443 self.logger.debug("delete_tenant():GET REST API call {} failed. "\
444 "Return status code {}".format(orgvdc_herf,
445 response.status_code))
446 raise vimconn.vimconnNotFoundException("Fail to get tenant {}".format(tenant_id))
447
448 lxmlroot_respond = lxmlElementTree.fromstring(response.content)
449 namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.iteritems() if prefix}
kasarc5bf2932018-03-09 04:15:22 -0800450 #For python3
451 #namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.items() if prefix}
kated47ad5f2017-08-03 02:16:13 -0700452 namespaces["xmlns"]= "http://www.vmware.com/vcloud/v1.5"
453 vdc_remove_href = lxmlroot_respond.find("xmlns:Link[@rel='remove']",namespaces).attrib['href']
454 vdc_remove_href = vdc_remove_href + '?recursive=true&force=true'
455
kasarc5bf2932018-03-09 04:15:22 -0800456 response = self.perform_request(req_type='DELETE',
457 url=vdc_remove_href,
458 headers=headers)
kated47ad5f2017-08-03 02:16:13 -0700459
460 if response.status_code == 202:
kasarc5bf2932018-03-09 04:15:22 -0800461 time.sleep(5)
462 return tenant_id
kated47ad5f2017-08-03 02:16:13 -0700463 else:
464 self.logger.debug("delete_tenant(): DELETE REST API call {} failed. "\
465 "Return status code {}".format(vdc_remove_href,
466 response.status_code))
467 raise vimconn.vimconnException("Fail to delete tenant with ID {}".format(tenant_id))
468 else:
469 self.logger.debug("delete_tenant():Incorrect tenant ID {}".format(tenant_id))
470 raise vimconn.vimconnNotFoundException("Fail to get tenant {}".format(tenant_id))
471
bayramov325fa1c2016-09-08 01:42:46 -0700472
473 def get_tenant_list(self, filter_dict={}):
bayramovb6ffe792016-09-28 11:50:56 +0400474 """Obtain tenants of VIM
bayramov325fa1c2016-09-08 01:42:46 -0700475 filter_dict can contain the following keys:
476 name: filter by tenant name
477 id: filter by tenant uuid/id
478 <other VIM specific>
bayramovb6ffe792016-09-28 11:50:56 +0400479 Returns the tenant list of dictionaries:
bayramov325fa1c2016-09-08 01:42:46 -0700480 [{'name':'<name>, 'id':'<id>, ...}, ...]
bayramov325fa1c2016-09-08 01:42:46 -0700481
bayramovb6ffe792016-09-28 11:50:56 +0400482 """
bayramovef390722016-09-27 03:34:46 -0700483 org_dict = self.get_org(self.org_uuid)
484 vdcs_dict = org_dict['vdcs']
485
486 vdclist = []
487 try:
488 for k in vdcs_dict:
489 entry = {'name': vdcs_dict[k], 'id': k}
bayramovb6ffe792016-09-28 11:50:56 +0400490 # if caller didn't specify dictionary we return all tenants.
491 if filter_dict is not None and filter_dict:
492 filtered_entry = entry.copy()
493 filtered_dict = set(entry.keys()) - set(filter_dict)
494 for unwanted_key in filtered_dict: del entry[unwanted_key]
495 if filter_dict == entry:
496 vdclist.append(filtered_entry)
497 else:
498 vdclist.append(entry)
bayramovef390722016-09-27 03:34:46 -0700499 except:
500 self.logger.debug("Error in get_tenant_list()")
501 self.logger.debug(traceback.format_exc())
bayramovb6ffe792016-09-28 11:50:56 +0400502 raise vimconn.vimconnException("Incorrect state. {}")
bayramovef390722016-09-27 03:34:46 -0700503
504 return vdclist
505
garciadeblasebd66722019-01-31 16:01:31 +0000506 def new_network(self, net_name, net_type, ip_profile=None, shared=False, vlan=None):
bayramovb6ffe792016-09-28 11:50:56 +0400507 """Adds a tenant network to VIM
garciadeblasebd66722019-01-31 16:01:31 +0000508 Params:
509 'net_name': name of the network
510 'net_type': one of:
511 'bridge': overlay isolated network
512 'data': underlay E-LAN network for Passthrough and SRIOV interfaces
513 'ptp': underlay E-LINE network for Passthrough and SRIOV interfaces.
514 'ip_profile': is a dict containing the IP parameters of the network
515 'ip_version': can be "IPv4" or "IPv6" (Currently only IPv4 is implemented)
516 'subnet_address': ip_prefix_schema, that is X.X.X.X/Y
517 'gateway_address': (Optional) ip_schema, that is X.X.X.X
518 'dns_address': (Optional) comma separated list of ip_schema, e.g. X.X.X.X[,X,X,X,X]
519 'dhcp_enabled': True or False
520 'dhcp_start_address': ip_schema, first IP to grant
521 'dhcp_count': number of IPs to grant.
522 'shared': if this network can be seen/use by other tenants/organization
523 'vlan': in case of a data or ptp net_type, the intended vlan tag to be used for the network
524 Returns a tuple with the network identifier and created_items, or raises an exception on error
525 created_items can be None or a dictionary where this method can include key-values that will be passed to
526 the method delete_network. Can be used to store created segments, created l2gw connections, etc.
527 Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same
528 as not present.
529 """
bayramov325fa1c2016-09-08 01:42:46 -0700530
bhangare0e571a92017-01-12 04:02:23 -0800531 self.logger.debug("new_network tenant {} net_type {} ip_profile {} shared {}"
532 .format(net_name, net_type, ip_profile, shared))
bayramov325fa1c2016-09-08 01:42:46 -0700533
garciadeblasebd66722019-01-31 16:01:31 +0000534 created_items = {}
bayramovef390722016-09-27 03:34:46 -0700535 isshared = 'false'
536 if shared:
537 isshared = 'true'
538
kateac1e3792017-04-01 02:16:39 -0700539# ############# Stub code for SRIOV #################
540# if net_type == "data" or net_type == "ptp":
541# if self.config.get('dv_switch_name') == None:
542# raise vimconn.vimconnConflictException("You must provide 'dv_switch_name' at config value")
543# network_uuid = self.create_dvPort_group(net_name)
544
bhangare0e571a92017-01-12 04:02:23 -0800545 network_uuid = self.create_network(network_name=net_name, net_type=net_type,
546 ip_profile=ip_profile, isshared=isshared)
bayramovef390722016-09-27 03:34:46 -0700547 if network_uuid is not None:
garciadeblasebd66722019-01-31 16:01:31 +0000548 return network_uuid, created_items
bayramovef390722016-09-27 03:34:46 -0700549 else:
bayramovbd6160f2016-09-28 04:12:05 +0400550 raise vimconn.vimconnUnexpectedResponse("Failed create a new network {}".format(net_name))
bayramovef390722016-09-27 03:34:46 -0700551
552 def get_vcd_network_list(self):
553 """ Method available organization for a logged in tenant
554
555 Returns:
556 The return vca object that letter can be used to connect to vcloud direct as admin
557 """
558
kate15f1c382016-12-15 01:12:40 -0800559 self.logger.debug("get_vcd_network_list(): retrieving network list for vcd {}".format(self.tenant_name))
bayramovef390722016-09-27 03:34:46 -0700560
kate15f1c382016-12-15 01:12:40 -0800561 if not self.tenant_name:
562 raise vimconn.vimconnConnectionException("Tenant name is empty.")
563
kasarc5bf2932018-03-09 04:15:22 -0800564 org, vdc = self.get_vdc_details()
kate15f1c382016-12-15 01:12:40 -0800565 if vdc is None:
566 raise vimconn.vimconnConnectionException("Can't retrieve information for a VDC {}".format(self.tenant_name))
567
kasarc5bf2932018-03-09 04:15:22 -0800568 vdc_uuid = vdc.get('id').split(":")[3]
569 if self.client._session:
570 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
571 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
572 response = self.perform_request(req_type='GET',
573 url=vdc.get('href'),
574 headers=headers)
575 if response.status_code != 200:
576 self.logger.error("Failed to get vdc content")
577 raise vimconn.vimconnNotFoundException("Failed to get vdc content")
578 else:
579 content = XmlElementTree.fromstring(response.content)
sbhangarea8e5b782018-06-21 02:10:03 -0700580
bayramovef390722016-09-27 03:34:46 -0700581 network_list = []
582 try:
kasarc5bf2932018-03-09 04:15:22 -0800583 for item in content:
584 if item.tag.split('}')[-1] == 'AvailableNetworks':
585 for net in item:
586 response = self.perform_request(req_type='GET',
587 url=net.get('href'),
588 headers=headers)
bayramovef390722016-09-27 03:34:46 -0700589
kasarc5bf2932018-03-09 04:15:22 -0800590 if response.status_code != 200:
591 self.logger.error("Failed to get network content")
592 raise vimconn.vimconnNotFoundException("Failed to get network content")
593 else:
594 net_details = XmlElementTree.fromstring(response.content)
595
596 filter_dict = {}
597 net_uuid = net_details.get('id').split(":")
598 if len(net_uuid) != 4:
599 continue
600 else:
601 net_uuid = net_uuid[3]
602 # create dict entry
Ravi Chamarty5b2d5c12018-10-22 23:59:10 +0000603 self.logger.debug("get_vcd_network_list(): Adding network {} "
604 "to a list vcd id {} network {}".format(net_uuid,
605 vdc_uuid,
606 net_details.get('name')))
kasarc5bf2932018-03-09 04:15:22 -0800607 filter_dict["name"] = net_details.get('name')
608 filter_dict["id"] = net_uuid
609 if [i.text for i in net_details if i.tag.split('}')[-1] == 'IsShared'][0] == 'true':
610 shared = True
611 else:
612 shared = False
613 filter_dict["shared"] = shared
614 filter_dict["tenant_id"] = vdc_uuid
kasar40d97802018-05-02 05:58:07 -0700615 if int(net_details.get('status')) == 1:
kasarc5bf2932018-03-09 04:15:22 -0800616 filter_dict["admin_state_up"] = True
617 else:
618 filter_dict["admin_state_up"] = False
619 filter_dict["status"] = "ACTIVE"
620 filter_dict["type"] = "bridge"
621 network_list.append(filter_dict)
622 self.logger.debug("get_vcd_network_list adding entry {}".format(filter_dict))
bayramovef390722016-09-27 03:34:46 -0700623 except:
kasarc5bf2932018-03-09 04:15:22 -0800624 self.logger.debug("Error in get_vcd_network_list", exc_info=True)
bayramovef390722016-09-27 03:34:46 -0700625 pass
626
627 self.logger.debug("get_vcd_network_list returning {}".format(network_list))
628 return network_list
bayramov325fa1c2016-09-08 01:42:46 -0700629
630 def get_network_list(self, filter_dict={}):
bayramovb6ffe792016-09-28 11:50:56 +0400631 """Obtain tenant networks of VIM
bayramov325fa1c2016-09-08 01:42:46 -0700632 Filter_dict can be:
bayramovef390722016-09-27 03:34:46 -0700633 name: network name OR/AND
634 id: network uuid OR/AND
635 shared: boolean OR/AND
636 tenant_id: tenant OR/AND
bayramov325fa1c2016-09-08 01:42:46 -0700637 admin_state_up: boolean
638 status: 'ACTIVE'
bayramovef390722016-09-27 03:34:46 -0700639
640 [{key : value , key : value}]
641
bayramov325fa1c2016-09-08 01:42:46 -0700642 Returns the network list of dictionaries:
643 [{<the fields at Filter_dict plus some VIM specific>}, ...]
644 List can be empty
bayramovb6ffe792016-09-28 11:50:56 +0400645 """
bayramov325fa1c2016-09-08 01:42:46 -0700646
bhangare1a0b97c2017-06-21 02:20:15 -0700647 self.logger.debug("get_network_list(): retrieving network list for vcd {}".format(self.tenant_name))
kate15f1c382016-12-15 01:12:40 -0800648
649 if not self.tenant_name:
650 raise vimconn.vimconnConnectionException("Tenant name is empty.")
bayramov325fa1c2016-09-08 01:42:46 -0700651
kasarc5bf2932018-03-09 04:15:22 -0800652 org, vdc = self.get_vdc_details()
kate15f1c382016-12-15 01:12:40 -0800653 if vdc is None:
654 raise vimconn.vimconnConnectionException("Can't retrieve information for a VDC {}.".format(self.tenant_name))
bayramov325fa1c2016-09-08 01:42:46 -0700655
bayramovef390722016-09-27 03:34:46 -0700656 try:
kasarc5bf2932018-03-09 04:15:22 -0800657 vdcid = vdc.get('id').split(":")[3]
658
659 if self.client._session:
660 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
661 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
662 response = self.perform_request(req_type='GET',
663 url=vdc.get('href'),
664 headers=headers)
665 if response.status_code != 200:
666 self.logger.error("Failed to get vdc content")
667 raise vimconn.vimconnNotFoundException("Failed to get vdc content")
668 else:
669 content = XmlElementTree.fromstring(response.content)
670
bhangarebfdca492017-03-11 01:32:46 -0800671 network_list = []
kasarc5bf2932018-03-09 04:15:22 -0800672 for item in content:
673 if item.tag.split('}')[-1] == 'AvailableNetworks':
674 for net in item:
675 response = self.perform_request(req_type='GET',
676 url=net.get('href'),
677 headers=headers)
bhangarebfdca492017-03-11 01:32:46 -0800678
kasarc5bf2932018-03-09 04:15:22 -0800679 if response.status_code != 200:
680 self.logger.error("Failed to get network content")
681 raise vimconn.vimconnNotFoundException("Failed to get network content")
682 else:
683 net_details = XmlElementTree.fromstring(response.content)
bayramovef390722016-09-27 03:34:46 -0700684
kasarc5bf2932018-03-09 04:15:22 -0800685 filter_entry = {}
686 net_uuid = net_details.get('id').split(":")
687 if len(net_uuid) != 4:
688 continue
689 else:
sbhangarea8e5b782018-06-21 02:10:03 -0700690 net_uuid = net_uuid[3]
kasarc5bf2932018-03-09 04:15:22 -0800691 # create dict entry
Ravi Chamarty5b2d5c12018-10-22 23:59:10 +0000692 self.logger.debug("get_network_list(): Adding net {}"
693 " to a list vcd id {} network {}".format(net_uuid,
694 vdcid,
695 net_details.get('name')))
kasarc5bf2932018-03-09 04:15:22 -0800696 filter_entry["name"] = net_details.get('name')
697 filter_entry["id"] = net_uuid
698 if [i.text for i in net_details if i.tag.split('}')[-1] == 'IsShared'][0] == 'true':
699 shared = True
700 else:
701 shared = False
702 filter_entry["shared"] = shared
703 filter_entry["tenant_id"] = vdcid
kasar40d97802018-05-02 05:58:07 -0700704 if int(net_details.get('status')) == 1:
kasarc5bf2932018-03-09 04:15:22 -0800705 filter_entry["admin_state_up"] = True
706 else:
707 filter_entry["admin_state_up"] = False
708 filter_entry["status"] = "ACTIVE"
709 filter_entry["type"] = "bridge"
710 filtered_entry = filter_entry.copy()
711
712 if filter_dict is not None and filter_dict:
713 # we remove all the key : value we don't care and match only
714 # respected field
715 filtered_dict = set(filter_entry.keys()) - set(filter_dict)
716 for unwanted_key in filtered_dict: del filter_entry[unwanted_key]
717 if filter_dict == filter_entry:
718 network_list.append(filtered_entry)
719 else:
720 network_list.append(filtered_entry)
721 except Exception as e:
722 self.logger.debug("Error in get_network_list",exc_info=True)
723 if isinstance(e, vimconn.vimconnException):
724 raise
725 else:
726 raise vimconn.vimconnNotFoundException("Failed : Networks list not found {} ".format(e))
bayramov325fa1c2016-09-08 01:42:46 -0700727
728 self.logger.debug("Returning {}".format(network_list))
729 return network_list
730
731 def get_network(self, net_id):
bayramovfe3f3c92016-10-04 07:53:41 +0400732 """Method obtains network details of net_id VIM network
bayramovef390722016-09-27 03:34:46 -0700733 Return a dict with the fields at filter_dict (see get_network_list) plus some VIM specific>}, ...]"""
734
bayramovef390722016-09-27 03:34:46 -0700735 try:
kasarc5bf2932018-03-09 04:15:22 -0800736 org, vdc = self.get_vdc_details()
737 vdc_id = vdc.get('id').split(":")[3]
738 if self.client._session:
739 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
740 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
741 response = self.perform_request(req_type='GET',
742 url=vdc.get('href'),
743 headers=headers)
744 if response.status_code != 200:
745 self.logger.error("Failed to get vdc content")
746 raise vimconn.vimconnNotFoundException("Failed to get vdc content")
747 else:
748 content = XmlElementTree.fromstring(response.content)
bhangarebfdca492017-03-11 01:32:46 -0800749
bhangarebfdca492017-03-11 01:32:46 -0800750 filter_dict = {}
751
kasarc5bf2932018-03-09 04:15:22 -0800752 for item in content:
753 if item.tag.split('}')[-1] == 'AvailableNetworks':
754 for net in item:
755 response = self.perform_request(req_type='GET',
756 url=net.get('href'),
757 headers=headers)
kasarc30a04e2017-08-24 05:58:18 -0700758
kasarc5bf2932018-03-09 04:15:22 -0800759 if response.status_code != 200:
760 self.logger.error("Failed to get network content")
761 raise vimconn.vimconnNotFoundException("Failed to get network content")
762 else:
763 net_details = XmlElementTree.fromstring(response.content)
764
765 vdc_network_id = net_details.get('id').split(":")
766 if len(vdc_network_id) == 4 and vdc_network_id[3] == net_id:
767 filter_dict["name"] = net_details.get('name')
768 filter_dict["id"] = vdc_network_id[3]
769 if [i.text for i in net_details if i.tag.split('}')[-1] == 'IsShared'][0] == 'true':
770 shared = True
771 else:
772 shared = False
773 filter_dict["shared"] = shared
774 filter_dict["tenant_id"] = vdc_id
kasar40d97802018-05-02 05:58:07 -0700775 if int(net_details.get('status')) == 1:
kasarc5bf2932018-03-09 04:15:22 -0800776 filter_dict["admin_state_up"] = True
777 else:
778 filter_dict["admin_state_up"] = False
779 filter_dict["status"] = "ACTIVE"
780 filter_dict["type"] = "bridge"
781 self.logger.debug("Returning {}".format(filter_dict))
782 return filter_dict
bayramovef390722016-09-27 03:34:46 -0700783 else:
kasarc5bf2932018-03-09 04:15:22 -0800784 raise vimconn.vimconnNotFoundException("Network {} not found".format(net_id))
kasarc30a04e2017-08-24 05:58:18 -0700785 except Exception as e:
bayramovef390722016-09-27 03:34:46 -0700786 self.logger.debug("Error in get_network")
787 self.logger.debug(traceback.format_exc())
kasarc30a04e2017-08-24 05:58:18 -0700788 if isinstance(e, vimconn.vimconnException):
789 raise
790 else:
791 raise vimconn.vimconnNotFoundException("Failed : Network not found {} ".format(e))
bayramovef390722016-09-27 03:34:46 -0700792
793 return filter_dict
bayramov325fa1c2016-09-08 01:42:46 -0700794
garciadeblasebd66722019-01-31 16:01:31 +0000795 def delete_network(self, net_id, created_items=None):
bayramovef390722016-09-27 03:34:46 -0700796 """
garciadeblasebd66722019-01-31 16:01:31 +0000797 Removes a tenant network from VIM and its associated elements
798 :param net_id: VIM identifier of the network, provided by method new_network
799 :param created_items: dictionary with extra items to be deleted. provided by method new_network
800 Returns the network identifier or raises an exception upon error or when network is not found
bayramovef390722016-09-27 03:34:46 -0700801 """
802
kateac1e3792017-04-01 02:16:39 -0700803 # ############# Stub code for SRIOV #################
804# dvport_group = self.get_dvport_group(net_id)
805# if dvport_group:
806# #delete portgroup
807# status = self.destroy_dvport_group(net_id)
808# if status:
809# # Remove vlanID from persistent info
810# if net_id in self.persistent_info["used_vlanIDs"]:
811# del self.persistent_info["used_vlanIDs"][net_id]
812#
813# return net_id
814
bayramovfe3f3c92016-10-04 07:53:41 +0400815 vcd_network = self.get_vcd_network(network_uuid=net_id)
816 if vcd_network is not None and vcd_network:
817 if self.delete_network_action(network_uuid=net_id):
818 return net_id
bayramovef390722016-09-27 03:34:46 -0700819 else:
820 raise vimconn.vimconnNotFoundException("Network {} not found".format(net_id))
bayramov325fa1c2016-09-08 01:42:46 -0700821
822 def refresh_nets_status(self, net_list):
bayramovbd6160f2016-09-28 04:12:05 +0400823 """Get the status of the networks
bayramov325fa1c2016-09-08 01:42:46 -0700824 Params: the list of network identifiers
825 Returns a dictionary with:
826 net_id: #VIM id of this network
827 status: #Mandatory. Text with one of:
828 # DELETED (not found at vim)
bayramovbd6160f2016-09-28 04:12:05 +0400829 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
bayramov325fa1c2016-09-08 01:42:46 -0700830 # OTHER (Vim reported other status not understood)
831 # ERROR (VIM indicates an ERROR status)
bayramovbd6160f2016-09-28 04:12:05 +0400832 # ACTIVE, INACTIVE, DOWN (admin down),
bayramov325fa1c2016-09-08 01:42:46 -0700833 # BUILD (on building process)
834 #
bayramovbd6160f2016-09-28 04:12:05 +0400835 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
bayramov325fa1c2016-09-08 01:42:46 -0700836 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
837
bayramovbd6160f2016-09-28 04:12:05 +0400838 """
bayramov325fa1c2016-09-08 01:42:46 -0700839
bayramovef390722016-09-27 03:34:46 -0700840 dict_entry = {}
841 try:
842 for net in net_list:
bayramovef390722016-09-27 03:34:46 -0700843 errormsg = ''
844 vcd_network = self.get_vcd_network(network_uuid=net)
bayramovfe3f3c92016-10-04 07:53:41 +0400845 if vcd_network is not None and vcd_network:
bhangare92d4af32016-12-24 02:54:51 -0800846 if vcd_network['status'] == '1':
bayramovef390722016-09-27 03:34:46 -0700847 status = 'ACTIVE'
848 else:
849 status = 'DOWN'
850 else:
851 status = 'DELETED'
bayramovfe3f3c92016-10-04 07:53:41 +0400852 errormsg = 'Network not found.'
853
854 dict_entry[net] = {'status': status, 'error_msg': errormsg,
bhangare92d4af32016-12-24 02:54:51 -0800855 'vim_info': yaml.safe_dump(vcd_network)}
bayramovef390722016-09-27 03:34:46 -0700856 except:
857 self.logger.debug("Error in refresh_nets_status")
858 self.logger.debug(traceback.format_exc())
859
860 return dict_entry
861
bayramovbd6160f2016-09-28 04:12:05 +0400862 def get_flavor(self, flavor_id):
bayramovef390722016-09-27 03:34:46 -0700863 """Obtain flavor details from the VIM
864 Returns the flavor dict details {'id':<>, 'name':<>, other vim specific } #TODO to concrete
865 """
kateeb044522017-03-06 23:54:39 -0800866 if flavor_id not in vimconnector.flavorlist:
bayramovfe3f3c92016-10-04 07:53:41 +0400867 raise vimconn.vimconnNotFoundException("Flavor not found.")
kateeb044522017-03-06 23:54:39 -0800868 return vimconnector.flavorlist[flavor_id]
bayramov325fa1c2016-09-08 01:42:46 -0700869
870 def new_flavor(self, flavor_data):
bayramovef390722016-09-27 03:34:46 -0700871 """Adds a tenant flavor to VIM
bayramov325fa1c2016-09-08 01:42:46 -0700872 flavor_data contains a dictionary with information, keys:
873 name: flavor name
874 ram: memory (cloud type) in MBytes
875 vpcus: cpus (cloud type)
876 extended: EPA parameters
877 - numas: #items requested in same NUMA
878 memory: number of 1G huge pages memory
879 paired-threads|cores|threads: number of paired hyperthreads, complete cores OR individual threads
880 interfaces: # passthrough(PT) or SRIOV interfaces attached to this numa
881 - name: interface name
882 dedicated: yes|no|yes:sriov; for PT, SRIOV or only one SRIOV for the physical NIC
883 bandwidth: X Gbps; requested guarantee bandwidth
bayramovef390722016-09-27 03:34:46 -0700884 vpci: requested virtual PCI address
bayramov325fa1c2016-09-08 01:42:46 -0700885 disk: disk size
886 is_public:
bayramov325fa1c2016-09-08 01:42:46 -0700887 #TODO to concrete
bayramovef390722016-09-27 03:34:46 -0700888 Returns the flavor identifier"""
bayramov325fa1c2016-09-08 01:42:46 -0700889
bayramovef390722016-09-27 03:34:46 -0700890 # generate a new uuid put to internal dict and return it.
bhangarea92ae392017-01-12 22:30:29 -0800891 self.logger.debug("Creating new flavor - flavor_data: {}".format(flavor_data))
892 new_flavor=flavor_data
893 ram = flavor_data.get(FLAVOR_RAM_KEY, 1024)
894 cpu = flavor_data.get(FLAVOR_VCPUS_KEY, 1)
garciadeblas79d1a1a2017-12-11 16:07:07 +0100895 disk = flavor_data.get(FLAVOR_DISK_KEY, 0)
bhangarea92ae392017-01-12 22:30:29 -0800896
kasarfeaaa052017-06-08 03:46:18 -0700897 if not isinstance(ram, int):
898 raise vimconn.vimconnException("Non-integer value for ram")
899 elif not isinstance(cpu, int):
900 raise vimconn.vimconnException("Non-integer value for cpu")
901 elif not isinstance(disk, int):
902 raise vimconn.vimconnException("Non-integer value for disk")
903
bhangarea92ae392017-01-12 22:30:29 -0800904 extended_flv = flavor_data.get("extended")
905 if extended_flv:
906 numas=extended_flv.get("numas")
907 if numas:
908 for numa in numas:
909 #overwrite ram and vcpus
sbhangarea8e5b782018-06-21 02:10:03 -0700910 if 'memory' in numa:
911 ram = numa['memory']*1024
bhangarea92ae392017-01-12 22:30:29 -0800912 if 'paired-threads' in numa:
913 cpu = numa['paired-threads']*2
914 elif 'cores' in numa:
915 cpu = numa['cores']
916 elif 'threads' in numa:
917 cpu = numa['threads']
918
919 new_flavor[FLAVOR_RAM_KEY] = ram
920 new_flavor[FLAVOR_VCPUS_KEY] = cpu
921 new_flavor[FLAVOR_DISK_KEY] = disk
922 # generate a new uuid put to internal dict and return it.
bayramovef390722016-09-27 03:34:46 -0700923 flavor_id = uuid.uuid4()
kateeb044522017-03-06 23:54:39 -0800924 vimconnector.flavorlist[str(flavor_id)] = new_flavor
bhangarea92ae392017-01-12 22:30:29 -0800925 self.logger.debug("Created flavor - {} : {}".format(flavor_id, new_flavor))
bayramov325fa1c2016-09-08 01:42:46 -0700926
bayramovef390722016-09-27 03:34:46 -0700927 return str(flavor_id)
bayramov325fa1c2016-09-08 01:42:46 -0700928
929 def delete_flavor(self, flavor_id):
bayramovef390722016-09-27 03:34:46 -0700930 """Deletes a tenant flavor from VIM identify by its id
bayramov325fa1c2016-09-08 01:42:46 -0700931
bayramovfe3f3c92016-10-04 07:53:41 +0400932 Returns the used id or raise an exception
bayramovef390722016-09-27 03:34:46 -0700933 """
kateeb044522017-03-06 23:54:39 -0800934 if flavor_id not in vimconnector.flavorlist:
bayramovfe3f3c92016-10-04 07:53:41 +0400935 raise vimconn.vimconnNotFoundException("Flavor not found.")
bayramovef390722016-09-27 03:34:46 -0700936
kateeb044522017-03-06 23:54:39 -0800937 vimconnector.flavorlist.pop(flavor_id, None)
bayramovef390722016-09-27 03:34:46 -0700938 return flavor_id
939
940 def new_image(self, image_dict):
bayramov5761ad12016-10-04 09:00:30 +0400941 """
bayramov325fa1c2016-09-08 01:42:46 -0700942 Adds a tenant image to VIM
943 Returns:
944 200, image-id if the image is created
945 <0, message if there is an error
bayramov5761ad12016-10-04 09:00:30 +0400946 """
bayramov325fa1c2016-09-08 01:42:46 -0700947
bayramovef390722016-09-27 03:34:46 -0700948 return self.get_image_id_from_path(image_dict['location'])
bayramov325fa1c2016-09-08 01:42:46 -0700949
950 def delete_image(self, image_id):
bayramovfe3f3c92016-10-04 07:53:41 +0400951 """
kated47ad5f2017-08-03 02:16:13 -0700952 Deletes a tenant image from VIM
953 Args:
954 image_id is ID of Image to be deleted
955 Return:
956 returns the image identifier in UUID format or raises an exception on error
bayramovfe3f3c92016-10-04 07:53:41 +0400957 """
kasarc5bf2932018-03-09 04:15:22 -0800958 conn = self.connect_as_admin()
959 if not conn:
960 raise vimconn.vimconnConnectionException("Failed to connect vCD")
kated47ad5f2017-08-03 02:16:13 -0700961 # Get Catalog details
kasarc5bf2932018-03-09 04:15:22 -0800962 url_list = [self.url, '/api/catalog/', image_id]
kated47ad5f2017-08-03 02:16:13 -0700963 catalog_herf = ''.join(url_list)
kasarc5bf2932018-03-09 04:15:22 -0800964
965 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
966 'x-vcloud-authorization': conn._session.headers['x-vcloud-authorization']}
967
968 response = self.perform_request(req_type='GET',
969 url=catalog_herf,
sbhangarea8e5b782018-06-21 02:10:03 -0700970 headers=headers)
bayramovfe3f3c92016-10-04 07:53:41 +0400971
kated47ad5f2017-08-03 02:16:13 -0700972 if response.status_code != requests.codes.ok:
973 self.logger.debug("delete_image():GET REST API call {} failed. "\
974 "Return status code {}".format(catalog_herf,
975 response.status_code))
976 raise vimconn.vimconnNotFoundException("Fail to get image {}".format(image_id))
977
978 lxmlroot_respond = lxmlElementTree.fromstring(response.content)
979 namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.iteritems() if prefix}
kasarc5bf2932018-03-09 04:15:22 -0800980 #For python3
981 #namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.items() if prefix}
kated47ad5f2017-08-03 02:16:13 -0700982 namespaces["xmlns"]= "http://www.vmware.com/vcloud/v1.5"
983
984 catalogItems_section = lxmlroot_respond.find("xmlns:CatalogItems",namespaces)
985 catalogItems = catalogItems_section.iterfind("xmlns:CatalogItem",namespaces)
986 for catalogItem in catalogItems:
987 catalogItem_href = catalogItem.attrib['href']
988
kasarc5bf2932018-03-09 04:15:22 -0800989 response = self.perform_request(req_type='GET',
990 url=catalogItem_href,
991 headers=headers)
kated47ad5f2017-08-03 02:16:13 -0700992
993 if response.status_code != requests.codes.ok:
994 self.logger.debug("delete_image():GET REST API call {} failed. "\
995 "Return status code {}".format(catalog_herf,
996 response.status_code))
997 raise vimconn.vimconnNotFoundException("Fail to get catalogItem {} for catalog {}".format(
998 catalogItem,
999 image_id))
1000
1001 lxmlroot_respond = lxmlElementTree.fromstring(response.content)
1002 namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.iteritems() if prefix}
kasarc5bf2932018-03-09 04:15:22 -08001003 #For python3
1004 #namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.items() if prefix}
kated47ad5f2017-08-03 02:16:13 -07001005 namespaces["xmlns"]= "http://www.vmware.com/vcloud/v1.5"
1006 catalogitem_remove_href = lxmlroot_respond.find("xmlns:Link[@rel='remove']",namespaces).attrib['href']
1007
1008 #Remove catalogItem
kasarc5bf2932018-03-09 04:15:22 -08001009 response = self.perform_request(req_type='DELETE',
1010 url=catalogitem_remove_href,
sbhangarea8e5b782018-06-21 02:10:03 -07001011 headers=headers)
kated47ad5f2017-08-03 02:16:13 -07001012 if response.status_code == requests.codes.no_content:
1013 self.logger.debug("Deleted Catalog item {}".format(catalogItem))
1014 else:
1015 raise vimconn.vimconnException("Fail to delete Catalog Item {}".format(catalogItem))
1016
1017 #Remove catalog
kasarc5bf2932018-03-09 04:15:22 -08001018 url_list = [self.url, '/api/admin/catalog/', image_id]
kated47ad5f2017-08-03 02:16:13 -07001019 catalog_remove_herf = ''.join(url_list)
kasarc5bf2932018-03-09 04:15:22 -08001020 response = self.perform_request(req_type='DELETE',
1021 url=catalog_remove_herf,
1022 headers=headers)
kated47ad5f2017-08-03 02:16:13 -07001023
1024 if response.status_code == requests.codes.no_content:
1025 self.logger.debug("Deleted Catalog {}".format(image_id))
1026 return image_id
1027 else:
1028 raise vimconn.vimconnException("Fail to delete Catalog {}".format(image_id))
1029
bayramov325fa1c2016-09-08 01:42:46 -07001030
1031 def catalog_exists(self, catalog_name, catalogs):
bayramovfe3f3c92016-10-04 07:53:41 +04001032 """
1033
1034 :param catalog_name:
1035 :param catalogs:
1036 :return:
1037 """
bayramov325fa1c2016-09-08 01:42:46 -07001038 for catalog in catalogs:
kasarc5bf2932018-03-09 04:15:22 -08001039 if catalog['name'] == catalog_name:
Ananda Baitharu319c26f2019-03-05 17:34:31 +00001040 return catalog['id']
bayramov325fa1c2016-09-08 01:42:46 -07001041
bayramovb6ffe792016-09-28 11:50:56 +04001042 def create_vimcatalog(self, vca=None, catalog_name=None):
bayramovfe3f3c92016-10-04 07:53:41 +04001043 """ Create new catalog entry in vCloud director.
bayramovb6ffe792016-09-28 11:50:56 +04001044
1045 Args
1046 vca: vCloud director.
1047 catalog_name catalog that client wish to create. Note no validation done for a name.
1048 Client must make sure that provide valid string representation.
1049
Ananda Baitharu319c26f2019-03-05 17:34:31 +00001050 Returns catalog id if catalog created else None.
bayramovb6ffe792016-09-28 11:50:56 +04001051
1052 """
1053 try:
Ananda Baitharu319c26f2019-03-05 17:34:31 +00001054 lxml_catalog_element = vca.create_catalog(catalog_name, catalog_name)
1055 if lxml_catalog_element:
1056 id_attr_value = lxml_catalog_element.get('id') # 'urn:vcloud:catalog:7490d561-d384-4dac-8229-3575fd1fc7b4'
1057 return id_attr_value.split(':')[-1]
kasarc5bf2932018-03-09 04:15:22 -08001058 catalogs = vca.list_catalogs()
Ananda Baitharu319c26f2019-03-05 17:34:31 +00001059 except Exception as ex:
1060 self.logger.error(
1061 'create_vimcatalog(): Creation of catalog "{}" failed with error: {}'.format(catalog_name, ex))
1062 raise
bayramov325fa1c2016-09-08 01:42:46 -07001063 return self.catalog_exists(catalog_name, catalogs)
1064
bayramov5761ad12016-10-04 09:00:30 +04001065 # noinspection PyIncorrectDocstring
bayramovfe3f3c92016-10-04 07:53:41 +04001066 def upload_ovf(self, vca=None, catalog_name=None, image_name=None, media_file_name=None,
1067 description='', progress=False, chunk_bytes=128 * 1024):
bayramov325fa1c2016-09-08 01:42:46 -07001068 """
1069 Uploads a OVF file to a vCloud catalog
1070
bayramov5761ad12016-10-04 09:00:30 +04001071 :param chunk_bytes:
1072 :param progress:
1073 :param description:
1074 :param image_name:
1075 :param vca:
bayramov325fa1c2016-09-08 01:42:46 -07001076 :param catalog_name: (str): The name of the catalog to upload the media.
bayramov325fa1c2016-09-08 01:42:46 -07001077 :param media_file_name: (str): The name of the local media file to upload.
1078 :return: (bool) True if the media file was successfully uploaded, false otherwise.
1079 """
1080 os.path.isfile(media_file_name)
1081 statinfo = os.stat(media_file_name)
bayramov325fa1c2016-09-08 01:42:46 -07001082
1083 # find a catalog entry where we upload OVF.
1084 # create vApp Template and check the status if vCD able to read OVF it will respond with appropirate
1085 # status change.
1086 # if VCD can parse OVF we upload VMDK file
bhangarebfdca492017-03-11 01:32:46 -08001087 try:
kasarc5bf2932018-03-09 04:15:22 -08001088 for catalog in vca.list_catalogs():
1089 if catalog_name != catalog['name']:
bhangarebfdca492017-03-11 01:32:46 -08001090 continue
kasarc5bf2932018-03-09 04:15:22 -08001091 catalog_href = "{}/api/catalog/{}/action/upload".format(self.url, catalog['id'])
bhangarebfdca492017-03-11 01:32:46 -08001092 data = """
kasarc5bf2932018-03-09 04:15:22 -08001093 <UploadVAppTemplateParams name="{}" xmlns="http://www.vmware.com/vcloud/v1.5" xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1"><Description>{} vApp Template</Description></UploadVAppTemplateParams>
1094 """.format(catalog_name, description)
1095
1096 if self.client:
1097 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
1098 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
1099 headers['Content-Type'] = 'application/vnd.vmware.vcloud.uploadVAppTemplateParams+xml'
1100
1101 response = self.perform_request(req_type='POST',
1102 url=catalog_href,
1103 headers=headers,
1104 data=data)
1105
bhangarebfdca492017-03-11 01:32:46 -08001106 if response.status_code == requests.codes.created:
1107 catalogItem = XmlElementTree.fromstring(response.content)
1108 entity = [child for child in catalogItem if
1109 child.get("type") == "application/vnd.vmware.vcloud.vAppTemplate+xml"][0]
1110 href = entity.get('href')
1111 template = href
kasarc5bf2932018-03-09 04:15:22 -08001112
1113 response = self.perform_request(req_type='GET',
1114 url=href,
1115 headers=headers)
bhangarebfdca492017-03-11 01:32:46 -08001116
1117 if response.status_code == requests.codes.ok:
bhangarebfdca492017-03-11 01:32:46 -08001118 headers['Content-Type'] = 'Content-Type text/xml'
kasarc5bf2932018-03-09 04:15:22 -08001119 result = re.search('rel="upload:default"\shref="(.*?\/descriptor.ovf)"',response.content)
1120 if result:
1121 transfer_href = result.group(1)
1122
1123 response = self.perform_request(req_type='PUT',
1124 url=transfer_href,
1125 headers=headers,
1126 data=open(media_file_name, 'rb'))
bhangarebfdca492017-03-11 01:32:46 -08001127 if response.status_code != requests.codes.ok:
1128 self.logger.debug(
1129 "Failed create vApp template for catalog name {} and image {}".format(catalog_name,
1130 media_file_name))
1131 return False
1132
1133 # TODO fix this with aync block
1134 time.sleep(5)
1135
1136 self.logger.debug("vApp template for catalog name {} and image {}".format(catalog_name, media_file_name))
1137
1138 # uploading VMDK file
1139 # check status of OVF upload and upload remaining files.
kasarc5bf2932018-03-09 04:15:22 -08001140 response = self.perform_request(req_type='GET',
1141 url=template,
1142 headers=headers)
bhangarebfdca492017-03-11 01:32:46 -08001143
1144 if response.status_code == requests.codes.ok:
kasarc5bf2932018-03-09 04:15:22 -08001145 result = re.search('rel="upload:default"\s*href="(.*?vmdk)"',response.content)
1146 if result:
1147 link_href = result.group(1)
1148 # we skip ovf since it already uploaded.
1149 if 'ovf' in link_href:
1150 continue
1151 # The OVF file and VMDK must be in a same directory
1152 head, tail = os.path.split(media_file_name)
1153 file_vmdk = head + '/' + link_href.split("/")[-1]
1154 if not os.path.isfile(file_vmdk):
1155 return False
1156 statinfo = os.stat(file_vmdk)
1157 if statinfo.st_size == 0:
1158 return False
1159 hrefvmdk = link_href
1160
1161 if progress:
1162 widgets = ['Uploading file: ', Percentage(), ' ', Bar(), ' ', ETA(), ' ',
1163 FileTransferSpeed()]
1164 progress_bar = ProgressBar(widgets=widgets, maxval=statinfo.st_size).start()
1165
1166 bytes_transferred = 0
1167 f = open(file_vmdk, 'rb')
1168 while bytes_transferred < statinfo.st_size:
1169 my_bytes = f.read(chunk_bytes)
1170 if len(my_bytes) <= chunk_bytes:
1171 headers['Content-Range'] = 'bytes %s-%s/%s' % (
1172 bytes_transferred, len(my_bytes) - 1, statinfo.st_size)
1173 headers['Content-Length'] = str(len(my_bytes))
1174 response = requests.put(url=hrefvmdk,
1175 headers=headers,
1176 data=my_bytes,
1177 verify=False)
1178 if response.status_code == requests.codes.ok:
1179 bytes_transferred += len(my_bytes)
1180 if progress:
1181 progress_bar.update(bytes_transferred)
1182 else:
1183 self.logger.debug(
1184 'file upload failed with error: [%s] %s' % (response.status_code,
1185 response.content))
1186
1187 f.close()
bhangarebfdca492017-03-11 01:32:46 -08001188 return False
kasarc5bf2932018-03-09 04:15:22 -08001189 f.close()
1190 if progress:
1191 progress_bar.finish()
1192 time.sleep(10)
1193 return True
1194 else:
1195 self.logger.debug("Failed retrieve vApp template for catalog name {} for OVF {}".
1196 format(catalog_name, media_file_name))
1197 return False
bhangarebfdca492017-03-11 01:32:46 -08001198 except Exception as exp:
1199 self.logger.debug("Failed while uploading OVF to catalog {} for OVF file {} with Exception {}"
1200 .format(catalog_name,media_file_name, exp))
1201 raise vimconn.vimconnException(
1202 "Failed while uploading OVF to catalog {} for OVF file {} with Exception {}"
1203 .format(catalog_name,media_file_name, exp))
bayramov325fa1c2016-09-08 01:42:46 -07001204
1205 self.logger.debug("Failed retrieve catalog name {} for OVF file {}".format(catalog_name, media_file_name))
1206 return False
1207
bayramovfe3f3c92016-10-04 07:53:41 +04001208 def upload_vimimage(self, vca=None, catalog_name=None, media_name=None, medial_file_name=None, progress=False):
bayramov325fa1c2016-09-08 01:42:46 -07001209 """Upload media file"""
bayramovfe3f3c92016-10-04 07:53:41 +04001210 # TODO add named parameters for readability
1211
1212 return self.upload_ovf(vca=vca, catalog_name=catalog_name, image_name=media_name.split(".")[0],
1213 media_file_name=medial_file_name, description='medial_file_name', progress=progress)
bayramov325fa1c2016-09-08 01:42:46 -07001214
bayramovb6ffe792016-09-28 11:50:56 +04001215 def validate_uuid4(self, uuid_string=None):
1216 """ Method validate correct format of UUID.
1217
1218 Return: true if string represent valid uuid
1219 """
1220 try:
1221 val = uuid.UUID(uuid_string, version=4)
1222 except ValueError:
1223 return False
1224 return True
1225
1226 def get_catalogid(self, catalog_name=None, catalogs=None):
1227 """ Method check catalog and return catalog ID in UUID format.
1228
1229 Args
1230 catalog_name: catalog name as string
1231 catalogs: list of catalogs.
1232
1233 Return: catalogs uuid
1234 """
1235
bayramov325fa1c2016-09-08 01:42:46 -07001236 for catalog in catalogs:
kasarc5bf2932018-03-09 04:15:22 -08001237 if catalog['name'] == catalog_name:
1238 catalog_id = catalog['id']
1239 return catalog_id
bayramov325fa1c2016-09-08 01:42:46 -07001240 return None
1241
bayramovb6ffe792016-09-28 11:50:56 +04001242 def get_catalogbyid(self, catalog_uuid=None, catalogs=None):
1243 """ Method check catalog and return catalog name lookup done by catalog UUID.
1244
1245 Args
1246 catalog_name: catalog name as string
1247 catalogs: list of catalogs.
1248
1249 Return: catalogs name or None
1250 """
1251
1252 if not self.validate_uuid4(uuid_string=catalog_uuid):
1253 return None
1254
bayramov325fa1c2016-09-08 01:42:46 -07001255 for catalog in catalogs:
kasarc5bf2932018-03-09 04:15:22 -08001256 catalog_id = catalog.get('id')
bayramovb6ffe792016-09-28 11:50:56 +04001257 if catalog_id == catalog_uuid:
kasarc5bf2932018-03-09 04:15:22 -08001258 return catalog.get('name')
bayramov325fa1c2016-09-08 01:42:46 -07001259 return None
1260
bhangare06312472017-03-30 05:49:07 -07001261 def get_catalog_obj(self, catalog_uuid=None, catalogs=None):
1262 """ Method check catalog and return catalog name lookup done by catalog UUID.
1263
1264 Args
1265 catalog_name: catalog name as string
1266 catalogs: list of catalogs.
1267
1268 Return: catalogs name or None
1269 """
1270
1271 if not self.validate_uuid4(uuid_string=catalog_uuid):
1272 return None
1273
1274 for catalog in catalogs:
kasarc5bf2932018-03-09 04:15:22 -08001275 catalog_id = catalog.get('id')
bhangare06312472017-03-30 05:49:07 -07001276 if catalog_id == catalog_uuid:
1277 return catalog
1278 return None
1279
bayramovfe3f3c92016-10-04 07:53:41 +04001280 def get_image_id_from_path(self, path=None, progress=False):
bayramovb6ffe792016-09-28 11:50:56 +04001281 """ Method upload OVF image to vCloud director.
bayramov325fa1c2016-09-08 01:42:46 -07001282
bayramovb6ffe792016-09-28 11:50:56 +04001283 Each OVF image represented as single catalog entry in vcloud director.
1284 The method check for existing catalog entry. The check done by file name without file extension.
1285
1286 if given catalog name already present method will respond with existing catalog uuid otherwise
1287 it will create new catalog entry and upload OVF file to newly created catalog.
1288
1289 If method can't create catalog entry or upload a file it will throw exception.
1290
bayramovfe3f3c92016-10-04 07:53:41 +04001291 Method accept boolean flag progress that will output progress bar. It useful method
1292 for standalone upload use case. In case to test large file upload.
1293
bayramovb6ffe792016-09-28 11:50:56 +04001294 Args
bayramovfe3f3c92016-10-04 07:53:41 +04001295 path: - valid path to OVF file.
1296 progress - boolean progress bar show progress bar.
bayramovb6ffe792016-09-28 11:50:56 +04001297
1298 Return: if image uploaded correct method will provide image catalog UUID.
1299 """
bayramov325fa1c2016-09-08 01:42:46 -07001300
kate15f1c382016-12-15 01:12:40 -08001301 if not path:
bayramovfe3f3c92016-10-04 07:53:41 +04001302 raise vimconn.vimconnException("Image path can't be None.")
1303
1304 if not os.path.isfile(path):
1305 raise vimconn.vimconnException("Can't read file. File not found.")
1306
1307 if not os.access(path, os.R_OK):
1308 raise vimconn.vimconnException("Can't read file. Check file permission to read.")
1309
1310 self.logger.debug("get_image_id_from_path() client requesting {} ".format(path))
bayramov325fa1c2016-09-08 01:42:46 -07001311
1312 dirpath, filename = os.path.split(path)
1313 flname, file_extension = os.path.splitext(path)
1314 if file_extension != '.ovf':
bayramovfe3f3c92016-10-04 07:53:41 +04001315 self.logger.debug("Wrong file extension {} connector support only OVF container.".format(file_extension))
bayramovb6ffe792016-09-28 11:50:56 +04001316 raise vimconn.vimconnException("Wrong container. vCloud director supports only OVF.")
kate15f1c382016-12-15 01:12:40 -08001317
bayramov325fa1c2016-09-08 01:42:46 -07001318 catalog_name = os.path.splitext(filename)[0]
kate15f1c382016-12-15 01:12:40 -08001319 catalog_md5_name = hashlib.md5(path).hexdigest()
1320 self.logger.debug("File name {} Catalog Name {} file path {} "
1321 "vdc catalog name {}".format(filename, catalog_name, path, catalog_md5_name))
bayramov325fa1c2016-09-08 01:42:46 -07001322
bhangarebfdca492017-03-11 01:32:46 -08001323 try:
kasarc5bf2932018-03-09 04:15:22 -08001324 org,vdc = self.get_vdc_details()
1325 catalogs = org.list_catalogs()
bhangarebfdca492017-03-11 01:32:46 -08001326 except Exception as exp:
1327 self.logger.debug("Failed get catalogs() with Exception {} ".format(exp))
1328 raise vimconn.vimconnException("Failed get catalogs() with Exception {} ".format(exp))
1329
bayramov325fa1c2016-09-08 01:42:46 -07001330 if len(catalogs) == 0:
bayramovfe3f3c92016-10-04 07:53:41 +04001331 self.logger.info("Creating a new catalog entry {} in vcloud director".format(catalog_name))
Ananda Baitharu319c26f2019-03-05 17:34:31 +00001332 if self.create_vimcatalog(org, catalog_md5_name) is None:
kate15f1c382016-12-15 01:12:40 -08001333 raise vimconn.vimconnException("Failed create new catalog {} ".format(catalog_md5_name))
kasarc5bf2932018-03-09 04:15:22 -08001334
1335 result = self.upload_vimimage(vca=org, catalog_name=catalog_md5_name,
bayramovfe3f3c92016-10-04 07:53:41 +04001336 media_name=filename, medial_file_name=path, progress=progress)
bayramov325fa1c2016-09-08 01:42:46 -07001337 if not result:
bayramovb6ffe792016-09-28 11:50:56 +04001338 raise vimconn.vimconnException("Failed create vApp template for catalog {} ".format(catalog_name))
kasarc5bf2932018-03-09 04:15:22 -08001339 return self.get_catalogid(catalog_name, catalogs)
bayramov325fa1c2016-09-08 01:42:46 -07001340 else:
1341 for catalog in catalogs:
1342 # search for existing catalog if we find same name we return ID
1343 # TODO optimize this
kasarc5bf2932018-03-09 04:15:22 -08001344 if catalog['name'] == catalog_md5_name:
kate15f1c382016-12-15 01:12:40 -08001345 self.logger.debug("Found existing catalog entry for {} "
1346 "catalog id {}".format(catalog_name,
1347 self.get_catalogid(catalog_md5_name, catalogs)))
kasarc5bf2932018-03-09 04:15:22 -08001348 return self.get_catalogid(catalog_md5_name, catalogs)
bayramov325fa1c2016-09-08 01:42:46 -07001349
bayramovfe3f3c92016-10-04 07:53:41 +04001350 # if we didn't find existing catalog we create a new one and upload image.
kate15f1c382016-12-15 01:12:40 -08001351 self.logger.debug("Creating new catalog entry {} - {}".format(catalog_name, catalog_md5_name))
Ananda Baitharu319c26f2019-03-05 17:34:31 +00001352 if self.create_vimcatalog(org, catalog_md5_name) is None:
kate15f1c382016-12-15 01:12:40 -08001353 raise vimconn.vimconnException("Failed create new catalog {} ".format(catalog_md5_name))
bayramovfe3f3c92016-10-04 07:53:41 +04001354
kasarc5bf2932018-03-09 04:15:22 -08001355 result = self.upload_vimimage(vca=org, catalog_name=catalog_md5_name,
bayramovfe3f3c92016-10-04 07:53:41 +04001356 media_name=filename, medial_file_name=path, progress=progress)
bayramov325fa1c2016-09-08 01:42:46 -07001357 if not result:
kate15f1c382016-12-15 01:12:40 -08001358 raise vimconn.vimconnException("Failed create vApp template for catalog {} ".format(catalog_md5_name))
bayramov325fa1c2016-09-08 01:42:46 -07001359
kasarc5bf2932018-03-09 04:15:22 -08001360 return self.get_catalogid(catalog_md5_name, org.list_catalogs())
bayramov325fa1c2016-09-08 01:42:46 -07001361
kate8fc61fc2017-01-23 19:57:06 -08001362 def get_image_list(self, filter_dict={}):
1363 '''Obtain tenant images from VIM
1364 Filter_dict can be:
1365 name: image name
1366 id: image uuid
1367 checksum: image checksum
1368 location: image path
1369 Returns the image list of dictionaries:
1370 [{<the fields at Filter_dict plus some VIM specific>}, ...]
1371 List can be empty
1372 '''
bhangare1a0b97c2017-06-21 02:20:15 -07001373
kate8fc61fc2017-01-23 19:57:06 -08001374 try:
kasarc5bf2932018-03-09 04:15:22 -08001375 org, vdc = self.get_vdc_details()
kate8fc61fc2017-01-23 19:57:06 -08001376 image_list = []
kasarc5bf2932018-03-09 04:15:22 -08001377 catalogs = org.list_catalogs()
kate8fc61fc2017-01-23 19:57:06 -08001378 if len(catalogs) == 0:
1379 return image_list
1380 else:
1381 for catalog in catalogs:
kasarc5bf2932018-03-09 04:15:22 -08001382 catalog_uuid = catalog.get('id')
1383 name = catalog.get('name')
kate8fc61fc2017-01-23 19:57:06 -08001384 filtered_dict = {}
kate34718682017-01-24 03:20:43 -08001385 if filter_dict.get("name") and filter_dict["name"] != name:
1386 continue
1387 if filter_dict.get("id") and filter_dict["id"] != catalog_uuid:
1388 continue
1389 filtered_dict ["name"] = name
1390 filtered_dict ["id"] = catalog_uuid
1391 image_list.append(filtered_dict)
kate8fc61fc2017-01-23 19:57:06 -08001392
1393 self.logger.debug("List of already created catalog items: {}".format(image_list))
1394 return image_list
1395 except Exception as exp:
kated63062f2017-01-24 07:26:52 -08001396 raise vimconn.vimconnException("Exception occured while retriving catalog items {}".format(exp))
kate8fc61fc2017-01-23 19:57:06 -08001397
bayramovef390722016-09-27 03:34:46 -07001398 def get_vappid(self, vdc=None, vapp_name=None):
1399 """ Method takes vdc object and vApp name and returns vapp uuid or None
1400
1401 Args:
bayramovef390722016-09-27 03:34:46 -07001402 vdc: The VDC object.
1403 vapp_name: is application vappp name identifier
1404
bayramovb6ffe792016-09-28 11:50:56 +04001405 Returns:
bayramovef390722016-09-27 03:34:46 -07001406 The return vApp name otherwise None
1407 """
bayramovef390722016-09-27 03:34:46 -07001408 if vdc is None or vapp_name is None:
1409 return None
1410 # UUID has following format https://host/api/vApp/vapp-30da58a3-e7c7-4d09-8f68-d4c8201169cf
bayramov325fa1c2016-09-08 01:42:46 -07001411 try:
1412 refs = filter(lambda ref: ref.name == vapp_name and ref.type_ == 'application/vnd.vmware.vcloud.vApp+xml',
bayramovef390722016-09-27 03:34:46 -07001413 vdc.ResourceEntities.ResourceEntity)
kasarc5bf2932018-03-09 04:15:22 -08001414 #For python3
1415 #refs = [ref for ref in vdc.ResourceEntities.ResourceEntity\
1416 # if ref.name == vapp_name and ref.type_ == 'application/vnd.vmware.vcloud.vApp+xml']
bayramov325fa1c2016-09-08 01:42:46 -07001417 if len(refs) == 1:
1418 return refs[0].href.split("vapp")[1][1:]
bayramovef390722016-09-27 03:34:46 -07001419 except Exception as e:
1420 self.logger.exception(e)
1421 return False
1422 return None
1423
bayramovfe3f3c92016-10-04 07:53:41 +04001424 def check_vapp(self, vdc=None, vapp_uuid=None):
1425 """ Method Method returns True or False if vapp deployed in vCloud director
bayramovef390722016-09-27 03:34:46 -07001426
1427 Args:
1428 vca: Connector to VCA
1429 vdc: The VDC object.
1430 vappid: vappid is application identifier
1431
1432 Returns:
bayramovfe3f3c92016-10-04 07:53:41 +04001433 The return True if vApp deployed
bayramov5761ad12016-10-04 09:00:30 +04001434 :param vdc:
1435 :param vapp_uuid:
bayramovef390722016-09-27 03:34:46 -07001436 """
1437 try:
1438 refs = filter(lambda ref:
1439 ref.type_ == 'application/vnd.vmware.vcloud.vApp+xml',
1440 vdc.ResourceEntities.ResourceEntity)
kasarc5bf2932018-03-09 04:15:22 -08001441 #For python3
1442 #refs = [ref for ref in vdc.ResourceEntities.ResourceEntity\
1443 # if ref.type_ == 'application/vnd.vmware.vcloud.vApp+xml']
bayramovef390722016-09-27 03:34:46 -07001444 for ref in refs:
1445 vappid = ref.href.split("vapp")[1][1:]
1446 # find vapp with respected vapp uuid
bayramovfe3f3c92016-10-04 07:53:41 +04001447 if vappid == vapp_uuid:
bayramovef390722016-09-27 03:34:46 -07001448 return True
1449 except Exception as e:
1450 self.logger.exception(e)
1451 return False
1452 return False
1453
kasarc5bf2932018-03-09 04:15:22 -08001454 def get_namebyvappid(self, vapp_uuid=None):
bayramovef390722016-09-27 03:34:46 -07001455 """Method returns vApp name from vCD and lookup done by vapp_id.
1456
1457 Args:
bayramovfe3f3c92016-10-04 07:53:41 +04001458 vapp_uuid: vappid is application identifier
bayramovef390722016-09-27 03:34:46 -07001459
1460 Returns:
1461 The return vApp name otherwise None
1462 """
bayramovef390722016-09-27 03:34:46 -07001463 try:
kasarc5bf2932018-03-09 04:15:22 -08001464 if self.client and vapp_uuid:
1465 vapp_call = "{}/api/vApp/vapp-{}".format(self.url, vapp_uuid)
1466 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
1467 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
bhangare1a0b97c2017-06-21 02:20:15 -07001468
kasarc5bf2932018-03-09 04:15:22 -08001469 response = self.perform_request(req_type='GET',
1470 url=vapp_call,
sbhangarea8e5b782018-06-21 02:10:03 -07001471 headers=headers)
kasarc5bf2932018-03-09 04:15:22 -08001472 #Retry login if session expired & retry sending request
1473 if response.status_code == 403:
1474 response = self.retry_rest('GET', vapp_call)
bhangare1a0b97c2017-06-21 02:20:15 -07001475
kasarc5bf2932018-03-09 04:15:22 -08001476 tree = XmlElementTree.fromstring(response.content)
1477 return tree.attrib['name']
bayramovef390722016-09-27 03:34:46 -07001478 except Exception as e:
1479 self.logger.exception(e)
bayramov325fa1c2016-09-08 01:42:46 -07001480 return None
1481 return None
1482
tierno19860412017-10-03 10:46:46 +02001483 def new_vminstance(self, name=None, description="", start=False, image_id=None, flavor_id=None, net_list=[],
tierno5a3273c2017-08-29 11:43:46 +02001484 cloud_config=None, disk_list=None, availability_zone_index=None, availability_zone_list=None):
bayramov325fa1c2016-09-08 01:42:46 -07001485 """Adds a VM instance to VIM
1486 Params:
tierno19860412017-10-03 10:46:46 +02001487 'start': (boolean) indicates if VM must start or created in pause mode.
1488 'image_id','flavor_id': image and flavor VIM id to use for the VM
1489 'net_list': list of interfaces, each one is a dictionary with:
1490 'name': (optional) name for the interface.
1491 'net_id': VIM network id where this interface must be connect to. Mandatory for type==virtual
1492 'vpci': (optional) virtual vPCI address to assign at the VM. Can be ignored depending on VIM capabilities
garciadeblasc4f4d732018-10-25 18:17:24 +02001493 'model': (optional and only have sense for type==virtual) interface model: virtio, e1000, ...
tierno19860412017-10-03 10:46:46 +02001494 'mac_address': (optional) mac address to assign to this interface
1495 #TODO: CHECK if an optional 'vlan' parameter is needed for VIMs when type if VF and net_id is not provided,
1496 the VLAN tag to be used. In case net_id is provided, the internal network vlan is used for tagging VF
1497 'type': (mandatory) can be one of:
1498 'virtual', in this case always connected to a network of type 'net_type=bridge'
tierno66eba6e2017-11-10 17:09:18 +01001499 '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 +02001500 can created unconnected
tierno66eba6e2017-11-10 17:09:18 +01001501 'SR-IOV' or 'VF' (SRIOV with VLAN tag): same as PF for network connectivity.
tierno19860412017-10-03 10:46:46 +02001502 'VFnotShared'(SRIOV without VLAN tag) same as PF for network connectivity. VF where no other VFs
1503 are allocated on the same physical NIC
1504 'bw': (optional) only for PF/VF/VFnotShared. Minimal Bandwidth required for the interface in GBPS
1505 'port_security': (optional) If False it must avoid any traffic filtering at this interface. If missing
1506 or True, it must apply the default VIM behaviour
1507 After execution the method will add the key:
1508 'vim_id': must be filled/added by this method with the VIM identifier generated by the VIM for this
1509 interface. 'net_list' is modified
1510 'cloud_config': (optional) dictionary with:
1511 'key-pairs': (optional) list of strings with the public key to be inserted to the default user
1512 'users': (optional) list of users to be inserted, each item is a dict with:
1513 'name': (mandatory) user name,
1514 'key-pairs': (optional) list of strings with the public key to be inserted to the user
1515 'user-data': (optional) can be a string with the text script to be passed directly to cloud-init,
1516 or a list of strings, each one contains a script to be passed, usually with a MIMEmultipart file
1517 'config-files': (optional). List of files to be transferred. Each item is a dict with:
1518 'dest': (mandatory) string with the destination absolute path
1519 'encoding': (optional, by default text). Can be one of:
1520 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
1521 'content' (mandatory): string with the content of the file
1522 'permissions': (optional) string with file permissions, typically octal notation '0644'
1523 'owner': (optional) file owner, string with the format 'owner:group'
1524 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk)
1525 'disk_list': (optional) list with additional disks to the VM. Each item is a dict with:
1526 'image_id': (optional). VIM id of an existing image. If not provided an empty disk must be mounted
1527 'size': (mandatory) string with the size of the disk in GB
1528 availability_zone_index: Index of availability_zone_list to use for this this VM. None if not AV required
1529 availability_zone_list: list of availability zones given by user in the VNFD descriptor. Ignore if
1530 availability_zone_index is None
tierno98e909c2017-10-14 13:27:03 +02001531 Returns a tuple with the instance identifier and created_items or raises an exception on error
1532 created_items can be None or a dictionary where this method can include key-values that will be passed to
1533 the method delete_vminstance and action_vminstance. Can be used to store created ports, volumes, etc.
1534 Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same
1535 as not present.
bayramov325fa1c2016-09-08 01:42:46 -07001536 """
kate15f1c382016-12-15 01:12:40 -08001537 self.logger.info("Creating new instance for entry {}".format(name))
sbhangarea8e5b782018-06-21 02:10:03 -07001538 self.logger.debug("desc {} boot {} image_id: {} flavor_id: {} net_list: {} cloud_config {} disk_list {} "\
1539 "availability_zone_index {} availability_zone_list {}"\
1540 .format(description, start, image_id, flavor_id, net_list, cloud_config, disk_list,\
1541 availability_zone_index, availability_zone_list))
bayramov325fa1c2016-09-08 01:42:46 -07001542
bayramov5761ad12016-10-04 09:00:30 +04001543 #new vm name = vmname + tenant_id + uuid
1544 new_vm_name = [name, '-', str(uuid.uuid4())]
kate15f1c382016-12-15 01:12:40 -08001545 vmname_andid = ''.join(new_vm_name)
bayramov5761ad12016-10-04 09:00:30 +04001546
kasarc5bf2932018-03-09 04:15:22 -08001547 for net in net_list:
Ravi Chamartye21c9cc2018-10-24 02:14:53 +00001548 if net['type'] == "PCI-PASSTHROUGH":
kasarc5bf2932018-03-09 04:15:22 -08001549 raise vimconn.vimconnNotSupportedException(
1550 "Current vCD version does not support type : {}".format(net['type']))
bayramov325fa1c2016-09-08 01:42:46 -07001551
kasarc5bf2932018-03-09 04:15:22 -08001552 if len(net_list) > 10:
1553 raise vimconn.vimconnNotSupportedException(
1554 "The VM hardware versions 7 and above support upto 10 NICs only")
1555
1556 # if vm already deployed we return existing uuid
bayramovef390722016-09-27 03:34:46 -07001557 # we check for presence of VDC, Catalog entry and Flavor.
kasarc5bf2932018-03-09 04:15:22 -08001558 org, vdc = self.get_vdc_details()
bayramovef390722016-09-27 03:34:46 -07001559 if vdc is None:
bayramovfe3f3c92016-10-04 07:53:41 +04001560 raise vimconn.vimconnNotFoundException(
bayramovb6ffe792016-09-28 11:50:56 +04001561 "new_vminstance(): Failed create vApp {}: (Failed retrieve VDC information)".format(name))
kasarc5bf2932018-03-09 04:15:22 -08001562 catalogs = org.list_catalogs()
bhangare1a0b97c2017-06-21 02:20:15 -07001563 if catalogs is None:
1564 #Retry once, if failed by refreshing token
1565 self.get_token()
kasarc5bf2932018-03-09 04:15:22 -08001566 org = Org(self.client, resource=self.client.get_org())
1567 catalogs = org.list_catalogs()
bayramovef390722016-09-27 03:34:46 -07001568 if catalogs is None:
bayramovfe3f3c92016-10-04 07:53:41 +04001569 raise vimconn.vimconnNotFoundException(
kate15f1c382016-12-15 01:12:40 -08001570 "new_vminstance(): Failed create vApp {}: (Failed retrieve catalogs list)".format(name))
bayramovbd6160f2016-09-28 04:12:05 +04001571
kate15f1c382016-12-15 01:12:40 -08001572 catalog_hash_name = self.get_catalogbyid(catalog_uuid=image_id, catalogs=catalogs)
1573 if catalog_hash_name:
1574 self.logger.info("Found catalog entry {} for image id {}".format(catalog_hash_name, image_id))
1575 else:
1576 raise vimconn.vimconnNotFoundException("new_vminstance(): Failed create vApp {}: "
1577 "(Failed retrieve catalog information {})".format(name, image_id))
1578
kate15f1c382016-12-15 01:12:40 -08001579 # Set vCPU and Memory based on flavor.
bayramovb6ffe792016-09-28 11:50:56 +04001580 vm_cpus = None
1581 vm_memory = None
bhangarea92ae392017-01-12 22:30:29 -08001582 vm_disk = None
bhangare68e73e62017-07-04 22:44:01 -07001583 numas = None
kateac1e3792017-04-01 02:16:39 -07001584
bayramovb6ffe792016-09-28 11:50:56 +04001585 if flavor_id is not None:
kateeb044522017-03-06 23:54:39 -08001586 if flavor_id not in vimconnector.flavorlist:
kate15f1c382016-12-15 01:12:40 -08001587 raise vimconn.vimconnNotFoundException("new_vminstance(): Failed create vApp {}: "
1588 "Failed retrieve flavor information "
1589 "flavor id {}".format(name, flavor_id))
bayramovb6ffe792016-09-28 11:50:56 +04001590 else:
1591 try:
kateeb044522017-03-06 23:54:39 -08001592 flavor = vimconnector.flavorlist[flavor_id]
kate15f1c382016-12-15 01:12:40 -08001593 vm_cpus = flavor[FLAVOR_VCPUS_KEY]
1594 vm_memory = flavor[FLAVOR_RAM_KEY]
bhangarea92ae392017-01-12 22:30:29 -08001595 vm_disk = flavor[FLAVOR_DISK_KEY]
bhangarefda5f7c2017-01-12 23:50:34 -08001596 extended = flavor.get("extended", None)
1597 if extended:
1598 numas=extended.get("numas", None)
kateac1e3792017-04-01 02:16:39 -07001599
kateeb044522017-03-06 23:54:39 -08001600 except Exception as exp:
1601 raise vimconn.vimconnException("Corrupted flavor. {}.Exception: {}".format(flavor_id, exp))
bayramov325fa1c2016-09-08 01:42:46 -07001602
bayramovef390722016-09-27 03:34:46 -07001603 # image upload creates template name as catalog name space Template.
kate15f1c382016-12-15 01:12:40 -08001604 templateName = self.get_catalogbyid(catalog_uuid=image_id, catalogs=catalogs)
bayramovef390722016-09-27 03:34:46 -07001605 power_on = 'false'
1606 if start:
1607 power_on = 'true'
1608
1609 # client must provide at least one entry in net_list if not we report error
bhangare0e571a92017-01-12 04:02:23 -08001610 #If net type is mgmt, then configure it as primary net & use its NIC index as primary NIC
1611 #If no mgmt, then the 1st NN in netlist is considered as primary net.
1612 primary_net = None
kate15f1c382016-12-15 01:12:40 -08001613 primary_netname = None
Ravi Chamarty1fdf9992018-10-07 15:45:44 +00001614 primary_net_href = None
kate15f1c382016-12-15 01:12:40 -08001615 network_mode = 'bridged'
bayramovb6ffe792016-09-28 11:50:56 +04001616 if net_list is not None and len(net_list) > 0:
bhangare0e571a92017-01-12 04:02:23 -08001617 for net in net_list:
tierno19860412017-10-03 10:46:46 +02001618 if 'use' in net and net['use'] == 'mgmt' and not primary_net:
bhangare0e571a92017-01-12 04:02:23 -08001619 primary_net = net
bayramovb6ffe792016-09-28 11:50:56 +04001620 if primary_net is None:
bhangare0e571a92017-01-12 04:02:23 -08001621 primary_net = net_list[0]
1622
1623 try:
1624 primary_net_id = primary_net['net_id']
Ravi Chamarty1fdf9992018-10-07 15:45:44 +00001625 url_list = [self.url, '/api/network/', primary_net_id]
1626 primary_net_href = ''.join(url_list)
bhangare0e571a92017-01-12 04:02:23 -08001627 network_dict = self.get_vcd_network(network_uuid=primary_net_id)
1628 if 'name' in network_dict:
1629 primary_netname = network_dict['name']
1630
1631 except KeyError:
1632 raise vimconn.vimconnException("Corrupted flavor. {}".format(primary_net))
1633 else:
1634 raise vimconn.vimconnUnexpectedResponse("new_vminstance(): Failed network list is empty.".format(name))
bayramovef390722016-09-27 03:34:46 -07001635
1636 # use: 'data', 'bridge', 'mgmt'
1637 # create vApp. Set vcpu and ram based on flavor id.
bhangarebfdca492017-03-11 01:32:46 -08001638 try:
kasarc5bf2932018-03-09 04:15:22 -08001639 vdc_obj = VDC(self.client, resource=org.get_vdc(self.tenant_name))
1640 if not vdc_obj:
sbhangarea8e5b782018-06-21 02:10:03 -07001641 raise vimconn.vimconnNotFoundException("new_vminstance(): Failed to get VDC object")
bhangare1a0b97c2017-06-21 02:20:15 -07001642
kasarc5bf2932018-03-09 04:15:22 -08001643 for retry in (1,2):
1644 items = org.get_catalog_item(catalog_hash_name, catalog_hash_name)
1645 catalog_items = [items.attrib]
1646
1647 if len(catalog_items) == 1:
1648 if self.client:
1649 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
1650 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
1651
1652 response = self.perform_request(req_type='GET',
1653 url=catalog_items[0].get('href'),
1654 headers=headers)
1655 catalogItem = XmlElementTree.fromstring(response.content)
1656 entity = [child for child in catalogItem if child.get("type") == "application/vnd.vmware.vcloud.vAppTemplate+xml"][0]
1657 vapp_tempalte_href = entity.get("href")
sbhangarea8e5b782018-06-21 02:10:03 -07001658
kasarc5bf2932018-03-09 04:15:22 -08001659 response = self.perform_request(req_type='GET',
1660 url=vapp_tempalte_href,
sbhangarea8e5b782018-06-21 02:10:03 -07001661 headers=headers)
kasarc5bf2932018-03-09 04:15:22 -08001662 if response.status_code != requests.codes.ok:
1663 self.logger.debug("REST API call {} failed. Return status code {}".format(vapp_tempalte_href,
1664 response.status_code))
1665 else:
1666 result = (response.content).replace("\n"," ")
1667
Ananda Baitharu94be48a2019-05-10 15:43:18 +00001668 vapp_template_tree = XmlElementTree.fromstring(response.content)
1669 children_element = [child for child in vapp_template_tree if 'Children' in child.tag][0]
1670 vm_element = [child for child in children_element if 'Vm' in child.tag][0]
1671 vm_name = vm_element.get('name')
1672 vm_id = vm_element.get('id')
1673 vm_href = vm_element.get('href')
kasarc5bf2932018-03-09 04:15:22 -08001674
1675 cpus = re.search('<rasd:Description>Number of Virtual CPUs</.*?>(\d+)</rasd:VirtualQuantity>',result).group(1)
1676 memory_mb = re.search('<rasd:Description>Memory Size</.*?>(\d+)</rasd:VirtualQuantity>',result).group(1)
1677 cores = re.search('<vmw:CoresPerSocket ovf:required.*?>(\d+)</vmw:CoresPerSocket>',result).group(1)
1678
sbhangarea8e5b782018-06-21 02:10:03 -07001679 headers['Content-Type'] = 'application/vnd.vmware.vcloud.instantiateVAppTemplateParams+xml'
kasarc5bf2932018-03-09 04:15:22 -08001680 vdc_id = vdc.get('id').split(':')[-1]
1681 instantiate_vapp_href = "{}/api/vdc/{}/action/instantiateVAppTemplate".format(self.url,
sbhangarea8e5b782018-06-21 02:10:03 -07001682 vdc_id)
kasarc5bf2932018-03-09 04:15:22 -08001683 data = """<?xml version="1.0" encoding="UTF-8"?>
1684 <InstantiateVAppTemplateParams
1685 xmlns="http://www.vmware.com/vcloud/v1.5"
1686 name="{}"
1687 deploy="false"
1688 powerOn="false"
1689 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1690 xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1">
1691 <Description>Vapp instantiation</Description>
1692 <InstantiationParams>
1693 <NetworkConfigSection>
1694 <ovf:Info>Configuration parameters for logical networks</ovf:Info>
Ravi Chamarty1fdf9992018-10-07 15:45:44 +00001695 <NetworkConfig networkName="{}">
kasarc5bf2932018-03-09 04:15:22 -08001696 <Configuration>
Ravi Chamarty1fdf9992018-10-07 15:45:44 +00001697 <ParentNetwork href="{}" />
kasarc5bf2932018-03-09 04:15:22 -08001698 <FenceMode>bridged</FenceMode>
1699 </Configuration>
1700 </NetworkConfig>
1701 </NetworkConfigSection>
1702 <LeaseSettingsSection
1703 type="application/vnd.vmware.vcloud.leaseSettingsSection+xml">
1704 <ovf:Info>Lease Settings</ovf:Info>
1705 <StorageLeaseInSeconds>172800</StorageLeaseInSeconds>
1706 <StorageLeaseExpiration>2014-04-25T08:08:16.438-07:00</StorageLeaseExpiration>
1707 </LeaseSettingsSection>
1708 </InstantiationParams>
sbhangarea8e5b782018-06-21 02:10:03 -07001709 <Source href="{}"/>
kasarc5bf2932018-03-09 04:15:22 -08001710 <SourcedItem>
1711 <Source href="{}" id="{}" name="{}"
1712 type="application/vnd.vmware.vcloud.vm+xml"/>
1713 <VmGeneralParams>
1714 <NeedsCustomization>false</NeedsCustomization>
1715 </VmGeneralParams>
1716 <InstantiationParams>
1717 <NetworkConnectionSection>
1718 <ovf:Info>Specifies the available VM network connections</ovf:Info>
1719 <NetworkConnection network="{}">
1720 <NetworkConnectionIndex>0</NetworkConnectionIndex>
1721 <IsConnected>true</IsConnected>
sbhangarea8e5b782018-06-21 02:10:03 -07001722 <IpAddressAllocationMode>DHCP</IpAddressAllocationMode>
1723 </NetworkConnection>
kasarc5bf2932018-03-09 04:15:22 -08001724 </NetworkConnectionSection><ovf:VirtualHardwareSection>
1725 <ovf:Info>Virtual hardware requirements</ovf:Info>
1726 <ovf:Item xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData"
1727 xmlns:vmw="http://www.vmware.com/schema/ovf">
1728 <rasd:AllocationUnits>hertz * 10^6</rasd:AllocationUnits>
1729 <rasd:Description>Number of Virtual CPUs</rasd:Description>
1730 <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 -07001731 <rasd:InstanceID>4</rasd:InstanceID>
kasarc5bf2932018-03-09 04:15:22 -08001732 <rasd:Reservation>0</rasd:Reservation>
1733 <rasd:ResourceType>3</rasd:ResourceType>
1734 <rasd:VirtualQuantity xmlns:py="http://codespeak.net/lxml/objectify/pytype" py:pytype="int">{cpu}</rasd:VirtualQuantity>
1735 <rasd:Weight>0</rasd:Weight>
1736 <vmw:CoresPerSocket ovf:required="false">{core}</vmw:CoresPerSocket>
1737 </ovf:Item><ovf:Item xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData">
1738 <rasd:AllocationUnits>byte * 2^20</rasd:AllocationUnits>
1739 <rasd:Description>Memory Size</rasd:Description>
1740 <rasd:ElementName xmlns:py="http://codespeak.net/lxml/objectify/pytype" py:pytype="str">{memory} MB of memory</rasd:ElementName>
1741 <rasd:InstanceID>5</rasd:InstanceID>
1742 <rasd:Reservation>0</rasd:Reservation>
1743 <rasd:ResourceType>4</rasd:ResourceType>
1744 <rasd:VirtualQuantity xmlns:py="http://codespeak.net/lxml/objectify/pytype" py:pytype="int">{memory}</rasd:VirtualQuantity>
1745 <rasd:Weight>0</rasd:Weight>
1746 </ovf:Item>
sbhangarea8e5b782018-06-21 02:10:03 -07001747 </ovf:VirtualHardwareSection>
kasarc5bf2932018-03-09 04:15:22 -08001748 </InstantiationParams>
1749 </SourcedItem>
1750 <AllEULAsAccepted>false</AllEULAsAccepted>
1751 </InstantiateVAppTemplateParams>""".format(vmname_andid,
Ravi Chamarty1fdf9992018-10-07 15:45:44 +00001752 primary_netname,
1753 primary_net_href,
kasarc5bf2932018-03-09 04:15:22 -08001754 vapp_tempalte_href,
1755 vm_href,
1756 vm_id,
1757 vm_name,
1758 primary_netname,
1759 cpu=cpus,
1760 core=cores,
1761 memory=memory_mb)
1762
1763 response = self.perform_request(req_type='POST',
1764 url=instantiate_vapp_href,
1765 headers=headers,
1766 data=data)
1767
1768 if response.status_code != 201:
1769 self.logger.error("REST call {} failed reason : {}"\
1770 "status code : {}".format(instantiate_vapp_href,
1771 response.content,
1772 response.status_code))
1773 raise vimconn.vimconnException("new_vminstance(): Failed to create"\
1774 "vAapp {}".format(vmname_andid))
1775 else:
1776 vapptask = self.get_task_from_response(response.content)
1777
1778 if vapptask is None and retry==1:
bhangare68e73e62017-07-04 22:44:01 -07001779 self.get_token() # Retry getting token
1780 continue
1781 else:
1782 break
1783
bhangare1a0b97c2017-06-21 02:20:15 -07001784 if vapptask is None or vapptask is False:
bhangarebfdca492017-03-11 01:32:46 -08001785 raise vimconn.vimconnUnexpectedResponse(
1786 "new_vminstance(): failed to create vApp {}".format(vmname_andid))
kasarc5bf2932018-03-09 04:15:22 -08001787
sbhangarea8e5b782018-06-21 02:10:03 -07001788 # wait for task to complete
kasarc5bf2932018-03-09 04:15:22 -08001789 result = self.client.get_task_monitor().wait_for_success(task=vapptask)
1790
1791 if result.get('status') == 'success':
1792 self.logger.debug("new_vminstance(): Sucessfully created Vapp {}".format(vmname_andid))
1793 else:
1794 raise vimconn.vimconnUnexpectedResponse(
1795 "new_vminstance(): failed to create vApp {}".format(vmname_andid))
bhangarebfdca492017-03-11 01:32:46 -08001796
1797 except Exception as exp:
1798 raise vimconn.vimconnUnexpectedResponse(
1799 "new_vminstance(): failed to create vApp {} with Exception:{}".format(vmname_andid, exp))
bayramovef390722016-09-27 03:34:46 -07001800
1801 # we should have now vapp in undeployed state.
bhangarebfdca492017-03-11 01:32:46 -08001802 try:
kasarc5bf2932018-03-09 04:15:22 -08001803 vdc_obj = VDC(self.client, href=vdc.get('href'))
1804 vapp_resource = vdc_obj.get_vapp(vmname_andid)
1805 vapp_uuid = vapp_resource.get('id').split(':')[-1]
1806 vapp = VApp(self.client, resource=vapp_resource)
bhangare1a0b97c2017-06-21 02:20:15 -07001807
bhangarebfdca492017-03-11 01:32:46 -08001808 except Exception as exp:
1809 raise vimconn.vimconnUnexpectedResponse(
1810 "new_vminstance(): Failed to retrieve vApp {} after creation: Exception:{}"
1811 .format(vmname_andid, exp))
1812
bhangare1a0b97c2017-06-21 02:20:15 -07001813 if vapp_uuid is None:
bayramovbd6160f2016-09-28 04:12:05 +04001814 raise vimconn.vimconnUnexpectedResponse(
bhangarebfdca492017-03-11 01:32:46 -08001815 "new_vminstance(): Failed to retrieve vApp {} after creation".format(
bhangarea92ae392017-01-12 22:30:29 -08001816 vmname_andid))
1817
kateac1e3792017-04-01 02:16:39 -07001818 #Add PCI passthrough/SRIOV configrations
bhangarefda5f7c2017-01-12 23:50:34 -08001819 vm_obj = None
kateac1e3792017-04-01 02:16:39 -07001820 pci_devices_info = []
kateac1e3792017-04-01 02:16:39 -07001821 reserve_memory = False
1822
1823 for net in net_list:
tierno66eba6e2017-11-10 17:09:18 +01001824 if net["type"] == "PF" or net["type"] == "PCI-PASSTHROUGH":
kateac1e3792017-04-01 02:16:39 -07001825 pci_devices_info.append(net)
tierno66eba6e2017-11-10 17:09:18 +01001826 elif (net["type"] == "VF" or net["type"] == "SR-IOV" or net["type"] == "VFnotShared") and 'net_id'in net:
Ravi Chamartye21c9cc2018-10-24 02:14:53 +00001827 reserve_memory = True
kateac1e3792017-04-01 02:16:39 -07001828
1829 #Add PCI
bhangarefda5f7c2017-01-12 23:50:34 -08001830 if len(pci_devices_info) > 0:
1831 self.logger.info("Need to add PCI devices {} into VM {}".format(pci_devices_info,
1832 vmname_andid ))
1833 PCI_devices_status, vm_obj, vcenter_conect = self.add_pci_devices(vapp_uuid,
1834 pci_devices_info,
1835 vmname_andid)
1836 if PCI_devices_status:
1837 self.logger.info("Added PCI devives {} to VM {}".format(
1838 pci_devices_info,
1839 vmname_andid)
1840 )
kateac1e3792017-04-01 02:16:39 -07001841 reserve_memory = True
bhangarefda5f7c2017-01-12 23:50:34 -08001842 else:
1843 self.logger.info("Fail to add PCI devives {} to VM {}".format(
1844 pci_devices_info,
1845 vmname_andid)
1846 )
bhangare1a0b97c2017-06-21 02:20:15 -07001847
bhangare06312472017-03-30 05:49:07 -07001848 # Modify vm disk
bhangarea92ae392017-01-12 22:30:29 -08001849 if vm_disk:
1850 #Assuming there is only one disk in ovf and fast provisioning in organization vDC is disabled
1851 result = self.modify_vm_disk(vapp_uuid, vm_disk)
1852 if result :
1853 self.logger.debug("Modified Disk size of VM {} ".format(vmname_andid))
bayramovef390722016-09-27 03:34:46 -07001854
bhangare06312472017-03-30 05:49:07 -07001855 #Add new or existing disks to vApp
1856 if disk_list:
1857 added_existing_disk = False
1858 for disk in disk_list:
kasar0c007d62017-05-19 03:13:57 -07001859 if 'device_type' in disk and disk['device_type'] == 'cdrom':
1860 image_id = disk['image_id']
1861 # Adding CD-ROM to VM
1862 # will revisit code once specification ready to support this feature
1863 self.insert_media_to_vm(vapp, image_id)
1864 elif "image_id" in disk and disk["image_id"] is not None:
bhangare06312472017-03-30 05:49:07 -07001865 self.logger.debug("Adding existing disk from image {} to vm {} ".format(
1866 disk["image_id"] , vapp_uuid))
1867 self.add_existing_disk(catalogs=catalogs,
1868 image_id=disk["image_id"],
1869 size = disk["size"],
1870 template_name=templateName,
1871 vapp_uuid=vapp_uuid
1872 )
1873 added_existing_disk = True
1874 else:
1875 #Wait till added existing disk gets reflected into vCD database/API
1876 if added_existing_disk:
1877 time.sleep(5)
1878 added_existing_disk = False
bhangare1a0b97c2017-06-21 02:20:15 -07001879 self.add_new_disk(vapp_uuid, disk['size'])
bhangare06312472017-03-30 05:49:07 -07001880
kasarde691232017-03-25 03:37:31 -07001881 if numas:
1882 # Assigning numa affinity setting
1883 for numa in numas:
1884 if 'paired-threads-id' in numa:
1885 paired_threads_id = numa['paired-threads-id']
1886 self.set_numa_affinity(vapp_uuid, paired_threads_id)
1887
bhangare0e571a92017-01-12 04:02:23 -08001888 # add NICs & connect to networks in netlist
bayramovef390722016-09-27 03:34:46 -07001889 try:
Ravi Chamarty5b2d5c12018-10-22 23:59:10 +00001890 vdc_obj = VDC(self.client, href=vdc.get('href'))
1891 vapp_resource = vdc_obj.get_vapp(vmname_andid)
1892 vapp = VApp(self.client, resource=vapp_resource)
Ravi Chamartyb0ab2c02018-10-26 01:40:50 +00001893 vapp_id = vapp_resource.get('id').split(':')[-1]
Ravi Chamarty5b2d5c12018-10-22 23:59:10 +00001894
1895 self.logger.info("Removing primary NIC: ")
1896 # First remove all NICs so that NIC properties can be adjusted as needed
1897 self.remove_primary_network_adapter_from_all_vms(vapp)
1898
kate15f1c382016-12-15 01:12:40 -08001899 self.logger.info("Request to connect VM to a network: {}".format(net_list))
bhangare0e571a92017-01-12 04:02:23 -08001900 primary_nic_index = 0
Ravi Chamarty1fdf9992018-10-07 15:45:44 +00001901 nicIndex = 0
bayramovef390722016-09-27 03:34:46 -07001902 for net in net_list:
1903 # openmano uses network id in UUID format.
1904 # vCloud Director need a name so we do reverse operation from provided UUID we lookup a name
kate15f1c382016-12-15 01:12:40 -08001905 # [{'use': 'bridge', 'net_id': '527d4bf7-566a-41e7-a9e7-ca3cdd9cef4f', 'type': 'virtual',
1906 # 'vpci': '0000:00:11.0', 'name': 'eth0'}]
1907
1908 if 'net_id' not in net:
1909 continue
1910
bhangare97b192d2017-10-05 00:51:15 -07001911 #Using net_id as a vim_id i.e. vim interface id, as do not have saperate vim interface id
1912 #Same will be returned in refresh_vms_status() as vim_interface_id
1913 net['vim_id'] = net['net_id'] # Provide the same VIM identifier as the VIM network
tierno19860412017-10-03 10:46:46 +02001914
bayramovef390722016-09-27 03:34:46 -07001915 interface_net_id = net['net_id']
kate15f1c382016-12-15 01:12:40 -08001916 interface_net_name = self.get_network_name_by_id(network_uuid=interface_net_id)
bayramovef390722016-09-27 03:34:46 -07001917 interface_network_mode = net['use']
1918
bhangare0e571a92017-01-12 04:02:23 -08001919 if interface_network_mode == 'mgmt':
1920 primary_nic_index = nicIndex
1921
kate15f1c382016-12-15 01:12:40 -08001922 """- POOL (A static IP address is allocated automatically from a pool of addresses.)
1923 - DHCP (The IP address is obtained from a DHCP service.)
1924 - MANUAL (The IP address is assigned manually in the IpAddress element.)
1925 - NONE (No IP addressing mode specified.)"""
1926
1927 if primary_netname is not None:
Ravi Chamarty5b2d5c12018-10-22 23:59:10 +00001928 self.logger.debug("new_vminstance(): Filtering by net name {}".format(interface_net_name))
kasarc5bf2932018-03-09 04:15:22 -08001929 nets = filter(lambda n: n.get('name') == interface_net_name, self.get_network_list())
1930 #For python3
1931 #nets = [n for n in self.get_network_list() if n.get('name') == interface_net_name]
bayramovef390722016-09-27 03:34:46 -07001932 if len(nets) == 1:
kasarc5bf2932018-03-09 04:15:22 -08001933 self.logger.info("new_vminstance(): Found requested network: {}".format(nets[0].get('name')))
bhangare1a0b97c2017-06-21 02:20:15 -07001934
Ravi Chamarty5b2d5c12018-10-22 23:59:10 +00001935 if interface_net_name != primary_netname:
1936 # connect network to VM - with all DHCP by default
1937 self.logger.info("new_vminstance(): Attaching net {} to vapp".format(interface_net_name))
Ravi Chamartyb0ab2c02018-10-26 01:40:50 +00001938 self.connect_vapp_to_org_vdc_network(vapp_id, nets[0].get('name'))
kasar3ac5dc42017-03-15 06:28:22 -07001939
Ravi Chamartye21c9cc2018-10-24 02:14:53 +00001940 type_list = ('PF', 'PCI-PASSTHROUGH', 'VFnotShared')
Ravi Chamarty41840092018-10-31 16:12:38 +00001941 nic_type = 'VMXNET3'
kasar3ac5dc42017-03-15 06:28:22 -07001942 if 'type' in net and net['type'] not in type_list:
1943 # fetching nic type from vnf
1944 if 'model' in net:
garciadeblas31e141b2018-10-25 18:33:19 +02001945 if net['model'] is not None:
1946 if net['model'].lower() == 'paravirt' or net['model'].lower() == 'virtio':
1947 nic_type = 'VMXNET3'
kasar204e39e2018-01-25 00:57:02 -08001948 else:
1949 nic_type = net['model']
1950
kasar3ac5dc42017-03-15 06:28:22 -07001951 self.logger.info("new_vminstance(): adding network adapter "\
kasarc5bf2932018-03-09 04:15:22 -08001952 "to a network {}".format(nets[0].get('name')))
1953 self.add_network_adapter_to_vms(vapp, nets[0].get('name'),
kasar3ac5dc42017-03-15 06:28:22 -07001954 primary_nic_index,
1955 nicIndex,
kasardc1f02e2017-03-25 07:20:30 -07001956 net,
kasar3ac5dc42017-03-15 06:28:22 -07001957 nic_type=nic_type)
1958 else:
1959 self.logger.info("new_vminstance(): adding network adapter "\
kasarc5bf2932018-03-09 04:15:22 -08001960 "to a network {}".format(nets[0].get('name')))
Ravi Chamarty5063a982018-10-26 13:47:14 +00001961 if net['type'] in ['SR-IOV', 'VF']:
1962 nic_type = net['type']
kasarc5bf2932018-03-09 04:15:22 -08001963 self.add_network_adapter_to_vms(vapp, nets[0].get('name'),
kasar3ac5dc42017-03-15 06:28:22 -07001964 primary_nic_index,
kasardc1f02e2017-03-25 07:20:30 -07001965 nicIndex,
Ravi Chamarty5063a982018-10-26 13:47:14 +00001966 net,
1967 nic_type=nic_type)
bhangare0e571a92017-01-12 04:02:23 -08001968 nicIndex += 1
bayramovef390722016-09-27 03:34:46 -07001969
kasardc1f02e2017-03-25 07:20:30 -07001970 # cloud-init for ssh-key injection
1971 if cloud_config:
Ananda Baitharu319c26f2019-03-05 17:34:31 +00001972 # Create a catalog which will be carrying the config drive ISO
1973 # This catalog is deleted during vApp deletion. The catalog name carries
1974 # vApp UUID and thats how it gets identified during its deletion.
1975 config_drive_catalog_name = 'cfg_drv-' + vapp_uuid
1976 self.logger.info('new_vminstance(): Creating catalog "{}" to carry config drive ISO'.format(
1977 config_drive_catalog_name))
1978 config_drive_catalog_id = self.create_vimcatalog(org, config_drive_catalog_name)
1979 if config_drive_catalog_id is None:
1980 error_msg = "new_vminstance(): Failed to create new catalog '{}' to carry the config drive " \
1981 "ISO".format(config_drive_catalog_name)
1982 raise Exception(error_msg)
1983
1984 # Create config-drive ISO
1985 _, userdata = self._create_user_data(cloud_config)
1986 # self.logger.debug('new_vminstance(): The userdata for cloud-init: {}'.format(userdata))
1987 iso_path = self.create_config_drive_iso(userdata)
1988 self.logger.debug('new_vminstance(): The ISO is successfully created. Path: {}'.format(iso_path))
1989
1990 self.logger.info('new_vminstance(): uploading iso to catalog {}'.format(config_drive_catalog_name))
1991 self.upload_iso_to_catalog(config_drive_catalog_id, iso_path)
1992 # Attach the config-drive ISO to the VM
1993 self.logger.info('new_vminstance(): Attaching the config-drive ISO to the VM')
1994 # The ISO remains in INVALID_STATE right after the PUT request (its a blocking call though)
1995 time.sleep(5)
1996 self.insert_media_to_vm(vapp, config_drive_catalog_id)
1997 shutil.rmtree(os.path.dirname(iso_path), ignore_errors=True)
kasardc1f02e2017-03-25 07:20:30 -07001998
kateac1e3792017-04-01 02:16:39 -07001999 # If VM has PCI devices or SRIOV reserve memory for VM
2000 if reserve_memory:
Ravi Chamartye21c9cc2018-10-24 02:14:53 +00002001 self.reserve_memory_for_all_vms(vapp, memory_mb)
bhangarefda5f7c2017-01-12 23:50:34 -08002002
kasarc5bf2932018-03-09 04:15:22 -08002003 self.logger.debug("new_vminstance(): starting power on vApp {} ".format(vmname_andid))
bhangare1a0b97c2017-06-21 02:20:15 -07002004
kasarc5bf2932018-03-09 04:15:22 -08002005 poweron_task = self.power_on_vapp(vapp_id, vmname_andid)
2006 result = self.client.get_task_monitor().wait_for_success(task=poweron_task)
2007 if result.get('status') == 'success':
2008 self.logger.info("new_vminstance(): Successfully power on "\
2009 "vApp {}".format(vmname_andid))
2010 else:
2011 self.logger.error("new_vminstance(): failed to power on vApp "\
2012 "{}".format(vmname_andid))
bhangarebfdca492017-03-11 01:32:46 -08002013
Ananda Baitharu319c26f2019-03-05 17:34:31 +00002014 except Exception as exp:
2015 try:
2016 self.delete_vminstance(vapp_uuid)
2017 except Exception as exp2:
2018 self.logger.error("new_vminstance rollback fail {}".format(exp2))
bhangarebfdca492017-03-11 01:32:46 -08002019 # it might be a case if specific mandatory entry in dict is empty or some other pyVcloud exception
kasarc5bf2932018-03-09 04:15:22 -08002020 self.logger.error("new_vminstance(): Failed create new vm instance {} with exception {}"
bhangare1a0b97c2017-06-21 02:20:15 -07002021 .format(name, exp))
2022 raise vimconn.vimconnException("new_vminstance(): Failed create new vm instance {} with exception {}"
2023 .format(name, exp))
bhangarefda5f7c2017-01-12 23:50:34 -08002024
bayramovef390722016-09-27 03:34:46 -07002025 # check if vApp deployed and if that the case return vApp UUID otherwise -1
kate13ab2c42016-12-23 01:34:24 -08002026 wait_time = 0
2027 vapp_uuid = None
2028 while wait_time <= MAX_WAIT_TIME:
bhangarebfdca492017-03-11 01:32:46 -08002029 try:
kasarc5bf2932018-03-09 04:15:22 -08002030 vapp_resource = vdc_obj.get_vapp(vmname_andid)
sbhangarea8e5b782018-06-21 02:10:03 -07002031 vapp = VApp(self.client, resource=vapp_resource)
bhangarebfdca492017-03-11 01:32:46 -08002032 except Exception as exp:
2033 raise vimconn.vimconnUnexpectedResponse(
2034 "new_vminstance(): Failed to retrieve vApp {} after creation: Exception:{}"
2035 .format(vmname_andid, exp))
2036
kasarc5bf2932018-03-09 04:15:22 -08002037 #if vapp and vapp.me.deployed:
2038 if vapp and vapp_resource.get('deployed') == 'true':
2039 vapp_uuid = vapp_resource.get('id').split(':')[-1]
kate13ab2c42016-12-23 01:34:24 -08002040 break
2041 else:
2042 self.logger.debug("new_vminstance(): Wait for vApp {} to deploy".format(name))
2043 time.sleep(INTERVAL_TIME)
2044
2045 wait_time +=INTERVAL_TIME
2046
sbhangarea8e5b782018-06-21 02:10:03 -07002047 #SET Affinity Rule for VM
2048 #Pre-requisites: User has created Hosh Groups in vCenter with respective Hosts to be used
2049 #While creating VIM account user has to pass the Host Group names in availability_zone list
2050 #"availability_zone" is a part of VIM "config" parameters
2051 #For example, in VIM config: "availability_zone":["HG_170","HG_174","HG_175"]
2052 #Host groups are referred as availability zones
2053 #With following procedure, deployed VM will be added into a VM group.
2054 #Then A VM to Host Affinity rule will be created using the VM group & Host group.
2055 if(availability_zone_list):
2056 self.logger.debug("Existing Host Groups in VIM {}".format(self.config.get('availability_zone')))
2057 #Admin access required for creating Affinity rules
2058 client = self.connect_as_admin()
2059 if not client:
2060 raise vimconn.vimconnConnectionException("Failed to connect vCD as admin")
2061 else:
2062 self.client = client
2063 if self.client:
2064 headers = {'Accept':'application/*+xml;version=27.0',
2065 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
2066 #Step1: Get provider vdc details from organization
2067 pvdc_href = self.get_pvdc_for_org(self.tenant_name, headers)
2068 if pvdc_href is not None:
2069 #Step2: Found required pvdc, now get resource pool information
2070 respool_href = self.get_resource_pool_details(pvdc_href, headers)
2071 if respool_href is None:
2072 #Raise error if respool_href not found
2073 msg = "new_vminstance():Error in finding resource pool details in pvdc {}"\
2074 .format(pvdc_href)
2075 self.log_message(msg)
2076
2077 #Step3: Verify requested availability zone(hostGroup) is present in vCD
2078 # get availability Zone
2079 vm_az = self.get_vm_availability_zone(availability_zone_index, availability_zone_list)
2080 # check if provided av zone(hostGroup) is present in vCD VIM
2081 status = self.check_availibility_zone(vm_az, respool_href, headers)
2082 if status is False:
2083 msg = "new_vminstance(): Error in finding availability zone(Host Group): {} in "\
2084 "resource pool {} status: {}".format(vm_az,respool_href,status)
2085 self.log_message(msg)
2086 else:
2087 self.logger.debug ("new_vminstance(): Availability zone {} found in VIM".format(vm_az))
2088
2089 #Step4: Find VM group references to create vm group
2090 vmgrp_href = self.find_vmgroup_reference(respool_href, headers)
2091 if vmgrp_href == None:
2092 msg = "new_vminstance(): No reference to VmGroup found in resource pool"
2093 self.log_message(msg)
2094
2095 #Step5: Create a VmGroup with name az_VmGroup
2096 vmgrp_name = vm_az + "_" + name #Formed VM Group name = Host Group name + VM name
2097 status = self.create_vmgroup(vmgrp_name, vmgrp_href, headers)
2098 if status is not True:
2099 msg = "new_vminstance(): Error in creating VM group {}".format(vmgrp_name)
2100 self.log_message(msg)
2101
2102 #VM Group url to add vms to vm group
2103 vmgrpname_url = self.url + "/api/admin/extension/vmGroup/name/"+ vmgrp_name
2104
2105 #Step6: Add VM to VM Group
2106 #Find VM uuid from vapp_uuid
2107 vm_details = self.get_vapp_details_rest(vapp_uuid)
2108 vm_uuid = vm_details['vmuuid']
2109
2110 status = self.add_vm_to_vmgroup(vm_uuid, vmgrpname_url, vmgrp_name, headers)
2111 if status is not True:
2112 msg = "new_vminstance(): Error in adding VM to VM group {}".format(vmgrp_name)
2113 self.log_message(msg)
2114
2115 #Step7: Create VM to Host affinity rule
2116 addrule_href = self.get_add_rule_reference (respool_href, headers)
2117 if addrule_href is None:
2118 msg = "new_vminstance(): Error in finding href to add rule in resource pool: {}"\
2119 .format(respool_href)
2120 self.log_message(msg)
2121
2122 status = self.create_vm_to_host_affinity_rule(addrule_href, vmgrp_name, vm_az, "Affinity", headers)
2123 if status is False:
2124 msg = "new_vminstance(): Error in creating affinity rule for VM {} in Host group {}"\
2125 .format(name, vm_az)
2126 self.log_message(msg)
2127 else:
2128 self.logger.debug("new_vminstance(): Affinity rule created successfully. Added {} in Host group {}"\
2129 .format(name, vm_az))
2130 #Reset token to a normal user to perform other operations
2131 self.get_token()
2132
bayramovef390722016-09-27 03:34:46 -07002133 if vapp_uuid is not None:
tierno98e909c2017-10-14 13:27:03 +02002134 return vapp_uuid, None
bayramovbd6160f2016-09-28 04:12:05 +04002135 else:
2136 raise vimconn.vimconnUnexpectedResponse("new_vminstance(): Failed create new vm instance {}".format(name))
bayramov325fa1c2016-09-08 01:42:46 -07002137
Ananda Baitharu319c26f2019-03-05 17:34:31 +00002138 def create_config_drive_iso(self, user_data):
2139 tmpdir = tempfile.mkdtemp()
2140 iso_path = os.path.join(tmpdir, 'ConfigDrive.iso')
2141 latest_dir = os.path.join(tmpdir, 'openstack', 'latest')
2142 os.makedirs(latest_dir)
2143 with open(os.path.join(latest_dir, 'meta_data.json'), 'w') as meta_file_obj, \
2144 open(os.path.join(latest_dir, 'user_data'), 'w') as userdata_file_obj:
2145 userdata_file_obj.write(user_data)
2146 meta_file_obj.write(json.dumps({"availability_zone": "nova",
2147 "launch_index": 0,
2148 "name": "ConfigDrive",
2149 "uuid": str(uuid.uuid4())}
2150 )
2151 )
2152 genisoimage_cmd = 'genisoimage -J -r -V config-2 -o {iso_path} {source_dir_path}'.format(
2153 iso_path=iso_path, source_dir_path=tmpdir)
2154 self.logger.info('create_config_drive_iso(): Creating ISO by running command "{}"'.format(genisoimage_cmd))
2155 try:
2156 FNULL = open(os.devnull, 'w')
2157 subprocess.check_call(genisoimage_cmd, shell=True, stdout=FNULL)
2158 except subprocess.CalledProcessError as e:
2159 shutil.rmtree(tmpdir, ignore_errors=True)
2160 error_msg = 'create_config_drive_iso(): Exception while running genisoimage command: {}'.format(e)
2161 self.logger.error(error_msg)
2162 raise Exception(error_msg)
2163 return iso_path
2164
2165 def upload_iso_to_catalog(self, catalog_id, iso_file_path):
2166 if not os.path.isfile(iso_file_path):
2167 error_msg = "upload_iso_to_catalog(): Given iso file is not present. Given path: {}".format(iso_file_path)
2168 self.logger.error(error_msg)
2169 raise Exception(error_msg)
2170 iso_file_stat = os.stat(iso_file_path)
2171 xml_media_elem = '''<?xml version="1.0" encoding="UTF-8"?>
2172 <Media
2173 xmlns="http://www.vmware.com/vcloud/v1.5"
2174 name="{iso_name}"
2175 size="{iso_size}"
2176 imageType="iso">
2177 <Description>ISO image for config-drive</Description>
2178 </Media>'''.format(iso_name=os.path.basename(iso_file_path), iso_size=iso_file_stat.st_size)
2179 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
2180 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
2181 headers['Content-Type'] = 'application/vnd.vmware.vcloud.media+xml'
2182 catalog_href = self.url + '/api/catalog/' + catalog_id + '/action/upload'
2183 response = self.perform_request(req_type='POST', url=catalog_href, headers=headers, data=xml_media_elem)
2184
2185 if response.status_code != 201:
2186 error_msg = "upload_iso_to_catalog(): Failed to POST an action/upload request to {}".format(catalog_href)
2187 self.logger.error(error_msg)
2188 raise Exception(error_msg)
2189
2190 catalogItem = XmlElementTree.fromstring(response.content)
2191 entity = [child for child in catalogItem if child.get("type") == "application/vnd.vmware.vcloud.media+xml"][0]
2192 entity_href = entity.get('href')
2193
2194 response = self.perform_request(req_type='GET', url=entity_href, headers=headers)
2195 if response.status_code != 200:
2196 raise Exception("upload_iso_to_catalog(): Failed to GET entity href {}".format(entity_href))
2197
2198 match = re.search(r'<Files>\s+?<File.+?href="(.+?)"/>\s+?</File>\s+?</Files>', response.text, re.DOTALL)
2199 if match:
2200 media_upload_href = match.group(1)
2201 else:
2202 raise Exception('Could not parse the upload URL for the media file from the last response')
Ananda Baitharub23558c2019-06-18 13:53:37 +00002203 upload_iso_task = self.get_task_from_response(response.content)
Ananda Baitharu319c26f2019-03-05 17:34:31 +00002204 headers['Content-Type'] = 'application/octet-stream'
2205 response = self.perform_request(req_type='PUT',
2206 url=media_upload_href,
2207 headers=headers,
2208 data=open(iso_file_path, 'rb'))
2209
2210 if response.status_code != 200:
2211 raise Exception('PUT request to "{}" failed'.format(media_upload_href))
Ananda Baitharub23558c2019-06-18 13:53:37 +00002212 result = self.client.get_task_monitor().wait_for_success(task=upload_iso_task)
2213 if result.get('status') != 'success':
2214 raise Exception('The upload iso task failed with status {}'.format(result.get('status')))
sbhangarea8e5b782018-06-21 02:10:03 -07002215
2216 def get_vcd_availibility_zones(self,respool_href, headers):
2217 """ Method to find presence of av zone is VIM resource pool
2218
2219 Args:
2220 respool_href - resource pool href
2221 headers - header information
2222
2223 Returns:
2224 vcd_az - list of azone present in vCD
2225 """
2226 vcd_az = []
2227 url=respool_href
2228 resp = self.perform_request(req_type='GET',url=respool_href, headers=headers)
2229
2230 if resp.status_code != requests.codes.ok:
2231 self.logger.debug ("REST API call {} failed. Return status code {}".format(url, resp.status_code))
2232 else:
2233 #Get the href to hostGroups and find provided hostGroup is present in it
2234 resp_xml = XmlElementTree.fromstring(resp.content)
2235 for child in resp_xml:
2236 if 'VMWProviderVdcResourcePool' in child.tag:
2237 for schild in child:
2238 if 'Link' in schild.tag:
2239 if schild.attrib.get('type') == "application/vnd.vmware.admin.vmwHostGroupsType+xml":
2240 hostGroup = schild.attrib.get('href')
2241 hg_resp = self.perform_request(req_type='GET',url=hostGroup, headers=headers)
2242 if hg_resp.status_code != requests.codes.ok:
2243 self.logger.debug ("REST API call {} failed. Return status code {}".format(hostGroup, hg_resp.status_code))
2244 else:
2245 hg_resp_xml = XmlElementTree.fromstring(hg_resp.content)
2246 for hostGroup in hg_resp_xml:
2247 if 'HostGroup' in hostGroup.tag:
2248 #append host group name to the list
2249 vcd_az.append(hostGroup.attrib.get("name"))
2250 return vcd_az
2251
2252
2253 def set_availability_zones(self):
2254 """
2255 Set vim availability zone
2256 """
2257
2258 vim_availability_zones = None
2259 availability_zone = None
2260 if 'availability_zone' in self.config:
2261 vim_availability_zones = self.config.get('availability_zone')
2262 if isinstance(vim_availability_zones, str):
2263 availability_zone = [vim_availability_zones]
2264 elif isinstance(vim_availability_zones, list):
2265 availability_zone = vim_availability_zones
2266 else:
2267 return availability_zone
2268
2269 return availability_zone
2270
2271
2272 def get_vm_availability_zone(self, availability_zone_index, availability_zone_list):
2273 """
2274 Return the availability zone to be used by the created VM.
2275 returns: The VIM availability zone to be used or None
2276 """
2277 if availability_zone_index is None:
2278 if not self.config.get('availability_zone'):
2279 return None
2280 elif isinstance(self.config.get('availability_zone'), str):
2281 return self.config['availability_zone']
2282 else:
2283 return self.config['availability_zone'][0]
2284
2285 vim_availability_zones = self.availability_zone
2286
2287 # check if VIM offer enough availability zones describe in the VNFD
2288 if vim_availability_zones and len(availability_zone_list) <= len(vim_availability_zones):
2289 # check if all the names of NFV AV match VIM AV names
2290 match_by_index = False
2291 for av in availability_zone_list:
2292 if av not in vim_availability_zones:
2293 match_by_index = True
2294 break
2295 if match_by_index:
2296 self.logger.debug("Required Availability zone or Host Group not found in VIM config")
2297 self.logger.debug("Input Availability zone list: {}".format(availability_zone_list))
2298 self.logger.debug("VIM configured Availability zones: {}".format(vim_availability_zones))
2299 self.logger.debug("VIM Availability zones will be used by index")
2300 return vim_availability_zones[availability_zone_index]
2301 else:
2302 return availability_zone_list[availability_zone_index]
2303 else:
2304 raise vimconn.vimconnConflictException("No enough availability zones at VIM for this deployment")
2305
2306
2307 def create_vm_to_host_affinity_rule(self, addrule_href, vmgrpname, hostgrpname, polarity, headers):
2308 """ Method to create VM to Host Affinity rule in vCD
2309
2310 Args:
2311 addrule_href - href to make a POST request
2312 vmgrpname - name of the VM group created
2313 hostgrpnmae - name of the host group created earlier
2314 polarity - Affinity or Anti-affinity (default: Affinity)
2315 headers - headers to make REST call
2316
2317 Returns:
2318 True- if rule is created
2319 False- Failed to create rule due to some error
2320
2321 """
2322 task_status = False
2323 rule_name = polarity + "_" + vmgrpname
2324 payload = """<?xml version="1.0" encoding="UTF-8"?>
2325 <vmext:VMWVmHostAffinityRule
2326 xmlns:vmext="http://www.vmware.com/vcloud/extension/v1.5"
2327 xmlns:vcloud="http://www.vmware.com/vcloud/v1.5"
2328 type="application/vnd.vmware.admin.vmwVmHostAffinityRule+xml">
2329 <vcloud:Name>{}</vcloud:Name>
2330 <vcloud:IsEnabled>true</vcloud:IsEnabled>
2331 <vcloud:IsMandatory>true</vcloud:IsMandatory>
2332 <vcloud:Polarity>{}</vcloud:Polarity>
2333 <vmext:HostGroupName>{}</vmext:HostGroupName>
2334 <vmext:VmGroupName>{}</vmext:VmGroupName>
2335 </vmext:VMWVmHostAffinityRule>""".format(rule_name, polarity, hostgrpname, vmgrpname)
2336
2337 resp = self.perform_request(req_type='POST',url=addrule_href, headers=headers, data=payload)
2338
2339 if resp.status_code != requests.codes.accepted:
2340 self.logger.debug ("REST API call {} failed. Return status code {}".format(addrule_href, resp.status_code))
2341 task_status = False
2342 return task_status
2343 else:
2344 affinity_task = self.get_task_from_response(resp.content)
2345 self.logger.debug ("affinity_task: {}".format(affinity_task))
2346 if affinity_task is None or affinity_task is False:
2347 raise vimconn.vimconnUnexpectedResponse("failed to find affinity task")
2348 # wait for task to complete
2349 result = self.client.get_task_monitor().wait_for_success(task=affinity_task)
2350 if result.get('status') == 'success':
2351 self.logger.debug("Successfully created affinity rule {}".format(rule_name))
2352 return True
2353 else:
2354 raise vimconn.vimconnUnexpectedResponse(
2355 "failed to create affinity rule {}".format(rule_name))
2356
2357
2358 def get_add_rule_reference (self, respool_href, headers):
2359 """ This method finds href to add vm to host affinity rule to vCD
2360
2361 Args:
2362 respool_href- href to resource pool
2363 headers- header information to make REST call
2364
2365 Returns:
2366 None - if no valid href to add rule found or
2367 addrule_href - href to add vm to host affinity rule of resource pool
2368 """
2369 addrule_href = None
2370 resp = self.perform_request(req_type='GET',url=respool_href, headers=headers)
2371
2372 if resp.status_code != requests.codes.ok:
2373 self.logger.debug ("REST API call {} failed. Return status code {}".format(respool_href, resp.status_code))
2374 else:
2375
2376 resp_xml = XmlElementTree.fromstring(resp.content)
2377 for child in resp_xml:
2378 if 'VMWProviderVdcResourcePool' in child.tag:
2379 for schild in child:
2380 if 'Link' in schild.tag:
2381 if schild.attrib.get('type') == "application/vnd.vmware.admin.vmwVmHostAffinityRule+xml" and \
2382 schild.attrib.get('rel') == "add":
2383 addrule_href = schild.attrib.get('href')
2384 break
2385
2386 return addrule_href
2387
2388
2389 def add_vm_to_vmgroup(self, vm_uuid, vmGroupNameURL, vmGroup_name, headers):
2390 """ Method to add deployed VM to newly created VM Group.
2391 This is required to create VM to Host affinity in vCD
2392
2393 Args:
2394 vm_uuid- newly created vm uuid
2395 vmGroupNameURL- URL to VM Group name
2396 vmGroup_name- Name of VM group created
2397 headers- Headers for REST request
2398
2399 Returns:
2400 True- if VM added to VM group successfully
2401 False- if any error encounter
2402 """
2403
2404 addvm_resp = self.perform_request(req_type='GET',url=vmGroupNameURL, headers=headers)#, data=payload)
2405
2406 if addvm_resp.status_code != requests.codes.ok:
2407 self.logger.debug ("REST API call to get VM Group Name url {} failed. Return status code {}"\
2408 .format(vmGroupNameURL, addvm_resp.status_code))
2409 return False
2410 else:
2411 resp_xml = XmlElementTree.fromstring(addvm_resp.content)
2412 for child in resp_xml:
2413 if child.tag.split('}')[1] == 'Link':
2414 if child.attrib.get("rel") == "addVms":
2415 addvmtogrpURL = child.attrib.get("href")
2416
2417 #Get vm details
2418 url_list = [self.url, '/api/vApp/vm-',vm_uuid]
2419 vmdetailsURL = ''.join(url_list)
2420
2421 resp = self.perform_request(req_type='GET',url=vmdetailsURL, headers=headers)
2422
2423 if resp.status_code != requests.codes.ok:
2424 self.logger.debug ("REST API call {} failed. Return status code {}".format(vmdetailsURL, resp.status_code))
2425 return False
2426
2427 #Parse VM details
2428 resp_xml = XmlElementTree.fromstring(resp.content)
2429 if resp_xml.tag.split('}')[1] == "Vm":
2430 vm_id = resp_xml.attrib.get("id")
2431 vm_name = resp_xml.attrib.get("name")
2432 vm_href = resp_xml.attrib.get("href")
2433 #print vm_id, vm_name, vm_href
2434 #Add VM into VMgroup
2435 payload = """<?xml version="1.0" encoding="UTF-8"?>\
2436 <ns2:Vms xmlns:ns2="http://www.vmware.com/vcloud/v1.5" \
2437 xmlns="http://www.vmware.com/vcloud/versions" \
2438 xmlns:ns3="http://schemas.dmtf.org/ovf/envelope/1" \
2439 xmlns:ns4="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData" \
2440 xmlns:ns5="http://schemas.dmtf.org/wbem/wscim/1/common" \
2441 xmlns:ns6="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData" \
2442 xmlns:ns7="http://www.vmware.com/schema/ovf" \
2443 xmlns:ns8="http://schemas.dmtf.org/ovf/environment/1" \
2444 xmlns:ns9="http://www.vmware.com/vcloud/extension/v1.5">\
2445 <ns2:VmReference href="{}" id="{}" name="{}" \
2446 type="application/vnd.vmware.vcloud.vm+xml" />\
2447 </ns2:Vms>""".format(vm_href, vm_id, vm_name)
2448
2449 addvmtogrp_resp = self.perform_request(req_type='POST',url=addvmtogrpURL, headers=headers, data=payload)
2450
2451 if addvmtogrp_resp.status_code != requests.codes.accepted:
2452 self.logger.debug ("REST API call {} failed. Return status code {}".format(addvmtogrpURL, addvmtogrp_resp.status_code))
2453 return False
2454 else:
2455 self.logger.debug ("Done adding VM {} to VMgroup {}".format(vm_name, vmGroup_name))
2456 return True
2457
2458
2459 def create_vmgroup(self, vmgroup_name, vmgroup_href, headers):
2460 """Method to create a VM group in vCD
2461
2462 Args:
2463 vmgroup_name : Name of VM group to be created
2464 vmgroup_href : href for vmgroup
2465 headers- Headers for REST request
2466 """
2467 #POST to add URL with required data
2468 vmgroup_status = False
2469 payload = """<VMWVmGroup xmlns="http://www.vmware.com/vcloud/extension/v1.5" \
2470 xmlns:vcloud_v1.5="http://www.vmware.com/vcloud/v1.5" name="{}">\
2471 <vmCount>1</vmCount>\
2472 </VMWVmGroup>""".format(vmgroup_name)
2473 resp = self.perform_request(req_type='POST',url=vmgroup_href, headers=headers, data=payload)
2474
2475 if resp.status_code != requests.codes.accepted:
2476 self.logger.debug ("REST API call {} failed. Return status code {}".format(vmgroup_href, resp.status_code))
2477 return vmgroup_status
2478 else:
2479 vmgroup_task = self.get_task_from_response(resp.content)
2480 if vmgroup_task is None or vmgroup_task is False:
2481 raise vimconn.vimconnUnexpectedResponse(
2482 "create_vmgroup(): failed to create VM group {}".format(vmgroup_name))
2483
2484 # wait for task to complete
2485 result = self.client.get_task_monitor().wait_for_success(task=vmgroup_task)
2486
2487 if result.get('status') == 'success':
2488 self.logger.debug("create_vmgroup(): Successfully created VM group {}".format(vmgroup_name))
2489 #time.sleep(10)
2490 vmgroup_status = True
2491 return vmgroup_status
2492 else:
2493 raise vimconn.vimconnUnexpectedResponse(\
2494 "create_vmgroup(): failed to create VM group {}".format(vmgroup_name))
2495
2496
2497 def find_vmgroup_reference(self, url, headers):
2498 """ Method to create a new VMGroup which is required to add created VM
2499 Args:
2500 url- resource pool href
2501 headers- header information
2502
2503 Returns:
2504 returns href to VM group to create VM group
2505 """
2506 #Perform GET on resource pool to find 'add' link to create VMGroup
2507 #https://vcd-ip/api/admin/extension/providervdc/<providervdc id>/resourcePools
2508 vmgrp_href = None
2509 resp = self.perform_request(req_type='GET',url=url, headers=headers)
2510
2511 if resp.status_code != requests.codes.ok:
2512 self.logger.debug ("REST API call {} failed. Return status code {}".format(url, resp.status_code))
2513 else:
2514 #Get the href to add vmGroup to vCD
2515 resp_xml = XmlElementTree.fromstring(resp.content)
2516 for child in resp_xml:
2517 if 'VMWProviderVdcResourcePool' in child.tag:
2518 for schild in child:
2519 if 'Link' in schild.tag:
2520 #Find href with type VMGroup and rel with add
2521 if schild.attrib.get('type') == "application/vnd.vmware.admin.vmwVmGroupType+xml"\
2522 and schild.attrib.get('rel') == "add":
2523 vmgrp_href = schild.attrib.get('href')
2524 return vmgrp_href
2525
2526
2527 def check_availibility_zone(self, az, respool_href, headers):
2528 """ Method to verify requested av zone is present or not in provided
2529 resource pool
2530
2531 Args:
2532 az - name of hostgroup (availibility_zone)
2533 respool_href - Resource Pool href
2534 headers - Headers to make REST call
2535 Returns:
2536 az_found - True if availibility_zone is found else False
2537 """
2538 az_found = False
2539 headers['Accept']='application/*+xml;version=27.0'
2540 resp = self.perform_request(req_type='GET',url=respool_href, headers=headers)
2541
2542 if resp.status_code != requests.codes.ok:
2543 self.logger.debug ("REST API call {} failed. Return status code {}".format(respool_href, resp.status_code))
2544 else:
2545 #Get the href to hostGroups and find provided hostGroup is present in it
2546 resp_xml = XmlElementTree.fromstring(resp.content)
2547
2548 for child in resp_xml:
2549 if 'VMWProviderVdcResourcePool' in child.tag:
2550 for schild in child:
2551 if 'Link' in schild.tag:
2552 if schild.attrib.get('type') == "application/vnd.vmware.admin.vmwHostGroupsType+xml":
2553 hostGroup_href = schild.attrib.get('href')
2554 hg_resp = self.perform_request(req_type='GET',url=hostGroup_href, headers=headers)
2555 if hg_resp.status_code != requests.codes.ok:
2556 self.logger.debug ("REST API call {} failed. Return status code {}".format(hostGroup_href, hg_resp.status_code))
2557 else:
2558 hg_resp_xml = XmlElementTree.fromstring(hg_resp.content)
2559 for hostGroup in hg_resp_xml:
2560 if 'HostGroup' in hostGroup.tag:
2561 if hostGroup.attrib.get("name") == az:
2562 az_found = True
2563 break
2564 return az_found
2565
2566
2567 def get_pvdc_for_org(self, org_vdc, headers):
2568 """ This method gets provider vdc references from organisation
2569
2570 Args:
2571 org_vdc - name of the organisation VDC to find pvdc
2572 headers - headers to make REST call
2573
2574 Returns:
2575 None - if no pvdc href found else
2576 pvdc_href - href to pvdc
2577 """
2578
2579 #Get provider VDC references from vCD
2580 pvdc_href = None
2581 #url = '<vcd url>/api/admin/extension/providerVdcReferences'
2582 url_list = [self.url, '/api/admin/extension/providerVdcReferences']
2583 url = ''.join(url_list)
2584
2585 response = self.perform_request(req_type='GET',url=url, headers=headers)
2586 if response.status_code != requests.codes.ok:
2587 self.logger.debug ("REST API call {} failed. Return status code {}"\
2588 .format(url, response.status_code))
2589 else:
2590 xmlroot_response = XmlElementTree.fromstring(response.content)
2591 for child in xmlroot_response:
2592 if 'ProviderVdcReference' in child.tag:
2593 pvdc_href = child.attrib.get('href')
2594 #Get vdcReferences to find org
2595 pvdc_resp = self.perform_request(req_type='GET',url=pvdc_href, headers=headers)
2596 if pvdc_resp.status_code != requests.codes.ok:
2597 raise vimconn.vimconnException("REST API call {} failed. "\
2598 "Return status code {}"\
2599 .format(url, pvdc_resp.status_code))
2600
2601 pvdc_resp_xml = XmlElementTree.fromstring(pvdc_resp.content)
2602 for child in pvdc_resp_xml:
2603 if 'Link' in child.tag:
2604 if child.attrib.get('type') == "application/vnd.vmware.admin.vdcReferences+xml":
2605 vdc_href = child.attrib.get('href')
2606
2607 #Check if provided org is present in vdc
2608 vdc_resp = self.perform_request(req_type='GET',
2609 url=vdc_href,
2610 headers=headers)
2611 if vdc_resp.status_code != requests.codes.ok:
2612 raise vimconn.vimconnException("REST API call {} failed. "\
2613 "Return status code {}"\
2614 .format(url, vdc_resp.status_code))
2615 vdc_resp_xml = XmlElementTree.fromstring(vdc_resp.content)
2616 for child in vdc_resp_xml:
2617 if 'VdcReference' in child.tag:
2618 if child.attrib.get('name') == org_vdc:
2619 return pvdc_href
2620
2621
2622 def get_resource_pool_details(self, pvdc_href, headers):
2623 """ Method to get resource pool information.
2624 Host groups are property of resource group.
2625 To get host groups, we need to GET details of resource pool.
2626
2627 Args:
2628 pvdc_href: href to pvdc details
2629 headers: headers
2630
2631 Returns:
2632 respool_href - Returns href link reference to resource pool
2633 """
2634 respool_href = None
2635 resp = self.perform_request(req_type='GET',url=pvdc_href, headers=headers)
2636
2637 if resp.status_code != requests.codes.ok:
2638 self.logger.debug ("REST API call {} failed. Return status code {}"\
2639 .format(pvdc_href, resp.status_code))
2640 else:
2641 respool_resp_xml = XmlElementTree.fromstring(resp.content)
2642 for child in respool_resp_xml:
2643 if 'Link' in child.tag:
2644 if child.attrib.get('type') == "application/vnd.vmware.admin.vmwProviderVdcResourcePoolSet+xml":
2645 respool_href = child.attrib.get("href")
2646 break
2647 return respool_href
2648
2649
2650 def log_message(self, msg):
2651 """
2652 Method to log error messages related to Affinity rule creation
2653 in new_vminstance & raise Exception
2654 Args :
2655 msg - Error message to be logged
2656
2657 """
2658 #get token to connect vCD as a normal user
2659 self.get_token()
2660 self.logger.debug(msg)
2661 raise vimconn.vimconnException(msg)
2662
2663
bayramovef390722016-09-27 03:34:46 -07002664 ##
2665 ##
2666 ## based on current discussion
2667 ##
2668 ##
2669 ## server:
2670 # created: '2016-09-08T11:51:58'
2671 # description: simple-instance.linux1.1
2672 # flavor: ddc6776e-75a9-11e6-ad5f-0800273e724c
2673 # hostId: e836c036-74e7-11e6-b249-0800273e724c
2674 # image: dde30fe6-75a9-11e6-ad5f-0800273e724c
2675 # status: ACTIVE
2676 # error_msg:
2677 # interfaces: …
2678 #
bayramovfe3f3c92016-10-04 07:53:41 +04002679 def get_vminstance(self, vim_vm_uuid=None):
bayramov5761ad12016-10-04 09:00:30 +04002680 """Returns the VM instance information from VIM"""
bayramov325fa1c2016-09-08 01:42:46 -07002681
bayramovef390722016-09-27 03:34:46 -07002682 self.logger.debug("Client requesting vm instance {} ".format(vim_vm_uuid))
bayramov325fa1c2016-09-08 01:42:46 -07002683
kasarc5bf2932018-03-09 04:15:22 -08002684 org, vdc = self.get_vdc_details()
bayramovef390722016-09-27 03:34:46 -07002685 if vdc is None:
bayramovfe3f3c92016-10-04 07:53:41 +04002686 raise vimconn.vimconnConnectionException(
2687 "Failed to get a reference of VDC for a tenant {}".format(self.tenant_name))
bayramov325fa1c2016-09-08 01:42:46 -07002688
bayramovfe3f3c92016-10-04 07:53:41 +04002689 vm_info_dict = self.get_vapp_details_rest(vapp_uuid=vim_vm_uuid)
2690 if not vm_info_dict:
bayramovef390722016-09-27 03:34:46 -07002691 self.logger.debug("get_vminstance(): Failed to get vApp name by UUID {}".format(vim_vm_uuid))
bayramovfe3f3c92016-10-04 07:53:41 +04002692 raise vimconn.vimconnNotFoundException("Failed to get vApp name by UUID {}".format(vim_vm_uuid))
bayramov325fa1c2016-09-08 01:42:46 -07002693
bayramovfe3f3c92016-10-04 07:53:41 +04002694 status_key = vm_info_dict['status']
2695 error = ''
bayramovef390722016-09-27 03:34:46 -07002696 try:
bayramovfe3f3c92016-10-04 07:53:41 +04002697 vm_dict = {'created': vm_info_dict['created'],
2698 'description': vm_info_dict['name'],
2699 'status': vcdStatusCode2manoFormat[int(status_key)],
2700 'hostId': vm_info_dict['vmuuid'],
2701 'error_msg': error,
2702 'vim_info': yaml.safe_dump(vm_info_dict), 'interfaces': []}
bayramovef390722016-09-27 03:34:46 -07002703
bayramov5761ad12016-10-04 09:00:30 +04002704 if 'interfaces' in vm_info_dict:
bayramovfe3f3c92016-10-04 07:53:41 +04002705 vm_dict['interfaces'] = vm_info_dict['interfaces']
2706 else:
2707 vm_dict['interfaces'] = []
2708 except KeyError:
2709 vm_dict = {'created': '',
2710 'description': '',
2711 'status': vcdStatusCode2manoFormat[int(-1)],
2712 'hostId': vm_info_dict['vmuuid'],
2713 'error_msg': "Inconsistency state",
2714 'vim_info': yaml.safe_dump(vm_info_dict), 'interfaces': []}
bayramovef390722016-09-27 03:34:46 -07002715
2716 return vm_dict
2717
tierno98e909c2017-10-14 13:27:03 +02002718 def delete_vminstance(self, vm__vim_uuid, created_items=None):
bayramovef390722016-09-27 03:34:46 -07002719 """Method poweroff and remove VM instance from vcloud director network.
2720
2721 Args:
2722 vm__vim_uuid: VM UUID
2723
2724 Returns:
2725 Returns the instance identifier
2726 """
2727
2728 self.logger.debug("Client requesting delete vm instance {} ".format(vm__vim_uuid))
bayramov325fa1c2016-09-08 01:42:46 -07002729
kasarc5bf2932018-03-09 04:15:22 -08002730 org, vdc = self.get_vdc_details()
sbhangarea8e5b782018-06-21 02:10:03 -07002731 vdc_obj = VDC(self.client, href=vdc.get('href'))
kasarc5bf2932018-03-09 04:15:22 -08002732 if vdc_obj is None:
bayramovef390722016-09-27 03:34:46 -07002733 self.logger.debug("delete_vminstance(): Failed to get a reference of VDC for a tenant {}".format(
2734 self.tenant_name))
bayramovbd6160f2016-09-28 04:12:05 +04002735 raise vimconn.vimconnException(
2736 "delete_vminstance(): Failed to get a reference of VDC for a tenant {}".format(self.tenant_name))
bayramov325fa1c2016-09-08 01:42:46 -07002737
bayramovef390722016-09-27 03:34:46 -07002738 try:
kasarc5bf2932018-03-09 04:15:22 -08002739 vapp_name = self.get_namebyvappid(vm__vim_uuid)
bayramovef390722016-09-27 03:34:46 -07002740 if vapp_name is None:
2741 self.logger.debug("delete_vminstance(): Failed to get vm by given {} vm uuid".format(vm__vim_uuid))
2742 return -1, "delete_vminstance(): Failed to get vm by given {} vm uuid".format(vm__vim_uuid)
Ravi Chamarty41840092018-10-31 16:12:38 +00002743 self.logger.info("Deleting vApp {} and UUID {}".format(vapp_name, vm__vim_uuid))
2744 vapp_resource = vdc_obj.get_vapp(vapp_name)
2745 vapp = VApp(self.client, resource=vapp_resource)
bayramov325fa1c2016-09-08 01:42:46 -07002746
bayramovef390722016-09-27 03:34:46 -07002747 # Delete vApp and wait for status change if task executed and vApp is None.
bayramov325fa1c2016-09-08 01:42:46 -07002748
kate13ab2c42016-12-23 01:34:24 -08002749 if vapp:
kasarc5bf2932018-03-09 04:15:22 -08002750 if vapp_resource.get('deployed') == 'true':
kate13ab2c42016-12-23 01:34:24 -08002751 self.logger.info("Powering off vApp {}".format(vapp_name))
2752 #Power off vApp
2753 powered_off = False
2754 wait_time = 0
2755 while wait_time <= MAX_WAIT_TIME:
kasarc5bf2932018-03-09 04:15:22 -08002756 power_off_task = vapp.power_off()
2757 result = self.client.get_task_monitor().wait_for_success(task=power_off_task)
bayramovef390722016-09-27 03:34:46 -07002758
kasarc5bf2932018-03-09 04:15:22 -08002759 if result.get('status') == 'success':
2760 powered_off = True
2761 break
kate13ab2c42016-12-23 01:34:24 -08002762 else:
2763 self.logger.info("Wait for vApp {} to power off".format(vapp_name))
2764 time.sleep(INTERVAL_TIME)
2765
2766 wait_time +=INTERVAL_TIME
2767 if not powered_off:
2768 self.logger.debug("delete_vminstance(): Failed to power off VM instance {} ".format(vm__vim_uuid))
2769 else:
2770 self.logger.info("delete_vminstance(): Powered off VM instance {} ".format(vm__vim_uuid))
2771
2772 #Undeploy vApp
2773 self.logger.info("Undeploy vApp {}".format(vapp_name))
2774 wait_time = 0
2775 undeployed = False
2776 while wait_time <= MAX_WAIT_TIME:
sbhangarea8e5b782018-06-21 02:10:03 -07002777 vapp = VApp(self.client, resource=vapp_resource)
kate13ab2c42016-12-23 01:34:24 -08002778 if not vapp:
2779 self.logger.debug("delete_vminstance(): Failed to get vm by given {} vm uuid".format(vm__vim_uuid))
2780 return -1, "delete_vminstance(): Failed to get vm by given {} vm uuid".format(vm__vim_uuid)
kasarc5bf2932018-03-09 04:15:22 -08002781 undeploy_task = vapp.undeploy()
kate13ab2c42016-12-23 01:34:24 -08002782
kasarc5bf2932018-03-09 04:15:22 -08002783 result = self.client.get_task_monitor().wait_for_success(task=undeploy_task)
2784 if result.get('status') == 'success':
2785 undeployed = True
2786 break
kate13ab2c42016-12-23 01:34:24 -08002787 else:
2788 self.logger.debug("Wait for vApp {} to undeploy".format(vapp_name))
2789 time.sleep(INTERVAL_TIME)
2790
2791 wait_time +=INTERVAL_TIME
2792
2793 if not undeployed:
sbhangarea8e5b782018-06-21 02:10:03 -07002794 self.logger.debug("delete_vminstance(): Failed to undeploy vApp {} ".format(vm__vim_uuid))
kate13ab2c42016-12-23 01:34:24 -08002795
2796 # delete vapp
2797 self.logger.info("Start deletion of vApp {} ".format(vapp_name))
kate13ab2c42016-12-23 01:34:24 -08002798
2799 if vapp is not None:
2800 wait_time = 0
2801 result = False
2802
2803 while wait_time <= MAX_WAIT_TIME:
kasarc5bf2932018-03-09 04:15:22 -08002804 vapp = VApp(self.client, resource=vapp_resource)
kate13ab2c42016-12-23 01:34:24 -08002805 if not vapp:
2806 self.logger.debug("delete_vminstance(): Failed to get vm by given {} vm uuid".format(vm__vim_uuid))
2807 return -1, "delete_vminstance(): Failed to get vm by given {} vm uuid".format(vm__vim_uuid)
2808
kasarc5bf2932018-03-09 04:15:22 -08002809 delete_task = vdc_obj.delete_vapp(vapp.name, force=True)
kate13ab2c42016-12-23 01:34:24 -08002810
kasarc5bf2932018-03-09 04:15:22 -08002811 result = self.client.get_task_monitor().wait_for_success(task=delete_task)
sbhangarea8e5b782018-06-21 02:10:03 -07002812 if result.get('status') == 'success':
kasarc5bf2932018-03-09 04:15:22 -08002813 break
kate13ab2c42016-12-23 01:34:24 -08002814 else:
2815 self.logger.debug("Wait for vApp {} to delete".format(vapp_name))
2816 time.sleep(INTERVAL_TIME)
2817
2818 wait_time +=INTERVAL_TIME
2819
kasarc5bf2932018-03-09 04:15:22 -08002820 if result is None:
bayramovbd6160f2016-09-28 04:12:05 +04002821 self.logger.debug("delete_vminstance(): Failed delete uuid {} ".format(vm__vim_uuid))
kasarc5bf2932018-03-09 04:15:22 -08002822 else:
2823 self.logger.info("Deleted vm instance {} sccessfully".format(vm__vim_uuid))
Ananda Baitharu319c26f2019-03-05 17:34:31 +00002824 config_drive_catalog_name, config_drive_catalog_id = 'cfg_drv-' + vm__vim_uuid, None
2825 catalog_list = self.get_image_list()
2826 try:
2827 config_drive_catalog_id = [catalog_['id'] for catalog_ in catalog_list
2828 if catalog_['name'] == config_drive_catalog_name][0]
2829 except IndexError:
2830 pass
2831 if config_drive_catalog_id:
2832 self.logger.debug('delete_vminstance(): Found a config drive catalog {} matching '
2833 'vapp_name"{}". Deleting it.'.format(config_drive_catalog_id, vapp_name))
2834 self.delete_image(config_drive_catalog_id)
kasarc5bf2932018-03-09 04:15:22 -08002835 return vm__vim_uuid
bayramovef390722016-09-27 03:34:46 -07002836 except:
2837 self.logger.debug(traceback.format_exc())
bayramovbd6160f2016-09-28 04:12:05 +04002838 raise vimconn.vimconnException("delete_vminstance(): Failed delete vm instance {}".format(vm__vim_uuid))
bayramovef390722016-09-27 03:34:46 -07002839
bayramov325fa1c2016-09-08 01:42:46 -07002840
2841 def refresh_vms_status(self, vm_list):
bayramovef390722016-09-27 03:34:46 -07002842 """Get the status of the virtual machines and their interfaces/ports
bayramov325fa1c2016-09-08 01:42:46 -07002843 Params: the list of VM identifiers
2844 Returns a dictionary with:
2845 vm_id: #VIM id of this Virtual Machine
2846 status: #Mandatory. Text with one of:
2847 # DELETED (not found at vim)
bayramovef390722016-09-27 03:34:46 -07002848 # VIM_ERROR (Cannot connect to VIM, VIM response error, ...)
bayramov325fa1c2016-09-08 01:42:46 -07002849 # OTHER (Vim reported other status not understood)
2850 # ERROR (VIM indicates an ERROR status)
bayramovef390722016-09-27 03:34:46 -07002851 # ACTIVE, PAUSED, SUSPENDED, INACTIVE (not running),
bayramov325fa1c2016-09-08 01:42:46 -07002852 # CREATING (on building process), ERROR
2853 # ACTIVE:NoMgmtIP (Active but any of its interface has an IP address
2854 #
bayramovef390722016-09-27 03:34:46 -07002855 error_msg: #Text with VIM error message, if any. Or the VIM connection ERROR
bayramov325fa1c2016-09-08 01:42:46 -07002856 vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
2857 interfaces:
2858 - vim_info: #Text with plain information obtained from vim (yaml.safe_dump)
2859 mac_address: #Text format XX:XX:XX:XX:XX:XX
2860 vim_net_id: #network id where this interface is connected
2861 vim_interface_id: #interface/port VIM id
2862 ip_address: #null, or text with IPv4, IPv6 address
bayramovef390722016-09-27 03:34:46 -07002863 """
bayramov325fa1c2016-09-08 01:42:46 -07002864
bayramovef390722016-09-27 03:34:46 -07002865 self.logger.debug("Client requesting refresh vm status for {} ".format(vm_list))
bhangare985a1fd2017-01-31 01:53:21 -08002866
kasarc5bf2932018-03-09 04:15:22 -08002867 org,vdc = self.get_vdc_details()
bayramovef390722016-09-27 03:34:46 -07002868 if vdc is None:
bayramovbd6160f2016-09-28 04:12:05 +04002869 raise vimconn.vimconnException("Failed to get a reference of VDC for a tenant {}".format(self.tenant_name))
bayramovef390722016-09-27 03:34:46 -07002870
2871 vms_dict = {}
kasarde691232017-03-25 03:37:31 -07002872 nsx_edge_list = []
bayramovef390722016-09-27 03:34:46 -07002873 for vmuuid in vm_list:
kasarc5bf2932018-03-09 04:15:22 -08002874 vapp_name = self.get_namebyvappid(vmuuid)
2875 if vapp_name is not None:
bayramovef390722016-09-27 03:34:46 -07002876
bayramovef390722016-09-27 03:34:46 -07002877 try:
bhangare1a0b97c2017-06-21 02:20:15 -07002878 vm_pci_details = self.get_vm_pci_details(vmuuid)
kasarc5bf2932018-03-09 04:15:22 -08002879 vdc_obj = VDC(self.client, href=vdc.get('href'))
2880 vapp_resource = vdc_obj.get_vapp(vapp_name)
2881 the_vapp = VApp(self.client, resource=vapp_resource)
bhangarebfdca492017-03-11 01:32:46 -08002882
kasarc5bf2932018-03-09 04:15:22 -08002883 vm_details = {}
2884 for vm in the_vapp.get_all_vms():
2885 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
sbhangarea8e5b782018-06-21 02:10:03 -07002886 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
kasarc5bf2932018-03-09 04:15:22 -08002887 response = self.perform_request(req_type='GET',
2888 url=vm.get('href'),
2889 headers=headers)
bhangarebfdca492017-03-11 01:32:46 -08002890
kasarc5bf2932018-03-09 04:15:22 -08002891 if response.status_code != 200:
2892 self.logger.error("refresh_vms_status : REST call {} failed reason : {}"\
2893 "status code : {}".format(vm.get('href'),
2894 response.content,
2895 response.status_code))
2896 raise vimconn.vimconnException("refresh_vms_status : Failed to get "\
2897 "VM details")
2898 xmlroot = XmlElementTree.fromstring(response.content)
kasarde691232017-03-25 03:37:31 -07002899
Ravi Chamarty1fdf9992018-10-07 15:45:44 +00002900
kasarc5bf2932018-03-09 04:15:22 -08002901 result = response.content.replace("\n"," ")
Ravi Chamarty1fdf9992018-10-07 15:45:44 +00002902 hdd_match = re.search('vcloud:capacity="(\d+)"\svcloud:storageProfileOverrideVmDefault=',result)
2903 if hdd_match:
2904 hdd_mb = hdd_match.group(1)
2905 vm_details['hdd_mb'] = int(hdd_mb) if hdd_mb else None
2906 cpus_match = re.search('<rasd:Description>Number of Virtual CPUs</.*?>(\d+)</rasd:VirtualQuantity>',result)
2907 if cpus_match:
2908 cpus = cpus_match.group(1)
2909 vm_details['cpus'] = int(cpus) if cpus else None
kasarc5bf2932018-03-09 04:15:22 -08002910 memory_mb = re.search('<rasd:Description>Memory Size</.*?>(\d+)</rasd:VirtualQuantity>',result).group(1)
2911 vm_details['memory_mb'] = int(memory_mb) if memory_mb else None
2912 vm_details['status'] = vcdStatusCode2manoFormat[int(xmlroot.get('status'))]
2913 vm_details['id'] = xmlroot.get('id')
2914 vm_details['name'] = xmlroot.get('name')
2915 vm_info = [vm_details]
2916 if vm_pci_details:
sbhangarea8e5b782018-06-21 02:10:03 -07002917 vm_info[0].update(vm_pci_details)
kasarc5bf2932018-03-09 04:15:22 -08002918
2919 vm_dict = {'status': vcdStatusCode2manoFormat[int(vapp_resource.get('status'))],
2920 'error_msg': vcdStatusCode2manoFormat[int(vapp_resource.get('status'))],
2921 'vim_info': yaml.safe_dump(vm_info), 'interfaces': []}
2922
2923 # get networks
sbhangarea8e5b782018-06-21 02:10:03 -07002924 vm_ip = None
kasar1b7b9522018-05-15 06:15:07 -07002925 vm_mac = None
2926 networks = re.findall('<NetworkConnection needsCustomization=.*?</NetworkConnection>',result)
2927 for network in networks:
2928 mac_s = re.search('<MACAddress>(.*?)</MACAddress>',network)
2929 vm_mac = mac_s.group(1) if mac_s else None
2930 ip_s = re.search('<IpAddress>(.*?)</IpAddress>',network)
2931 vm_ip = ip_s.group(1) if ip_s else None
kasarc5bf2932018-03-09 04:15:22 -08002932
kasar1b7b9522018-05-15 06:15:07 -07002933 if vm_ip is None:
2934 if not nsx_edge_list:
2935 nsx_edge_list = self.get_edge_details()
2936 if nsx_edge_list is None:
2937 raise vimconn.vimconnException("refresh_vms_status:"\
2938 "Failed to get edge details from NSX Manager")
2939 if vm_mac is not None:
2940 vm_ip = self.get_ipaddr_from_NSXedge(nsx_edge_list, vm_mac)
kasarc5bf2932018-03-09 04:15:22 -08002941
kasarabac1e22018-05-17 02:44:39 -07002942 net_s = re.search('network="(.*?)"',network)
2943 network_name = net_s.group(1) if net_s else None
2944
kasar1b7b9522018-05-15 06:15:07 -07002945 vm_net_id = self.get_network_id_by_name(network_name)
2946 interface = {"mac_address": vm_mac,
2947 "vim_net_id": vm_net_id,
2948 "vim_interface_id": vm_net_id,
2949 "ip_address": vm_ip}
2950
2951 vm_dict["interfaces"].append(interface)
kasarc5bf2932018-03-09 04:15:22 -08002952
bayramovef390722016-09-27 03:34:46 -07002953 # add a vm to vm dict
2954 vms_dict.setdefault(vmuuid, vm_dict)
kasarc5bf2932018-03-09 04:15:22 -08002955 self.logger.debug("refresh_vms_status : vm info {}".format(vm_dict))
bhangarebfdca492017-03-11 01:32:46 -08002956 except Exception as exp:
2957 self.logger.debug("Error in response {}".format(exp))
bayramovef390722016-09-27 03:34:46 -07002958 self.logger.debug(traceback.format_exc())
2959
2960 return vms_dict
2961
kasarde691232017-03-25 03:37:31 -07002962
2963 def get_edge_details(self):
2964 """Get the NSX edge list from NSX Manager
2965 Returns list of NSX edges
2966 """
2967 edge_list = []
2968 rheaders = {'Content-Type': 'application/xml'}
2969 nsx_api_url = '/api/4.0/edges'
2970
2971 self.logger.debug("Get edge details from NSX Manager {} {}".format(self.nsx_manager, nsx_api_url))
2972
2973 try:
2974 resp = requests.get(self.nsx_manager + nsx_api_url,
2975 auth = (self.nsx_user, self.nsx_password),
2976 verify = False, headers = rheaders)
2977 if resp.status_code == requests.codes.ok:
2978 paged_Edge_List = XmlElementTree.fromstring(resp.text)
2979 for edge_pages in paged_Edge_List:
2980 if edge_pages.tag == 'edgePage':
2981 for edge_summary in edge_pages:
2982 if edge_summary.tag == 'pagingInfo':
2983 for element in edge_summary:
2984 if element.tag == 'totalCount' and element.text == '0':
2985 raise vimconn.vimconnException("get_edge_details: No NSX edges details found: {}"
2986 .format(self.nsx_manager))
2987
2988 if edge_summary.tag == 'edgeSummary':
2989 for element in edge_summary:
2990 if element.tag == 'id':
2991 edge_list.append(element.text)
2992 else:
2993 raise vimconn.vimconnException("get_edge_details: No NSX edge details found: {}"
2994 .format(self.nsx_manager))
2995
2996 if not edge_list:
2997 raise vimconn.vimconnException("get_edge_details: "\
2998 "No NSX edge details found: {}"
2999 .format(self.nsx_manager))
3000 else:
3001 self.logger.debug("get_edge_details: Found NSX edges {}".format(edge_list))
3002 return edge_list
3003 else:
3004 self.logger.debug("get_edge_details: "
3005 "Failed to get NSX edge details from NSX Manager: {}"
3006 .format(resp.content))
3007 return None
3008
3009 except Exception as exp:
3010 self.logger.debug("get_edge_details: "\
3011 "Failed to get NSX edge details from NSX Manager: {}"
3012 .format(exp))
3013 raise vimconn.vimconnException("get_edge_details: "\
3014 "Failed to get NSX edge details from NSX Manager: {}"
3015 .format(exp))
3016
3017
3018 def get_ipaddr_from_NSXedge(self, nsx_edges, mac_address):
3019 """Get IP address details from NSX edges, using the MAC address
3020 PARAMS: nsx_edges : List of NSX edges
3021 mac_address : Find IP address corresponding to this MAC address
3022 Returns: IP address corrresponding to the provided MAC address
3023 """
3024
3025 ip_addr = None
3026 rheaders = {'Content-Type': 'application/xml'}
3027
3028 self.logger.debug("get_ipaddr_from_NSXedge: Finding IP addr from NSX edge")
3029
3030 try:
3031 for edge in nsx_edges:
3032 nsx_api_url = '/api/4.0/edges/'+ edge +'/dhcp/leaseInfo'
3033
3034 resp = requests.get(self.nsx_manager + nsx_api_url,
3035 auth = (self.nsx_user, self.nsx_password),
3036 verify = False, headers = rheaders)
3037
3038 if resp.status_code == requests.codes.ok:
3039 dhcp_leases = XmlElementTree.fromstring(resp.text)
3040 for child in dhcp_leases:
3041 if child.tag == 'dhcpLeaseInfo':
3042 dhcpLeaseInfo = child
3043 for leaseInfo in dhcpLeaseInfo:
3044 for elem in leaseInfo:
3045 if (elem.tag)=='macAddress':
3046 edge_mac_addr = elem.text
3047 if (elem.tag)=='ipAddress':
3048 ip_addr = elem.text
3049 if edge_mac_addr is not None:
3050 if edge_mac_addr == mac_address:
3051 self.logger.debug("Found ip addr {} for mac {} at NSX edge {}"
3052 .format(ip_addr, mac_address,edge))
3053 return ip_addr
3054 else:
3055 self.logger.debug("get_ipaddr_from_NSXedge: "\
3056 "Error occurred while getting DHCP lease info from NSX Manager: {}"
3057 .format(resp.content))
3058
3059 self.logger.debug("get_ipaddr_from_NSXedge: No IP addr found in any NSX edge")
3060 return None
3061
3062 except XmlElementTree.ParseError as Err:
3063 self.logger.debug("ParseError in response from NSX Manager {}".format(Err.message), exc_info=True)
3064
3065
tierno98e909c2017-10-14 13:27:03 +02003066 def action_vminstance(self, vm__vim_uuid=None, action_dict=None, created_items={}):
bayramovef390722016-09-27 03:34:46 -07003067 """Send and action over a VM instance from VIM
3068 Returns the vm_id if the action was successfully sent to the VIM"""
3069
3070 self.logger.debug("Received action for vm {} and action dict {}".format(vm__vim_uuid, action_dict))
3071 if vm__vim_uuid is None or action_dict is None:
bayramovbd6160f2016-09-28 04:12:05 +04003072 raise vimconn.vimconnException("Invalid request. VM id or action is None.")
bayramovef390722016-09-27 03:34:46 -07003073
kasarc5bf2932018-03-09 04:15:22 -08003074 org, vdc = self.get_vdc_details()
bayramovef390722016-09-27 03:34:46 -07003075 if vdc is None:
tierno98e909c2017-10-14 13:27:03 +02003076 raise vimconn.vimconnException("Failed to get a reference of VDC for a tenant {}".format(self.tenant_name))
bayramovef390722016-09-27 03:34:46 -07003077
kasarc5bf2932018-03-09 04:15:22 -08003078 vapp_name = self.get_namebyvappid(vm__vim_uuid)
bayramovef390722016-09-27 03:34:46 -07003079 if vapp_name is None:
3080 self.logger.debug("action_vminstance(): Failed to get vm by given {} vm uuid".format(vm__vim_uuid))
bayramovbd6160f2016-09-28 04:12:05 +04003081 raise vimconn.vimconnException("Failed to get vm by given {} vm uuid".format(vm__vim_uuid))
bayramovef390722016-09-27 03:34:46 -07003082 else:
3083 self.logger.info("Action_vminstance vApp {} and UUID {}".format(vapp_name, vm__vim_uuid))
3084
3085 try:
kasarc5bf2932018-03-09 04:15:22 -08003086 vdc_obj = VDC(self.client, href=vdc.get('href'))
3087 vapp_resource = vdc_obj.get_vapp(vapp_name)
sbhangarea8e5b782018-06-21 02:10:03 -07003088 vapp = VApp(self.client, resource=vapp_resource)
bayramovef390722016-09-27 03:34:46 -07003089 if "start" in action_dict:
kasar3ac5dc42017-03-15 06:28:22 -07003090 self.logger.info("action_vminstance: Power on vApp: {}".format(vapp_name))
sbhangarea8e5b782018-06-21 02:10:03 -07003091 poweron_task = self.power_on_vapp(vm__vim_uuid, vapp_name)
3092 result = self.client.get_task_monitor().wait_for_success(task=poweron_task)
kasarc5bf2932018-03-09 04:15:22 -08003093 self.instance_actions_result("start", result, vapp_name)
kasar3ac5dc42017-03-15 06:28:22 -07003094 elif "rebuild" in action_dict:
3095 self.logger.info("action_vminstance: Rebuild vApp: {}".format(vapp_name))
kasarc5bf2932018-03-09 04:15:22 -08003096 rebuild_task = vapp.deploy(power_on=True)
sbhangarea8e5b782018-06-21 02:10:03 -07003097 result = self.client.get_task_monitor().wait_for_success(task=rebuild_task)
kasar3ac5dc42017-03-15 06:28:22 -07003098 self.instance_actions_result("rebuild", result, vapp_name)
bayramovef390722016-09-27 03:34:46 -07003099 elif "pause" in action_dict:
kasar3ac5dc42017-03-15 06:28:22 -07003100 self.logger.info("action_vminstance: pause vApp: {}".format(vapp_name))
kasarc5bf2932018-03-09 04:15:22 -08003101 pause_task = vapp.undeploy(action='suspend')
sbhangarea8e5b782018-06-21 02:10:03 -07003102 result = self.client.get_task_monitor().wait_for_success(task=pause_task)
kasar3ac5dc42017-03-15 06:28:22 -07003103 self.instance_actions_result("pause", result, vapp_name)
bayramovef390722016-09-27 03:34:46 -07003104 elif "resume" in action_dict:
kasar3ac5dc42017-03-15 06:28:22 -07003105 self.logger.info("action_vminstance: resume vApp: {}".format(vapp_name))
kasarc5bf2932018-03-09 04:15:22 -08003106 poweron_task = self.power_on_vapp(vm__vim_uuid, vapp_name)
3107 result = self.client.get_task_monitor().wait_for_success(task=poweron_task)
kasar3ac5dc42017-03-15 06:28:22 -07003108 self.instance_actions_result("resume", result, vapp_name)
bayramovef390722016-09-27 03:34:46 -07003109 elif "shutoff" in action_dict or "shutdown" in action_dict:
kasar3ac5dc42017-03-15 06:28:22 -07003110 action_name , value = action_dict.items()[0]
kasarc5bf2932018-03-09 04:15:22 -08003111 #For python3
3112 #action_name , value = list(action_dict.items())[0]
kasar3ac5dc42017-03-15 06:28:22 -07003113 self.logger.info("action_vminstance: {} vApp: {}".format(action_name, vapp_name))
kasarc5bf2932018-03-09 04:15:22 -08003114 shutdown_task = vapp.shutdown()
3115 result = self.client.get_task_monitor().wait_for_success(task=shutdown_task)
kasar3ac5dc42017-03-15 06:28:22 -07003116 if action_name == "shutdown":
3117 self.instance_actions_result("shutdown", result, vapp_name)
bhangare985a1fd2017-01-31 01:53:21 -08003118 else:
kasar3ac5dc42017-03-15 06:28:22 -07003119 self.instance_actions_result("shutoff", result, vapp_name)
bayramovef390722016-09-27 03:34:46 -07003120 elif "forceOff" in action_dict:
kasarc5bf2932018-03-09 04:15:22 -08003121 result = vapp.undeploy(action='powerOff')
kasar3ac5dc42017-03-15 06:28:22 -07003122 self.instance_actions_result("forceOff", result, vapp_name)
3123 elif "reboot" in action_dict:
3124 self.logger.info("action_vminstance: reboot vApp: {}".format(vapp_name))
kasarc5bf2932018-03-09 04:15:22 -08003125 reboot_task = vapp.reboot()
sbhangarea8e5b782018-06-21 02:10:03 -07003126 self.client.get_task_monitor().wait_for_success(task=reboot_task)
bayramovef390722016-09-27 03:34:46 -07003127 else:
kasar3ac5dc42017-03-15 06:28:22 -07003128 raise vimconn.vimconnException("action_vminstance: Invalid action {} or action is None.".format(action_dict))
kasarc5bf2932018-03-09 04:15:22 -08003129 return vm__vim_uuid
bhangarebfdca492017-03-11 01:32:46 -08003130 except Exception as exp :
3131 self.logger.debug("action_vminstance: Failed with Exception {}".format(exp))
3132 raise vimconn.vimconnException("action_vminstance: Failed with Exception {}".format(exp))
bayramovef390722016-09-27 03:34:46 -07003133
kasar3ac5dc42017-03-15 06:28:22 -07003134 def instance_actions_result(self, action, result, vapp_name):
kasarc5bf2932018-03-09 04:15:22 -08003135 if result.get('status') == 'success':
kasar3ac5dc42017-03-15 06:28:22 -07003136 self.logger.info("action_vminstance: Sucessfully {} the vApp: {}".format(action, vapp_name))
3137 else:
3138 self.logger.error("action_vminstance: Failed to {} vApp: {}".format(action, vapp_name))
3139
kasarcf655072019-03-20 01:40:05 -07003140 def get_vminstance_console(self, vm_id, console_type="novnc"):
bayramovef390722016-09-27 03:34:46 -07003141 """
bayramov325fa1c2016-09-08 01:42:46 -07003142 Get a console for the virtual machine
3143 Params:
3144 vm_id: uuid of the VM
3145 console_type, can be:
bayramovef390722016-09-27 03:34:46 -07003146 "novnc" (by default), "xvpvnc" for VNC types,
bayramov325fa1c2016-09-08 01:42:46 -07003147 "rdp-html5" for RDP types, "spice-html5" for SPICE types
3148 Returns dict with the console parameters:
3149 protocol: ssh, ftp, http, https, ...
bayramovef390722016-09-27 03:34:46 -07003150 server: usually ip address
3151 port: the http, ssh, ... port
3152 suffix: extra text, e.g. the http path and query string
3153 """
kasarcf655072019-03-20 01:40:05 -07003154 console_dict = {}
3155
3156 if console_type==None or console_type=='novnc':
3157
3158 url_rest_call = "{}/api/vApp/vm-{}/screen/action/acquireMksTicket".format(self.url, vm_id)
3159
3160 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
3161 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
3162 response = self.perform_request(req_type='POST',
3163 url=url_rest_call,
3164 headers=headers)
3165
3166 if response.status_code == 403:
3167 response = self.retry_rest('GET', url_rest_call)
3168
3169 if response.status_code != 200:
3170 self.logger.error("REST call {} failed reason : {}"\
3171 "status code : {}".format(url_rest_call,
3172 response.content,
3173 response.status_code))
3174 raise vimconn.vimconnException("get_vminstance_console : Failed to get "\
3175 "VM Mks ticket details")
3176 s = re.search("<Host>(.*?)</Host>",response.content)
3177 console_dict['server'] = s.group(1) if s else None
3178 s1 = re.search("<Port>(\d+)</Port>",response.content)
3179 console_dict['port'] = s1.group(1) if s1 else None
3180
3181
3182 url_rest_call = "{}/api/vApp/vm-{}/screen/action/acquireTicket".format(self.url, vm_id)
3183
3184 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
3185 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
3186 response = self.perform_request(req_type='POST',
3187 url=url_rest_call,
3188 headers=headers)
3189
3190 if response.status_code == 403:
3191 response = self.retry_rest('GET', url_rest_call)
3192
3193 if response.status_code != 200:
3194 self.logger.error("REST call {} failed reason : {}"\
3195 "status code : {}".format(url_rest_call,
3196 response.content,
3197 response.status_code))
3198 raise vimconn.vimconnException("get_vminstance_console : Failed to get "\
3199 "VM console details")
3200 s = re.search(">.*?/(vm-\d+.*)</",response.content)
3201 console_dict['suffix'] = s.group(1) if s else None
3202 console_dict['protocol'] = "https"
3203
3204 return console_dict
bayramov325fa1c2016-09-08 01:42:46 -07003205
bayramovef390722016-09-27 03:34:46 -07003206 # NOT USED METHODS in current version
bayramov325fa1c2016-09-08 01:42:46 -07003207
3208 def host_vim2gui(self, host, server_dict):
bayramov5761ad12016-10-04 09:00:30 +04003209 """Transform host dictionary from VIM format to GUI format,
bayramov325fa1c2016-09-08 01:42:46 -07003210 and append to the server_dict
bayramov5761ad12016-10-04 09:00:30 +04003211 """
bayramovbd6160f2016-09-28 04:12:05 +04003212 raise vimconn.vimconnNotImplemented("Should have implemented this")
bayramov325fa1c2016-09-08 01:42:46 -07003213
3214 def get_hosts_info(self):
bayramov5761ad12016-10-04 09:00:30 +04003215 """Get the information of deployed hosts
3216 Returns the hosts content"""
bayramovbd6160f2016-09-28 04:12:05 +04003217 raise vimconn.vimconnNotImplemented("Should have implemented this")
bayramov325fa1c2016-09-08 01:42:46 -07003218
3219 def get_hosts(self, vim_tenant):
bayramov5761ad12016-10-04 09:00:30 +04003220 """Get the hosts and deployed instances
3221 Returns the hosts content"""
bayramovbd6160f2016-09-28 04:12:05 +04003222 raise vimconn.vimconnNotImplemented("Should have implemented this")
bayramov325fa1c2016-09-08 01:42:46 -07003223
3224 def get_processor_rankings(self):
bayramov5761ad12016-10-04 09:00:30 +04003225 """Get the processor rankings in the VIM database"""
bayramovbd6160f2016-09-28 04:12:05 +04003226 raise vimconn.vimconnNotImplemented("Should have implemented this")
bayramov325fa1c2016-09-08 01:42:46 -07003227
3228 def new_host(self, host_data):
bayramov5761ad12016-10-04 09:00:30 +04003229 """Adds a new host to VIM"""
bayramov325fa1c2016-09-08 01:42:46 -07003230 '''Returns status code of the VIM response'''
bayramovbd6160f2016-09-28 04:12:05 +04003231 raise vimconn.vimconnNotImplemented("Should have implemented this")
bayramov325fa1c2016-09-08 01:42:46 -07003232
3233 def new_external_port(self, port_data):
bayramov5761ad12016-10-04 09:00:30 +04003234 """Adds a external port to VIM"""
bayramov325fa1c2016-09-08 01:42:46 -07003235 '''Returns the port identifier'''
bayramovbd6160f2016-09-28 04:12:05 +04003236 raise vimconn.vimconnNotImplemented("Should have implemented this")
bayramov325fa1c2016-09-08 01:42:46 -07003237
bayramovef390722016-09-27 03:34:46 -07003238 def new_external_network(self, net_name, net_type):
bayramov5761ad12016-10-04 09:00:30 +04003239 """Adds a external network to VIM (shared)"""
bayramov325fa1c2016-09-08 01:42:46 -07003240 '''Returns the network identifier'''
bayramovbd6160f2016-09-28 04:12:05 +04003241 raise vimconn.vimconnNotImplemented("Should have implemented this")
bayramov325fa1c2016-09-08 01:42:46 -07003242
3243 def connect_port_network(self, port_id, network_id, admin=False):
bayramov5761ad12016-10-04 09:00:30 +04003244 """Connects a external port to a network"""
bayramov325fa1c2016-09-08 01:42:46 -07003245 '''Returns status code of the VIM response'''
bayramovbd6160f2016-09-28 04:12:05 +04003246 raise vimconn.vimconnNotImplemented("Should have implemented this")
bayramov325fa1c2016-09-08 01:42:46 -07003247
3248 def new_vminstancefromJSON(self, vm_data):
bayramov5761ad12016-10-04 09:00:30 +04003249 """Adds a VM instance to VIM"""
bayramov325fa1c2016-09-08 01:42:46 -07003250 '''Returns the instance identifier'''
bayramovbd6160f2016-09-28 04:12:05 +04003251 raise vimconn.vimconnNotImplemented("Should have implemented this")
bayramov325fa1c2016-09-08 01:42:46 -07003252
kate15f1c382016-12-15 01:12:40 -08003253 def get_network_name_by_id(self, network_uuid=None):
bayramovef390722016-09-27 03:34:46 -07003254 """Method gets vcloud director network named based on supplied uuid.
3255
3256 Args:
kate15f1c382016-12-15 01:12:40 -08003257 network_uuid: network_id
bayramovef390722016-09-27 03:34:46 -07003258
3259 Returns:
3260 The return network name.
3261 """
3262
kate15f1c382016-12-15 01:12:40 -08003263 if not network_uuid:
bayramovef390722016-09-27 03:34:46 -07003264 return None
3265
3266 try:
kate15f1c382016-12-15 01:12:40 -08003267 org_dict = self.get_org(self.org_uuid)
3268 if 'networks' in org_dict:
3269 org_network_dict = org_dict['networks']
3270 for net_uuid in org_network_dict:
3271 if net_uuid == network_uuid:
3272 return org_network_dict[net_uuid]
bayramovef390722016-09-27 03:34:46 -07003273 except:
3274 self.logger.debug("Exception in get_network_name_by_id")
3275 self.logger.debug(traceback.format_exc())
3276
3277 return None
3278
bhangare2c855072016-12-27 01:41:28 -08003279 def get_network_id_by_name(self, network_name=None):
3280 """Method gets vcloud director network uuid based on supplied name.
3281
3282 Args:
3283 network_name: network_name
3284 Returns:
3285 The return network uuid.
3286 network_uuid: network_id
3287 """
3288
bhangare2c855072016-12-27 01:41:28 -08003289 if not network_name:
3290 self.logger.debug("get_network_id_by_name() : Network name is empty")
3291 return None
3292
3293 try:
3294 org_dict = self.get_org(self.org_uuid)
3295 if org_dict and 'networks' in org_dict:
3296 org_network_dict = org_dict['networks']
3297 for net_uuid,net_name in org_network_dict.iteritems():
kasarc5bf2932018-03-09 04:15:22 -08003298 #For python3
3299 #for net_uuid,net_name in org_network_dict.items():
bhangare2c855072016-12-27 01:41:28 -08003300 if net_name == network_name:
3301 return net_uuid
3302
3303 except KeyError as exp:
3304 self.logger.debug("get_network_id_by_name() : KeyError- {} ".format(exp))
3305
3306 return None
3307
bayramovef390722016-09-27 03:34:46 -07003308 def list_org_action(self):
3309 """
3310 Method leverages vCloud director and query for available organization for particular user
3311
3312 Args:
3313 vca - is active VCA connection.
3314 vdc_name - is a vdc name that will be used to query vms action
3315
3316 Returns:
3317 The return XML respond
3318 """
kasarc5bf2932018-03-09 04:15:22 -08003319 url_list = [self.url, '/api/org']
bayramovef390722016-09-27 03:34:46 -07003320 vm_list_rest_call = ''.join(url_list)
3321
kasarc5bf2932018-03-09 04:15:22 -08003322 if self.client._session:
3323 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
3324 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
3325
3326 response = self.perform_request(req_type='GET',
3327 url=vm_list_rest_call,
3328 headers=headers)
bhangare1a0b97c2017-06-21 02:20:15 -07003329
3330 if response.status_code == 403:
3331 response = self.retry_rest('GET', vm_list_rest_call)
3332
bayramovef390722016-09-27 03:34:46 -07003333 if response.status_code == requests.codes.ok:
3334 return response.content
3335
3336 return None
3337
3338 def get_org_action(self, org_uuid=None):
3339 """
kasarc5bf2932018-03-09 04:15:22 -08003340 Method leverages vCloud director and retrieve available object for organization.
bayramovef390722016-09-27 03:34:46 -07003341
3342 Args:
kasarc5bf2932018-03-09 04:15:22 -08003343 org_uuid - vCD organization uuid
3344 self.client - is active connection.
bayramovef390722016-09-27 03:34:46 -07003345
3346 Returns:
3347 The return XML respond
3348 """
3349
bayramovef390722016-09-27 03:34:46 -07003350 if org_uuid is None:
3351 return None
3352
kasarc5bf2932018-03-09 04:15:22 -08003353 url_list = [self.url, '/api/org/', org_uuid]
bayramovef390722016-09-27 03:34:46 -07003354 vm_list_rest_call = ''.join(url_list)
3355
sbhangarea8e5b782018-06-21 02:10:03 -07003356 if self.client._session:
kasarc5bf2932018-03-09 04:15:22 -08003357 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
sbhangarea8e5b782018-06-21 02:10:03 -07003358 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
bhangare1a0b97c2017-06-21 02:20:15 -07003359
kasarc5bf2932018-03-09 04:15:22 -08003360 #response = requests.get(vm_list_rest_call, headers=headers, verify=False)
3361 response = self.perform_request(req_type='GET',
3362 url=vm_list_rest_call,
3363 headers=headers)
bhangare1a0b97c2017-06-21 02:20:15 -07003364 if response.status_code == 403:
3365 response = self.retry_rest('GET', vm_list_rest_call)
3366
bayramovef390722016-09-27 03:34:46 -07003367 if response.status_code == requests.codes.ok:
sbhangarea8e5b782018-06-21 02:10:03 -07003368 return response.content
bayramovef390722016-09-27 03:34:46 -07003369 return None
3370
3371 def get_org(self, org_uuid=None):
3372 """
3373 Method retrieves available organization in vCloud Director
3374
3375 Args:
bayramovb6ffe792016-09-28 11:50:56 +04003376 org_uuid - is a organization uuid.
bayramovef390722016-09-27 03:34:46 -07003377
3378 Returns:
bayramovb6ffe792016-09-28 11:50:56 +04003379 The return dictionary with following key
3380 "network" - for network list under the org
3381 "catalogs" - for network list under the org
3382 "vdcs" - for vdc list under org
bayramovef390722016-09-27 03:34:46 -07003383 """
3384
3385 org_dict = {}
bayramovef390722016-09-27 03:34:46 -07003386
3387 if org_uuid is None:
3388 return org_dict
3389
3390 content = self.get_org_action(org_uuid=org_uuid)
3391 try:
3392 vdc_list = {}
3393 network_list = {}
3394 catalog_list = {}
3395 vm_list_xmlroot = XmlElementTree.fromstring(content)
3396 for child in vm_list_xmlroot:
3397 if child.attrib['type'] == 'application/vnd.vmware.vcloud.vdc+xml':
3398 vdc_list[child.attrib['href'].split("/")[-1:][0]] = child.attrib['name']
3399 org_dict['vdcs'] = vdc_list
3400 if child.attrib['type'] == 'application/vnd.vmware.vcloud.orgNetwork+xml':
3401 network_list[child.attrib['href'].split("/")[-1:][0]] = child.attrib['name']
3402 org_dict['networks'] = network_list
3403 if child.attrib['type'] == 'application/vnd.vmware.vcloud.catalog+xml':
3404 catalog_list[child.attrib['href'].split("/")[-1:][0]] = child.attrib['name']
3405 org_dict['catalogs'] = catalog_list
3406 except:
3407 pass
3408
3409 return org_dict
3410
3411 def get_org_list(self):
3412 """
3413 Method retrieves available organization in vCloud Director
3414
3415 Args:
3416 vca - is active VCA connection.
3417
3418 Returns:
3419 The return dictionary and key for each entry VDC UUID
3420 """
3421
3422 org_dict = {}
bayramovef390722016-09-27 03:34:46 -07003423
3424 content = self.list_org_action()
3425 try:
3426 vm_list_xmlroot = XmlElementTree.fromstring(content)
3427 for vm_xml in vm_list_xmlroot:
3428 if vm_xml.tag.split("}")[1] == 'Org':
3429 org_uuid = vm_xml.attrib['href'].split('/')[-1:]
3430 org_dict[org_uuid[0]] = vm_xml.attrib['name']
3431 except:
3432 pass
3433
3434 return org_dict
3435
3436 def vms_view_action(self, vdc_name=None):
3437 """ Method leverages vCloud director vms query call
3438
3439 Args:
3440 vca - is active VCA connection.
3441 vdc_name - is a vdc name that will be used to query vms action
3442
3443 Returns:
3444 The return XML respond
3445 """
3446 vca = self.connect()
3447 if vdc_name is None:
3448 return None
3449
3450 url_list = [vca.host, '/api/vms/query']
3451 vm_list_rest_call = ''.join(url_list)
3452
3453 if not (not vca.vcloud_session or not vca.vcloud_session.organization):
3454 refs = filter(lambda ref: ref.name == vdc_name and ref.type_ == 'application/vnd.vmware.vcloud.vdc+xml',
3455 vca.vcloud_session.organization.Link)
kasarc5bf2932018-03-09 04:15:22 -08003456 #For python3
3457 #refs = [ref for ref in vca.vcloud_session.organization.Link if ref.name == vdc_name and\
3458 # ref.type_ == 'application/vnd.vmware.vcloud.vdc+xml']
bayramovef390722016-09-27 03:34:46 -07003459 if len(refs) == 1:
3460 response = Http.get(url=vm_list_rest_call,
3461 headers=vca.vcloud_session.get_vcloud_headers(),
3462 verify=vca.verify,
3463 logger=vca.logger)
3464 if response.status_code == requests.codes.ok:
3465 return response.content
3466
3467 return None
3468
3469 def get_vapp_list(self, vdc_name=None):
3470 """
3471 Method retrieves vApp list deployed vCloud director and returns a dictionary
3472 contains a list of all vapp deployed for queried VDC.
3473 The key for a dictionary is vApp UUID
3474
3475
3476 Args:
3477 vca - is active VCA connection.
3478 vdc_name - is a vdc name that will be used to query vms action
3479
3480 Returns:
3481 The return dictionary and key for each entry vapp UUID
3482 """
3483
3484 vapp_dict = {}
3485 if vdc_name is None:
3486 return vapp_dict
3487
3488 content = self.vms_view_action(vdc_name=vdc_name)
3489 try:
3490 vm_list_xmlroot = XmlElementTree.fromstring(content)
3491 for vm_xml in vm_list_xmlroot:
3492 if vm_xml.tag.split("}")[1] == 'VMRecord':
3493 if vm_xml.attrib['isVAppTemplate'] == 'true':
3494 rawuuid = vm_xml.attrib['container'].split('/')[-1:]
3495 if 'vappTemplate-' in rawuuid[0]:
3496 # vm in format vappTemplate-e63d40e7-4ff5-4c6d-851f-96c1e4da86a5 we remove
3497 # vm and use raw UUID as key
3498 vapp_dict[rawuuid[0][13:]] = vm_xml.attrib
3499 except:
3500 pass
3501
3502 return vapp_dict
3503
3504 def get_vm_list(self, vdc_name=None):
3505 """
3506 Method retrieves VM's list deployed vCloud director. It returns a dictionary
3507 contains a list of all VM's deployed for queried VDC.
3508 The key for a dictionary is VM UUID
3509
3510
3511 Args:
3512 vca - is active VCA connection.
3513 vdc_name - is a vdc name that will be used to query vms action
3514
3515 Returns:
3516 The return dictionary and key for each entry vapp UUID
3517 """
3518 vm_dict = {}
3519
3520 if vdc_name is None:
3521 return vm_dict
3522
3523 content = self.vms_view_action(vdc_name=vdc_name)
3524 try:
3525 vm_list_xmlroot = XmlElementTree.fromstring(content)
3526 for vm_xml in vm_list_xmlroot:
3527 if vm_xml.tag.split("}")[1] == 'VMRecord':
3528 if vm_xml.attrib['isVAppTemplate'] == 'false':
3529 rawuuid = vm_xml.attrib['href'].split('/')[-1:]
3530 if 'vm-' in rawuuid[0]:
3531 # vm in format vm-e63d40e7-4ff5-4c6d-851f-96c1e4da86a5 we remove
3532 # vm and use raw UUID as key
3533 vm_dict[rawuuid[0][3:]] = vm_xml.attrib
3534 except:
3535 pass
3536
3537 return vm_dict
3538
3539 def get_vapp(self, vdc_name=None, vapp_name=None, isuuid=False):
3540 """
bayramovb6ffe792016-09-28 11:50:56 +04003541 Method retrieves VM deployed vCloud director. It returns VM attribute as dictionary
bayramovef390722016-09-27 03:34:46 -07003542 contains a list of all VM's deployed for queried VDC.
3543 The key for a dictionary is VM UUID
3544
3545
3546 Args:
3547 vca - is active VCA connection.
3548 vdc_name - is a vdc name that will be used to query vms action
3549
3550 Returns:
3551 The return dictionary and key for each entry vapp UUID
3552 """
3553 vm_dict = {}
3554 vca = self.connect()
3555 if not vca:
3556 raise vimconn.vimconnConnectionException("self.connect() is failed")
3557
3558 if vdc_name is None:
3559 return vm_dict
3560
3561 content = self.vms_view_action(vdc_name=vdc_name)
3562 try:
3563 vm_list_xmlroot = XmlElementTree.fromstring(content)
3564 for vm_xml in vm_list_xmlroot:
bayramovb6ffe792016-09-28 11:50:56 +04003565 if vm_xml.tag.split("}")[1] == 'VMRecord' and vm_xml.attrib['isVAppTemplate'] == 'false':
3566 # lookup done by UUID
bayramovef390722016-09-27 03:34:46 -07003567 if isuuid:
bayramovef390722016-09-27 03:34:46 -07003568 if vapp_name in vm_xml.attrib['container']:
3569 rawuuid = vm_xml.attrib['href'].split('/')[-1:]
3570 if 'vm-' in rawuuid[0]:
bayramovef390722016-09-27 03:34:46 -07003571 vm_dict[rawuuid[0][3:]] = vm_xml.attrib
bayramovb6ffe792016-09-28 11:50:56 +04003572 break
3573 # lookup done by Name
3574 else:
3575 if vapp_name in vm_xml.attrib['name']:
3576 rawuuid = vm_xml.attrib['href'].split('/')[-1:]
3577 if 'vm-' in rawuuid[0]:
3578 vm_dict[rawuuid[0][3:]] = vm_xml.attrib
3579 break
bayramovef390722016-09-27 03:34:46 -07003580 except:
3581 pass
3582
3583 return vm_dict
3584
3585 def get_network_action(self, network_uuid=None):
3586 """
3587 Method leverages vCloud director and query network based on network uuid
3588
3589 Args:
3590 vca - is active VCA connection.
3591 network_uuid - is a network uuid
3592
3593 Returns:
3594 The return XML respond
3595 """
3596
bayramovef390722016-09-27 03:34:46 -07003597 if network_uuid is None:
3598 return None
3599
kasarc5bf2932018-03-09 04:15:22 -08003600 url_list = [self.url, '/api/network/', network_uuid]
bayramovef390722016-09-27 03:34:46 -07003601 vm_list_rest_call = ''.join(url_list)
3602
kasarc5bf2932018-03-09 04:15:22 -08003603 if self.client._session:
3604 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
3605 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
bhangare1a0b97c2017-06-21 02:20:15 -07003606
kasarc5bf2932018-03-09 04:15:22 -08003607 response = self.perform_request(req_type='GET',
3608 url=vm_list_rest_call,
3609 headers=headers)
bhangare1a0b97c2017-06-21 02:20:15 -07003610 #Retry login if session expired & retry sending request
3611 if response.status_code == 403:
3612 response = self.retry_rest('GET', vm_list_rest_call)
3613
bayramovef390722016-09-27 03:34:46 -07003614 if response.status_code == requests.codes.ok:
3615 return response.content
3616
3617 return None
3618
3619 def get_vcd_network(self, network_uuid=None):
3620 """
3621 Method retrieves available network from vCloud Director
3622
3623 Args:
3624 network_uuid - is VCD network UUID
3625
3626 Each element serialized as key : value pair
3627
3628 Following keys available for access. network_configuration['Gateway'}
3629 <Configuration>
3630 <IpScopes>
3631 <IpScope>
3632 <IsInherited>true</IsInherited>
3633 <Gateway>172.16.252.100</Gateway>
3634 <Netmask>255.255.255.0</Netmask>
3635 <Dns1>172.16.254.201</Dns1>
3636 <Dns2>172.16.254.202</Dns2>
3637 <DnsSuffix>vmwarelab.edu</DnsSuffix>
3638 <IsEnabled>true</IsEnabled>
3639 <IpRanges>
3640 <IpRange>
3641 <StartAddress>172.16.252.1</StartAddress>
3642 <EndAddress>172.16.252.99</EndAddress>
3643 </IpRange>
3644 </IpRanges>
3645 </IpScope>
3646 </IpScopes>
3647 <FenceMode>bridged</FenceMode>
3648
3649 Returns:
3650 The return dictionary and key for each entry vapp UUID
3651 """
3652
3653 network_configuration = {}
3654 if network_uuid is None:
3655 return network_uuid
3656
bayramovef390722016-09-27 03:34:46 -07003657 try:
bhangarebfdca492017-03-11 01:32:46 -08003658 content = self.get_network_action(network_uuid=network_uuid)
bayramovef390722016-09-27 03:34:46 -07003659 vm_list_xmlroot = XmlElementTree.fromstring(content)
3660
3661 network_configuration['status'] = vm_list_xmlroot.get("status")
3662 network_configuration['name'] = vm_list_xmlroot.get("name")
3663 network_configuration['uuid'] = vm_list_xmlroot.get("id").split(":")[3]
3664
3665 for child in vm_list_xmlroot:
3666 if child.tag.split("}")[1] == 'IsShared':
3667 network_configuration['isShared'] = child.text.strip()
3668 if child.tag.split("}")[1] == 'Configuration':
3669 for configuration in child.iter():
3670 tagKey = configuration.tag.split("}")[1].strip()
3671 if tagKey != "":
3672 network_configuration[tagKey] = configuration.text.strip()
3673 return network_configuration
bhangarebfdca492017-03-11 01:32:46 -08003674 except Exception as exp :
3675 self.logger.debug("get_vcd_network: Failed with Exception {}".format(exp))
3676 raise vimconn.vimconnException("get_vcd_network: Failed with Exception {}".format(exp))
bayramovef390722016-09-27 03:34:46 -07003677
3678 return network_configuration
3679
3680 def delete_network_action(self, network_uuid=None):
3681 """
3682 Method delete given network from vCloud director
3683
3684 Args:
3685 network_uuid - is a network uuid that client wish to delete
3686
3687 Returns:
3688 The return None or XML respond or false
3689 """
kasarc5bf2932018-03-09 04:15:22 -08003690 client = self.connect_as_admin()
3691 if not client:
3692 raise vimconn.vimconnConnectionException("Failed to connect vCD as admin")
bayramovef390722016-09-27 03:34:46 -07003693 if network_uuid is None:
3694 return False
3695
kasarc5bf2932018-03-09 04:15:22 -08003696 url_list = [self.url, '/api/admin/network/', network_uuid]
bayramovef390722016-09-27 03:34:46 -07003697 vm_list_rest_call = ''.join(url_list)
3698
kasarc5bf2932018-03-09 04:15:22 -08003699 if client._session:
3700 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
sbhangarea8e5b782018-06-21 02:10:03 -07003701 'x-vcloud-authorization': client._session.headers['x-vcloud-authorization']}
kasarc5bf2932018-03-09 04:15:22 -08003702 response = self.perform_request(req_type='DELETE',
3703 url=vm_list_rest_call,
3704 headers=headers)
bayramovef390722016-09-27 03:34:46 -07003705 if response.status_code == 202:
3706 return True
3707
3708 return False
3709
bhangare0e571a92017-01-12 04:02:23 -08003710 def create_network(self, network_name=None, net_type='bridge', parent_network_uuid=None,
3711 ip_profile=None, isshared='true'):
bayramovef390722016-09-27 03:34:46 -07003712 """
3713 Method create network in vCloud director
3714
3715 Args:
3716 network_name - is network name to be created.
bhangare0e571a92017-01-12 04:02:23 -08003717 net_type - can be 'bridge','data','ptp','mgmt'.
3718 ip_profile is a dict containing the IP parameters of the network
3719 isshared - is a boolean
bayramovef390722016-09-27 03:34:46 -07003720 parent_network_uuid - is parent provider vdc network that will be used for mapping.
3721 It optional attribute. by default if no parent network indicate the first available will be used.
3722
3723 Returns:
3724 The return network uuid or return None
3725 """
3726
bayramov5761ad12016-10-04 09:00:30 +04003727 new_network_name = [network_name, '-', str(uuid.uuid4())]
3728 content = self.create_network_rest(network_name=''.join(new_network_name),
bhangare0e571a92017-01-12 04:02:23 -08003729 ip_profile=ip_profile,
3730 net_type=net_type,
bayramovef390722016-09-27 03:34:46 -07003731 parent_network_uuid=parent_network_uuid,
3732 isshared=isshared)
3733 if content is None:
3734 self.logger.debug("Failed create network {}.".format(network_name))
3735 return None
3736
3737 try:
3738 vm_list_xmlroot = XmlElementTree.fromstring(content)
3739 vcd_uuid = vm_list_xmlroot.get('id').split(":")
3740 if len(vcd_uuid) == 4:
bhangarebfdca492017-03-11 01:32:46 -08003741 self.logger.info("Created new network name: {} uuid: {}".format(network_name, vcd_uuid[3]))
bayramovef390722016-09-27 03:34:46 -07003742 return vcd_uuid[3]
3743 except:
3744 self.logger.debug("Failed create network {}".format(network_name))
3745 return None
3746
bhangare0e571a92017-01-12 04:02:23 -08003747 def create_network_rest(self, network_name=None, net_type='bridge', parent_network_uuid=None,
3748 ip_profile=None, isshared='true'):
bayramovef390722016-09-27 03:34:46 -07003749 """
3750 Method create network in vCloud director
3751
3752 Args:
3753 network_name - is network name to be created.
bhangare0e571a92017-01-12 04:02:23 -08003754 net_type - can be 'bridge','data','ptp','mgmt'.
3755 ip_profile is a dict containing the IP parameters of the network
3756 isshared - is a boolean
bayramovef390722016-09-27 03:34:46 -07003757 parent_network_uuid - is parent provider vdc network that will be used for mapping.
3758 It optional attribute. by default if no parent network indicate the first available will be used.
3759
3760 Returns:
3761 The return network uuid or return None
3762 """
kasarc5bf2932018-03-09 04:15:22 -08003763 client_as_admin = self.connect_as_admin()
3764 if not client_as_admin:
3765 raise vimconn.vimconnConnectionException("Failed to connect vCD.")
bayramovef390722016-09-27 03:34:46 -07003766 if network_name is None:
3767 return None
3768
kasarc5bf2932018-03-09 04:15:22 -08003769 url_list = [self.url, '/api/admin/vdc/', self.tenant_id]
bayramovef390722016-09-27 03:34:46 -07003770 vm_list_rest_call = ''.join(url_list)
kasarc5bf2932018-03-09 04:15:22 -08003771
3772 if client_as_admin._session:
3773 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
3774 'x-vcloud-authorization': client_as_admin._session.headers['x-vcloud-authorization']}
3775
3776 response = self.perform_request(req_type='GET',
3777 url=vm_list_rest_call,
3778 headers=headers)
bayramovef390722016-09-27 03:34:46 -07003779
3780 provider_network = None
3781 available_networks = None
3782 add_vdc_rest_url = None
3783
3784 if response.status_code != requests.codes.ok:
3785 self.logger.debug("REST API call {} failed. Return status code {}".format(vm_list_rest_call,
3786 response.status_code))
3787 return None
3788 else:
3789 try:
3790 vm_list_xmlroot = XmlElementTree.fromstring(response.content)
3791 for child in vm_list_xmlroot:
3792 if child.tag.split("}")[1] == 'ProviderVdcReference':
3793 provider_network = child.attrib.get('href')
3794 # application/vnd.vmware.admin.providervdc+xml
3795 if child.tag.split("}")[1] == 'Link':
3796 if child.attrib.get('type') == 'application/vnd.vmware.vcloud.orgVdcNetwork+xml' \
3797 and child.attrib.get('rel') == 'add':
3798 add_vdc_rest_url = child.attrib.get('href')
3799 except:
3800 self.logger.debug("Failed parse respond for rest api call {}".format(vm_list_rest_call))
3801 self.logger.debug("Respond body {}".format(response.content))
3802 return None
3803
3804 # find pvdc provided available network
kasarc5bf2932018-03-09 04:15:22 -08003805 response = self.perform_request(req_type='GET',
3806 url=provider_network,
3807 headers=headers)
bayramovef390722016-09-27 03:34:46 -07003808 if response.status_code != requests.codes.ok:
3809 self.logger.debug("REST API call {} failed. Return status code {}".format(vm_list_rest_call,
3810 response.status_code))
3811 return None
3812
bayramovef390722016-09-27 03:34:46 -07003813 if parent_network_uuid is None:
3814 try:
3815 vm_list_xmlroot = XmlElementTree.fromstring(response.content)
3816 for child in vm_list_xmlroot.iter():
3817 if child.tag.split("}")[1] == 'AvailableNetworks':
3818 for networks in child.iter():
3819 # application/vnd.vmware.admin.network+xml
3820 if networks.attrib.get('href') is not None:
3821 available_networks = networks.attrib.get('href')
3822 break
3823 except:
3824 return None
3825
bhangarebfdca492017-03-11 01:32:46 -08003826 try:
3827 #Configure IP profile of the network
3828 ip_profile = ip_profile if ip_profile is not None else DEFAULT_IP_PROFILE
bhangare0e571a92017-01-12 04:02:23 -08003829
kasarde691232017-03-25 03:37:31 -07003830 if 'subnet_address' not in ip_profile or ip_profile['subnet_address'] is None:
3831 subnet_rand = random.randint(0, 255)
3832 ip_base = "192.168.{}.".format(subnet_rand)
3833 ip_profile['subnet_address'] = ip_base + "0/24"
3834 else:
3835 ip_base = ip_profile['subnet_address'].rsplit('.',1)[0] + '.'
3836
bhangarebfdca492017-03-11 01:32:46 -08003837 if 'gateway_address' not in ip_profile or ip_profile['gateway_address'] is None:
kasarde691232017-03-25 03:37:31 -07003838 ip_profile['gateway_address']=ip_base + "1"
bhangarebfdca492017-03-11 01:32:46 -08003839 if 'dhcp_count' not in ip_profile or ip_profile['dhcp_count'] is None:
3840 ip_profile['dhcp_count']=DEFAULT_IP_PROFILE['dhcp_count']
bhangarebfdca492017-03-11 01:32:46 -08003841 if 'dhcp_enabled' not in ip_profile or ip_profile['dhcp_enabled'] is None:
3842 ip_profile['dhcp_enabled']=DEFAULT_IP_PROFILE['dhcp_enabled']
3843 if 'dhcp_start_address' not in ip_profile or ip_profile['dhcp_start_address'] is None:
kasarde691232017-03-25 03:37:31 -07003844 ip_profile['dhcp_start_address']=ip_base + "3"
bhangarebfdca492017-03-11 01:32:46 -08003845 if 'ip_version' not in ip_profile or ip_profile['ip_version'] is None:
3846 ip_profile['ip_version']=DEFAULT_IP_PROFILE['ip_version']
3847 if 'dns_address' not in ip_profile or ip_profile['dns_address'] is None:
kasarde691232017-03-25 03:37:31 -07003848 ip_profile['dns_address']=ip_base + "2"
bhangare0e571a92017-01-12 04:02:23 -08003849
bhangarebfdca492017-03-11 01:32:46 -08003850 gateway_address=ip_profile['gateway_address']
3851 dhcp_count=int(ip_profile['dhcp_count'])
3852 subnet_address=self.convert_cidr_to_netmask(ip_profile['subnet_address'])
bhangare0e571a92017-01-12 04:02:23 -08003853
bhangarebfdca492017-03-11 01:32:46 -08003854 if ip_profile['dhcp_enabled']==True:
3855 dhcp_enabled='true'
3856 else:
3857 dhcp_enabled='false'
3858 dhcp_start_address=ip_profile['dhcp_start_address']
bhangare0e571a92017-01-12 04:02:23 -08003859
bhangarebfdca492017-03-11 01:32:46 -08003860 #derive dhcp_end_address from dhcp_start_address & dhcp_count
3861 end_ip_int = int(netaddr.IPAddress(dhcp_start_address))
3862 end_ip_int += dhcp_count - 1
3863 dhcp_end_address = str(netaddr.IPAddress(end_ip_int))
3864
3865 ip_version=ip_profile['ip_version']
3866 dns_address=ip_profile['dns_address']
3867 except KeyError as exp:
3868 self.logger.debug("Create Network REST: Key error {}".format(exp))
3869 raise vimconn.vimconnException("Create Network REST: Key error{}".format(exp))
bhangare0e571a92017-01-12 04:02:23 -08003870
bayramovef390722016-09-27 03:34:46 -07003871 # either use client provided UUID or search for a first available
3872 # if both are not defined we return none
3873 if parent_network_uuid is not None:
Ravi Chamartyb0ab2c02018-10-26 01:40:50 +00003874 provider_network = None
3875 available_networks = None
3876 add_vdc_rest_url = None
3877
3878 url_list = [self.url, '/api/admin/vdc/', self.tenant_id, '/networks']
bayramovef390722016-09-27 03:34:46 -07003879 add_vdc_rest_url = ''.join(url_list)
3880
Ravi Chamartyb0ab2c02018-10-26 01:40:50 +00003881 url_list = [self.url, '/api/admin/network/', parent_network_uuid]
3882 available_networks = ''.join(url_list)
3883
bhangare06312472017-03-30 05:49:07 -07003884 #Creating all networks as Direct Org VDC type networks.
3885 #Unused in case of Underlay (data/ptp) network interface.
Ananda Baitharu6d40da22019-01-14 13:05:58 +00003886 fence_mode="isolated"
bhangare06312472017-03-30 05:49:07 -07003887 is_inherited='false'
tierno455612d2017-05-30 16:40:10 +02003888 dns_list = dns_address.split(";")
3889 dns1 = dns_list[0]
3890 dns2_text = ""
3891 if len(dns_list) >= 2:
3892 dns2_text = "\n <Dns2>{}</Dns2>\n".format(dns_list[1])
bhangare06312472017-03-30 05:49:07 -07003893 data = """ <OrgVdcNetwork name="{0:s}" xmlns="http://www.vmware.com/vcloud/v1.5">
3894 <Description>Openmano created</Description>
3895 <Configuration>
3896 <IpScopes>
3897 <IpScope>
3898 <IsInherited>{1:s}</IsInherited>
3899 <Gateway>{2:s}</Gateway>
3900 <Netmask>{3:s}</Netmask>
tierno455612d2017-05-30 16:40:10 +02003901 <Dns1>{4:s}</Dns1>{5:s}
3902 <IsEnabled>{6:s}</IsEnabled>
bhangare06312472017-03-30 05:49:07 -07003903 <IpRanges>
3904 <IpRange>
tierno455612d2017-05-30 16:40:10 +02003905 <StartAddress>{7:s}</StartAddress>
3906 <EndAddress>{8:s}</EndAddress>
bhangare06312472017-03-30 05:49:07 -07003907 </IpRange>
3908 </IpRanges>
3909 </IpScope>
3910 </IpScopes>
Ananda Baitharu6d40da22019-01-14 13:05:58 +00003911 <FenceMode>{9:s}</FenceMode>
bhangare06312472017-03-30 05:49:07 -07003912 </Configuration>
Ananda Baitharu6d40da22019-01-14 13:05:58 +00003913 <IsShared>{10:s}</IsShared>
bhangare06312472017-03-30 05:49:07 -07003914 </OrgVdcNetwork> """.format(escape(network_name), is_inherited, gateway_address,
tierno455612d2017-05-30 16:40:10 +02003915 subnet_address, dns1, dns2_text, dhcp_enabled,
Ananda Baitharu6d40da22019-01-14 13:05:58 +00003916 dhcp_start_address, dhcp_end_address,
bhangare06312472017-03-30 05:49:07 -07003917 fence_mode, isshared)
bayramovef390722016-09-27 03:34:46 -07003918
bayramovef390722016-09-27 03:34:46 -07003919 headers['Content-Type'] = 'application/vnd.vmware.vcloud.orgVdcNetwork+xml'
bhangare0e571a92017-01-12 04:02:23 -08003920 try:
kasarc5bf2932018-03-09 04:15:22 -08003921 response = self.perform_request(req_type='POST',
3922 url=add_vdc_rest_url,
3923 headers=headers,
3924 data=data)
bayramovef390722016-09-27 03:34:46 -07003925
bhangare0e571a92017-01-12 04:02:23 -08003926 if response.status_code != 201:
bhangarebfdca492017-03-11 01:32:46 -08003927 self.logger.debug("Create Network POST REST API call failed. Return status code {}, Response content: {}"
3928 .format(response.status_code,response.content))
bhangare0e571a92017-01-12 04:02:23 -08003929 else:
kasarc5bf2932018-03-09 04:15:22 -08003930 network_task = self.get_task_from_response(response.content)
3931 self.logger.debug("Create Network REST : Waiting for Network creation complete")
3932 time.sleep(5)
3933 result = self.client.get_task_monitor().wait_for_success(task=network_task)
sbhangarea8e5b782018-06-21 02:10:03 -07003934 if result.get('status') == 'success':
kasarc5bf2932018-03-09 04:15:22 -08003935 return response.content
3936 else:
3937 self.logger.debug("create_network_rest task failed. Network Create response : {}"
3938 .format(response.content))
bhangare0e571a92017-01-12 04:02:23 -08003939 except Exception as exp:
3940 self.logger.debug("create_network_rest : Exception : {} ".format(exp))
3941
3942 return None
3943
3944 def convert_cidr_to_netmask(self, cidr_ip=None):
3945 """
3946 Method sets convert CIDR netmask address to normal IP format
3947 Args:
3948 cidr_ip : CIDR IP address
3949 Returns:
3950 netmask : Converted netmask
3951 """
3952 if cidr_ip is not None:
3953 if '/' in cidr_ip:
3954 network, net_bits = cidr_ip.split('/')
3955 netmask = socket.inet_ntoa(struct.pack(">I", (0xffffffff << (32 - int(net_bits))) & 0xffffffff))
3956 else:
3957 netmask = cidr_ip
3958 return netmask
bayramovef390722016-09-27 03:34:46 -07003959 return None
3960
3961 def get_provider_rest(self, vca=None):
3962 """
3963 Method gets provider vdc view from vcloud director
3964
3965 Args:
3966 network_name - is network name to be created.
3967 parent_network_uuid - is parent provider vdc network that will be used for mapping.
3968 It optional attribute. by default if no parent network indicate the first available will be used.
3969
3970 Returns:
3971 The return xml content of respond or None
3972 """
3973
kasarc5bf2932018-03-09 04:15:22 -08003974 url_list = [self.url, '/api/admin']
3975 if vca:
3976 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
3977 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
3978 response = self.perform_request(req_type='GET',
3979 url=''.join(url_list),
3980 headers=headers)
bayramovef390722016-09-27 03:34:46 -07003981
3982 if response.status_code == requests.codes.ok:
3983 return response.content
3984 return None
3985
3986 def create_vdc(self, vdc_name=None):
3987
3988 vdc_dict = {}
3989
3990 xml_content = self.create_vdc_from_tmpl_rest(vdc_name=vdc_name)
3991 if xml_content is not None:
bayramovef390722016-09-27 03:34:46 -07003992 try:
3993 task_resp_xmlroot = XmlElementTree.fromstring(xml_content)
3994 for child in task_resp_xmlroot:
3995 if child.tag.split("}")[1] == 'Owner':
3996 vdc_id = child.attrib.get('href').split("/")[-1]
3997 vdc_dict[vdc_id] = task_resp_xmlroot.get('href')
3998 return vdc_dict
3999 except:
4000 self.logger.debug("Respond body {}".format(xml_content))
4001
4002 return None
4003
4004 def create_vdc_from_tmpl_rest(self, vdc_name=None):
4005 """
4006 Method create vdc in vCloud director based on VDC template.
kasarc5bf2932018-03-09 04:15:22 -08004007 it uses pre-defined template.
bayramovef390722016-09-27 03:34:46 -07004008
4009 Args:
4010 vdc_name - name of a new vdc.
4011
4012 Returns:
4013 The return xml content of respond or None
4014 """
kasarc5bf2932018-03-09 04:15:22 -08004015 # pre-requesite atleast one vdc template should be available in vCD
bayramovef390722016-09-27 03:34:46 -07004016 self.logger.info("Creating new vdc {}".format(vdc_name))
kasarc5bf2932018-03-09 04:15:22 -08004017 vca = self.connect_as_admin()
bayramovef390722016-09-27 03:34:46 -07004018 if not vca:
kasarc5bf2932018-03-09 04:15:22 -08004019 raise vimconn.vimconnConnectionException("Failed to connect vCD")
bayramovef390722016-09-27 03:34:46 -07004020 if vdc_name is None:
4021 return None
4022
kasarc5bf2932018-03-09 04:15:22 -08004023 url_list = [self.url, '/api/vdcTemplates']
bayramovef390722016-09-27 03:34:46 -07004024 vm_list_rest_call = ''.join(url_list)
kasarc5bf2932018-03-09 04:15:22 -08004025
4026 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
4027 'x-vcloud-authorization': vca._session.headers['x-vcloud-authorization']}
4028 response = self.perform_request(req_type='GET',
4029 url=vm_list_rest_call,
4030 headers=headers)
bayramovef390722016-09-27 03:34:46 -07004031
4032 # container url to a template
4033 vdc_template_ref = None
4034 try:
4035 vm_list_xmlroot = XmlElementTree.fromstring(response.content)
4036 for child in vm_list_xmlroot:
4037 # application/vnd.vmware.admin.providervdc+xml
4038 # we need find a template from witch we instantiate VDC
4039 if child.tag.split("}")[1] == 'VdcTemplate':
kated47ad5f2017-08-03 02:16:13 -07004040 if child.attrib.get('type') == 'application/vnd.vmware.admin.vdcTemplate+xml':
bayramovef390722016-09-27 03:34:46 -07004041 vdc_template_ref = child.attrib.get('href')
4042 except:
4043 self.logger.debug("Failed parse respond for rest api call {}".format(vm_list_rest_call))
4044 self.logger.debug("Respond body {}".format(response.content))
4045 return None
4046
4047 # if we didn't found required pre defined template we return None
4048 if vdc_template_ref is None:
4049 return None
4050
4051 try:
4052 # instantiate vdc
kasarc5bf2932018-03-09 04:15:22 -08004053 url_list = [self.url, '/api/org/', self.org_uuid, '/action/instantiate']
bayramovef390722016-09-27 03:34:46 -07004054 vm_list_rest_call = ''.join(url_list)
4055 data = """<InstantiateVdcTemplateParams name="{0:s}" xmlns="http://www.vmware.com/vcloud/v1.5">
4056 <Source href="{1:s}"></Source>
4057 <Description>opnemano</Description>
4058 </InstantiateVdcTemplateParams>""".format(vdc_name, vdc_template_ref)
kated47ad5f2017-08-03 02:16:13 -07004059
kasarc5bf2932018-03-09 04:15:22 -08004060 headers['Content-Type'] = 'application/vnd.vmware.vcloud.instantiateVdcTemplateParams+xml'
4061
4062 response = self.perform_request(req_type='POST',
4063 url=vm_list_rest_call,
4064 headers=headers,
4065 data=data)
4066
4067 vdc_task = self.get_task_from_response(response.content)
4068 self.client.get_task_monitor().wait_for_success(task=vdc_task)
kated47ad5f2017-08-03 02:16:13 -07004069
bayramovef390722016-09-27 03:34:46 -07004070 # if we all ok we respond with content otherwise by default None
4071 if response.status_code >= 200 and response.status_code < 300:
4072 return response.content
4073 return None
4074 except:
4075 self.logger.debug("Failed parse respond for rest api call {}".format(vm_list_rest_call))
4076 self.logger.debug("Respond body {}".format(response.content))
4077
4078 return None
4079
4080 def create_vdc_rest(self, vdc_name=None):
4081 """
4082 Method create network in vCloud director
4083
4084 Args:
kasarc5bf2932018-03-09 04:15:22 -08004085 vdc_name - vdc name to be created
bayramovef390722016-09-27 03:34:46 -07004086 Returns:
kasarc5bf2932018-03-09 04:15:22 -08004087 The return response
bayramovef390722016-09-27 03:34:46 -07004088 """
4089
4090 self.logger.info("Creating new vdc {}".format(vdc_name))
bayramovef390722016-09-27 03:34:46 -07004091
4092 vca = self.connect_as_admin()
4093 if not vca:
kasarc5bf2932018-03-09 04:15:22 -08004094 raise vimconn.vimconnConnectionException("Failed to connect vCD")
bayramovef390722016-09-27 03:34:46 -07004095 if vdc_name is None:
4096 return None
4097
kasarc5bf2932018-03-09 04:15:22 -08004098 url_list = [self.url, '/api/admin/org/', self.org_uuid]
bayramovef390722016-09-27 03:34:46 -07004099 vm_list_rest_call = ''.join(url_list)
kasarc5bf2932018-03-09 04:15:22 -08004100
4101 if vca._session:
4102 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
4103 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
4104 response = self.perform_request(req_type='GET',
4105 url=vm_list_rest_call,
4106 headers=headers)
bayramovef390722016-09-27 03:34:46 -07004107
4108 provider_vdc_ref = None
4109 add_vdc_rest_url = None
4110 available_networks = None
4111
4112 if response.status_code != requests.codes.ok:
4113 self.logger.debug("REST API call {} failed. Return status code {}".format(vm_list_rest_call,
4114 response.status_code))
4115 return None
4116 else:
4117 try:
4118 vm_list_xmlroot = XmlElementTree.fromstring(response.content)
4119 for child in vm_list_xmlroot:
4120 # application/vnd.vmware.admin.providervdc+xml
4121 if child.tag.split("}")[1] == 'Link':
4122 if child.attrib.get('type') == 'application/vnd.vmware.admin.createVdcParams+xml' \
4123 and child.attrib.get('rel') == 'add':
4124 add_vdc_rest_url = child.attrib.get('href')
4125 except:
4126 self.logger.debug("Failed parse respond for rest api call {}".format(vm_list_rest_call))
4127 self.logger.debug("Respond body {}".format(response.content))
4128 return None
4129
4130 response = self.get_provider_rest(vca=vca)
bayramovef390722016-09-27 03:34:46 -07004131 try:
4132 vm_list_xmlroot = XmlElementTree.fromstring(response)
4133 for child in vm_list_xmlroot:
4134 if child.tag.split("}")[1] == 'ProviderVdcReferences':
4135 for sub_child in child:
4136 provider_vdc_ref = sub_child.attrib.get('href')
4137 except:
4138 self.logger.debug("Failed parse respond for rest api call {}".format(vm_list_rest_call))
4139 self.logger.debug("Respond body {}".format(response))
4140 return None
4141
bayramovef390722016-09-27 03:34:46 -07004142 if add_vdc_rest_url is not None and provider_vdc_ref is not None:
4143 data = """ <CreateVdcParams name="{0:s}" xmlns="http://www.vmware.com/vcloud/v1.5"><Description>{1:s}</Description>
4144 <AllocationModel>ReservationPool</AllocationModel>
4145 <ComputeCapacity><Cpu><Units>MHz</Units><Allocated>2048</Allocated><Limit>2048</Limit></Cpu>
4146 <Memory><Units>MB</Units><Allocated>2048</Allocated><Limit>2048</Limit></Memory>
4147 </ComputeCapacity><NicQuota>0</NicQuota><NetworkQuota>100</NetworkQuota>
4148 <VdcStorageProfile><Enabled>true</Enabled><Units>MB</Units><Limit>20480</Limit><Default>true</Default></VdcStorageProfile>
4149 <ProviderVdcReference
4150 name="Main Provider"
4151 href="{2:s}" />
4152 <UsesFastProvisioning>true</UsesFastProvisioning></CreateVdcParams>""".format(escape(vdc_name),
4153 escape(vdc_name),
4154 provider_vdc_ref)
4155
bayramovef390722016-09-27 03:34:46 -07004156 headers['Content-Type'] = 'application/vnd.vmware.admin.createVdcParams+xml'
kasarc5bf2932018-03-09 04:15:22 -08004157
4158 response = self.perform_request(req_type='POST',
4159 url=add_vdc_rest_url,
4160 headers=headers,
4161 data=data)
bayramovef390722016-09-27 03:34:46 -07004162
bayramovef390722016-09-27 03:34:46 -07004163 # if we all ok we respond with content otherwise by default None
4164 if response.status_code == 201:
4165 return response.content
4166 return None
bayramovfe3f3c92016-10-04 07:53:41 +04004167
bhangarefda5f7c2017-01-12 23:50:34 -08004168 def get_vapp_details_rest(self, vapp_uuid=None, need_admin_access=False):
bayramovfe3f3c92016-10-04 07:53:41 +04004169 """
4170 Method retrieve vapp detail from vCloud director
4171
4172 Args:
4173 vapp_uuid - is vapp identifier.
4174
4175 Returns:
4176 The return network uuid or return None
4177 """
4178
4179 parsed_respond = {}
bhangarefda5f7c2017-01-12 23:50:34 -08004180 vca = None
bayramovfe3f3c92016-10-04 07:53:41 +04004181
bhangarefda5f7c2017-01-12 23:50:34 -08004182 if need_admin_access:
4183 vca = self.connect_as_admin()
4184 else:
sbhangarea8e5b782018-06-21 02:10:03 -07004185 vca = self.client
bhangarefda5f7c2017-01-12 23:50:34 -08004186
bayramovfe3f3c92016-10-04 07:53:41 +04004187 if not vca:
kasarc5bf2932018-03-09 04:15:22 -08004188 raise vimconn.vimconnConnectionException("Failed to connect vCD")
bayramovfe3f3c92016-10-04 07:53:41 +04004189 if vapp_uuid is None:
4190 return None
4191
kasarc5bf2932018-03-09 04:15:22 -08004192 url_list = [self.url, '/api/vApp/vapp-', vapp_uuid]
bayramovfe3f3c92016-10-04 07:53:41 +04004193 get_vapp_restcall = ''.join(url_list)
bhangarefda5f7c2017-01-12 23:50:34 -08004194
kasarc5bf2932018-03-09 04:15:22 -08004195 if vca._session:
4196 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
sbhangarea8e5b782018-06-21 02:10:03 -07004197 'x-vcloud-authorization': vca._session.headers['x-vcloud-authorization']}
kasarc5bf2932018-03-09 04:15:22 -08004198 response = self.perform_request(req_type='GET',
4199 url=get_vapp_restcall,
4200 headers=headers)
bayramovfe3f3c92016-10-04 07:53:41 +04004201
bhangare1a0b97c2017-06-21 02:20:15 -07004202 if response.status_code == 403:
4203 if need_admin_access == False:
4204 response = self.retry_rest('GET', get_vapp_restcall)
4205
bayramovfe3f3c92016-10-04 07:53:41 +04004206 if response.status_code != requests.codes.ok:
4207 self.logger.debug("REST API call {} failed. Return status code {}".format(get_vapp_restcall,
4208 response.status_code))
4209 return parsed_respond
4210
4211 try:
4212 xmlroot_respond = XmlElementTree.fromstring(response.content)
4213 parsed_respond['ovfDescriptorUploaded'] = xmlroot_respond.attrib['ovfDescriptorUploaded']
4214
bhangarea92ae392017-01-12 22:30:29 -08004215 namespaces = {"vssd":"http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData" ,
4216 'ovf': 'http://schemas.dmtf.org/ovf/envelope/1',
4217 'vmw': 'http://www.vmware.com/schema/ovf',
4218 'vm': 'http://www.vmware.com/vcloud/v1.5',
4219 'rasd':"http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData",
4220 "vmext":"http://www.vmware.com/vcloud/extension/v1.5",
4221 "xmlns":"http://www.vmware.com/vcloud/v1.5"
4222 }
bayramovfe3f3c92016-10-04 07:53:41 +04004223
bhangarea92ae392017-01-12 22:30:29 -08004224 created_section = xmlroot_respond.find('vm:DateCreated', namespaces)
bayramovfe3f3c92016-10-04 07:53:41 +04004225 if created_section is not None:
4226 parsed_respond['created'] = created_section.text
4227
bhangarea92ae392017-01-12 22:30:29 -08004228 network_section = xmlroot_respond.find('vm:NetworkConfigSection/vm:NetworkConfig', namespaces)
bayramov5761ad12016-10-04 09:00:30 +04004229 if network_section is not None and 'networkName' in network_section.attrib:
bayramovfe3f3c92016-10-04 07:53:41 +04004230 parsed_respond['networkname'] = network_section.attrib['networkName']
4231
4232 ipscopes_section = \
4233 xmlroot_respond.find('vm:NetworkConfigSection/vm:NetworkConfig/vm:Configuration/vm:IpScopes',
bhangarea92ae392017-01-12 22:30:29 -08004234 namespaces)
bayramovfe3f3c92016-10-04 07:53:41 +04004235 if ipscopes_section is not None:
4236 for ipscope in ipscopes_section:
4237 for scope in ipscope:
4238 tag_key = scope.tag.split("}")[1]
4239 if tag_key == 'IpRanges':
4240 ip_ranges = scope.getchildren()
4241 for ipblock in ip_ranges:
4242 for block in ipblock:
4243 parsed_respond[block.tag.split("}")[1]] = block.text
4244 else:
4245 parsed_respond[tag_key] = scope.text
4246
4247 # parse children section for other attrib
bhangarea92ae392017-01-12 22:30:29 -08004248 children_section = xmlroot_respond.find('vm:Children/', namespaces)
bayramovfe3f3c92016-10-04 07:53:41 +04004249 if children_section is not None:
4250 parsed_respond['name'] = children_section.attrib['name']
bhangarea92ae392017-01-12 22:30:29 -08004251 parsed_respond['nestedHypervisorEnabled'] = children_section.attrib['nestedHypervisorEnabled'] \
4252 if "nestedHypervisorEnabled" in children_section.attrib else None
bayramovfe3f3c92016-10-04 07:53:41 +04004253 parsed_respond['deployed'] = children_section.attrib['deployed']
4254 parsed_respond['status'] = children_section.attrib['status']
4255 parsed_respond['vmuuid'] = children_section.attrib['id'].split(":")[-1]
bhangarea92ae392017-01-12 22:30:29 -08004256 network_adapter = children_section.find('vm:NetworkConnectionSection', namespaces)
bayramovfe3f3c92016-10-04 07:53:41 +04004257 nic_list = []
4258 for adapters in network_adapter:
4259 adapter_key = adapters.tag.split("}")[1]
4260 if adapter_key == 'PrimaryNetworkConnectionIndex':
4261 parsed_respond['primarynetwork'] = adapters.text
4262 if adapter_key == 'NetworkConnection':
4263 vnic = {}
bayramov5761ad12016-10-04 09:00:30 +04004264 if 'network' in adapters.attrib:
bayramovfe3f3c92016-10-04 07:53:41 +04004265 vnic['network'] = adapters.attrib['network']
4266 for adapter in adapters:
4267 setting_key = adapter.tag.split("}")[1]
4268 vnic[setting_key] = adapter.text
4269 nic_list.append(vnic)
4270
4271 for link in children_section:
bayramov5761ad12016-10-04 09:00:30 +04004272 if link.tag.split("}")[1] == 'Link' and 'rel' in link.attrib:
bayramovfe3f3c92016-10-04 07:53:41 +04004273 if link.attrib['rel'] == 'screen:acquireTicket':
4274 parsed_respond['acquireTicket'] = link.attrib
4275 if link.attrib['rel'] == 'screen:acquireMksTicket':
4276 parsed_respond['acquireMksTicket'] = link.attrib
4277
4278 parsed_respond['interfaces'] = nic_list
bhangarefda5f7c2017-01-12 23:50:34 -08004279 vCloud_extension_section = children_section.find('xmlns:VCloudExtension', namespaces)
4280 if vCloud_extension_section is not None:
4281 vm_vcenter_info = {}
4282 vim_info = vCloud_extension_section.find('vmext:VmVimInfo', namespaces)
4283 vmext = vim_info.find('vmext:VmVimObjectRef', namespaces)
4284 if vmext is not None:
4285 vm_vcenter_info["vm_moref_id"] = vmext.find('vmext:MoRef', namespaces).text
bhangarefda5f7c2017-01-12 23:50:34 -08004286 parsed_respond["vm_vcenter_info"]= vm_vcenter_info
bayramovfe3f3c92016-10-04 07:53:41 +04004287
bhangarea92ae392017-01-12 22:30:29 -08004288 virtual_hardware_section = children_section.find('ovf:VirtualHardwareSection', namespaces)
4289 vm_virtual_hardware_info = {}
4290 if virtual_hardware_section is not None:
4291 for item in virtual_hardware_section.iterfind('ovf:Item',namespaces):
4292 if item.find("rasd:Description",namespaces).text == "Hard disk":
4293 disk_size = item.find("rasd:HostResource" ,namespaces
4294 ).attrib["{"+namespaces['vm']+"}capacity"]
4295
4296 vm_virtual_hardware_info["disk_size"]= disk_size
4297 break
4298
4299 for link in virtual_hardware_section:
4300 if link.tag.split("}")[1] == 'Link' and 'rel' in link.attrib:
4301 if link.attrib['rel'] == 'edit' and link.attrib['href'].endswith("/disks"):
4302 vm_virtual_hardware_info["disk_edit_href"] = link.attrib['href']
4303 break
4304
4305 parsed_respond["vm_virtual_hardware"]= vm_virtual_hardware_info
4306 except Exception as exp :
4307 self.logger.info("Error occurred calling rest api for getting vApp details {}".format(exp))
bayramovfe3f3c92016-10-04 07:53:41 +04004308 return parsed_respond
4309
kasarc5bf2932018-03-09 04:15:22 -08004310 def acquire_console(self, vm_uuid=None):
bayramovfe3f3c92016-10-04 07:53:41 +04004311
bayramovfe3f3c92016-10-04 07:53:41 +04004312 if vm_uuid is None:
4313 return None
kasarc5bf2932018-03-09 04:15:22 -08004314 if self.client._session:
4315 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
4316 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
4317 vm_dict = self.get_vapp_details_rest(vapp_uuid=vm_uuid)
bayramovfe3f3c92016-10-04 07:53:41 +04004318 console_dict = vm_dict['acquireTicket']
4319 console_rest_call = console_dict['href']
4320
kasarc5bf2932018-03-09 04:15:22 -08004321 response = self.perform_request(req_type='POST',
4322 url=console_rest_call,
4323 headers=headers)
4324
bhangare1a0b97c2017-06-21 02:20:15 -07004325 if response.status_code == 403:
4326 response = self.retry_rest('POST', console_rest_call)
bayramovfe3f3c92016-10-04 07:53:41 +04004327
bayramov5761ad12016-10-04 09:00:30 +04004328 if response.status_code == requests.codes.ok:
4329 return response.content
bayramovfe3f3c92016-10-04 07:53:41 +04004330
kate15f1c382016-12-15 01:12:40 -08004331 return None
kate13ab2c42016-12-23 01:34:24 -08004332
bhangarea92ae392017-01-12 22:30:29 -08004333 def modify_vm_disk(self, vapp_uuid, flavor_disk):
4334 """
4335 Method retrieve vm disk details
4336
4337 Args:
4338 vapp_uuid - is vapp identifier.
4339 flavor_disk - disk size as specified in VNFD (flavor)
4340
4341 Returns:
4342 The return network uuid or return None
4343 """
4344 status = None
4345 try:
4346 #Flavor disk is in GB convert it into MB
4347 flavor_disk = int(flavor_disk) * 1024
4348 vm_details = self.get_vapp_details_rest(vapp_uuid)
4349 if vm_details:
4350 vm_name = vm_details["name"]
4351 self.logger.info("VM: {} flavor_disk :{}".format(vm_name , flavor_disk))
4352
4353 if vm_details and "vm_virtual_hardware" in vm_details:
4354 vm_disk = int(vm_details["vm_virtual_hardware"]["disk_size"])
4355 disk_edit_href = vm_details["vm_virtual_hardware"]["disk_edit_href"]
4356
4357 self.logger.info("VM: {} VM_disk :{}".format(vm_name , vm_disk))
4358
4359 if flavor_disk > vm_disk:
4360 status = self.modify_vm_disk_rest(disk_edit_href ,flavor_disk)
4361 self.logger.info("Modify disk of VM {} from {} to {} MB".format(vm_name,
4362 vm_disk, flavor_disk ))
4363 else:
4364 status = True
4365 self.logger.info("No need to modify disk of VM {}".format(vm_name))
4366
4367 return status
4368 except Exception as exp:
4369 self.logger.info("Error occurred while modifing disk size {}".format(exp))
4370
4371
4372 def modify_vm_disk_rest(self, disk_href , disk_size):
4373 """
4374 Method retrieve modify vm disk size
4375
4376 Args:
4377 disk_href - vCD API URL to GET and PUT disk data
4378 disk_size - disk size as specified in VNFD (flavor)
4379
4380 Returns:
4381 The return network uuid or return None
4382 """
bhangarea92ae392017-01-12 22:30:29 -08004383 if disk_href is None or disk_size is None:
4384 return None
4385
kasarc5bf2932018-03-09 04:15:22 -08004386 if self.client._session:
4387 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
sbhangarea8e5b782018-06-21 02:10:03 -07004388 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
kasarc5bf2932018-03-09 04:15:22 -08004389 response = self.perform_request(req_type='GET',
4390 url=disk_href,
4391 headers=headers)
bhangare1a0b97c2017-06-21 02:20:15 -07004392
4393 if response.status_code == 403:
4394 response = self.retry_rest('GET', disk_href)
bhangarea92ae392017-01-12 22:30:29 -08004395
4396 if response.status_code != requests.codes.ok:
4397 self.logger.debug("GET REST API call {} failed. Return status code {}".format(disk_href,
4398 response.status_code))
4399 return None
4400 try:
4401 lxmlroot_respond = lxmlElementTree.fromstring(response.content)
4402 namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.iteritems() if prefix}
kasarc5bf2932018-03-09 04:15:22 -08004403 #For python3
4404 #namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.items() if prefix}
bhangarea92ae392017-01-12 22:30:29 -08004405 namespaces["xmlns"]= "http://www.vmware.com/vcloud/v1.5"
4406
4407 for item in lxmlroot_respond.iterfind('xmlns:Item',namespaces):
4408 if item.find("rasd:Description",namespaces).text == "Hard disk":
4409 disk_item = item.find("rasd:HostResource" ,namespaces )
4410 if disk_item is not None:
4411 disk_item.attrib["{"+namespaces['xmlns']+"}capacity"] = str(disk_size)
4412 break
4413
4414 data = lxmlElementTree.tostring(lxmlroot_respond, encoding='utf8', method='xml',
4415 xml_declaration=True)
4416
4417 #Send PUT request to modify disk size
bhangarea92ae392017-01-12 22:30:29 -08004418 headers['Content-Type'] = 'application/vnd.vmware.vcloud.rasdItemsList+xml; charset=ISO-8859-1'
4419
kasarc5bf2932018-03-09 04:15:22 -08004420 response = self.perform_request(req_type='PUT',
4421 url=disk_href,
4422 headers=headers,
4423 data=data)
bhangare1a0b97c2017-06-21 02:20:15 -07004424 if response.status_code == 403:
4425 add_headers = {'Content-Type': headers['Content-Type']}
4426 response = self.retry_rest('PUT', disk_href, add_headers, data)
bhangarea92ae392017-01-12 22:30:29 -08004427
4428 if response.status_code != 202:
4429 self.logger.debug("PUT REST API call {} failed. Return status code {}".format(disk_href,
4430 response.status_code))
4431 else:
kasarc5bf2932018-03-09 04:15:22 -08004432 modify_disk_task = self.get_task_from_response(response.content)
4433 result = self.client.get_task_monitor().wait_for_success(task=modify_disk_task)
4434 if result.get('status') == 'success':
4435 return True
4436 else:
sbhangarea8e5b782018-06-21 02:10:03 -07004437 return False
bhangarea92ae392017-01-12 22:30:29 -08004438 return None
4439
4440 except Exception as exp :
4441 self.logger.info("Error occurred calling rest api for modifing disk size {}".format(exp))
4442 return None
4443
bhangarefda5f7c2017-01-12 23:50:34 -08004444 def add_pci_devices(self, vapp_uuid , pci_devices , vmname_andid):
4445 """
4446 Method to attach pci devices to VM
4447
4448 Args:
4449 vapp_uuid - uuid of vApp/VM
4450 pci_devices - pci devices infromation as specified in VNFD (flavor)
4451
4452 Returns:
4453 The status of add pci device task , vm object and
4454 vcenter_conect object
4455 """
4456 vm_obj = None
bhangarefda5f7c2017-01-12 23:50:34 -08004457 self.logger.info("Add pci devices {} into vApp {}".format(pci_devices , vapp_uuid))
bhangare06312472017-03-30 05:49:07 -07004458 vcenter_conect, content = self.get_vcenter_content()
4459 vm_moref_id = self.get_vm_moref_id(vapp_uuid)
kateeb044522017-03-06 23:54:39 -08004460
bhangare06312472017-03-30 05:49:07 -07004461 if vm_moref_id:
bhangarefda5f7c2017-01-12 23:50:34 -08004462 try:
4463 no_of_pci_devices = len(pci_devices)
4464 if no_of_pci_devices > 0:
bhangarefda5f7c2017-01-12 23:50:34 -08004465 #Get VM and its host
bhangare06312472017-03-30 05:49:07 -07004466 host_obj, vm_obj = self.get_vm_obj(content, vm_moref_id)
bhangarefda5f7c2017-01-12 23:50:34 -08004467 self.logger.info("VM {} is currently on host {}".format(vm_obj, host_obj))
4468 if host_obj and vm_obj:
4469 #get PCI devies from host on which vapp is currently installed
4470 avilable_pci_devices = self.get_pci_devices(host_obj, no_of_pci_devices)
4471
4472 if avilable_pci_devices is None:
4473 #find other hosts with active pci devices
4474 new_host_obj , avilable_pci_devices = self.get_host_and_PCIdevices(
4475 content,
4476 no_of_pci_devices
4477 )
4478
4479 if new_host_obj is not None and avilable_pci_devices is not None and len(avilable_pci_devices)> 0:
4480 #Migrate vm to the host where PCI devices are availble
4481 self.logger.info("Relocate VM {} on new host {}".format(vm_obj, new_host_obj))
4482 task = self.relocate_vm(new_host_obj, vm_obj)
4483 if task is not None:
4484 result = self.wait_for_vcenter_task(task, vcenter_conect)
4485 self.logger.info("Migrate VM status: {}".format(result))
4486 host_obj = new_host_obj
4487 else:
4488 self.logger.info("Fail to migrate VM : {}".format(result))
4489 raise vimconn.vimconnNotFoundException(
4490 "Fail to migrate VM : {} to host {}".format(
4491 vmname_andid,
4492 new_host_obj)
4493 )
4494
4495 if host_obj is not None and avilable_pci_devices is not None and len(avilable_pci_devices)> 0:
4496 #Add PCI devices one by one
4497 for pci_device in avilable_pci_devices:
4498 task = self.add_pci_to_vm(host_obj, vm_obj, pci_device)
4499 if task:
4500 status= self.wait_for_vcenter_task(task, vcenter_conect)
4501 if status:
4502 self.logger.info("Added PCI device {} to VM {}".format(pci_device,str(vm_obj)))
4503 else:
kateeb044522017-03-06 23:54:39 -08004504 self.logger.error("Fail to add PCI device {} to VM {}".format(pci_device,str(vm_obj)))
bhangarefda5f7c2017-01-12 23:50:34 -08004505 return True, vm_obj, vcenter_conect
4506 else:
4507 self.logger.error("Currently there is no host with"\
4508 " {} number of avaialble PCI devices required for VM {}".format(
4509 no_of_pci_devices,
4510 vmname_andid)
4511 )
4512 raise vimconn.vimconnNotFoundException(
4513 "Currently there is no host with {} "\
4514 "number of avaialble PCI devices required for VM {}".format(
4515 no_of_pci_devices,
4516 vmname_andid))
4517 else:
4518 self.logger.debug("No infromation about PCI devices {} ",pci_devices)
4519
4520 except vmodl.MethodFault as error:
4521 self.logger.error("Error occurred while adding PCI devices {} ",error)
4522 return None, vm_obj, vcenter_conect
4523
4524 def get_vm_obj(self, content, mob_id):
4525 """
4526 Method to get the vsphere VM object associated with a given morf ID
4527 Args:
4528 vapp_uuid - uuid of vApp/VM
4529 content - vCenter content object
4530 mob_id - mob_id of VM
4531
4532 Returns:
4533 VM and host object
4534 """
4535 vm_obj = None
4536 host_obj = None
4537 try :
4538 container = content.viewManager.CreateContainerView(content.rootFolder,
4539 [vim.VirtualMachine], True
4540 )
4541 for vm in container.view:
4542 mobID = vm._GetMoId()
4543 if mobID == mob_id:
4544 vm_obj = vm
4545 host_obj = vm_obj.runtime.host
4546 break
4547 except Exception as exp:
4548 self.logger.error("Error occurred while finding VM object : {}".format(exp))
4549 return host_obj, vm_obj
4550
4551 def get_pci_devices(self, host, need_devices):
4552 """
4553 Method to get the details of pci devices on given host
4554 Args:
4555 host - vSphere host object
4556 need_devices - number of pci devices needed on host
4557
4558 Returns:
4559 array of pci devices
4560 """
4561 all_devices = []
4562 all_device_ids = []
4563 used_devices_ids = []
4564
4565 try:
4566 if host:
4567 pciPassthruInfo = host.config.pciPassthruInfo
4568 pciDevies = host.hardware.pciDevice
4569
4570 for pci_status in pciPassthruInfo:
4571 if pci_status.passthruActive:
4572 for device in pciDevies:
4573 if device.id == pci_status.id:
4574 all_device_ids.append(device.id)
4575 all_devices.append(device)
4576
4577 #check if devices are in use
4578 avalible_devices = all_devices
4579 for vm in host.vm:
4580 if vm.runtime.powerState == vim.VirtualMachinePowerState.poweredOn:
4581 vm_devices = vm.config.hardware.device
4582 for device in vm_devices:
4583 if type(device) is vim.vm.device.VirtualPCIPassthrough:
4584 if device.backing.id in all_device_ids:
4585 for use_device in avalible_devices:
4586 if use_device.id == device.backing.id:
4587 avalible_devices.remove(use_device)
4588 used_devices_ids.append(device.backing.id)
4589 self.logger.debug("Device {} from devices {}"\
4590 "is in use".format(device.backing.id,
4591 device)
4592 )
4593 if len(avalible_devices) < need_devices:
4594 self.logger.debug("Host {} don't have {} number of active devices".format(host,
4595 need_devices))
4596 self.logger.debug("found only {} devives {}".format(len(avalible_devices),
4597 avalible_devices))
4598 return None
4599 else:
4600 required_devices = avalible_devices[:need_devices]
4601 self.logger.info("Found {} PCI devivces on host {} but required only {}".format(
4602 len(avalible_devices),
4603 host,
4604 need_devices))
4605 self.logger.info("Retruning {} devices as {}".format(need_devices,
4606 required_devices ))
4607 return required_devices
4608
4609 except Exception as exp:
4610 self.logger.error("Error {} occurred while finding pci devices on host: {}".format(exp, host))
4611
4612 return None
4613
4614 def get_host_and_PCIdevices(self, content, need_devices):
4615 """
4616 Method to get the details of pci devices infromation on all hosts
4617
4618 Args:
4619 content - vSphere host object
4620 need_devices - number of pci devices needed on host
4621
4622 Returns:
4623 array of pci devices and host object
4624 """
4625 host_obj = None
4626 pci_device_objs = None
4627 try:
4628 if content:
4629 container = content.viewManager.CreateContainerView(content.rootFolder,
4630 [vim.HostSystem], True)
4631 for host in container.view:
4632 devices = self.get_pci_devices(host, need_devices)
4633 if devices:
4634 host_obj = host
4635 pci_device_objs = devices
4636 break
4637 except Exception as exp:
4638 self.logger.error("Error {} occurred while finding pci devices on host: {}".format(exp, host_obj))
4639
4640 return host_obj,pci_device_objs
4641
4642 def relocate_vm(self, dest_host, vm) :
4643 """
4644 Method to get the relocate VM to new host
4645
4646 Args:
4647 dest_host - vSphere host object
4648 vm - vSphere VM object
4649
4650 Returns:
4651 task object
4652 """
4653 task = None
4654 try:
4655 relocate_spec = vim.vm.RelocateSpec(host=dest_host)
4656 task = vm.Relocate(relocate_spec)
4657 self.logger.info("Migrating {} to destination host {}".format(vm, dest_host))
4658 except Exception as exp:
4659 self.logger.error("Error occurred while relocate VM {} to new host {}: {}".format(
4660 dest_host, vm, exp))
4661 return task
4662
4663 def wait_for_vcenter_task(self, task, actionName='job', hideResult=False):
4664 """
4665 Waits and provides updates on a vSphere task
4666 """
4667 while task.info.state == vim.TaskInfo.State.running:
4668 time.sleep(2)
4669
4670 if task.info.state == vim.TaskInfo.State.success:
4671 if task.info.result is not None and not hideResult:
4672 self.logger.info('{} completed successfully, result: {}'.format(
4673 actionName,
4674 task.info.result))
4675 else:
4676 self.logger.info('Task {} completed successfully.'.format(actionName))
4677 else:
4678 self.logger.error('{} did not complete successfully: {} '.format(
4679 actionName,
4680 task.info.error)
4681 )
4682
4683 return task.info.result
4684
4685 def add_pci_to_vm(self,host_object, vm_object, host_pci_dev):
4686 """
4687 Method to add pci device in given VM
4688
4689 Args:
4690 host_object - vSphere host object
4691 vm_object - vSphere VM object
4692 host_pci_dev - host_pci_dev must be one of the devices from the
4693 host_object.hardware.pciDevice list
4694 which is configured as a PCI passthrough device
4695
4696 Returns:
4697 task object
4698 """
4699 task = None
4700 if vm_object and host_object and host_pci_dev:
4701 try :
4702 #Add PCI device to VM
4703 pci_passthroughs = vm_object.environmentBrowser.QueryConfigTarget(host=None).pciPassthrough
4704 systemid_by_pciid = {item.pciDevice.id: item.systemId for item in pci_passthroughs}
4705
4706 if host_pci_dev.id not in systemid_by_pciid:
4707 self.logger.error("Device {} is not a passthrough device ".format(host_pci_dev))
4708 return None
4709
4710 deviceId = hex(host_pci_dev.deviceId % 2**16).lstrip('0x')
4711 backing = vim.VirtualPCIPassthroughDeviceBackingInfo(deviceId=deviceId,
4712 id=host_pci_dev.id,
4713 systemId=systemid_by_pciid[host_pci_dev.id],
4714 vendorId=host_pci_dev.vendorId,
4715 deviceName=host_pci_dev.deviceName)
4716
4717 hba_object = vim.VirtualPCIPassthrough(key=-100, backing=backing)
4718
4719 new_device_config = vim.VirtualDeviceConfigSpec(device=hba_object)
4720 new_device_config.operation = "add"
4721 vmConfigSpec = vim.vm.ConfigSpec()
4722 vmConfigSpec.deviceChange = [new_device_config]
4723
4724 task = vm_object.ReconfigVM_Task(spec=vmConfigSpec)
4725 self.logger.info("Adding PCI device {} into VM {} from host {} ".format(
4726 host_pci_dev, vm_object, host_object)
4727 )
4728 except Exception as exp:
4729 self.logger.error("Error occurred while adding pci devive {} to VM {}: {}".format(
4730 host_pci_dev,
4731 vm_object,
4732 exp))
4733 return task
4734
bhangare06312472017-03-30 05:49:07 -07004735 def get_vm_vcenter_info(self):
bhangarefda5f7c2017-01-12 23:50:34 -08004736 """
kateeb044522017-03-06 23:54:39 -08004737 Method to get details of vCenter and vm
bhangarefda5f7c2017-01-12 23:50:34 -08004738
4739 Args:
4740 vapp_uuid - uuid of vApp or VM
4741
4742 Returns:
4743 Moref Id of VM and deails of vCenter
4744 """
kateeb044522017-03-06 23:54:39 -08004745 vm_vcenter_info = {}
bhangarefda5f7c2017-01-12 23:50:34 -08004746
kateeb044522017-03-06 23:54:39 -08004747 if self.vcenter_ip is not None:
4748 vm_vcenter_info["vm_vcenter_ip"] = self.vcenter_ip
4749 else:
4750 raise vimconn.vimconnException(message="vCenter IP is not provided."\
4751 " Please provide vCenter IP while attaching datacenter to tenant in --config")
4752 if self.vcenter_port is not None:
4753 vm_vcenter_info["vm_vcenter_port"] = self.vcenter_port
4754 else:
4755 raise vimconn.vimconnException(message="vCenter port is not provided."\
4756 " Please provide vCenter port while attaching datacenter to tenant in --config")
4757 if self.vcenter_user is not None:
4758 vm_vcenter_info["vm_vcenter_user"] = self.vcenter_user
4759 else:
4760 raise vimconn.vimconnException(message="vCenter user is not provided."\
4761 " Please provide vCenter user while attaching datacenter to tenant in --config")
bhangarefda5f7c2017-01-12 23:50:34 -08004762
kateeb044522017-03-06 23:54:39 -08004763 if self.vcenter_password is not None:
4764 vm_vcenter_info["vm_vcenter_password"] = self.vcenter_password
4765 else:
4766 raise vimconn.vimconnException(message="vCenter user password is not provided."\
4767 " Please provide vCenter user password while attaching datacenter to tenant in --config")
bhangarefda5f7c2017-01-12 23:50:34 -08004768
bhangare06312472017-03-30 05:49:07 -07004769 return vm_vcenter_info
bhangarefda5f7c2017-01-12 23:50:34 -08004770
4771
4772 def get_vm_pci_details(self, vmuuid):
4773 """
4774 Method to get VM PCI device details from vCenter
4775
4776 Args:
4777 vm_obj - vSphere VM object
4778
4779 Returns:
4780 dict of PCI devives attached to VM
4781
4782 """
4783 vm_pci_devices_info = {}
4784 try:
bhangare06312472017-03-30 05:49:07 -07004785 vcenter_conect, content = self.get_vcenter_content()
4786 vm_moref_id = self.get_vm_moref_id(vmuuid)
4787 if vm_moref_id:
bhangarefda5f7c2017-01-12 23:50:34 -08004788 #Get VM and its host
kateeb044522017-03-06 23:54:39 -08004789 if content:
bhangare06312472017-03-30 05:49:07 -07004790 host_obj, vm_obj = self.get_vm_obj(content, vm_moref_id)
kateeb044522017-03-06 23:54:39 -08004791 if host_obj and vm_obj:
4792 vm_pci_devices_info["host_name"]= host_obj.name
4793 vm_pci_devices_info["host_ip"]= host_obj.config.network.vnic[0].spec.ip.ipAddress
4794 for device in vm_obj.config.hardware.device:
4795 if type(device) == vim.vm.device.VirtualPCIPassthrough:
4796 device_details={'devide_id':device.backing.id,
4797 'pciSlotNumber':device.slotInfo.pciSlotNumber,
4798 }
4799 vm_pci_devices_info[device.deviceInfo.label] = device_details
4800 else:
4801 self.logger.error("Can not connect to vCenter while getting "\
4802 "PCI devices infromationn")
4803 return vm_pci_devices_info
bhangarefda5f7c2017-01-12 23:50:34 -08004804 except Exception as exp:
kateeb044522017-03-06 23:54:39 -08004805 self.logger.error("Error occurred while getting VM infromationn"\
4806 " for VM : {}".format(exp))
4807 raise vimconn.vimconnException(message=exp)
bhangare0e571a92017-01-12 04:02:23 -08004808
Ravi Chamarty5b2d5c12018-10-22 23:59:10 +00004809
Ravi Chamartyb0ab2c02018-10-26 01:40:50 +00004810 def reserve_memory_for_all_vms(self, vapp, memory_mb):
4811 """
4812 Method to reserve memory for all VMs
4813 Args :
4814 vapp - VApp
4815 memory_mb - Memory in MB
4816 Returns:
4817 None
4818 """
4819
4820 self.logger.info("Reserve memory for 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-{}/virtualHardwareSection/memory".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 headers['Content-Type'] = 'application/vnd.vmware.vcloud.rasdItem+xml'
4829 response = self.perform_request(req_type='GET',
4830 url=url_rest_call,
4831 headers=headers)
4832
4833 if response.status_code == 403:
4834 response = self.retry_rest('GET', url_rest_call)
4835
4836 if response.status_code != 200:
4837 self.logger.error("REST call {} failed reason : {}"\
4838 "status code : {}".format(url_rest_call,
4839 response.content,
4840 response.status_code))
4841 raise vimconn.vimconnException("reserve_memory_for_all_vms : Failed to get "\
4842 "memory")
4843
4844 bytexml = bytes(bytearray(response.content, encoding='utf-8'))
4845 contentelem = lxmlElementTree.XML(bytexml)
4846 namespaces = {prefix:uri for prefix,uri in contentelem.nsmap.iteritems() if prefix}
4847 namespaces["xmlns"]= "http://www.vmware.com/vcloud/v1.5"
4848
4849 # Find the reservation element in the response
4850 memelem_list = contentelem.findall(".//rasd:Reservation", namespaces)
4851 for memelem in memelem_list:
4852 memelem.text = str(memory_mb)
4853
4854 newdata = lxmlElementTree.tostring(contentelem, pretty_print=True)
4855
4856 response = self.perform_request(req_type='PUT',
4857 url=url_rest_call,
4858 headers=headers,
4859 data=newdata)
4860
4861 if response.status_code == 403:
4862 add_headers = {'Content-Type': headers['Content-Type']}
4863 response = self.retry_rest('PUT', url_rest_call, add_headers, newdata)
4864
4865 if response.status_code != 202:
4866 self.logger.error("REST call {} failed reason : {}"\
4867 "status code : {} ".format(url_rest_call,
4868 response.content,
4869 response.status_code))
4870 raise vimconn.vimconnException("reserve_memory_for_all_vms : Failed to update "\
4871 "virtual hardware memory section")
4872 else:
4873 mem_task = self.get_task_from_response(response.content)
4874 result = self.client.get_task_monitor().wait_for_success(task=mem_task)
4875 if result.get('status') == 'success':
4876 self.logger.info("reserve_memory_for_all_vms(): VM {} succeeded "\
4877 .format(vm_id))
4878 else:
4879 self.logger.error("reserve_memory_for_all_vms(): VM {} failed "\
4880 .format(vm_id))
4881
4882 def connect_vapp_to_org_vdc_network(self, vapp_id, net_name):
4883 """
4884 Configure VApp network config with org vdc network
4885 Args :
4886 vapp - VApp
4887 Returns:
4888 None
4889 """
4890
4891 self.logger.info("Connecting vapp {} to org vdc network {}".
4892 format(vapp_id, net_name))
4893
4894 url_rest_call = "{}/api/vApp/vapp-{}/networkConfigSection/".format(self.url, vapp_id)
4895
4896 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
4897 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
4898 response = self.perform_request(req_type='GET',
4899 url=url_rest_call,
4900 headers=headers)
4901
4902 if response.status_code == 403:
4903 response = self.retry_rest('GET', url_rest_call)
4904
4905 if response.status_code != 200:
4906 self.logger.error("REST call {} failed reason : {}"\
4907 "status code : {}".format(url_rest_call,
4908 response.content,
4909 response.status_code))
4910 raise vimconn.vimconnException("connect_vapp_to_org_vdc_network : Failed to get "\
4911 "network config section")
4912
4913 data = response.content
4914 headers['Content-Type'] = 'application/vnd.vmware.vcloud.networkConfigSection+xml'
4915 net_id = self.get_network_id_by_name(net_name)
4916 if not net_id:
4917 raise vimconn.vimconnException("connect_vapp_to_org_vdc_network : Failed to find "\
4918 "existing network")
4919
4920 bytexml = bytes(bytearray(data, encoding='utf-8'))
4921 newelem = lxmlElementTree.XML(bytexml)
4922 namespaces = {prefix: uri for prefix, uri in newelem.nsmap.iteritems() if prefix}
4923 namespaces["xmlns"] = "http://www.vmware.com/vcloud/v1.5"
4924 nwcfglist = newelem.findall(".//xmlns:NetworkConfig", namespaces)
4925
Ravi Chamarty259aebc2019-06-05 17:06:39 +00004926 # VCD 9.7 returns an incorrect parentnetwork element. Fix it before PUT operation
4927 parentnetworklist = newelem.findall(".//xmlns:ParentNetwork", namespaces)
4928 if parentnetworklist:
4929 for pn in parentnetworklist:
4930 if "href" not in pn.keys():
4931 id_val = pn.get("id")
4932 href_val = "{}/api/network/{}".format(self.url, id_val)
4933 pn.set("href", href_val)
4934
Ravi Chamartyb0ab2c02018-10-26 01:40:50 +00004935 newstr = """<NetworkConfig networkName="{}">
4936 <Configuration>
4937 <ParentNetwork href="{}/api/network/{}"/>
4938 <FenceMode>bridged</FenceMode>
4939 </Configuration>
4940 </NetworkConfig>
4941 """.format(net_name, self.url, net_id)
4942 newcfgelem = lxmlElementTree.fromstring(newstr)
4943 if nwcfglist:
4944 nwcfglist[0].addnext(newcfgelem)
4945
4946 newdata = lxmlElementTree.tostring(newelem, pretty_print=True)
4947
4948 response = self.perform_request(req_type='PUT',
4949 url=url_rest_call,
4950 headers=headers,
4951 data=newdata)
4952
4953 if response.status_code == 403:
4954 add_headers = {'Content-Type': headers['Content-Type']}
4955 response = self.retry_rest('PUT', url_rest_call, add_headers, newdata)
4956
4957 if response.status_code != 202:
4958 self.logger.error("REST call {} failed reason : {}"\
4959 "status code : {} ".format(url_rest_call,
4960 response.content,
4961 response.status_code))
4962 raise vimconn.vimconnException("connect_vapp_to_org_vdc_network : Failed to update "\
4963 "network config section")
4964 else:
4965 vapp_task = self.get_task_from_response(response.content)
4966 result = self.client.get_task_monitor().wait_for_success(task=vapp_task)
4967 if result.get('status') == 'success':
4968 self.logger.info("connect_vapp_to_org_vdc_network(): Vapp {} connected to "\
4969 "network {}".format(vapp_id, net_name))
4970 else:
4971 self.logger.error("connect_vapp_to_org_vdc_network(): Vapp {} failed to "\
4972 "connect to network {}".format(vapp_id, net_name))
4973
Ravi Chamarty5b2d5c12018-10-22 23:59:10 +00004974 def remove_primary_network_adapter_from_all_vms(self, vapp):
4975 """
4976 Method to remove network adapter type to vm
4977 Args :
4978 vapp - VApp
4979 Returns:
4980 None
4981 """
4982
4983 self.logger.info("Removing network adapter from all VMs")
4984 for vms in vapp.get_all_vms():
4985 vm_id = vms.get('id').split(':')[-1]
4986
4987 url_rest_call = "{}/api/vApp/vm-{}/networkConnectionSection/".format(self.url, vm_id)
4988
4989 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
4990 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
4991 response = self.perform_request(req_type='GET',
4992 url=url_rest_call,
4993 headers=headers)
4994
4995 if response.status_code == 403:
4996 response = self.retry_rest('GET', url_rest_call)
4997
4998 if response.status_code != 200:
4999 self.logger.error("REST call {} failed reason : {}"\
5000 "status code : {}".format(url_rest_call,
5001 response.content,
5002 response.status_code))
5003 raise vimconn.vimconnException("remove_primary_network_adapter : Failed to get "\
5004 "network connection section")
5005
5006 data = response.content
5007 data = data.split('<Link rel="edit"')[0]
5008
5009 headers['Content-Type'] = 'application/vnd.vmware.vcloud.networkConnectionSection+xml'
5010
5011 newdata = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
5012 <NetworkConnectionSection xmlns="http://www.vmware.com/vcloud/v1.5"
5013 xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1"
5014 xmlns:vssd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData"
5015 xmlns:common="http://schemas.dmtf.org/wbem/wscim/1/common"
5016 xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData"
5017 xmlns:vmw="http://www.vmware.com/schema/ovf"
5018 xmlns:ovfenv="http://schemas.dmtf.org/ovf/environment/1"
5019 xmlns:vmext="http://www.vmware.com/vcloud/extension/v1.5"
5020 xmlns:ns9="http://www.vmware.com/vcloud/versions"
5021 href="{url}" type="application/vnd.vmware.vcloud.networkConnectionSection+xml" ovf:required="false">
5022 <ovf:Info>Specifies the available VM network connections</ovf:Info>
5023 <PrimaryNetworkConnectionIndex>0</PrimaryNetworkConnectionIndex>
5024 <Link rel="edit" href="{url}" type="application/vnd.vmware.vcloud.networkConnectionSection+xml"/>
5025 </NetworkConnectionSection>""".format(url=url_rest_call)
5026 response = self.perform_request(req_type='PUT',
5027 url=url_rest_call,
5028 headers=headers,
5029 data=newdata)
5030
5031 if response.status_code == 403:
5032 add_headers = {'Content-Type': headers['Content-Type']}
5033 response = self.retry_rest('PUT', url_rest_call, add_headers, newdata)
5034
5035 if response.status_code != 202:
5036 self.logger.error("REST call {} failed reason : {}"\
5037 "status code : {} ".format(url_rest_call,
5038 response.content,
5039 response.status_code))
5040 raise vimconn.vimconnException("remove_primary_network_adapter : Failed to update "\
5041 "network connection section")
5042 else:
5043 nic_task = self.get_task_from_response(response.content)
5044 result = self.client.get_task_monitor().wait_for_success(task=nic_task)
5045 if result.get('status') == 'success':
5046 self.logger.info("remove_primary_network_adapter(): VM {} conneced to "\
5047 "default NIC type".format(vm_id))
5048 else:
5049 self.logger.error("remove_primary_network_adapter(): VM {} failed to "\
5050 "connect NIC type".format(vm_id))
5051
kasardc1f02e2017-03-25 07:20:30 -07005052 def add_network_adapter_to_vms(self, vapp, network_name, primary_nic_index, nicIndex, net, nic_type=None):
kasar3ac5dc42017-03-15 06:28:22 -07005053 """
5054 Method to add network adapter type to vm
5055 Args :
5056 network_name - name of network
5057 primary_nic_index - int value for primary nic index
5058 nicIndex - int value for nic index
5059 nic_type - specify model name to which add to vm
5060 Returns:
5061 None
5062 """
kasar3ac5dc42017-03-15 06:28:22 -07005063
Ravi Chamarty5b2d5c12018-10-22 23:59:10 +00005064 self.logger.info("Add network adapter to VM: network_name {} nicIndex {} nic_type {}".\
5065 format(network_name, nicIndex, nic_type))
kasar4cb6e902017-03-18 00:17:27 -07005066 try:
kasard2963622017-03-31 05:53:17 -07005067 ip_address = None
kasardc1f02e2017-03-25 07:20:30 -07005068 floating_ip = False
kasarc5bf2932018-03-09 04:15:22 -08005069 mac_address = None
kasardc1f02e2017-03-25 07:20:30 -07005070 if 'floating_ip' in net: floating_ip = net['floating_ip']
kasard2963622017-03-31 05:53:17 -07005071
5072 # Stub for ip_address feature
5073 if 'ip_address' in net: ip_address = net['ip_address']
5074
kasarc5bf2932018-03-09 04:15:22 -08005075 if 'mac_address' in net: mac_address = net['mac_address']
5076
kasard2963622017-03-31 05:53:17 -07005077 if floating_ip:
5078 allocation_mode = "POOL"
5079 elif ip_address:
5080 allocation_mode = "MANUAL"
5081 else:
5082 allocation_mode = "DHCP"
kasardc1f02e2017-03-25 07:20:30 -07005083
kasar3ac5dc42017-03-15 06:28:22 -07005084 if not nic_type:
kasarc5bf2932018-03-09 04:15:22 -08005085 for vms in vapp.get_all_vms():
5086 vm_id = vms.get('id').split(':')[-1]
kasar3ac5dc42017-03-15 06:28:22 -07005087
kasarc5bf2932018-03-09 04:15:22 -08005088 url_rest_call = "{}/api/vApp/vm-{}/networkConnectionSection/".format(self.url, vm_id)
kasar3ac5dc42017-03-15 06:28:22 -07005089
kasarc5bf2932018-03-09 04:15:22 -08005090 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
sbhangarea8e5b782018-06-21 02:10:03 -07005091 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
kasarc5bf2932018-03-09 04:15:22 -08005092 response = self.perform_request(req_type='GET',
5093 url=url_rest_call,
5094 headers=headers)
bhangare1a0b97c2017-06-21 02:20:15 -07005095
5096 if response.status_code == 403:
5097 response = self.retry_rest('GET', url_rest_call)
5098
kasar3ac5dc42017-03-15 06:28:22 -07005099 if response.status_code != 200:
kasar4cb6e902017-03-18 00:17:27 -07005100 self.logger.error("REST call {} failed reason : {}"\
5101 "status code : {}".format(url_rest_call,
5102 response.content,
5103 response.status_code))
5104 raise vimconn.vimconnException("add_network_adapter_to_vms : Failed to get "\
5105 "network connection section")
kasar3ac5dc42017-03-15 06:28:22 -07005106
5107 data = response.content
kasarc5bf2932018-03-09 04:15:22 -08005108 data = data.split('<Link rel="edit"')[0]
kasar3ac5dc42017-03-15 06:28:22 -07005109 if '<PrimaryNetworkConnectionIndex>' not in data:
Ravi Chamarty5b2d5c12018-10-22 23:59:10 +00005110 self.logger.debug("add_network_adapter PrimaryNIC not in data")
kasar3ac5dc42017-03-15 06:28:22 -07005111 item = """<PrimaryNetworkConnectionIndex>{}</PrimaryNetworkConnectionIndex>
5112 <NetworkConnection network="{}">
5113 <NetworkConnectionIndex>{}</NetworkConnectionIndex>
5114 <IsConnected>true</IsConnected>
kasardc1f02e2017-03-25 07:20:30 -07005115 <IpAddressAllocationMode>{}</IpAddressAllocationMode>
5116 </NetworkConnection>""".format(primary_nic_index, network_name, nicIndex,
5117 allocation_mode)
kasard2963622017-03-31 05:53:17 -07005118 # Stub for ip_address feature
5119 if ip_address:
5120 ip_tag = '<IpAddress>{}</IpAddress>'.format(ip_address)
5121 item = item.replace('</NetworkConnectionIndex>\n','</NetworkConnectionIndex>\n{}\n'.format(ip_tag))
kasardc1f02e2017-03-25 07:20:30 -07005122
kasarc5bf2932018-03-09 04:15:22 -08005123 if mac_address:
5124 mac_tag = '<MACAddress>{}</MACAddress>'.format(mac_address)
5125 item = item.replace('</IsConnected>\n','</IsConnected>\n{}\n'.format(mac_tag))
5126
5127 data = data.replace('</ovf:Info>\n','</ovf:Info>\n{}\n</NetworkConnectionSection>'.format(item))
kasar3ac5dc42017-03-15 06:28:22 -07005128 else:
Ravi Chamarty5b2d5c12018-10-22 23:59:10 +00005129 self.logger.debug("add_network_adapter PrimaryNIC in data")
kasar3ac5dc42017-03-15 06:28:22 -07005130 new_item = """<NetworkConnection network="{}">
5131 <NetworkConnectionIndex>{}</NetworkConnectionIndex>
5132 <IsConnected>true</IsConnected>
kasardc1f02e2017-03-25 07:20:30 -07005133 <IpAddressAllocationMode>{}</IpAddressAllocationMode>
5134 </NetworkConnection>""".format(network_name, nicIndex,
5135 allocation_mode)
kasard2963622017-03-31 05:53:17 -07005136 # Stub for ip_address feature
5137 if ip_address:
5138 ip_tag = '<IpAddress>{}</IpAddress>'.format(ip_address)
5139 new_item = new_item.replace('</NetworkConnectionIndex>\n','</NetworkConnectionIndex>\n{}\n'.format(ip_tag))
kasardc1f02e2017-03-25 07:20:30 -07005140
kasarc5bf2932018-03-09 04:15:22 -08005141 if mac_address:
5142 mac_tag = '<MACAddress>{}</MACAddress>'.format(mac_address)
5143 new_item = new_item.replace('</IsConnected>\n','</IsConnected>\n{}\n'.format(mac_tag))
kasar3ac5dc42017-03-15 06:28:22 -07005144
kasarc5bf2932018-03-09 04:15:22 -08005145 data = data + new_item + '</NetworkConnectionSection>'
5146
kasar3ac5dc42017-03-15 06:28:22 -07005147 headers['Content-Type'] = 'application/vnd.vmware.vcloud.networkConnectionSection+xml'
kasarc5bf2932018-03-09 04:15:22 -08005148
5149 response = self.perform_request(req_type='PUT',
5150 url=url_rest_call,
5151 headers=headers,
5152 data=data)
bhangare1a0b97c2017-06-21 02:20:15 -07005153
5154 if response.status_code == 403:
5155 add_headers = {'Content-Type': headers['Content-Type']}
5156 response = self.retry_rest('PUT', url_rest_call, add_headers, data)
5157
kasar3ac5dc42017-03-15 06:28:22 -07005158 if response.status_code != 202:
kasar4cb6e902017-03-18 00:17:27 -07005159 self.logger.error("REST call {} failed reason : {}"\
5160 "status code : {} ".format(url_rest_call,
5161 response.content,
5162 response.status_code))
5163 raise vimconn.vimconnException("add_network_adapter_to_vms : Failed to update "\
5164 "network connection section")
kasar3ac5dc42017-03-15 06:28:22 -07005165 else:
kasarc5bf2932018-03-09 04:15:22 -08005166 nic_task = self.get_task_from_response(response.content)
sbhangarea8e5b782018-06-21 02:10:03 -07005167 result = self.client.get_task_monitor().wait_for_success(task=nic_task)
kasarc5bf2932018-03-09 04:15:22 -08005168 if result.get('status') == 'success':
kasar3ac5dc42017-03-15 06:28:22 -07005169 self.logger.info("add_network_adapter_to_vms(): VM {} conneced to "\
5170 "default NIC type".format(vm_id))
5171 else:
5172 self.logger.error("add_network_adapter_to_vms(): VM {} failed to "\
5173 "connect NIC type".format(vm_id))
5174 else:
kasarc5bf2932018-03-09 04:15:22 -08005175 for vms in vapp.get_all_vms():
5176 vm_id = vms.get('id').split(':')[-1]
kasar3ac5dc42017-03-15 06:28:22 -07005177
kasarc5bf2932018-03-09 04:15:22 -08005178 url_rest_call = "{}/api/vApp/vm-{}/networkConnectionSection/".format(self.url, vm_id)
5179
5180 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
5181 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
5182 response = self.perform_request(req_type='GET',
5183 url=url_rest_call,
5184 headers=headers)
bhangare1a0b97c2017-06-21 02:20:15 -07005185
5186 if response.status_code == 403:
5187 response = self.retry_rest('GET', url_rest_call)
5188
kasar3ac5dc42017-03-15 06:28:22 -07005189 if response.status_code != 200:
kasar4cb6e902017-03-18 00:17:27 -07005190 self.logger.error("REST call {} failed reason : {}"\
5191 "status code : {}".format(url_rest_call,
5192 response.content,
5193 response.status_code))
5194 raise vimconn.vimconnException("add_network_adapter_to_vms : Failed to get "\
5195 "network connection section")
kasar3ac5dc42017-03-15 06:28:22 -07005196 data = response.content
kasarc5bf2932018-03-09 04:15:22 -08005197 data = data.split('<Link rel="edit"')[0]
Ravi Chamartye21c9cc2018-10-24 02:14:53 +00005198 vcd_netadapter_type = nic_type
5199 if nic_type in ['SR-IOV', 'VF']:
5200 vcd_netadapter_type = "SRIOVETHERNETCARD"
5201
kasar3ac5dc42017-03-15 06:28:22 -07005202 if '<PrimaryNetworkConnectionIndex>' not in data:
Ravi Chamarty5b2d5c12018-10-22 23:59:10 +00005203 self.logger.debug("add_network_adapter PrimaryNIC not in data nic_type {}".format(nic_type))
kasar3ac5dc42017-03-15 06:28:22 -07005204 item = """<PrimaryNetworkConnectionIndex>{}</PrimaryNetworkConnectionIndex>
5205 <NetworkConnection network="{}">
5206 <NetworkConnectionIndex>{}</NetworkConnectionIndex>
5207 <IsConnected>true</IsConnected>
kasardc1f02e2017-03-25 07:20:30 -07005208 <IpAddressAllocationMode>{}</IpAddressAllocationMode>
kasar3ac5dc42017-03-15 06:28:22 -07005209 <NetworkAdapterType>{}</NetworkAdapterType>
kasardc1f02e2017-03-25 07:20:30 -07005210 </NetworkConnection>""".format(primary_nic_index, network_name, nicIndex,
Ravi Chamartye21c9cc2018-10-24 02:14:53 +00005211 allocation_mode, vcd_netadapter_type)
kasard2963622017-03-31 05:53:17 -07005212 # Stub for ip_address feature
5213 if ip_address:
5214 ip_tag = '<IpAddress>{}</IpAddress>'.format(ip_address)
5215 item = item.replace('</NetworkConnectionIndex>\n','</NetworkConnectionIndex>\n{}\n'.format(ip_tag))
kasardc1f02e2017-03-25 07:20:30 -07005216
kasarc5bf2932018-03-09 04:15:22 -08005217 if mac_address:
5218 mac_tag = '<MACAddress>{}</MACAddress>'.format(mac_address)
sbhangarea8e5b782018-06-21 02:10:03 -07005219 item = item.replace('</IsConnected>\n','</IsConnected>\n{}\n'.format(mac_tag))
kasarc5bf2932018-03-09 04:15:22 -08005220
5221 data = data.replace('</ovf:Info>\n','</ovf:Info>\n{}\n</NetworkConnectionSection>'.format(item))
kasar3ac5dc42017-03-15 06:28:22 -07005222 else:
Ravi Chamarty5b2d5c12018-10-22 23:59:10 +00005223 self.logger.debug("add_network_adapter PrimaryNIC in data nic_type {}".format(nic_type))
kasar3ac5dc42017-03-15 06:28:22 -07005224 new_item = """<NetworkConnection network="{}">
5225 <NetworkConnectionIndex>{}</NetworkConnectionIndex>
5226 <IsConnected>true</IsConnected>
kasardc1f02e2017-03-25 07:20:30 -07005227 <IpAddressAllocationMode>{}</IpAddressAllocationMode>
kasar3ac5dc42017-03-15 06:28:22 -07005228 <NetworkAdapterType>{}</NetworkAdapterType>
kasardc1f02e2017-03-25 07:20:30 -07005229 </NetworkConnection>""".format(network_name, nicIndex,
Ravi Chamartye21c9cc2018-10-24 02:14:53 +00005230 allocation_mode, vcd_netadapter_type)
kasard2963622017-03-31 05:53:17 -07005231 # Stub for ip_address feature
5232 if ip_address:
5233 ip_tag = '<IpAddress>{}</IpAddress>'.format(ip_address)
5234 new_item = new_item.replace('</NetworkConnectionIndex>\n','</NetworkConnectionIndex>\n{}\n'.format(ip_tag))
kasardc1f02e2017-03-25 07:20:30 -07005235
kasarc5bf2932018-03-09 04:15:22 -08005236 if mac_address:
5237 mac_tag = '<MACAddress>{}</MACAddress>'.format(mac_address)
5238 new_item = new_item.replace('</IsConnected>\n','</IsConnected>\n{}\n'.format(mac_tag))
kasar3ac5dc42017-03-15 06:28:22 -07005239
kasarc5bf2932018-03-09 04:15:22 -08005240 data = data + new_item + '</NetworkConnectionSection>'
5241
kasar3ac5dc42017-03-15 06:28:22 -07005242 headers['Content-Type'] = 'application/vnd.vmware.vcloud.networkConnectionSection+xml'
kasarc5bf2932018-03-09 04:15:22 -08005243
5244 response = self.perform_request(req_type='PUT',
5245 url=url_rest_call,
5246 headers=headers,
5247 data=data)
bhangare1a0b97c2017-06-21 02:20:15 -07005248
5249 if response.status_code == 403:
5250 add_headers = {'Content-Type': headers['Content-Type']}
5251 response = self.retry_rest('PUT', url_rest_call, add_headers, data)
kasar3ac5dc42017-03-15 06:28:22 -07005252
5253 if response.status_code != 202:
kasar4cb6e902017-03-18 00:17:27 -07005254 self.logger.error("REST call {} failed reason : {}"\
5255 "status code : {}".format(url_rest_call,
5256 response.content,
5257 response.status_code))
5258 raise vimconn.vimconnException("add_network_adapter_to_vms : Failed to update "\
5259 "network connection section")
kasar3ac5dc42017-03-15 06:28:22 -07005260 else:
kasarc5bf2932018-03-09 04:15:22 -08005261 nic_task = self.get_task_from_response(response.content)
5262 result = self.client.get_task_monitor().wait_for_success(task=nic_task)
5263 if result.get('status') == 'success':
kasar3ac5dc42017-03-15 06:28:22 -07005264 self.logger.info("add_network_adapter_to_vms(): VM {} "\
5265 "conneced to NIC type {}".format(vm_id, nic_type))
5266 else:
5267 self.logger.error("add_network_adapter_to_vms(): VM {} "\
5268 "failed to connect NIC type {}".format(vm_id, nic_type))
5269 except Exception as exp:
kasar4cb6e902017-03-18 00:17:27 -07005270 self.logger.error("add_network_adapter_to_vms() : exception occurred "\
5271 "while adding Network adapter")
5272 raise vimconn.vimconnException(message=exp)
kasarde691232017-03-25 03:37:31 -07005273
5274
5275 def set_numa_affinity(self, vmuuid, paired_threads_id):
5276 """
5277 Method to assign numa affinity in vm configuration parammeters
5278 Args :
5279 vmuuid - vm uuid
5280 paired_threads_id - one or more virtual processor
5281 numbers
5282 Returns:
5283 return if True
5284 """
5285 try:
kasar204e39e2018-01-25 00:57:02 -08005286 vcenter_conect, content = self.get_vcenter_content()
5287 vm_moref_id = self.get_vm_moref_id(vmuuid)
kasarde691232017-03-25 03:37:31 -07005288
kasar204e39e2018-01-25 00:57:02 -08005289 host_obj, vm_obj = self.get_vm_obj(content ,vm_moref_id)
5290 if vm_obj:
5291 config_spec = vim.vm.ConfigSpec()
5292 config_spec.extraConfig = []
5293 opt = vim.option.OptionValue()
5294 opt.key = 'numa.nodeAffinity'
5295 opt.value = str(paired_threads_id)
5296 config_spec.extraConfig.append(opt)
5297 task = vm_obj.ReconfigVM_Task(config_spec)
5298 if task:
5299 result = self.wait_for_vcenter_task(task, vcenter_conect)
5300 extra_config = vm_obj.config.extraConfig
5301 flag = False
5302 for opts in extra_config:
5303 if 'numa.nodeAffinity' in opts.key:
5304 flag = True
5305 self.logger.info("set_numa_affinity: Sucessfully assign numa affinity "\
5306 "value {} for vm {}".format(opt.value, vm_obj))
5307 if flag:
5308 return
5309 else:
5310 self.logger.error("set_numa_affinity: Failed to assign numa affinity")
kasarde691232017-03-25 03:37:31 -07005311 except Exception as exp:
5312 self.logger.error("set_numa_affinity : exception occurred while setting numa affinity "\
5313 "for VM {} : {}".format(vm_obj, vm_moref_id))
5314 raise vimconn.vimconnException("set_numa_affinity : Error {} failed to assign numa "\
5315 "affinity".format(exp))
kasardc1f02e2017-03-25 07:20:30 -07005316
5317
5318 def cloud_init(self, vapp, cloud_config):
5319 """
5320 Method to inject ssh-key
5321 vapp - vapp object
5322 cloud_config a dictionary with:
5323 'key-pairs': (optional) list of strings with the public key to be inserted to the default user
5324 'users': (optional) list of users to be inserted, each item is a dict with:
5325 'name': (mandatory) user name,
5326 'key-pairs': (optional) list of strings with the public key to be inserted to the user
tierno40e1bce2017-08-09 09:12:04 +02005327 'user-data': (optional) can be a string with the text script to be passed directly to cloud-init,
5328 or a list of strings, each one contains a script to be passed, usually with a MIMEmultipart file
kasardc1f02e2017-03-25 07:20:30 -07005329 'config-files': (optional). List of files to be transferred. Each item is a dict with:
5330 'dest': (mandatory) string with the destination absolute path
5331 'encoding': (optional, by default text). Can be one of:
5332 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64'
5333 'content' (mandatory): string with the content of the file
5334 'permissions': (optional) string with file permissions, typically octal notation '0644'
5335 'owner': (optional) file owner, string with the format 'owner:group'
5336 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk
5337 """
kasardc1f02e2017-03-25 07:20:30 -07005338 try:
kasar2aa50742017-08-08 02:11:22 -07005339 if not isinstance(cloud_config, dict):
5340 raise Exception("cloud_init : parameter cloud_config is not a dictionary")
5341 else:
kasardc1f02e2017-03-25 07:20:30 -07005342 key_pairs = []
5343 userdata = []
5344 if "key-pairs" in cloud_config:
5345 key_pairs = cloud_config["key-pairs"]
5346
5347 if "users" in cloud_config:
5348 userdata = cloud_config["users"]
5349
kasar2aa50742017-08-08 02:11:22 -07005350 self.logger.debug("cloud_init : Guest os customization started..")
5351 customize_script = self.format_script(key_pairs=key_pairs, users_list=userdata)
kasarc5bf2932018-03-09 04:15:22 -08005352 customize_script = customize_script.replace("&","&amp;")
kasar2aa50742017-08-08 02:11:22 -07005353 self.guest_customization(vapp, customize_script)
kasardc1f02e2017-03-25 07:20:30 -07005354
kasardc1f02e2017-03-25 07:20:30 -07005355 except Exception as exp:
5356 self.logger.error("cloud_init : exception occurred while injecting "\
5357 "ssh-key")
5358 raise vimconn.vimconnException("cloud_init : Error {} failed to inject "\
5359 "ssh-key".format(exp))
bhangare06312472017-03-30 05:49:07 -07005360
kasar2aa50742017-08-08 02:11:22 -07005361 def format_script(self, key_pairs=[], users_list=[]):
kasarc5bf2932018-03-09 04:15:22 -08005362 bash_script = """#!/bin/sh
kasar2aa50742017-08-08 02:11:22 -07005363 echo performing customization tasks with param $1 at `date "+DATE: %Y-%m-%d - TIME: %H:%M:%S"` >> /root/customization.log
5364 if [ "$1" = "precustomization" ];then
5365 echo performing precustomization tasks on `date "+DATE: %Y-%m-%d - TIME: %H:%M:%S"` >> /root/customization.log
5366 """
5367
5368 keys = "\n".join(key_pairs)
5369 if keys:
5370 keys_data = """
5371 if [ ! -d /root/.ssh ];then
5372 mkdir /root/.ssh
5373 chown root:root /root/.ssh
5374 chmod 700 /root/.ssh
5375 touch /root/.ssh/authorized_keys
5376 chown root:root /root/.ssh/authorized_keys
5377 chmod 600 /root/.ssh/authorized_keys
5378 # make centos with selinux happy
5379 which restorecon && restorecon -Rv /root/.ssh
5380 else
5381 touch /root/.ssh/authorized_keys
5382 chown root:root /root/.ssh/authorized_keys
5383 chmod 600 /root/.ssh/authorized_keys
5384 fi
5385 echo '{key}' >> /root/.ssh/authorized_keys
5386 """.format(key=keys)
5387
5388 bash_script+= keys_data
5389
5390 for user in users_list:
5391 if 'name' in user: user_name = user['name']
5392 if 'key-pairs' in user:
5393 user_keys = "\n".join(user['key-pairs'])
5394 else:
5395 user_keys = None
5396
5397 add_user_name = """
5398 useradd -d /home/{user_name} -m -g users -s /bin/bash {user_name}
5399 """.format(user_name=user_name)
5400
5401 bash_script+= add_user_name
5402
5403 if user_keys:
5404 user_keys_data = """
5405 mkdir /home/{user_name}/.ssh
5406 chown {user_name}:{user_name} /home/{user_name}/.ssh
5407 chmod 700 /home/{user_name}/.ssh
5408 touch /home/{user_name}/.ssh/authorized_keys
5409 chown {user_name}:{user_name} /home/{user_name}/.ssh/authorized_keys
5410 chmod 600 /home/{user_name}/.ssh/authorized_keys
5411 # make centos with selinux happy
5412 which restorecon && restorecon -Rv /home/{user_name}/.ssh
5413 echo '{user_key}' >> /home/{user_name}/.ssh/authorized_keys
5414 """.format(user_name=user_name,user_key=user_keys)
5415
5416 bash_script+= user_keys_data
5417
5418 return bash_script+"\n\tfi"
5419
5420 def guest_customization(self, vapp, customize_script):
5421 """
5422 Method to customize guest os
5423 vapp - Vapp object
5424 customize_script - Customize script to be run at first boot of VM.
5425 """
kasarc5bf2932018-03-09 04:15:22 -08005426 for vm in vapp.get_all_vms():
5427 vm_id = vm.get('id').split(':')[-1]
sbhangarea8e5b782018-06-21 02:10:03 -07005428 vm_name = vm.get('name')
5429 vm_name = vm_name.replace('_','-')
5430
kasarc5bf2932018-03-09 04:15:22 -08005431 vm_customization_url = "{}/api/vApp/vm-{}/guestCustomizationSection/".format(self.url, vm_id)
5432 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
5433 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
5434
5435 headers['Content-Type'] = "application/vnd.vmware.vcloud.guestCustomizationSection+xml"
5436
5437 data = """<GuestCustomizationSection
5438 xmlns="http://www.vmware.com/vcloud/v1.5"
5439 xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1"
5440 ovf:required="false" href="{}" type="application/vnd.vmware.vcloud.guestCustomizationSection+xml">
5441 <ovf:Info>Specifies Guest OS Customization Settings</ovf:Info>
5442 <Enabled>true</Enabled>
5443 <ChangeSid>false</ChangeSid>
5444 <VirtualMachineId>{}</VirtualMachineId>
5445 <JoinDomainEnabled>false</JoinDomainEnabled>
5446 <UseOrgSettings>false</UseOrgSettings>
5447 <AdminPasswordEnabled>false</AdminPasswordEnabled>
5448 <AdminPasswordAuto>true</AdminPasswordAuto>
5449 <AdminAutoLogonEnabled>false</AdminAutoLogonEnabled>
5450 <AdminAutoLogonCount>0</AdminAutoLogonCount>
5451 <ResetPasswordRequired>false</ResetPasswordRequired>
5452 <CustomizationScript>{}</CustomizationScript>
5453 <ComputerName>{}</ComputerName>
5454 <Link href="{}" type="application/vnd.vmware.vcloud.guestCustomizationSection+xml" rel="edit"/>
sbhangarea8e5b782018-06-21 02:10:03 -07005455 </GuestCustomizationSection>
kasarc5bf2932018-03-09 04:15:22 -08005456 """.format(vm_customization_url,
5457 vm_id,
5458 customize_script,
5459 vm_name,
sbhangarea8e5b782018-06-21 02:10:03 -07005460 vm_customization_url)
kasarc5bf2932018-03-09 04:15:22 -08005461
5462 response = self.perform_request(req_type='PUT',
5463 url=vm_customization_url,
5464 headers=headers,
5465 data=data)
5466 if response.status_code == 202:
5467 guest_task = self.get_task_from_response(response.content)
5468 self.client.get_task_monitor().wait_for_success(task=guest_task)
kasar2aa50742017-08-08 02:11:22 -07005469 self.logger.info("guest_customization : customized guest os task "\
5470 "completed for VM {}".format(vm_name))
5471 else:
5472 self.logger.error("guest_customization : task for customized guest os"\
5473 "failed for VM {}".format(vm_name))
5474 raise vimconn.vimconnException("guest_customization : failed to perform"\
5475 "guest os customization on VM {}".format(vm_name))
bhangare06312472017-03-30 05:49:07 -07005476
bhangare1a0b97c2017-06-21 02:20:15 -07005477 def add_new_disk(self, vapp_uuid, disk_size):
bhangare06312472017-03-30 05:49:07 -07005478 """
5479 Method to create an empty vm disk
5480
5481 Args:
5482 vapp_uuid - is vapp identifier.
5483 disk_size - size of disk to be created in GB
5484
5485 Returns:
5486 None
5487 """
5488 status = False
5489 vm_details = None
5490 try:
5491 #Disk size in GB, convert it into MB
5492 if disk_size is not None:
5493 disk_size_mb = int(disk_size) * 1024
5494 vm_details = self.get_vapp_details_rest(vapp_uuid)
5495
5496 if vm_details and "vm_virtual_hardware" in vm_details:
5497 self.logger.info("Adding disk to VM: {} disk size:{}GB".format(vm_details["name"], disk_size))
5498 disk_href = vm_details["vm_virtual_hardware"]["disk_edit_href"]
bhangare1a0b97c2017-06-21 02:20:15 -07005499 status = self.add_new_disk_rest(disk_href, disk_size_mb)
bhangare06312472017-03-30 05:49:07 -07005500
5501 except Exception as exp:
5502 msg = "Error occurred while creating new disk {}.".format(exp)
5503 self.rollback_newvm(vapp_uuid, msg)
5504
5505 if status:
5506 self.logger.info("Added new disk to VM: {} disk size:{}GB".format(vm_details["name"], disk_size))
5507 else:
5508 #If failed to add disk, delete VM
5509 msg = "add_new_disk: Failed to add new disk to {}".format(vm_details["name"])
5510 self.rollback_newvm(vapp_uuid, msg)
5511
5512
bhangare1a0b97c2017-06-21 02:20:15 -07005513 def add_new_disk_rest(self, disk_href, disk_size_mb):
bhangare06312472017-03-30 05:49:07 -07005514 """
5515 Retrives vApp Disks section & add new empty disk
5516
5517 Args:
5518 disk_href: Disk section href to addd disk
5519 disk_size_mb: Disk size in MB
5520
5521 Returns: Status of add new disk task
5522 """
5523 status = False
kasarc5bf2932018-03-09 04:15:22 -08005524 if self.client._session:
5525 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
sbhangarea8e5b782018-06-21 02:10:03 -07005526 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
kasarc5bf2932018-03-09 04:15:22 -08005527 response = self.perform_request(req_type='GET',
5528 url=disk_href,
5529 headers=headers)
bhangare1a0b97c2017-06-21 02:20:15 -07005530
5531 if response.status_code == 403:
5532 response = self.retry_rest('GET', disk_href)
bhangare06312472017-03-30 05:49:07 -07005533
5534 if response.status_code != requests.codes.ok:
5535 self.logger.error("add_new_disk_rest: GET REST API call {} failed. Return status code {}"
5536 .format(disk_href, response.status_code))
5537 return status
5538 try:
5539 #Find but type & max of instance IDs assigned to disks
5540 lxmlroot_respond = lxmlElementTree.fromstring(response.content)
5541 namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.iteritems() if prefix}
kasarc5bf2932018-03-09 04:15:22 -08005542 #For python3
5543 #namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.items() if prefix}
bhangare06312472017-03-30 05:49:07 -07005544 namespaces["xmlns"]= "http://www.vmware.com/vcloud/v1.5"
5545 instance_id = 0
5546 for item in lxmlroot_respond.iterfind('xmlns:Item',namespaces):
5547 if item.find("rasd:Description",namespaces).text == "Hard disk":
5548 inst_id = int(item.find("rasd:InstanceID" ,namespaces).text)
5549 if inst_id > instance_id:
5550 instance_id = inst_id
5551 disk_item = item.find("rasd:HostResource" ,namespaces)
5552 bus_subtype = disk_item.attrib["{"+namespaces['xmlns']+"}busSubType"]
5553 bus_type = disk_item.attrib["{"+namespaces['xmlns']+"}busType"]
5554
5555 instance_id = instance_id + 1
5556 new_item = """<Item>
5557 <rasd:Description>Hard disk</rasd:Description>
5558 <rasd:ElementName>New disk</rasd:ElementName>
5559 <rasd:HostResource
5560 xmlns:vcloud="http://www.vmware.com/vcloud/v1.5"
5561 vcloud:capacity="{}"
5562 vcloud:busSubType="{}"
5563 vcloud:busType="{}"></rasd:HostResource>
5564 <rasd:InstanceID>{}</rasd:InstanceID>
5565 <rasd:ResourceType>17</rasd:ResourceType>
5566 </Item>""".format(disk_size_mb, bus_subtype, bus_type, instance_id)
5567
5568 new_data = response.content
5569 #Add new item at the bottom
5570 new_data = new_data.replace('</Item>\n</RasdItemsList>', '</Item>\n{}\n</RasdItemsList>'.format(new_item))
5571
5572 # Send PUT request to modify virtual hardware section with new disk
bhangare06312472017-03-30 05:49:07 -07005573 headers['Content-Type'] = 'application/vnd.vmware.vcloud.rasdItemsList+xml; charset=ISO-8859-1'
5574
kasarc5bf2932018-03-09 04:15:22 -08005575 response = self.perform_request(req_type='PUT',
5576 url=disk_href,
5577 data=new_data,
5578 headers=headers)
bhangare1a0b97c2017-06-21 02:20:15 -07005579
5580 if response.status_code == 403:
5581 add_headers = {'Content-Type': headers['Content-Type']}
5582 response = self.retry_rest('PUT', disk_href, add_headers, new_data)
bhangare06312472017-03-30 05:49:07 -07005583
5584 if response.status_code != 202:
5585 self.logger.error("PUT REST API call {} failed. Return status code {}. Response Content:{}"
5586 .format(disk_href, response.status_code, response.content))
5587 else:
kasarc5bf2932018-03-09 04:15:22 -08005588 add_disk_task = self.get_task_from_response(response.content)
5589 result = self.client.get_task_monitor().wait_for_success(task=add_disk_task)
sbhangarea8e5b782018-06-21 02:10:03 -07005590 if result.get('status') == 'success':
kasarc5bf2932018-03-09 04:15:22 -08005591 status = True
5592 else:
sbhangarea8e5b782018-06-21 02:10:03 -07005593 self.logger.error("Add new disk REST task failed to add {} MB disk".format(disk_size_mb))
bhangare06312472017-03-30 05:49:07 -07005594
5595 except Exception as exp:
5596 self.logger.error("Error occurred calling rest api for creating new disk {}".format(exp))
5597
5598 return status
5599
5600
5601 def add_existing_disk(self, catalogs=None, image_id=None, size=None, template_name=None, vapp_uuid=None):
5602 """
5603 Method to add existing disk to vm
5604 Args :
5605 catalogs - List of VDC catalogs
5606 image_id - Catalog ID
5607 template_name - Name of template in catalog
5608 vapp_uuid - UUID of vApp
5609 Returns:
5610 None
5611 """
5612 disk_info = None
5613 vcenter_conect, content = self.get_vcenter_content()
5614 #find moref-id of vm in image
5615 catalog_vm_info = self.get_vapp_template_details(catalogs=catalogs,
5616 image_id=image_id,
5617 )
5618
5619 if catalog_vm_info and "vm_vcenter_info" in catalog_vm_info:
5620 if "vm_moref_id" in catalog_vm_info["vm_vcenter_info"]:
5621 catalog_vm_moref_id = catalog_vm_info["vm_vcenter_info"].get("vm_moref_id", None)
5622 if catalog_vm_moref_id:
5623 self.logger.info("Moref_id of VM in catalog : {}" .format(catalog_vm_moref_id))
5624 host, catalog_vm_obj = self.get_vm_obj(content, catalog_vm_moref_id)
5625 if catalog_vm_obj:
5626 #find existing disk
5627 disk_info = self.find_disk(catalog_vm_obj)
5628 else:
5629 exp_msg = "No VM with image id {} found".format(image_id)
5630 self.rollback_newvm(vapp_uuid, exp_msg, exp_type="NotFound")
5631 else:
5632 exp_msg = "No Image found with image ID {} ".format(image_id)
5633 self.rollback_newvm(vapp_uuid, exp_msg, exp_type="NotFound")
5634
5635 if disk_info:
5636 self.logger.info("Existing disk_info : {}".format(disk_info))
5637 #get VM
5638 vm_moref_id = self.get_vm_moref_id(vapp_uuid)
5639 host, vm_obj = self.get_vm_obj(content, vm_moref_id)
5640 if vm_obj:
5641 status = self.add_disk(vcenter_conect=vcenter_conect,
5642 vm=vm_obj,
5643 disk_info=disk_info,
5644 size=size,
5645 vapp_uuid=vapp_uuid
5646 )
5647 if status:
5648 self.logger.info("Disk from image id {} added to {}".format(image_id,
5649 vm_obj.config.name)
5650 )
5651 else:
5652 msg = "No disk found with image id {} to add in VM {}".format(
5653 image_id,
5654 vm_obj.config.name)
5655 self.rollback_newvm(vapp_uuid, msg, exp_type="NotFound")
5656
5657
5658 def find_disk(self, vm_obj):
5659 """
5660 Method to find details of existing disk in VM
5661 Args :
5662 vm_obj - vCenter object of VM
5663 image_id - Catalog ID
5664 Returns:
5665 disk_info : dict of disk details
5666 """
5667 disk_info = {}
5668 if vm_obj:
5669 try:
5670 devices = vm_obj.config.hardware.device
5671 for device in devices:
5672 if type(device) is vim.vm.device.VirtualDisk:
5673 if isinstance(device.backing,vim.vm.device.VirtualDisk.FlatVer2BackingInfo) and hasattr(device.backing, 'fileName'):
5674 disk_info["full_path"] = device.backing.fileName
5675 disk_info["datastore"] = device.backing.datastore
5676 disk_info["capacityKB"] = device.capacityInKB
5677 break
5678 except Exception as exp:
5679 self.logger.error("find_disk() : exception occurred while "\
5680 "getting existing disk details :{}".format(exp))
5681 return disk_info
5682
5683
5684 def add_disk(self, vcenter_conect=None, vm=None, size=None, vapp_uuid=None, disk_info={}):
5685 """
5686 Method to add existing disk in VM
5687 Args :
5688 vcenter_conect - vCenter content object
5689 vm - vCenter vm object
5690 disk_info : dict of disk details
5691 Returns:
5692 status : status of add disk task
5693 """
5694 datastore = disk_info["datastore"] if "datastore" in disk_info else None
5695 fullpath = disk_info["full_path"] if "full_path" in disk_info else None
5696 capacityKB = disk_info["capacityKB"] if "capacityKB" in disk_info else None
5697 if size is not None:
5698 #Convert size from GB to KB
5699 sizeKB = int(size) * 1024 * 1024
5700 #compare size of existing disk and user given size.Assign whicherver is greater
5701 self.logger.info("Add Existing disk : sizeKB {} , capacityKB {}".format(
5702 sizeKB, capacityKB))
5703 if sizeKB > capacityKB:
5704 capacityKB = sizeKB
5705
5706 if datastore and fullpath and capacityKB:
5707 try:
5708 spec = vim.vm.ConfigSpec()
5709 # get all disks on a VM, set unit_number to the next available
5710 unit_number = 0
5711 for dev in vm.config.hardware.device:
5712 if hasattr(dev.backing, 'fileName'):
5713 unit_number = int(dev.unitNumber) + 1
5714 # unit_number 7 reserved for scsi controller
5715 if unit_number == 7:
5716 unit_number += 1
5717 if isinstance(dev, vim.vm.device.VirtualDisk):
5718 #vim.vm.device.VirtualSCSIController
5719 controller_key = dev.controllerKey
5720
5721 self.logger.info("Add Existing disk : unit number {} , controller key {}".format(
5722 unit_number, controller_key))
5723 # add disk here
5724 dev_changes = []
5725 disk_spec = vim.vm.device.VirtualDeviceSpec()
5726 disk_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add
5727 disk_spec.device = vim.vm.device.VirtualDisk()
5728 disk_spec.device.backing = \
5729 vim.vm.device.VirtualDisk.FlatVer2BackingInfo()
5730 disk_spec.device.backing.thinProvisioned = True
5731 disk_spec.device.backing.diskMode = 'persistent'
5732 disk_spec.device.backing.datastore = datastore
5733 disk_spec.device.backing.fileName = fullpath
5734
5735 disk_spec.device.unitNumber = unit_number
5736 disk_spec.device.capacityInKB = capacityKB
5737 disk_spec.device.controllerKey = controller_key
5738 dev_changes.append(disk_spec)
5739 spec.deviceChange = dev_changes
5740 task = vm.ReconfigVM_Task(spec=spec)
5741 status = self.wait_for_vcenter_task(task, vcenter_conect)
5742 return status
5743 except Exception as exp:
5744 exp_msg = "add_disk() : exception {} occurred while adding disk "\
5745 "{} to vm {}".format(exp,
5746 fullpath,
5747 vm.config.name)
5748 self.rollback_newvm(vapp_uuid, exp_msg)
5749 else:
5750 msg = "add_disk() : Can not add disk to VM with disk info {} ".format(disk_info)
5751 self.rollback_newvm(vapp_uuid, msg)
5752
5753
5754 def get_vcenter_content(self):
5755 """
5756 Get the vsphere content object
5757 """
5758 try:
5759 vm_vcenter_info = self.get_vm_vcenter_info()
5760 except Exception as exp:
5761 self.logger.error("Error occurred while getting vCenter infromationn"\
5762 " for VM : {}".format(exp))
5763 raise vimconn.vimconnException(message=exp)
5764
5765 context = None
5766 if hasattr(ssl, '_create_unverified_context'):
5767 context = ssl._create_unverified_context()
5768
5769 vcenter_conect = SmartConnect(
5770 host=vm_vcenter_info["vm_vcenter_ip"],
5771 user=vm_vcenter_info["vm_vcenter_user"],
5772 pwd=vm_vcenter_info["vm_vcenter_password"],
5773 port=int(vm_vcenter_info["vm_vcenter_port"]),
5774 sslContext=context
5775 )
5776 atexit.register(Disconnect, vcenter_conect)
5777 content = vcenter_conect.RetrieveContent()
5778 return vcenter_conect, content
5779
5780
5781 def get_vm_moref_id(self, vapp_uuid):
5782 """
5783 Get the moref_id of given VM
5784 """
5785 try:
5786 if vapp_uuid:
5787 vm_details = self.get_vapp_details_rest(vapp_uuid, need_admin_access=True)
5788 if vm_details and "vm_vcenter_info" in vm_details:
5789 vm_moref_id = vm_details["vm_vcenter_info"].get("vm_moref_id", None)
bhangare06312472017-03-30 05:49:07 -07005790 return vm_moref_id
5791
5792 except Exception as exp:
5793 self.logger.error("Error occurred while getting VM moref ID "\
5794 " for VM : {}".format(exp))
5795 return None
5796
5797
5798 def get_vapp_template_details(self, catalogs=None, image_id=None , template_name=None):
5799 """
5800 Method to get vApp template details
5801 Args :
5802 catalogs - list of VDC catalogs
5803 image_id - Catalog ID to find
5804 template_name : template name in catalog
5805 Returns:
5806 parsed_respond : dict of vApp tempalte details
5807 """
5808 parsed_response = {}
5809
5810 vca = self.connect_as_admin()
5811 if not vca:
kasarc5bf2932018-03-09 04:15:22 -08005812 raise vimconn.vimconnConnectionException("Failed to connect vCD")
bhangare06312472017-03-30 05:49:07 -07005813
5814 try:
kasarc5bf2932018-03-09 04:15:22 -08005815 org, vdc = self.get_vdc_details()
bhangare06312472017-03-30 05:49:07 -07005816 catalog = self.get_catalog_obj(image_id, catalogs)
5817 if catalog:
kasarc5bf2932018-03-09 04:15:22 -08005818 items = org.get_catalog_item(catalog.get('name'), catalog.get('name'))
5819 catalog_items = [items.attrib]
5820
bhangare06312472017-03-30 05:49:07 -07005821 if len(catalog_items) == 1:
kasarc5bf2932018-03-09 04:15:22 -08005822 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
sbhangarea8e5b782018-06-21 02:10:03 -07005823 'x-vcloud-authorization': vca._session.headers['x-vcloud-authorization']}
kasarc5bf2932018-03-09 04:15:22 -08005824
5825 response = self.perform_request(req_type='GET',
5826 url=catalog_items[0].get('href'),
5827 headers=headers)
bhangare06312472017-03-30 05:49:07 -07005828 catalogItem = XmlElementTree.fromstring(response.content)
5829 entity = [child for child in catalogItem if child.get("type") == "application/vnd.vmware.vcloud.vAppTemplate+xml"][0]
5830 vapp_tempalte_href = entity.get("href")
5831 #get vapp details and parse moref id
5832
5833 namespaces = {"vssd":"http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData" ,
5834 'ovf': 'http://schemas.dmtf.org/ovf/envelope/1',
5835 'vmw': 'http://www.vmware.com/schema/ovf',
5836 'vm': 'http://www.vmware.com/vcloud/v1.5',
5837 'rasd':"http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData",
5838 'vmext':"http://www.vmware.com/vcloud/extension/v1.5",
5839 'xmlns':"http://www.vmware.com/vcloud/v1.5"
5840 }
5841
kasarc5bf2932018-03-09 04:15:22 -08005842 if vca._session:
5843 response = self.perform_request(req_type='GET',
5844 url=vapp_tempalte_href,
5845 headers=headers)
bhangare06312472017-03-30 05:49:07 -07005846
5847 if response.status_code != requests.codes.ok:
5848 self.logger.debug("REST API call {} failed. Return status code {}".format(
5849 vapp_tempalte_href, response.status_code))
5850
5851 else:
5852 xmlroot_respond = XmlElementTree.fromstring(response.content)
5853 children_section = xmlroot_respond.find('vm:Children/', namespaces)
5854 if children_section is not None:
5855 vCloud_extension_section = children_section.find('xmlns:VCloudExtension', namespaces)
5856 if vCloud_extension_section is not None:
5857 vm_vcenter_info = {}
5858 vim_info = vCloud_extension_section.find('vmext:VmVimInfo', namespaces)
5859 vmext = vim_info.find('vmext:VmVimObjectRef', namespaces)
5860 if vmext is not None:
5861 vm_vcenter_info["vm_moref_id"] = vmext.find('vmext:MoRef', namespaces).text
5862 parsed_response["vm_vcenter_info"]= vm_vcenter_info
5863
5864 except Exception as exp :
5865 self.logger.info("Error occurred calling rest api for getting vApp details {}".format(exp))
5866
5867 return parsed_response
5868
5869
5870 def rollback_newvm(self, vapp_uuid, msg , exp_type="Genric"):
5871 """
5872 Method to delete vApp
5873 Args :
5874 vapp_uuid - vApp UUID
5875 msg - Error message to be logged
5876 exp_type : Exception type
5877 Returns:
5878 None
5879 """
5880 if vapp_uuid:
5881 status = self.delete_vminstance(vapp_uuid)
5882 else:
5883 msg = "No vApp ID"
5884 self.logger.error(msg)
5885 if exp_type == "Genric":
5886 raise vimconn.vimconnException(msg)
5887 elif exp_type == "NotFound":
5888 raise vimconn.vimconnNotFoundException(message=msg)
5889
kateac1e3792017-04-01 02:16:39 -07005890 def add_sriov(self, vapp_uuid, sriov_nets, vmname_andid):
5891 """
5892 Method to attach SRIOV adapters to VM
5893
5894 Args:
5895 vapp_uuid - uuid of vApp/VM
5896 sriov_nets - SRIOV devices infromation as specified in VNFD (flavor)
5897 vmname_andid - vmname
5898
5899 Returns:
5900 The status of add SRIOV adapter task , vm object and
5901 vcenter_conect object
5902 """
5903 vm_obj = None
5904 vcenter_conect, content = self.get_vcenter_content()
5905 vm_moref_id = self.get_vm_moref_id(vapp_uuid)
5906
5907 if vm_moref_id:
5908 try:
5909 no_of_sriov_devices = len(sriov_nets)
5910 if no_of_sriov_devices > 0:
5911 #Get VM and its host
5912 host_obj, vm_obj = self.get_vm_obj(content, vm_moref_id)
5913 self.logger.info("VM {} is currently on host {}".format(vm_obj, host_obj))
5914 if host_obj and vm_obj:
5915 #get SRIOV devies from host on which vapp is currently installed
5916 avilable_sriov_devices = self.get_sriov_devices(host_obj,
5917 no_of_sriov_devices,
5918 )
5919
5920 if len(avilable_sriov_devices) == 0:
5921 #find other hosts with active pci devices
5922 new_host_obj , avilable_sriov_devices = self.get_host_and_sriov_devices(
5923 content,
5924 no_of_sriov_devices,
5925 )
5926
5927 if new_host_obj is not None and len(avilable_sriov_devices)> 0:
5928 #Migrate vm to the host where SRIOV devices are available
5929 self.logger.info("Relocate VM {} on new host {}".format(vm_obj,
5930 new_host_obj))
5931 task = self.relocate_vm(new_host_obj, vm_obj)
5932 if task is not None:
5933 result = self.wait_for_vcenter_task(task, vcenter_conect)
5934 self.logger.info("Migrate VM status: {}".format(result))
5935 host_obj = new_host_obj
5936 else:
5937 self.logger.info("Fail to migrate VM : {}".format(result))
5938 raise vimconn.vimconnNotFoundException(
5939 "Fail to migrate VM : {} to host {}".format(
5940 vmname_andid,
5941 new_host_obj)
5942 )
5943
5944 if host_obj is not None and avilable_sriov_devices is not None and len(avilable_sriov_devices)> 0:
5945 #Add SRIOV devices one by one
5946 for sriov_net in sriov_nets:
5947 network_name = sriov_net.get('net_id')
5948 dvs_portgr_name = self.create_dvPort_group(network_name)
tierno66eba6e2017-11-10 17:09:18 +01005949 if sriov_net.get('type') == "VF" or sriov_net.get('type') == "SR-IOV":
kateac1e3792017-04-01 02:16:39 -07005950 #add vlan ID ,Modify portgroup for vlan ID
5951 self.configure_vlanID(content, vcenter_conect, network_name)
5952
5953 task = self.add_sriov_to_vm(content,
5954 vm_obj,
5955 host_obj,
5956 network_name,
5957 avilable_sriov_devices[0]
5958 )
5959 if task:
5960 status= self.wait_for_vcenter_task(task, vcenter_conect)
5961 if status:
5962 self.logger.info("Added SRIOV {} to VM {}".format(
5963 no_of_sriov_devices,
5964 str(vm_obj)))
5965 else:
5966 self.logger.error("Fail to add SRIOV {} to VM {}".format(
5967 no_of_sriov_devices,
5968 str(vm_obj)))
5969 raise vimconn.vimconnUnexpectedResponse(
5970 "Fail to add SRIOV adapter in VM ".format(str(vm_obj))
5971 )
5972 return True, vm_obj, vcenter_conect
5973 else:
5974 self.logger.error("Currently there is no host with"\
5975 " {} number of avaialble SRIOV "\
5976 "VFs required for VM {}".format(
5977 no_of_sriov_devices,
5978 vmname_andid)
5979 )
5980 raise vimconn.vimconnNotFoundException(
5981 "Currently there is no host with {} "\
5982 "number of avaialble SRIOV devices required for VM {}".format(
5983 no_of_sriov_devices,
5984 vmname_andid))
5985 else:
5986 self.logger.debug("No infromation about SRIOV devices {} ",sriov_nets)
5987
5988 except vmodl.MethodFault as error:
5989 self.logger.error("Error occurred while adding SRIOV {} ",error)
5990 return None, vm_obj, vcenter_conect
5991
5992
5993 def get_sriov_devices(self,host, no_of_vfs):
5994 """
5995 Method to get the details of SRIOV devices on given host
5996 Args:
5997 host - vSphere host object
5998 no_of_vfs - number of VFs needed on host
5999
6000 Returns:
6001 array of SRIOV devices
6002 """
6003 sriovInfo=[]
6004 if host:
6005 for device in host.config.pciPassthruInfo:
6006 if isinstance(device,vim.host.SriovInfo) and device.sriovActive:
6007 if device.numVirtualFunction >= no_of_vfs:
6008 sriovInfo.append(device)
6009 break
6010 return sriovInfo
6011
6012
6013 def get_host_and_sriov_devices(self, content, no_of_vfs):
6014 """
6015 Method to get the details of SRIOV devices infromation on all hosts
6016
6017 Args:
6018 content - vSphere host object
6019 no_of_vfs - number of pci VFs needed on host
6020
6021 Returns:
6022 array of SRIOV devices and host object
6023 """
6024 host_obj = None
6025 sriov_device_objs = None
6026 try:
6027 if content:
6028 container = content.viewManager.CreateContainerView(content.rootFolder,
6029 [vim.HostSystem], True)
6030 for host in container.view:
6031 devices = self.get_sriov_devices(host, no_of_vfs)
6032 if devices:
6033 host_obj = host
6034 sriov_device_objs = devices
6035 break
6036 except Exception as exp:
6037 self.logger.error("Error {} occurred while finding SRIOV devices on host: {}".format(exp, host_obj))
6038
6039 return host_obj,sriov_device_objs
6040
6041
6042 def add_sriov_to_vm(self,content, vm_obj, host_obj, network_name, sriov_device):
6043 """
6044 Method to add SRIOV adapter to vm
6045
6046 Args:
6047 host_obj - vSphere host object
6048 vm_obj - vSphere vm object
6049 content - vCenter content object
6050 network_name - name of distributed virtaul portgroup
6051 sriov_device - SRIOV device info
6052
6053 Returns:
6054 task object
6055 """
6056 devices = []
6057 vnic_label = "sriov nic"
6058 try:
6059 dvs_portgr = self.get_dvport_group(network_name)
6060 network_name = dvs_portgr.name
6061 nic = vim.vm.device.VirtualDeviceSpec()
6062 # VM device
6063 nic.operation = vim.vm.device.VirtualDeviceSpec.Operation.add
6064 nic.device = vim.vm.device.VirtualSriovEthernetCard()
6065 nic.device.addressType = 'assigned'
6066 #nic.device.key = 13016
6067 nic.device.deviceInfo = vim.Description()
6068 nic.device.deviceInfo.label = vnic_label
6069 nic.device.deviceInfo.summary = network_name
6070 nic.device.backing = vim.vm.device.VirtualEthernetCard.NetworkBackingInfo()
6071
6072 nic.device.backing.network = self.get_obj(content, [vim.Network], network_name)
6073 nic.device.backing.deviceName = network_name
6074 nic.device.backing.useAutoDetect = False
6075 nic.device.connectable = vim.vm.device.VirtualDevice.ConnectInfo()
6076 nic.device.connectable.startConnected = True
6077 nic.device.connectable.allowGuestControl = True
6078
6079 nic.device.sriovBacking = vim.vm.device.VirtualSriovEthernetCard.SriovBackingInfo()
6080 nic.device.sriovBacking.physicalFunctionBacking = vim.vm.device.VirtualPCIPassthrough.DeviceBackingInfo()
6081 nic.device.sriovBacking.physicalFunctionBacking.id = sriov_device.id
6082
6083 devices.append(nic)
6084 vmconf = vim.vm.ConfigSpec(deviceChange=devices)
6085 task = vm_obj.ReconfigVM_Task(vmconf)
6086 return task
6087 except Exception as exp:
6088 self.logger.error("Error {} occurred while adding SRIOV adapter in VM: {}".format(exp, vm_obj))
6089 return None
6090
6091
6092 def create_dvPort_group(self, network_name):
6093 """
6094 Method to create disributed virtual portgroup
6095
6096 Args:
6097 network_name - name of network/portgroup
6098
6099 Returns:
6100 portgroup key
6101 """
6102 try:
6103 new_network_name = [network_name, '-', str(uuid.uuid4())]
6104 network_name=''.join(new_network_name)
6105 vcenter_conect, content = self.get_vcenter_content()
6106
6107 dv_switch = self.get_obj(content, [vim.DistributedVirtualSwitch], self.dvs_name)
6108 if dv_switch:
6109 dv_pg_spec = vim.dvs.DistributedVirtualPortgroup.ConfigSpec()
6110 dv_pg_spec.name = network_name
6111
6112 dv_pg_spec.type = vim.dvs.DistributedVirtualPortgroup.PortgroupType.earlyBinding
6113 dv_pg_spec.defaultPortConfig = vim.dvs.VmwareDistributedVirtualSwitch.VmwarePortConfigPolicy()
6114 dv_pg_spec.defaultPortConfig.securityPolicy = vim.dvs.VmwareDistributedVirtualSwitch.SecurityPolicy()
6115 dv_pg_spec.defaultPortConfig.securityPolicy.allowPromiscuous = vim.BoolPolicy(value=False)
6116 dv_pg_spec.defaultPortConfig.securityPolicy.forgedTransmits = vim.BoolPolicy(value=False)
6117 dv_pg_spec.defaultPortConfig.securityPolicy.macChanges = vim.BoolPolicy(value=False)
6118
6119 task = dv_switch.AddDVPortgroup_Task([dv_pg_spec])
6120 self.wait_for_vcenter_task(task, vcenter_conect)
6121
6122 dvPort_group = self.get_obj(content, [vim.dvs.DistributedVirtualPortgroup], network_name)
6123 if dvPort_group:
6124 self.logger.info("Created disributed virtaul port group: {}".format(dvPort_group))
6125 return dvPort_group.key
6126 else:
6127 self.logger.debug("No disributed virtual switch found with name {}".format(network_name))
6128
6129 except Exception as exp:
6130 self.logger.error("Error occurred while creating disributed virtaul port group {}"\
6131 " : {}".format(network_name, exp))
6132 return None
6133
6134 def reconfig_portgroup(self, content, dvPort_group_name , config_info={}):
6135 """
6136 Method to reconfigure disributed virtual portgroup
6137
6138 Args:
6139 dvPort_group_name - name of disributed virtual portgroup
6140 content - vCenter content object
6141 config_info - disributed virtual portgroup configuration
6142
6143 Returns:
6144 task object
6145 """
6146 try:
6147 dvPort_group = self.get_dvport_group(dvPort_group_name)
6148 if dvPort_group:
6149 dv_pg_spec = vim.dvs.DistributedVirtualPortgroup.ConfigSpec()
6150 dv_pg_spec.configVersion = dvPort_group.config.configVersion
6151 dv_pg_spec.defaultPortConfig = vim.dvs.VmwareDistributedVirtualSwitch.VmwarePortConfigPolicy()
6152 if "vlanID" in config_info:
6153 dv_pg_spec.defaultPortConfig.vlan = vim.dvs.VmwareDistributedVirtualSwitch.VlanIdSpec()
6154 dv_pg_spec.defaultPortConfig.vlan.vlanId = config_info.get('vlanID')
6155
6156 task = dvPort_group.ReconfigureDVPortgroup_Task(spec=dv_pg_spec)
6157 return task
6158 else:
6159 return None
6160 except Exception as exp:
6161 self.logger.error("Error occurred while reconfiguraing disributed virtaul port group {}"\
6162 " : {}".format(dvPort_group_name, exp))
6163 return None
6164
6165
6166 def destroy_dvport_group(self , dvPort_group_name):
6167 """
6168 Method to destroy disributed virtual portgroup
6169
6170 Args:
6171 network_name - name of network/portgroup
6172
6173 Returns:
6174 True if portgroup successfully got deleted else false
6175 """
6176 vcenter_conect, content = self.get_vcenter_content()
6177 try:
6178 status = None
6179 dvPort_group = self.get_dvport_group(dvPort_group_name)
6180 if dvPort_group:
6181 task = dvPort_group.Destroy_Task()
6182 status = self.wait_for_vcenter_task(task, vcenter_conect)
6183 return status
6184 except vmodl.MethodFault as exp:
6185 self.logger.error("Caught vmodl fault {} while deleting disributed virtaul port group {}".format(
6186 exp, dvPort_group_name))
6187 return None
6188
6189
6190 def get_dvport_group(self, dvPort_group_name):
6191 """
6192 Method to get disributed virtual portgroup
6193
6194 Args:
6195 network_name - name of network/portgroup
6196
6197 Returns:
6198 portgroup object
6199 """
6200 vcenter_conect, content = self.get_vcenter_content()
6201 dvPort_group = None
6202 try:
6203 container = content.viewManager.CreateContainerView(content.rootFolder, [vim.dvs.DistributedVirtualPortgroup], True)
6204 for item in container.view:
6205 if item.key == dvPort_group_name:
6206 dvPort_group = item
6207 break
6208 return dvPort_group
6209 except vmodl.MethodFault as exp:
6210 self.logger.error("Caught vmodl fault {} for disributed virtaul port group {}".format(
6211 exp, dvPort_group_name))
6212 return None
6213
6214 def get_vlanID_from_dvs_portgr(self, dvPort_group_name):
6215 """
6216 Method to get disributed virtual portgroup vlanID
6217
6218 Args:
6219 network_name - name of network/portgroup
6220
6221 Returns:
6222 vlan ID
6223 """
6224 vlanId = None
6225 try:
6226 dvPort_group = self.get_dvport_group(dvPort_group_name)
6227 if dvPort_group:
6228 vlanId = dvPort_group.config.defaultPortConfig.vlan.vlanId
6229 except vmodl.MethodFault as exp:
6230 self.logger.error("Caught vmodl fault {} for disributed virtaul port group {}".format(
6231 exp, dvPort_group_name))
6232 return vlanId
6233
6234
6235 def configure_vlanID(self, content, vcenter_conect, dvPort_group_name):
6236 """
6237 Method to configure vlanID in disributed virtual portgroup vlanID
6238
6239 Args:
6240 network_name - name of network/portgroup
6241
6242 Returns:
6243 None
6244 """
6245 vlanID = self.get_vlanID_from_dvs_portgr(dvPort_group_name)
6246 if vlanID == 0:
6247 #configure vlanID
6248 vlanID = self.genrate_vlanID(dvPort_group_name)
6249 config = {"vlanID":vlanID}
6250 task = self.reconfig_portgroup(content, dvPort_group_name,
6251 config_info=config)
6252 if task:
6253 status= self.wait_for_vcenter_task(task, vcenter_conect)
6254 if status:
6255 self.logger.info("Reconfigured Port group {} for vlan ID {}".format(
6256 dvPort_group_name,vlanID))
6257 else:
6258 self.logger.error("Fail reconfigure portgroup {} for vlanID{}".format(
6259 dvPort_group_name, vlanID))
6260
6261
6262 def genrate_vlanID(self, network_name):
6263 """
6264 Method to get unused vlanID
6265 Args:
6266 network_name - name of network/portgroup
6267 Returns:
6268 vlanID
6269 """
6270 vlan_id = None
6271 used_ids = []
6272 if self.config.get('vlanID_range') == None:
6273 raise vimconn.vimconnConflictException("You must provide a 'vlanID_range' "\
6274 "at config value before creating sriov network with vlan tag")
6275 if "used_vlanIDs" not in self.persistent_info:
6276 self.persistent_info["used_vlanIDs"] = {}
6277 else:
6278 used_ids = self.persistent_info["used_vlanIDs"].values()
kasarc5bf2932018-03-09 04:15:22 -08006279 #For python3
6280 #used_ids = list(self.persistent_info["used_vlanIDs"].values())
kateac1e3792017-04-01 02:16:39 -07006281
6282 for vlanID_range in self.config.get('vlanID_range'):
6283 start_vlanid , end_vlanid = vlanID_range.split("-")
6284 if start_vlanid > end_vlanid:
6285 raise vimconn.vimconnConflictException("Invalid vlan ID range {}".format(
6286 vlanID_range))
6287
6288 for id in xrange(int(start_vlanid), int(end_vlanid) + 1):
kasarc5bf2932018-03-09 04:15:22 -08006289 #For python3
6290 #for id in range(int(start_vlanid), int(end_vlanid) + 1):
kateac1e3792017-04-01 02:16:39 -07006291 if id not in used_ids:
6292 vlan_id = id
6293 self.persistent_info["used_vlanIDs"][network_name] = vlan_id
6294 return vlan_id
6295 if vlan_id is None:
6296 raise vimconn.vimconnConflictException("All Vlan IDs are in use")
6297
6298
6299 def get_obj(self, content, vimtype, name):
6300 """
6301 Get the vsphere object associated with a given text name
6302 """
6303 obj = None
6304 container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True)
6305 for item in container.view:
6306 if item.name == name:
6307 obj = item
6308 break
6309 return obj
6310
kasar0c007d62017-05-19 03:13:57 -07006311
6312 def insert_media_to_vm(self, vapp, image_id):
6313 """
6314 Method to insert media CD-ROM (ISO image) from catalog to vm.
6315 vapp - vapp object to get vm id
6316 Image_id - image id for cdrom to be inerted to vm
6317 """
6318 # create connection object
6319 vca = self.connect()
6320 try:
6321 # fetching catalog details
kasarc5bf2932018-03-09 04:15:22 -08006322 rest_url = "{}/api/catalog/{}".format(self.url, image_id)
6323 if vca._session:
6324 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
sbhangarea8e5b782018-06-21 02:10:03 -07006325 'x-vcloud-authorization': vca._session.headers['x-vcloud-authorization']}
kasarc5bf2932018-03-09 04:15:22 -08006326 response = self.perform_request(req_type='GET',
6327 url=rest_url,
6328 headers=headers)
kasar0c007d62017-05-19 03:13:57 -07006329
6330 if response.status_code != 200:
6331 self.logger.error("REST call {} failed reason : {}"\
6332 "status code : {}".format(url_rest_call,
6333 response.content,
6334 response.status_code))
6335 raise vimconn.vimconnException("insert_media_to_vm(): Failed to get "\
6336 "catalog details")
6337 # searching iso name and id
6338 iso_name,media_id = self.get_media_details(vca, response.content)
6339
6340 if iso_name and media_id:
6341 data ="""<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
6342 <ns6:MediaInsertOrEjectParams
Ananda Baitharu319c26f2019-03-05 17:34:31 +00006343 xmlns="http://www.vmware.com/vcloud/versions" xmlns:ns2="http://schemas.dmtf.org/ovf/envelope/1"
6344 xmlns:ns3="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData"
6345 xmlns:ns4="http://schemas.dmtf.org/wbem/wscim/1/common"
6346 xmlns:ns5="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData"
6347 xmlns:ns6="http://www.vmware.com/vcloud/v1.5"
6348 xmlns:ns7="http://www.vmware.com/schema/ovf"
6349 xmlns:ns8="http://schemas.dmtf.org/ovf/environment/1"
6350 xmlns:ns9="http://www.vmware.com/vcloud/extension/v1.5">
kasar0c007d62017-05-19 03:13:57 -07006351 <ns6:Media
6352 type="application/vnd.vmware.vcloud.media+xml"
Ananda Baitharu319c26f2019-03-05 17:34:31 +00006353 name="{}"
kasar0c007d62017-05-19 03:13:57 -07006354 id="urn:vcloud:media:{}"
6355 href="https://{}/api/media/{}"/>
6356 </ns6:MediaInsertOrEjectParams>""".format(iso_name, media_id,
kasarc5bf2932018-03-09 04:15:22 -08006357 self.url,media_id)
kasar0c007d62017-05-19 03:13:57 -07006358
kasarc5bf2932018-03-09 04:15:22 -08006359 for vms in vapp.get_all_vms():
6360 vm_id = vms.get('id').split(':')[-1]
kasar0c007d62017-05-19 03:13:57 -07006361
kasar0c007d62017-05-19 03:13:57 -07006362 headers['Content-Type'] = 'application/vnd.vmware.vcloud.mediaInsertOrEjectParams+xml'
kasarc5bf2932018-03-09 04:15:22 -08006363 rest_url = "{}/api/vApp/vm-{}/media/action/insertMedia".format(self.url,vm_id)
kasar0c007d62017-05-19 03:13:57 -07006364
kasarc5bf2932018-03-09 04:15:22 -08006365 response = self.perform_request(req_type='POST',
6366 url=rest_url,
6367 data=data,
6368 headers=headers)
kasar0c007d62017-05-19 03:13:57 -07006369
6370 if response.status_code != 202:
Ananda Baitharu319c26f2019-03-05 17:34:31 +00006371 error_msg = "insert_media_to_vm() : Failed to insert CD-ROM to vm. Reason {}. " \
6372 "Status code {}".format(response.text, response.status_code)
6373 self.logger.error(error_msg)
6374 raise vimconn.vimconnException(error_msg)
kasar0c007d62017-05-19 03:13:57 -07006375 else:
kasarc5bf2932018-03-09 04:15:22 -08006376 task = self.get_task_from_response(response.content)
6377 result = self.client.get_task_monitor().wait_for_success(task=task)
6378 if result.get('status') == 'success':
kasar0c007d62017-05-19 03:13:57 -07006379 self.logger.info("insert_media_to_vm(): Sucessfully inserted media ISO"\
6380 " image to vm {}".format(vm_id))
kasarc5bf2932018-03-09 04:15:22 -08006381
kasar0c007d62017-05-19 03:13:57 -07006382 except Exception as exp:
6383 self.logger.error("insert_media_to_vm() : exception occurred "\
6384 "while inserting media CD-ROM")
6385 raise vimconn.vimconnException(message=exp)
6386
6387
6388 def get_media_details(self, vca, content):
6389 """
6390 Method to get catalog item details
6391 vca - connection object
6392 content - Catalog details
6393 Return - Media name, media id
6394 """
6395 cataloghref_list = []
6396 try:
6397 if content:
6398 vm_list_xmlroot = XmlElementTree.fromstring(content)
6399 for child in vm_list_xmlroot.iter():
6400 if 'CatalogItem' in child.tag:
6401 cataloghref_list.append(child.attrib.get('href'))
6402 if cataloghref_list is not None:
6403 for href in cataloghref_list:
6404 if href:
kasarc5bf2932018-03-09 04:15:22 -08006405 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
sbhangarea8e5b782018-06-21 02:10:03 -07006406 'x-vcloud-authorization': vca._session.headers['x-vcloud-authorization']}
kasarc5bf2932018-03-09 04:15:22 -08006407 response = self.perform_request(req_type='GET',
6408 url=href,
6409 headers=headers)
kasar0c007d62017-05-19 03:13:57 -07006410 if response.status_code != 200:
6411 self.logger.error("REST call {} failed reason : {}"\
6412 "status code : {}".format(href,
6413 response.content,
6414 response.status_code))
6415 raise vimconn.vimconnException("get_media_details : Failed to get "\
6416 "catalogitem details")
6417 list_xmlroot = XmlElementTree.fromstring(response.content)
6418 for child in list_xmlroot.iter():
6419 if 'Entity' in child.tag:
6420 if 'media' in child.attrib.get('href'):
6421 name = child.attrib.get('name')
6422 media_id = child.attrib.get('href').split('/').pop()
6423 return name,media_id
6424 else:
6425 self.logger.debug("Media name and id not found")
6426 return False,False
6427 except Exception as exp:
6428 self.logger.error("get_media_details : exception occurred "\
6429 "getting media details")
6430 raise vimconn.vimconnException(message=exp)
6431
bhangare1a0b97c2017-06-21 02:20:15 -07006432
kated47ad5f2017-08-03 02:16:13 -07006433 def retry_rest(self, method, url, add_headers=None, data=None):
bhangare1a0b97c2017-06-21 02:20:15 -07006434 """ Method to get Token & retry respective REST request
6435 Args:
6436 api - REST API - Can be one of 'GET' or 'PUT' or 'POST'
6437 url - request url to be used
6438 add_headers - Additional headers (optional)
6439 data - Request payload data to be passed in request
6440 Returns:
6441 response - Response of request
6442 """
6443 response = None
6444
6445 #Get token
6446 self.get_token()
6447
kasarc5bf2932018-03-09 04:15:22 -08006448 if self.client._session:
6449 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
sbhangarea8e5b782018-06-21 02:10:03 -07006450 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
bhangare1a0b97c2017-06-21 02:20:15 -07006451
6452 if add_headers:
6453 headers.update(add_headers)
6454
kated47ad5f2017-08-03 02:16:13 -07006455 if method == 'GET':
kasarc5bf2932018-03-09 04:15:22 -08006456 response = self.perform_request(req_type='GET',
6457 url=url,
6458 headers=headers)
kated47ad5f2017-08-03 02:16:13 -07006459 elif method == 'PUT':
kasarc5bf2932018-03-09 04:15:22 -08006460 response = self.perform_request(req_type='PUT',
6461 url=url,
6462 headers=headers,
sbhangarea8e5b782018-06-21 02:10:03 -07006463 data=data)
kated47ad5f2017-08-03 02:16:13 -07006464 elif method == 'POST':
kasarc5bf2932018-03-09 04:15:22 -08006465 response = self.perform_request(req_type='POST',
6466 url=url,
6467 headers=headers,
sbhangarea8e5b782018-06-21 02:10:03 -07006468 data=data)
kated47ad5f2017-08-03 02:16:13 -07006469 elif method == 'DELETE':
kasarc5bf2932018-03-09 04:15:22 -08006470 response = self.perform_request(req_type='DELETE',
6471 url=url,
6472 headers=headers)
kated47ad5f2017-08-03 02:16:13 -07006473 return response
6474
bhangare1a0b97c2017-06-21 02:20:15 -07006475
6476 def get_token(self):
6477 """ Generate a new token if expired
6478
6479 Returns:
kasarc5bf2932018-03-09 04:15:22 -08006480 The return client object that letter can be used to connect to vCloud director as admin for VDC
bhangare1a0b97c2017-06-21 02:20:15 -07006481 """
bhangare1a0b97c2017-06-21 02:20:15 -07006482 try:
6483 self.logger.debug("Generate token for vca {} as {} to datacenter {}.".format(self.org_name,
6484 self.user,
6485 self.org_name))
kasarc5bf2932018-03-09 04:15:22 -08006486 host = self.url
6487 client = Client(host, verify_ssl_certs=False)
beierl01e776a2019-07-15 09:40:24 -04006488 client.set_highest_supported_version()
kasarc5bf2932018-03-09 04:15:22 -08006489 client.set_credentials(BasicLoginCredentials(self.user, self.org_name, self.passwd))
6490 # connection object
sbhangarea8e5b782018-06-21 02:10:03 -07006491 self.client = client
bhangare1a0b97c2017-06-21 02:20:15 -07006492
6493 except:
6494 raise vimconn.vimconnConnectionException("Can't connect to a vCloud director org: "
6495 "{} as user: {}".format(self.org_name, self.user))
6496
kasarc5bf2932018-03-09 04:15:22 -08006497 if not client:
6498 raise vimconn.vimconnConnectionException("Failed while reconnecting vCD")
bhangare1a0b97c2017-06-21 02:20:15 -07006499
bhangare1a0b97c2017-06-21 02:20:15 -07006500
6501 def get_vdc_details(self):
6502 """ Get VDC details using pyVcloud Lib
6503
kasarc5bf2932018-03-09 04:15:22 -08006504 Returns org and vdc object
bhangare1a0b97c2017-06-21 02:20:15 -07006505 """
Ravi Chamartyb7ff3552018-10-30 19:51:23 +00006506 vdc = None
6507 try:
6508 org = Org(self.client, resource=self.client.get_org())
6509 vdc = org.get_vdc(self.tenant_name)
6510 except Exception as e:
6511 # pyvcloud not giving a specific exception, Refresh nevertheless
6512 self.logger.debug("Received exception {}, refreshing token ".format(str(e)))
bhangare1a0b97c2017-06-21 02:20:15 -07006513
6514 #Retry once, if failed by refreshing token
6515 if vdc is None:
6516 self.get_token()
Ravi Chamartyb7ff3552018-10-30 19:51:23 +00006517 org = Org(self.client, resource=self.client.get_org())
kasarc5bf2932018-03-09 04:15:22 -08006518 vdc = org.get_vdc(self.tenant_name)
bhangare1a0b97c2017-06-21 02:20:15 -07006519
kasarc5bf2932018-03-09 04:15:22 -08006520 return org, vdc
6521
6522
6523 def perform_request(self, req_type, url, headers=None, data=None):
6524 """Perform the POST/PUT/GET/DELETE request."""
6525
6526 #Log REST request details
6527 self.log_request(req_type, url=url, headers=headers, data=data)
6528 # perform request and return its result
6529 if req_type == 'GET':
6530 response = requests.get(url=url,
6531 headers=headers,
6532 verify=False)
6533 elif req_type == 'PUT':
6534 response = requests.put(url=url,
6535 headers=headers,
6536 data=data,
6537 verify=False)
6538 elif req_type == 'POST':
6539 response = requests.post(url=url,
6540 headers=headers,
6541 data=data,
6542 verify=False)
6543 elif req_type == 'DELETE':
6544 response = requests.delete(url=url,
6545 headers=headers,
6546 verify=False)
6547 #Log the REST response
6548 self.log_response(response)
6549
6550 return response
6551
6552
6553 def log_request(self, req_type, url=None, headers=None, data=None):
6554 """Logs REST request details"""
6555
6556 if req_type is not None:
6557 self.logger.debug("Request type: {}".format(req_type))
6558
6559 if url is not None:
6560 self.logger.debug("Request url: {}".format(url))
6561
6562 if headers is not None:
6563 for header in headers:
6564 self.logger.debug("Request header: {}: {}".format(header, headers[header]))
6565
6566 if data is not None:
6567 self.logger.debug("Request data: {}".format(data))
6568
6569
6570 def log_response(self, response):
6571 """Logs REST response details"""
6572
6573 self.logger.debug("Response status code: {} ".format(response.status_code))
6574
6575
6576 def get_task_from_response(self, content):
6577 """
6578 content - API response content(response.content)
sbhangarea8e5b782018-06-21 02:10:03 -07006579 return task object
kasarc5bf2932018-03-09 04:15:22 -08006580 """
6581 xmlroot = XmlElementTree.fromstring(content)
6582 if xmlroot.tag.split('}')[1] == "Task":
6583 return xmlroot
sbhangarea8e5b782018-06-21 02:10:03 -07006584 else:
kasarc5bf2932018-03-09 04:15:22 -08006585 for ele in xmlroot:
6586 if ele.tag.split("}")[1] == "Tasks":
6587 task = ele[0]
sbhangarea8e5b782018-06-21 02:10:03 -07006588 break
kasarc5bf2932018-03-09 04:15:22 -08006589 return task
6590
6591
6592 def power_on_vapp(self,vapp_id, vapp_name):
6593 """
6594 vapp_id - vApp uuid
6595 vapp_name - vAapp name
sbhangarea8e5b782018-06-21 02:10:03 -07006596 return - Task object
kasarc5bf2932018-03-09 04:15:22 -08006597 """
6598 headers = {'Accept':'application/*+xml;version=' + API_VERSION,
6599 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
sbhangarea8e5b782018-06-21 02:10:03 -07006600
kasarc5bf2932018-03-09 04:15:22 -08006601 poweron_href = "{}/api/vApp/vapp-{}/power/action/powerOn".format(self.url,
6602 vapp_id)
6603 response = self.perform_request(req_type='POST',
6604 url=poweron_href,
6605 headers=headers)
6606
6607 if response.status_code != 202:
6608 self.logger.error("REST call {} failed reason : {}"\
6609 "status code : {} ".format(poweron_href,
6610 response.content,
6611 response.status_code))
6612 raise vimconn.vimconnException("power_on_vapp() : Failed to power on "\
6613 "vApp {}".format(vapp_name))
6614 else:
6615 poweron_task = self.get_task_from_response(response.content)
6616 return poweron_task
bhangare1a0b97c2017-06-21 02:20:15 -07006617
kated47ad5f2017-08-03 02:16:13 -07006618