From e91ba98e1b36039d0d504701b22fcc1ad27fa718 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Mon, 8 Oct 2018 13:13:31 +0100 Subject: [PATCH] Convert V2's tag endpoints to use the new data model interface --- data/model/tag.py | 8 +++++++- data/registry_model/datatypes.py | 5 +++++ data/registry_model/interface.py | 4 +++- data/registry_model/registry_pre_oci_model.py | 6 ++++-- endpoints/v2/tag.py | 15 ++++++++++++--- 5 files changed, 31 insertions(+), 7 deletions(-) diff --git a/data/model/tag.py b/data/model/tag.py index 77ac3bee8..705ebe6dc 100644 --- a/data/model/tag.py +++ b/data/model/tag.py @@ -204,7 +204,7 @@ def get_tag_manifest_digests(tags): return {manifest.tag_id: manifest.digest for manifest in manifests} -def list_active_repo_tags(repo): +def list_active_repo_tags(repo, start_id=None, limit=None): """ Returns all of the active, non-hidden tags in a repository, joined to they images and (if present), their manifest. """ @@ -216,6 +216,12 @@ def list_active_repo_tags(repo): .switch(RepositoryTag) .join(TagManifest, JOIN.LEFT_OUTER)) + if start_id is not None: + query = query.where(RepositoryTag.id >= start_id) + + if limit is not None: + query = query.limit(limit) + return query diff --git a/data/registry_model/datatypes.py b/data/registry_model/datatypes.py index f36d34d79..78f038479 100644 --- a/data/registry_model/datatypes.py +++ b/data/registry_model/datatypes.py @@ -117,6 +117,11 @@ class Tag(datatype('Tag', ['name', 'reversion', 'manifest_digest', 'lifetime_sta """ return legacy_image + @property + def id(self): + """ The ID of this tag for pagination purposes only. """ + return self._db_id + class Manifest(datatype('Manifest', ['digest', 'media_type', 'manifest_bytes'])): """ Manifest represents a manifest in a repository. """ diff --git a/data/registry_model/interface.py b/data/registry_model/interface.py index 3376ef5f4..f7a1dadc5 100644 --- a/data/registry_model/interface.py +++ b/data/registry_model/interface.py @@ -95,7 +95,9 @@ class RegistryDataInterface(object): """ @abstractmethod - def list_repository_tags(self, repository_ref, include_legacy_images=False): + def list_repository_tags(self, repository_ref, include_legacy_images=False, + start_pagination_id=None, + limit=None): """ Returns a list of all the active tags in the repository. Note that this can be a *heavy* operation on repositories with a lot of tags, and should be avoided for more targetted diff --git a/data/registry_model/registry_pre_oci_model.py b/data/registry_model/registry_pre_oci_model.py index 5297a92c4..e119852b6 100644 --- a/data/registry_model/registry_pre_oci_model.py +++ b/data/registry_model/registry_pre_oci_model.py @@ -287,7 +287,9 @@ class PreOCIModel(RegistryDataInterface): """ return Label.for_label(model.label.delete_manifest_label(label_uuid, manifest._db_id)) - def list_repository_tags(self, repository_ref, include_legacy_images=False): + def list_repository_tags(self, repository_ref, include_legacy_images=False, + start_pagination_id=None, + limit=None): """ Returns a list of all the active tags in the repository. Note that this can be a *heavy* operation on repositories with a lot of tags, and should be avoided for more targetted @@ -296,7 +298,7 @@ class PreOCIModel(RegistryDataInterface): # NOTE: include_legacy_images isn't used here because `list_active_repo_tags` includes the # information already, so we might as well just use it. However, the new model classes will # *not* include it by default, so we make it a parameter now. - tags = model.tag.list_active_repo_tags(repository_ref._db_id) + tags = model.tag.list_active_repo_tags(repository_ref._db_id, start_pagination_id, limit) return [Tag.for_repository_tag(tag, legacy_image=LegacyImage.for_image(tag.image), manifest_digest=(tag.tagmanifest.digest diff --git a/endpoints/v2/tag.py b/endpoints/v2/tag.py index cc2867aa7..fec91f11e 100644 --- a/endpoints/v2/tag.py +++ b/endpoints/v2/tag.py @@ -1,9 +1,10 @@ from flask import jsonify from auth.registry_jwt_auth import process_registry_jwt_auth +from data.registry_model import registry_model from endpoints.decorators import anon_protect, parse_repository_name from endpoints.v2 import v2_bp, require_repo_read, paginate -from endpoints.v2.models_pre_oci import data_model as model +from endpoints.v2.errors import NameUnknown @v2_bp.route('//tags/list', methods=['GET']) @@ -13,10 +14,18 @@ from endpoints.v2.models_pre_oci import data_model as model @anon_protect @paginate() def list_all_tags(namespace_name, repo_name, start_id, limit, pagination_callback): - tags = list(model.repository_tags(namespace_name, repo_name, start_id, limit)) + repository_ref = registry_model.lookup_repository(namespace_name, repo_name) + if repository_ref is None: + raise NameUnknown() + + # NOTE: We add 1 to the limit because that's how pagination_callback knows if there are + # additional tags. + tags = registry_model.list_repository_tags(repository_ref, start_pagination_id=start_id, + limit=limit + 1) response = jsonify({ 'name': '{0}/{1}'.format(namespace_name, repo_name), - 'tags': [tag.name for tag in tags][0:limit],}) + 'tags': [tag.name for tag in tags][0:limit], + }) pagination_callback(tags, response) return response