Signed-off-by: bayramov <mbayramov@vmware.com>
# contact with: nfvlabs@tid.es
##
# contact with: nfvlabs@tid.es
##
vimconn_vmware implementation an Abstract class in order to interact with VMware vCloud Director.
mbayramov@vmware.com
vimconn_vmware implementation an Abstract class in order to interact with VMware vCloud Director.
mbayramov@vmware.com
from progressbar import Percentage, Bar, ETA, FileTransferSpeed, ProgressBar
import vimconn
from progressbar import Percentage, Bar, ETA, FileTransferSpeed, ProgressBar
import vimconn
self.logger.setLevel(10)
if log_level:
self.logger.setLevel(10)
if log_level:
- self.logger.setLevel( getattr(logging, log_level) )
+ self.logger.setLevel(getattr(logging, log_level))
try:
self.admin_user = config['admin_username']
try:
self.admin_user = config['admin_username']
self.vca = None
if not url:
self.vca = None
if not url:
- raise TypeError, 'url param can not be NoneType'
+ raise vimconn.vimconnException('url param can not be NoneType')
if not self.url_admin: # try to use normal url
self.url_admin = self.url
if not self.url_admin: # try to use normal url
self.url_admin = self.url
# we have two case if we want to initialize VDC ID or VDC name at run time
# tenant_name provided but no tenant id
# we have two case if we want to initialize VDC ID or VDC name at run time
# tenant_name provided but no tenant id
- if self.tenant_id is None and self.tenant_name is not None and org_details_dict.has_key('vdcs'):
+ if self.tenant_id is None and self.tenant_name is not None and 'vdcs' in org_details_dict:
vdcs_dict = org_details_dict['vdcs']
for vdc in vdcs_dict:
if vdcs_dict[vdc] == self.tenant_name:
vdcs_dict = org_details_dict['vdcs']
for vdc in vdcs_dict:
if vdcs_dict[vdc] == self.tenant_name:
else:
raise vimconn.vimconnException("Tenant name indicated but not present in vcloud director.")
# case two we have tenant_id but we don't have tenant name so we find and set it.
else:
raise vimconn.vimconnException("Tenant name indicated but not present in vcloud director.")
# case two we have tenant_id but we don't have tenant name so we find and set it.
- if self.tenant_id is not None and self.tenant_name is None and org_details_dict.has_key('vdcs'):
+ if self.tenant_id is not None and self.tenant_name is None and 'vdcs' in org_details_dict:
vdcs_dict = org_details_dict['vdcs']
for vdc in vdcs_dict:
if vdc == self.tenant_id:
vdcs_dict = org_details_dict['vdcs']
for vdc in vdcs_dict:
if vdc == self.tenant_id:
"""Obtain flavor details from the VIM
Returns the flavor dict details {'id':<>, 'name':<>, other vim specific } #TODO to concrete
"""
"""Obtain flavor details from the VIM
Returns the flavor dict details {'id':<>, 'name':<>, other vim specific } #TODO to concrete
"""
- if not flavorlist.has_key(flavor_id):
+ if flavor_id not in flavorlist:
raise vimconn.vimconnNotFoundException("Flavor not found.")
return flavorlist[flavor_id]
raise vimconn.vimconnNotFoundException("Flavor not found.")
return flavorlist[flavor_id]
Returns the used id or raise an exception
"""
Returns the used id or raise an exception
"""
- if not flavorlist.has_key(flavor_id):
+ if flavor_id not in flavorlist:
raise vimconn.vimconnNotFoundException("Flavor not found.")
flavorlist.pop(flavor_id, None)
return flavor_id
def new_image(self, image_dict):
raise vimconn.vimconnNotFoundException("Flavor not found.")
flavorlist.pop(flavor_id, None)
return flavor_id
def new_image(self, image_dict):
Adds a tenant image to VIM
Returns:
200, image-id if the image is created
<0, message if there is an error
Adds a tenant image to VIM
Returns:
200, image-id if the image is created
<0, message if there is an error
return self.get_image_id_from_path(image_dict['location'])
return self.get_image_id_from_path(image_dict['location'])
:param image_id:
:return:
"""
:param image_id:
:return:
"""
raise vimconn.vimconnNotImplemented("Should have implemented this")
raise vimconn.vimconnNotImplemented("Should have implemented this")
return False
return self.catalog_exists(catalog_name, catalogs)
return False
return self.catalog_exists(catalog_name, catalogs)
+ # noinspection PyIncorrectDocstring
def upload_ovf(self, vca=None, catalog_name=None, image_name=None, media_file_name=None,
description='', progress=False, chunk_bytes=128 * 1024):
"""
Uploads a OVF file to a vCloud catalog
def upload_ovf(self, vca=None, catalog_name=None, image_name=None, media_file_name=None,
description='', progress=False, chunk_bytes=128 * 1024):
"""
Uploads a OVF file to a vCloud catalog
+ :param chunk_bytes:
+ :param progress:
+ :param description:
+ :param image_name:
+ :param vca:
:param catalog_name: (str): The name of the catalog to upload the media.
:param catalog_name: (str): The name of the catalog to upload the media.
- :param item_name: (str): The name of the media file in the catalog.
:param media_file_name: (str): The name of the local media file to upload.
:return: (bool) True if the media file was successfully uploaded, false otherwise.
"""
os.path.isfile(media_file_name)
statinfo = os.stat(media_file_name)
:param media_file_name: (str): The name of the local media file to upload.
:return: (bool) True if the media file was successfully uploaded, false otherwise.
"""
os.path.isfile(media_file_name)
statinfo = os.stat(media_file_name)
# find a catalog entry where we upload OVF.
# create vApp Template and check the status if vCD able to read OVF it will respond with appropirate
# find a catalog entry where we upload OVF.
# create vApp Template and check the status if vCD able to read OVF it will respond with appropirate
""" Method takes vdc object and vApp name and returns vapp uuid or None
Args:
""" Method takes vdc object and vApp name and returns vapp uuid or None
Args:
vdc: The VDC object.
vapp_name: is application vappp name identifier
vdc: The VDC object.
vapp_name: is application vappp name identifier
Returns:
The return True if vApp deployed
Returns:
The return True if vApp deployed
+ :param vdc:
+ :param vapp_uuid:
"""
try:
refs = filter(lambda ref:
"""
try:
refs = filter(lambda ref:
if not vca:
raise vimconn.vimconnConnectionException("self.connect() is failed.")
if not vca:
raise vimconn.vimconnConnectionException("self.connect() is failed.")
+ #new vm name = vmname + tenant_id + uuid
+ new_vm_name = [name, '-', str(uuid.uuid4())]
+ full_name = ''.join(new_vm_name)
+
# if vm already deployed we return existing uuid
# if vm already deployed we return existing uuid
- vapp_uuid = self.get_vappid(vca.get_vdc(self.tenant_name), name)
- if vapp_uuid is not None:
- return vapp_uuid
+ # vapp_uuid = self.get_vappid(vca.get_vdc(self.tenant_name), name)
+ # if vapp_uuid is not None:
+ # return vapp_uuid
# we check for presence of VDC, Catalog entry and Flavor.
vdc = vca.get_vdc(self.tenant_name)
# we check for presence of VDC, Catalog entry and Flavor.
vdc = vca.get_vdc(self.tenant_name)
catalogs = vca.get_catalogs()
if catalogs is None:
raise vimconn.vimconnNotFoundException(
catalogs = vca.get_catalogs()
if catalogs is None:
raise vimconn.vimconnNotFoundException(
- "new_vminstance(): Failed create vApp {}: Failed create vApp {}: "
- "(Failed retrieve catalog information)".format(name))
+ "new_vminstance(): Failed create vApp {}: ""(Failed retrieve catalog information)".format(name))
vm_cpus = None
vm_memory = None
vm_cpus = None
vm_memory = None
power_on = 'true'
# client must provide at least one entry in net_list if not we report error
power_on = 'true'
# client must provide at least one entry in net_list if not we report error
primary_net_name = None
if net_list is not None and len(net_list) > 0:
primary_net = net_list[0]
if primary_net is None:
raise vimconn.vimconnUnexpectedResponse("new_vminstance(): Failed network list is empty.".format(name))
primary_net_name = None
if net_list is not None and len(net_list) > 0:
primary_net = net_list[0]
if primary_net is None:
raise vimconn.vimconnUnexpectedResponse("new_vminstance(): Failed network list is empty.".format(name))
else:
try:
primary_net_id = primary_net['net_id']
else:
try:
primary_net_id = primary_net['net_id']
# use: 'data', 'bridge', 'mgmt'
# create vApp. Set vcpu and ram based on flavor id.
# use: 'data', 'bridge', 'mgmt'
# create vApp. Set vcpu and ram based on flavor id.
- vapptask = vca.create_vapp(self.tenant_name, name, templateName,
+ vapptask = vca.create_vapp(self.tenant_name, full_name, templateName,
self.get_catalogbyid(image_id, catalogs),
network_name=primary_net_name, # can be None if net_list None
network_mode='bridged',
self.get_catalogbyid(image_id, catalogs),
network_name=primary_net_name, # can be None if net_list None
network_mode='bridged',
vm_cpus=vm_cpus, # can be None if flavor is None
vm_memory=vm_memory) # can be None if flavor is None
if vapptask is None or vapptask is False:
vm_cpus=vm_cpus, # can be None if flavor is None
vm_memory=vm_memory) # can be None if flavor is None
if vapptask is None or vapptask is False:
- raise vimconn.vimconnUnexpectedResponse("new_vminstance(): failed deploy vApp {}".format(name))
+ raise vimconn.vimconnUnexpectedResponse("new_vminstance(): failed deploy vApp {}".format(full_name))
if type(vapptask) is VappTask:
vca.block_until_completed(vapptask)
# we should have now vapp in undeployed state.
if type(vapptask) is VappTask:
vca.block_until_completed(vapptask)
# we should have now vapp in undeployed state.
- vapp = vca.get_vapp(vca.get_vdc(self.tenant_name), name)
+ vapp = vca.get_vapp(vca.get_vdc(self.tenant_name), full_name)
if vapp is None:
raise vimconn.vimconnUnexpectedResponse(
if vapp is None:
raise vimconn.vimconnUnexpectedResponse(
- "new_vminstance(): Failed failed retrieve vApp {} after we deployed".format(name))
+ "new_vminstance(): Failed failed retrieve vApp {} after we deployed".format(full_name))
vca.block_until_completed(deploytask)
# check if vApp deployed and if that the case return vApp UUID otherwise -1
vca.block_until_completed(deploytask)
# check if vApp deployed and if that the case return vApp UUID otherwise -1
- vapp_uuid = self.get_vappid(vca.get_vdc(self.tenant_name), name)
+ vapp_uuid = self.get_vappid(vca.get_vdc(self.tenant_name), full_name)
if vapp_uuid is not None:
return vapp_uuid
else:
if vapp_uuid is not None:
return vapp_uuid
else:
# interfaces: …
#
def get_vminstance(self, vim_vm_uuid=None):
# interfaces: …
#
def get_vminstance(self, vim_vm_uuid=None):
- '''Returns the VM instance information from VIM'''
+ """Returns the VM instance information from VIM"""
self.logger.debug("Client requesting vm instance {} ".format(vim_vm_uuid))
vca = self.connect()
self.logger.debug("Client requesting vm instance {} ".format(vim_vm_uuid))
vca = self.connect()
'error_msg': error,
'vim_info': yaml.safe_dump(vm_info_dict), 'interfaces': []}
'error_msg': error,
'vim_info': yaml.safe_dump(vm_info_dict), 'interfaces': []}
- if vm_info_dict.has_key('interfaces'):
+ if 'interfaces' in vm_info_dict:
vm_dict['interfaces'] = vm_info_dict['interfaces']
else:
vm_dict['interfaces'] = []
vm_dict['interfaces'] = vm_info_dict['interfaces']
else:
vm_dict['interfaces'] = []
vm_info = the_vapp.get_vms_details()
vm_status = vm_info[0]['status']
vm_info = the_vapp.get_vms_details()
vm_status = vm_info[0]['status']
- vm_dict = {'status': None, 'error_msg': None, 'vim_info': None, 'interfaces': []}
- vm_dict['status'] = vcdStatusCode2manoFormat[the_vapp.me.get_status()]
- vm_dict['error_msg'] = vcdStatusCode2manoFormat[the_vapp.me.get_status()]
- vm_dict['vim_info'] = yaml.safe_dump(the_vapp.get_vms_details())
+ vm_dict = {'status': vcdStatusCode2manoFormat[the_vapp.me.get_status()],
+ 'error_msg': vcdStatusCode2manoFormat[the_vapp.me.get_status()],
+ 'vim_info': yaml.safe_dump(the_vapp.get_vms_details()), 'interfaces': []}
for vapp_network in vm_app_networks:
for vm_network in vapp_network:
if vm_network['name'] == vmname:
for vapp_network in vm_app_networks:
for vm_network in vapp_network:
if vm_network['name'] == vmname:
+ interface = {"mac_address": vm_network['mac'],
+ "vim_net_id": self.get_network_name_by_id(vm_network['network_name']),
+ "vim_interface_id": vm_network['network_name'],
+ 'ip_address': vm_network['ip']}
# interface['vim_info'] = yaml.safe_dump(vm_network)
# interface['vim_info'] = yaml.safe_dump(vm_network)
- interface["mac_address"] = vm_network['mac']
- interface["vim_net_id"] = self.get_network_name_by_id(vm_network['network_name'])
- interface["vim_interface_id"] = vm_network['network_name']
- interface['ip_address'] = vm_network['ip']
vm_dict["interfaces"].append(interface)
# add a vm to vm dict
vms_dict.setdefault(vmuuid, vm_dict)
vm_dict["interfaces"].append(interface)
# add a vm to vm dict
vms_dict.setdefault(vmuuid, vm_dict)
# NOT USED METHODS in current version
def host_vim2gui(self, host, server_dict):
# NOT USED METHODS in current version
def host_vim2gui(self, host, server_dict):
- '''Transform host dictionary from VIM format to GUI format,
+ """Transform host dictionary from VIM format to GUI format,
and append to the server_dict
and append to the server_dict
raise vimconn.vimconnNotImplemented("Should have implemented this")
def get_hosts_info(self):
raise vimconn.vimconnNotImplemented("Should have implemented this")
def get_hosts_info(self):
- '''Get the information of deployed hosts
- Returns the hosts content'''
+ """Get the information of deployed hosts
+ Returns the hosts content"""
raise vimconn.vimconnNotImplemented("Should have implemented this")
def get_hosts(self, vim_tenant):
raise vimconn.vimconnNotImplemented("Should have implemented this")
def get_hosts(self, vim_tenant):
- '''Get the hosts and deployed instances
- Returns the hosts content'''
+ """Get the hosts and deployed instances
+ Returns the hosts content"""
raise vimconn.vimconnNotImplemented("Should have implemented this")
def get_processor_rankings(self):
raise vimconn.vimconnNotImplemented("Should have implemented this")
def get_processor_rankings(self):
- '''Get the processor rankings in the VIM database'''
+ """Get the processor rankings in the VIM database"""
raise vimconn.vimconnNotImplemented("Should have implemented this")
def new_host(self, host_data):
raise vimconn.vimconnNotImplemented("Should have implemented this")
def new_host(self, host_data):
- '''Adds a new host to VIM'''
+ """Adds a new host to VIM"""
'''Returns status code of the VIM response'''
raise vimconn.vimconnNotImplemented("Should have implemented this")
def new_external_port(self, port_data):
'''Returns status code of the VIM response'''
raise vimconn.vimconnNotImplemented("Should have implemented this")
def new_external_port(self, port_data):
- '''Adds a external port to VIM'''
+ """Adds a external port to VIM"""
'''Returns the port identifier'''
raise vimconn.vimconnNotImplemented("Should have implemented this")
def new_external_network(self, net_name, net_type):
'''Returns the port identifier'''
raise vimconn.vimconnNotImplemented("Should have implemented this")
def new_external_network(self, net_name, net_type):
- '''Adds a external network to VIM (shared)'''
+ """Adds a external network to VIM (shared)"""
'''Returns the network identifier'''
raise vimconn.vimconnNotImplemented("Should have implemented this")
def connect_port_network(self, port_id, network_id, admin=False):
'''Returns the network identifier'''
raise vimconn.vimconnNotImplemented("Should have implemented this")
def connect_port_network(self, port_id, network_id, admin=False):
- '''Connects a external port to a network'''
+ """Connects a external port to a network"""
'''Returns status code of the VIM response'''
raise vimconn.vimconnNotImplemented("Should have implemented this")
def new_vminstancefromJSON(self, vm_data):
'''Returns status code of the VIM response'''
raise vimconn.vimconnNotImplemented("Should have implemented this")
def new_vminstancefromJSON(self, vm_data):
- '''Adds a VM instance to VIM'''
+ """Adds a VM instance to VIM"""
'''Returns the instance identifier'''
raise vimconn.vimconnNotImplemented("Should have implemented this")
'''Returns the instance identifier'''
raise vimconn.vimconnNotImplemented("Should have implemented this")
The return network uuid or return None
"""
The return network uuid or return None
"""
- content = self.create_network_rest(network_name=network_name,
+ new_network_name = [network_name, '-', str(uuid.uuid4())]
+ content = self.create_network_rest(network_name=''.join(new_network_name),
parent_network_uuid=parent_network_uuid,
isshared=isshared)
if content is None:
parent_network_uuid=parent_network_uuid,
isshared=isshared)
if content is None:
vca = self.connect_as_admin()
if not vca:
vca = self.connect_as_admin()
if not vca:
- raise vimconn.vimconnConnectionException("self.connect() is failed")
+ raise vimconn.vimconnConnectionException("self.connect() is failed.")
if network_name is None:
return None
if network_name is None:
return None
parsed_respond['created'] = created_section.text
network_section = xmlroot_respond.find('vm:NetworkConfigSection/vm:NetworkConfig', namespace_vm)
parsed_respond['created'] = created_section.text
network_section = xmlroot_respond.find('vm:NetworkConfigSection/vm:NetworkConfig', namespace_vm)
- if network_section is not None and network_section.attrib.has_key('networkName'):
+ if network_section is not None and 'networkName' in network_section.attrib:
parsed_respond['networkname'] = network_section.attrib['networkName']
ipscopes_section = \
parsed_respond['networkname'] = network_section.attrib['networkName']
ipscopes_section = \
parsed_respond['primarynetwork'] = adapters.text
if adapter_key == 'NetworkConnection':
vnic = {}
parsed_respond['primarynetwork'] = adapters.text
if adapter_key == 'NetworkConnection':
vnic = {}
- if adapters.attrib.has_key('network'):
+ if 'network' in adapters.attrib:
vnic['network'] = adapters.attrib['network']
for adapter in adapters:
setting_key = adapter.tag.split("}")[1]
vnic['network'] = adapters.attrib['network']
for adapter in adapters:
setting_key = adapter.tag.split("}")[1]
nic_list.append(vnic)
for link in children_section:
nic_list.append(vnic)
for link in children_section:
- if link.tag.split("}")[1] == 'Link' and link.attrib.has_key('rel'):
+ if link.tag.split("}")[1] == 'Link' and 'rel' in link.attrib:
if link.attrib['rel'] == 'screen:acquireTicket':
parsed_respond['acquireTicket'] = link.attrib
if link.attrib['rel'] == 'screen:acquireMksTicket':
if link.attrib['rel'] == 'screen:acquireTicket':
parsed_respond['acquireTicket'] = link.attrib
if link.attrib['rel'] == 'screen:acquireMksTicket':
- def acuireConsole(self, vm_uuid=None):
+ def acuire_console(self, vm_uuid=None):
vca = self.connect()
if not vca:
vca = self.connect()
if not vca:
return None
if not (not vca.vcloud_session or not vca.vcloud_session.organization):
return None
if not (not vca.vcloud_session or not vca.vcloud_session.organization):
- vm_dict = self.get_vapp_details_rest(self, vm_uuid=vm_uuid)
+ vm_dict = self.get_vapp_details_rest(self, vapp_uuid=vm_uuid)
console_dict = vm_dict['acquireTicket']
console_rest_call = console_dict['href']
console_dict = vm_dict['acquireTicket']
console_rest_call = console_dict['href']
verify=vca.verify,
logger=vca.logger)
verify=vca.verify,
logger=vca.logger)
- if response.status_code != requests.codes.ok:
- return None
+ if response.status_code == requests.codes.ok:
+ return response.content
- return response.content
+ return None
\ No newline at end of file
if vim_catalog is None:
return None
if vim_catalog is None:
return None
+ print (" Booting {} image id {} ".format(vm_name, vim_catalog))
vm_uuid = vim.new_vminstance(name=vm_name, image_id=vim_catalog)
if vm_uuid is not None and validate_uuid4(vm_uuid):
print("Image booted and vm uuid {}".format(vm_uuid))
vm_uuid = vim.new_vminstance(name=vm_name, image_id=vim_catalog)
if vm_uuid is not None and validate_uuid4(vm_uuid):
print("Image booted and vm uuid {}".format(vm_uuid))
if vapp_dict is not None:
print_vapp(vapp_dict=vapp_dict)
return True
if vapp_dict is not None:
print_vapp(vapp_dict=vapp_dict)
return True
+ except vimconn.vimconnNotFoundException as notFound:
+ print("Failed boot {} image".format(image_name))
+ print(notFound.message)
+ except vimconn.vimconnException as vimconError:
+ print("Failed boot {} image".format(image_name))
+ print(vimconError.message)
- print("Failed uploaded {} image".format(image_name))
+ print("Failed boot {} image".format(image_name))
def image_action(vim=None, action=None, namespace=None):
def image_action(vim=None, action=None, namespace=None):