root / snf-astakos-app / astakos / api / quotas.py @ 8fb8d0cf
History | View | Annotate | Download (9.8 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 django.utils import simplejson as json |
35 |
from django.views.decorators.csrf import csrf_exempt |
36 |
from django.http import HttpResponse |
37 |
|
38 |
from snf_django.lib.db.transaction import commit_on_success_strict |
39 |
|
40 |
from snf_django.lib import api |
41 |
from snf_django.lib.api.faults import BadRequest, ItemNotFound |
42 |
|
43 |
from astakos.im.register import get_resources |
44 |
from astakos.im.quotas import get_user_quotas, service_get_quotas |
45 |
|
46 |
import astakos.quotaholder_app.exception as qh_exception |
47 |
import astakos.quotaholder_app.callpoint as qh |
48 |
|
49 |
from .util import (json_response, is_integer, are_integer, |
50 |
user_from_token, component_from_token) |
51 |
|
52 |
|
53 |
@api.api_method(http_method='GET', token_required=True, user_required=False) |
54 |
@user_from_token
|
55 |
def quotas(request): |
56 |
result = get_user_quotas(request.user) |
57 |
return json_response(result)
|
58 |
|
59 |
|
60 |
@api.api_method(http_method='GET', token_required=True, user_required=False) |
61 |
@component_from_token
|
62 |
def service_quotas(request): |
63 |
user = request.GET.get('user')
|
64 |
users = [user] if user is not None else None |
65 |
result = service_get_quotas(request.component_instance, users=users) |
66 |
|
67 |
if user is not None and result == {}: |
68 |
raise ItemNotFound("No such user '%s'" % user) |
69 |
|
70 |
return json_response(result)
|
71 |
|
72 |
|
73 |
@api.api_method(http_method='GET', token_required=False, user_required=False) |
74 |
def resources(request): |
75 |
result = get_resources() |
76 |
return json_response(result)
|
77 |
|
78 |
|
79 |
@csrf_exempt
|
80 |
def commissions(request): |
81 |
method = request.method |
82 |
if method == 'GET': |
83 |
return get_pending_commissions(request)
|
84 |
elif method == 'POST': |
85 |
return issue_commission(request)
|
86 |
else:
|
87 |
raise BadRequest('Method not allowed.') |
88 |
|
89 |
|
90 |
@api.api_method(http_method='GET', token_required=True, user_required=False) |
91 |
@component_from_token
|
92 |
def get_pending_commissions(request): |
93 |
data = request.GET |
94 |
client_key = str(request.component_instance)
|
95 |
|
96 |
result = qh.get_pending_commissions(clientkey=client_key) |
97 |
return json_response(result)
|
98 |
|
99 |
|
100 |
def _provisions_to_list(provisions): |
101 |
lst = [] |
102 |
for provision in provisions: |
103 |
try:
|
104 |
holder = provision['holder']
|
105 |
source = provision['source']
|
106 |
resource = provision['resource']
|
107 |
quantity = provision['quantity']
|
108 |
key = (holder, source, resource) |
109 |
lst.append((key, quantity)) |
110 |
if not is_integer(quantity): |
111 |
raise ValueError() |
112 |
except (TypeError, KeyError, ValueError): |
113 |
raise BadRequest("Malformed provision %s" % str(provision)) |
114 |
return lst
|
115 |
|
116 |
|
117 |
@csrf_exempt
|
118 |
@api.api_method(http_method='POST', token_required=True, user_required=False) |
119 |
@component_from_token
|
120 |
def issue_commission(request): |
121 |
data = request.raw_post_data |
122 |
try:
|
123 |
input_data = json.loads(data) |
124 |
except json.JSONDecodeError:
|
125 |
raise BadRequest("POST data should be in json format.") |
126 |
|
127 |
client_key = str(request.component_instance)
|
128 |
provisions = input_data.get('provisions')
|
129 |
if provisions is None: |
130 |
raise BadRequest("Provisions are missing.") |
131 |
if not isinstance(provisions, list): |
132 |
raise BadRequest("Provisions should be a list.") |
133 |
|
134 |
provisions = _provisions_to_list(provisions) |
135 |
force = input_data.get('force', False) |
136 |
if not isinstance(force, bool): |
137 |
raise BadRequest('"force" option should be a boolean.') |
138 |
|
139 |
auto_accept = input_data.get('auto_accept', False) |
140 |
if not isinstance(auto_accept, bool): |
141 |
raise BadRequest('"auto_accept" option should be a boolean.') |
142 |
|
143 |
name = input_data.get('name', "") |
144 |
if not isinstance(name, basestring): |
145 |
raise BadRequest("Commission name should be a string.") |
146 |
|
147 |
try:
|
148 |
result = _issue_commission(clientkey=client_key, |
149 |
provisions=provisions, |
150 |
name=name, |
151 |
force=force, |
152 |
accept=auto_accept) |
153 |
data = {"serial": result}
|
154 |
status_code = 201
|
155 |
except (qh_exception.NoCapacityError,
|
156 |
qh_exception.NoQuantityError) as e:
|
157 |
status_code = 413
|
158 |
body = {"message": e.message,
|
159 |
"code": status_code,
|
160 |
"data": e.data,
|
161 |
} |
162 |
data = {"overLimit": body}
|
163 |
except qh_exception.NoHoldingError as e: |
164 |
status_code = 404
|
165 |
body = {"message": e.message,
|
166 |
"code": status_code,
|
167 |
"data": e.data,
|
168 |
} |
169 |
data = {"itemNotFound": body}
|
170 |
except qh_exception.InvalidDataError as e: |
171 |
status_code = 400
|
172 |
body = {"message": e.message,
|
173 |
"code": status_code,
|
174 |
} |
175 |
data = {"badRequest": body}
|
176 |
|
177 |
return json_response(data, status_code=status_code)
|
178 |
|
179 |
|
180 |
@commit_on_success_strict()
|
181 |
def _issue_commission(clientkey, provisions, name, force, accept): |
182 |
serial = qh.issue_commission(clientkey=clientkey, |
183 |
provisions=provisions, |
184 |
name=name, |
185 |
force=force) |
186 |
if accept:
|
187 |
done = qh.resolve_pending_commission(clientkey=clientkey, |
188 |
serial=serial) |
189 |
|
190 |
return serial
|
191 |
|
192 |
|
193 |
def notFoundCF(serial): |
194 |
body = {"code": 404, |
195 |
"message": "serial %s does not exist" % serial, |
196 |
} |
197 |
return {"itemNotFound": body} |
198 |
|
199 |
|
200 |
def conflictingCF(serial): |
201 |
body = {"code": 400, |
202 |
"message": "cannot both accept and reject serial %s" % serial, |
203 |
} |
204 |
return {"badRequest": body} |
205 |
|
206 |
|
207 |
@csrf_exempt
|
208 |
@api.api_method(http_method='POST', token_required=True, user_required=False) |
209 |
@component_from_token
|
210 |
@commit_on_success_strict()
|
211 |
def resolve_pending_commissions(request): |
212 |
data = request.raw_post_data |
213 |
try:
|
214 |
input_data = json.loads(data) |
215 |
except json.JSONDecodeError:
|
216 |
raise BadRequest("POST data should be in json format.") |
217 |
|
218 |
client_key = str(request.component_instance)
|
219 |
accept = input_data.get('accept', [])
|
220 |
reject = input_data.get('reject', [])
|
221 |
|
222 |
if not isinstance(accept, list) or not isinstance(reject, list): |
223 |
m = '"accept" and "reject" should reference lists of serials.'
|
224 |
raise BadRequest(m)
|
225 |
|
226 |
if not are_integer(accept) or not are_integer(reject): |
227 |
raise BadRequest("Serials should be integer.") |
228 |
|
229 |
result = qh.resolve_pending_commissions(clientkey=client_key, |
230 |
accept_set=accept, |
231 |
reject_set=reject) |
232 |
accepted, rejected, notFound, conflicting = result |
233 |
notFound = [(serial, notFoundCF(serial)) for serial in notFound] |
234 |
conflicting = [(serial, conflictingCF(serial)) for serial in conflicting] |
235 |
cloudfaults = notFound + conflicting |
236 |
data = {'accepted': accepted,
|
237 |
'rejected': rejected,
|
238 |
'failed': cloudfaults
|
239 |
} |
240 |
|
241 |
return json_response(data)
|
242 |
|
243 |
|
244 |
@api.api_method(http_method='GET', token_required=True, user_required=False) |
245 |
@component_from_token
|
246 |
def get_commission(request, serial): |
247 |
data = request.GET |
248 |
client_key = str(request.component_instance)
|
249 |
try:
|
250 |
serial = int(serial)
|
251 |
except ValueError: |
252 |
raise BadRequest("Serial should be an integer.") |
253 |
|
254 |
try:
|
255 |
data = qh.get_commission(clientkey=client_key, |
256 |
serial=serial) |
257 |
status_code = 200
|
258 |
return json_response(data, status_code)
|
259 |
except qh_exception.NoCommissionError as e: |
260 |
return HttpResponse(status=404) |
261 |
|
262 |
|
263 |
@csrf_exempt
|
264 |
@api.api_method(http_method='POST', token_required=True, user_required=False) |
265 |
@component_from_token
|
266 |
@commit_on_success_strict()
|
267 |
def serial_action(request, serial): |
268 |
data = request.raw_post_data |
269 |
try:
|
270 |
input_data = json.loads(data) |
271 |
except json.JSONDecodeError:
|
272 |
raise BadRequest("POST data should be in json format.") |
273 |
|
274 |
try:
|
275 |
serial = int(serial)
|
276 |
except ValueError: |
277 |
raise BadRequest("Serial should be an integer.") |
278 |
|
279 |
client_key = str(request.component_instance)
|
280 |
|
281 |
accept = 'accept' in input_data |
282 |
reject = 'reject' in input_data |
283 |
|
284 |
if accept == reject:
|
285 |
raise BadRequest('Specify either accept or reject action.') |
286 |
|
287 |
result = qh.resolve_pending_commission(clientkey=client_key, |
288 |
serial=serial, |
289 |
accept=accept) |
290 |
response = HttpResponse() |
291 |
if not result: |
292 |
response.status_code = 404
|
293 |
|
294 |
return response
|