__init__.py 12.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Copyright 2012-2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
#   1. Redistributions of source code must retain the above
#      copyright notice, this list of conditions and the following
#      disclaimer.
#
#   2. Redistributions in binary form must reproduce the above
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials
#      provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.

from optparse import make_option

from django.core.management.base import BaseCommand, CommandError
from django.core.exceptions import FieldError

from snf_django.management import utils
from snf_django.lib.astakos import UserCache

42
43
import distutils

44
45
46
47
48
49
50
51
52

class SynnefoCommand(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option(
            "--output-format",
            dest="output_format",
            metavar="[pretty, csv, json]",
            default="pretty",
            choices=["pretty", "csv", "json"],
53
54
            help="Select the output format: pretty [the default], json, "
                 "csv [comma-separated output]"),
55
56
57
    )


58
class ListCommand(SynnefoCommand):
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
    """Generic *-list management command.

    Management command to handle common tasks when implementing a -list
    management command. This class handles the following tasks:

    * Retrieving objects from database.

    The DB model class is declared in ``object_class`` class attribute. Also,
    results can be filter using either the ``filters`` and ``excludes``
    attribute or the "--filter-by" option.

    * Display specific fields of the database objects.

    List of available fields is defined in the ``FIELDS`` class attribute,
    which is a dictionary mapping from field names to tuples containing the
    way the field is retrieved and a text help message to display. The first
    field of the tuple is either a string containing a chain of attribute
    accesses (e.g. "machine.flavor.cpu") either a callable function, taking
    as argument the DB object and returning a single value.

    The fields that will be displayed be default is contained in the ``fields``
    class attribute. The user can specify different fields using the "--fields"
    option.

    * Handling of user UUIDs and names.

    If the ``user_uuid_field`` is declared, then "--user" and "--display-mails"
    options will become available. The first one allows filtering via either
    a user's UUID or display name. The "--displayname" option will append
    the displayname of ther user with "user_uuid_field" to the output.

    * Pretty printing output to a nice table.

    """

    # The following fields must be handled in the ListCommand subclasses!

    # The django DB model
    object_class = None
    # The name of the field containg the user ID of the user, if any.
    user_uuid_field = None
    # The name of the field containg the deleted flag, if any.
    deleted_field = None
    # Dictionary with all available fields
    FIELDS = {}
    # List of fields to display by default
    fields = []
    # Default filters and excludes
    filters = {}
    excludes = {}
    # Order results
    order_by = None

    # Fields used only with user_user_field
113
    astakos_auth_url = None
114
115
    astakos_token = None

116
117
118
119
    # Optimize DB queries
    prefetch_related = []
    select_related = []

120
    help = "Generic List Command"
121
    option_list = SynnefoCommand.option_list + (
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
        make_option(
            "-o", "--output",
            dest="fields",
            help="Comma-separated list of output fields"),
        make_option(
            "--list-fields",
            dest="list_fields",
            action="store_true",
            default=False,
            help="List available output fields"),
        make_option(
            "--filter-by",
            dest="filter_by",
            metavar="FILTERS",
            help="Filter results. Comma separated list of key `cond` val pairs"
                 " that displayed entries must satisfy. e.g."
                 " --filter-by \"deleted=False,id>=22\"."),
        make_option(
            "--list-filters",
            dest="list_filters",
            action="store_true",
            default=False,
            help="List available filters"),
        make_option(
            "--no-headers",
            dest="headers",
            action="store_false",
            default=True,
            help="Do not display headers"),
    )

    def __init__(self, *args, **kwargs):
        if self.user_uuid_field:
155
156
157
            assert(self.astakos_auth_url), "astakos_auth_url attribute is "\
                                           "needed when user_uuid_field "\
                                           "is declared"
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
            assert(self.astakos_token), "astakos_token attribute is needed"\
                                        " when user_uuid_field is declared"
            self.option_list += (
                make_option(
                    "-u", "--user",
                    dest="user",
                    metavar="USER",
                    help="List items only for this user."
                         " 'USER' can be either a user UUID or a display"
                         " name"),
                make_option(
                    "--display-mails",
                    dest="display_mails",
                    action="store_true",
                    default=False,
                    help="Include the user's email"),
            )

        if self.deleted_field:
            self.option_list += (
                make_option(
                    "-d", "--deleted",
                    dest="deleted",
                    action="store_true",
                    help="Display only deleted items"),
            )
        super(ListCommand, self).__init__(*args, **kwargs)

    def handle(self, *args, **options):
        if len(args) > 0:
            raise CommandError("List commands do not accept any argument")

        assert(self.object_class), "object_class variable must be declared"

        if options["list_fields"]:
            self.display_fields()
            return

        if options["list_filters"]:
            self.display_filters()
            return

        # --output option
        if options["fields"]:
            fields = options["fields"]
            fields = fields.split(",")
            self.validate_fields(fields)
            self.fields = options["fields"].split(",")

        # --filter-by option
        if options["filter_by"]:
209
210
            filters, excludes = \
                utils.parse_queryset_filters(options["filter_by"])
211
212
213
214
215
216
217
218
219
220
        else:
            filters, excludes = ({}, {})

        self.filters.update(filters)
        self.excludes.update(excludes)

        # --user option
        user = options.get("user")
        if user:
            if "@" in user:
221
                ucache = UserCache(self.astakos_auth_url, self.astakos_token)
222
223
224
225
226
227
228
229
230
231
232
233
234
235
                user = ucache.get_uuid(user)
            self.filters[self.user_uuid_field] = user

        # --deleted option
        if self.deleted_field:
            deleted = options.get("deleted")
            if deleted:
                self.filters[self.deleted_field] = True
            else:
                self.filters[self.deleted_field] = False

        # Special handling of arguments
        self.handle_args(self, *args, **options)

236
237
238
        select_related = getattr(self, "select_related", [])
        prefetch_related = getattr(self, "prefetch_related", [])

239
240
        objects = self.object_class.objects
        try:
241
242
243
244
            for sr in select_related:
                objects = objects.select_related(sr)
            for pr in prefetch_related:
                objects = objects.prefetch_related(pr)
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
            objects = objects.filter(**self.filters)
            objects = objects.exclude(**self.excludes)
        except FieldError as e:
            raise CommandError(e)
        except Exception as e:
            raise CommandError("Can not filter results: %s" % e)

        order_key = self.order_by if self.order_by is not None else 'pk'
        objects = objects.order_by(order_key)

        # --display-mails option
        display_mails = options.get("display_mails")
        if display_mails:
            if 'user_mail' in self.object_class._meta.get_all_field_names():
                raise RuntimeError("%s has already a 'user_mail' attribute")

            self.fields.append("user.email")
            self.FIELDS["user.email"] =\
                ("user_email", "The email of the owner.")
            uuids = [getattr(obj, self.user_uuid_field) for obj in objects]
265
            ucache = UserCache(self.astakos_auth_url, self.astakos_token)
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
            ucache.fetch_names(list(set(uuids)))
            for obj in objects:
                uuid = getattr(obj, self.user_uuid_field)
                obj.user_email = ucache.get_name(uuid)

        # Special handling of DB results
        objects = list(objects)
        self.handle_db_objects(objects, **options)

        headers = self.fields
        columns = [self.FIELDS[key][0] for key in headers]

        table = []
        for obj in objects:
            row = []
            for attr in columns:
                if callable(attr):
                    row.append(attr(obj))
                else:
                    item = obj
                    attrs = attr.split(".")
                    for attr in attrs:
                        item = getattr(item, attr)
                    row.append(item)
            table.append(row)

        # Special handle of output
        self.handle_output(table, headers)

        # Print output
        output_format = options["output_format"]
        if output_format != "json" and not options["headers"]:
            headers = None
        utils.pprint_table(self.stdout, table, headers, output_format)

    def handle_args(self, *args, **kwargs):
        pass

    def handle_db_objects(self, objects, **options):
        pass

    def handle_output(self, table, headers):
        pass

    def display_fields(self):
        headers = ["Field", "Description"]
        table = []
        for field, (_, help_msg) in self.FIELDS.items():
            table.append((field, help_msg))
        utils.pprint_table(self.stdout, table, headers)

    def validate_fields(self, fields):
        for f in fields:
            if f not in self.FIELDS.keys():
                raise CommandError("Unknown field '%s'. 'Use --list-fields"
                                   " option to find out available fields."
                                   % f)

    def display_filters(self):
        headers = ["Filter", "Description", "Help"]
        table = []
        for field in self.object_class._meta.fields:
            table.append((field.name, field.verbose_name, field.help_text))
        utils.pprint_table(self.stdout, table, headers)
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348


class RemoveCommand(BaseCommand):
    help = "Generic remove command"
    option_list = BaseCommand.option_list + (
        make_option(
            "-f", "--force",
            dest="force",
            action="store_true",
            default=False,
            help="Do not prompt for confirmation"),
    )

    def confirm_deletion(self, force, resource='', args=''):
        if force is True:
            return True

        ids = ', '.join(args)
        self.stdout.write("Are you sure you want to delete %s %s?"
349
                          " [Y/N] " % (resource, ids))
350
351
352
353
354
        try:
            answer = distutils.util.strtobool(raw_input())
            if answer != 1:
                raise CommandError("Aborting deletion")
        except ValueError:
355
356
            raise CommandError("Unaccepted input value. Please choose yes/no"
                               " (y/n).")