Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / api / quotas.py @ 948e15bc

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.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
import astakos.quotaholder.callpoint as qh
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=True, user_required=False)
63
@service_from_token
64
def service_quotas(request):
65
    user = request.GET.get('user')
66
    users = [user] if user is not None else None
67
    result = get_service_quotas(request.service_instance, users=users)
68
    return json_response(result)
69

    
70

    
71
@api.api_method(http_method='GET', token_required=False, user_required=False)
72
def resources(request):
73
    result = get_resources()
74
    return json_response(result)
75

    
76

    
77
@csrf_exempt
78
def commissions(request):
79
    method = request.method
80
    if method == 'GET':
81
        return get_pending_commissions(request)
82
    elif method == 'POST':
83
        return issue_commission(request)
84
    else:
85
        raise BadRequest('Method not allowed.')
86

    
87

    
88
@api.api_method(http_method='GET', token_required=True, user_required=False)
89
@service_from_token
90
def get_pending_commissions(request):
91
    data = request.GET
92
    client_key = str(request.service_instance)
93

    
94
    result = qh.get_pending_commissions(clientkey=client_key)
95
    return json_response(result)
96

    
97

    
98
@csrf_exempt
99
@api.api_method(http_method='POST', token_required=True, user_required=False)
100
@service_from_token
101
def issue_commission(request):
102
    data = request.raw_post_data
103
    input_data = json.loads(data)
104

    
105
    client_key = str(request.service_instance)
106
    provisions = input_data['provisions']
107
    force = input_data.get('force', False)
108
    auto_accept = input_data.get('auto_accept', False)
109

    
110
    try:
111
        result = _issue_commission(clientkey=client_key,
112
                                   provisions=provisions,
113
                                   force=force,
114
                                   accept=auto_accept)
115
        data = {"serial": result}
116
        status_code = 201
117
    except (qh_exception.NoCapacityError,
118
            qh_exception.NoQuantityError) as e:
119
        status_code = 413
120
        body = {"message": e.message,
121
                "code": status_code,
122
                "data": e.data,
123
                }
124
        data = {"overLimit": body}
125
    except qh_exception.NoHoldingError as e:
126
        status_code = 404
127
        body = {"message": e.message,
128
                "code": status_code,
129
                "data": e.data,
130
                }
131
        data = {"itemNotFound": body}
132
    except qh_exception.InvalidDataError as e:
133
        status_code = 400
134
        body = {"message": e.message,
135
                "code": status_code,
136
                }
137
        data = {"badRequest": body}
138

    
139
    return json_response(data, status_code=status_code)
140

    
141

    
142
@commit_on_success_strict()
143
def _issue_commission(clientkey, provisions, force, accept):
144
    serial = qh.issue_commission(clientkey=clientkey,
145
                                 provisions=provisions,
146
                                 force=force)
147
    if accept:
148
        done = qh.accept_commission(clientkey=clientkey,
149
                                    serial=serial)
150

    
151
    return serial
152

    
153

    
154
def notFoundCF(serial):
155
    body = {"code": 404,
156
            "message": "serial %s does not exist" % serial,
157
            }
158
    return {"itemNotFound": body}
159

    
160

    
161
def conflictingCF(serial):
162
    body = {"code": 400,
163
            "message": "cannot both accept and reject serial %s" % serial,
164
            }
165
    return {"badRequest": body}
166

    
167

    
168
@csrf_exempt
169
@api.api_method(http_method='POST', token_required=True, user_required=False)
170
@service_from_token
171
@commit_on_success_strict()
172
def resolve_pending_commissions(request):
173
    data = request.raw_post_data
174
    input_data = json.loads(data)
175

    
176
    client_key = str(request.service_instance)
177
    accept = input_data.get('accept', [])
178
    reject = input_data.get('reject', [])
179

    
180
    result = qh.resolve_pending_commissions(clientkey=client_key,
181
                                            accept_set=accept,
182
                                            reject_set=reject)
183
    accepted, rejected, notFound, conflicting = result
184
    notFound = [(serial, notFoundCF(serial)) for serial in notFound]
185
    conflicting = [(serial, conflictingCF(serial)) for serial in conflicting]
186
    cloudfaults = notFound + conflicting
187
    data = {'accepted': accepted,
188
            'rejected': rejected,
189
            'failed': cloudfaults
190
            }
191

    
192
    return json_response(data)
193

    
194

    
195
@api.api_method(http_method='GET', token_required=True, user_required=False)
196
@service_from_token
197
def get_commission(request, serial):
198
    data = request.GET
199
    client_key = str(request.service_instance)
200
    serial = int(serial)
201

    
202
    try:
203
        data = qh.get_commission(clientkey=client_key,
204
                                 serial=serial)
205
        status_code = 200
206
        return json_response(data, status_code)
207
    except qh_exception.NoCommissionError as e:
208
        return HttpResponse(status=404)
209

    
210

    
211
@csrf_exempt
212
@api.api_method(http_method='POST', token_required=True, user_required=False)
213
@service_from_token
214
@commit_on_success_strict()
215
def serial_action(request, serial):
216
    data = request.raw_post_data
217
    input_data = json.loads(data)
218
    serial = int(serial)
219

    
220
    client_key = str(request.service_instance)
221

    
222
    accept = 'accept' in input_data
223
    reject = 'reject' in input_data
224

    
225
    if accept == reject:
226
        raise BadRequest('Specify either accept or reject action.')
227

    
228
    result = qh.resolve_pending_commission(clientkey=client_key,
229
                                           serial=serial,
230
                                           accept=accept)
231
    response = HttpResponse()
232
    if not result:
233
        response.status_code = 404
234

    
235
    return response