Convert V2's tag endpoints to use the new data model interface

This commit is contained in:
Joseph Schorr 2018-10-08 13:13:31 +01:00
parent 6b5064aba4
commit e91ba98e1b
5 changed files with 31 additions and 7 deletions

View file

@ -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

View file

@ -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. """

View file

@ -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

View file

@ -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

View file

@ -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('/<repopath:repository>/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