Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / api / management / commands / listnetworks.py @ af6a3bc5

History | View | Annotate | Download (4.3 kB)

1 8fad925a Giorgos Verigakis
# Copyright 2012 GRNET S.A. All rights reserved.
2 8fad925a Giorgos Verigakis
#
3 8fad925a Giorgos Verigakis
# Redistribution and use in source and binary forms, with or
4 8fad925a Giorgos Verigakis
# without modification, are permitted provided that the following
5 8fad925a Giorgos Verigakis
# conditions are met:
6 8fad925a Giorgos Verigakis
#
7 8fad925a Giorgos Verigakis
#   1. Redistributions of source code must retain the above
8 8fad925a Giorgos Verigakis
#      copyright notice, this list of conditions and the following
9 8fad925a Giorgos Verigakis
#      disclaimer.
10 8fad925a Giorgos Verigakis
#
11 8fad925a Giorgos Verigakis
#   2. Redistributions in binary form must reproduce the above
12 8fad925a Giorgos Verigakis
#      copyright notice, this list of conditions and the following
13 8fad925a Giorgos Verigakis
#      disclaimer in the documentation and/or other materials
14 8fad925a Giorgos Verigakis
#      provided with the distribution.
15 8fad925a Giorgos Verigakis
#
16 8fad925a Giorgos Verigakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 8fad925a Giorgos Verigakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 8fad925a Giorgos Verigakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 8fad925a Giorgos Verigakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 8fad925a Giorgos Verigakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 8fad925a Giorgos Verigakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 8fad925a Giorgos Verigakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 8fad925a Giorgos Verigakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 8fad925a Giorgos Verigakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 8fad925a Giorgos Verigakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 8fad925a Giorgos Verigakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 8fad925a Giorgos Verigakis
# POSSIBILITY OF SUCH DAMAGE.
28 8fad925a Giorgos Verigakis
#
29 8fad925a Giorgos Verigakis
# The views and conclusions contained in the software and
30 8fad925a Giorgos Verigakis
# documentation are those of the authors and should not be
31 8fad925a Giorgos Verigakis
# interpreted as representing official policies, either expressed
32 8fad925a Giorgos Verigakis
# or implied, of GRNET S.A.
33 8fad925a Giorgos Verigakis
34 8fad925a Giorgos Verigakis
from optparse import make_option
35 8fad925a Giorgos Verigakis
36 8fad925a Giorgos Verigakis
from django.core.management.base import BaseCommand, CommandError
37 8fad925a Giorgos Verigakis
38 8fad925a Giorgos Verigakis
from synnefo.db.models import Network
39 8fad925a Giorgos Verigakis
40 8fad925a Giorgos Verigakis
from ._common import format_bool
41 8fad925a Giorgos Verigakis
42 8fad925a Giorgos Verigakis
43 8fad925a Giorgos Verigakis
class Command(BaseCommand):
44 8fad925a Giorgos Verigakis
    help = "List networks"
45 8fad925a Giorgos Verigakis
46 8fad925a Giorgos Verigakis
    option_list = BaseCommand.option_list + (
47 8fad925a Giorgos Verigakis
        make_option('-c',
48 8fad925a Giorgos Verigakis
            action='store_true',
49 8fad925a Giorgos Verigakis
            dest='csv',
50 8fad925a Giorgos Verigakis
            default=False,
51 8fad925a Giorgos Verigakis
            help="Use pipes to separate values"),
52 8fad925a Giorgos Verigakis
        make_option('--deleted',
53 8fad925a Giorgos Verigakis
            action='store_true',
54 8fad925a Giorgos Verigakis
            dest='deleted',
55 8fad925a Giorgos Verigakis
            default=False,
56 8fad925a Giorgos Verigakis
            help="List only deleted networks"),
57 8fad925a Giorgos Verigakis
        make_option('--public',
58 8fad925a Giorgos Verigakis
            action='store_true',
59 8fad925a Giorgos Verigakis
            dest='public',
60 8fad925a Giorgos Verigakis
            default=False,
61 8fad925a Giorgos Verigakis
            help="List only public networks"),
62 cc3f266e Christos Stavrakakis
        make_option('--ipv6',
63 cc3f266e Christos Stavrakakis
            action='store_true',
64 cc3f266e Christos Stavrakakis
            dest='ipv6',
65 cc3f266e Christos Stavrakakis
            default=False,
66 cc3f266e Christos Stavrakakis
            help="Show IPv6 information of the network"),
67 8fad925a Giorgos Verigakis
        )
68 8fad925a Giorgos Verigakis
69 8fad925a Giorgos Verigakis
    def handle(self, *args, **options):
70 8fad925a Giorgos Verigakis
        if args:
71 8fad925a Giorgos Verigakis
            raise CommandError("Command doesn't accept any arguments")
72 8fad925a Giorgos Verigakis
73 8fad925a Giorgos Verigakis
        networks = Network.objects.all()
74 8fad925a Giorgos Verigakis
        if options['deleted']:
75 5061546f Christos Stavrakakis
            networks = networks.filter(deleted=True)
76 8fad925a Giorgos Verigakis
        else:
77 5061546f Christos Stavrakakis
            networks = networks.exclude(deleted=True)
78 8fad925a Giorgos Verigakis
79 8fad925a Giorgos Verigakis
        if options['public']:
80 8fad925a Giorgos Verigakis
            networks = networks.filter(public=True)
81 8fad925a Giorgos Verigakis
82 cc3f266e Christos Stavrakakis
        labels = ['id', 'name', 'type', 'owner',
83 cc3f266e Christos Stavrakakis
                  'mac_prefix', 'dhcp', 'state', 'link', 'vms', 'public']
84 cc3f266e Christos Stavrakakis
        columns = [3, 16, 22, 30, 10, 6, 8, 12, 4, 6]
85 cc3f266e Christos Stavrakakis
86 cc3f266e Christos Stavrakakis
        if options['ipv6']:
87 cc3f266e Christos Stavrakakis
            labels.extend(['IPv6 Subnet', 'IPv6 Gateway'])
88 cc3f266e Christos Stavrakakis
            columns.extend([16, 16])
89 cc3f266e Christos Stavrakakis
        else:
90 cc3f266e Christos Stavrakakis
            labels.extend(['IPv4 Subnet', 'IPv4 Gateway'])
91 cc3f266e Christos Stavrakakis
            columns.extend([14, 14])
92 8fad925a Giorgos Verigakis
93 8fad925a Giorgos Verigakis
        if not options['csv']:
94 8fad925a Giorgos Verigakis
            line = ' '.join(l.rjust(w) for l, w in zip(labels, columns))
95 8fad925a Giorgos Verigakis
            self.stdout.write(line + '\n')
96 8fad925a Giorgos Verigakis
            sep = '-' * len(line)
97 8fad925a Giorgos Verigakis
            self.stdout.write(sep + '\n')
98 8fad925a Giorgos Verigakis
99 8fad925a Giorgos Verigakis
        for network in networks:
100 cc3f266e Christos Stavrakakis
            fields = [str(network.id),
101 8fad925a Giorgos Verigakis
                      network.name,
102 6dd70a5c Christos Stavrakakis
                      network.type,
103 8fad925a Giorgos Verigakis
                      network.userid or '',
104 bf1d3a70 Christos Stavrakakis
                      network.mac_prefix or '',
105 bf1d3a70 Christos Stavrakakis
                      str(network.dhcp),
106 8fad925a Giorgos Verigakis
                      network.state,
107 bf1d3a70 Christos Stavrakakis
                      network.link or '',
108 8fad925a Giorgos Verigakis
                      str(network.machines.count()),
109 cc3f266e Christos Stavrakakis
                      format_bool(network.public)]
110 cc3f266e Christos Stavrakakis
111 cc3f266e Christos Stavrakakis
            if options['ipv6']:
112 cc3f266e Christos Stavrakakis
                fields.extend([network.subnet6 or '', network.gateway6 or ''])
113 cc3f266e Christos Stavrakakis
            else:
114 cc3f266e Christos Stavrakakis
                fields.extend([network.subnet, network.gateway or ''])
115 8fad925a Giorgos Verigakis
116 8fad925a Giorgos Verigakis
            if options['csv']:
117 8fad925a Giorgos Verigakis
                line = '|'.join(fields)
118 8fad925a Giorgos Verigakis
            else:
119 8fad925a Giorgos Verigakis
                line = ' '.join(f.rjust(w) for f, w in zip(fields, columns))
120 8fad925a Giorgos Verigakis
121 8fad925a Giorgos Verigakis
            self.stdout.write(line.encode('utf8') + '\n')