Statistics
| Branch: | Tag: | Revision:

root / snf-common / synnefo / lib / quotaholder / api / quotaholder.py @ ef0fa70b

History | View | Annotate | Download (12.5 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 synnefo.lib.commissioning import (CanonifyException, SpecifyException,
38
                                       Specificator, Null, Integer, Text,
39
                                       Tuple, ListOf, Dict, Args)
40
from random import choice, randint
41

    
42
Context             =   Dict(classname='Context', null=True, show=False)
43

    
44
class Name(Text):
45
    def init(self):
46
        self.opts.update({'regex': "[\w.:@+/-]+", 'maxlen':512})
47
        Text.init(self)
48

    
49
    def _random_choice(self, kw):
50
        alphabet = u'abcdef_1233490.:@/-αβγδεζ'
51
        length = randint(1, 48)
52
        return ''.join(choice(alphabet) for _ in xrange(length))
53

    
54
class Nonnegative(Integer):
55
    def init(self):
56
        self.opts.update({'minimum': 0})
57

    
58
class Positive(Integer):
59
    def init(self):
60
        self.opts.update({'minimum': 1})
61

    
62
Serial              =   Positive(classname='Serial')
63

    
64
ClientKey           =   Name(classname='ClientKey')
65
Nothing             =   Null(classname='Nothing')
66

    
67
Entity              =   Name(classname='Entity')
68
Owner               =   Name(classname='Owner')
69
Key                 =   Text(classname='Key')
70
NewKey              =   Text(classname='Newkey')
71
OwnerKey            =   Text(classname='OwnerKey')
72
Resource            =   Name(classname='Resource')
73
Policy              =   Name(classname='Policy')
74

    
75
Quantity            =   Integer(classname='Quantity', null=True)
76
Capacity            =   Nonnegative(classname='Capacity', null=True)
77
ImportLimit         =   Nonnegative(classname='ImportLimit', null=True)
78
ExportLimit         =   Nonnegative(classname='ExportLimit', null=True)
79
QuantityDelta       =   Integer(classname='QuantityDelta', null=True)
80
CapacityDelta       =   Integer(classname='CapacityDelta', null=True)
81
ImportLimitDelta    =   Integer(classname='ImportLimitDelta', null=True)
82
ExportLimitDelta    =   Integer(classname='ExportLimitDelta', null=True)
83
Imported            =   Nonnegative(classname='Imported')
84
Exported            =   Nonnegative(classname='Exported')
85
Returned            =   Nonnegative(classname='Returned')
86
Released            =   Nonnegative(classname='Released')
87
Flags               =   Nonnegative(classname='Flags')
88
Index               =   Nonnegative(classname='Index')
89

    
90
Timepoint           =   Text(classname='Timepoint', maxlen=24)
91
Reason              =   Text(   classname   =   'Reason',
92
                                regex       =   '(ACCEPT|REJECT):.*',
93
                                maxlen      =   128         )
94

    
95
Bool                =   Integer(classname='Bool')
96

    
97
class QuotaholderAPI(Specificator):
98

    
99
    def create_entity   (
100
                self,
101
                context         =   Context,
102
                create_entity   =   ListOf(Entity, Owner, Key, OwnerKey, nonempty=1)
103
        ):
104
        rejected = ListOf(Index)
105
        return rejected
106

    
107
    def set_entity_key  (
108
                self,
109
                context         =   Context,
110
                set_entity_key  =   ListOf(Entity, Key, NewKey)
111
        ):
112
        rejected = ListOf(Entity)
113
        return rejected
114

    
115
    def list_entities   (
116
                self,
117
                context         =   Context,
118
                entity          =   Entity,
119
                key             =   Key
120
        ):
121
        entities = ListOf(Entity)
122
        return entities
123

    
124
    def get_entity  (
125
                self,
126
                context     =   Context,
127
                get_entity  =   ListOf(Entity, Key, nonempty=1)
128
        ):
129
        entities = ListOf(Entity, Owner)
130
        return entities
131

    
132
    def get_limits  (
133
                self,
134
                context     =   Context,
135
                get_limits  =   ListOf(Policy, nonempty=1)
136
        ):
137
        limits = ListOf(Policy, Quantity, Capacity,
138
                        ImportLimit, ExportLimit)
139
        return limits
140

    
141
    def set_limits  (
142
                self,
143
                context     =   Context,
144
                set_limits  =   ListOf( Policy, Quantity, Capacity,
145
                                        ImportLimit, ExportLimit,
146
                                        nonempty=1 )
147
        ):
148
        rejected = ListOf(Policy)
149
        return rejected
150

    
151
    def get_holding (
152
                self,
153
                context     =   Context,
154
                get_holding =   ListOf(Entity, Resource, Key)
155
        ):
156
        holdings = ListOf(  Entity, Resource, Policy,
157
                            Imported, Exported, Returned, Released, Flags   )
158
        return holdings
159

    
160
    def set_holding (
161
                self,
162
                context     =   Context,
163
                set_holding =   ListOf(Entity, Resource, Key, Policy, Flags)
164
        ):
165
        rejected = ListOf(Entity, Resource, Policy)
166
        return rejected
167

    
168
    def init_holding (
169
                self,
170
                context      =   Context,
171
                init_holding =   ListOf(Entity, Resource, Key, Policy,
172
                                        Imported, Exported, Returned, Released,
173
                                        Flags)
174
        ):
175
        rejected = ListOf(Index)
176
        return rejected
177

    
178
    def reset_holding (
179
                self,
180
                context       =   Context,
181
                reset_holding =   ListOf(Entity, Resource, Key,
182
                                        Imported, Exported, Returned, Released)
183
        ):
184
        rejected = ListOf(Index)
185
        return rejected
186

    
187
    def release_holding (
188
                self,
189
                context         =   Context,
190
                release_holding =   ListOf(Entity, Resource, Key)
191
        ):
192
        rejected = ListOf(Index)
193
        return rejected
194

    
195
    def list_resources  (
196
                self,
197
                context     =   Context,
198
                entity      =   Entity,
199
                key         =   Key
200
        ):
201
        resources = ListOf(Resource)
202
        return resources
203

    
204
    def list_holdings   (
205
                self,
206
                context         =   Context,
207
                list_holdings   =   ListOf(Entity, Key)
208
        ):
209

    
210
        rejected = ListOf(Entity)
211
        holdings_list = ListOf(ListOf(Entity, Resource,
212
                                      Imported, Exported,
213
                                      Returned, Released))
214
        return Tuple(holdings_list, rejected)
215

    
216
    def get_quota   (
217
                self,
218
                context     =   Context,
219
                get_quota   =   ListOf(Entity, Resource, Key)
220
        ):
221
        quotas = ListOf(Entity, Resource,
222
                        Quantity, Capacity,
223
                        ImportLimit, ExportLimit,
224
                        Imported, Exported,
225
                        Returned, Released,
226
                        Flags)
227
        return quotas
228

    
229
    def set_quota   (
230
                self,
231
                context     =   Context,
232
                set_quota   =   ListOf( Entity, Resource, Key,
233
                                        Quantity, Capacity,
234
                                        ImportLimit, ExportLimit, Flags )
235
        ):
236
        rejected = ListOf(Entity, Resource)
237
        return rejected
238

    
239
    def add_quota   (
240
                self,
241
                context     =   Context,
242
                clientkey   =   ClientKey,
243
                serial      =   Serial,
244
                add_quota   =   ListOf( Entity, Resource, Key,
245
                                        QuantityDelta, CapacityDelta,
246
                                        ImportLimitDelta, ExportLimitDelta )
247
        ):
248
        rejected = ListOf(Entity, Resource)
249
        return rejected
250

    
251
    def ack_serial (
252
                self,
253
                context     =   Context,
254
                clientkey   =   ClientKey,
255
                serial      =   Serial,
256
                fetch_args  =   Bool
257
        ):
258
        return ListOf(Entity, Resource)
259

    
260
    def issue_commission    (
261
                self,
262
                context     =   Context,
263
                target      =   Entity,
264
                key         =   Key,
265
                clientkey   =   ClientKey,
266
                name        =   Text(default=''),
267
                provisions  =   ListOf(Entity, Resource, Quantity)
268
        ):
269
        return Serial
270

    
271
    def accept_commission   (
272
                self,
273
                context     =   Context,
274
                clientkey   =   ClientKey,
275
                serials     =   ListOf(Serial),
276
                reason      =   Text(default='ACCEPT')
277
        ):
278
        return Nothing
279

    
280
    def reject_commission   (
281
                self,
282
                context     =   Context,
283
                clientkey   =   ClientKey,
284
                serials     =   ListOf(Serial),
285
                reason      =   Text(default='REJECT')
286
        ):
287
        return Nothing
288

    
289
    def get_pending_commissions (
290
                    self,
291
                    context     =   Context,
292
                    clientkey   =   ClientKey
293
        ):
294
        pending = ListOf(Serial)
295
        return pending
296

    
297
    def resolve_pending_commissions (
298
                    self,
299
                    context     =   Context,
300
                    clientkey   =   ClientKey,
301
                    max_serial  =   Serial,
302
                    accept_set  =   ListOf(Serial)
303
        ):
304
        return Nothing
305

    
306
    def release_entity  (
307
                self,
308
                context         =   Context,
309
                release_entity  =   ListOf(Entity, Key, nonempty=1)
310
        ):
311
        rejected = ListOf(Entity)
312
        return rejected
313

    
314
    def get_timeline    (
315
                self,
316
                context         =   Context,
317
                after           =   Timepoint,
318
                before          =   Timepoint,
319
                get_timeline    =   ListOf(Entity, Resource, Key)
320
        ):
321
        timeline = ListOf(Dict(
322
                            serial                      =   Serial,
323
                            source                      =   Entity,
324
                            target                      =   Entity,
325
                            resource                    =   Resource,
326
                            name                        =   Name(),
327
                            quantity                    =   Quantity,
328
                            source_allocated            =   Quantity,
329
                            source_allocated_through    =   Quantity,
330
                            source_inbound              =   Quantity,
331
                            source_inbound_through      =   Quantity,
332
                            source_outbound             =   Quantity,
333
                            source_outbound_through     =   Quantity,
334
                            target_allocated            =   Quantity,
335
                            target_allocated_through    =   Quantity,
336
                            target_inbound              =   Quantity,
337
                            target_inbound_through      =   Quantity,
338
                            target_outbound             =   Quantity,
339
                            target_outbound_through     =   Quantity,
340
                            issue_time                  =   Timepoint,
341
                            log_time                    =   Timepoint,
342
                            reason                      =   Reason,
343

    
344
                            strict  =   True))
345
        return timeline
346