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/v1/tag.py

114 lines
3.6 KiB
Python
Raw Normal View History

2013-09-25 21:50:03 +00:00
import logging
import json
2013-09-25 21:50:03 +00:00
from flask import abort, request, jsonify, make_response, session
2013-09-25 21:50:03 +00:00
2016-07-12 17:48:44 +00:00
from auth.decorators import process_auth
2013-09-25 21:50:03 +00:00
from auth.permissions import (ReadRepositoryPermission,
ModifyRepositoryPermission)
from data import model
2016-09-23 21:50:09 +00:00
from data.interfaces.v1 import pre_oci_model as model
from endpoints.common import parse_repository_name
from endpoints.decorators import anon_protect
2015-06-22 21:37:13 +00:00
from endpoints.v1 import v1_bp
from util.audit import track_and_log
from util.names import TAG_ERROR, TAG_REGEX
2013-09-25 21:50:03 +00:00
logger = logging.getLogger(__name__)
@v1_bp.route('/repositories/<repopath:repository>/tags', methods=['GET'])
2013-09-25 21:50:03 +00:00
@process_auth
@anon_protect
2016-03-09 21:20:28 +00:00
@parse_repository_name()
def get_tags(namespace_name, repo_name):
permission = ReadRepositoryPermission(namespace_name, repo_name)
2013-09-25 21:50:03 +00:00
if permission.can() or model.repository_is_public(namespace_name, repo_name):
repo = model.get_repository(namespace_name, repo_name)
if repo.kind != 'image':
msg = 'This repository is for managing %s resources and not container images.' % repo.kind
abort(405, message=msg, namespace=namespace_name)
tags = model.list_tags(namespace_name, repo_name)
2013-10-01 21:23:44 +00:00
tag_map = {tag.name: tag.image.docker_image_id for tag in tags}
2013-09-25 21:50:03 +00:00
return jsonify(tag_map)
abort(403)
@v1_bp.route('/repositories/<repopath:repository>/tags/<tag>', methods=['GET'])
2013-09-25 21:50:03 +00:00
@process_auth
@anon_protect
2016-03-09 21:20:28 +00:00
@parse_repository_name()
def get_tag(namespace_name, repo_name, tag):
permission = ReadRepositoryPermission(namespace_name, repo_name)
2013-09-25 21:50:03 +00:00
if permission.can() or model.repository_is_public(namespace_name, repo_name):
repo = model.get_repository(namespace_name, repo_name)
if repo.kind != 'image':
msg = 'This repository is for managing %s resources and not container images.' % repo.kind
abort(405, message=msg, namespace=namespace_name)
image_id = model.find_image_id_by_tag(namespace_name, repo_name, tag)
2016-07-12 17:48:44 +00:00
if image_id is None:
abort(404)
2016-07-12 17:48:44 +00:00
resp = make_response('"%s"' % image_id)
resp.headers['Content-Type'] = 'application/json'
return resp
2013-09-25 21:50:03 +00:00
abort(403)
@v1_bp.route('/repositories/<repopath:repository>/tags/<tag>', methods=['PUT'])
2013-09-25 21:50:03 +00:00
@process_auth
@anon_protect
2016-03-09 21:20:28 +00:00
@parse_repository_name()
def put_tag(namespace_name, repo_name, tag):
permission = ModifyRepositoryPermission(namespace_name, repo_name)
2013-09-25 21:50:03 +00:00
if permission.can():
2015-10-05 20:36:33 +00:00
if not TAG_REGEX.match(tag):
abort(400, TAG_ERROR)
repo = model.get_repository(namespace_name, repo_name)
if repo.kind != 'image':
msg = 'This repository is for managing %s resources and not container images.' % repo.kind
abort(405, message=msg, namespace=namespace_name)
2016-07-12 17:48:44 +00:00
image_id = json.loads(request.data)
model.create_or_update_tag(namespace_name, repo_name, image_id, tag)
2013-09-25 21:50:03 +00:00
# Store the updated tag.
2016-03-09 21:20:28 +00:00
if 'pushed_tags' not in session:
session['pushed_tags'] = {}
2016-07-12 17:48:44 +00:00
session['pushed_tags'][tag] = image_id
2013-09-25 21:50:03 +00:00
return make_response('Created', 200)
abort(403)
@v1_bp.route('/repositories/<repopath:repository>/tags/<tag>', methods=['DELETE'])
2013-09-25 21:50:03 +00:00
@process_auth
@anon_protect
2016-03-09 21:20:28 +00:00
@parse_repository_name()
def delete_tag(namespace_name, repo_name, tag):
permission = ModifyRepositoryPermission(namespace_name, repo_name)
2013-09-25 21:50:03 +00:00
if permission.can():
repo = model.get_repository(namespace_name, repo_name)
if repo.kind != 'image':
msg = 'This repository is for managing %s resources and not container images.' % repo.kind
abort(405, message=msg, namespace=namespace_name)
model.delete_tag(namespace_name, repo_name, tag)
track_and_log('delete_tag', model.get_repository(namespace_name, repo_name), tag=tag)
return make_response('Deleted', 200)
2013-09-25 21:50:03 +00:00
abort(403)