Exemple #1
0
    def create_catalog(cls):
        """Creates a catalog by the name specified in the configuration  file.

        Skips creating one, if such a catalog already exists.

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

        try:
            catalog_author_client = Environment.get_client_in_default_org(
                CommonRoles.CATALOG_AUTHOR)
            org = Org(catalog_author_client, href=cls._org_href)
            catalog_name = cls._config['vcd']['default_catalog_name']
            catalog_records = org.list_catalogs()
            for catalog_record in catalog_records:
                if catalog_record.get('name') == catalog_name:
                    cls._logger.debug('Reusing existing catalog ' +
                                      catalog_name)
                    return

            cls._logger.debug('Creating new catalog ' + catalog_name)
            catalog_resource = org.create_catalog(
                name=catalog_name, description='')
            catalog_author_client.get_task_monitor().wait_for_success(
                task=catalog_resource.Tasks.Task[0])
        finally:
            catalog_author_client.logout()
Exemple #2
0
def create(client, context, name, description, shared):
    logging.debug("_INIT_create_catalog__")
    logging.debug("name = [%s]  desc = [%s] shared = [%s] ", name, description,
                  shared)

    result = pyvcloudprovider_pb2.CreateCatalogResult()
    result.created = False
    try:
        logged_in_org = client.get_org()
        org = Org(client, resource=logged_in_org)

        try:
            catalog = org.create_catalog(name=name, description=description)
            result.created = True

            if shared:
                success = share_catalog(client, name, shared)
                if not success:
                    result.created = False
        except Exception as e:
            error_message = "__ ERROR Creating catalog [{0}] __ERROR_MESSAGE__ [{1}]".format(
                name, str(e))
            logging.warn(error_message)
            context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
            context.set_details(error_message)
    except Exception as e:
        error_message = "__ ERROR Creating catalog [{0}] __ERROR_MESSAGE__ [{1}]".format(
            name, str(e))
        logging.warn(error_message)
        context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
        context.set_details(error_message)

    logging.debug("_DONE_create_catalog__")
    return result
def create(client, context, name, description):
    logging.debug("_INIT_create_catalog__")
    logging.debug("name = [%s]  desc = [%s] ", name, description)

    result = pyvcloudprovider_pb2.CreateCatalogResult()
    result.created = False
    try:
        logged_in_org = client.get_org()
        org = Org(client, resource=logged_in_org)

        try:
            catalog = org.create_catalog(name=name, description=description)
            result.created = True
        except Exception as e:
            error_message = "__ ERROR Creating catalog [{0}] __ERROR_MESSAGE__ [{1}]".format(
                name, str(e))
            logging.warn(error_message)
            context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
            context.set_details(error_message)
    except Exception as e:
        error_message = "__ ERROR Creating catalog [{0}] __ERROR_MESSAGE__ [{1}]".format(
            name, str(e))
        logging.warn(error_message)
        context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
        context.set_details(error_message)

    logging.debug("_DONE_create_catalog__")
    return result
Exemple #4
0
def create(ctx, catalog_name, description):
    try:
        client = ctx.obj['client']
        in_use_org_href = ctx.obj['profiles'].get('org_href')
        org = Org(client, in_use_org_href)
        c = org.create_catalog(catalog_name, description)
        stdout(c.Tasks.Task[0], ctx, show_id=True)
    except Exception as e:
        stderr(e, ctx)
Exemple #5
0
def create(ctx, catalog_name, description):
    try:
        restore_session(ctx)
        client = ctx.obj['client']
        in_use_org_href = ctx.obj['profiles'].get('org_href')
        org = Org(client, in_use_org_href)
        c = org.create_catalog(catalog_name, description)
        stdout(c.Tasks.Task[0], ctx, show_id=True)
    except Exception as e:
        stderr(e, ctx)
Exemple #6
0
def create(client, name, description, shared):
    logging.debug("===_INIT_create name = [%s]  desc = [%s] shared = [%s] ",
                  name, description, shared)

    try:
        logged_in_org = client.get_org()
        org = Org(client, resource=logged_in_org)
        result = pyvcloudprovider_pb2.CreateCatalogResult()
        result.created = False
        try:
            catalog = org.create_catalog(name=name, description=description)
            result.created = True
        except Exception as e:
            logging.info("===__INFO__ Not Created catalog [" + name + "]")
        logging.debug(
            "===_DONE_create name = [%s]  desc = [%s] shared = [%s] ", name,
            description, shared)
        return result

    except Exception as e:
        logging.warn("error occured", e)
Exemple #7
0
def create(client, name, description):
    logging.debug("=== create catalog called === \n")

    try:
        logged_in_org = client.get_org()
        logging.info("loged in org ================ \n\n\n\n\n")
        org = Org(client, resource=logged_in_org)
        logging.info(
            "\n\n\n\n\n\n            got ord ,,,, ================== \n\n\n")
        result = pyvcloudprovider_pb2.CreateCatalogResult()
        result.created = False
        try:
            catalog = org.create_catalog(name, description)
            result.created = True
        except Exception as e:
            logging.info("\n Not Created catalog [" + name + "]")
        return result

    except Exception as e:
        print(e)
        print("\n")
        logging.warn("error occured", e)
Exemple #8
0
    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()
    print("Catalog now exists: {0}".format(catalog_resource.get('name')))

# Check for catalog_items containing OVF templates in the catalog and
# upload them if they are missing.
for catalog_item in cfg.catalog_items:
    try:
        catalog_item_resource = org.get_catalog_item(
            catalog_item['catalog_name'], catalog_item['item_name'])
        print("Catalog item exists: {0}".format(catalog_item['item_name']))
    except Exception:
        # Define a progress reporter to track upload, since it takes
        # a while.
        def progress_reporter(transferred, total):
            print("{:,} of {:,} bytes, {:.0%}".format(
class Catalog(VcdAnsibleModule):
    def __init__(self, **kwargs):
        super(Catalog, self).__init__(**kwargs)
        logged_in_org = self.client.get_org()
        self.org = Org(self.client, resource=logged_in_org)

    def manage_states(self):
        state = self.params.get('state')
        if state == "present":
            return self.create()

        if state == "absent":
            return self.delete()

        if state == "update":
            return self.update()

    def manage_operations(self):
        operation = self.params.get('operation')
        if operation == "shared":
            return self.shared()

        if operation == "read":
            return self.read()

        if operation == "list_items":
            return self.list_items()

    def create(self):
        catalog_name = self.params.get('catalog_name')
        description = self.params.get('description')
        response = dict()
        response['changed'] = False

        try:
            self.org.get_catalog(name=catalog_name)
        except EntityNotFoundException:
            self.org.create_catalog(name=catalog_name, description=description)
            response['msg'] = 'Catalog {} has been created.'.format(
                catalog_name)
            response['changed'] = True
        else:
            response['warnings'] = 'Catalog {} is already present.'.format(
                catalog_name)

        return response

    def delete(self):
        catalog_name = self.params.get('catalog_name')
        response = dict()
        response['changed'] = False

        try:
            self.org.get_catalog(name=catalog_name)
        except EntityNotFoundException:
            response['warnings'] = 'Catalog {} is not present.'.format(
                catalog_name)
        else:
            self.org.delete_catalog(catalog_name)
            response['msg'] = 'Catalog {} has been deleted.'.format(
                catalog_name)
            response['changed'] = True

        return response

    def update(self):
        catalog_name = self.params.get('catalog_name')
        new_catalog_name = self.params.get('new_catalog_name')
        description = self.params.get('description')
        response = dict()
        response['changed'] = False

        if not new_catalog_name:
            new_catalog_name = catalog_name

        self.org.update_catalog(old_catalog_name=catalog_name,
                                new_catalog_name=new_catalog_name,
                                description=description)
        response['msg'] = 'Catalog {} has been updated.'.format(catalog_name)
        response['changed'] = True

        return response

    def shared(self):
        catalog_name = self.params.get('catalog_name')
        shared = self.params.get('shared')
        response = dict()
        response['changed'] = False

        self.org.share_catalog(name=catalog_name, share=shared)
        response[
            'msg'] = 'Catalog {} shared state has been updated to [shared={}].'.format(
                catalog_name, shared)
        response['changed'] = True

        return response

    def read(self):
        catalog_name = self.params.get('catalog_name')
        response = dict()
        result = dict()
        response['changed'] = False

        catalog = self.org.get_catalog(catalog_name)
        result['name'] = str(catalog.get("name"))
        result['description'] = str(catalog.Description)
        result['shared'] = str(catalog.IsPublished)
        response['msg'] = result
        response['changed'] = False

        return response

    def list_items(self):
        catalog_name = self.params.get('catalog_name')
        response = dict()
        response['changed'] = False

        catalog_items = self.org.list_catalog_items(catalog_name)
        response['msg'] = [
            catalog_item['name'] for catalog_item in catalog_items
        ]
        response['changed'] = False

        return response
Exemple #10
0
    def get_catalog_id_from_path(self, catalog_name=None, path=None, progress=False):
        """
        Args
            catalog - catalog name to be created
            path: - valid path to OVF file.
            progress - boolean progress bar show progress bar.

        Return: if image uploaded correct method will provide image catalog UUID.
        """
        if not path:
            raise Exception("Image path can't be None.")

        if not os.path.isfile(path):
            raise Exception("Can't read file. File not found.")

        if not os.access(path, os.R_OK):
            raise Exception("Can't read file. Check file permission to read.")

        self.logger.debug("get_catalog_id_from_path() client requesting {} ".format(path))

        _, filename = os.path.split(path)
        _, file_extension = os.path.splitext(path)
        if file_extension != '.ovf':
            self.logger.debug("Wrong file extension {} connector support only OVF container.".format(file_extension))
            raise Exception("Wrong container.  vCloud director supports only OVF.")

        self.logger.debug("File name {} Catalog Name {} file path {} ".format(filename,
                                                                              catalog_name,
                                                                              path))
        try:
            client = self.connect()
            if not client:
                raise Exception("Failed to connect vCD")
            org = Org(client, resource=client.get_org())
            catalogs = org.list_catalogs()
        except Exception as exp:
            self.logger.debug("Failed get catalogs() with Exception {} ".format(exp))
            raise Exception("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 = org.create_catalog(catalog_name, catalog_name)
            if result is None:
                raise Exception("Failed to create new catalog {} ".format(catalog_name))
            result = self.upload_ovf(org=org, catalog_name=catalog_name, image_name=filename.split(".")[0],
                                     media_file_name=path, description='medial_file_name', progress=progress)
            if not result:
                raise Exception("Failed to create vApp template for catalog {} ".format(catalog_name))
            return self.get_catalogid(catalog_name, catalogs)
        else:
            for catalog in catalogs:
                # search for existing catalog if we find same name we return ID
                if catalog['name'] == catalog_name:
                    self.logger.debug("Found existing catalog entry for {} "
                                      "catalog id {}".format(catalog_name,
                                                             self.get_catalogid(catalog_name, catalogs)))
                    return self.get_catalogid(catalog_name, 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_name))
        result = org.create_catalog(catalog_name, catalog_name)
        if result is None:
            raise Exception("Failed to create new catalog {} ".format(catalog_name))

        result = self.upload_ovf(org=org, catalog_name=catalog_name, image_name=filename.split(".")[0],
                                 media_file_name=path, description='medial_file_name', progress=progress)
        if not result:
            raise Exception("Failed create vApp template for catalog {} ".format(catalog_name))
Exemple #11
0
    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()
    print("Catalog now exists: {0}".format(catalog_resource.get('name')))

# Check for catalog_items containing OVF templates in the catalog and
# upload them if they are missing.
for catalog_item in cfg.catalog_items:
    try:
        catalog_item_resource = org.get_catalog_item(
            catalog_item['catalog_name'], catalog_item['item_name'])
        print("Catalog item exists: {0}".format(catalog_item['item_name']))
    except Exception:
        # Define a progress reporter to track upload, since it takes
        # a while.
        def progress_reporter(transferred, total):
            print("{:,} of {:,} bytes, {:.0%}".format(
Exemple #12
0
def install_cse(ctx, config_file_name, template_name, no_capture, update,
                amqp_install, ext_install):
    check_config(config_file_name)
    click.secho('Installing CSE on vCD from file: %s, template: %s' %
                (config_file_name, template_name))
    config = get_config(config_file_name)
    client = Client(config['vcd']['host'],
                    api_version=config['vcd']['api_version'],
                    verify_ssl_certs=config['vcd']['verify'],
                    log_file='cse-install.log',
                    log_headers=True,
                    log_bodies=True)
    client.set_credentials(
        BasicLoginCredentials(config['vcd']['username'], 'System',
                              config['vcd']['password']))
    click.echo(
        'Connected to vCloud Director as system '
        'administrator (%s:%s): %s' %
        (config['vcd']['host'], config['vcd']['port'], bool_to_msg(True)))
    click.secho('Installing  \'%s\' service broker' % config['broker']['type'])
    if config['broker']['type'] == 'default':
        orgs = client.get_org_list()
        org_href = None
        for org in [o for o in orgs.Org if hasattr(orgs, 'Org')]:
            if org.get('name') == config['broker']['org']:
                org_href = org.get('href')
        org = Org(client, href=org_href)
        click.echo('Find org \'%s\': %s' % (org.get_name(), bool_to_msg(True)))
        vdc_resource = org.get_vdc(config['broker']['vdc'])
        click.echo('Find vdc \'%s\': %s' %
                   (vdc_resource.get('name'), bool_to_msg(True)))
        try:
            catalog = org.get_catalog(config['broker']['catalog'])
        except Exception:
            click.secho('Creating catalog %s ' % config['broker']['catalog'],
                        nl=False,
                        fg='green')
            catalog = org.create_catalog(config['broker']['catalog'],
                                         'CSE catalog')
            org.share_catalog(config['broker']['catalog'])
            click.secho('done', fg='blue')
            catalog = org.get_catalog(config['broker']['catalog'])
        click.echo(
            'Find catalog \'%s\': %s' %
            (config['broker']['catalog'], bool_to_msg(catalog is not None)))
        for template in config['broker']['templates']:
            if template_name == '*' or template['name'] == template_name:
                click.secho('Processing template: %s' % template['name'])
                k8s_template = None
                try:
                    k8s_template = org.get_catalog_item(
                        config['broker']['catalog'], template['catalog_item'])
                    click.echo(
                        'Find template \'%s\', \'%s\': %s' %
                        (config['broker']['catalog'], template['catalog_item'],
                         bool_to_msg(k8s_template is not None)))
                except Exception:
                    pass
                try:
                    if k8s_template is None or update:
                        if update:
                            click.secho('Updating template')
                        else:
                            click.secho('Creating template')
                        create_template(ctx, config, client, org, vdc_resource,
                                        catalog, no_capture, template)
                        k8s_template = org.get_catalog_item(
                            config['broker']['catalog'],
                            template['catalog_item'])
                        if update:
                            click.echo('Updated template \'%s\', \'%s\': %s' %
                                       (config['broker']['catalog'],
                                        template['catalog_item'],
                                        bool_to_msg(k8s_template is not None)))
                        else:
                            click.echo('Find template \'%s\', \'%s\': %s' %
                                       (config['broker']['catalog'],
                                        template['catalog_item'],
                                        bool_to_msg(k8s_template is not None)))
                except Exception:
                    LOGGER.error(traceback.format_exc())
                    click.echo('Can\'t create or update template \'%s\' '
                               '\'%s\': %s' %
                               (template['name'], config['broker']['catalog'],
                                template['catalog_item']))
        configure_amqp_settings(ctx, client, config, amqp_install)
        register_extension(ctx, client, config, ext_install)
        click.secho('Start CSE with: \'cse run %s\'' % config_file_name)
Exemple #13
0
 def test_create_catalog(self):
     logged_in_org = self.client.get_org()
     org = Org(self.client, resource=logged_in_org)
     catalog = org.create_catalog(self.config['vcd']['catalog'], 'test catalog')
     assert self.config['vcd']['catalog'] == catalog.get('name')
Exemple #14
0
 def test_create_catalog(self):
     logged_in_org = self.client.get_org()
     org = Org(self.client, resource=logged_in_org)
     catalog = org.create_catalog(self.config['vcd']['catalog'],
                                  'test catalog')
     assert self.config['vcd']['catalog'] == catalog.get('name')
class Catalog(VcdAnsibleModule):
    def __init__(self, **kwargs):
        super(Catalog, self).__init__(**kwargs)
        logged_in_org = self.client.get_org()
        self.org = Org(self.client, resource=logged_in_org)

    def manage_states(self):
        state = self.params.get('state')
        if state == "present":
            return self.create()

        if state == "absent":
            return self.delete()

        if state == "update":
            return self.update()

    def manage_operations(self):
        operation = self.params.get('operation')
        if operation == "shared":
            return self.shared()

        if operation == "read":
            return self.read()

        if operation == "list_items":
            return self.list_items()

    def create(self):
        catalog_name = self.params.get('catalog_name')
        description = self.params.get('description')
        response = dict()
        response['changed'] = False

        try:
            self.org.get_catalog(name=catalog_name)
        except EntityNotFoundException:
            self.org.create_catalog(name=catalog_name, description=description)
            response['msg'] = 'Catalog {} has been created.'.format(catalog_name)
            response['changed'] = True
        else:
            response['warnings'] = 'Catalog {} is already present.'.format(catalog_name)

        return response

    def delete(self):
        catalog_name = self.params.get('catalog_name')
        response = dict()
        response['changed'] = False

        try:
            self.org.get_catalog(name=catalog_name)
        except EntityNotFoundException:
            response['warnings'] = 'Catalog {} is not present.'.format(catalog_name)
        else:
            self.org.delete_catalog(catalog_name)
            response['msg'] = 'Catalog {} has been deleted.'.format(catalog_name)
            response['changed'] = True

        return response

    def update(self):
        catalog_name = self.params.get('catalog_name')
        new_catalog_name = self.params.get('new_catalog_name')
        description = self.params.get('description')
        response = dict()
        response['changed'] = False

        if not new_catalog_name:
            new_catalog_name = catalog_name

        self.org.update_catalog(old_catalog_name=catalog_name,
                                new_catalog_name=new_catalog_name,
                                description=description)
        response['msg'] = 'Catalog {} has been updated.'.format(catalog_name)
        response['changed'] = True

        return response

    def shared(self):
        catalog_name = self.params.get('catalog_name')
        shared = self.params.get('shared')
        response = dict()
        response['changed'] = False

        self.org.share_catalog(name=catalog_name, share=shared)
        response['msg'] = 'Catalog {} shared state has been updated to [shared={}].'.format(catalog_name, shared)
        response['changed'] = True

        return response

    def read(self):
        catalog_name = self.params.get('catalog_name')
        response = dict()
        result = dict()
        response['changed'] = False

        catalog = self.org.get_catalog(catalog_name)
        result['name'] = str(catalog.get("name"))
        result['description'] = str(catalog.Description)
        result['shared'] = str(catalog.IsPublished)
        response['msg'] = result
        response['changed'] = False

        return response

    def list_items(self):
        catalog_name = self.params.get('catalog_name')
        response = dict()
        response['changed'] = False

        catalog_items = self.org.list_catalog_items(catalog_name)
        response['msg'] = [catalog_item['name'] for catalog_item in catalog_items]
        response['changed'] = False

        return response
Exemple #16
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")