Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (12.1 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
class QuotaholderAPI(Specificator):
96

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
237
    def add_quota   (
238
                self,
239
                context     =   Context,
240
                add_quota   =   ListOf( Entity, Resource, Key,
241
                                        QuantityDelta, CapacityDelta,
242
                                        ImportLimitDelta, ExportLimitDelta )
243
        ):
244
        rejected = ListOf(Entity, Resource)
245
        return rejected
246

    
247
    def issue_commission    (
248
                self,
249
                context     =   Context,
250
                target      =   Entity,
251
                key         =   Key,
252
                clientkey   =   ClientKey,
253
                name        =   Text(default=''),
254
                provisions  =   ListOf(Entity, Resource, Quantity)
255
        ):
256
        return Serial
257

    
258
    def accept_commission   (
259
                self,
260
                context     =   Context,
261
                clientkey   =   ClientKey,
262
                serials     =   ListOf(Serial),
263
                reason      =   Text(default='ACCEPT')
264
        ):
265
        return Nothing
266

    
267
    def reject_commission   (
268
                self,
269
                context     =   Context,
270
                clientkey   =   ClientKey,
271
                serials     =   ListOf(Serial),
272
                reason      =   Text(default='REJECT')
273
        ):
274
        return Nothing
275

    
276
    def get_pending_commissions (
277
                    self,
278
                    context     =   Context,
279
                    clientkey   =   ClientKey
280
        ):
281
        pending = ListOf(Serial)
282
        return pending
283

    
284
    def resolve_pending_commissions (
285
                    self,
286
                    context     =   Context,
287
                    clientkey   =   ClientKey,
288
                    max_serial  =   Serial,
289
                    accept_set  =   ListOf(Serial)
290
        ):
291
        return Nothing
292

    
293
    def release_entity  (
294
                self,
295
                context         =   Context,
296
                release_entity  =   ListOf(Entity, Key, nonempty=1)
297
        ):
298
        rejected = ListOf(Entity)
299
        return rejected
300

    
301
    def get_timeline    (
302
                self,
303
                context         =   Context,
304
                after           =   Timepoint,
305
                before          =   Timepoint,
306
                get_timeline    =   ListOf(Entity, Resource, Key)
307
        ):
308
        timeline = ListOf(Dict(
309
                            serial                      =   Serial,
310
                            source                      =   Entity,
311
                            target                      =   Entity,
312
                            resource                    =   Resource,
313
                            name                        =   Name(),
314
                            quantity                    =   Quantity,
315
                            source_allocated            =   Quantity,
316
                            source_allocated_through    =   Quantity,
317
                            source_inbound              =   Quantity,
318
                            source_inbound_through      =   Quantity,
319
                            source_outbound             =   Quantity,
320
                            source_outbound_through     =   Quantity,
321
                            target_allocated            =   Quantity,
322
                            target_allocated_through    =   Quantity,
323
                            target_inbound              =   Quantity,
324
                            target_inbound_through      =   Quantity,
325
                            target_outbound             =   Quantity,
326
                            target_outbound_through     =   Quantity,
327
                            issue_time                  =   Timepoint,
328
                            log_time                    =   Timepoint,
329
                            reason                      =   Reason,
330

    
331
                            strict  =   True))
332
        return timeline
333