root / snf-astakos-app / astakos / im / resources.py @ 3a527b3a
History | View | Annotate | Download (3.1 kB)
1 |
# Copyright 2013 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from astakos.im.models import Resource |
35 |
from astakos.im.quotas import qh_add_resource_limit, qh_sync_new_resource |
36 |
import logging |
37 |
|
38 |
logger = logging.getLogger(__name__) |
39 |
|
40 |
fields = ['name', 'desc', 'unit'] |
41 |
|
42 |
|
43 |
class ResourceException(Exception): |
44 |
pass
|
45 |
|
46 |
|
47 |
def add_resource(service, resource_dict): |
48 |
name = resource_dict.get('name')
|
49 |
if not name: |
50 |
raise ResourceException("Malformed resource dict.") |
51 |
|
52 |
try:
|
53 |
r = Resource.objects.get_for_update(name=name) |
54 |
exists = True
|
55 |
except Resource.DoesNotExist:
|
56 |
r = Resource(uplimit=0)
|
57 |
exists = False
|
58 |
|
59 |
r.service = service |
60 |
for field in fields: |
61 |
value = resource_dict.get(field) |
62 |
if value is not None: |
63 |
setattr(r, field, value)
|
64 |
|
65 |
r.save() |
66 |
if not exists: |
67 |
qh_sync_new_resource(r, 0)
|
68 |
|
69 |
if exists:
|
70 |
logger.info("Updated resource %s." % (name))
|
71 |
else:
|
72 |
logger.info("Added resource %s." % (name))
|
73 |
return exists
|
74 |
|
75 |
|
76 |
def update_resource(resource, uplimit): |
77 |
old_uplimit = resource.uplimit |
78 |
resource.uplimit = uplimit |
79 |
resource.save() |
80 |
|
81 |
logger.info("Updated resource %s with limit %s."
|
82 |
% (resource.name, uplimit)) |
83 |
diff = uplimit - old_uplimit |
84 |
if diff != 0: |
85 |
qh_add_resource_limit(resource, diff) |
86 |
|
87 |
|
88 |
def get_resources(resources=None, services=None): |
89 |
if resources is None: |
90 |
rs = Resource.objects.all() |
91 |
else:
|
92 |
rs = Resource.objects.filter(name__in=resources) |
93 |
|
94 |
if services is not None: |
95 |
rs = rs.filter(service__in=services) |
96 |
|
97 |
resource_dict = {} |
98 |
for r in rs: |
99 |
resource_dict[r.full_name()] = r.get_info() |
100 |
|
101 |
return resource_dict
|