Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-app / pithos / api / management / commands / storagequota.py @ b17e5550

History | View | Annotate | Download (2.7 kB)

1 e9500731 Antony Chazapis
# Copyright 2012 GRNET S.A. All rights reserved.
2 e9500731 Antony Chazapis
#
3 e9500731 Antony Chazapis
# Redistribution and use in source and binary forms, with or
4 e9500731 Antony Chazapis
# without modification, are permitted provided that the following
5 e9500731 Antony Chazapis
# conditions are met:
6 e9500731 Antony Chazapis
#
7 e9500731 Antony Chazapis
#   1. Redistributions of source code must retain the above
8 e9500731 Antony Chazapis
#      copyright notice, this list of conditions and the following
9 e9500731 Antony Chazapis
#      disclaimer.
10 e9500731 Antony Chazapis
#
11 e9500731 Antony Chazapis
#   2. Redistributions in binary form must reproduce the above
12 e9500731 Antony Chazapis
#      copyright notice, this list of conditions and the following
13 e9500731 Antony Chazapis
#      disclaimer in the documentation and/or other materials
14 e9500731 Antony Chazapis
#      provided with the distribution.
15 e9500731 Antony Chazapis
#
16 e9500731 Antony Chazapis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 e9500731 Antony Chazapis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 e9500731 Antony Chazapis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 e9500731 Antony Chazapis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 e9500731 Antony Chazapis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 e9500731 Antony Chazapis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 e9500731 Antony Chazapis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 e9500731 Antony Chazapis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 e9500731 Antony Chazapis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 e9500731 Antony Chazapis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 e9500731 Antony Chazapis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 e9500731 Antony Chazapis
# POSSIBILITY OF SUCH DAMAGE.
28 e9500731 Antony Chazapis
#
29 e9500731 Antony Chazapis
# The views and conclusions contained in the software and
30 e9500731 Antony Chazapis
# documentation are those of the authors and should not be
31 e9500731 Antony Chazapis
# interpreted as representing official policies, either expressed
32 e9500731 Antony Chazapis
# or implied, of GRNET S.A.
33 e9500731 Antony Chazapis
34 e9500731 Antony Chazapis
from optparse import make_option
35 e9500731 Antony Chazapis
36 e9500731 Antony Chazapis
from django.core.management.base import BaseCommand, CommandError
37 e9500731 Antony Chazapis
38 b1dadd0e Sofia Papagiannaki
from pithos.api.util import get_backend
39 e9500731 Antony Chazapis
40 19ddd41b Sofia Papagiannaki
41 e9500731 Antony Chazapis
class Command(BaseCommand):
42 e9500731 Antony Chazapis
    args = "<user>"
43 e9500731 Antony Chazapis
    help = "Get/set a user's quota"
44 d50ed8d4 Sofia Papagiannaki
45 e9500731 Antony Chazapis
    option_list = BaseCommand.option_list + (
46 e9500731 Antony Chazapis
        make_option('--set-quota',
47 d50ed8d4 Sofia Papagiannaki
                    dest='quota',
48 d50ed8d4 Sofia Papagiannaki
                    metavar='BYTES',
49 d50ed8d4 Sofia Papagiannaki
                    help="Set user's quota"),
50 d50ed8d4 Sofia Papagiannaki
    )
51 d50ed8d4 Sofia Papagiannaki
52 e9500731 Antony Chazapis
    def handle(self, *args, **options):
53 e9500731 Antony Chazapis
        if len(args) != 1:
54 e9500731 Antony Chazapis
            raise CommandError("Please provide a user")
55 d50ed8d4 Sofia Papagiannaki
56 e9500731 Antony Chazapis
        user = args[0]
57 e9500731 Antony Chazapis
        quota = options.get('quota')
58 e9500731 Antony Chazapis
        if quota is not None:
59 e9500731 Antony Chazapis
            try:
60 e9500731 Antony Chazapis
                quota = int(quota)
61 e9500731 Antony Chazapis
            except ValueError:
62 e9500731 Antony Chazapis
                raise CommandError("Invalid quota")
63 d50ed8d4 Sofia Papagiannaki
64 b1dadd0e Sofia Papagiannaki
        backend = get_backend()
65 6c997921 Sofia Papagiannaki
66 6c997921 Sofia Papagiannaki
        if backend.using_external_quotaholder:
67 6c997921 Sofia Papagiannaki
            raise CommandError("The system uses an extrenal quota holder.")
68 6c997921 Sofia Papagiannaki
69 e9500731 Antony Chazapis
        if quota is not None:
70 e9500731 Antony Chazapis
            backend.update_account_policy(user, user, {'quota': quota})
71 e9500731 Antony Chazapis
        else:
72 d50ed8d4 Sofia Papagiannaki
            self.stdout.write("Quota for %s: %s\n" % (
73 d50ed8d4 Sofia Papagiannaki
                user, backend.get_account_policy(user, user)['quota']))
74 e9500731 Antony Chazapis
        backend.close()