2015-05-14 20:47:38 +00:00
|
|
|
""" List, create and manage repository events/notifications. """
|
|
|
|
|
2017-05-18 21:52:50 +00:00
|
|
|
import logging
|
2015-07-15 21:25:41 +00:00
|
|
|
from flask import request
|
2014-07-16 20:30:47 +00:00
|
|
|
|
|
|
|
from endpoints.api import (RepositoryParamResource, nickname, resource, require_repo_admin,
|
2016-04-11 20:20:11 +00:00
|
|
|
log_action, validate_json_request, request_error,
|
2017-03-22 18:30:13 +00:00
|
|
|
path_param, disallow_for_app_repositories)
|
2017-07-14 13:09:56 +00:00
|
|
|
from endpoints.exception import NotFound
|
2017-07-14 13:29:59 +00:00
|
|
|
from notifications import build_notification_data
|
2017-07-14 13:09:56 +00:00
|
|
|
from notifications.notificationevent import NotificationEvent
|
|
|
|
from notifications.notificationmethod import (NotificationMethod,
|
|
|
|
CannotValidateNotificationMethodException)
|
2017-07-13 19:43:26 +00:00
|
|
|
from workers.notificationworker.models_pre_oci import notification
|
2017-07-17 21:56:32 +00:00
|
|
|
from endpoints.api.repositorynotification_models_pre_oci import pre_oci_model as model
|
2017-07-13 19:43:26 +00:00
|
|
|
|
2017-05-18 21:52:50 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2014-07-16 20:30:47 +00:00
|
|
|
|
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
@resource('/v1/repository/<apirepopath:repository>/notification/')
|
2014-08-19 23:05:28 +00:00
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
2014-07-18 02:51:58 +00:00
|
|
|
class RepositoryNotificationList(RepositoryParamResource):
|
2014-07-16 20:30:47 +00:00
|
|
|
""" Resource for dealing with listing and creating notifications on a repository. """
|
|
|
|
schemas = {
|
|
|
|
'NotificationCreateRequest': {
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'Information for creating a notification on a repository',
|
|
|
|
'required': [
|
|
|
|
'event',
|
|
|
|
'method',
|
2015-12-03 23:28:07 +00:00
|
|
|
'config',
|
|
|
|
'eventConfig',
|
2014-07-16 20:30:47 +00:00
|
|
|
],
|
|
|
|
'properties': {
|
|
|
|
'event': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The event on which the notification will respond',
|
|
|
|
},
|
|
|
|
'method': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The method of notification (such as email or web callback)',
|
|
|
|
},
|
|
|
|
'config': {
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'JSON config information for the specific method of notification'
|
2015-08-17 20:30:15 +00:00
|
|
|
},
|
2015-10-13 22:14:52 +00:00
|
|
|
'eventConfig': {
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'JSON config information for the specific event of notification',
|
|
|
|
},
|
2015-08-17 20:30:15 +00:00
|
|
|
'title': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The human-readable title of the notification',
|
|
|
|
},
|
2014-07-16 20:30:47 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
@require_repo_admin
|
|
|
|
@nickname('createRepoNotification')
|
2017-03-22 18:30:13 +00:00
|
|
|
@disallow_for_app_repositories
|
2014-07-16 20:30:47 +00:00
|
|
|
@validate_json_request('NotificationCreateRequest')
|
2017-07-17 21:53:08 +00:00
|
|
|
def post(self, namespace_name, repository_name):
|
2015-07-15 21:25:41 +00:00
|
|
|
parsed = request.get_json()
|
2017-07-14 13:09:56 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
method_handler = NotificationMethod.get_method(parsed['method'])
|
2014-07-28 18:58:12 +00:00
|
|
|
try:
|
2017-07-17 21:55:00 +00:00
|
|
|
method_handler.validate(namespace_name, repository_name, parsed['config'])
|
2014-07-28 18:58:12 +00:00
|
|
|
except CannotValidateNotificationMethodException as ex:
|
|
|
|
raise request_error(message=ex.message)
|
2017-07-14 13:09:56 +00:00
|
|
|
|
|
|
|
new_notification = model.create_repo_notification(namespace_name, repository_name,
|
|
|
|
parsed['event'],
|
|
|
|
parsed['method'],
|
|
|
|
parsed['config'],
|
|
|
|
parsed['eventConfig'],
|
2017-07-17 21:53:08 +00:00
|
|
|
parsed.get('title'))
|
|
|
|
|
|
|
|
log_action('add_repo_notification', namespace_name,
|
|
|
|
{'repo': repository_name, 'namespace': namespace_name,
|
2017-02-14 19:55:24 +00:00
|
|
|
'notification_id': new_notification.uuid,
|
2017-07-17 21:56:32 +00:00
|
|
|
'event': new_notification.event_name, 'method': new_notification.method_name},
|
2017-07-17 21:55:00 +00:00
|
|
|
repo_name=repository_name)
|
2017-07-17 21:53:08 +00:00
|
|
|
return new_notification.to_dict(), 201
|
2014-07-16 20:30:47 +00:00
|
|
|
|
|
|
|
@require_repo_admin
|
|
|
|
@nickname('listRepoNotifications')
|
2017-03-22 18:30:13 +00:00
|
|
|
@disallow_for_app_repositories
|
2017-07-17 21:53:08 +00:00
|
|
|
def get(self, namespace_name, repository_name):
|
2014-07-16 20:30:47 +00:00
|
|
|
""" List the notifications for the specified repository. """
|
2017-07-17 21:53:08 +00:00
|
|
|
notifications = model.list_repo_notifications(namespace_name, repository_name)
|
2014-07-16 20:30:47 +00:00
|
|
|
return {
|
2017-07-17 21:53:08 +00:00
|
|
|
'notifications': [n.to_dict() for n in notifications]
|
2014-07-16 20:30:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
@resource('/v1/repository/<apirepopath:repository>/notification/<uuid>')
|
2014-08-19 23:05:28 +00:00
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
|
|
|
@path_param('uuid', 'The UUID of the notification')
|
2014-07-18 02:51:58 +00:00
|
|
|
class RepositoryNotification(RepositoryParamResource):
|
2014-07-16 20:30:47 +00:00
|
|
|
""" Resource for dealing with specific notifications. """
|
|
|
|
@require_repo_admin
|
|
|
|
@nickname('getRepoNotification')
|
2017-03-22 18:30:13 +00:00
|
|
|
@disallow_for_app_repositories
|
2017-07-17 21:53:08 +00:00
|
|
|
def get(self, namespace_name, repository_name, uuid):
|
2014-07-16 20:30:47 +00:00
|
|
|
""" Get information for the specified notification. """
|
2017-07-17 21:56:32 +00:00
|
|
|
found = model.get_repo_notification(uuid)
|
|
|
|
if not found:
|
2014-07-16 20:30:47 +00:00
|
|
|
raise NotFound()
|
2017-07-17 21:53:08 +00:00
|
|
|
return found.to_dict()
|
2014-07-16 20:30:47 +00:00
|
|
|
|
|
|
|
@require_repo_admin
|
|
|
|
@nickname('deleteRepoNotification')
|
2017-03-22 18:30:13 +00:00
|
|
|
@disallow_for_app_repositories
|
2017-07-17 21:53:08 +00:00
|
|
|
def delete(self, namespace_name, repository_name, uuid):
|
2014-07-16 20:30:47 +00:00
|
|
|
""" Deletes the specified notification. """
|
2017-07-17 21:55:00 +00:00
|
|
|
deleted = model.delete_repo_notification(namespace_name, repository_name, uuid)
|
2017-07-17 21:56:32 +00:00
|
|
|
if not deleted:
|
|
|
|
raise InvalidRequest("No repository notification found for: %s, %s, %s" % (namespace_name, repository_name, uuid))
|
2017-07-14 13:09:56 +00:00
|
|
|
|
2017-07-17 21:53:08 +00:00
|
|
|
log_action('delete_repo_notification', namespace_name,
|
|
|
|
{'repo': repository_name, 'namespace': namespace_name, 'notification_id': uuid,
|
2017-07-17 21:56:32 +00:00
|
|
|
'event': deleted.event_name, 'method': deleted.method_name},
|
2017-07-17 21:55:00 +00:00
|
|
|
repo_name=repository_name)
|
2014-07-16 20:30:47 +00:00
|
|
|
|
|
|
|
return 'No Content', 204
|
2014-07-18 02:51:58 +00:00
|
|
|
|
2017-05-18 21:52:50 +00:00
|
|
|
@require_repo_admin
|
|
|
|
@nickname('resetRepositoryNotificationFailures')
|
|
|
|
@disallow_for_app_repositories
|
2017-07-17 21:53:08 +00:00
|
|
|
def post(self, namespace_name, repository_name, uuid):
|
2017-05-18 21:52:50 +00:00
|
|
|
""" Resets repository notification to 0 failures. """
|
2017-07-17 21:55:00 +00:00
|
|
|
reset = model.reset_notification_number_of_failures(namespace_name, repository_name, uuid)
|
2017-07-17 21:56:32 +00:00
|
|
|
if not reset:
|
|
|
|
raise InvalidRequest("No repository notification found for: %s, %s, %s" % (namespace_name, repository_name, uuid))
|
2017-07-14 13:09:56 +00:00
|
|
|
|
2017-07-17 21:56:32 +00:00
|
|
|
log_action('reset_repo_notification', namespace_name,
|
|
|
|
{'repo': repository_name, 'namespace': namespace_name, 'notification_id': uuid,
|
|
|
|
'event': reset.event_name, 'method': reset.method_name},
|
|
|
|
repo_name=repository_name)
|
2017-05-18 21:52:50 +00:00
|
|
|
|
|
|
|
return 'No Content', 204
|
|
|
|
|
2014-07-18 02:51:58 +00:00
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
@resource('/v1/repository/<apirepopath:repository>/notification/<uuid>/test')
|
2014-08-19 23:05:28 +00:00
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
|
|
|
@path_param('uuid', 'The UUID of the notification')
|
2014-07-18 02:51:58 +00:00
|
|
|
class TestRepositoryNotification(RepositoryParamResource):
|
|
|
|
""" Resource for queuing a test of a notification. """
|
|
|
|
@require_repo_admin
|
|
|
|
@nickname('testRepoNotification')
|
2017-03-22 18:30:13 +00:00
|
|
|
@disallow_for_app_repositories
|
2017-07-17 21:56:32 +00:00
|
|
|
def post(self, namespace_name, repository_name, uuid):
|
2014-07-18 02:51:58 +00:00
|
|
|
""" Queues a test notification for this repository. """
|
2017-07-17 21:56:32 +00:00
|
|
|
test_note = model.queue_test_notification(uuid)
|
|
|
|
|
2017-07-17 21:53:08 +00:00
|
|
|
if not test_note:
|
2017-07-17 21:56:32 +00:00
|
|
|
raise InvalidRequest("No repository notification found for: %s, %s, %s" % (namespace_name, repository_name, uuid))
|
2017-07-14 13:09:56 +00:00
|
|
|
|
2017-07-17 21:56:32 +00:00
|
|
|
return {}, 200
|