Commit 8f6939a7 authored by Antoine Millet's avatar Antoine Millet
Browse files

A lot of handler methodz now takes a query on input.

parent f9c3b722
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -5,5 +5,14 @@
class AlreadyRegistered(Exception):
    pass


class AuthenticationError(Exception):
    pass


class RightError(Exception):
    pass


class BadObjectError(Exception):
    pass
+79 −44
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@ import inspect
import logging
from sjrpc.utils import RpcHandler, pure
from conf import CCConf
from exceptions import AlreadyRegistered, AuthenticationError
from exceptions import AlreadyRegistered, AuthenticationError, RightError

def listed(func):
    func.__listed__ = True
@@ -46,8 +46,8 @@ class CCHandler(RpcHandler):

class OnlineCCHandler(CCHandler):

    def on_disconnect(self, connection):
        self._server.unregister(connection)
    def on_disconnect(self, conn):
        self._server.unregister(conn)


class HypervisorHandler(OnlineCCHandler):
@@ -62,9 +62,8 @@ class ClientHandler(OnlineCCHandler):
    Handler binded to 'cli' role.
    '''

    @pure
    @listed
    def list(self, query):
    def list(self, conn, query):
        '''
        List all objects registered on this instance.
        '''
@@ -85,96 +84,132 @@ class ClientHandler(OnlineCCHandler):
            if vm_to_start:
                hv.connection.call(method, vm_to_start, *args, **kwargs)

    @pure
    @listed
    def start(self, query):
    def start(self, conn, query):
        self._vm_action(query, 'start_vm')

    @pure
    @listed
    def stop(self, query, force=False):
    def stop(self, conn, query, force=False):
        self._vm_action(query, 'stop_vm', force)

    @pure
    @listed
    def destroy(self, query):
    def destroy(self, conn, query):
        self.stop(query, force=True)

    @pure
    @listed
    def pause(self, query):
    def pause(self, conn, query):
        self._vm_action(query, 'suspend_vm')

    @pure
    @listed
    def resume(self, query):
    def resume(self, conn, query):
        self._vm_action(query, 'resume_vm')

    @pure
    @listed
    def passwd(self, login, password, method='ssha'):
    def passwd(self, conn, login, password, method='ssha'):
        '''
        Define a new password for specified user.
        '''
        self._server.conf.set_password(login, password, method)

    @pure
    @listed
    def addaccount(self, login, role, password=None):
    def addaccount(self, conn, login, role, password=None):
        '''
        Create a new account with specified login.
        '''
        self._server.conf.create_account(login, role, password)

    @pure
    @listed
    def addtag(self, login, tag_name, tag_value):
    def addtag(self, conn, query, tag_name, tag_value):
        '''
        Add a tag to the account with specified login.
        Add a tag to the account which match the specified query.
        '''
        self._server.conf.add_tag(login, tag_name, tag_value)
        
    @pure
        objects = self._server.list(query)
        for obj in objects:
            if 'a' not in obj:
                raise BadObjectError('All objects must have the "a" tag.')
        for obj in objects:
            self._server.conf.add_tag(obj['a'], tag_name, tag_value)

    @listed
    def deltag(self, login, tag):
    def deltag(self, conn, query, tag):
        '''
        Remove a tag of the account with specified login.
        '''
        self._server.conf.remove_tag(login, tag)
        objects = self._server.list(query)
        for obj in objects:
            if 'a' not in obj:
                raise BadObjectError('All objects must have the "a" tag.')
        for obj in objects:
            self._server.conf.remove_tag(obj['a'], tag)

    @pure
    @listed
    def tags(self, login):
    def tags(self, conn, query):
        '''
        Return all static tags attached to the specified login.
        '''
        return self._server.conf.show(login)['tags']
        objects = self._server.list(query)
        for obj in objects:
            if 'a' not in obj:
                raise BadObjectError('All objects must have the "a" tag.')
        for obj in objects:
            return self._server.conf.show(obj['a'])['tags']

    @pure
    @listed
    def delaccount(self, login):
    def delaccount(self, conn, query):
        '''
        Delete the account with specified login.
        '''
        self._server.conf.remove_account(login)
        objects = self._server.list(query)
        for obj in objects:
            if 'a' not in obj:
                raise BadObjectError('All objects must have the "a" tag.')
        for obj in objects:
            self._server.conf.remove_account(obj['a'])

    @pure
    @listed
    def rights(self, login):
        return self._server.conf.show(login)['rights']
    def rights(self, conn, query):
        '''
        Get the rights of an object set.
        '''
        
        objects = self._server.list(query)
        rules = {}
        for obj in objects:
            if 'a' in obj:
                rules[obj['a']] = self._server.conf.show(obj['a'])['rights']
            else:
                raise BadObjectError('All objects must have the "a" tag.')
        
        return rules

    @pure
    @listed
    def addright(self, login, tql, method=None, allow=True, index=None):
        self._server.conf.add_right(login, tql, method, allow, index)
    def addright(self, conn, query, tql, method=None, allow=True, index=None):
        '''
        Add a right rule to the selected objects.
        '''
        
        objects = self._server.list(query)
        for obj in objects:
            if 'a' not in obj:
                raise BadObjectError('All objects must have the "a" tag.')
        for obj in objects:
            self._server.conf.add_right(obj['a'], tql, method, allow, index)

    @pure
    @listed
    def delright(self, login, index):
        self._server.conf.remove_right(login, index)
    def delright(self, conn, query, index):
        '''
        Remove a right rule from the selected objects.
        '''
        
    @pure
    def proxy_client(self, login, command, *args, **kwargs):
        objects = self._server.list(query)
        for obj in objects:
            if 'a' not in obj:
                raise BadObjectError('All objects must have the "a" tag.')
        for obj in objects:
            self._server.conf.remove_right(obj['a'], index)

    def proxy_client(self, conn, login, command, *args, **kwargs):
        client = self._server.get_connection(login)
        return client.connection.call(command, *args, **kwargs)

@@ -192,7 +227,7 @@ class WelcomeHandler(CCHandler):
    }

    @listed
    def authentify(self, connection, login, password):
    def authentify(self, conn, login, password):
        '''
        Authenticate the client.
        '''