Commit 146326c4 authored by Giorgos Korfiatis's avatar Giorgos Korfiatis

astakos: update service-add and service-list

parent 4d9d67d5
# Copyright 2012 GRNET S.A. All rights reserved. # Copyright 2012, 2013 GRNET S.A. All rights reserved.
# #
# Redistribution and use in source and binary forms, with or # Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following # without modification, are permitted provided that the following
...@@ -34,26 +34,35 @@ ...@@ -34,26 +34,35 @@
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from astakos.im.api.callpoint import AstakosCallpoint from astakos.im.api.callpoint import AstakosCallpoint
from astakos.im.models import Service
class Command(BaseCommand): class Command(BaseCommand):
args = "<name> <api_url>" args = "<name> <endpoint URL>"
help = "Register a service" help = "Register a service"
def handle(self, *args, **options): def handle(self, *args, **options):
if len(args) < 2: if len(args) < 2:
raise CommandError("Invalid number of arguments") raise CommandError("Invalid number of arguments")
s = {'name':args[0], 'api_url':args[1]} name = args[0]
if len(args) == 3: api_url = args[1]
s['icon'] = args[2]
try:
s = Service.objects.get(name=name)
m = "There already exists service named '%s'." % name
raise CommandError(m)
except Service.DoesNotExist:
pass
services = list(Service.objects.filter(api_url=api_url))
if services:
m = "URL '%s' is registered for another service." % api_url
raise CommandError(m)
try: try:
c = AstakosCallpoint() s = Service.objects.create(name=name, api_url=api_url)
r = c.add_services((s,)).next() except BaseException as e:
except Exception, e: raise CommandError("Failed to create service.")
raise CommandError(e)
else: else:
if r.is_success: self.stdout.write('Token: %s\n' % s.auth_token)
self.stdout.write(
'Service created successfully\n')
else:
raise CommandError(r.reason)
# Copyright 2012 GRNET S.A. All rights reserved. # Copyright 2012, 2013 GRNET S.A. All rights reserved.
# #
# Redistribution and use in source and binary forms, with or # Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following # without modification, are permitted provided that the following
...@@ -31,44 +31,22 @@ ...@@ -31,44 +31,22 @@
# interpreted as representing official policies, either expressed # interpreted as representing official policies, either expressed
# or implied, of GRNET S.A. # or implied, of GRNET S.A.
from optparse import make_option
from django.core.management.base import NoArgsCommand
from synnefo.webproject.management.commands import ListCommand
from astakos.im.models import Service from astakos.im.models import Service
class Command(NoArgsCommand): class Command(ListCommand):
help = "List services" help = "List services"
object_class = Service
option_list = NoArgsCommand.option_list + (
make_option('-c', FIELDS = {
action='store_true', "id": ("id", "ID"),
dest='csv', "name": ("name", "Service Name"),
default=False, "url": ("api_url", "Service API url"),
help="Use pipes to separate values"), "token": ("auth_token", "Authentication token"),
) "created": ("auth_token_created", "Token creation date"),
"expires": ("auth_token_expires", "Token expiration date"),
def handle_noargs(self, **options): }
services = Service.objects.all().order_by('id')
fields = ["id", "name", "url", "token", "created", "expires"]
labels = ('id', 'name', 'API url', 'auth_token')
columns = (3, 12, 70, 20)
if not options['csv']:
line = ' '.join(l.rjust(w) for l, w in zip(labels, columns))
self.stdout.write(line + '\n')
sep = '-' * len(line)
self.stdout.write(sep + '\n')
for service in services:
fields = (str(service.id), service.name,
service.api_url,
service.auth_token or '')
if options['csv']:
line = '|'.join(fields)
else:
line = ' '.join(f.rjust(w) for f, w in zip(fields, columns))
self.stdout.write(line + '\n')
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