2016-10-17 19:43:03 +00:00
|
|
|
""" Messages API. """
|
|
|
|
from flask import abort
|
|
|
|
from flask import make_response
|
|
|
|
from flask import request
|
|
|
|
|
|
|
|
import features
|
|
|
|
from auth import scopes
|
|
|
|
from auth.permissions import SuperUserPermission
|
|
|
|
from endpoints.api import (ApiResource, resource, nickname,
|
|
|
|
require_fresh_login, verify_not_prod, validate_json_request,
|
|
|
|
require_scope, show_if,)
|
2017-07-20 17:09:49 +00:00
|
|
|
from globalmessages_models_pre_oci import pre_oci_model as model
|
2016-10-17 19:43:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
@resource('/v1/messages')
|
|
|
|
class GlobalUserMessages(ApiResource):
|
|
|
|
""" Resource for getting a list of super user messages """
|
|
|
|
schemas = {
|
|
|
|
'GetMessage': {
|
|
|
|
'id': 'GetMessage',
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'Messages that a super user has saved in the past',
|
|
|
|
'properties': {
|
|
|
|
'message': {
|
|
|
|
'type': 'array',
|
|
|
|
'description': 'A list of messages',
|
|
|
|
'itemType': {
|
|
|
|
'type': 'object',
|
|
|
|
'properties': {
|
|
|
|
'uuid': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The message id',
|
|
|
|
},
|
|
|
|
'content': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The actual message',
|
|
|
|
},
|
2017-01-17 21:41:24 +00:00
|
|
|
'media_type': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The media type of the message',
|
|
|
|
'enum': ['text/plain', 'text/markdown'],
|
|
|
|
},
|
|
|
|
'severity': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The severity of the message',
|
|
|
|
'enum': ['info', 'warning', 'error'],
|
|
|
|
},
|
2016-10-17 19:43:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'CreateMessage': {
|
|
|
|
'id': 'CreateMessage',
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'Create a new message',
|
|
|
|
'properties': {
|
|
|
|
'message': {
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'A single message',
|
2017-07-05 19:05:19 +00:00
|
|
|
'required': [
|
|
|
|
'content',
|
|
|
|
'media_type',
|
|
|
|
'severity',
|
|
|
|
],
|
2016-10-17 19:43:03 +00:00
|
|
|
'properties': {
|
|
|
|
'content': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The actual message',
|
|
|
|
},
|
2017-01-17 21:41:24 +00:00
|
|
|
'media_type': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The media type of the message',
|
|
|
|
'enum': ['text/plain', 'text/markdown'],
|
|
|
|
},
|
|
|
|
'severity': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The severity of the message',
|
|
|
|
'enum': ['info', 'warning', 'error'],
|
|
|
|
},
|
2016-10-17 19:43:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@nickname('getGlobalMessages')
|
|
|
|
def get(self):
|
|
|
|
""" Return a super users messages """
|
|
|
|
return {
|
2017-07-20 17:09:49 +00:00
|
|
|
'messages': [m.to_dict() for m in model.get_all_messages()],
|
2016-10-17 19:43:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@require_fresh_login
|
|
|
|
@verify_not_prod
|
|
|
|
@nickname('createGlobalMessage')
|
|
|
|
@validate_json_request('CreateMessage')
|
|
|
|
@require_scope(scopes.SUPERUSER)
|
|
|
|
def post(self):
|
|
|
|
""" Create a message """
|
2016-10-17 21:43:12 +00:00
|
|
|
if not features.SUPER_USERS:
|
|
|
|
abort(404)
|
|
|
|
|
2016-10-17 19:43:03 +00:00
|
|
|
if SuperUserPermission().can():
|
2017-07-26 18:38:56 +00:00
|
|
|
message_req = request.get_json()['message']
|
|
|
|
message = model.create_message(message_req['severity'], message_req['media_type'], message_req['content'])
|
|
|
|
if message is None:
|
|
|
|
abort(400)
|
2016-10-17 19:43:03 +00:00
|
|
|
return make_response('', 201)
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
|
|
|
@resource('/v1/message/<uuid>')
|
|
|
|
@show_if(features.SUPER_USERS)
|
|
|
|
class GlobalUserMessage(ApiResource):
|
|
|
|
""" Resource for managing individual messages """
|
|
|
|
@require_fresh_login
|
|
|
|
@verify_not_prod
|
|
|
|
@nickname('deleteGlobalMessage')
|
|
|
|
@require_scope(scopes.SUPERUSER)
|
|
|
|
def delete(self, uuid):
|
|
|
|
""" Delete a message """
|
|
|
|
if SuperUserPermission().can():
|
2017-07-20 17:09:49 +00:00
|
|
|
model.delete_message(uuid)
|
2016-10-17 19:43:03 +00:00
|
|
|
return make_response('', 204)
|
|
|
|
|
|
|
|
abort(403)
|