Newer
Older
#!/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 OptionCommand, TqlCommand
class Command_addaccount(TqlCommand):
TqlCommand.__init__(self, cli, argv0)
self.remove_option("--direct")
self.remove_option("--raw")
self.set_usage("%prog [options] <account name> <role> [password]")
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:
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)
class Command_delaccount(TqlCommand):
def __init__(self, cli, argv0):
TqlCommand.__init__(self, cli, argv0)
self.tql_filter += "&a"
self.parse_args(argv)
if len(self.args) != 1:
raise cmdBadArgument("<tql>")
self.rpccall("delaccount", self.args[0])
class Command_close(TqlCommand):
def __init__(self, cli, argv0):
TqlCommand.__init__(self, cli, argv0)
self.tql_filter += "&a&-close"
self.parse_args(argv)
if len(self.args) != 1:
raise cmdBadArgument("<tql>")
self.rpccall("close", self.args[0])
class Command_declose(TqlCommand):
def __init__(self, cli, argv0):
TqlCommand.__init__(self, cli, argv0)
self.tql_filter += "&a&close"
self.parse_args(argv)
if len(self.args) != 1:
raise cmdBadArgument("<tql>")
self.rpccall("declose", self.args[0])
def __init__(self, cli, argv0):
TqlCommand.__init__(self, cli, argv0)
self.tql_filter += "&a"
def __init__(self, cli, argv0):
TqlCommand.__init__(self, cli, argv0)
self.set_usage("%prog [options] [tql] [password]")
# 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()
# 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)