root / snf-astakos-app / astakos / im / endpoints / quotaholder.py @ 670de92a
History | View | Annotate | Download (5.2 kB)
1 |
# Copyright 2011-2012 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 |
import socket |
35 |
import logging |
36 |
import itertools |
37 |
|
38 |
from functools import wraps |
39 |
from itertools import tee |
40 |
|
41 |
from django.utils.translation import ugettext as _ |
42 |
|
43 |
from astakos.im.settings import QUOTA_HOLDER_URL, LOGGING_LEVEL |
44 |
|
45 |
if QUOTA_HOLDER_URL:
|
46 |
from commissioning.clients.quotaholder import QuotaholderHTTP |
47 |
|
48 |
ENTITY_KEY = '1'
|
49 |
|
50 |
logger = logging.getLogger(__name__) |
51 |
|
52 |
|
53 |
def call(func_name): |
54 |
"""Decorator function for QuotaholderHTTP client calls."""
|
55 |
def decorator(payload_func): |
56 |
@wraps(payload_func)
|
57 |
def wrapper(entities=(), client=None, **kwargs): |
58 |
if not entities: |
59 |
return ()
|
60 |
|
61 |
if not QUOTA_HOLDER_URL: |
62 |
return ()
|
63 |
|
64 |
c = client or QuotaholderHTTP(QUOTA_HOLDER_URL)
|
65 |
func = c.__dict__.get(func_name) |
66 |
if not func: |
67 |
return c,
|
68 |
|
69 |
data = payload_func(entities, client, **kwargs) |
70 |
if not data: |
71 |
return c,
|
72 |
|
73 |
funcname = func.__name__ |
74 |
kwargs = {'context': {}, funcname: data}
|
75 |
rejected = func(**kwargs) |
76 |
msg = _('%s: %s - Rejected: %s' % (funcname, data, rejected,))
|
77 |
logger.log(LOGGING_LEVEL, msg) |
78 |
return c, rejected
|
79 |
return wrapper
|
80 |
return decorator
|
81 |
|
82 |
@call('set_quota') |
83 |
def send_quota(users, client=None): |
84 |
data = [] |
85 |
append = data.append |
86 |
for user in users: |
87 |
for resource, uplimit in user.quota.iteritems(): |
88 |
key = ENTITY_KEY |
89 |
quantity = None
|
90 |
capacity = uplimit |
91 |
import_limit = None
|
92 |
export_limit = None
|
93 |
flags = 0
|
94 |
args = (user.email, resource, key, quantity, capacity, import_limit, |
95 |
export_limit, flags) |
96 |
append(args) |
97 |
return data
|
98 |
|
99 |
|
100 |
@call('set_quota') |
101 |
def send_resource_quantities(resources, client=None): |
102 |
data = [] |
103 |
append = data.append |
104 |
for resource in resources: |
105 |
key = ENTITY_KEY |
106 |
quantity = resource.meta.filter(key='quantity') or None |
107 |
capacity = None
|
108 |
import_limit = None
|
109 |
export_limit = None
|
110 |
flags = 0
|
111 |
args = (resource.service, resource, key, quantity, capacity, |
112 |
import_limit, export_limit, flags) |
113 |
append(args) |
114 |
return data
|
115 |
|
116 |
|
117 |
@call('get_quota') |
118 |
def get_quota(users, client=None): |
119 |
data = [] |
120 |
append = data.append |
121 |
for user in users: |
122 |
try:
|
123 |
entity = user.email |
124 |
except AttributeError: |
125 |
continue
|
126 |
else:
|
127 |
for r in user.quota.keys(): |
128 |
args = entity, r, ENTITY_KEY |
129 |
append(args) |
130 |
return data
|
131 |
|
132 |
|
133 |
@call('create_entity') |
134 |
def create_entities(entities, client=None, field=''): |
135 |
data = [] |
136 |
append = data.append |
137 |
for entity in entities: |
138 |
try:
|
139 |
entity = entity.__getattribute__(field) |
140 |
except AttributeError: |
141 |
continue
|
142 |
owner = 'system'
|
143 |
key = ENTITY_KEY |
144 |
ownerkey = ''
|
145 |
args = entity, owner, key, ownerkey |
146 |
append(args) |
147 |
return data
|
148 |
|
149 |
|
150 |
def register_users(users, client=None): |
151 |
users, copy = itertools.tee(users) |
152 |
client, rejected = create_entities(entities=users, |
153 |
client=client, |
154 |
field='email')
|
155 |
created = (e for e in copy if unicode(e) not in rejected) |
156 |
return send_quota(created, client)
|
157 |
|
158 |
|
159 |
def register_resources(resources, client=None): |
160 |
resources, copy = itertools.tee(resources) |
161 |
client, rejected = create_entities(entities=resources, |
162 |
client=client, |
163 |
field='service')
|
164 |
created = (e for e in copy if unicode(e) not in rejected) |
165 |
return send_resource_quantities(created, client)
|