Project

Profile

Help

Story #4072

Updated by bmbouter about 1 year ago

h2. Problem

While making an upload API for pulp_ansible, I receive only a sha256 from the user to validate the uploaded data. To save an Artifact I need the other digests and the size. This causes me to manually compute all the digest info and perform any validation.

This is going to be a common problem for plugin writers receiving Artifact data and not using the downloaders, e.g. upload.

h2. Solution

Add a static method to Artifact named <code>create_and_validate</code> <code>create</code> with the following signature:

<code>def create_and_validate(expected_digests=None, create(expected_digests=None, expected_size=None):</code>

These params are the same type/names as the ones used by BaseDownloader "here":https://github.com/pulp/pulp/blob/d9eb035dcf87679d00ae57d734fd81a3dbd2dc9a/plugin/pulpcore/plugin/download/base.py#L64

h2. Usage

For example I can call it with:

<code>my_artifact = Artifact.create_and_validate(expected_digests={'sha256': Artifact.create(expected_digests={'sha256': 'long_digest_value_goes_here'})</code>

h2. Exception Raising

If the sha256 doesn't match, I expect it to raise a DigestValidationError or a SizeValidationError, just like the downloaders. See this docstring:

<pre>
:class:`~pulpcore.plugin.download.DigestValidationError`: When any of the
``expected_digest`` values don't match the digest of the data passed to
:meth:`~pulpcore.plugin.download.BaseDownloader.handle_data`.
:class:`~pulpcore.plugin.download.SizeValidationError`: When the
``expected_size`` value doesn't match the size of the data passed to
:meth:`~pulpcore.plugin.download.BaseDownloader.handle_data`.
</pre>

^ is from "here":https://github.com/pulp/pulp/blob/d9eb035dcf87679d00ae57d734fd81a3dbd2dc9a/plugin/pulpcore/plugin/download/base.py#L112-L117

Back