Statistics
| Branch: | Tag: | Revision:

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

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.settings import (BACKEND_QUOTA, BACKEND_VERSIONING)
39 e9500731 Antony Chazapis
40 b1dadd0e Sofia Papagiannaki
from pithos.api.util import get_backend
41 e9500731 Antony Chazapis
42 e9500731 Antony Chazapis
class Command(BaseCommand):
43 e9500731 Antony Chazapis
    args = "<user>"
44 e9500731 Antony Chazapis
    help = "Get/set a user's quota"
45 d50ed8d4 Sofia Papagiannaki
46 e9500731 Antony Chazapis
    option_list = BaseCommand.option_list + (
47 e9500731 Antony Chazapis
        make_option('--set-quota',
48 d50ed8d4 Sofia Papagiannaki
                    dest='quota',
49 d50ed8d4 Sofia Papagiannaki
                    metavar='BYTES',
50 d50ed8d4 Sofia Papagiannaki
                    help="Set user's quota"),
51 d50ed8d4 Sofia Papagiannaki
    )
52 d50ed8d4 Sofia Papagiannaki
53 e9500731 Antony Chazapis
    def handle(self, *args, **options):
54 e9500731 Antony Chazapis
        if len(args) != 1:
55 e9500731 Antony Chazapis
            raise CommandError("Please provide a user")
56 d50ed8d4 Sofia Papagiannaki
57 e9500731 Antony Chazapis
        user = args[0]
58 e9500731 Antony Chazapis
        quota = options.get('quota')
59 e9500731 Antony Chazapis
        if quota is not None:
60 e9500731 Antony Chazapis
            try:
61 e9500731 Antony Chazapis
                quota = int(quota)
62 e9500731 Antony Chazapis
            except ValueError:
63 e9500731 Antony Chazapis
                raise CommandError("Invalid quota")
64 d50ed8d4 Sofia Papagiannaki
65 b1dadd0e Sofia Papagiannaki
        backend = get_backend()
66 e9500731 Antony Chazapis
        backend.default_policy['quota'] = BACKEND_QUOTA
67 e9500731 Antony Chazapis
        backend.default_policy['versioning'] = BACKEND_VERSIONING
68 e9500731 Antony Chazapis
        if quota is not None:
69 e9500731 Antony Chazapis
            backend.update_account_policy(user, user, {'quota': quota})
70 e9500731 Antony Chazapis
        else:
71 d50ed8d4 Sofia Papagiannaki
            self.stdout.write("Quota for %s: %s\n" % (
72 d50ed8d4 Sofia Papagiannaki
                user, backend.get_account_policy(user, user)['quota']))
73 e9500731 Antony Chazapis
        backend.close()