Revision 42f0a495 snf-astakos-app/astakos/im/management/commands/service-add.py

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)

Also available in: Unified diff