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