2016-07-18 22:20:00 +00:00
|
|
|
""" Manage the manifests of a repository. """
|
|
|
|
|
|
|
|
from app import label_validator
|
|
|
|
from flask import request
|
|
|
|
from endpoints.api import (resource, nickname, require_repo_read, require_repo_write,
|
|
|
|
RepositoryParamResource, log_action, validate_json_request,
|
2017-07-20 18:24:43 +00:00
|
|
|
path_param, parse_args, query_param, abort, api,
|
2017-03-22 18:30:13 +00:00
|
|
|
disallow_for_app_repositories)
|
2016-07-18 22:20:00 +00:00
|
|
|
from endpoints.exception import NotFound
|
2017-07-20 18:24:43 +00:00
|
|
|
from manifest_models_pre_oci import pre_oci_model as model
|
|
|
|
from data.model import InvalidLabelKeyException, InvalidMediaTypeException
|
2016-07-18 22:20:00 +00:00
|
|
|
|
|
|
|
from digest import digest_tools
|
2017-06-20 19:50:46 +00:00
|
|
|
from util.validation import VALID_LABEL_KEY_REGEX
|
2016-07-18 22:20:00 +00:00
|
|
|
|
|
|
|
BASE_MANIFEST_ROUTE = '/v1/repository/<apirepopath:repository>/manifest/<regex("{0}"):manifestref>'
|
|
|
|
MANIFEST_DIGEST_ROUTE = BASE_MANIFEST_ROUTE.format(digest_tools.DIGEST_PATTERN)
|
|
|
|
ALLOWED_LABEL_MEDIA_TYPES = ['text/plain', 'application/json']
|
|
|
|
|
|
|
|
|
|
|
|
@resource(MANIFEST_DIGEST_ROUTE + '/labels')
|
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
|
|
|
@path_param('manifestref', 'The digest of the manifest')
|
|
|
|
class RepositoryManifestLabels(RepositoryParamResource):
|
|
|
|
""" Resource for listing the labels on a specific repository manifest. """
|
|
|
|
schemas = {
|
|
|
|
'AddLabel': {
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'Adds a label to a manifest',
|
|
|
|
'required': [
|
|
|
|
'key',
|
|
|
|
'value',
|
|
|
|
'media_type',
|
|
|
|
],
|
|
|
|
'properties': {
|
|
|
|
'key': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The key for the label',
|
|
|
|
},
|
|
|
|
'value': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The value for the label',
|
|
|
|
},
|
|
|
|
'media_type': {
|
2017-03-10 00:04:35 +00:00
|
|
|
'type': ['string', 'null'],
|
2016-07-18 22:20:00 +00:00
|
|
|
'description': 'The media type for this label',
|
2017-03-10 00:04:35 +00:00
|
|
|
'enum': ALLOWED_LABEL_MEDIA_TYPES + [None],
|
2016-07-18 22:20:00 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
@require_repo_read
|
|
|
|
@nickname('listManifestLabels')
|
2017-03-22 18:30:13 +00:00
|
|
|
@disallow_for_app_repositories
|
2016-07-18 22:20:00 +00:00
|
|
|
@parse_args()
|
|
|
|
@query_param('filter', 'If specified, only labels matching the given prefix will be returned',
|
|
|
|
type=str, default=None)
|
2017-07-20 18:24:43 +00:00
|
|
|
def get(self, namespace_name, repository_name, manifestref, parsed_args):
|
|
|
|
labels = model.get_manifest_labels(namespace_name, repository_name, manifestref, filter=parsed_args['filter'])
|
|
|
|
if labels is None:
|
2016-07-18 22:20:00 +00:00
|
|
|
raise NotFound()
|
|
|
|
return {
|
2017-07-20 18:24:43 +00:00
|
|
|
'labels': [label.to_dict() for label in labels]
|
2016-07-18 22:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@require_repo_write
|
|
|
|
@nickname('addManifestLabel')
|
2017-03-22 18:30:13 +00:00
|
|
|
@disallow_for_app_repositories
|
2016-07-18 22:20:00 +00:00
|
|
|
@validate_json_request('AddLabel')
|
2017-07-20 18:24:43 +00:00
|
|
|
def post(self, namespace_name, repository_name, manifestref):
|
2016-07-18 22:20:00 +00:00
|
|
|
""" Adds a new label into the tag manifest. """
|
|
|
|
label_data = request.get_json()
|
|
|
|
|
|
|
|
# Check for any reserved prefixes.
|
|
|
|
if label_validator.has_reserved_prefix(label_data['key']):
|
|
|
|
abort(400, message='Label has a reserved prefix')
|
|
|
|
|
2017-06-20 19:50:46 +00:00
|
|
|
label = None
|
|
|
|
try:
|
2017-07-20 18:24:43 +00:00
|
|
|
label = model.create_manifest_label(namespace_name,
|
|
|
|
repository_name,
|
|
|
|
manifestref,
|
|
|
|
label_data['key'],
|
|
|
|
label_data['value'],
|
|
|
|
'api',
|
|
|
|
label_data['media_type'])
|
|
|
|
except InvalidLabelKeyException:
|
2017-06-20 19:50:46 +00:00
|
|
|
abort(400, message='Label is of an invalid format or missing please use %s format for labels'.format(
|
|
|
|
VALID_LABEL_KEY_REGEX))
|
2017-07-20 18:24:43 +00:00
|
|
|
except InvalidMediaTypeException:
|
2017-06-20 19:50:46 +00:00
|
|
|
abort(400, message='Media type is invalid please use a valid media type of text/plain or application/json')
|
|
|
|
|
2017-07-20 18:24:43 +00:00
|
|
|
if label is None:
|
|
|
|
raise NotFound()
|
|
|
|
|
2016-07-18 22:20:00 +00:00
|
|
|
metadata = {
|
|
|
|
'id': label.uuid,
|
2017-07-20 18:24:43 +00:00
|
|
|
'key': label.key,
|
|
|
|
'value': label.value,
|
2016-07-18 22:20:00 +00:00
|
|
|
'manifest_digest': manifestref,
|
2017-07-20 18:24:43 +00:00
|
|
|
'media_type': label.media_type_name,
|
|
|
|
'namespace': namespace_name,
|
|
|
|
'repo': repository_name,
|
2016-07-18 22:20:00 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 18:24:43 +00:00
|
|
|
log_action('manifest_label_add', namespace_name, metadata, repo_name=repository_name)
|
2016-07-18 22:20:00 +00:00
|
|
|
|
2017-07-20 18:24:43 +00:00
|
|
|
resp = {'label': label.to_dict()}
|
|
|
|
repo_string = '%s/%s' % (namespace_name, repository_name)
|
2016-07-18 22:20:00 +00:00
|
|
|
headers = {
|
|
|
|
'Location': api.url_for(ManageRepositoryManifestLabel, repository=repo_string,
|
|
|
|
manifestref=manifestref, labelid=label.uuid),
|
|
|
|
}
|
|
|
|
return resp, 201, headers
|
|
|
|
|
|
|
|
|
|
|
|
@resource(MANIFEST_DIGEST_ROUTE + '/labels/<labelid>')
|
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
|
|
|
@path_param('manifestref', 'The digest of the manifest')
|
|
|
|
@path_param('labelid', 'The ID of the label')
|
|
|
|
class ManageRepositoryManifestLabel(RepositoryParamResource):
|
|
|
|
""" Resource for managing the labels on a specific repository manifest. """
|
|
|
|
@require_repo_read
|
|
|
|
@nickname('getManifestLabel')
|
2017-03-22 18:30:13 +00:00
|
|
|
@disallow_for_app_repositories
|
2017-07-20 18:24:43 +00:00
|
|
|
def get(self, namespace_name, repository_name, manifestref, labelid):
|
2016-07-18 22:20:00 +00:00
|
|
|
""" Retrieves the label with the specific ID under the manifest. """
|
2017-07-20 18:24:43 +00:00
|
|
|
label = model.get_manifest_label(namespace_name, repository_name, manifestref, labelid)
|
2016-07-18 22:20:00 +00:00
|
|
|
if label is None:
|
|
|
|
raise NotFound()
|
|
|
|
|
2017-07-20 18:24:43 +00:00
|
|
|
return label.to_dict()
|
2016-07-18 22:20:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
@require_repo_write
|
|
|
|
@nickname('deleteManifestLabel')
|
2017-03-22 18:30:13 +00:00
|
|
|
@disallow_for_app_repositories
|
2017-07-20 18:24:43 +00:00
|
|
|
def delete(self, namespace_name, repository_name, manifestref, labelid):
|
2016-07-18 22:20:00 +00:00
|
|
|
""" Deletes an existing label from a manifest. """
|
2017-07-20 18:24:43 +00:00
|
|
|
deleted = model.delete_manifest_label(namespace_name, repository_name, manifestref, labelid)
|
2016-07-18 22:20:00 +00:00
|
|
|
if deleted is None:
|
|
|
|
raise NotFound()
|
|
|
|
|
|
|
|
metadata = {
|
|
|
|
'id': labelid,
|
|
|
|
'key': deleted.key,
|
|
|
|
'value': deleted.value,
|
2017-02-14 19:55:24 +00:00
|
|
|
'manifest_digest': manifestref,
|
2017-07-20 18:24:43 +00:00
|
|
|
'namespace': namespace_name,
|
|
|
|
'repo': repository_name,
|
2016-07-18 22:20:00 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 18:24:43 +00:00
|
|
|
log_action('manifest_label_delete', namespace_name, metadata, repo_name=repository_name)
|
2016-12-06 21:26:28 +00:00
|
|
|
return '', 204
|
2016-07-18 22:20:00 +00:00
|
|
|
|