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