Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / management / commands / service-show.py @ 35cbac33

History | View | Annotate | Download (3.2 kB)

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