root / kamaki / clients / quotaholder / api / quotaholder.py @ 6764f588
History | View | Annotate | Download (10.3 kB)
1 |
# -*- coding: utf-8 -*-
|
---|---|
2 |
#
|
3 |
# Copyright 2012 GRNET S.A. All rights reserved.
|
4 |
#
|
5 |
# Redistribution and use in source and binary forms, with or
|
6 |
# without modification, are permitted provided that the following
|
7 |
# conditions are met:
|
8 |
#
|
9 |
# 1. Redistributions of source code must retain the above
|
10 |
# copyright notice, this list of conditions and the following
|
11 |
# disclaimer.
|
12 |
#
|
13 |
# 2. Redistributions in binary form must reproduce the above
|
14 |
# copyright notice, this list of conditions and the following
|
15 |
# disclaimer in the documentation and/or other materials
|
16 |
# provided with the distribution.
|
17 |
#
|
18 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
19 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
20 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
21 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
22 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
23 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
24 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
25 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
26 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
27 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
28 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
29 |
# POSSIBILITY OF SUCH DAMAGE.
|
30 |
#
|
31 |
# The views and conclusions contained in the software and
|
32 |
# documentation are those of the authors and should not be
|
33 |
# interpreted as representing official policies, either expressed
|
34 |
# or implied, of GRNET S.A.
|
35 |
|
36 |
|
37 |
from kamaki.clients.commissioning import ( |
38 |
CanonifyException, |
39 |
SpecifyException, |
40 |
Specificator, |
41 |
Null, |
42 |
Integer, |
43 |
Text, |
44 |
Tuple, |
45 |
ListOf, |
46 |
Dict, |
47 |
Args) |
48 |
from random import choice, randint |
49 |
|
50 |
Context = Dict(classname='Context', null=True, show=False) |
51 |
|
52 |
|
53 |
class Name(Text): |
54 |
def init(self): |
55 |
self.opts.update({'regex': "[\w.:@+/-]+", 'maxlen': 512}) |
56 |
Text.init(self)
|
57 |
|
58 |
def _random_choice(self, kw): |
59 |
alphabet = u'abcdef_1233490.:@/-αβγδεζ'
|
60 |
length = randint(1, 48) |
61 |
return ''.join(choice(alphabet) for _ in xrange(length)) |
62 |
|
63 |
|
64 |
class Nonnegative(Integer): |
65 |
def init(self): |
66 |
self.opts.update({'minimum': 0}) |
67 |
|
68 |
|
69 |
class Positive(Integer): |
70 |
def init(self): |
71 |
self.opts.update({'minimum': 1}) |
72 |
|
73 |
|
74 |
QH_PRACTICALLY_INFINITE = 10 ** 32 |
75 |
|
76 |
Serial = Positive(classname='Serial')
|
77 |
|
78 |
ClientKey = Name(classname='ClientKey')
|
79 |
Nothing = Null(classname='Nothing')
|
80 |
|
81 |
Entity = Name(classname='Entity')
|
82 |
Owner = Name(classname='Owner')
|
83 |
Key = Text(classname='Key')
|
84 |
NewKey = Text(classname='Newkey')
|
85 |
OwnerKey = Text(classname='OwnerKey')
|
86 |
Resource = Name(classname='Resource')
|
87 |
Policy = Name(classname='Policy')
|
88 |
|
89 |
Quantity = Integer(classname='Quantity')
|
90 |
Capacity = Nonnegative(classname='Capacity')
|
91 |
ImportLimit = Nonnegative(classname='ImportLimit')
|
92 |
ExportLimit = Nonnegative(classname='ExportLimit')
|
93 |
QuantityDelta = Integer(classname='QuantityDelta')
|
94 |
CapacityDelta = Integer(classname='CapacityDelta')
|
95 |
ImportLimitDelta = Integer(classname='ImportLimitDelta')
|
96 |
ExportLimitDelta = Integer(classname='ExportLimitDelta')
|
97 |
Imported = Nonnegative(classname='Imported')
|
98 |
Exported = Nonnegative(classname='Exported')
|
99 |
Returned = Nonnegative(classname='Returned')
|
100 |
Released = Nonnegative(classname='Released')
|
101 |
Flags = Nonnegative(classname='Flags')
|
102 |
Index = Nonnegative(classname='Index')
|
103 |
|
104 |
Timepoint = Text(classname='Timepoint', maxlen=24) |
105 |
Reason = Text(classname='Reason', regex='(ACCEPT|REJECT):.*', maxlen=128) |
106 |
|
107 |
|
108 |
class QuotaholderAPI(Specificator): |
109 |
|
110 |
def create_entity( |
111 |
self,
|
112 |
context=Context, |
113 |
create_entity=ListOf(Entity, Owner, Key, OwnerKey, |
114 |
nonempty=1)
|
115 |
): |
116 |
rejected = ListOf(Index) |
117 |
return rejected
|
118 |
|
119 |
def set_entity_key( |
120 |
self,
|
121 |
context=Context, |
122 |
set_entity_key=ListOf(Entity, Key, NewKey) |
123 |
): |
124 |
rejected = ListOf(Entity) |
125 |
return rejected
|
126 |
|
127 |
def list_entities( |
128 |
self,
|
129 |
context=Context, |
130 |
entity=Entity, |
131 |
key=Key |
132 |
): |
133 |
entities = ListOf(Entity) |
134 |
return entities
|
135 |
|
136 |
def get_entity( |
137 |
self,
|
138 |
context=Context, |
139 |
get_entity=ListOf(Entity, Key, nonempty=1)
|
140 |
): |
141 |
entities = ListOf(Entity, Owner) |
142 |
return entities
|
143 |
|
144 |
def get_limits( |
145 |
self,
|
146 |
context=Context, |
147 |
get_limits=ListOf(Policy, nonempty=1)
|
148 |
): |
149 |
limits = ListOf(Policy, Quantity, Capacity, |
150 |
ImportLimit, ExportLimit) |
151 |
return limits
|
152 |
|
153 |
def set_limits( |
154 |
self,
|
155 |
context=Context, |
156 |
set_limits=ListOf( |
157 |
Policy, |
158 |
Quantity, |
159 |
Capacity, |
160 |
ImportLimit, |
161 |
ExportLimit, |
162 |
nonempty=1)
|
163 |
): |
164 |
rejected = ListOf(Policy) |
165 |
return rejected
|
166 |
|
167 |
def get_holding( |
168 |
self,
|
169 |
context=Context, |
170 |
get_holding=ListOf(Entity, Resource, Key) |
171 |
): |
172 |
holdings = ListOf( |
173 |
Entity, |
174 |
Resource, |
175 |
Policy, |
176 |
Imported, |
177 |
Exported, |
178 |
Returned, |
179 |
Released, |
180 |
Flags) |
181 |
return holdings
|
182 |
|
183 |
def set_holding( |
184 |
self,
|
185 |
context=Context, |
186 |
set_holding=ListOf(Entity, Resource, Key, Policy, Flags) |
187 |
): |
188 |
rejected = ListOf(Entity, Resource, Policy) |
189 |
return rejected
|
190 |
|
191 |
def init_holding( |
192 |
self,
|
193 |
context=Context, |
194 |
init_holding=ListOf( |
195 |
Entity, |
196 |
Resource, |
197 |
Key, |
198 |
Policy, |
199 |
Imported, |
200 |
Exported, |
201 |
Returned, |
202 |
Released, |
203 |
Flags) |
204 |
): |
205 |
rejected = ListOf(Index) |
206 |
return rejected
|
207 |
|
208 |
def reset_holding( |
209 |
self,
|
210 |
context=Context, |
211 |
reset_holding=ListOf( |
212 |
Entity, |
213 |
Resource, |
214 |
Key, |
215 |
Imported, |
216 |
Exported, |
217 |
Returned, |
218 |
Released) |
219 |
): |
220 |
rejected = ListOf(Index) |
221 |
return rejected
|
222 |
|
223 |
def release_holding( |
224 |
self,
|
225 |
context=Context, |
226 |
release_holding=ListOf(Entity, Resource, Key) |
227 |
): |
228 |
rejected = ListOf(Index) |
229 |
return rejected
|
230 |
|
231 |
def list_resources(self, context=Context, entity=Entity, key=Key): |
232 |
resources = ListOf(Resource) |
233 |
return resources
|
234 |
|
235 |
def list_holdings( |
236 |
self,
|
237 |
context=Context, |
238 |
list_holdings=ListOf(Entity, Key) |
239 |
): |
240 |
rejected = ListOf(Entity) |
241 |
holdings_list = ListOf(ListOf(Entity, Resource, |
242 |
Imported, Exported, |
243 |
Returned, Released)) |
244 |
return Tuple(holdings_list, rejected)
|
245 |
|
246 |
def get_quota( |
247 |
self,
|
248 |
context=Context, |
249 |
get_quota=ListOf(Entity, Resource, Key) |
250 |
): |
251 |
quotas = ListOf(Entity, Resource, |
252 |
Quantity, Capacity, |
253 |
ImportLimit, ExportLimit, |
254 |
Imported, Exported, |
255 |
Returned, Released, |
256 |
Flags) |
257 |
return quotas
|
258 |
|
259 |
def set_quota( |
260 |
self,
|
261 |
context=Context, |
262 |
set_quota=ListOf( |
263 |
Entity, |
264 |
Resource, |
265 |
Key, |
266 |
Quantity, |
267 |
Capacity, |
268 |
ImportLimit, |
269 |
ExportLimit, |
270 |
Flags) |
271 |
): |
272 |
rejected = ListOf(Entity, Resource) |
273 |
return rejected
|
274 |
|
275 |
def add_quota( |
276 |
self,
|
277 |
context=Context, |
278 |
clientkey=ClientKey, |
279 |
serial=Serial, |
280 |
sub_quota=ListOf( |
281 |
Entity, |
282 |
Resource, |
283 |
Key, |
284 |
QuantityDelta, |
285 |
CapacityDelta, |
286 |
ImportLimitDelta, |
287 |
ExportLimitDelta), |
288 |
add_quota=ListOf( |
289 |
Entity, |
290 |
Resource, |
291 |
Key, |
292 |
QuantityDelta, |
293 |
CapacityDelta, |
294 |
ImportLimitDelta, |
295 |
ExportLimitDelta) |
296 |
): |
297 |
rejected = ListOf(Entity, Resource) |
298 |
return rejected
|
299 |
|
300 |
def query_serials( |
301 |
self,
|
302 |
context=Context, |
303 |
clientkey=ClientKey, |
304 |
serials=ListOf(Serial) |
305 |
): |
306 |
return ListOf(Serial)
|
307 |
|
308 |
def ack_serials( |
309 |
self,
|
310 |
context=Context, |
311 |
clientkey=ClientKey, |
312 |
serials=ListOf(Serial) |
313 |
): |
314 |
return Nothing
|
315 |
|
316 |
def issue_commission( |
317 |
self,
|
318 |
context=Context, |
319 |
target=Entity, |
320 |
key=Key, |
321 |
clientkey=ClientKey, |
322 |
name=Text(default=''),
|
323 |
provisions=ListOf(Entity, Resource, Quantity) |
324 |
): |
325 |
return Serial
|
326 |
|
327 |
def accept_commission( |
328 |
self,
|
329 |
context=Context, |
330 |
clientkey=ClientKey, |
331 |
serials=ListOf(Serial), |
332 |
reason=Text(default='ACCEPT')
|
333 |
): |
334 |
return Nothing
|
335 |
|
336 |
def reject_commission( |
337 |
self,
|
338 |
context=Context, |
339 |
clientkey=ClientKey, |
340 |
serials=ListOf(Serial), |
341 |
reason=Text(default='REJECT')
|
342 |
): |
343 |
return Nothing
|
344 |
|
345 |
def get_pending_commissions( |
346 |
self,
|
347 |
context=Context, |
348 |
clientkey=ClientKey |
349 |
): |
350 |
pending = ListOf(Serial) |
351 |
return pending
|
352 |
|
353 |
def resolve_pending_commissions( |
354 |
self,
|
355 |
context=Context, |
356 |
clientkey=ClientKey, |
357 |
max_serial=Serial, |
358 |
accept_set=ListOf(Serial) |
359 |
): |
360 |
return Nothing
|
361 |
|
362 |
def release_entity( |
363 |
self,
|
364 |
context=Context, |
365 |
release_entity=ListOf(Entity, Key, nonempty=1)
|
366 |
): |
367 |
rejected = ListOf(Entity) |
368 |
return rejected
|
369 |
|
370 |
def get_timeline( |
371 |
self,
|
372 |
context=Context, |
373 |
after=Timepoint, |
374 |
before=Timepoint, |
375 |
get_timeline=ListOf(Entity, Resource, Key) |
376 |
): |
377 |
timeline = ListOf(Dict( |
378 |
serial=Serial, |
379 |
source=Entity, |
380 |
target=Entity, |
381 |
resource=Resource, |
382 |
name=Name(), |
383 |
quantity=Quantity, |
384 |
source_allocated=Quantity, |
385 |
source_allocated_through=Quantity, |
386 |
source_inbound=Quantity, |
387 |
source_inbound_through=Quantity, |
388 |
source_outbound=Quantity, |
389 |
source_outbound_through=Quantity, |
390 |
target_allocated=Quantity, |
391 |
target_allocated_through=Quantity, |
392 |
target_inbound=Quantity, |
393 |
target_inbound_through=Quantity, |
394 |
target_outbound=Quantity, |
395 |
target_outbound_through=Quantity, |
396 |
issue_time=Timepoint, |
397 |
log_time=Timepoint, |
398 |
reason=Reason, |
399 |
strict=True))
|
400 |
return timeline
|