[Pulp-list] Content Unit Multiple Uploads, Same Unit Key

Jay Medrano jay.medrano at neulion.com
Wed Aug 10 16:05:42 UTC 2016


Here's an answer to my own question, to the best of my abilities. The
quest for the answer was definitely worth it. In my case, I wanted to
import a new file, even if an existing unit key matched the new import
unit key. In the process I wanted to update the existing checksum metadata
field. I verified that the storage directory contains only one file for
that unit key and that its content reflects the newest upload. Here's are
the changes I added to make this work. Any further help/suggestions are
welcome.

        try:
            artifact = models.Artifact.from_file(file_path, metadata)
            artifact.save_and_import_content(file_path)
            repo_controller.associate_single_unit(repo.repo_obj, artifact)
        except NotUniqueError:
            artifact = artifact.__class__.objects.get(**artifact.unit_key)
+          artifact._checksum = models.Artifact.checksum(file_path)
+          artifact.save_and_import_content(file_path)
            repo_controller.associate_single_unit(repo.repo_obj, artifact)
        except Exception as e:
            return {'success_flag': False, 'summary': e.message,
'details': {}}
        return {'success_flag': True, 'summary': '', 'details': {}}

From: Jay Medrano [mailto:jay.medrano at neulion.com]
Sent: Friday, August 05, 2016 2:26 PM
To: 'pulp-list at redhat.com' <pulp-list at redhat.com>
Subject: Content Unit Multiple Uploads, Same Unit Key

Hi,

I’m currently working on developing a plugin to support build artifacts
with custom metadata. I’d like to know if there is a way to update or
replace an existing content unit. There are some use cases we’ve
identified where we want to replace a previous content unit that has the
same unit key. This might be a rare case, and the obvious alternative is
to delete the existing content unit before starting the new upload. I was
wondering if there was a way to handle this during the second upload? In
the Importer upload_unit function, I noticed from other plugin examples
that NotUniqueError exceptions are handled by re-associating the old
content unit. I might be interpreting that incorrectly. If that’s the
case, most of our re-upload attempts would fail silently, and the old data
would remain. Can someone help me find a way to replace, or update an
existing content unit that has an identical unit key, preferably during
the upload/import process?

Here is the relevant plugin code that tries to import/associate a new
content unit.
        try:
            artifact = models.Artifact.from_file(file_path, metadata)
            artifact.save_and_import_content(file_path)
            repo_controller.associate_single_unit(repo.repo_obj, artifact)
        except NotUniqueError:
            artifact = artifact.__class__.objects.get(**artifact.unit_key)
            repo_controller.associate_single_unit(repo.repo_obj, artifact)
        except Exception as e:
            return {'success_flag': False, 'summary': e.message,
'details': {}}
        return {'success_flag': True, 'summary': '', 'details': {}}

Jay




More information about the Pulp-list mailing list