Skip to content
Snippets Groups Projects
account.py 3.91 KiB
Newer Older
Seblu's avatar
Seblu committed
#!/usr/bin/env python
#coding=utf8

'''
CloudControl accounts related commands
'''

from cccli.exception import *
from sjrpc.core.exceptions import *
from cccli.printer import Printer, color
Seblu's avatar
Seblu committed
from cccli.command import TqlCommand
Seblu's avatar
Seblu committed

class Command_addaccount(TqlCommand):
Seblu's avatar
Seblu committed
    '''Create an account'''

Seblu's avatar
Seblu committed
    def __init__(self, cli, argv0):
        TqlCommand.__init__(self, cli, argv0)
        self.remove_option("--direct")
        self.remove_option("--raw")
        self.set_usage("%prog [options] <account name> <role> [password]")
Seblu's avatar
Seblu committed
    def __call__(self, argv):
Seblu's avatar
Seblu committed
        # parse args
        self.parse_args(argv)
        if len(self.args) == 2:
            _pass = None
        elif len(self.args) == 3:
            _pass = self.args[2]
            if self.printer.isinteractive():
                self.printer.warn("Password detected, removing last line from history")
                self.printer.history.removelast()
        else:
Seblu's avatar
Seblu committed
            raise cmdBadArgument()
        try:
            # add account
            self.cli.rpc.call("addaccount", self.args[0], self.args[1])
        except RpcError as e:
            raise cmdError("RPCError: %s"%str(e))
        # set password
        if _pass is not None:
            self.rpccall("passwd", "a=%s"%self.args[0], _pass, _direct=True)
    def remote_functions(self):
        return set(("addaccount", "passwd"))

Seblu's avatar
Seblu committed

class Command_delaccount(TqlCommand):
Seblu's avatar
Seblu committed
    '''Delete an account'''

    def __init__(self, cli, argv0):
        TqlCommand.__init__(self, cli, argv0)
        self.tql_filter += "&a"

Seblu's avatar
Seblu committed
    def __call__(self, argv):
        self.parse_args(argv)
        if len(self.args) != 1:
            raise cmdBadArgument("<tql>")
        self.rpccall("delaccount", self.args[0])
Seblu's avatar
Seblu committed

    def remote_functions(self):
        return set(("delaccount",))

Seblu's avatar
Seblu committed

class Command_close(TqlCommand):
Seblu's avatar
Seblu committed
    '''Disable accounts'''

    def __init__(self, cli, argv0):
        TqlCommand.__init__(self, cli, argv0)
        self.tql_filter += "&a&-close"

Seblu's avatar
Seblu committed
    def __call__(self, argv):
        self.parse_args(argv)
        if len(self.args) != 1:
            raise cmdBadArgument("<tql>")
        self.rpccall("close", self.args[0])
Seblu's avatar
Seblu committed

    def remote_functions(self):
        return set(("close",))

Seblu's avatar
Seblu committed

class Command_declose(TqlCommand):
Seblu's avatar
Seblu committed
    '''Enable accounts'''

    def __init__(self, cli, argv0):
        TqlCommand.__init__(self, cli, argv0)
        self.tql_filter += "&a&close"

Seblu's avatar
Seblu committed
    def __call__(self, argv):
        self.parse_args(argv)
        if len(self.args) != 1:
            raise cmdBadArgument("<tql>")
        self.rpccall("declose", self.args[0])
Seblu's avatar
Seblu committed

    def remote_functions(self):
        return set(("declose",))

Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
class Command_passwd(TqlCommand):
Seblu's avatar
Seblu committed
    '''Change account password'''

Seblu's avatar
Seblu committed
    def __init__(self, cli, argv0):
        TqlCommand.__init__(self, cli, argv0)
        self.tql_filter += "&a"

    def __init__(self, cli, argv0):
        TqlCommand.__init__(self, cli, argv0)
Seblu's avatar
Seblu committed
        self.set_usage("%prog [options] [tql] [password]")

Seblu's avatar
Seblu committed
    def __call__(self, argv):
Seblu's avatar
Seblu committed
        # parse args
        self.parse_args(argv)
        _tql = None
        _pass = None
        if len(self.args) == 0:
            _tql = "a=%s"%self.cli.settings["login"]
        elif len(self.args) == 1:
            _tql = self.args[0]
        elif len(self.args) == 2:
            _tql = self.args[0]
            _pass = self.args[1]
            _check = self.args[1]
            if self.printer.isinteractive():
                self.printer.warn("Removing last line from history")
                self.printer.history.removelast()
Seblu's avatar
Seblu committed
        else:
            raise cmdBadArgument()
Seblu's avatar
Seblu committed
        # get password
        if _pass is None:
            _pass = self.printer.getpass("Password: ")
            _check = self.printer.getpass("Again: ")
        if _pass != _check:
            raise cmdError("You don't type twice the same password. Aborted")
        # execute command
        self.rpccall("passwd", _tql, _pass)

    def remote_functions(self):
        return set(("passwd",))