Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / api / management / commands / flavor-list.py @ d443e1dd

History | View | Annotate | Download (3.5 kB)

1 76f5441f Giorgos Verigakis
# Copyright 2012 GRNET S.A. All rights reserved.
2 76f5441f Giorgos Verigakis
#
3 76f5441f Giorgos Verigakis
# Redistribution and use in source and binary forms, with or
4 76f5441f Giorgos Verigakis
# without modification, are permitted provided that the following
5 76f5441f Giorgos Verigakis
# conditions are met:
6 76f5441f Giorgos Verigakis
#
7 76f5441f Giorgos Verigakis
#   1. Redistributions of source code must retain the above
8 76f5441f Giorgos Verigakis
#      copyright notice, this list of conditions and the following
9 76f5441f Giorgos Verigakis
#      disclaimer.
10 76f5441f Giorgos Verigakis
#
11 76f5441f Giorgos Verigakis
#   2. Redistributions in binary form must reproduce the above
12 76f5441f Giorgos Verigakis
#      copyright notice, this list of conditions and the following
13 76f5441f Giorgos Verigakis
#      disclaimer in the documentation and/or other materials
14 76f5441f Giorgos Verigakis
#      provided with the distribution.
15 76f5441f Giorgos Verigakis
#
16 76f5441f Giorgos Verigakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 76f5441f Giorgos Verigakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 76f5441f Giorgos Verigakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 76f5441f Giorgos Verigakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 76f5441f Giorgos Verigakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 76f5441f Giorgos Verigakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 76f5441f Giorgos Verigakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 76f5441f Giorgos Verigakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 76f5441f Giorgos Verigakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 76f5441f Giorgos Verigakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 76f5441f Giorgos Verigakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 76f5441f Giorgos Verigakis
# POSSIBILITY OF SUCH DAMAGE.
28 76f5441f Giorgos Verigakis
#
29 76f5441f Giorgos Verigakis
# The views and conclusions contained in the software and
30 76f5441f Giorgos Verigakis
# documentation are those of the authors and should not be
31 76f5441f Giorgos Verigakis
# interpreted as representing official policies, either expressed
32 76f5441f Giorgos Verigakis
# or implied, of GRNET S.A.
33 76f5441f Giorgos Verigakis
34 76f5441f Giorgos Verigakis
from optparse import make_option
35 76f5441f Giorgos Verigakis
36 76f5441f Giorgos Verigakis
from django.core.management.base import BaseCommand, CommandError
37 7a0aa449 Christos Stavrakakis
from synnefo.management.common import (format_bool, filter_results,
38 7a0aa449 Christos Stavrakakis
                                       pprint_table)
39 76f5441f Giorgos Verigakis
40 76f5441f Giorgos Verigakis
from synnefo.db.models import Flavor
41 76f5441f Giorgos Verigakis
42 bad9404c Christos Stavrakakis
FIELDS = Flavor._meta.get_all_field_names()
43 bad9404c Christos Stavrakakis
44 76f5441f Giorgos Verigakis
45 76f5441f Giorgos Verigakis
class Command(BaseCommand):
46 76f5441f Giorgos Verigakis
    help = "List flavors"
47 af88de58 Christos Stavrakakis
48 76f5441f Giorgos Verigakis
    option_list = BaseCommand.option_list + (
49 76f5441f Giorgos Verigakis
        make_option('-c',
50 76f5441f Giorgos Verigakis
            action='store_true',
51 76f5441f Giorgos Verigakis
            dest='csv',
52 76f5441f Giorgos Verigakis
            default=False,
53 76f5441f Giorgos Verigakis
            help="Use pipes to separate values"),
54 af88de58 Christos Stavrakakis
        make_option('--deleted',
55 af88de58 Christos Stavrakakis
            action='store_true',
56 af88de58 Christos Stavrakakis
            dest='deleted',
57 af88de58 Christos Stavrakakis
            default=False,
58 af88de58 Christos Stavrakakis
            help="Include deleted flavors"),
59 bad9404c Christos Stavrakakis
         make_option('--filter-by',
60 bad9404c Christos Stavrakakis
            dest='filter_by',
61 bad9404c Christos Stavrakakis
            help="Filter results. Comma seperated list of key=val pairs"
62 bad9404c Christos Stavrakakis
                 " that displayed entries must satisfy. e.g."
63 bad9404c Christos Stavrakakis
                 " --filter-by \"cpu=1,ram!=1024\"."
64 bad9404c Christos Stavrakakis
                 "Available keys are: %s" % ", ".join(FIELDS))
65 76f5441f Giorgos Verigakis
        )
66 af88de58 Christos Stavrakakis
67 76f5441f Giorgos Verigakis
    def handle(self, *args, **options):
68 76f5441f Giorgos Verigakis
        if args:
69 76f5441f Giorgos Verigakis
            raise CommandError("Command doesn't accept any arguments")
70 af88de58 Christos Stavrakakis
71 af88de58 Christos Stavrakakis
        if options['deleted']:
72 af88de58 Christos Stavrakakis
            flavors = Flavor.objects.all()
73 af88de58 Christos Stavrakakis
        else:
74 af88de58 Christos Stavrakakis
            flavors = Flavor.objects.filter(deleted=False)
75 af88de58 Christos Stavrakakis
76 bad9404c Christos Stavrakakis
        filter_by = options['filter_by']
77 bad9404c Christos Stavrakakis
        if filter_by:
78 bad9404c Christos Stavrakakis
            flavors = filter_results(flavors, filter_by)
79 bad9404c Christos Stavrakakis
80 7a0aa449 Christos Stavrakakis
        headers = ('id', 'name', 'cpus', 'ram', 'disk', 'template', 'deleted')
81 7a0aa449 Christos Stavrakakis
        table = []
82 6639a243 Christos Stavrakakis
        for flavor in flavors.order_by('id'):
83 76f5441f Giorgos Verigakis
            id = str(flavor.id)
84 76f5441f Giorgos Verigakis
            cpu = str(flavor.cpu)
85 76f5441f Giorgos Verigakis
            ram = str(flavor.ram)
86 76f5441f Giorgos Verigakis
            disk = str(flavor.disk)
87 76f5441f Giorgos Verigakis
            deleted = format_bool(flavor.deleted)
88 76f5441f Giorgos Verigakis
            fields = (id, flavor.name, cpu, ram, disk, flavor.disk_template,
89 76f5441f Giorgos Verigakis
                      deleted)
90 af88de58 Christos Stavrakakis
91 7a0aa449 Christos Stavrakakis
            table.append(fields)
92 af88de58 Christos Stavrakakis
93 7a0aa449 Christos Stavrakakis
        separator = " | " if options['csv'] else None
94 7a0aa449 Christos Stavrakakis
        pprint_table(self.stdout, table, headers, separator)