def _execute(self):
        # Build the storage backing for the library to be created using given datastore name
        self.datastore_name = self.args.datastorename
        storage_backings = self.helper.create_storage_backings(
            service_manager=self.servicemanager,
            datastore_name=self.datastore_name)

        # Create a local content library backed by the VC datastore using vAPIs
        self.local_lib_id = self.helper.create_local_library(
            storage_backings, self.lib_name)

        # Create a new library item in the content library for uploading the files
        self.lib_item_id = self.helper.create_library_item(
            library_id=self.local_lib_id,
            item_name=self.lib_item_name,
            item_description='Sample template from ova file',
            item_type='ovf')
        print('Library item created. ID: {0}'.format(self.lib_item_id))

        ova_file_map = self.helper.get_ova_file_map(
            self.SIGNED_OVA_RELATIVE_DIR,
            local_filename=self.SIGNED_OVA_FILENAME)
        # Create a new upload session for uploading the files
        # To ignore expected warnings and skip preview info check,
        # you can set create_spec.warning_behavior during session creation
        session_id = self.client.upload_service.create(
            create_spec=UpdateSessionModel(library_item_id=self.lib_item_id),
            client_token=generate_random_uuid())
        self.helper.upload_files_in_session(ova_file_map, session_id)

        # Wait for terminal preview state and obtain preview warnings if any
        self.wait_for_terminal_preview_state(session_id, AVAILABLE)
        session = self.client.upload_service.get(session_id)
        preview_info = session.preview_info

        # Collect generated preview warning types
        preview_warning_types = []
        print('Preview warnings for the session are the following:')
        for preview_warning in preview_info.warnings:
            print(preview_warning.message.default_message)
            preview_warning_types.append(preview_warning.type)

        # Ignore preview warnings on session
        ignore_warning_behaviors = []
        for warning_type in preview_warning_types:
            warning_behavior = WarningBehavior(type=warning_type, ignored=True)
            ignore_warning_behaviors.append(warning_behavior)
        self.client.upload_service.update(
            session_id,
            update_spec=UpdateSessionModel(
                warning_behavior=ignore_warning_behaviors))
        print(
            'All preview warnings are ignored, proceeding to complete the session'
        )

        self.client.upload_service.complete(session_id)
        self.client.upload_service.delete(session_id)
        print(
            'Uploaded ova file as an ovf template to library item {0}'.format(
                self.lib_item_id))
Beispiel #2
0
    def upload_files(self, library_item_id, files_map):
        """
        Upload a VM template to the published CL

        """
        # Create a new upload session for uploading the files
        session_id = self.client.upload_service.create(
            create_spec=UpdateSessionModel(library_item_id=library_item_id),
            client_token=generate_random_uuid())
        self.upload_files_in_session(files_map, session_id)
        self.client.upload_service.complete(session_id)
        self.client.upload_service.delete(session_id)
Beispiel #3
0
    def delete_and_upload_scenario(self, library_id):
        """
        :param library_id: the OVF item will be created and updated in this library
        :return: None

        Content update scenario 1:
        Update OVF library item by creating an update session for the
        OVF item, removing all existing files in the session, then
        adding all new files into the same update session, and completing
        the session to finish the content update.
        """

        # Create a new library item in the content library for uploading the files
        ovf_item_id = self.helper.create_library_item(
            library_id=library_id,
            item_name='demo-ovf-item',
            item_description='Sample simple VM template',
            item_type='ovf')
        assert ovf_item_id is not None
        print('Library item created id: {0}'.format(ovf_item_id))
        print('OVF Library item version (at creation) {0}:'.format(
            self.get_item_version(ovf_item_id)))

        # Upload a VM template to the CL
        ovf_files_map = self.helper.get_ovf_files_map(
            ClsApiHelper.SIMPLE_OVF_RELATIVE_DIR)
        self.helper.upload_files(library_item_id=ovf_item_id,
                                 files_map=ovf_files_map)
        print('Uploaded ovf and vmdk files to library item {0}'.format(
            ovf_item_id))
        original_version = self.get_item_version(ovf_item_id)
        print('OVF Library item version (on original content upload): {0}'.
              format(original_version))

        # Create a new session and perform content update
        session_id = self.client.upload_service.create(
            create_spec=UpdateSessionModel(library_item_id=ovf_item_id),
            client_token=generate_random_uuid())
        existing_files = self.client.upload_file_service.list(session_id)
        for file in existing_files:
            print('deleting {0}'.format(file.name))
            self.client.upload_file_service.remove(session_id, file.name)
        ovf_files_map = self.helper.get_ovf_files_map(
            ovf_location=ClsApiHelper.PLAIN_OVF_RELATIVE_DIR)
        self.helper.upload_files_in_session(ovf_files_map, session_id)
        self.client.upload_service.complete(session_id)
        self.client.upload_service.delete(session_id)
        updated_version = self.get_item_version(ovf_item_id)
        print('OVF Library item version (after content update): {0}'.format(
            updated_version))
        assert updated_version > original_version, 'content update should increase the version'
Beispiel #4
0
    def replace_scenario(self, library_id):
        """
        :param library_id: the Iso item will be created, and then replaced in this library
        :return: None

        Content update scenario 2:
        Update ISO library item by creating an update session for the
        item, then adding the new ISO file using the same session file
        name into the update session, which will replace the existing
        ISO file upon session complete.
        """

        iso_item_id = self.helper.create_library_item(
            library_id=library_id,
            item_name=self.ISO_ITEM_NAME,
            item_description='Sample iso file',
            item_type='iso')
        print('ISO Library item version (on creation) {0}:'.format(
            self.get_item_version(iso_item_id)))

        iso_files_map = self.helper.get_iso_file_map(
            item_filename=self.ISO_FILE_1, disk_filename=self.ISO_FILE_1)
        self.helper.upload_files(library_item_id=iso_item_id,
                                 files_map=iso_files_map)
        original_version = self.get_item_version(iso_item_id)
        print('ISO Library item version (on original content upload) {0}:'.
              format(original_version))

        session_id = self.client.upload_service.create(
            create_spec=UpdateSessionModel(library_item_id=iso_item_id),
            client_token=generate_random_uuid())
        # Use the same item filename (update endpoint, as it's a replace scenario)
        iso_files_map = self.helper.get_iso_file_map(
            item_filename=self.ISO_FILE_1, disk_filename=self.ISO_FILE_2)

        self.helper.upload_files_in_session(iso_files_map, session_id)
        self.client.upload_service.complete(session_id)
        self.client.upload_service.delete(session_id)
        updated_version = self.get_item_version(iso_item_id)
        print('ISO Library item version (after content update): {0}'.format(
            updated_version))
        assert updated_version > original_version, 'content update should increase the version'