Skip to content
Snippets Groups Projects
account.py 2.82 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
from cccli.command.command import Command, TqlCommand
Seblu's avatar
Seblu committed

class Command_addaccount(Command):
    '''Create an account'''

    def __call__(self, argv):
        if len(argv) != 3:
            raise cmdBadArgument()
        try:
            self.cli.rpc.call("addaccount", argv[1], argv[2])
        except RpcError as e:
            raise cmdError("RPCError: %s"%str(e))

    def usage(self):
        return "Usage: addaccount <account name> <role>"

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)

Seblu's avatar
Seblu committed
    def __call__(self, argv):
        try:
            (options, args) = self.option.parse_args(argv[1:])
        except SystemExit:
            return
        if len(args) != 1:
Seblu's avatar
Seblu committed
            raise cmdBadArgument()
        try:
            d = self.cli.rpc.call("delaccount", args[0])
            if options.status:
                self.show_status(d)
Seblu's avatar
Seblu committed
        except RpcError as e:
            raise cmdError("RPCError: %s"%str(e))


class Command_close(Command):
    '''Disable accounts'''

    def __call__(self, argv):
        if len(argv) != 2:
            raise cmdBadArgument()
        try:
            self.cli.rpc.call("close", argv[1])
        except RpcError as e:
            raise cmdError("RPCError: %s"%str(e))

    def usage(self):
        return "Usage: close <tql>"
Seblu's avatar
Seblu committed


class Command_declose(Command):
    '''Enable accounts'''

    def __call__(self, argv):

        if len(argv) != 2:
            raise cmdBadArgument()
        try:
            self.cli.rpc.call("declose", argv[1])
        except RpcError as e:
            raise cmdError("RPCError: %s"%str(e))

    def usage(self):
        return "Usage: declose <tql>"
Seblu's avatar
Seblu committed


class Command_passwd(Command):
    '''Change account password'''

    def __call__(self, argv):
        if len(argv) == 1:
            argv.append("a=%s"%self.cli.settings["login"])
        if len(argv) == 2:
            a = self.printer.getpass("Password: ")
            b = self.printer.getpass("Again: ")
            if a != b:
                raise cmdError("You don't type twice the same password. Aborted")
            argv.append(a)
        elif len(argv) == 3:
            if self.cli.interactive:
                s = "You cannot set password with clear argument in interactive mode.\n"
                s += "*** Think to clean your history! ***"
                raise cmdError(s)
        else:
            raise cmdBadArgument()
        try:
            self.cli.rpc.call("passwd", argv[1], argv[2])
        except RpcError as e:
            raise cmdError("RPCError: %s"%str(e))

    def usage(self):
        return "Usage: passwd [tql] [password]"