Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / management / commands / component-show.py @ 88f5242e

History | View | Annotate | Download (3.5 kB)

1 6ef1e2eb Giorgos Korfiatis
# Copyright 2013 GRNET S.A. All rights reserved.
2 6ef1e2eb Giorgos Korfiatis
#
3 6ef1e2eb Giorgos Korfiatis
# Redistribution and use in source and binary forms, with or
4 6ef1e2eb Giorgos Korfiatis
# without modification, are permitted provided that the following
5 6ef1e2eb Giorgos Korfiatis
# conditions are met:
6 6ef1e2eb Giorgos Korfiatis
#
7 6ef1e2eb Giorgos Korfiatis
#   1. Redistributions of source code must retain the above
8 6ef1e2eb Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
9 6ef1e2eb Giorgos Korfiatis
#      disclaimer.
10 6ef1e2eb Giorgos Korfiatis
#
11 6ef1e2eb Giorgos Korfiatis
#   2. Redistributions in binary form must reproduce the above
12 6ef1e2eb Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
13 6ef1e2eb Giorgos Korfiatis
#      disclaimer in the documentation and/or other materials
14 6ef1e2eb Giorgos Korfiatis
#      provided with the distribution.
15 6ef1e2eb Giorgos Korfiatis
#
16 6ef1e2eb Giorgos Korfiatis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 6ef1e2eb Giorgos Korfiatis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 6ef1e2eb Giorgos Korfiatis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 6ef1e2eb Giorgos Korfiatis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 6ef1e2eb Giorgos Korfiatis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 6ef1e2eb Giorgos Korfiatis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 6ef1e2eb Giorgos Korfiatis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 6ef1e2eb Giorgos Korfiatis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 6ef1e2eb Giorgos Korfiatis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 6ef1e2eb Giorgos Korfiatis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 6ef1e2eb Giorgos Korfiatis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 6ef1e2eb Giorgos Korfiatis
# POSSIBILITY OF SUCH DAMAGE.
28 6ef1e2eb Giorgos Korfiatis
#
29 6ef1e2eb Giorgos Korfiatis
# The views and conclusions contained in the software and
30 6ef1e2eb Giorgos Korfiatis
# documentation are those of the authors and should not be
31 6ef1e2eb Giorgos Korfiatis
# interpreted as representing official policies, either expressed
32 6ef1e2eb Giorgos Korfiatis
# or implied, of GRNET S.A.
33 6ef1e2eb Giorgos Korfiatis
34 6ef1e2eb Giorgos Korfiatis
from django.core.management.base import CommandError
35 6ef1e2eb Giorgos Korfiatis
from astakos.im.models import Component
36 6ef1e2eb Giorgos Korfiatis
from synnefo.lib.ordereddict import OrderedDict
37 6ef1e2eb Giorgos Korfiatis
from snf_django.management.commands import SynnefoCommand
38 6ef1e2eb Giorgos Korfiatis
from snf_django.management import utils
39 6ef1e2eb Giorgos Korfiatis
40 6ef1e2eb Giorgos Korfiatis
41 6ef1e2eb Giorgos Korfiatis
class Command(SynnefoCommand):
42 6ef1e2eb Giorgos Korfiatis
    args = "<component name or ID>"
43 6ef1e2eb Giorgos Korfiatis
    help = "Show component details"
44 6ef1e2eb Giorgos Korfiatis
45 6ef1e2eb Giorgos Korfiatis
    def handle(self, *args, **options):
46 6ef1e2eb Giorgos Korfiatis
        if len(args) != 1:
47 6ef1e2eb Giorgos Korfiatis
            raise CommandError("Please provide a component name or ID.")
48 6ef1e2eb Giorgos Korfiatis
49 6ef1e2eb Giorgos Korfiatis
        identifier = args[0]
50 6ef1e2eb Giorgos Korfiatis
        if identifier.isdigit():
51 6ef1e2eb Giorgos Korfiatis
            try:
52 6ef1e2eb Giorgos Korfiatis
                component = Component.objects.get(id=int(identifier))
53 6ef1e2eb Giorgos Korfiatis
            except Component.DoesNotExist:
54 6ef1e2eb Giorgos Korfiatis
                raise CommandError('No component found with ID %s.' %
55 6ef1e2eb Giorgos Korfiatis
                                   identifier)
56 6ef1e2eb Giorgos Korfiatis
        else:
57 6ef1e2eb Giorgos Korfiatis
            try:
58 6ef1e2eb Giorgos Korfiatis
                component = Component.objects.get(name=identifier)
59 6ef1e2eb Giorgos Korfiatis
            except Component.DoesNotExist:
60 6ef1e2eb Giorgos Korfiatis
                raise CommandError('No component found named %s.' % identifier)
61 6ef1e2eb Giorgos Korfiatis
62 6ef1e2eb Giorgos Korfiatis
        kv = OrderedDict(
63 6ef1e2eb Giorgos Korfiatis
            [
64 6ef1e2eb Giorgos Korfiatis
                ('id', component.id),
65 6ef1e2eb Giorgos Korfiatis
                ('name', component.name),
66 6ef1e2eb Giorgos Korfiatis
                ('base url', component.base_url),
67 6ef1e2eb Giorgos Korfiatis
                ('ui url', component.url),
68 6ef1e2eb Giorgos Korfiatis
                ('token', component.auth_token),
69 6ef1e2eb Giorgos Korfiatis
                ('token created', component.auth_token_created),
70 6ef1e2eb Giorgos Korfiatis
                ('token expires', component.auth_token_expires),
71 6ef1e2eb Giorgos Korfiatis
            ])
72 6ef1e2eb Giorgos Korfiatis
73 6ef1e2eb Giorgos Korfiatis
        utils.pprint_table(self.stdout, [kv.values()], kv.keys(),
74 6ef1e2eb Giorgos Korfiatis
                           options["output_format"], vertical=True)
75 6ef1e2eb Giorgos Korfiatis
76 6ef1e2eb Giorgos Korfiatis
        services = component.service_set.all()
77 6ef1e2eb Giorgos Korfiatis
78 6ef1e2eb Giorgos Korfiatis
        service_data = []
79 6ef1e2eb Giorgos Korfiatis
        for service in services:
80 6ef1e2eb Giorgos Korfiatis
            service_data.append((service.id, service.name, service.type))
81 6ef1e2eb Giorgos Korfiatis
82 6ef1e2eb Giorgos Korfiatis
        if service_data:
83 6ef1e2eb Giorgos Korfiatis
            self.stdout.write('\n')
84 6ef1e2eb Giorgos Korfiatis
            labels = ('id', 'name', 'type')
85 6ef1e2eb Giorgos Korfiatis
            utils.pprint_table(self.stdout, service_data, labels,
86 6ef1e2eb Giorgos Korfiatis
                               options["output_format"],
87 6ef1e2eb Giorgos Korfiatis
                               title='Registered services')