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

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

Seblu's avatar
Seblu committed
    def __init__(self, cli, argv0):
        OptionCommand.__init__(self, cli, argv0)
        self.set_usage("%prog [options] <account name> <role>")

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:
Seblu's avatar
Seblu committed
            raise cmdBadArgument()
Seblu's avatar
Seblu committed
        # execute command
        self.rpccall("addaccount", self.args[0], self.args[1])
Seblu's avatar
Seblu committed

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

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

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

    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


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]"