Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / api / quotas.py @ 6609821f

History | View | Annotate | Download (9.8 kB)

1 18f21257 Giorgos Korfiatis
# Copyright 2013 GRNET S.A. All rights reserved.
2 18f21257 Giorgos Korfiatis
#
3 18f21257 Giorgos Korfiatis
# Redistribution and use in source and binary forms, with or
4 18f21257 Giorgos Korfiatis
# without modification, are permitted provided that the following
5 18f21257 Giorgos Korfiatis
# conditions are met:
6 18f21257 Giorgos Korfiatis
#
7 18f21257 Giorgos Korfiatis
#   1. Redistributions of source code must retain the above
8 18f21257 Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
9 18f21257 Giorgos Korfiatis
#      disclaimer.
10 18f21257 Giorgos Korfiatis
#
11 18f21257 Giorgos Korfiatis
#   2. Redistributions in binary form must reproduce the above
12 18f21257 Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
13 18f21257 Giorgos Korfiatis
#      disclaimer in the documentation and/or other materials
14 18f21257 Giorgos Korfiatis
#      provided with the distribution.
15 18f21257 Giorgos Korfiatis
#
16 18f21257 Giorgos Korfiatis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 18f21257 Giorgos Korfiatis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 18f21257 Giorgos Korfiatis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 18f21257 Giorgos Korfiatis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 18f21257 Giorgos Korfiatis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 18f21257 Giorgos Korfiatis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 18f21257 Giorgos Korfiatis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 18f21257 Giorgos Korfiatis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 18f21257 Giorgos Korfiatis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 18f21257 Giorgos Korfiatis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 18f21257 Giorgos Korfiatis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 18f21257 Giorgos Korfiatis
# POSSIBILITY OF SUCH DAMAGE.
28 18f21257 Giorgos Korfiatis
#
29 18f21257 Giorgos Korfiatis
# The views and conclusions contained in the software and
30 18f21257 Giorgos Korfiatis
# documentation are those of the authors and should not be
31 18f21257 Giorgos Korfiatis
# interpreted as representing official policies, either expressed
32 18f21257 Giorgos Korfiatis
# or implied, of GRNET S.A.
33 18f21257 Giorgos Korfiatis
34 18f21257 Giorgos Korfiatis
from django.utils import simplejson as json
35 18f21257 Giorgos Korfiatis
from django.views.decorators.csrf import csrf_exempt
36 18f21257 Giorgos Korfiatis
from django.http import HttpResponse
37 18f21257 Giorgos Korfiatis
38 b830f774 Giorgos Korfiatis
from snf_django.lib.db.transaction import commit_on_success_strict
39 befa5997 Giorgos Korfiatis
40 befa5997 Giorgos Korfiatis
from snf_django.lib import api
41 ad730cc5 Giorgos Korfiatis
from snf_django.lib.api.faults import BadRequest, ItemNotFound
42 befa5997 Giorgos Korfiatis
43 08043775 Giorgos Korfiatis
from astakos.im.resources import get_resources
44 271d2c4c Giorgos Korfiatis
from astakos.im.quotas import get_user_quotas, service_get_quotas
45 18f21257 Giorgos Korfiatis
46 ec5e00aa Giorgos Korfiatis
import astakos.quotaholder_app.exception as qh_exception
47 ec5e00aa Giorgos Korfiatis
import astakos.quotaholder_app.callpoint as qh
48 18f21257 Giorgos Korfiatis
49 79b5d61b Sofia Papagiannaki
from .util import (json_response, is_integer, are_integer,
50 79b5d61b Sofia Papagiannaki
                   user_from_token, service_from_token)
51 18f21257 Giorgos Korfiatis
52 befa5997 Giorgos Korfiatis
@api.api_method(http_method='GET', token_required=True, user_required=False)
53 befa5997 Giorgos Korfiatis
@user_from_token
54 78c3a39c Sofia Papagiannaki
def quotas(request):
55 78c3a39c Sofia Papagiannaki
    result = get_user_quotas(request.user)
56 18f21257 Giorgos Korfiatis
    return json_response(result)
57 18f21257 Giorgos Korfiatis
58 18f21257 Giorgos Korfiatis
59 bb005bc2 Giorgos Korfiatis
@api.api_method(http_method='GET', token_required=True, user_required=False)
60 bb005bc2 Giorgos Korfiatis
@service_from_token
61 bb005bc2 Giorgos Korfiatis
def service_quotas(request):
62 5afce44d Giorgos Korfiatis
    user = request.GET.get('user')
63 5afce44d Giorgos Korfiatis
    users = [user] if user is not None else None
64 271d2c4c Giorgos Korfiatis
    result = service_get_quotas(request.service_instance, users=users)
65 ad730cc5 Giorgos Korfiatis
66 ad730cc5 Giorgos Korfiatis
    if user is not None and result == {}:
67 ad730cc5 Giorgos Korfiatis
        raise ItemNotFound("No such user '%s'" % user)
68 ad730cc5 Giorgos Korfiatis
69 bb005bc2 Giorgos Korfiatis
    return json_response(result)
70 bb005bc2 Giorgos Korfiatis
71 bb005bc2 Giorgos Korfiatis
72 befa5997 Giorgos Korfiatis
@api.api_method(http_method='GET', token_required=False, user_required=False)
73 1028e568 Giorgos Korfiatis
def resources(request):
74 1028e568 Giorgos Korfiatis
    result = get_resources()
75 1028e568 Giorgos Korfiatis
    return json_response(result)
76 1028e568 Giorgos Korfiatis
77 1028e568 Giorgos Korfiatis
78 18f21257 Giorgos Korfiatis
@csrf_exempt
79 18f21257 Giorgos Korfiatis
def commissions(request):
80 18f21257 Giorgos Korfiatis
    method = request.method
81 18f21257 Giorgos Korfiatis
    if method == 'GET':
82 18f21257 Giorgos Korfiatis
        return get_pending_commissions(request)
83 18f21257 Giorgos Korfiatis
    elif method == 'POST':
84 18f21257 Giorgos Korfiatis
        return issue_commission(request)
85 18f21257 Giorgos Korfiatis
    else:
86 18f21257 Giorgos Korfiatis
        raise BadRequest('Method not allowed.')
87 18f21257 Giorgos Korfiatis
88 18f21257 Giorgos Korfiatis
89 befa5997 Giorgos Korfiatis
@api.api_method(http_method='GET', token_required=True, user_required=False)
90 befa5997 Giorgos Korfiatis
@service_from_token
91 18f21257 Giorgos Korfiatis
def get_pending_commissions(request):
92 18f21257 Giorgos Korfiatis
    data = request.GET
93 a4398c8c Giorgos Korfiatis
    client_key = str(request.service_instance)
94 18f21257 Giorgos Korfiatis
95 18f21257 Giorgos Korfiatis
    result = qh.get_pending_commissions(clientkey=client_key)
96 18f21257 Giorgos Korfiatis
    return json_response(result)
97 18f21257 Giorgos Korfiatis
98 18f21257 Giorgos Korfiatis
99 bbf79074 Giorgos Korfiatis
def _provisions_to_list(provisions):
100 bbf79074 Giorgos Korfiatis
    lst = []
101 bbf79074 Giorgos Korfiatis
    for provision in provisions:
102 bbf79074 Giorgos Korfiatis
        try:
103 bbf79074 Giorgos Korfiatis
            holder = provision['holder']
104 bbf79074 Giorgos Korfiatis
            source = provision['source']
105 bbf79074 Giorgos Korfiatis
            resource = provision['resource']
106 bbf79074 Giorgos Korfiatis
            quantity = provision['quantity']
107 bbf79074 Giorgos Korfiatis
            key = (holder, source, resource)
108 bbf79074 Giorgos Korfiatis
            lst.append((key, quantity))
109 ad730cc5 Giorgos Korfiatis
            if not is_integer(quantity):
110 ad730cc5 Giorgos Korfiatis
                raise ValueError()
111 4df49ee7 Giorgos Korfiatis
        except (TypeError, KeyError, ValueError):
112 ad730cc5 Giorgos Korfiatis
            raise BadRequest("Malformed provision %s" % str(provision))
113 bbf79074 Giorgos Korfiatis
    return lst
114 bbf79074 Giorgos Korfiatis
115 bbf79074 Giorgos Korfiatis
116 18f21257 Giorgos Korfiatis
@csrf_exempt
117 befa5997 Giorgos Korfiatis
@api.api_method(http_method='POST', token_required=True, user_required=False)
118 befa5997 Giorgos Korfiatis
@service_from_token
119 18f21257 Giorgos Korfiatis
def issue_commission(request):
120 18f21257 Giorgos Korfiatis
    data = request.raw_post_data
121 4df49ee7 Giorgos Korfiatis
    try:
122 4df49ee7 Giorgos Korfiatis
        input_data = json.loads(data)
123 4df49ee7 Giorgos Korfiatis
    except json.JSONDecodeError:
124 4df49ee7 Giorgos Korfiatis
        raise BadRequest("POST data should be in json format.")
125 18f21257 Giorgos Korfiatis
126 a4398c8c Giorgos Korfiatis
    client_key = str(request.service_instance)
127 ad730cc5 Giorgos Korfiatis
    provisions = input_data.get('provisions')
128 ad730cc5 Giorgos Korfiatis
    if provisions is None:
129 ad730cc5 Giorgos Korfiatis
        raise BadRequest("Provisions are missing.")
130 3a1bed03 Giorgos Korfiatis
    if not isinstance(provisions, list):
131 4df49ee7 Giorgos Korfiatis
        raise BadRequest("Provisions should be a list.")
132 4df49ee7 Giorgos Korfiatis
133 bbf79074 Giorgos Korfiatis
    provisions = _provisions_to_list(provisions)
134 18f21257 Giorgos Korfiatis
    force = input_data.get('force', False)
135 3a1bed03 Giorgos Korfiatis
    if not isinstance(force, bool):
136 3a1bed03 Giorgos Korfiatis
        raise BadRequest('"force" option should be a boolean.')
137 3a1bed03 Giorgos Korfiatis
138 18f21257 Giorgos Korfiatis
    auto_accept = input_data.get('auto_accept', False)
139 3a1bed03 Giorgos Korfiatis
    if not isinstance(auto_accept, bool):
140 3a1bed03 Giorgos Korfiatis
        raise BadRequest('"auto_accept" option should be a boolean.')
141 3a1bed03 Giorgos Korfiatis
142 3a1bed03 Giorgos Korfiatis
    name = input_data.get('name', "")
143 2931c601 Kostas Papadimitriou
    if not isinstance(name, basestring):
144 3a1bed03 Giorgos Korfiatis
        raise BadRequest("Commission name should be a string.")
145 18f21257 Giorgos Korfiatis
146 18f21257 Giorgos Korfiatis
    try:
147 18f21257 Giorgos Korfiatis
        result = _issue_commission(clientkey=client_key,
148 18f21257 Giorgos Korfiatis
                                   provisions=provisions,
149 3a1bed03 Giorgos Korfiatis
                                   name=name,
150 18f21257 Giorgos Korfiatis
                                   force=force,
151 18f21257 Giorgos Korfiatis
                                   accept=auto_accept)
152 18f21257 Giorgos Korfiatis
        data = {"serial": result}
153 18f21257 Giorgos Korfiatis
        status_code = 201
154 18f21257 Giorgos Korfiatis
    except (qh_exception.NoCapacityError,
155 18f21257 Giorgos Korfiatis
            qh_exception.NoQuantityError) as e:
156 18f21257 Giorgos Korfiatis
        status_code = 413
157 18f21257 Giorgos Korfiatis
        body = {"message": e.message,
158 18f21257 Giorgos Korfiatis
                "code": status_code,
159 18f21257 Giorgos Korfiatis
                "data": e.data,
160 18f21257 Giorgos Korfiatis
                }
161 18f21257 Giorgos Korfiatis
        data = {"overLimit": body}
162 18f21257 Giorgos Korfiatis
    except qh_exception.NoHoldingError as e:
163 18f21257 Giorgos Korfiatis
        status_code = 404
164 18f21257 Giorgos Korfiatis
        body = {"message": e.message,
165 18f21257 Giorgos Korfiatis
                "code": status_code,
166 18f21257 Giorgos Korfiatis
                "data": e.data,
167 18f21257 Giorgos Korfiatis
                }
168 18f21257 Giorgos Korfiatis
        data = {"itemNotFound": body}
169 18f21257 Giorgos Korfiatis
    except qh_exception.InvalidDataError as e:
170 18f21257 Giorgos Korfiatis
        status_code = 400
171 18f21257 Giorgos Korfiatis
        body = {"message": e.message,
172 18f21257 Giorgos Korfiatis
                "code": status_code,
173 18f21257 Giorgos Korfiatis
                }
174 18f21257 Giorgos Korfiatis
        data = {"badRequest": body}
175 18f21257 Giorgos Korfiatis
176 18f21257 Giorgos Korfiatis
    return json_response(data, status_code=status_code)
177 18f21257 Giorgos Korfiatis
178 18f21257 Giorgos Korfiatis
179 18f21257 Giorgos Korfiatis
@commit_on_success_strict()
180 3a1bed03 Giorgos Korfiatis
def _issue_commission(clientkey, provisions, name, force, accept):
181 18f21257 Giorgos Korfiatis
    serial = qh.issue_commission(clientkey=clientkey,
182 18f21257 Giorgos Korfiatis
                                 provisions=provisions,
183 3a1bed03 Giorgos Korfiatis
                                 name=name,
184 18f21257 Giorgos Korfiatis
                                 force=force)
185 18f21257 Giorgos Korfiatis
    if accept:
186 3f7b6f6f Giorgos Korfiatis
        done = qh.resolve_pending_commission(clientkey=clientkey,
187 3f7b6f6f Giorgos Korfiatis
                                             serial=serial)
188 18f21257 Giorgos Korfiatis
189 18f21257 Giorgos Korfiatis
    return serial
190 18f21257 Giorgos Korfiatis
191 18f21257 Giorgos Korfiatis
192 6c0f4562 Giorgos Korfiatis
def notFoundCF(serial):
193 6c0f4562 Giorgos Korfiatis
    body = {"code": 404,
194 6c0f4562 Giorgos Korfiatis
            "message": "serial %s does not exist" % serial,
195 6c0f4562 Giorgos Korfiatis
            }
196 6c0f4562 Giorgos Korfiatis
    return {"itemNotFound": body}
197 6c0f4562 Giorgos Korfiatis
198 6c0f4562 Giorgos Korfiatis
199 6c0f4562 Giorgos Korfiatis
def conflictingCF(serial):
200 6c0f4562 Giorgos Korfiatis
    body = {"code": 400,
201 6c0f4562 Giorgos Korfiatis
            "message": "cannot both accept and reject serial %s" % serial,
202 6c0f4562 Giorgos Korfiatis
            }
203 6c0f4562 Giorgos Korfiatis
    return {"badRequest": body}
204 18f21257 Giorgos Korfiatis
205 18f21257 Giorgos Korfiatis
206 18f21257 Giorgos Korfiatis
@csrf_exempt
207 befa5997 Giorgos Korfiatis
@api.api_method(http_method='POST', token_required=True, user_required=False)
208 befa5997 Giorgos Korfiatis
@service_from_token
209 18f21257 Giorgos Korfiatis
@commit_on_success_strict()
210 18f21257 Giorgos Korfiatis
def resolve_pending_commissions(request):
211 18f21257 Giorgos Korfiatis
    data = request.raw_post_data
212 4df49ee7 Giorgos Korfiatis
    try:
213 4df49ee7 Giorgos Korfiatis
        input_data = json.loads(data)
214 4df49ee7 Giorgos Korfiatis
    except json.JSONDecodeError:
215 4df49ee7 Giorgos Korfiatis
        raise BadRequest("POST data should be in json format.")
216 18f21257 Giorgos Korfiatis
217 a4398c8c Giorgos Korfiatis
    client_key = str(request.service_instance)
218 18f21257 Giorgos Korfiatis
    accept = input_data.get('accept', [])
219 18f21257 Giorgos Korfiatis
    reject = input_data.get('reject', [])
220 18f21257 Giorgos Korfiatis
221 4df49ee7 Giorgos Korfiatis
    if not isinstance(accept, list) or not isinstance(reject, list):
222 4df49ee7 Giorgos Korfiatis
        m = '"accept" and "reject" should reference lists of serials.'
223 4df49ee7 Giorgos Korfiatis
        raise BadRequest(m)
224 4df49ee7 Giorgos Korfiatis
225 ad730cc5 Giorgos Korfiatis
    if not are_integer(accept) or not are_integer(reject):
226 ad730cc5 Giorgos Korfiatis
        raise BadRequest("Serials should be integer.")
227 ad730cc5 Giorgos Korfiatis
228 18f21257 Giorgos Korfiatis
    result = qh.resolve_pending_commissions(clientkey=client_key,
229 18f21257 Giorgos Korfiatis
                                            accept_set=accept,
230 18f21257 Giorgos Korfiatis
                                            reject_set=reject)
231 6c0f4562 Giorgos Korfiatis
    accepted, rejected, notFound, conflicting = result
232 6c0f4562 Giorgos Korfiatis
    notFound = [(serial, notFoundCF(serial)) for serial in notFound]
233 6c0f4562 Giorgos Korfiatis
    conflicting = [(serial, conflictingCF(serial)) for serial in conflicting]
234 6c0f4562 Giorgos Korfiatis
    cloudfaults = notFound + conflicting
235 18f21257 Giorgos Korfiatis
    data = {'accepted': accepted,
236 18f21257 Giorgos Korfiatis
            'rejected': rejected,
237 18f21257 Giorgos Korfiatis
            'failed': cloudfaults
238 18f21257 Giorgos Korfiatis
            }
239 18f21257 Giorgos Korfiatis
240 18f21257 Giorgos Korfiatis
    return json_response(data)
241 18f21257 Giorgos Korfiatis
242 18f21257 Giorgos Korfiatis
243 befa5997 Giorgos Korfiatis
@api.api_method(http_method='GET', token_required=True, user_required=False)
244 befa5997 Giorgos Korfiatis
@service_from_token
245 18f21257 Giorgos Korfiatis
def get_commission(request, serial):
246 18f21257 Giorgos Korfiatis
    data = request.GET
247 a4398c8c Giorgos Korfiatis
    client_key = str(request.service_instance)
248 ad730cc5 Giorgos Korfiatis
    try:
249 ad730cc5 Giorgos Korfiatis
        serial = int(serial)
250 ad730cc5 Giorgos Korfiatis
    except ValueError:
251 ad730cc5 Giorgos Korfiatis
        raise BadRequest("Serial should be an integer.")
252 18f21257 Giorgos Korfiatis
253 18f21257 Giorgos Korfiatis
    try:
254 18f21257 Giorgos Korfiatis
        data = qh.get_commission(clientkey=client_key,
255 18f21257 Giorgos Korfiatis
                                 serial=serial)
256 18f21257 Giorgos Korfiatis
        status_code = 200
257 18f21257 Giorgos Korfiatis
        return json_response(data, status_code)
258 18f21257 Giorgos Korfiatis
    except qh_exception.NoCommissionError as e:
259 18f21257 Giorgos Korfiatis
        return HttpResponse(status=404)
260 18f21257 Giorgos Korfiatis
261 18f21257 Giorgos Korfiatis
262 18f21257 Giorgos Korfiatis
@csrf_exempt
263 befa5997 Giorgos Korfiatis
@api.api_method(http_method='POST', token_required=True, user_required=False)
264 befa5997 Giorgos Korfiatis
@service_from_token
265 18f21257 Giorgos Korfiatis
@commit_on_success_strict()
266 18f21257 Giorgos Korfiatis
def serial_action(request, serial):
267 18f21257 Giorgos Korfiatis
    data = request.raw_post_data
268 4df49ee7 Giorgos Korfiatis
    try:
269 4df49ee7 Giorgos Korfiatis
        input_data = json.loads(data)
270 4df49ee7 Giorgos Korfiatis
    except json.JSONDecodeError:
271 4df49ee7 Giorgos Korfiatis
        raise BadRequest("POST data should be in json format.")
272 4df49ee7 Giorgos Korfiatis
273 ad730cc5 Giorgos Korfiatis
    try:
274 ad730cc5 Giorgos Korfiatis
        serial = int(serial)
275 ad730cc5 Giorgos Korfiatis
    except ValueError:
276 ad730cc5 Giorgos Korfiatis
        raise BadRequest("Serial should be an integer.")
277 18f21257 Giorgos Korfiatis
278 a4398c8c Giorgos Korfiatis
    client_key = str(request.service_instance)
279 18f21257 Giorgos Korfiatis
280 18f21257 Giorgos Korfiatis
    accept = 'accept' in input_data
281 18f21257 Giorgos Korfiatis
    reject = 'reject' in input_data
282 18f21257 Giorgos Korfiatis
283 18f21257 Giorgos Korfiatis
    if accept == reject:
284 18f21257 Giorgos Korfiatis
        raise BadRequest('Specify either accept or reject action.')
285 18f21257 Giorgos Korfiatis
286 6c0f4562 Giorgos Korfiatis
    result = qh.resolve_pending_commission(clientkey=client_key,
287 6c0f4562 Giorgos Korfiatis
                                           serial=serial,
288 6c0f4562 Giorgos Korfiatis
                                           accept=accept)
289 18f21257 Giorgos Korfiatis
    response = HttpResponse()
290 18f21257 Giorgos Korfiatis
    if not result:
291 18f21257 Giorgos Korfiatis
        response.status_code = 404
292 18f21257 Giorgos Korfiatis
293 18f21257 Giorgos Korfiatis
    return response