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