Skip to content

Commit

Permalink
Allow content to be filtered during copy
Browse files Browse the repository at this point in the history
  • Loading branch information
David Davis authored and dralley committed Mar 9, 2020
1 parent 61ad7d5 commit a7d75a9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGES/6009.feature
@@ -0,0 +1 @@
Add a criteria parameter to the copy api that can be used to filter content to by copied.
31 changes: 25 additions & 6 deletions pulp_rpm/app/tasks/copy.py
Expand Up @@ -5,23 +5,42 @@
from pulp_rpm.app.models import RpmRepository


def _filter_content(content, criteria):
"""
Filter content in the source repository version by criteria.
Args:
content: a queryset of content to filter
criteria: a validated dict that maps content type to a list of filter criteria
"""
if not criteria:
return content

content_pks = []
for content_type in RpmRepository.CONTENT_TYPES:
if criteria.get(content_type.TYPE):
filters = Q()
for filter in criteria[content_type.TYPE]:
filters |= Q(**filter)
content_pks += content_type.objects.filter(filters).values_list("pk", flat=True)

return content.filter(pk__in=content_pks)


def copy_content(source_repo_version_pk, dest_repo_pk, criteria, dependency_solving):
"""
Copy content from one repo to another.
Args:
source_repo_version_pk: repository version primary key to copy units from
dest_repo_pk: repository primary key to copy units into
types: a tuple of strings representing the '_type' values of types to include in the copy
criteria: a dict that maps type to a list of criteria to filter content by. Note that this
criteria MUST be validated before being passed to this task.
"""
source_repo_version = RepositoryVersion.objects.get(pk=source_repo_version_pk)
source_repo = RpmRepository.objects.get(pk=source_repo_version.repository)
content_types = source_repo.CONTENT_TYPES
list(content_types)

dest_repo = RpmRepository.objects.get(pk=dest_repo_pk)

content_to_copy = source_repo_version.content
content_to_copy = _filter_content(source_repo_version.content, criteria)

with dest_repo.new_version() as new_version:
new_version.add_content(content_to_copy)
14 changes: 11 additions & 3 deletions pulp_rpm/tests/functional/api/test_copy.py
Expand Up @@ -68,10 +68,8 @@ def _do_test(self, criteria, expected_results):
get_added_content_summary(source_repo), RPM_FIXTURE_SUMMARY
)

# Copy all RPMs
criteria = {}
rpm_copy(self.cfg, source_repo, dest_repo, criteria)
dest_repo = self.client.get(source_repo['pulp_href'])
dest_repo = self.client.get(dest_repo['pulp_href'])

# Check that we have the correct content counts.
self.assertDictEqual(get_content_summary(dest_repo), expected_results)
Expand All @@ -89,3 +87,13 @@ def test_copy_all(self):
# criteria = {}
# results = {}
# self._do_test(criteria, results)

def test_copy_by_advisory_id(self):
"""Test copying an advisory by its id."""
criteria = {
'advisory': [{'id': 'RHEA-2012:0056'}]
}
results = {
RPM_ADVISORY_CONTENT_NAME: 1
}
self._do_test(criteria, results)

0 comments on commit a7d75a9

Please sign in to comment.