Commit a13ff09d authored by Bruno Boiget's avatar Bruno Boiget
Browse files

Merge branch 'acl' into 'develop'

Intégration de serverselection dans server

See merge request !3
parents bd1074bb ea59faf4
[request_definition]
r = sub, obj, act
[policy_definition]
p = sub, obj, act
[role_definition]
g = _, _
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act
\ No newline at end of file
p, admin, server/*, create
p, admin, server/*, describe
p, admin, server/*, delete
p, admin, server/*, update
p, user, server/*, describe
g, owner, admin
......@@ -16,5 +16,18 @@ CREATE TABLE Server (
ServerEnvironment JSON,
LastPeerConnection TIMESTAMP DEFAULT NULL
);
-- serverselection table creation
CREATE TABLE ServerSelection (
ServerSelectionId SERIAL PRIMARY KEY,
ServerSelectionName VARCHAR(255) NOT NULL,
ServerSelectionDescription VARCHAR(255) NOT NULL,
ServerSelectionServersId INTEGER [],
ServerSelectionUsers hstore,
Dynamique BOOLEAN NOT NULL,
Requete VARCHAR(255)
);
ALTER TABLE ServerSelection ALTER COLUMN Dynamique
SET DEFAULT FALSE;
COMMIT;
......@@ -3,5 +3,6 @@
BEGIN;
SELECT 1/COUNT(*) FROM information_schema.schemata WHERE schema_name = 'server';
SELECT 1/COUNT(*) FROM information_schema.schemata WHERE schema_name = 'serverselection';
ROLLBACK;
......@@ -18,6 +18,16 @@ from server.server.error import (ServerError,
ServerErrorServerNameNotProvided,
ServerErrorUnknownServerId,
ServerErrorUnknownServerModelId)
from server.serverselection.lib import ServerSelection
from server.serverselection.error import (ServerSelectionError,
ServerSelectionErrorDatabaseNotAvailable,
ServerSelectionErrorDbConnection,
ServerSelectionErrorInvalidServerSelectionId,
ServerSelectionErrorServerSelectionNameNotProvided,
ServerSelectionErrorUnknownServerSelectionId,
ServerSelectionErrorDuplicateServerSelectionName,
ServerSelectionEmptyRecordDatabaseError)
from zephir.config import ServiceConfig
......@@ -30,6 +40,7 @@ class ServerRunner(ZephirCommonController):
# Create instance of working code
super().__init__(*args, **kwargs)
self.server = Server()
self.serverselection = ServerSelection()
self.conn = None
@register_wamp('v1.server.list', notification_uri=None, database=True)
......@@ -48,6 +59,7 @@ class ServerRunner(ZephirCommonController):
@register_wamp('v1.server.describe', notification_uri=None, database=True)
async def describe_server(self, cursor, serverid, configuration):
try:
server = self.server.describe_server(cursor, serverid)
if configuration:
......@@ -86,17 +98,20 @@ class ServerRunner(ZephirCommonController):
return b'{}'
@register_wamp('v1.server.create', notification_uri='v1.server.created', database=True)
async def create_server(self, cursor, servername, serverdescription, servermodelid, serverpassphrase):
async def create_server(self, cursor, _session_user, servername, serverdescription, servermodelid, serverpassphrase):
try:
result = self.server.create_server(cursor, servername, serverdescription, servermodelid)
return_code = await self.call('v1.vault.secret.set',
secretkey="{}_passphrase".format(result['serverid']),
secret={"passphrase" : serverpassphrase})
if return_code:
if return_code:
defaultserverselection = self._default_user_serverselection(cursor, _session_user)
self.serverselection.add_server_to_selection(cursor, result['serverid'], defaultserverselection['serverselectionid'])
return result
else:
raise ServerError('put passphrase return code status not available')
raise ServerError('put passphrase return code status not available')
except ServerErrorDatabaseNotAvailable as err:
raise ApplicationError('server.error.database-not-available', reason=str(err))
......@@ -245,8 +260,7 @@ class ServerRunner(ZephirCommonController):
async def exec_describe(self, job_id, automation):
if automation == 'salt':
results = await self.call('v1.execution.salt.job.describe',
jid=str(job_id))
jid=str(job_id))
else:
raise Exception(_('Automation engine not supported: {}').format(automation))
ret = []
......@@ -278,6 +292,234 @@ class ServerRunner(ZephirCommonController):
except ServerError as err:
raise ApplicationError('server.error', reason=str(err))
@register_wamp('v1.serverselection.list', notification_uri=None, database=True)
async def list_serverselections(self, cursor):
try:
return self.serverselection.list_serverselections(cursor)
except ServerSelectionErrorDatabaseNotAvailable as err:
raise ApplicationError('serverselection.error.database_not_available', reason=str(err))
except ServerSelectionErrorDbConnection as err:
raise ApplicationError('serverselection.error.db-connection', reason=str(err))
except ServerSelectionError as err:
raise ApplicationError('serverselection.error', reason=str(err))
@register_wamp('v1.serverselection.describe', notification_uri=None, database=True)
async def describe_serverselection(self, cursor, serverselectionid):
try:
return self.serverselection.describe_serverselection(cursor, serverselectionid)
except ServerSelectionErrorDatabaseNotAvailable as err:
raise ApplicationError('serverselection.error.database_not_available', reason=str(err))
except ServerSelectionErrorDbConnection as err:
raise ApplicationError('serverselection.error.db-connection', reason=str(err))
except ServerSelectionErrorInvalidServerSelectionId as err:
raise ApplicationError('serverselection.error.invalid_server_id', reason=str(err))
except ServerSelectionErrorUnknownServerSelectionId as err:
raise ApplicationError('serverselection.error.unknown_server_id', reason=str(err))
except ServerSelectionError as err:
raise ApplicationError('serverselection.error', reason=str(err))
@register_wamp('v1.serverselection.user.list', notification_uri=None, database=True)
async def list_user_serverselections(self, cursor, _session_user):
try:
serverselectionuser = _session_user['username']
return self.serverselection.list_user_serverselections(cursor, serverselectionuser)
except ServerSelectionErrorDatabaseNotAvailable as err:
raise ApplicationError('serverselection.error.database_not_available', reason=str(err))
except ServerSelectionErrorDbConnection as err:
raise ApplicationError('serverselection.error.db-connection', reason=str(err))
except ServerSelectionError as err:
raise ApplicationError('serverselection.error', reason=str(err))
@register_wamp('v1.serverselection.user.default', notification_uri=None, database=True)
async def default_user_serverselection(self, cursor, _session_user):
return self._default_user_serverselection(cursor, _session_user)
def _default_user_serverselection(self, cursor, _session_user):
try:
serverselectionuser = _session_user['username']
return self.serverselection.default_user_serverselection(cursor, serverselectionuser)
except ServerSelectionEmptyRecordDatabaseError as err:
serverselectionname = serverselectionuser + "_DEFAULT"
serverselectiondescription = serverselectionuser + " server selection"
self.serverselection.create_serverselection(cursor, serverselectionname, serverselectiondescription, serverselectionuser)
return self.serverselection.default_user_serverselection(cursor, serverselectionuser)
except ServerSelectionErrorDatabaseNotAvailable as err:
raise ApplicationError('serverselection.error.database_not_available', reason=str(err))
except ServerSelectionErrorDbConnection as err:
raise ApplicationError('serverselection.error.db-connection', reason=str(err))
except ServerSelectionError as err:
raise ApplicationError('serverselection.error', reason=str(err))
@register_wamp('v1.serverselection.user.server.list', notification_uri=None, database=True)
async def list_user_servers(self, cursor, _session_user):
try:
serverselectionuser = _session_user['username']
return self.serverselection.list_user_servers(cursor, serverselectionuser)
except ServerSelectionErrorDatabaseNotAvailable as err:
raise ApplicationError('serverselection.error.database_not_available', reason=str(err))
except ServerSelectionErrorDbConnection as err:
raise ApplicationError('serverselection.error.db-connection', reason=str(err))
except ServerSelectionError as err:
raise ApplicationError('serverselection.error', reason=str(err))
@register_wamp('v1.serverselection.create', notification_uri=None, database=True)
async def create_serverselection(self, cursor, serverselectionname, serverselectiondescription, _session_user):
try:
serverselectionuser = _session_user['username']
return self.serverselection.create_serverselection(cursor, serverselectionname, serverselectiondescription, serverselectionuser)
except ServerSelectionErrorDatabaseNotAvailable as err:
raise ApplicationError('serverselection.error.database-not-available', reason=str(err))
except ServerSelectionErrorDbConnection as err:
raise ApplicationError('serverselection.error.db-connection', reason=str(err))
except ServerSelectionErrorDuplicateServerSelectionName as err:
raise ApplicationError('serverselection.static.create.error.duplicate_serverselection', reason=str(err))
except ServerSelectionError as err:
raise ApplicationError('serverselection.error', reason=str(err))
@register_wamp('v1.serverselection.update', notification_uri=None, database=True)
async def update_serverselection(self, cursor, serverselectionid, serverselectionname, serverselectiondescription, dynamique, requete):
try:
return self.serverselection.update_serverselection(cursor, serverselectionid, serverselectionname, serverselectiondescription, dynamique, requete)
except ServerSelectionErrorDatabaseNotAvailable as err:
raise ApplicationError('serverselection.error.database-not-available', reason=str(err))
except ServerSelectionErrorDbConnection as err:
raise ApplicationError('serverselection.error.db-connection', reason=str(err))
except ServerSelectionErrorInvalidServerSelectionId as err:
raise ApplicationError('serverselection.error.invalid_serverselection_id', reason=str(err))
except ServerSelectionErrorUnknownServerSelectionId as err:
raise ApplicationError('serverselection.error.unknown_serverselection_id', reason=str(err))
except ServerSelectionError as err:
raise ApplicationError('serverselection.error', reason=str(err))
@register_wamp('v1.serverselection.delete', notification_uri=None, database=True)
async def delete_serverselection(self, cursor, serverselectionid):
try:
return self.serverselection.delete_serverselection(cursor, serverselectionid)
except ServerSelectionErrorDatabaseNotAvailable as err:
raise ApplicationError('serverselection.error.database_not_available', reason=str(err))
except ServerSelectionErrorDbConnection as err:
raise ApplicationError('serverselection.error.db-connection', reason=str(err))
except ServerSelectionErrorInvalidServerSelectionId as err:
raise ApplicationError('serverselection.error.invalid_serverselection_id', reason=str(err))
except ServerSelectionErrorUnknownServerSelectionId as err:
raise ApplicationError('serverselection.error.unknown_serverselection_id', reason=str(err))
except ServerSelectionError as err:
raise ApplicationError('serverselection.error', reason=str(err))
@register_wamp('v1.serverselection.server.add', notification_uri=None, database=True)
async def add_server_to_selection(self, cursor, serverselectionserversid, serverselectionid):
try:
return self.serverselection.add_server_to_selection(cursor, serverselectionserversid, serverselectionid)
except ServerSelectionErrorDatabaseNotAvailable as err:
raise ApplicationError('serverselection.error.database-not-available', reason=str(err))
except ServerSelectionErrorDbConnection as err:
raise ApplicationError('serverselection.error.db-connection', reason=str(err))
except ServerSelectionError as err:
raise ApplicationError('serverselection.error', reason=str(err))
@register_wamp('v1.serverselection.server.remove', notification_uri=None, database=True)
async def remove_server_from_selection(self, cursor, serverselectionserversid, serverselectionid):
try:
return self.serverselection.remove_server_from_selection(cursor, serverselectionserversid, serverselectionid)
except ServerSelectionErrorDatabaseNotAvailable as err:
raise ApplicationError('serverselection.error.database_not_available', reason=str(err))
except ServerSelectionErrorDbConnection as err:
raise ApplicationError('serverselection.error.db-connection', reason=str(err))
except ServerSelectionErrorInvalidServerSelectionId as err:
raise ApplicationError('serverselection.error.invalid_serverselection_id', reason=str(err))
except ServerSelectionErrorUnknownServerSelectionId as err:
raise ApplicationError('serverselection.error.unknown_serverselection_id', reason=str(err))
except ServerSelectionError as err:
raise ApplicationError('serverselection.error', reason=str(err))
@register_wamp('v1.serverselection.user.add', notification_uri=None, database=True)
async def add_user_to_serverselection(self, cursor, serverselectionid, serverselectionuser, serverselectionuserrole):
try:
return self.serverselection.add_user_to_serverselection(cursor, serverselectionid, serverselectionuser, serverselectionuserrole)
except ServerSelectionErrorDatabaseNotAvailable as err:
raise ApplicationError('serverselection.error.database_not_available', reason=str(err))
except ServerSelectionErrorDbConnection as err:
raise ApplicationError('serverselection.error.db-connection', reason=str(err))
except ServerSelectionErrorInvalidServerSelectionId as err:
raise ApplicationError('serverselection.error.invalid_serverselection_id', reason=str(err))
except ServerSelectionErrorUnknownServerSelectionId as err:
raise ApplicationError('serverselection.error.unknown_serverselection_id', reason=str(err))
except ServerSelectionError as err:
raise ApplicationError('serverselection.error', reason=str(err))
@register_wamp('v1.serverselection.user.remove', notification_uri=None, database=True)
async def remove_user_from_serverselection(self, cursor, serverselectionid, serverselectionuser):
try:
return self.serverselection.remove_user_from_serverselection(cursor, serverselectionid, serverselectionuser)
except ServerSelectionErrorDatabaseNotAvailable as err:
raise ApplicationError('serverselection.error.database_not_available', reason=str(err))
except ServerSelectionErrorDbConnection as err:
raise ApplicationError('serverselection.error.db-connection', reason=str(err))
except ServerSelectionErrorInvalidServerSelectionId as err:
raise ApplicationError('serverselection.error.invalid_serverselection_id', reason=str(err))
except ServerSelectionErrorUnknownServerSelectionId as err:
raise ApplicationError('serverselection.error.unknown_serverselection_id', reason=str(err))
except ServerSelectionError as err:
raise ApplicationError('serverselection.error', reason=str(err))
@register_wamp('v1.serverselection.user.update', notification_uri=None, database=True)
async def update_user_to_serverselection(self, cursor, serverselectionid, serverselectionuser, serverselectionuserrole):
try:
return self.serverselection.update_user_to_serverselection(cursor, serverselectionid, serverselectionuser, serverselectionuserrole)
except ServerSelectionErrorDatabaseNotAvailable as err:
raise ApplicationError('serverselection.error.database_not_available', reason=str(err))
except ServerSelectionErrorDbConnection as err:
raise ApplicationError('serverselection.error.db-connection', reason=str(err))
except ServerSelectionErrorInvalidServerSelectionId as err:
raise ApplicationError('serverselection.error.invalid_serverselection_id', reason=str(err))
except ServerSelectionErrorUnknownServerSelectionId as err:
raise ApplicationError('serverselection.error.unknown_serverselection_id', reason=str(err))
except ServerSelectionError as err:
raise ApplicationError('serverselection.error', reason=str(err))
@register_wamp('v1.serverselection.user.role.get', notification_uri=None, database=True)
async def get_serverselection_user_role(self, cursor, serverselectionid, username):
try:
return self.serverselection.get_serverselection_user_role(cursor, serverselectionid, username)
except ServerSelectionErrorDatabaseNotAvailable as err:
raise ApplicationError('serverselection.error.database_not_available', reason=str(err))
except ServerSelectionErrorDbConnection as err:
raise ApplicationError('serverselection.error.db-connection', reason=str(err))
except ServerSelectionErrorInvalidServerSelectionId as err:
raise ApplicationError('serverselection.error.invalid_serverselection_id', reason=str(err))
except ServerSelectionErrorUnknownServerSelectionId as err:
raise ApplicationError('serverselection.error.unknown_serverselection_id', reason=str(err))
except ServerSelectionError as err:
raise ApplicationError('serverselection.error', reason=str(err))
@register_wamp('v1.serverselection.user.role.server.get', notification_uri=None, database=True)
async def get_serverselection_user_server_role(self, cursor, serverid, username):
try:
return self.serverselection.get_serverselection_user_server_role(cursor, serverid, username)
except ServerSelectionErrorDatabaseNotAvailable as err:
raise ApplicationError('serverselection.error.database_not_available', reason=str(err))
except ServerSelectionErrorDbConnection as err:
raise ApplicationError('serverselection.error.db-connection', reason=str(err))
except ServerSelectionErrorInvalidServerSelectionId as err:
raise ApplicationError('serverselection.error.invalid_serverselection_id', reason=str(err))
except ServerSelectionErrorUnknownServerSelectionId as err:
raise ApplicationError('serverselection.error.unknown_serverselection_id', reason=str(err))
except ServerSelectionError as err:
raise ApplicationError('serverselection.error', reason=str(err))
if __name__ == '__main__':
run(ServerRunner)
......@@ -18,6 +18,7 @@ case $ZEPHIR_TEST_TYPE in
;;
"integration")
py.test /srv/test/test_server.py -x
py.test /srv/test/test_serverselection.py -x
exit $?
;;
"e2e")
......
from .lib import ServerSelection
__all__ = ['ServerSelection']
class ServerSelectionError(Exception):
"""Base class of :class:`ServerSelection` exceptions
"""
class ServerSelectionErrorDatabaseNotAvailable(ServerSelectionError):
"""No available database service
"""
class ServerSelectionErrorDbConnection(ServerSelectionError):
"""Database connection error
"""
class ServerSelectionErrorInvalidServerSelectionId(ServerSelectionError):
"""Invalid serverselection ID
"""
class ServerSelectionErrorServerSelectionNameNotProvided(ServerSelectionError):
"""Serverselectionname expected
"""
class ServerSelectionErrorUnknownServerSelectionId(ServerSelectionError):
"""Unknown serverselection ID
"""
class ServerSelectionErrorDuplicateServerSelectionName(ServerSelectionError):
"""Duplicate serverselection name
"""
class ServerSelectionEmptyRecordDatabaseError(ServerSelectionError):
"""Empty value in serverselection
"""
from .error import ServerSelectionError
from .query import (list_all_serverselections,
fetch_serverselection,
fetch_serverselection_dict,
fetch_all_user_serverselections,
fetch_default_user_serverselection,
fetch_all_user_servers,
insert_serverselection,
update_serverselection,
delete_serverselection,
add_server_to_serverselection,
remove_server_from_serverselection,
add_user_to_serverselection,
remove_user_from_serverselection,
update_user_from_serverselection,
fetch_serverselection_user_role,
fetch_serverselection_user_server_role,
erase_serverselection
)
class ServerSelection():
"""Serverselection Manage API
"""
def list_serverselections(self, cursor):
"""Fetch Serverselectionss from database
:return: list of serverselection object
"""
return list_all_serverselections(cursor)
def describe_serverselection(self, cursor, serverselectionid):
"""Get serverselection information asynchronously from database
:param `int` serverselectionid: servserverselection identifier
"""
return fetch_serverselection_dict(cursor, serverselectionid)
def list_user_serverselections(self, cursor, serverselectionuser):
"""Get serverselections of a user
:param str serverselectionuser: user name to be add to the serverselection
"""
return fetch_all_user_serverselections(cursor, serverselectionuser)
def list_user_servers(self, cursor, serverselectionuser):
"""Get serverselections of a user
:param str serverselectionuser: user name to be add to the serverselection
"""
return fetch_all_user_servers(cursor, serverselectionuser)
def default_user_serverselection(self, cursor, serverselectionuser):
"""Get the default serverselection of a user
:param str serverselectionuser: user name to be add to the serverselection
"""
return fetch_default_user_serverselection(cursor, serverselectionuser)
def create_serverselection(self, cursor, serverselectionname, serverselectiondescription, serverselectionuser):
"""Creates a serverselection in database
:param str serverselectionname: serverselection name
:param str serverselectiondescription: servermodel identifier
:param str serverselectionuser: user name to be add to the serverselection
:param str serverselectionuserrole: user role to be add to the serverselection
:return: newly created serverselection identifier
:rtype: int
"""
return insert_serverselection(cursor, serverselectionname, serverselectiondescription, serverselectionuser)
def update_serverselection(self, cursor, serverselectionid, serverselectionname, serverselectiondescription, dynamique, requete):
"""Updates a serverselection in database
:param int serverselectionid: serverselection identifier
:param str serverselectionname: server name
:param str serverselectiondescription: serverselection description
:param bool dynamique : is serverselection dynamique
:param str requete: request associated to the serverselection
:return bool: True for update success, False either
"""
return update_serverselection(cursor, serverselectionid, serverselectionname, serverselectiondescription, dynamique, requete)
def delete_serverselection(self, cursor, serverselectionid):
"""Deletes a serverselection in database
:param int serverselectionid: serverselection identifier
:return bool: True for delete success, False either
"""
return delete_serverselection(cursor, serverselectionid)
def erase_serverselection(self, cursor):
erase_serverselection(cursor)
def add_server_to_selection(self, cursor, serverselectionserversid, serverselectionid):
"""Add a server to a serverselection in database
:param int serverselectionserversid: server identifier
:param int serverselectionid: serverselection identifier
:return bool: True for addition success, False either
"""
return add_server_to_serverselection(cursor, serverselectionserversid, serverselectionid)
def remove_server_from_selection(self, cursor, serverselectionserversid, serverselectionid):
"""Remove a server from a serverselection in database
:param int serverselectionserversid: server identifier
:param int serverselectionid: serverselection identifier
:return bool: True for addition success, False either
"""
return remove_server_from_serverselection(cursor, serverselectionserversid, serverselectionid)
def add_user_to_serverselection(self, cursor, serverselectionid, username, role):
"""Add a user to a serverselection
:param int serverselectionserversid: server identifier
:param str username: user name to be add to the serverseleciton
:param str role: user role to be add to the serverseleciton
:return bool: True for addition success, False either
"""
return add_user_to_serverselection(cursor, serverselectionid, username, role)
def remove_user_from_serverselection(self, cursor, serverselectionid, username):
"""Remove a user from a serverselection
:param int serverselectionserversid: server identifier
:param int serverselectionid: serverselection identifier
:param str username: user name to be add to the serverseleciton
:return bool: True for addition success, False either
"""
return remove_user_from_serverselection(cursor, serverselectionid, username)
def update_user_to_serverselection(self, cursor, serverselectionid, username, role):
"""Update a user from a serverselection
:param int serverselectionserversid: server identifier
:param str username: user name to be add to the serverseleciton
:param str role: user role to be add to the serverseleciton
:return bool: True for addition success, False either
"""
return update_user_from_serverselection(cursor, serverselectionid, username, role)
def get_serverselection_user_role(self, cursor, serverselectionid, username):
"""Update a user from a serverselection
:param int serverselectionserversid: server identifier
:param str username: user name to be add to the serverseleciton
:param str role: user role to be add to the serverseleciton
:return bool: True for addition success, False either
"""
return fetch_serverselection_user_role(cursor, username, serverselectionid)
def get_serverselection_user_server_role(self, cursor, serverid, username):
"""Update a user from a serverselection
:param int serverselectionserversid: server identifier
:param str username: user name to be add to the serverseleciton
:param str role: user role to be add to the serverseleciton
:return bool: True for addition success, False either
"""
return fetch_serverselection_user_server_role(cursor, username, serverid)
from zephir.i18n import _
from json import dumps
from .error import ServerSelectionEmptyRecordDatabaseError
"""
List all serverselections
"""
FETCH_ALL_SERVERSELECTIONS = '''
SELECT serverselectionid, serverselectionname, serverselectiondescription, serverselectionserversid, serverselectionusers, dynamique, requete
FROM serverselection
'''
"""
Fetch one serverselection based on its ID
"""
FETCH_SERVERSELECTION = '''
SELECT serverselectionid, serverselectionname, serverselectiondescription, serverselectionserversid, serverselectionusers, dynamique, requete
FROM serverselection
WHERE serverselectionid = %s
'''
"""
Fetch all serverselection of a user
"""
FETCH_ALL_USER_SERVERSELECTIONS = '''
SELECT serverselectionid, serverselectionname, serverselectiondescription, serverselectionserversid, serverselectionusers, dynamique, requete
FROM serverselection
WHERE exist(serverselectionusers, %s)
'''
"""
Fetch the default serverselection of a user
"""