Fix pep8 compliance issues everywhere
[kamaki] / kamaki / clients / quotaholder / api / quotaholder.py
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(self, context=Context, create_entity=ListOf(
111             Entity, Owner, Key, OwnerKey, nonempty=1)):
112         rejected = ListOf(Index)
113         return rejected
114
115     def set_entity_key(
116         self,
117         context=Context,
118         set_entity_key=ListOf(Entity, Key, NewKey)
119     ):
120         rejected = ListOf(Entity)
121         return rejected
122
123     def list_entities(
124         self,
125         context=Context,
126         entity=Entity,
127         key=Key
128     ):
129         entities = ListOf(Entity)
130         return entities
131
132     def get_entity(
133         self,
134         context=Context,
135         get_entity=ListOf(Entity, Key, nonempty=1)
136     ):
137         entities = ListOf(Entity, Owner)
138         return entities
139
140     def get_limits(
141         self,
142         context=Context,
143         get_limits=ListOf(Policy, nonempty=1)
144     ):
145         limits = ListOf(Policy, Quantity, Capacity,
146                         ImportLimit, ExportLimit)
147         return limits
148
149     def set_limits(
150         self,
151         context=Context,
152         set_limits=ListOf(
153             Policy,
154             Quantity,
155             Capacity,
156             ImportLimit,
157             ExportLimit,
158             nonempty=1)
159     ):
160         rejected = ListOf(Policy)
161         return rejected
162
163     def get_holding(
164         self,
165         context=Context,
166         get_holding=ListOf(Entity, Resource, Key)
167     ):
168         holdings = ListOf(
169             Entity,
170             Resource,
171             Policy,
172             Imported,
173             Exported,
174             Returned,
175             Released,
176             Flags)
177         return holdings
178
179     def set_holding(
180         self,
181         context=Context,
182         set_holding=ListOf(Entity, Resource, Key, Policy, Flags)
183     ):
184         rejected = ListOf(Entity, Resource, Policy)
185         return rejected
186
187     def init_holding(
188         self,
189         context=Context,
190         init_holding=ListOf(
191             Entity,
192             Resource,
193             Key,
194             Policy,
195             Imported,
196             Exported,
197             Returned,
198             Released,
199             Flags)
200     ):
201         rejected = ListOf(Index)
202         return rejected
203
204     def reset_holding(
205         self,
206         context=Context,
207         reset_holding=ListOf(
208             Entity,
209             Resource,
210             Key,
211             Imported,
212             Exported,
213             Returned,
214             Released)
215     ):
216         rejected = ListOf(Index)
217         return rejected
218
219     def release_holding(
220         self,
221         context=Context,
222         release_holding=ListOf(Entity, Resource, Key)
223     ):
224         rejected = ListOf(Index)
225         return rejected
226
227     def list_resources(self, context=Context, entity=Entity, key=Key):
228         resources = ListOf(Resource)
229         return resources
230
231     def list_holdings(
232         self,
233         context=Context,
234         list_holdings=ListOf(Entity, Key)
235     ):
236         rejected = ListOf(Entity)
237         holdings_list = ListOf(ListOf(Entity, Resource,
238                                       Imported, Exported,
239                                       Returned, Released))
240         return Tuple(holdings_list, rejected)
241
242     def get_quota(
243         self,
244         context=Context,
245         get_quota=ListOf(Entity, Resource, Key)
246     ):
247         quotas = ListOf(Entity, Resource,
248                         Quantity, Capacity,
249                         ImportLimit, ExportLimit,
250                         Imported, Exported,
251                         Returned, Released,
252                         Flags)
253         return quotas
254
255     def set_quota(
256         self,
257         context=Context,
258         set_quota=ListOf(
259             Entity,
260             Resource,
261             Key,
262             Quantity,
263             Capacity,
264             ImportLimit,
265             ExportLimit,
266             Flags)
267     ):
268         rejected = ListOf(Entity, Resource)
269         return rejected
270
271     def add_quota(
272         self,
273         context=Context,
274         clientkey=ClientKey,
275         serial=Serial,
276         sub_quota=ListOf(
277             Entity,
278             Resource,
279             Key,
280             QuantityDelta,
281             CapacityDelta,
282             ImportLimitDelta,
283             ExportLimitDelta),
284         add_quota=ListOf(
285             Entity,
286             Resource,
287             Key,
288             QuantityDelta,
289             CapacityDelta,
290             ImportLimitDelta,
291             ExportLimitDelta)
292     ):
293         rejected = ListOf(Entity, Resource)
294         return rejected
295
296     def query_serials(
297         self,
298         context=Context,
299         clientkey=ClientKey,
300         serials=ListOf(Serial)
301     ):
302         return ListOf(Serial)
303
304     def ack_serials(
305         self,
306         context=Context,
307         clientkey=ClientKey,
308         serials=ListOf(Serial)
309     ):
310         return Nothing
311
312     def issue_commission(
313         self,
314         context=Context,
315         target=Entity,
316         key=Key,
317         clientkey=ClientKey,
318         name=Text(default=''),
319         provisions=ListOf(Entity, Resource, Quantity)
320     ):
321         return Serial
322
323     def accept_commission(
324         self,
325         context=Context,
326         clientkey=ClientKey,
327         serials=ListOf(Serial),
328         reason=Text(default='ACCEPT')
329     ):
330         return Nothing
331
332     def reject_commission(
333         self,
334         context=Context,
335         clientkey=ClientKey,
336         serials=ListOf(Serial),
337         reason=Text(default='REJECT')
338     ):
339         return Nothing
340
341     def get_pending_commissions(
342         self,
343         context=Context,
344         clientkey=ClientKey
345     ):
346         pending = ListOf(Serial)
347         return pending
348
349     def resolve_pending_commissions(
350         self,
351         context=Context,
352         clientkey=ClientKey,
353         max_serial=Serial,
354         accept_set=ListOf(Serial)
355     ):
356         return Nothing
357
358     def release_entity(
359         self,
360         context=Context,
361         release_entity=ListOf(Entity, Key, nonempty=1)
362     ):
363         rejected = ListOf(Entity)
364         return rejected
365
366     def get_timeline(
367         self,
368         context=Context,
369         after=Timepoint,
370         before=Timepoint,
371         get_timeline=ListOf(Entity, Resource, Key)
372     ):
373         timeline = ListOf(Dict(
374             serial=Serial,
375             source=Entity,
376             target=Entity,
377             resource=Resource,
378             name=Name(),
379             quantity=Quantity,
380             source_allocated=Quantity,
381             source_allocated_through=Quantity,
382             source_inbound=Quantity,
383             source_inbound_through=Quantity,
384             source_outbound=Quantity,
385             source_outbound_through=Quantity,
386             target_allocated=Quantity,
387             target_allocated_through=Quantity,
388             target_inbound=Quantity,
389             target_inbound_through=Quantity,
390             target_outbound=Quantity,
391             target_outbound_through=Quantity,
392             issue_time=Timepoint,
393             log_time=Timepoint,
394             reason=Reason,
395             strict=True))
396         return timeline