2018-10-08 14:26:39 +01:00
|
|
|
from collections import namedtuple
|
2017-06-02 15:30:51 -07:00
|
|
|
|
2018-06-05 17:31:11 -04:00
|
|
|
from flask import jsonify
|
2015-09-29 15:53:38 -04:00
|
|
|
|
2018-10-08 14:26:39 +01:00
|
|
|
import features
|
|
|
|
|
2018-06-19 13:27:16 -04:00
|
|
|
from app import model_cache
|
|
|
|
from auth.auth_context import get_authenticated_user, get_authenticated_context
|
2018-01-05 16:27:03 -05:00
|
|
|
from auth.registry_jwt_auth import process_registry_jwt_auth
|
2018-10-08 14:26:39 +01:00
|
|
|
from data import model
|
2018-06-19 13:27:16 -04:00
|
|
|
from data.cache import cache_key
|
2015-09-29 15:53:38 -04:00
|
|
|
from endpoints.decorators import anon_protect
|
2016-08-01 20:48:00 -04:00
|
|
|
from endpoints.v2 import v2_bp, paginate
|
2018-10-08 14:26:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Repository(namedtuple('Repository', ['id', 'namespace_name', 'name'])):
|
|
|
|
pass
|
2015-09-29 15:53:38 -04:00
|
|
|
|
2017-06-26 18:16:15 -04:00
|
|
|
|
2015-09-29 15:53:38 -04:00
|
|
|
@v2_bp.route('/_catalog', methods=['GET'])
|
2016-03-09 18:09:20 -05:00
|
|
|
@process_registry_jwt_auth()
|
2015-09-29 15:53:38 -04:00
|
|
|
@anon_protect
|
2016-08-01 20:48:00 -04:00
|
|
|
@paginate()
|
2018-06-18 16:11:26 -04:00
|
|
|
def catalog_search(start_id, limit, pagination_callback):
|
2018-06-19 13:27:16 -04:00
|
|
|
def _load_catalog():
|
|
|
|
include_public = bool(features.PUBLIC_CATALOG)
|
|
|
|
if not include_public and not get_authenticated_user():
|
|
|
|
return []
|
2018-06-05 17:31:00 -04:00
|
|
|
|
2018-06-19 13:27:16 -04:00
|
|
|
username = get_authenticated_user().username if get_authenticated_user() else None
|
|
|
|
if username and not get_authenticated_user().enabled:
|
|
|
|
return []
|
|
|
|
|
2018-10-08 14:26:39 +01:00
|
|
|
query = model.repository.get_visible_repositories(username,
|
|
|
|
kind_filter='image',
|
|
|
|
include_public=include_public,
|
|
|
|
start_id=start_id,
|
|
|
|
limit=limit + 1)
|
|
|
|
# NOTE: The repository ID is in `rid` (not `id`) here, as per the requirements of
|
|
|
|
# the `get_visible_repositories` call.
|
|
|
|
return [Repository(repo.rid, repo.namespace_user.username, repo.name)._asdict()
|
|
|
|
for repo in query]
|
2018-06-19 13:27:16 -04:00
|
|
|
|
|
|
|
context_key = get_authenticated_context().unique_key if get_authenticated_context() else None
|
|
|
|
catalog_cache_key = cache_key.for_catalog_page(context_key, start_id, limit)
|
|
|
|
visible_repositories = [Repository(**repo_dict) for repo_dict
|
|
|
|
in model_cache.retrieve(catalog_cache_key, _load_catalog)]
|
2018-06-05 17:31:00 -04:00
|
|
|
|
2015-09-29 15:53:38 -04:00
|
|
|
response = jsonify({
|
2016-07-26 18:41:51 -04:00
|
|
|
'repositories': ['%s/%s' % (repo.namespace_name, repo.name)
|
2018-06-18 16:11:26 -04:00
|
|
|
for repo in visible_repositories][0:limit],
|
|
|
|
})
|
2015-09-29 15:53:38 -04:00
|
|
|
|
2018-06-18 16:11:26 -04:00
|
|
|
pagination_callback(visible_repositories, response)
|
2015-09-29 15:53:38 -04:00
|
|
|
return response
|