Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / resources.py @ 75380308

History | View | Annotate | Download (2.6 kB)

1 b1ea24f3 Giorgos Korfiatis
# Copyright 2013 GRNET S.A. All rights reserved.
2 b1ea24f3 Giorgos Korfiatis
#
3 b1ea24f3 Giorgos Korfiatis
# Redistribution and use in source and binary forms, with or
4 b1ea24f3 Giorgos Korfiatis
# without modification, are permitted provided that the following
5 b1ea24f3 Giorgos Korfiatis
# conditions are met:
6 b1ea24f3 Giorgos Korfiatis
#
7 b1ea24f3 Giorgos Korfiatis
#   1. Redistributions of source code must retain the above
8 b1ea24f3 Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
9 b1ea24f3 Giorgos Korfiatis
#      disclaimer.
10 b1ea24f3 Giorgos Korfiatis
#
11 b1ea24f3 Giorgos Korfiatis
#   2. Redistributions in binary form must reproduce the above
12 b1ea24f3 Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
13 b1ea24f3 Giorgos Korfiatis
#      disclaimer in the documentation and/or other materials
14 b1ea24f3 Giorgos Korfiatis
#      provided with the distribution.
15 b1ea24f3 Giorgos Korfiatis
#
16 b1ea24f3 Giorgos Korfiatis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 b1ea24f3 Giorgos Korfiatis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 b1ea24f3 Giorgos Korfiatis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 b1ea24f3 Giorgos Korfiatis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 b1ea24f3 Giorgos Korfiatis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 b1ea24f3 Giorgos Korfiatis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 b1ea24f3 Giorgos Korfiatis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 b1ea24f3 Giorgos Korfiatis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 b1ea24f3 Giorgos Korfiatis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 b1ea24f3 Giorgos Korfiatis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 b1ea24f3 Giorgos Korfiatis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 b1ea24f3 Giorgos Korfiatis
# POSSIBILITY OF SUCH DAMAGE.
28 b1ea24f3 Giorgos Korfiatis
#
29 b1ea24f3 Giorgos Korfiatis
# The views and conclusions contained in the software and
30 b1ea24f3 Giorgos Korfiatis
# documentation are those of the authors and should not be
31 b1ea24f3 Giorgos Korfiatis
# interpreted as representing official policies, either expressed
32 b1ea24f3 Giorgos Korfiatis
# or implied, of GRNET S.A.
33 b1ea24f3 Giorgos Korfiatis
34 b1ea24f3 Giorgos Korfiatis
from astakos.im.models import Service, Resource
35 b1ea24f3 Giorgos Korfiatis
from astakos.im.functions import qh_sync_all_users
36 9747707e Giorgos Korfiatis
from astakos.im.quotas import qh_add_resource_limit, qh_sync_new_resource
37 9747707e Giorgos Korfiatis
import logging
38 b1ea24f3 Giorgos Korfiatis
39 9747707e Giorgos Korfiatis
logger = logging.getLogger(__name__)
40 b1ea24f3 Giorgos Korfiatis
41 9747707e Giorgos Korfiatis
42 9747707e Giorgos Korfiatis
def add_resource(service, resource, uplimit):
43 b1ea24f3 Giorgos Korfiatis
    try:
44 b1ea24f3 Giorgos Korfiatis
        s = Service.objects.get(name=service)
45 b1ea24f3 Giorgos Korfiatis
    except Service.DoesNotExist:
46 b1ea24f3 Giorgos Korfiatis
        raise Exception("Service %s is not registered." % (service))
47 b1ea24f3 Giorgos Korfiatis
48 9747707e Giorgos Korfiatis
    name = resource['name']
49 9747707e Giorgos Korfiatis
    try:
50 9747707e Giorgos Korfiatis
        r = Resource.objects.get_for_update(name=name)
51 9747707e Giorgos Korfiatis
        old_uplimit = r.uplimit
52 9747707e Giorgos Korfiatis
    except Resource.DoesNotExist:
53 9747707e Giorgos Korfiatis
        r = Resource()
54 9747707e Giorgos Korfiatis
        old_uplimit = None
55 b1ea24f3 Giorgos Korfiatis
56 9747707e Giorgos Korfiatis
    r.uplimit = uplimit
57 75380308 Kostas Papadimitriou
    r.service = service
58 9747707e Giorgos Korfiatis
    for key, value in resource.iteritems():
59 9747707e Giorgos Korfiatis
        setattr(r, key, value)
60 b1ea24f3 Giorgos Korfiatis
61 9747707e Giorgos Korfiatis
    r.save()
62 b1ea24f3 Giorgos Korfiatis
63 9747707e Giorgos Korfiatis
    if old_uplimit is not None:
64 9747707e Giorgos Korfiatis
        logger.info("Updated resource %s with limit %s." % (name, uplimit))
65 9747707e Giorgos Korfiatis
    else:
66 9747707e Giorgos Korfiatis
        logger.info("Added resource %s with limit %s." % (name, uplimit))
67 b1ea24f3 Giorgos Korfiatis
68 9747707e Giorgos Korfiatis
    if old_uplimit is not None:
69 9747707e Giorgos Korfiatis
        diff = uplimit - old_uplimit
70 9747707e Giorgos Korfiatis
        if diff != 0:
71 9747707e Giorgos Korfiatis
            qh_add_resource_limit(name, diff)
72 9747707e Giorgos Korfiatis
    else:
73 9747707e Giorgos Korfiatis
        qh_sync_new_resource(name, uplimit)