This repository has been archived on 2020-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
quay/endpoints/api/repository.py

171 lines
5.5 KiB
Python

import logging
import json
from functools import wraps
from flask.ext.restful import Resource, Api, reqparse, abort, fields
from flask.ext.login import current_user
from data import model
from endpoints.api import api, truthy_bool, format_date, nickname
from util.names import parse_namespace_repository
from auth.permissions import (ReadRepositoryPermission,
ModifyRepositoryPermission,
AdministerRepositoryPermission)
logger = logging.getLogger(__name__)
repo_api = Api(api)
def parse_repository_name(f):
@wraps(f)
def wrapper(repository, *args, **kwargs):
(namespace, repository) = parse_namespace_repository(repository)
return f(namespace, repository, *args, **kwargs)
return wrapper
class RepositoryParamResource(Resource):
method_decorators = [parse_repository_name]
def resource(*urls, **kwargs):
def wrapper(api_resource):
repo_api.add_resource(api_resource, *urls, **kwargs)
return api_resource
return wrapper
def require_repo_permission(permission_class, allow_public=False):
def wrapper(func):
@wraps(func)
def wrapped(self, namespace, repository, *args, **kwargs):
permission = permission_class(namespace, repository)
if (permission.can() or
(allow_public and
model.repository_is_public(namespace, repository))):
return func(self, namespace, repository, *args, **kwargs)
abort(403)
func.__required_permission = 'read'
return wrapped
return wrapper
require_repo_read = require_repo_permission(ReadRepositoryPermission, True)
require_repo_write = require_repo_permission(ModifyRepositoryPermission)
require_repo_admin = require_repo_permission(AdministerRepositoryPermission)
@resource('/v1/repository')
class RepositoryList(Resource):
@nickname('createRepo')
def post(self):
pass
@nickname('listRepos')
def get(self):
parser = reqparse.RequestParser()
parser.add_argument('page', type=int, help='Page number must be an int.')
parser.add_argument('limit', type=int, help='Limit must be an int.')
parser.add_argument('namespace', type=str)
parser.add_argument('public', type=truthy_bool, default=True)
parser.add_argument('private', type=truthy_bool, default=True)
parser.add_argument('sort', type=truthy_bool, default=False)
parser.add_argument('count', type=truthy_bool, default=False)
args = parser.parse_args()
def repo_view(repo_obj):
return {
'namespace': repo_obj.namespace,
'name': repo_obj.name,
'description': repo_obj.description,
'is_public': repo_obj.visibility.name == 'public',
}
username = None
if current_user.is_authenticated() and args['private']:
username = current_user.db_user().username
response = {}
repo_count = None
if args['count']:
repo_count = model.get_visible_repository_count(username,
include_public=args['public'],
namespace=args['namespace'])
response['count'] = repo_count
repo_query = model.get_visible_repositories(username, limit=args['limit'],
page=args['page'],
include_public=args['public'],
sort=args['sort'],
namespace=args['namespace'])
response['repositories'] = [repo_view(repo) for repo in repo_query]
return response
def image_view(image):
extended_props = image
if image.storage and image.storage.id:
extended_props = image.storage
command = extended_props.command
return {
'id': image.docker_image_id,
'created': format_date(extended_props.created),
'comment': extended_props.comment,
'command': json.loads(command) if command else None,
'ancestors': image.ancestors,
'dbid': image.id,
'size': extended_props.image_size,
}
@resource('/v1/repository/<path:repository>')
class Repository(RepositoryParamResource):
@require_repo_read
@nickname('getRepo')
def get(self, namespace, repository):
logger.debug('Get repo: %s/%s' % (namespace, repository))
def tag_view(tag):
image = model.get_tag_image(namespace, repository, tag.name)
if not image:
return {}
return {
'name': tag.name,
'image': image_view(image),
}
organization = None
try:
organization = model.get_organization(namespace)
except model.InvalidOrganizationException:
pass
is_public = model.repository_is_public(namespace, repository)
repo = model.get_repository(namespace, repository)
if repo:
tags = model.list_repository_tags(namespace, repository)
tag_dict = {tag.name: tag_view(tag) for tag in tags}
can_write = ModifyRepositoryPermission(namespace, repository).can()
can_admin = AdministerRepositoryPermission(namespace, repository).can()
active_builds = model.list_repository_builds(namespace, repository, 1,
include_inactive=False)
return {
'namespace': namespace,
'name': repository,
'description': repo.description,
'tags': tag_dict,
'can_write': can_write,
'can_admin': can_admin,
'is_public': is_public,
'is_building': len(list(active_builds)) > 0,
'is_organization': bool(organization),
'status_token': repo.badge_token if not is_public else ''
}
abort(404) # Not found