Statistics
| Branch: | Tag: | Revision:

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

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 22a46ee9 Giorgos Korfiatis
from django.core.management.base import CommandError
35 bd1f667b Giorgos Korfiatis
from astakos.im.models import Service, EndpointData
36 22a46ee9 Giorgos Korfiatis
from synnefo.lib.ordereddict import OrderedDict
37 7cfc0cef Christos Stavrakakis
from snf_django.management.commands import SynnefoCommand
38 d758784b Christos Stavrakakis
from snf_django.management import utils
39 22a46ee9 Giorgos Korfiatis
40 22a46ee9 Giorgos Korfiatis
41 22a46ee9 Giorgos Korfiatis
class Command(SynnefoCommand):
42 22a46ee9 Giorgos Korfiatis
    args = "<service name or ID>"
43 5e1ea6f7 Giorgos Korfiatis
    help = "Show service details"
44 22a46ee9 Giorgos Korfiatis
45 22a46ee9 Giorgos Korfiatis
    def handle(self, *args, **options):
46 22a46ee9 Giorgos Korfiatis
        if len(args) != 1:
47 22a46ee9 Giorgos Korfiatis
            raise CommandError("Please provide a service name or ID.")
48 22a46ee9 Giorgos Korfiatis
49 22a46ee9 Giorgos Korfiatis
        identifier = args[0]
50 22a46ee9 Giorgos Korfiatis
        if identifier.isdigit():
51 22a46ee9 Giorgos Korfiatis
            try:
52 22a46ee9 Giorgos Korfiatis
                service = Service.objects.get(id=int(identifier))
53 22a46ee9 Giorgos Korfiatis
            except Service.DoesNotExist:
54 22a46ee9 Giorgos Korfiatis
                raise CommandError('No service found with ID %s.' % identifier)
55 22a46ee9 Giorgos Korfiatis
        else:
56 22a46ee9 Giorgos Korfiatis
            try:
57 22a46ee9 Giorgos Korfiatis
                service = Service.objects.get(name=identifier)
58 22a46ee9 Giorgos Korfiatis
            except Service.DoesNotExist:
59 22a46ee9 Giorgos Korfiatis
                raise CommandError('No service found named %s.' % identifier)
60 22a46ee9 Giorgos Korfiatis
61 22a46ee9 Giorgos Korfiatis
        kv = OrderedDict(
62 22a46ee9 Giorgos Korfiatis
            [
63 22a46ee9 Giorgos Korfiatis
                ('id', service.id),
64 22a46ee9 Giorgos Korfiatis
                ('name', service.name),
65 bd1f667b Giorgos Korfiatis
                ('component', service.component),
66 22a46ee9 Giorgos Korfiatis
                ('type', service.type),
67 22a46ee9 Giorgos Korfiatis
            ])
68 22a46ee9 Giorgos Korfiatis
69 22a46ee9 Giorgos Korfiatis
        utils.pprint_table(self.stdout, [kv.values()], kv.keys(),
70 22a46ee9 Giorgos Korfiatis
                           options["output_format"], vertical=True)
71 bd1f667b Giorgos Korfiatis
72 bd1f667b Giorgos Korfiatis
        self.stdout.write('\n')
73 bd1f667b Giorgos Korfiatis
        endpoint_data = EndpointData.objects.filter(endpoint__service=service)
74 bd1f667b Giorgos Korfiatis
        data = []
75 bd1f667b Giorgos Korfiatis
        for ed in endpoint_data:
76 bd1f667b Giorgos Korfiatis
            data.append((ed.endpoint_id, ed.key, ed.value))
77 bd1f667b Giorgos Korfiatis
78 bd1f667b Giorgos Korfiatis
        labels = ('endpoint', 'key', 'value')
79 bd1f667b Giorgos Korfiatis
        utils.pprint_table(self.stdout, data, labels,
80 bd1f667b Giorgos Korfiatis
                           options["output_format"],
81 bd1f667b Giorgos Korfiatis
                           title='Endpoints')