Commit 82572759 authored by Giorgos Korfiatis's avatar Giorgos Korfiatis
Browse files

astakos: Improve argument handling in commands

parent da9ff70f
......@@ -40,7 +40,7 @@ class Command(BaseCommand):
help = "Register a component"
def handle(self, *args, **options):
if len(args) < 1:
if len(args) < 2:
raise CommandError("Invalid number of arguments")
name = args[0]
......
......@@ -84,6 +84,10 @@ class Command(BaseCommand):
renew_token = options.get('renew_token')
purge_services = options.get('purge_services')
if not any([url, auth_token, renew_token, purge_services]):
raise CommandError("No option specified.")
if url:
component.url = url
......
......@@ -80,50 +80,34 @@ class Command(BaseCommand):
"e.g. when denying a project")),
)
@commit_on_success_strict()
def handle(self, *args, **options):
message = options['message']
pid = options['terminate']
if pid is not None:
self.run_command(terminate, pid)
return
pid = options['resume']
if pid is not None:
self.run_command(resume, pid)
return
pid = options['suspend']
if pid is not None:
self.run_command(suspend, pid)
return
appid = options['approve']
if appid is not None:
self.run_command(approve_application, appid)
return
appid = options['deny']
if appid is not None:
self.run_command(deny_application, appid, reason=message)
return
if options['check_expired']:
self.expire(execute=False)
return
if options['terminate_expired']:
self.expire(execute=True)
def run_command(self, func, *args, **kwargs):
@commit_on_success_strict()
def inner():
try:
func(*args, **kwargs)
except BaseException as e:
raise CommandError(e)
inner()
actions = {
'terminate': terminate,
'resume': resume,
'suspend': suspend,
'approve': approve_application,
'deny': lambda a: deny_application(a, reason=message),
'check_expired': lambda _: self.expire(execute=False),
'terminate_expired': lambda _: self.expire(execute=True),
}
opts = [(key, value)
for (key, value) in options.items()
if key in actions and value]
if len(opts) != 1:
raise CommandError("Specify exactly one operation.")
key, value = opts[0]
action = actions[key]
try:
action(value)
except BaseException as e:
raise CommandError(e)
def print_expired(self, projects, execute):
length = len(projects)
......@@ -152,10 +136,6 @@ class Command(BaseCommand):
self.stdout.write('%d projects have been terminated.\n' % (
length,))
@commit_on_success_strict()
def expire(self, execute=False):
try:
projects = check_expiration(execute=execute)
self.print_expired(projects, execute)
except BaseException as e:
raise CommandError(e)
projects = check_expiration(execute=execute)
self.print_expired(projects, execute)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment