X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_ro%2Fvimconn_vmware.py;h=9faf8b40044d3d6579c237e51d66869a06247303;hb=5461675ac6705ee92916ed741da1914bd2162482;hp=a33ca4e69887205574835b338639f11c118a6289;hpb=34b93bac8a6cf769b996652605a2e43c18448728;p=osm%2FRO.git diff --git a/osm_ro/vimconn_vmware.py b/osm_ro/vimconn_vmware.py index a33ca4e6..9faf8b40 100644 --- a/osm_ro/vimconn_vmware.py +++ b/osm_ro/vimconn_vmware.py @@ -357,6 +357,11 @@ class vimconnector(vimconn.vimconnector): Returns: The return vca object that letter can be used to connect to vcloud direct as admin """ + vca = self.connect() + if not vca: + raise vimconn.vimconnConnectionException("self.connect() is failed.") + + self.vca = vca try: if self.org_uuid is None: org_dict = self.get_org_list() @@ -422,9 +427,61 @@ class vimconnector(vimconn.vimconnector): raise vimconn.vimconnException("Failed create tenant {}".format(tenant_name)) def delete_tenant(self, tenant_id=None): - """Delete a tenant from VIM""" - 'Returns the tenant identifier' - raise vimconn.vimconnNotImplemented("Should have implemented this") + """ Delete a tenant from VIM + Args: + tenant_id is tenant_id to be deleted. + + Return: + returns the tenant identifier in UUID format. + If action is failed method will throw exception + """ + vca = self.connect_as_admin() + if not vca: + raise vimconn.vimconnConnectionException("self.connect() is failed") + + if tenant_id is not None: + if vca.vcloud_session and vca.vcloud_session.organization: + #Get OrgVDC + url_list = [self.vca.host, '/api/vdc/', tenant_id] + orgvdc_herf = ''.join(url_list) + response = Http.get(url=orgvdc_herf, + headers=vca.vcloud_session.get_vcloud_headers(), + verify=vca.verify, + logger=vca.logger) + + if response.status_code != requests.codes.ok: + self.logger.debug("delete_tenant():GET REST API call {} failed. "\ + "Return status code {}".format(orgvdc_herf, + response.status_code)) + raise vimconn.vimconnNotFoundException("Fail to get tenant {}".format(tenant_id)) + + lxmlroot_respond = lxmlElementTree.fromstring(response.content) + namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.iteritems() if prefix} + namespaces["xmlns"]= "http://www.vmware.com/vcloud/v1.5" + vdc_remove_href = lxmlroot_respond.find("xmlns:Link[@rel='remove']",namespaces).attrib['href'] + vdc_remove_href = vdc_remove_href + '?recursive=true&force=true' + + #Delete OrgVDC + response = Http.delete(url=vdc_remove_href, + headers=vca.vcloud_session.get_vcloud_headers(), + verify=vca.verify, + logger=vca.logger) + + if response.status_code == 202: + delete_vdc_task = taskType.parseString(response.content, True) + if type(delete_vdc_task) is GenericTask: + self.vca.block_until_completed(delete_vdc_task) + self.logger.info("Deleted tenant with ID {}".format(tenant_id)) + return tenant_id + else: + self.logger.debug("delete_tenant(): DELETE REST API call {} failed. "\ + "Return status code {}".format(vdc_remove_href, + response.status_code)) + raise vimconn.vimconnException("Fail to delete tenant with ID {}".format(tenant_id)) + else: + self.logger.debug("delete_tenant():Incorrect tenant ID {}".format(tenant_id)) + raise vimconn.vimconnNotFoundException("Fail to get tenant {}".format(tenant_id)) + def get_tenant_list(self, filter_dict={}): """Obtain tenants of VIM @@ -495,19 +552,16 @@ class vimconnector(vimconn.vimconnector): """ self.logger.debug("get_vcd_network_list(): retrieving network list for vcd {}".format(self.tenant_name)) - vca = self.connect() - if not vca: - raise vimconn.vimconnConnectionException("self.connect() is failed.") if not self.tenant_name: raise vimconn.vimconnConnectionException("Tenant name is empty.") - vdc = vca.get_vdc(self.tenant_name) + vdc = self.get_vdc_details() if vdc is None: raise vimconn.vimconnConnectionException("Can't retrieve information for a VDC {}".format(self.tenant_name)) vdc_uuid = vdc.get_id().split(":")[3] - networks = vca.get_networks(vdc.get_name()) + networks = self.vca.get_networks(vdc.get_name()) network_list = [] try: for network in networks: @@ -553,21 +607,18 @@ class vimconnector(vimconn.vimconnector): List can be empty """ - self.logger.debug("get_vcd_network_list(): retrieving network list for vcd {}".format(self.tenant_name)) - vca = self.connect() - if not vca: - raise vimconn.vimconnConnectionException("self.connect() is failed.") + self.logger.debug("get_network_list(): retrieving network list for vcd {}".format(self.tenant_name)) if not self.tenant_name: raise vimconn.vimconnConnectionException("Tenant name is empty.") - vdc = vca.get_vdc(self.tenant_name) + vdc = self.get_vdc_details() if vdc is None: raise vimconn.vimconnConnectionException("Can't retrieve information for a VDC {}.".format(self.tenant_name)) try: vdcid = vdc.get_id().split(":")[3] - networks = vca.get_networks(vdc.get_name()) + networks = self.vca.get_networks(vdc.get_name()) network_list = [] for network in networks: @@ -613,15 +664,11 @@ class vimconnector(vimconn.vimconnector): """Method obtains network details of net_id VIM network Return a dict with the fields at filter_dict (see get_network_list) plus some VIM specific>}, ...]""" - vca = self.connect() - if not vca: - raise vimconn.vimconnConnectionException("self.connect() is failed") - try: - vdc = vca.get_vdc(self.tenant_name) + vdc = self.get_vdc_details() vdc_id = vdc.get_id().split(":")[3] - networks = vca.get_networks(vdc.get_name()) + networks = self.vca.get_networks(vdc.get_name()) filter_dict = {} for network in networks: @@ -652,10 +699,6 @@ class vimconnector(vimconn.vimconnector): Returns the network identifier or raise an exception """ - vca = self.connect() - if not vca: - raise vimconn.vimconnConnectionException("self.connect() for tenant {} is failed.".format(self.tenant_name)) - # ############# Stub code for SRIOV ################# # dvport_group = self.get_dvport_group(net_id) # if dvport_group: @@ -693,10 +736,6 @@ class vimconnector(vimconn.vimconnector): """ - vca = self.connect() - if not vca: - raise vimconn.vimconnConnectionException("self.connect() is failed") - dict_entry = {} try: for net in net_list: @@ -754,6 +793,13 @@ class vimconnector(vimconn.vimconnector): cpu = flavor_data.get(FLAVOR_VCPUS_KEY, 1) disk = flavor_data.get(FLAVOR_DISK_KEY, 1) + if not isinstance(ram, int): + raise vimconn.vimconnException("Non-integer value for ram") + elif not isinstance(cpu, int): + raise vimconn.vimconnException("Non-integer value for cpu") + elif not isinstance(disk, int): + raise vimconn.vimconnException("Non-integer value for disk") + extended_flv = flavor_data.get("extended") if extended_flv: numas=extended_flv.get("numas") @@ -801,12 +847,81 @@ class vimconnector(vimconn.vimconnector): def delete_image(self, image_id): """ - - :param image_id: - :return: + Deletes a tenant image from VIM + Args: + image_id is ID of Image to be deleted + Return: + returns the image identifier in UUID format or raises an exception on error """ + vca = self.connect_as_admin() + if not vca: + raise vimconn.vimconnConnectionException("self.connect() is failed") + # Get Catalog details + url_list = [self.vca.host, '/api/catalog/', image_id] + catalog_herf = ''.join(url_list) + response = Http.get(url=catalog_herf, + headers=vca.vcloud_session.get_vcloud_headers(), + verify=vca.verify, + logger=vca.logger) + + if response.status_code != requests.codes.ok: + self.logger.debug("delete_image():GET REST API call {} failed. "\ + "Return status code {}".format(catalog_herf, + response.status_code)) + raise vimconn.vimconnNotFoundException("Fail to get image {}".format(image_id)) + + lxmlroot_respond = lxmlElementTree.fromstring(response.content) + namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.iteritems() if prefix} + namespaces["xmlns"]= "http://www.vmware.com/vcloud/v1.5" + + catalogItems_section = lxmlroot_respond.find("xmlns:CatalogItems",namespaces) + catalogItems = catalogItems_section.iterfind("xmlns:CatalogItem",namespaces) + for catalogItem in catalogItems: + catalogItem_href = catalogItem.attrib['href'] + + #GET details of catalogItem + response = Http.get(url=catalogItem_href, + headers=vca.vcloud_session.get_vcloud_headers(), + verify=vca.verify, + logger=vca.logger) + + if response.status_code != requests.codes.ok: + self.logger.debug("delete_image():GET REST API call {} failed. "\ + "Return status code {}".format(catalog_herf, + response.status_code)) + raise vimconn.vimconnNotFoundException("Fail to get catalogItem {} for catalog {}".format( + catalogItem, + image_id)) + + lxmlroot_respond = lxmlElementTree.fromstring(response.content) + namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.iteritems() if prefix} + namespaces["xmlns"]= "http://www.vmware.com/vcloud/v1.5" + catalogitem_remove_href = lxmlroot_respond.find("xmlns:Link[@rel='remove']",namespaces).attrib['href'] + + #Remove catalogItem + response = Http.delete(url= catalogitem_remove_href, + headers=vca.vcloud_session.get_vcloud_headers(), + verify=vca.verify, + logger=vca.logger) + if response.status_code == requests.codes.no_content: + self.logger.debug("Deleted Catalog item {}".format(catalogItem)) + else: + raise vimconn.vimconnException("Fail to delete Catalog Item {}".format(catalogItem)) + + #Remove catalog + url_list = [self.vca.host, '/api/admin/catalog/', image_id] + catalog_remove_herf = ''.join(url_list) + response = Http.delete(url= catalog_remove_herf, + headers=vca.vcloud_session.get_vcloud_headers(), + verify=vca.verify, + logger=vca.logger) + + if response.status_code == requests.codes.no_content: + self.logger.debug("Deleted Catalog {}".format(image_id)) + return image_id + else: + raise vimconn.vimconnException("Fail to delete Catalog {}".format(image_id)) - raise vimconn.vimconnNotImplemented("Should have implemented this") def catalog_exists(self, catalog_name, catalogs): """ @@ -1077,9 +1192,6 @@ class vimconnector(vimconn.vimconnector): Return: if image uploaded correct method will provide image catalog UUID. """ - vca = self.connect() - if not vca: - raise vimconn.vimconnConnectionException("self.connect() is failed.") if not path: raise vimconn.vimconnException("Image path can't be None.") @@ -1104,21 +1216,21 @@ class vimconnector(vimconn.vimconnector): "vdc catalog name {}".format(filename, catalog_name, path, catalog_md5_name)) try: - catalogs = vca.get_catalogs() + catalogs = self.vca.get_catalogs() except Exception as exp: self.logger.debug("Failed get catalogs() with Exception {} ".format(exp)) raise vimconn.vimconnException("Failed get catalogs() with Exception {} ".format(exp)) if len(catalogs) == 0: self.logger.info("Creating a new catalog entry {} in vcloud director".format(catalog_name)) - result = self.create_vimcatalog(vca, catalog_md5_name) + result = self.create_vimcatalog(self.vca, catalog_md5_name) if not result: raise vimconn.vimconnException("Failed create new catalog {} ".format(catalog_md5_name)) - result = self.upload_vimimage(vca=vca, catalog_name=catalog_md5_name, + result = self.upload_vimimage(vca=self.vca, catalog_name=catalog_md5_name, media_name=filename, medial_file_name=path, progress=progress) if not result: raise vimconn.vimconnException("Failed create vApp template for catalog {} ".format(catalog_name)) - return self.get_catalogid(catalog_name, vca.get_catalogs()) + return self.get_catalogid(catalog_name, self.vca.get_catalogs()) else: for catalog in catalogs: # search for existing catalog if we find same name we return ID @@ -1127,20 +1239,20 @@ class vimconnector(vimconn.vimconnector): self.logger.debug("Found existing catalog entry for {} " "catalog id {}".format(catalog_name, self.get_catalogid(catalog_md5_name, catalogs))) - return self.get_catalogid(catalog_md5_name, vca.get_catalogs()) + return self.get_catalogid(catalog_md5_name, self.vca.get_catalogs()) # if we didn't find existing catalog we create a new one and upload image. self.logger.debug("Creating new catalog entry {} - {}".format(catalog_name, catalog_md5_name)) - result = self.create_vimcatalog(vca, catalog_md5_name) + result = self.create_vimcatalog(self.vca, catalog_md5_name) if not result: raise vimconn.vimconnException("Failed create new catalog {} ".format(catalog_md5_name)) - result = self.upload_vimimage(vca=vca, catalog_name=catalog_md5_name, + result = self.upload_vimimage(vca=self.vca, catalog_name=catalog_md5_name, media_name=filename, medial_file_name=path, progress=progress) if not result: raise vimconn.vimconnException("Failed create vApp template for catalog {} ".format(catalog_md5_name)) - return self.get_catalogid(catalog_md5_name, vca.get_catalogs()) + return self.get_catalogid(catalog_md5_name, self.vca.get_catalogs()) def get_image_list(self, filter_dict={}): '''Obtain tenant images from VIM @@ -1153,12 +1265,10 @@ class vimconnector(vimconn.vimconnector): [{}, ...] List can be empty ''' - vca = self.connect() - if not vca: - raise vimconn.vimconnConnectionException("self.connect() is failed.") + try: image_list = [] - catalogs = vca.get_catalogs() + catalogs = self.vca.get_catalogs() if len(catalogs) == 0: return image_list else: @@ -1229,7 +1339,7 @@ class vimconnector(vimconn.vimconnector): return False return False - def get_namebyvappid(self, vca=None, vdc=None, vapp_uuid=None): + def get_namebyvappid(self, vdc=None, vapp_uuid=None): """Method returns vApp name from vCD and lookup done by vapp_id. Args: @@ -1248,8 +1358,13 @@ class vimconnector(vimconn.vimconnector): # we care only about UUID the rest doesn't matter vappid = ref.href.split("vapp")[1][1:] if vappid == vapp_uuid: - response = Http.get(ref.href, headers=vca.vcloud_session.get_vcloud_headers(), verify=vca.verify, + response = Http.get(ref.href, headers=self.vca.vcloud_session.get_vcloud_headers(), verify=self.vca.verify, logger=self.logger) + + #Retry login if session expired & retry sending request + if response.status_code == 403: + response = self.retry_rest('GET', ref.href) + tree = XmlElementTree.fromstring(response.content) return tree.attrib['name'] except Exception as e: @@ -1258,7 +1373,7 @@ class vimconnector(vimconn.vimconnector): return None def new_vminstance(self, name=None, description="", start=False, image_id=None, flavor_id=None, net_list={}, - cloud_config=None, disk_list=None): + cloud_config=None, disk_list=None, availability_zone_index=None, availability_zone_list=None): """Adds a VM instance to VIM Params: start: indicates if VM must start or boot in pause mode. Ignored @@ -1284,9 +1399,6 @@ class vimconnector(vimconn.vimconnector): self.logger.info("Creating new instance for entry {}".format(name)) self.logger.debug("desc {} boot {} image_id: {} flavor_id: {} net_list: {} cloud_config {} disk_list {}".format( description, start, image_id, flavor_id, net_list, cloud_config, disk_list)) - vca = self.connect() - if not vca: - raise vimconn.vimconnConnectionException("self.connect() is failed.") #new vm name = vmname + tenant_id + uuid new_vm_name = [name, '-', str(uuid.uuid4())] @@ -1298,11 +1410,15 @@ class vimconnector(vimconn.vimconnector): # return vapp_uuid # we check for presence of VDC, Catalog entry and Flavor. - vdc = vca.get_vdc(self.tenant_name) + vdc = self.get_vdc_details() if vdc is None: raise vimconn.vimconnNotFoundException( "new_vminstance(): Failed create vApp {}: (Failed retrieve VDC information)".format(name)) - catalogs = vca.get_catalogs() + catalogs = self.vca.get_catalogs() + if catalogs is None: + #Retry once, if failed by refreshing token + self.get_token() + catalogs = self.vca.get_catalogs() if catalogs is None: raise vimconn.vimconnNotFoundException( "new_vminstance(): Failed create vApp {}: (Failed retrieve catalogs list)".format(name)) @@ -1319,6 +1435,7 @@ class vimconnector(vimconn.vimconnector): vm_cpus = None vm_memory = None vm_disk = None + numas = None if flavor_id is not None: if flavor_id not in vimconnector.flavorlist: @@ -1371,19 +1488,26 @@ class vimconnector(vimconn.vimconnector): # use: 'data', 'bridge', 'mgmt' # create vApp. Set vcpu and ram based on flavor id. try: - vapptask = vca.create_vapp(self.tenant_name, vmname_andid, templateName, - self.get_catalogbyid(image_id, catalogs), - network_name=None, # None while creating vapp - network_mode=network_mode, - vm_name=vmname_andid, - vm_cpus=vm_cpus, # can be None if flavor is None - vm_memory=vm_memory) # can be None if flavor is None + for retry in (1,2): + vapptask = self.vca.create_vapp(self.tenant_name, vmname_andid, templateName, + self.get_catalogbyid(image_id, catalogs), + network_name=None, # None while creating vapp + network_mode=network_mode, + vm_name=vmname_andid, + vm_cpus=vm_cpus, # can be None if flavor is None + vm_memory=vm_memory) # can be None if flavor is None + + if not vapptask and retry==1: + self.get_token() # Retry getting token + continue + else: + break if vapptask is None or vapptask is False: raise vimconn.vimconnUnexpectedResponse( "new_vminstance(): failed to create vApp {}".format(vmname_andid)) if type(vapptask) is VappTask: - vca.block_until_completed(vapptask) + self.vca.block_until_completed(vapptask) except Exception as exp: raise vimconn.vimconnUnexpectedResponse( @@ -1391,14 +1515,14 @@ class vimconnector(vimconn.vimconnector): # we should have now vapp in undeployed state. try: - vapp = vca.get_vapp(vca.get_vdc(self.tenant_name), vmname_andid) - vapp_uuid = self.get_vappid(vca.get_vdc(self.tenant_name), vmname_andid) + vapp_uuid = self.get_vappid(self.get_vdc_details(), vmname_andid) + except Exception as exp: raise vimconn.vimconnUnexpectedResponse( "new_vminstance(): Failed to retrieve vApp {} after creation: Exception:{}" .format(vmname_andid, exp)) - if vapp is None: + if vapp_uuid is None: raise vimconn.vimconnUnexpectedResponse( "new_vminstance(): Failed to retrieve vApp {} after creation".format( vmname_andid)) @@ -1433,6 +1557,8 @@ class vimconnector(vimconn.vimconnector): pci_devices_info, vmname_andid) ) + + vapp = self.vca.get_vapp(self.get_vdc_details(), vmname_andid) # Modify vm disk if vm_disk: #Assuming there is only one disk in ovf and fast provisioning in organization vDC is disabled @@ -1464,7 +1590,7 @@ class vimconnector(vimconn.vimconnector): if added_existing_disk: time.sleep(5) added_existing_disk = False - self.add_new_disk(vca, vapp_uuid, disk['size']) + self.add_new_disk(vapp_uuid, disk['size']) if numas: # Assigning numa affinity setting @@ -1500,12 +1626,14 @@ class vimconnector(vimconn.vimconnector): - NONE (No IP addressing mode specified.)""" if primary_netname is not None: - nets = filter(lambda n: n.name == interface_net_name, vca.get_networks(self.tenant_name)) + nets = filter(lambda n: n.name == interface_net_name, self.vca.get_networks(self.tenant_name)) if len(nets) == 1: self.logger.info("new_vminstance(): Found requested network: {}".format(nets[0].name)) + + vapp = self.vca.get_vapp(self.get_vdc_details(), vmname_andid) task = vapp.connect_to_network(nets[0].name, nets[0].href) if type(task) is GenericTask: - vca.block_until_completed(task) + self.vca.block_until_completed(task) # connect network to VM - with all DHCP by default type_list = ['PF','VF','VFnotShared'] @@ -1529,6 +1657,7 @@ class vimconnector(vimconn.vimconnector): net) nicIndex += 1 + vapp = self.vca.get_vapp(self.get_vdc_details(), vmname_andid) # cloud-init for ssh-key injection if cloud_config: self.cloud_init(vapp,cloud_config) @@ -1537,7 +1666,7 @@ class vimconnector(vimconn.vimconnector): self.logger.debug("new_vminstance(): Deploying vApp {} ".format(name)) deploytask = vapp.deploy(powerOn=False) if type(deploytask) is GenericTask: - vca.block_until_completed(deploytask) + self.vca.block_until_completed(deploytask) # ############# Stub code for SRIOV ################# #Add SRIOV @@ -1574,28 +1703,32 @@ class vimconnector(vimconn.vimconnector): str(memReserve),str(vm_obj))) self.logger.debug("new_vminstance(): power on vApp {} ".format(name)) + + vapp = self.vca.get_vapp(self.get_vdc_details(), vmname_andid) poweron_task = vapp.poweron() if type(poweron_task) is GenericTask: - vca.block_until_completed(poweron_task) + self.vca.block_until_completed(poweron_task) except Exception as exp : # it might be a case if specific mandatory entry in dict is empty or some other pyVcloud exception - self.logger.debug("new_vminstance(): Failed create new vm instance {}".format(name, exp)) - raise vimconn.vimconnException("new_vminstance(): Failed create new vm instance {}".format(name, exp)) + self.logger.debug("new_vminstance(): Failed create new vm instance {} with exception {}" + .format(name, exp)) + raise vimconn.vimconnException("new_vminstance(): Failed create new vm instance {} with exception {}" + .format(name, exp)) # check if vApp deployed and if that the case return vApp UUID otherwise -1 wait_time = 0 vapp_uuid = None while wait_time <= MAX_WAIT_TIME: try: - vapp = vca.get_vapp(vca.get_vdc(self.tenant_name), vmname_andid) + vapp = self.vca.get_vapp(self.get_vdc_details(), vmname_andid) except Exception as exp: raise vimconn.vimconnUnexpectedResponse( "new_vminstance(): Failed to retrieve vApp {} after creation: Exception:{}" .format(vmname_andid, exp)) if vapp and vapp.me.deployed: - vapp_uuid = self.get_vappid(vca.get_vdc(self.tenant_name), vmname_andid) + vapp_uuid = self.get_vappid(self.get_vdc_details(), vmname_andid) break else: self.logger.debug("new_vminstance(): Wait for vApp {} to deploy".format(name)) @@ -1627,11 +1760,8 @@ class vimconnector(vimconn.vimconnector): """Returns the VM instance information from VIM""" self.logger.debug("Client requesting vm instance {} ".format(vim_vm_uuid)) - vca = self.connect() - if not vca: - raise vimconn.vimconnConnectionException("self.connect() is failed.") - vdc = vca.get_vdc(self.tenant_name) + vdc = self.get_vdc_details() if vdc is None: raise vimconn.vimconnConnectionException( "Failed to get a reference of VDC for a tenant {}".format(self.tenant_name)) @@ -1676,11 +1806,8 @@ class vimconnector(vimconn.vimconnector): """ self.logger.debug("Client requesting delete vm instance {} ".format(vm__vim_uuid)) - vca = self.connect() - if not vca: - raise vimconn.vimconnConnectionException("self.connect() is failed.") - vdc = vca.get_vdc(self.tenant_name) + vdc = self.get_vdc_details() if vdc is None: self.logger.debug("delete_vminstance(): Failed to get a reference of VDC for a tenant {}".format( self.tenant_name)) @@ -1688,7 +1815,7 @@ class vimconnector(vimconn.vimconnector): "delete_vminstance(): Failed to get a reference of VDC for a tenant {}".format(self.tenant_name)) try: - vapp_name = self.get_namebyvappid(vca, vdc, vm__vim_uuid) + vapp_name = self.get_namebyvappid(vdc, vm__vim_uuid) if vapp_name is None: self.logger.debug("delete_vminstance(): Failed to get vm by given {} vm uuid".format(vm__vim_uuid)) return -1, "delete_vminstance(): Failed to get vm by given {} vm uuid".format(vm__vim_uuid) @@ -1696,7 +1823,7 @@ class vimconnector(vimconn.vimconnector): self.logger.info("Deleting vApp {} and UUID {}".format(vapp_name, vm__vim_uuid)) # Delete vApp and wait for status change if task executed and vApp is None. - vapp = vca.get_vapp(vca.get_vdc(self.tenant_name), vapp_name) + vapp = self.vca.get_vapp(self.get_vdc_details(), vapp_name) if vapp: if vapp.me.deployed: @@ -1705,14 +1832,14 @@ class vimconnector(vimconn.vimconnector): powered_off = False wait_time = 0 while wait_time <= MAX_WAIT_TIME: - vapp = vca.get_vapp(vca.get_vdc(self.tenant_name), vapp_name) + vapp = self.vca.get_vapp(self.get_vdc_details(), vapp_name) if not vapp: self.logger.debug("delete_vminstance(): Failed to get vm by given {} vm uuid".format(vm__vim_uuid)) return -1, "delete_vminstance(): Failed to get vm by given {} vm uuid".format(vm__vim_uuid) power_off_task = vapp.poweroff() if type(power_off_task) is GenericTask: - result = vca.block_until_completed(power_off_task) + result = self.vca.block_until_completed(power_off_task) if result: powered_off = True break @@ -1731,14 +1858,14 @@ class vimconnector(vimconn.vimconnector): wait_time = 0 undeployed = False while wait_time <= MAX_WAIT_TIME: - vapp = vca.get_vapp(vca.get_vdc(self.tenant_name), vapp_name) + vapp = self.vca.get_vapp(self.get_vdc_details(), vapp_name) if not vapp: self.logger.debug("delete_vminstance(): Failed to get vm by given {} vm uuid".format(vm__vim_uuid)) return -1, "delete_vminstance(): Failed to get vm by given {} vm uuid".format(vm__vim_uuid) undeploy_task = vapp.undeploy(action='powerOff') if type(undeploy_task) is GenericTask: - result = vca.block_until_completed(undeploy_task) + result = self.vca.block_until_completed(undeploy_task) if result: undeployed = True break @@ -1753,14 +1880,14 @@ class vimconnector(vimconn.vimconnector): # delete vapp self.logger.info("Start deletion of vApp {} ".format(vapp_name)) - vapp = vca.get_vapp(vca.get_vdc(self.tenant_name), vapp_name) + vapp = self.vca.get_vapp(self.get_vdc_details(), vapp_name) if vapp is not None: wait_time = 0 result = False while wait_time <= MAX_WAIT_TIME: - vapp = vca.get_vapp(vca.get_vdc(self.tenant_name), vapp_name) + vapp = self.vca.get_vapp(self.get_vdc_details(), vapp_name) if not vapp: self.logger.debug("delete_vminstance(): Failed to get vm by given {} vm uuid".format(vm__vim_uuid)) return -1, "delete_vminstance(): Failed to get vm by given {} vm uuid".format(vm__vim_uuid) @@ -1768,8 +1895,8 @@ class vimconnector(vimconn.vimconnector): delete_task = vapp.delete() if type(delete_task) is GenericTask: - vca.block_until_completed(delete_task) - result = vca.block_until_completed(delete_task) + self.vca.block_until_completed(delete_task) + result = self.vca.block_until_completed(delete_task) if result: break else: @@ -1785,7 +1912,7 @@ class vimconnector(vimconn.vimconnector): self.logger.debug(traceback.format_exc()) raise vimconn.vimconnException("delete_vminstance(): Failed delete vm instance {}".format(vm__vim_uuid)) - if vca.get_vapp(vca.get_vdc(self.tenant_name), vapp_name) is None: + if self.vca.get_vapp(self.get_vdc_details(), vapp_name) is None: self.logger.info("Deleted vm instance {} sccessfully".format(vm__vim_uuid)) return vm__vim_uuid else: @@ -1817,25 +1944,21 @@ class vimconnector(vimconn.vimconnector): self.logger.debug("Client requesting refresh vm status for {} ".format(vm_list)) - vca = self.connect() - if not vca: - raise vimconn.vimconnConnectionException("self.connect() is failed.") - - vdc = vca.get_vdc(self.tenant_name) + vdc = self.get_vdc_details() if vdc is None: raise vimconn.vimconnException("Failed to get a reference of VDC for a tenant {}".format(self.tenant_name)) vms_dict = {} nsx_edge_list = [] for vmuuid in vm_list: - vmname = self.get_namebyvappid(vca, vdc, vmuuid) + vmname = self.get_namebyvappid(self.get_vdc_details(), vmuuid) if vmname is not None: try: - the_vapp = vca.get_vapp(vdc, vmname) + vm_pci_details = self.get_vm_pci_details(vmuuid) + the_vapp = self.vca.get_vapp(self.get_vdc_details(), vmname) vm_info = the_vapp.get_vms_details() vm_status = vm_info[0]['status'] - vm_pci_details = self.get_vm_pci_details(vmuuid) vm_info[0].update(vm_pci_details) vm_dict = {'status': vcdStatusCode2manoFormat[the_vapp.me.get_status()], @@ -1984,15 +2107,11 @@ class vimconnector(vimconn.vimconnector): if vm__vim_uuid is None or action_dict is None: raise vimconn.vimconnException("Invalid request. VM id or action is None.") - vca = self.connect() - if not vca: - raise vimconn.vimconnConnectionException("self.connect() is failed.") - - vdc = vca.get_vdc(self.tenant_name) + vdc = self.get_vdc_details() if vdc is None: return -1, "Failed to get a reference of VDC for a tenant {}".format(self.tenant_name) - vapp_name = self.get_namebyvappid(vca, vdc, vm__vim_uuid) + vapp_name = self.get_namebyvappid(vdc, vm__vim_uuid) if vapp_name is None: self.logger.debug("action_vminstance(): Failed to get vm by given {} vm uuid".format(vm__vim_uuid)) raise vimconn.vimconnException("Failed to get vm by given {} vm uuid".format(vm__vim_uuid)) @@ -2000,7 +2119,7 @@ class vimconnector(vimconn.vimconnector): self.logger.info("Action_vminstance vApp {} and UUID {}".format(vapp_name, vm__vim_uuid)) try: - the_vapp = vca.get_vapp(vdc, vapp_name) + the_vapp = self.vca.get_vapp(vdc, vapp_name) # TODO fix all status if "start" in action_dict: vm_info = the_vapp.get_vms_details() @@ -2008,28 +2127,28 @@ class vimconnector(vimconn.vimconnector): self.logger.info("action_vminstance: Power on vApp: {}".format(vapp_name)) if vm_status == "Suspended" or vm_status == "Powered off": power_on_task = the_vapp.poweron() - result = vca.block_until_completed(power_on_task) + result = self.vca.block_until_completed(power_on_task) self.instance_actions_result("start", result, vapp_name) elif "rebuild" in action_dict: self.logger.info("action_vminstance: Rebuild vApp: {}".format(vapp_name)) rebuild_task = the_vapp.deploy(powerOn=True) - result = vca.block_until_completed(rebuild_task) + result = self.vca.block_until_completed(rebuild_task) self.instance_actions_result("rebuild", result, vapp_name) elif "pause" in action_dict: self.logger.info("action_vminstance: pause vApp: {}".format(vapp_name)) pause_task = the_vapp.undeploy(action='suspend') - result = vca.block_until_completed(pause_task) + result = self.vca.block_until_completed(pause_task) self.instance_actions_result("pause", result, vapp_name) elif "resume" in action_dict: self.logger.info("action_vminstance: resume vApp: {}".format(vapp_name)) power_task = the_vapp.poweron() - result = vca.block_until_completed(power_task) + result = self.vca.block_until_completed(power_task) self.instance_actions_result("resume", result, vapp_name) elif "shutoff" in action_dict or "shutdown" in action_dict: action_name , value = action_dict.items()[0] self.logger.info("action_vminstance: {} vApp: {}".format(action_name, vapp_name)) power_off_task = the_vapp.undeploy(action='powerOff') - result = vca.block_until_completed(power_off_task) + result = self.vca.block_until_completed(power_off_task) if action_name == "shutdown": self.instance_actions_result("shutdown", result, vapp_name) else: @@ -2126,10 +2245,6 @@ class vimconnector(vimconn.vimconnector): The return network name. """ - vca = self.connect() - if not vca: - raise vimconn.vimconnConnectionException("self.connect() is failed.") - if not network_uuid: return None @@ -2156,10 +2271,6 @@ class vimconnector(vimconn.vimconnector): network_uuid: network_id """ - vca = self.connect() - if not vca: - raise vimconn.vimconnConnectionException("self.connect() is failed.") - if not network_name: self.logger.debug("get_network_id_by_name() : Network name is empty") return None @@ -2189,18 +2300,18 @@ class vimconnector(vimconn.vimconnector): The return XML respond """ - vca = self.connect() - if not vca: - raise vimconn.vimconnConnectionException("self.connect() is failed") - - url_list = [vca.host, '/api/org'] + url_list = [self.vca.host, '/api/org'] vm_list_rest_call = ''.join(url_list) - if not (not vca.vcloud_session or not vca.vcloud_session.organization): + if not (not self.vca.vcloud_session or not self.vca.vcloud_session.organization): response = Http.get(url=vm_list_rest_call, - headers=vca.vcloud_session.get_vcloud_headers(), - verify=vca.verify, - logger=vca.logger) + headers=self.vca.vcloud_session.get_vcloud_headers(), + verify=self.vca.verify, + logger=self.vca.logger) + + if response.status_code == 403: + response = self.retry_rest('GET', vm_list_rest_call) + if response.status_code == requests.codes.ok: return response.content @@ -2218,21 +2329,22 @@ class vimconnector(vimconn.vimconnector): The return XML respond """ - vca = self.connect() - if not vca: - raise vimconn.vimconnConnectionException("self.connect() is failed") - if org_uuid is None: return None - url_list = [vca.host, '/api/org/', org_uuid] + url_list = [self.vca.host, '/api/org/', org_uuid] vm_list_rest_call = ''.join(url_list) - if not (not vca.vcloud_session or not vca.vcloud_session.organization): + if not (not self.vca.vcloud_session or not self.vca.vcloud_session.organization): response = Http.get(url=vm_list_rest_call, - headers=vca.vcloud_session.get_vcloud_headers(), - verify=vca.verify, - logger=vca.logger) + headers=self.vca.vcloud_session.get_vcloud_headers(), + verify=self.vca.verify, + logger=self.vca.logger) + + #Retry login if session expired & retry sending request + if response.status_code == 403: + response = self.retry_rest('GET', vm_list_rest_call) + if response.status_code == requests.codes.ok: return response.content @@ -2253,9 +2365,6 @@ class vimconnector(vimconn.vimconnector): """ org_dict = {} - vca = self.connect() - if not vca: - raise vimconn.vimconnConnectionException("self.connect() is failed") if org_uuid is None: return org_dict @@ -2293,9 +2402,6 @@ class vimconnector(vimconn.vimconnector): """ org_dict = {} - vca = self.connect() - if not vca: - raise vimconn.vimconnConnectionException("self.connect() is failed") content = self.list_org_action() try: @@ -2467,21 +2573,22 @@ class vimconnector(vimconn.vimconnector): The return XML respond """ - vca = self.connect() - if not vca: - raise vimconn.vimconnConnectionException("self.connect() is failed") - if network_uuid is None: return None - url_list = [vca.host, '/api/network/', network_uuid] + url_list = [self.vca.host, '/api/network/', network_uuid] vm_list_rest_call = ''.join(url_list) - if not (not vca.vcloud_session or not vca.vcloud_session.organization): + if not (not self.vca.vcloud_session or not self.vca.vcloud_session.organization): response = Http.get(url=vm_list_rest_call, - headers=vca.vcloud_session.get_vcloud_headers(), - verify=vca.verify, - logger=vca.logger) + headers=self.vca.vcloud_session.get_vcloud_headers(), + verify=self.vca.verify, + logger=self.vca.logger) + + #Retry login if session expired & retry sending request + if response.status_code == 403: + response = self.retry_rest('GET', vm_list_rest_call) + if response.status_code == requests.codes.ok: return response.content @@ -2906,8 +3013,7 @@ class vimconnector(vimconn.vimconnector): # application/vnd.vmware.admin.providervdc+xml # we need find a template from witch we instantiate VDC if child.tag.split("}")[1] == 'VdcTemplate': - if child.attrib.get('type') == 'application/vnd.vmware.admin.vdcTemplate+xml' and child.attrib.get( - 'name') == 'openmano': + if child.attrib.get('type') == 'application/vnd.vmware.admin.vdcTemplate+xml': vdc_template_ref = child.attrib.get('href') except: self.logger.debug("Failed parse respond for rest api call {}".format(vm_list_rest_call)) @@ -2930,6 +3036,11 @@ class vimconnector(vimconn.vimconnector): headers['Content-Type'] = 'application/vnd.vmware.vcloud.instantiateVdcTemplateParams+xml' response = Http.post(url=vm_list_rest_call, headers=headers, data=data, verify=vca.verify, logger=vca.logger) + + vdc_task = taskType.parseString(response.content, True) + if type(vdc_task) is GenericTask: + self.vca.block_until_completed(vdc_task) + # if we all ok we respond with content otherwise by default None if response.status_code >= 200 and response.status_code < 300: return response.content @@ -3044,7 +3155,7 @@ class vimconnector(vimconn.vimconnector): if need_admin_access: vca = self.connect_as_admin() else: - vca = self.connect() + vca = self.vca if not vca: raise vimconn.vimconnConnectionException("self.connect() is failed") @@ -3060,6 +3171,10 @@ class vimconnector(vimconn.vimconnector): verify=vca.verify, logger=vca.logger) + if response.status_code == 403: + if need_admin_access == False: + response = self.retry_rest('GET', get_vapp_restcall) + if response.status_code != requests.codes.ok: self.logger.debug("REST API call {} failed. Return status code {}".format(get_vapp_restcall, response.status_code)) @@ -3166,21 +3281,20 @@ class vimconnector(vimconn.vimconnector): def acuire_console(self, vm_uuid=None): - vca = self.connect() - if not vca: - raise vimconn.vimconnConnectionException("self.connect() is failed") if vm_uuid is None: return None - if not (not vca.vcloud_session or not vca.vcloud_session.organization): + if not (not self.vca.vcloud_session or not self.vca.vcloud_session.organization): vm_dict = self.get_vapp_details_rest(self, vapp_uuid=vm_uuid) console_dict = vm_dict['acquireTicket'] console_rest_call = console_dict['href'] response = Http.post(url=console_rest_call, - headers=vca.vcloud_session.get_vcloud_headers(), - verify=vca.verify, - logger=vca.logger) + headers=self.vca.vcloud_session.get_vcloud_headers(), + verify=self.vca.verify, + logger=self.vca.logger) + if response.status_code == 403: + response = self.retry_rest('POST', console_rest_call) if response.status_code == requests.codes.ok: return response.content @@ -3237,17 +3351,17 @@ class vimconnector(vimconn.vimconnector): Returns: The return network uuid or return None """ - vca = self.connect() - if not vca: - raise vimconn.vimconnConnectionException("self.connect() is failed") if disk_href is None or disk_size is None: return None - if vca.vcloud_session and vca.vcloud_session.organization: + if self.vca.vcloud_session and self.vca.vcloud_session.organization: response = Http.get(url=disk_href, - headers=vca.vcloud_session.get_vcloud_headers(), - verify=vca.verify, - logger=vca.logger) + headers=self.vca.vcloud_session.get_vcloud_headers(), + verify=self.vca.verify, + logger=self.vca.logger) + + if response.status_code == 403: + response = self.retry_rest('GET', disk_href) if response.status_code != requests.codes.ok: self.logger.debug("GET REST API call {} failed. Return status code {}".format(disk_href, @@ -3269,13 +3383,17 @@ class vimconnector(vimconn.vimconnector): xml_declaration=True) #Send PUT request to modify disk size - headers = vca.vcloud_session.get_vcloud_headers() + headers = self.vca.vcloud_session.get_vcloud_headers() headers['Content-Type'] = 'application/vnd.vmware.vcloud.rasdItemsList+xml; charset=ISO-8859-1' response = Http.put(url=disk_href, data=data, headers=headers, - verify=vca.verify, logger=self.logger) + verify=self.vca.verify, logger=self.logger) + + if response.status_code == 403: + add_headers = {'Content-Type': headers['Content-Type']} + response = self.retry_rest('PUT', disk_href, add_headers, data) if response.status_code != 202: self.logger.debug("PUT REST API call {} failed. Return status code {}".format(disk_href, @@ -3283,7 +3401,7 @@ class vimconnector(vimconn.vimconnector): else: modify_disk_task = taskType.parseString(response.content, True) if type(modify_disk_task) is GenericTask: - status = vca.block_until_completed(modify_disk_task) + status = self.vca.block_until_completed(modify_disk_task) return status return None @@ -3668,9 +3786,6 @@ class vimconnector(vimconn.vimconnector): Returns: None """ - vca = self.connect() - if not vca: - raise vimconn.vimconnConnectionException("Failed to connect vCloud director") try: ip_address = None @@ -3691,12 +3806,16 @@ class vimconnector(vimconn.vimconnector): for vms in vapp._get_vms(): vm_id = (vms.id).split(':')[-1] - url_rest_call = "{}/api/vApp/vm-{}/networkConnectionSection/".format(vca.host, vm_id) + url_rest_call = "{}/api/vApp/vm-{}/networkConnectionSection/".format(self.vca.host, vm_id) response = Http.get(url=url_rest_call, - headers=vca.vcloud_session.get_vcloud_headers(), - verify=vca.verify, - logger=vca.logger) + headers=self.vca.vcloud_session.get_vcloud_headers(), + verify=self.vca.verify, + logger=self.vca.logger) + + if response.status_code == 403: + response = self.retry_rest('GET', url_rest_call) + if response.status_code != 200: self.logger.error("REST call {} failed reason : {}"\ "status code : {}".format(url_rest_call, @@ -3734,11 +3853,16 @@ class vimconnector(vimconn.vimconnector): data = data.replace('\n','\n{}\n'.format(new_item)) - headers = vca.vcloud_session.get_vcloud_headers() + headers = self.vca.vcloud_session.get_vcloud_headers() headers['Content-Type'] = 'application/vnd.vmware.vcloud.networkConnectionSection+xml' response = Http.put(url=url_rest_call, headers=headers, data=data, - verify=vca.verify, - logger=vca.logger) + verify=self.vca.verify, + logger=self.vca.logger) + + if response.status_code == 403: + add_headers = {'Content-Type': headers['Content-Type']} + response = self.retry_rest('PUT', url_rest_call, add_headers, data) + if response.status_code != 202: self.logger.error("REST call {} failed reason : {}"\ "status code : {} ".format(url_rest_call, @@ -3749,7 +3873,7 @@ class vimconnector(vimconn.vimconnector): else: nic_task = taskType.parseString(response.content, True) if isinstance(nic_task, GenericTask): - vca.block_until_completed(nic_task) + self.vca.block_until_completed(nic_task) self.logger.info("add_network_adapter_to_vms(): VM {} conneced to "\ "default NIC type".format(vm_id)) else: @@ -3759,12 +3883,16 @@ class vimconnector(vimconn.vimconnector): for vms in vapp._get_vms(): vm_id = (vms.id).split(':')[-1] - url_rest_call = "{}/api/vApp/vm-{}/networkConnectionSection/".format(vca.host, vm_id) + url_rest_call = "{}/api/vApp/vm-{}/networkConnectionSection/".format(self.vca.host, vm_id) response = Http.get(url=url_rest_call, - headers=vca.vcloud_session.get_vcloud_headers(), - verify=vca.verify, - logger=vca.logger) + headers=self.vca.vcloud_session.get_vcloud_headers(), + verify=self.vca.verify, + logger=self.vca.logger) + + if response.status_code == 403: + response = self.retry_rest('GET', url_rest_call) + if response.status_code != 200: self.logger.error("REST call {} failed reason : {}"\ "status code : {}".format(url_rest_call, @@ -3803,11 +3931,15 @@ class vimconnector(vimconn.vimconnector): data = data.replace('\n','\n{}\n'.format(new_item)) - headers = vca.vcloud_session.get_vcloud_headers() + headers = self.vca.vcloud_session.get_vcloud_headers() headers['Content-Type'] = 'application/vnd.vmware.vcloud.networkConnectionSection+xml' response = Http.put(url=url_rest_call, headers=headers, data=data, - verify=vca.verify, - logger=vca.logger) + verify=self.vca.verify, + logger=self.vca.logger) + + if response.status_code == 403: + add_headers = {'Content-Type': headers['Content-Type']} + response = self.retry_rest('PUT', url_rest_call, add_headers, data) if response.status_code != 202: self.logger.error("REST call {} failed reason : {}"\ @@ -3819,7 +3951,7 @@ class vimconnector(vimconn.vimconnector): else: nic_task = taskType.parseString(response.content, True) if isinstance(nic_task, GenericTask): - vca.block_until_completed(nic_task) + self.vca.block_until_completed(nic_task) self.logger.info("add_network_adapter_to_vms(): VM {} "\ "conneced to NIC type {}".format(vm_id, nic_type)) else: @@ -3892,7 +4024,8 @@ class vimconnector(vimconn.vimconnector): 'users': (optional) list of users to be inserted, each item is a dict with: 'name': (mandatory) user name, 'key-pairs': (optional) list of strings with the public key to be inserted to the user - 'user-data': (optional) string is a text script to be passed directly to cloud-init + 'user-data': (optional) can be a string with the text script to be passed directly to cloud-init, + or a list of strings, each one contains a script to be passed, usually with a MIMEmultipart file 'config-files': (optional). List of files to be transferred. Each item is a dict with: 'dest': (mandatory) string with the destination absolute path 'encoding': (optional, by default text). Can be one of: @@ -3902,9 +4035,6 @@ class vimconnector(vimconn.vimconnector): 'owner': (optional) file owner, string with the format 'owner:group' 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk """ - vca = self.connect() - if not vca: - raise vimconn.vimconnConnectionException("Failed to connect vCloud director") try: if isinstance(cloud_config, dict): @@ -3966,7 +4096,7 @@ class vimconnector(vimconn.vimconnector): vm_name = vm.name task = vapp.customize_guest_os(vm_name, customization_script=customize_script) if isinstance(task, GenericTask): - vca.block_until_completed(task) + self.vca.block_until_completed(task) self.logger.info("cloud_init : customized guest os task "\ "completed for VM {}".format(vm_name)) else: @@ -3979,7 +4109,7 @@ class vimconnector(vimconn.vimconnector): "ssh-key".format(exp)) - def add_new_disk(self, vca, vapp_uuid, disk_size): + def add_new_disk(self, vapp_uuid, disk_size): """ Method to create an empty vm disk @@ -4001,7 +4131,7 @@ class vimconnector(vimconn.vimconnector): if vm_details and "vm_virtual_hardware" in vm_details: self.logger.info("Adding disk to VM: {} disk size:{}GB".format(vm_details["name"], disk_size)) disk_href = vm_details["vm_virtual_hardware"]["disk_edit_href"] - status = self.add_new_disk_rest(vca, disk_href, disk_size_mb) + status = self.add_new_disk_rest(disk_href, disk_size_mb) except Exception as exp: msg = "Error occurred while creating new disk {}.".format(exp) @@ -4015,7 +4145,7 @@ class vimconnector(vimconn.vimconnector): self.rollback_newvm(vapp_uuid, msg) - def add_new_disk_rest(self, vca, disk_href, disk_size_mb): + def add_new_disk_rest(self, disk_href, disk_size_mb): """ Retrives vApp Disks section & add new empty disk @@ -4026,11 +4156,14 @@ class vimconnector(vimconn.vimconnector): Returns: Status of add new disk task """ status = False - if vca.vcloud_session and vca.vcloud_session.organization: + if self.vca.vcloud_session and self.vca.vcloud_session.organization: response = Http.get(url=disk_href, - headers=vca.vcloud_session.get_vcloud_headers(), - verify=vca.verify, - logger=vca.logger) + headers=self.vca.vcloud_session.get_vcloud_headers(), + verify=self.vca.verify, + logger=self.vca.logger) + + if response.status_code == 403: + response = self.retry_rest('GET', disk_href) if response.status_code != requests.codes.ok: self.logger.error("add_new_disk_rest: GET REST API call {} failed. Return status code {}" @@ -4069,13 +4202,17 @@ class vimconnector(vimconn.vimconnector): new_data = new_data.replace('\n', '\n{}\n'.format(new_item)) # Send PUT request to modify virtual hardware section with new disk - headers = vca.vcloud_session.get_vcloud_headers() + headers = self.vca.vcloud_session.get_vcloud_headers() headers['Content-Type'] = 'application/vnd.vmware.vcloud.rasdItemsList+xml; charset=ISO-8859-1' response = Http.put(url=disk_href, data=new_data, headers=headers, - verify=vca.verify, logger=self.logger) + verify=self.vca.verify, logger=self.logger) + + if response.status_code == 403: + add_headers = {'Content-Type': headers['Content-Type']} + response = self.retry_rest('PUT', disk_href, add_headers, new_data) if response.status_code != 202: self.logger.error("PUT REST API call {} failed. Return status code {}. Response Content:{}" @@ -4083,7 +4220,7 @@ class vimconnector(vimconn.vimconnector): else: add_disk_task = taskType.parseString(response.content, True) if type(add_disk_task) is GenericTask: - status = vca.block_until_completed(add_disk_task) + status = self.vca.block_until_completed(add_disk_task) if not status: self.logger.error("Add new disk REST task failed to add {} MB disk".format(disk_size_mb)) @@ -4909,3 +5046,101 @@ class vimconnector(vimconn.vimconnector): "getting media details") raise vimconn.vimconnException(message=exp) + + def retry_rest(self, method, url, add_headers=None, data=None): + """ Method to get Token & retry respective REST request + Args: + api - REST API - Can be one of 'GET' or 'PUT' or 'POST' + url - request url to be used + add_headers - Additional headers (optional) + data - Request payload data to be passed in request + Returns: + response - Response of request + """ + response = None + + #Get token + self.get_token() + + headers=self.vca.vcloud_session.get_vcloud_headers() + + if add_headers: + headers.update(add_headers) + + if method == 'GET': + response = Http.get(url=url, + headers=headers, + verify=self.vca.verify, + logger=self.vca.logger) + elif method == 'PUT': + response = Http.put(url=url, + data=data, + headers=headers, + verify=self.vca.verify, + logger=self.logger) + elif method == 'POST': + response = Http.post(url=url, + headers=headers, + data=data, + verify=self.vca.verify, + logger=self.vca.logger) + elif method == 'DELETE': + response = Http.delete(url=url, + headers=headers, + verify=self.vca.verify, + logger=self.vca.logger) + return response + + + def get_token(self): + """ Generate a new token if expired + + Returns: + The return vca object that letter can be used to connect to vCloud director as admin for VDC + """ + vca = None + + try: + self.logger.debug("Generate token for vca {} as {} to datacenter {}.".format(self.org_name, + self.user, + self.org_name)) + vca = VCA(host=self.url, + username=self.user, + service_type=STANDALONE, + version=VCAVERSION, + verify=False, + log=False) + + result = vca.login(password=self.passwd, org=self.org_name) + if result is True: + result = vca.login(token=vca.token, org=self.org_name, org_url=vca.vcloud_session.org_url) + if result is True: + self.logger.info( + "Successfully generated token for vcloud direct org: {} as user: {}".format(self.org_name, self.user)) + #Update vca + self.vca = vca + return + + except: + raise vimconn.vimconnConnectionException("Can't connect to a vCloud director org: " + "{} as user: {}".format(self.org_name, self.user)) + + if not vca or not result: + raise vimconn.vimconnConnectionException("self.connect() is failed while reconnecting") + + + def get_vdc_details(self): + """ Get VDC details using pyVcloud Lib + + Returns vdc object + """ + vdc = self.vca.get_vdc(self.tenant_name) + + #Retry once, if failed by refreshing token + if vdc is None: + self.get_token() + vdc = self.vca.get_vdc(self.tenant_name) + + return vdc + +