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