Exemple #1
0
    def create_ovdc(cls):
        """Creates an org vdc with the name specified in the config file.

        Skips creating one, if such an org vdc already exists. Also stores the
        href of the org vdc as class variable for future use.

        :raises: Exception: if the class variable _org_href or _pvdc_name
            is not populated.
        """
        cls._basic_check()
        if cls._org_href is None:
            raise Exception('Org ' + cls._config['vcd']['default_org_name'] +
                            ' doesn\'t exist.')

        if cls._pvdc_name is None:
            raise Exception('pVDC ' + cls._config['vcd']['default_pvdc_name'] +
                            ' doesn\'t exist.')

        org = Org(cls._sys_admin_client, href=cls._org_href)
        ovdc_name = cls._config['vcd']['default_ovdc_name']
        for vdc in org.list_vdcs():
            if vdc.get('name').lower() == ovdc_name.lower():
                cls._logger.debug('Reusing existing ovdc ' + ovdc_name + '.')
                cls._ovdc_href = vdc.get('href')
                return

        storage_profiles = [{
            'name': cls._config['vcd']['default_storage_profile_name'],
            'enabled': True,
            'units': 'MB',
            'limit': 0,
            'default': True
        }]

        system = System(
            cls._sys_admin_client,
            admin_resource=cls._sys_admin_client.get_admin())
        netpool_to_use = cls._get_netpool_name_to_use(system)

        cls._logger.debug('Creating ovdc ' + ovdc_name + '.')
        vdc_resource = org.create_org_vdc(
            ovdc_name,
            cls._pvdc_name,
            network_pool_name=netpool_to_use,
            network_quota=cls._config['vcd']['default_network_quota'],
            storage_profiles=storage_profiles,
            uses_fast_provisioning=True,
            is_thin_provision=True)

        cls._sys_admin_client.get_task_monitor().wait_for_success(
            task=vdc_resource.Tasks.Task[0])

        org.reload()
        # The following contraption is required to get the non admin href of
        # the ovdc. vdc_resource contains the admin version of the href since
        # we created the ovdc as a sys admin.
        for vdc in org.list_vdcs():
            if vdc.get('name').lower() == ovdc_name.lower():
                cls._ovdc_href = vdc.get('href')
Exemple #2
0
def delete_catalog_item(item_name):
    org = Org(CLIENT, href=ORG_HREF)
    try:
        org.delete_catalog_item(CATALOG_NAME, item_name)
        utils.wait_for_catalog_item_to_resolve(CLIENT, CATALOG_NAME, item_name,
                                               org=org)
        org.reload()
    except EntityNotFoundException:
        pass
 def test_12_remove_rights_from_org(self):
     org_in_use = self.config['vcd']['org_in_use']
     org = Org(self.client,
               href=self.client.get_org_by_name(org_in_use).get('href'))
     right_name = self.config['vcd']['right_name']
     right_record_list = org.list_rights_of_org()
     no_of_rights_before = len(right_record_list)
     org.remove_rights([right_name])
     org.reload()
     right_record_list = org.list_rights_of_org()
     no_of_rights_after = len(right_record_list)
     assert no_of_rights_before > no_of_rights_after
Exemple #4
0
def delete_catalog_item(item_name, logger=NULL_LOGGER):
    logger.debug(f"Deleting catalog item: {item_name}")
    org = Org(CLIENT, href=ORG_HREF)
    try:
        org.delete_catalog_item(CATALOG_NAME, item_name)
        pyvcloud_utils.wait_for_catalog_item_to_resolve(CLIENT,
                                                        CATALOG_NAME,
                                                        item_name,
                                                        org=org)
        org.reload()
        logger.debug(f"Successfully deleted the catalog item: {item_name}")
    except EntityNotFoundException as e:
        logger.warning(f"Failed to delete catalog item {item_name}: {e}")
Exemple #5
0
    vdc_resource = org.get_vdc(cfg.vdc['vdc_name'])
    vdc = VDC(client, resource=vdc_resource)
    print("VDC already exists: {0}".format(vdc.name))
except Exception:
    print("VDC does not exist, creating: {0}".format(cfg.vdc['vdc_name']))
    vdc_kwargs = cfg.vdc
    # Vet the netpool and pvcd arguments as either can be '*' in which 
    # case we need to find default values. 
    _fill_in_pvdc_default(client, vdc_kwargs)
    _fill_in_netpool_default(client, vdc_kwargs)
    # Now create the VDC.  
    admin_vdc_resource = org.create_org_vdc(**vdc_kwargs)
    # The 'admin_vdc_resource' is not a task but an AdminVdc entity.  Tasks
    # are two levels down.
    handle_task(client, admin_vdc_resource.Tasks.Task[0])
    org.reload()
    vdc_resource = org.get_vdc(cfg.vdc['vdc_name'])
    vdc = VDC(client, resource=vdc_resource)
    print("VDC now exists: {0}".format(vdc.name))

# Ensure the catalog exists.  For now we don't do anything special with
# permissions.  As with VDC's we reload the org if we create a catalog
# so that it's visible in future calls.
try:
    catalog_resource = org.get_catalog_resource(cfg.catalog['name'])
    print("Catalog already exists: {0}".format(cfg.catalog['name']))
except Exception:
    print("Catalog does not exist, creating: {0}".format(cfg.catalog['name']))
    catalog_kwargs = cfg.catalog
    catalog_resource = org.create_catalog(**catalog_kwargs)
    org.reload()
Exemple #6
0
    def create_ovdc(cls):
        """Creates an org vdc with the name specified in the config file.

        Skips creating one, if such an org vdc already exists. Also stores the
        href of the org vdc as class variable for future use.

        :raises: Exception: if the class variable _org_href or _pvdc_name
            is not populated.
        """
        cls._basic_check()
        if cls._org_href is None:
            raise Exception('Org ' + cls._config['vcd']['default_org_name'] +
                            ' doesn\'t exist.')

        if cls._pvdc_name is None:
            raise Exception('pVDC ' + cls._config['vcd']['default_pvdc_name'] +
                            ' doesn\'t exist.')

        org = Org(cls._sys_admin_client, href=cls._org_href)
        ovdc_name = cls._config['vcd']['default_ovdc_name']
        for vdc in org.list_vdcs():
            if vdc.get('name').lower() == ovdc_name.lower():
                cls._logger.debug('Reusing existing ovdc ' + ovdc_name + '.')
                cls._ovdc_href = vdc.get('href')
                return

        storage_profiles = [{
            'name':
            cls._config['vcd']['default_storage_profile_name'],
            'enabled':
            True,
            'units':
            'MB',
            'limit':
            0,
            'default':
            True
        }]

        system = System(
            cls._sys_admin_client,
            admin_resource=cls._sys_admin_client.get_admin())
        netpool_to_use = cls._get_netpool_name_to_use(system)

        cls._logger.debug('Creating ovdc ' + ovdc_name + '.')
        vdc_resource = org.create_org_vdc(
            ovdc_name,
            cls._pvdc_name,
            network_pool_name=netpool_to_use,
            network_quota=cls._config['vcd']['default_network_quota'],
            storage_profiles=storage_profiles,
            uses_fast_provisioning=True,
            is_thin_provision=True)

        cls._sys_admin_client.get_task_monitor().wait_for_success(
            task=vdc_resource.Tasks.Task[0])

        org.reload()
        # The following contraption is required to get the non admin href of
        # the ovdc. vdc_resource contains the admin version of the href since
        # we created the ovdc as a sys admin.
        for vdc in org.list_vdcs():
            if vdc.get('name').lower() == ovdc_name.lower():
                cls._ovdc_href = vdc.get('href')
Exemple #7
0
    vdc_resource = org.get_vdc(cfg.vdc['vdc_name'])
    vdc = VDC(client, resource=vdc_resource)
    print("VDC already exists: {0}".format(vdc.name))
except Exception:
    print("VDC does not exist, creating: {0}".format(cfg.vdc['vdc_name']))
    vdc_kwargs = cfg.vdc
    # Vet the netpool and pvcd arguments as either can be '*' in which 
    # case we need to find default values. 
    _fill_in_pvdc_default(client, vdc_kwargs)
    _fill_in_netpool_default(client, vdc_kwargs)
    # Now create the VDC.  
    admin_vdc_resource = org.create_org_vdc(**vdc_kwargs)
    # The 'admin_vdc_resource' is not a task but an AdminVdc entity.  Tasks
    # are two levels down.
    handle_task(client, admin_vdc_resource.Tasks.Task[0])
    org.reload()
    vdc_resource = org.get_vdc(cfg.vdc['vdc_name'])
    vdc = VDC(client, resource=vdc_resource)
    print("VDC now exists: {0}".format(vdc.name))

# Ensure the catalog exists.  For now we don't do anything special with
# permissions.  As with VDC's we reload the org if we create a catalog
# so that it's visible in future calls.
try:
    catalog_resource = org.get_catalog_resource(cfg.catalog['name'])
    print("Catalog already exists: {0}".format(cfg.catalog['name']))
except Exception:
    print("Catalog does not exist, creating: {0}".format(cfg.catalog['name']))
    catalog_kwargs = cfg.catalog
    catalog_resource = org.create_catalog(**catalog_kwargs)
    org.reload()
Exemple #8
0
class OVFUploader(object):
    """ Class to convert input image into OVF format """
    def __init__(self,
                 ovf_file,
                 vcd_url=None,
                 username=None,
                 password=None,
                 orgname=None):
        self.ovf_file = os.path.abspath(ovf_file)
        self.vcd_url = vcd_url
        self.username = username
        self.password = password
        self.orgname = orgname
        try:
            client = Client(self.vcd_url,
                            verify_ssl_certs=False,
                            api_version=ApiVersion.VERSION_32.value,
                            log_requests=True,
                            log_headers=True,
                            log_bodies=True,
                            log_file=LOG_FILE)
            # sclient.set_highest_supported_version()
            client.set_credentials(
                BasicLoginCredentials(self.username, self.orgname,
                                      self.password))
            logger.info("Logged into {} using version {}".format(
                self.vcd_url, client.get_api_version()))
            self.client = client
            self.org = Org(self.client, resource=self.client.get_org())

        except Exception as exp:
            problem = Exception(
                "Failed to connect to vCD at {}, org {}, username {}:\n{}".
                format(self.vcd_url, self.orgname, self.username, exp))
            logger.error(problem)
            raise problem

        try:
            # Retrieve the VM name from the OVF.  We will use this as both the image and catalog name
            OVF_tree = etree.parse(self.ovf_file)
            root = OVF_tree.getroot()
            nsmap = {k: v for k, v in root.nsmap.items() if k}
            nsmap["xmlns"] = "http://schemas.dmtf.org/ovf/envelope/1"

            virtuasystem = root.find('xmlns:VirtualSystem', nsmap)
            name_tag = virtuasystem.find('xmlns:Name', nsmap)
            self.image_name = name_tag.text
            info_tag = virtuasystem.find('xmlns:Info', nsmap)
            self.image_description = info_tag.text

            references = root.find('xmlns:References', nsmap)
            file = references.find('xmlns:File', nsmap)
            self.vmdk_file = "{}/{}".format(
                os.path.dirname(self.ovf_file),
                file.attrib['{http://schemas.dmtf.org/ovf/envelope/1}href'])
            logger.info("Loaded VM {}: {}".format(self.image_name,
                                                  self.image_description))

        except Exception as exp:
            problem = Exception(
                "Failed to fetch VirtualSystem Name element from OVF {}:\n{}".
                format(self.ovf_file, exp))
            logger.error(problem)
            raise problem

    def make_catalog(self):
        self.catalog_id = None
        try:
            for catalog in self.org.list_catalogs():
                if catalog['name'] == self.image_name:
                    self.catalog_id = catalog['id']
            if self.catalog_id is None:
                logger.info("Creating a new catalog entry {} in vCD".format(
                    self.image_name))
                result = self.org.create_catalog(self.image_name,
                                                 self.image_description)
                if result is None:
                    raise Exception("Failed to create new catalog entry")
                self.catalog_id = result.attrib['id'].split(':')[-1]
                self.org.reload()

            logger.debug("Using catalog {}, id {}".format(
                self.image_name, self.catalog_id))

        except Exception as exp:
            problem = Exception("Failed to fetch catalog for {}:\n{} ".format(
                self.image_name, exp))
            logger.error(problem)
            raise problem

    def upload_ovf(self):

        ova_tarfilename, _ = os.path.splitext(self.ovf_file)
        ova_tarfilename += '.ova'
        try:
            # Check if the content already exists:
            resource_type = ResourceType.CATALOG_ITEM.value
            q = self.client.get_typed_query(
                resource_type,
                query_result_format=QueryResultFormat.ID_RECORDS,
                equality_filter=('catalogName', self.image_name))
            for item in list(q.execute()):
                if item.get('name') == self.image_name:
                    logger.info("Removing old version from catalog")
                    try:
                        self.org.delete_catalog_item(self.image_name,
                                                     self.image_name)
                    except InternalServerException as exp:
                        problem = Exception(
                            "Cannot delete vAppTemplate {}. Please check in vCD if "
                            "the content is still being imported into the catalog"
                            .format(self.image_name))
                        raise problem

            # Create a single OVA bundle
            ova = tarfile.open(name=ova_tarfilename, mode='w')
            ova.add(self.ovf_file, arcname=os.path.basename(self.ovf_file))
            ova.add(self.vmdk_file, arcname=os.path.basename(self.vmdk_file))
            ova.close()
            logger.info("Uploading content to vCD")
            self.org.upload_ovf(self.image_name,
                                ova_tarfilename,
                                item_name=self.image_name,
                                description=self.image_description,
                                callback=report_progress)
        except Exception as exp:
            problem = Exception("Failed to upload OVF {}:\n{} ".format(
                self.ovf_file, exp))
            logger.error(problem)
            raise problem
        finally:
            if os.path.exists(ova_tarfilename):
                os.remove(ova_tarfilename)

    def wait_for_task_completion(self):

        logger.info("Importing content to vCD")
        try:

            query = self.client.get_typed_query(
                query_type_name=ResourceType.TASK.value,
                qfilter='ownerName==' + self.username +
                ';(status==queued,status==preRunning,status==running)',
                query_result_format=QueryResultFormat.REFERENCES)

            upload_task = None
            tasks = list(query.execute())
            for task in tasks:
                if task.get('name') == 'VDC_UPLOAD_OVF_CONTENTS':
                    upload_task = self.client.get_resource(task.get('href'))
                    break

            bad_statuses = [
                TaskStatus.ABORTED, TaskStatus.CANCELED, TaskStatus.ERROR
            ]

        except Exception as exp:
            problem = Exception("Failed to import OVF {}:\n{} ".format(
                self.ovf_file, exp))
            logger.error(problem)
            raise problem

        while (True):
            task_status = upload_task.get('status').lower()
            if (hasattr(upload_task, 'Progress')):
                print("{}% complete  \r".format(upload_task.Progress), end='')

            for status in bad_statuses:
                if task_status == status.value.lower():
                    problem = Exception(
                        "vCD failed to import OVF {}:\n{}: {} ".format(
                            self.ovf_file, task_status,
                            upload_task.Error.get('Message')))
                    logger.error(problem)
                    raise problem
            if task_status == str(TaskStatus.SUCCESS.value).lower():
                break

            time.sleep(2)
            upload_task = self.client.get_resource(upload_task.get('href'))

        logger.info("OVF upload and import complete, content is ready to use")