root / snf-astakos-app / astakos / api / quotas.py @ 08043775
History | View | Annotate | Download (7.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 |
from astakos.api.util import json_response |
40 |
|
41 |
from snf_django.lib import api |
42 |
from snf_django.lib.api.faults import BadRequest, InternalServerError |
43 |
|
44 |
from astakos.im.api import api_method as generic_api_method |
45 |
from astakos.im.api.user import user_from_token |
46 |
from astakos.im.api.service import service_from_token |
47 |
|
48 |
from astakos.im.resources import get_resources |
49 |
from astakos.im.quotas import get_user_quotas, get_service_quotas |
50 |
|
51 |
import astakos.quotaholder.exception as qh_exception |
52 |
from astakos.quotaholder.callpoint import QuotaholderDjangoDBCallpoint |
53 |
qh = QuotaholderDjangoDBCallpoint() |
54 |
|
55 |
|
56 |
@api.api_method(http_method='GET', token_required=True, user_required=False) |
57 |
@user_from_token
|
58 |
def quotas(request, user=None): |
59 |
result = get_user_quotas(user) |
60 |
return json_response(result)
|
61 |
|
62 |
|
63 |
@api.api_method(http_method='GET', token_required=True, user_required=False) |
64 |
@service_from_token
|
65 |
def service_quotas(request): |
66 |
user = request.GET.get('user')
|
67 |
users = [user] if user is not None else None |
68 |
result = get_service_quotas(request.service_instance, users=users) |
69 |
return json_response(result)
|
70 |
|
71 |
|
72 |
@api.api_method(http_method='GET', token_required=False, user_required=False) |
73 |
def resources(request): |
74 |
result = get_resources() |
75 |
return json_response(result)
|
76 |
|
77 |
|
78 |
@csrf_exempt
|
79 |
def commissions(request): |
80 |
method = request.method |
81 |
if method == 'GET': |
82 |
return get_pending_commissions(request)
|
83 |
elif method == 'POST': |
84 |
return issue_commission(request)
|
85 |
else:
|
86 |
raise BadRequest('Method not allowed.') |
87 |
|
88 |
|
89 |
@api.api_method(http_method='GET', token_required=True, user_required=False) |
90 |
@service_from_token
|
91 |
def get_pending_commissions(request): |
92 |
data = request.GET |
93 |
client_key = str(request.service_instance)
|
94 |
|
95 |
result = qh.get_pending_commissions(clientkey=client_key) |
96 |
return json_response(result)
|
97 |
|
98 |
|
99 |
@csrf_exempt
|
100 |
@api.api_method(http_method='POST', token_required=True, user_required=False) |
101 |
@service_from_token
|
102 |
def issue_commission(request): |
103 |
data = request.raw_post_data |
104 |
input_data = json.loads(data) |
105 |
|
106 |
client_key = str(request.service_instance)
|
107 |
provisions = input_data['provisions']
|
108 |
force = input_data.get('force', False) |
109 |
auto_accept = input_data.get('auto_accept', False) |
110 |
|
111 |
try:
|
112 |
result = _issue_commission(clientkey=client_key, |
113 |
provisions=provisions, |
114 |
force=force, |
115 |
accept=auto_accept) |
116 |
data = {"serial": result}
|
117 |
status_code = 201
|
118 |
except (qh_exception.NoCapacityError,
|
119 |
qh_exception.NoQuantityError) as e:
|
120 |
status_code = 413
|
121 |
body = {"message": e.message,
|
122 |
"code": status_code,
|
123 |
"data": e.data,
|
124 |
} |
125 |
data = {"overLimit": body}
|
126 |
except qh_exception.NoHoldingError as e: |
127 |
status_code = 404
|
128 |
body = {"message": e.message,
|
129 |
"code": status_code,
|
130 |
"data": e.data,
|
131 |
} |
132 |
data = {"itemNotFound": body}
|
133 |
except qh_exception.InvalidDataError as e: |
134 |
status_code = 400
|
135 |
body = {"message": e.message,
|
136 |
"code": status_code,
|
137 |
} |
138 |
data = {"badRequest": body}
|
139 |
|
140 |
return json_response(data, status_code=status_code)
|
141 |
|
142 |
|
143 |
@commit_on_success_strict()
|
144 |
def _issue_commission(clientkey, provisions, force, accept): |
145 |
serial = qh.issue_commission(clientkey=clientkey, |
146 |
provisions=provisions, |
147 |
force=force) |
148 |
if accept:
|
149 |
done = qh.accept_commission(clientkey=clientkey, |
150 |
serial=serial) |
151 |
|
152 |
return serial
|
153 |
|
154 |
|
155 |
def notFoundCF(serial): |
156 |
body = {"code": 404, |
157 |
"message": "serial %s does not exist" % serial, |
158 |
} |
159 |
return {"itemNotFound": body} |
160 |
|
161 |
|
162 |
def conflictingCF(serial): |
163 |
body = {"code": 400, |
164 |
"message": "cannot both accept and reject serial %s" % serial, |
165 |
} |
166 |
return {"badRequest": body} |
167 |
|
168 |
|
169 |
@csrf_exempt
|
170 |
@api.api_method(http_method='POST', token_required=True, user_required=False) |
171 |
@service_from_token
|
172 |
@commit_on_success_strict()
|
173 |
def resolve_pending_commissions(request): |
174 |
data = request.raw_post_data |
175 |
input_data = json.loads(data) |
176 |
|
177 |
client_key = str(request.service_instance)
|
178 |
accept = input_data.get('accept', [])
|
179 |
reject = input_data.get('reject', [])
|
180 |
|
181 |
result = qh.resolve_pending_commissions(clientkey=client_key, |
182 |
accept_set=accept, |
183 |
reject_set=reject) |
184 |
accepted, rejected, notFound, conflicting = result |
185 |
notFound = [(serial, notFoundCF(serial)) for serial in notFound] |
186 |
conflicting = [(serial, conflictingCF(serial)) for serial in conflicting] |
187 |
cloudfaults = notFound + conflicting |
188 |
data = {'accepted': accepted,
|
189 |
'rejected': rejected,
|
190 |
'failed': cloudfaults
|
191 |
} |
192 |
|
193 |
return json_response(data)
|
194 |
|
195 |
|
196 |
@api.api_method(http_method='GET', token_required=True, user_required=False) |
197 |
@service_from_token
|
198 |
def get_commission(request, serial): |
199 |
data = request.GET |
200 |
client_key = str(request.service_instance)
|
201 |
serial = int(serial)
|
202 |
|
203 |
try:
|
204 |
data = qh.get_commission(clientkey=client_key, |
205 |
serial=serial) |
206 |
status_code = 200
|
207 |
return json_response(data, status_code)
|
208 |
except qh_exception.NoCommissionError as e: |
209 |
return HttpResponse(status=404) |
210 |
|
211 |
|
212 |
@csrf_exempt
|
213 |
@api.api_method(http_method='POST', token_required=True, user_required=False) |
214 |
@service_from_token
|
215 |
@commit_on_success_strict()
|
216 |
def serial_action(request, serial): |
217 |
data = request.raw_post_data |
218 |
input_data = json.loads(data) |
219 |
serial = int(serial)
|
220 |
|
221 |
client_key = str(request.service_instance)
|
222 |
|
223 |
accept = 'accept' in input_data |
224 |
reject = 'reject' in input_data |
225 |
|
226 |
if accept == reject:
|
227 |
raise BadRequest('Specify either accept or reject action.') |
228 |
|
229 |
result = qh.resolve_pending_commission(clientkey=client_key, |
230 |
serial=serial, |
231 |
accept=accept) |
232 |
response = HttpResponse() |
233 |
if not result: |
234 |
response.status_code = 404
|
235 |
|
236 |
return response
|