Skip to content

pulpcore.plugin.util

All utils documented here should be imported directly from the pulpcore.plugin.util namespace.

pulpcore.plugin.util

assign_role(rolename, entity, obj=None, domain=None)

Assign a role to a user or a group.

Parameters:

  • rolename (str) –

    Name of the role to assign.

  • entity (User or Group) –

    Entity to gain the role.

  • obj (Optional[BaseModel], default: None ) –

    Object the role permisssions are to be asserted on.

  • domain (Optional[Domain], default: None ) –

    Domain the role permissions are to be asserted on. Mutually exclusive with obj.

remove_role(rolename, entity, obj=None, domain=None)

Remove a role from a user or a group.

Parameters:

  • rolename (str) –

    Name of the role to assign.

  • entity (User or Group) –

    Entity to lose the role.

  • obj (Optional[BaseModel], default: None ) –

    Object the role permisssions are to be asserted on.

  • domain (Optional[Domain], default: None ) –

    Domain the role permissions are to be asserted on. Mutually exclusive with obj.

batch_qs(qs, batch_size=1000)

Returns a queryset batch from the given queryset.

Make sure to order the queryset.

Parameters:

  • qs

    The queryset we want to iterate over in batches.

  • batch_size

    Defaults to 1000.

Example

To iterate over a queryset while retrieving records from the DB in batches, use::

article_qs = Article.objects.order_by('id')
for qs in batch_qs(article_qs):
    for article in qs:
        print article.body

cache_key(base_path)

Returns the base-key(s) used in the Cache for the passed base_path(s).

extract_pk(uri, only_prn=False)

Resolve a resource URI or PRN to a simple PK value.

Provides a means to resolve an href/prn passed in a POST body to a primary key. Doesn't assume anything about whether the resource corresponding to the URI/PRN passed in actually exists.

Note

Cannot be used with nested URIs where the PK of the final resource is not present. e.g. RepositoryVersion URI consists of repository PK and version number - no RepositoryVersion PK is present within the URI.

Parameters:

  • uri (str) –

    A resource URI/PRN.

  • only_prn (bool, default: False ) –

    Ensure passed in value is only a valid PRN

Returns:

  • primary_key ( uuid4 ) –

    The primary key of the resource extracted from the URI/PRN.

Raises:

  • ValidationError

    on invalid URI/PRN.

get_artifact_url(artifact, headers=None, http_method=None)

Get artifact url.

Plugins can use this method to generate a pre-authenticated URL to the artifact in the configured storage backend. This can be used to facilitate external services for validation of the content.

This method will generate redirect links to the configured external object storage, or to the special "artifact redirect" distribution in the content-app top serve from the local filesystem or private cloud storage.

get_prn(instance=None, uri=None)

Get a Pulp Resource Name (PRN) for the specified model instance. It is similar to a HREF url in that it uniquely identifies a resource, but it also has the guarantee that it will not change regardless of API_ROOT or DOMAIN_ENABLED. This is used in our resource locking/ reservation code to identify resources.

The format for the PRN is as follows:

    prn:model-label-lower:pk

Examples:

instance=FileRepository(pk=123) -> prn:file.filerepository:123 instance=Artifact(pk=abc) -> prn:core.artifact:abc uri=/rerouted/api/v3/repositories/rpm/rpm/123/versions/2/ -> prn:core.repositoryversion:abc uri=/pulp/foodomain/api/v3/content/ansible/role/123/ -> prn:ansible.role:123

Parameters:

  • instance (Optional(django.models.Model, default: None ) –

    A model instance.

  • uri (Optional(str, default: None ) –

    A resource URI

Returns:

  • prn ( str ) –

    The PRN of the passed in resource

get_url(model, domain=None, request=None)

Get a resource url for the specified model instance or class. This returns the path component of the resource URI.

Parameters:

  • model (Model) –

    A model instance or class.

  • domain (Optional(str or Domain, default: None ) –

    The domain the url should be in if DOMAIN_ENABLED is set and

  • request (Optional(django.http.HttpRequest, default: None ) –

    The request object this url is being created for.

Returns:

  • str

    The path component of the resource url

gpg_verify(public_keys, signature, detached_data=None)

Check whether the provided gnupg signature is valid for one of the provided public keys.

Parameters:

  • public_keys (str) –

    Ascii armored public key data

  • signature ((str, file - like, Artifact)) –

    The signature data as a path or as a file-like object

  • detached_data (str) [optional], default: None ) –

    The filesystem path to signed data in case of a detached signature

Returns:

  • gnupg.Verify: The result of the verification

Raises:

raise_for_unknown_content_units(existing_content_units, content_units_pks_hrefs, domain=None)

Verify if all the specified content units were found in the database.

Parameters:

  • existing_content_units (Content) –

    Content filtered by specified_content_units.

  • content_units_pks_hrefs (dict) –

    An original dictionary of pk-href pairs that are used for the verification.

  • domain (Domain, default: None ) –

    A domain to use for verifying that all the content units are within the same domain. defaults to current domain.

Raises: ValidationError: If some of the referenced content units are not present in the database or content units are from multiple domains

reverse(viewname, args=None, kwargs=None, request=None, relative_url=True, **extra)

Customized reverse to handle Pulp specific parameters like domains and API_ROOT rewrite.

Calls DRF's reverse, but with request as None if relative_url is True (default) so that the returned url is always relative.

resolve_prn(prn)

Resolve a PRN to its model and pk.

Parameters:

  • prn (str) –

    The PRN to resolve.

Returns:

  • model_pk_tuple ( tuple ) –

    A tuple of the model class and pk of the PRN

Raises:

  • ValidationError

    on invalid PRN.