Merge pull request #2005 from charltonaustin/fix_spacing_for_motd
Moving the messages endpoint to something more generic, and making th…
This commit is contained in:
commit
f45aac063e
7 changed files with 120 additions and 105 deletions
|
@ -364,6 +364,7 @@ import endpoints.api.billing
|
|||
import endpoints.api.build
|
||||
import endpoints.api.discovery
|
||||
import endpoints.api.error
|
||||
import endpoints.api.globalmessages
|
||||
import endpoints.api.image
|
||||
import endpoints.api.logs
|
||||
import endpoints.api.manifest
|
||||
|
|
105
endpoints/api/globalmessages.py
Normal file
105
endpoints/api/globalmessages.py
Normal file
|
@ -0,0 +1,105 @@
|
|||
""" 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 data import model
|
||||
from endpoints.api import (ApiResource, resource, nickname,
|
||||
require_fresh_login, verify_not_prod, validate_json_request,
|
||||
require_scope, show_if,)
|
||||
|
||||
|
||||
@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',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
'CreateMessage': {
|
||||
'id': 'CreateMessage',
|
||||
'type': 'object',
|
||||
'description': 'Create a new message',
|
||||
'properties': {
|
||||
'message': {
|
||||
'type': 'object',
|
||||
'description': 'A single message',
|
||||
'properties': {
|
||||
'content': {
|
||||
'type': 'string',
|
||||
'description': 'The actual message',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@nickname('getGlobalMessages')
|
||||
def get(self):
|
||||
""" Return a super users messages """
|
||||
return {
|
||||
'messages': [message_view(m) for m in model.message.get_messages()],
|
||||
}
|
||||
|
||||
@require_fresh_login
|
||||
@verify_not_prod
|
||||
@nickname('createGlobalMessage')
|
||||
@validate_json_request('CreateMessage')
|
||||
@require_scope(scopes.SUPERUSER)
|
||||
@show_if(features.SUPER_USERS)
|
||||
def post(self):
|
||||
""" Create a message """
|
||||
if SuperUserPermission().can():
|
||||
model.message.create([request.get_json()['message']])
|
||||
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():
|
||||
model.message.delete_message([uuid])
|
||||
return make_response('', 204)
|
||||
|
||||
abort(403)
|
||||
|
||||
|
||||
def message_view(message):
|
||||
return {
|
||||
'uuid': message.uuid,
|
||||
'content': message.content,
|
||||
}
|
|
@ -819,95 +819,3 @@ class SuperUserServiceKeyApproval(ApiResource):
|
|||
return make_response('', 201)
|
||||
|
||||
abort(403)
|
||||
|
||||
@resource('/v1/messages')
|
||||
@show_if(features.SUPER_USERS)
|
||||
class SuperUserMessages(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',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
'CreateMessage': {
|
||||
'id': 'CreateMessage',
|
||||
'type': 'object',
|
||||
'description': 'Create a new message',
|
||||
'properties': {
|
||||
'message': {
|
||||
'type': 'object',
|
||||
'description': 'A single message',
|
||||
'properties': {
|
||||
'content': {
|
||||
'type': 'string',
|
||||
'description': 'The actual message',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@nickname('getMessages')
|
||||
def get(self):
|
||||
""" Return a super users messages """
|
||||
return {
|
||||
'messages': [message_view(m) for m in model.message.get_messages()],
|
||||
}
|
||||
|
||||
@require_fresh_login
|
||||
@verify_not_prod
|
||||
@nickname('createMessages')
|
||||
@validate_json_request('CreateMessage')
|
||||
@require_scope(scopes.SUPERUSER)
|
||||
def post(self):
|
||||
""" Create a message """
|
||||
if SuperUserPermission().can():
|
||||
model.message.create([request.get_json()['message']])
|
||||
return make_response('', 201)
|
||||
|
||||
abort(403)
|
||||
|
||||
|
||||
@resource('/v1/message/<uuid>')
|
||||
@show_if(features.SUPER_USERS)
|
||||
class SuperUserMessage(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():
|
||||
model.message.delete_message([uuid])
|
||||
return make_response('', 204)
|
||||
|
||||
abort(403)
|
||||
|
||||
|
||||
def message_view(message):
|
||||
return {
|
||||
'uuid': message.uuid,
|
||||
'content': message.content,
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ angular.module('quay').directive('quayMessageBar', function () {
|
|||
scope: {},
|
||||
controller: function ($scope, $element, ApiService) {
|
||||
$scope.messages = [];
|
||||
ApiService.getMessages().then(function (data) {
|
||||
ApiService.getGlobalMessages().then(function (data) {
|
||||
$scope.messages = data['messages'] || [];
|
||||
}, function (resp) {
|
||||
return true;
|
||||
|
|
|
@ -33,7 +33,7 @@ angular.module('quay').directive('globalMessageTab', function () {
|
|||
message: $scope.newMessage
|
||||
};
|
||||
|
||||
ApiService.createMessages(data, null).then(function (resp) {
|
||||
ApiService.createGlobalMessage(data, null).then(function (resp) {
|
||||
$scope.creatingMessage = false;
|
||||
$scope.createdMessage = {content: $scope.newMessage.content};
|
||||
$scope.newMessage = {};
|
||||
|
@ -62,7 +62,7 @@ angular.module('quay').directive('globalMessageTab', function () {
|
|||
};
|
||||
|
||||
$scope.loadMessageInternal = function () {
|
||||
ApiService.getMessages().then(function (resp) {
|
||||
ApiService.getGlobalMessages().then(function (resp) {
|
||||
$scope.messages = resp['messages'];
|
||||
}, function (resp) {
|
||||
$scope.messages = [];
|
||||
|
|
|
@ -51,7 +51,8 @@ from endpoints.api.superuser import (SuperUserLogs, SuperUserList, SuperUserMana
|
|||
SuperUserOrganizationManagement, SuperUserOrganizationList,
|
||||
SuperUserAggregateLogs, SuperUserServiceKeyManagement,
|
||||
SuperUserServiceKey, SuperUserServiceKeyApproval,
|
||||
SuperUserTakeOwnership, SuperUserMessages, SuperUserMessage)
|
||||
SuperUserTakeOwnership,)
|
||||
from endpoints.api.globalmessages import (GlobalUserMessage, GlobalUserMessages,)
|
||||
from endpoints.api.secscan import RepositoryImageSecurity
|
||||
from endpoints.api.manifest import RepositoryManifestLabels, ManageRepositoryManifestLabel
|
||||
|
||||
|
@ -4204,7 +4205,7 @@ class TestSuperUserManagement(ApiTestCase):
|
|||
class TestSuperUserMessages(ApiTestCase):
|
||||
def setUp(self):
|
||||
ApiTestCase.setUp(self)
|
||||
self._set_url(SuperUserMessages, username='freshuser')
|
||||
self._set_url(GlobalUserMessages, username='freshuser')
|
||||
|
||||
def test_get_anonymous(self):
|
||||
self._run_test('GET', 200, None, None)
|
||||
|
@ -4235,7 +4236,7 @@ class TestSuperUserMessages(ApiTestCase):
|
|||
class TestSuperUserMessage(ApiTestCase):
|
||||
def setUp(self):
|
||||
ApiTestCase.setUp(self)
|
||||
self._set_url(SuperUserMessage, uuid='1234')
|
||||
self._set_url(GlobalUserMessage, uuid='1234')
|
||||
|
||||
def test_delete_anonymous(self):
|
||||
self._run_test('DELETE', 401, None, None)
|
||||
|
|
|
@ -65,8 +65,8 @@ from endpoints.api.permission import (RepositoryUserPermission, RepositoryTeamPe
|
|||
RepositoryTeamPermissionList, RepositoryUserPermissionList)
|
||||
from endpoints.api.superuser import (SuperUserLogs, SuperUserList, SuperUserManagement,
|
||||
SuperUserServiceKeyManagement, SuperUserServiceKey,
|
||||
SuperUserServiceKeyApproval, SuperUserTakeOwnership,
|
||||
SuperUserMessages, SuperUserMessage)
|
||||
SuperUserServiceKeyApproval, SuperUserTakeOwnership,)
|
||||
from endpoints.api.globalmessages import (GlobalUserMessage, GlobalUserMessages,)
|
||||
from endpoints.api.secscan import RepositoryImageSecurity
|
||||
from endpoints.api.suconfig import (SuperUserRegistryStatus, SuperUserConfig, SuperUserConfigFile,
|
||||
SuperUserCreateInitialSuperUser)
|
||||
|
@ -4283,9 +4283,9 @@ class TestSuperUserManagement(ApiTestCase):
|
|||
self.login(ADMIN_ACCESS_USER)
|
||||
|
||||
# Create a message
|
||||
self.postResponse(SuperUserMessages, data=dict(message={"content": "new message"}), expected_code=201)
|
||||
self.postResponse(GlobalUserMessages, data=dict(message={"content": "new message"}), expected_code=201)
|
||||
|
||||
json = self.getJsonResponse(SuperUserMessages)
|
||||
json = self.getJsonResponse(GlobalUserMessages)
|
||||
|
||||
self.assertEquals(len(json['messages']), 2)
|
||||
self.assertEquals(json['messages'][1]["content"], "new message")
|
||||
|
@ -4294,10 +4294,10 @@ class TestSuperUserManagement(ApiTestCase):
|
|||
|
||||
def test_delete_message(self):
|
||||
self.login(ADMIN_ACCESS_USER)
|
||||
json = self.getJsonResponse(SuperUserMessages)
|
||||
self.deleteResponse(SuperUserMessage, {"uuid": json['messages'][0]['uuid']}, 204)
|
||||
json = self.getJsonResponse(GlobalUserMessages)
|
||||
self.deleteResponse(GlobalUserMessage, {"uuid": json['messages'][0]['uuid']}, 204)
|
||||
|
||||
json = self.getJsonResponse(SuperUserMessages)
|
||||
json = self.getJsonResponse(GlobalUserMessages)
|
||||
|
||||
self.assertEquals(len(json['messages']), 0)
|
||||
|
||||
|
|
Reference in a new issue