Revision 42f0a495

b/snf-astakos-app/astakos/im/management/commands/service-add.py
1
# Copyright 2012 GRNET S.A. All rights reserved.
1
# Copyright 2012, 2013 GRNET S.A. All rights reserved.
2 2
#
3 3
# Redistribution and use in source and binary forms, with or
4 4
# without modification, are permitted provided that the following
......
34 34
from django.core.management.base import BaseCommand, CommandError
35 35

  
36 36
from astakos.im.api.callpoint import AstakosCallpoint
37
from astakos.im.models import Service
38

  
37 39

  
38 40
class Command(BaseCommand):
39
    args = "<name> <api_url>"
41
    args = "<name> <endpoint URL>"
40 42
    help = "Register a service"
41 43

  
42 44
    def handle(self, *args, **options):
43 45
        if len(args) < 2:
44 46
            raise CommandError("Invalid number of arguments")
45 47

  
46
        s = {'name':args[0], 'api_url':args[1]}
47
        if len(args) == 3:
48
            s['icon'] = args[2]
48
        name = args[0]
49
        api_url = args[1]
50

  
51
        try:
52
            s = Service.objects.get(name=name)
53
            m = "There already exists service named '%s'." % name
54
            raise CommandError(m)
55
        except Service.DoesNotExist:
56
            pass
57

  
58
        services = list(Service.objects.filter(api_url=api_url))
59
        if services:
60
            m = "URL '%s' is registered for another service." % api_url
61
            raise CommandError(m)
62

  
49 63
        try:
50
            c = AstakosCallpoint()
51
            r = c.add_services((s,)).next()
52
        except Exception, e:
53
            raise CommandError(e)
64
            s = Service.objects.create(name=name, api_url=api_url)
65
        except BaseException as e:
66
            raise CommandError("Failed to create service.")
54 67
        else:
55
            if r.is_success:
56
                self.stdout.write(
57
                    'Service created successfully\n')
58
            else:
59
                raise CommandError(r.reason)
68
            self.stdout.write('Token: %s\n' % s.auth_token)
b/snf-astakos-app/astakos/im/management/commands/service-list.py
1
# Copyright 2012 GRNET S.A. All rights reserved.
1
# Copyright 2012, 2013 GRNET S.A. All rights reserved.
2 2
#
3 3
# Redistribution and use in source and binary forms, with or
4 4
# without modification, are permitted provided that the following
......
31 31
# interpreted as representing official policies, either expressed
32 32
# or implied, of GRNET S.A.
33 33

  
34
from optparse import make_option
35

  
36
from django.core.management.base import NoArgsCommand
37 34

  
35
from synnefo.webproject.management.commands import ListCommand
38 36
from astakos.im.models import Service
39 37

  
40 38

  
41
class Command(NoArgsCommand):
39
class Command(ListCommand):
42 40
    help = "List services"
43

  
44
    option_list = NoArgsCommand.option_list + (
45
        make_option('-c',
46
                    action='store_true',
47
                    dest='csv',
48
                    default=False,
49
                    help="Use pipes to separate values"),
50
    )
51

  
52
    def handle_noargs(self, **options):
53
        services = Service.objects.all().order_by('id')
54

  
55
        labels = ('id', 'name', 'API url', 'auth_token')
56
        columns = (3, 12, 70, 20)
57

  
58
        if not options['csv']:
59
            line = ' '.join(l.rjust(w) for l, w in zip(labels, columns))
60
            self.stdout.write(line + '\n')
61
            sep = '-' * len(line)
62
            self.stdout.write(sep + '\n')
63

  
64
        for service in services:
65
            fields = (str(service.id), service.name,
66
                      service.api_url,
67
                      service.auth_token or '')
68

  
69
            if options['csv']:
70
                line = '|'.join(fields)
71
            else:
72
                line = ' '.join(f.rjust(w) for f, w in zip(fields, columns))
73

  
74
            self.stdout.write(line + '\n')
41
    object_class = Service
42

  
43
    FIELDS = {
44
        "id": ("id", "ID"),
45
        "name": ("name", "Service Name"),
46
        "url": ("api_url", "Service API url"),
47
        "token": ("auth_token", "Authentication token"),
48
        "created": ("auth_token_created", "Token creation date"),
49
        "expires": ("auth_token_expires", "Token expiration date"),
50
    }
51

  
52
    fields = ["id", "name", "url", "token", "created", "expires"]

Also available in: Unified diff