pool-show.py 2.44 KB
Newer Older
Vangelis Koukis's avatar
Vangelis Koukis committed
1
# Copyright (C) 2010-2014 GRNET S.A.
2
#
Vangelis Koukis's avatar
Vangelis Koukis committed
3
4
5
6
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
7
#
Vangelis Koukis's avatar
Vangelis Koukis committed
8
9
10
11
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
12
#
Vangelis Koukis's avatar
Vangelis Koukis committed
13
14
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
15

16
from django.core.management.base import CommandError
17
18
19
from optparse import make_option

from synnefo.db.pools import bitarray_to_map
20
from synnefo.management import pprint, common
21
from snf_django.management.commands import SynnefoCommand
22
23
24

POOL_CHOICES = ['bridge', 'mac-prefix']

Christos Stavrakakis's avatar
Christos Stavrakakis committed
25

26
class Command(SynnefoCommand):
27
    args = "<pool_id>"
28
    help = "Show a pool"
29
    output_transaction = True
30
    option_list = SynnefoCommand.option_list + (
31
32
33
34
35
36
37
38
39
40
41
42
        make_option('--type', dest='type',
                    choices=POOL_CHOICES,
                    help="Type of pool"
                    ),
    )

    def handle(self, *args, **options):
        type_ = options['type']

        if not type_:
            raise CommandError("Type of pool is mandatory")

43
        pool_table = common.pool_table_from_type(type_)
44
45
46
47

        try:
            pool_id = int(args[0])
            pool_row = pool_table.objects.get(id=pool_id)
48
        except IndexError:
Christos Stavrakakis's avatar
Christos Stavrakakis committed
49
            raise CommandError("Please provide a pool ID")
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
        except (ValueError, pool_table.DoesNotExist):
            raise CommandError("Invalid pool ID")

        pool = pool_row.pool

        kv = {
            'id': pool_row.id,
            'offset': pool_row.offset,
            'base': pool_row.base,
            'size': pool_row.size,
            'available': pool.count_available(),
            'reserved': pool.count_reserved(),
        }

        for key, val in sorted(kv.items()):
            line = '%s: %s\n' % (key.rjust(16), val)
            self.stdout.write(line.encode('utf8'))

68
        step = (type_ == 'bridge') and 64 or 80
69
70
71
72
        pprint.pprint_pool('Available', pool.to_map(), step, self.stdout)
        pprint.pprint_pool('Reserved',
                           bitarray_to_map(pool.reserved[:pool_row.size]),
                           step, self.stdout)