07ec10d5966ac5acc0ff96d4e322ee4aea552243
[sfa.git] / sfa / util / faults.py
1 #----------------------------------------------------------------------
2 # Copyright (c) 2008 Board of Trustees, Princeton University
3 #
4 # Permission is hereby granted, free of charge, to any person obtaining
5 # a copy of this software and/or hardware specification (the "Work") to
6 # deal in the Work without restriction, including without limitation the
7 # rights to use, copy, modify, merge, publish, distribute, sublicense,
8 # and/or sell copies of the Work, and to permit persons to whom the Work
9 # is furnished to do so, subject to the following conditions:
10 #
11 # The above copyright notice and this permission notice shall be
12 # included in all copies or substantial portions of the Work.
13 #
14 # THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
21 # IN THE WORK.
22 #----------------------------------------------------------------------
23 #
24 # SFA API faults
25 #
26
27 from sfa.util.genicode import GENICODE
28 import xmlrpc.client
29
30
31 class SfaFault(xmlrpc.client.Fault):
32
33     def __init__(self, faultCode, faultString, extra=None):
34         if extra:
35             faultString += ": " + str(extra)
36         xmlrpc.client.Fault.__init__(self, faultCode, faultString)
37
38
39 class Forbidden(SfaFault):
40
41     def __init__(self,  extra=None):
42         faultString = "FORBIDDEN"
43         SfaFault.__init__(self, GENICODE.FORBIDDEN, faultString, extra)
44
45
46 class BadArgs(SfaFault):
47
48     def __init__(self,  extra=None):
49         faultString = "BADARGS"
50         SfaFault.__init__(self, GENICODE.BADARGS, faultString, extra)
51
52
53 class CredentialMismatch(SfaFault):
54
55     def __init__(self,  extra=None):
56         faultString = "Credential mismatch"
57         SfaFault.__init__(self, GENICODE.CREDENTIAL_MISMATCH,
58                           faultString, extra)
59
60
61 class SfaInvalidAPIMethod(SfaFault):
62
63     def __init__(self, method, interface=None, extra=None):
64         faultString = "Invalid method " + method
65         if interface:
66             faultString += " for interface " + interface
67         SfaFault.__init__(self, GENICODE.UNSUPPORTED, faultString, extra)
68
69
70 class SfaInvalidArgumentCount(SfaFault):
71
72     def __init__(self, got, min, max=min, extra=None):
73         if min != max:
74             expected = "%d-%d" % (min, max)
75         else:
76             expected = "%d" % min
77         faultString = "Expected %s arguments, got %d" % \
78                       (expected, got)
79         SfaFault.__init__(self, GENICODE.BADARGS, faultString, extra)
80
81
82 class SfaInvalidArgument(SfaFault):
83
84     def __init__(self, extra=None, name=None):
85         if name is not None:
86             faultString = "Invalid %s value" % name
87         else:
88             faultString = "Invalid argument"
89         SfaFault.__init__(self, GENICODE.BADARGS, faultString, extra)
90
91
92 class SfaAuthenticationFailure(SfaFault):
93
94     def __init__(self, extra=None):
95         faultString = "Failed to authenticate call"
96         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
97
98
99 class SfaDBError(SfaFault):
100
101     def __init__(self, extra=None):
102         faultString = "Database error"
103         SfaFault.__init__(self, GENICODE.DBERROR, faultString, extra)
104
105
106 class SfaPermissionDenied(SfaFault):
107
108     def __init__(self, extra=None):
109         faultString = "Permission denied"
110         SfaFault.__init__(self, GENICODE.FORBIDDEN, faultString, extra)
111
112
113 class SfaNotImplemented(SfaFault):
114
115     def __init__(self, interface=None, extra=None):
116         faultString = "Not implemented"
117         if interface:
118             faultString += " at interface " + interface
119         SfaFault.__init__(self, GENICODE.UNSUPPORTED, faultString, extra)
120
121
122 class SfaAPIError(SfaFault):
123
124     def __init__(self, extra=None):
125         faultString = "Internal SFA API error"
126         SfaFault.__init__(self, GENICODE.SERVERERROR, faultString, extra)
127
128
129 class MalformedHrnException(SfaFault):
130
131     def __init__(self, value, extra=None):
132         self.value = value
133         faultString = "Malformed HRN: %(value)s" % locals()
134         SfaFault.__init__(self, GENICODE.ERROR, extra)
135
136     def __str__(self):
137         return repr(self.value)
138
139
140 class TreeException(SfaFault):
141
142     def __init__(self, value, extra=None):
143         self.value = value
144         faultString = "Tree Exception: %(value)s, " % locals()
145         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
146
147     def __str__(self):
148         return repr(self.value)
149
150
151 class SearchFailed(SfaFault):
152
153     def __init__(self, value, extra=None):
154         self.value = value
155         faultString = "%s does not exist here " % self.value
156         SfaFault.__init__(self, GENICODE.SEARCHFAILED, faultString, extra)
157
158     def __str__(self):
159         return repr(self.value)
160
161
162 class NonExistingRecord(SfaFault):
163
164     def __init__(self, value, extra=None):
165         self.value = value
166         faultString = "Non exsiting record %(value)s, " % locals()
167         SfaFault.__init__(self, GENICODE.SEARCHFAILED, faultString, extra)
168
169     def __str__(self):
170         return repr(self.value)
171
172
173 class ExistingRecord(SfaFault):
174
175     def __init__(self, value, extra=None):
176         self.value = value
177         faultString = "Existing record: %(value)s, " % locals()
178         SfaFault.__init__(self, GENICODE.REFUSED, faultString, extra)
179
180     def __str__(self):
181         return repr(self.value)
182
183
184 class InvalidRPCParams(SfaFault):
185
186     def __init__(self, value, extra=None):
187         self.value = value
188         faultString = "Invalid RPC Params: %(value)s, " % locals()
189         SfaFault.__init__(self, GENICODE.RPCERROR, faultString, extra)
190
191     def __str__(self):
192         return repr(self.value)
193
194 # SMBAKER exceptions follow
195
196
197 class ConnectionKeyGIDMismatch(SfaFault):
198
199     def __init__(self, value, extra=None):
200         self.value = value
201         faultString = "Connection Key GID mismatch: %(value)s" % locals()
202         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
203
204     def __str__(self):
205         return repr(self.value)
206
207
208 class MissingCallerGID(SfaFault):
209
210     def __init__(self, value, extra=None):
211         self.value = value
212         faultString = "Missing Caller GID: %(value)s" % locals()
213         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
214
215     def __str__(self):
216         return repr(self.value)
217
218
219 class RecordNotFound(SfaFault):
220
221     def __init__(self, value, extra=None):
222         self.value = value
223         faultString = "Record not found: %(value)s" % locals()
224         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
225
226     def __str__(self):
227         return repr(self.value)
228
229
230 class UnknownSfaType(SfaFault):
231
232     def __init__(self, value, extra=None):
233         self.value = value
234         faultString = "Unknown SFA Type: %(value)s" % locals()
235         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
236
237     def __str__(self):
238         return repr(self.value)
239
240
241 class MissingAuthority(SfaFault):
242
243     def __init__(self, value, extra=None):
244         self.value = value
245         faultString = "Missing authority: %(value)s" % locals()
246         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
247
248     def __str__(self):
249         return repr(self.value)
250
251
252 class PlanetLabRecordDoesNotExist(SfaFault):
253
254     def __init__(self, value, extra=None):
255         self.value = value
256         faultString = "PlanetLab record does not exist : %(value)s" % locals()
257         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
258
259     def __str__(self):
260         return repr(self.value)
261
262
263 class PermissionError(SfaFault):
264
265     def __init__(self, value, extra=None):
266         self.value = value
267         faultString = "Permission error: %(value)s" % locals()
268         SfaFault.__init__(self, GENICODE.FORBIDDEN, faultString, extra)
269
270     def __str__(self):
271         return repr(self.value)
272
273
274 class InsufficientRights(SfaFault):
275
276     def __init__(self, value, extra=None):
277         self.value = value
278         faultString = "Insufficient rights: %(value)s" % locals()
279         SfaFault.__init__(self, GENICODE.FORBIDDEN, faultString, extra)
280
281     def __str__(self):
282         return repr(self.value)
283
284
285 class MissingDelegateBit(SfaFault):
286
287     def __init__(self, value, extra=None):
288         self.value = value
289         faultString = "Missing delegate bit: %(value)s" % locals()
290         SfaFault.__init__(self, GENICODE.FORBIDDEN, faultString, extra)
291
292     def __str__(self):
293         return repr(self.value)
294
295
296 class ChildRightsNotSubsetOfParent(SfaFault):
297
298     def __init__(self, value, extra=None):
299         self.value = value
300         faultString = "Child rights not subset of parent: %(value)s" % locals()
301         SfaFault.__init__(self, GENICODE.FORBIDDEN, faultString, extra)
302
303     def __str__(self):
304         return repr(self.value)
305
306
307 class CertMissingParent(SfaFault):
308
309     def __init__(self, value, extra=None):
310         self.value = value
311         faultString = "Cert missing parent: %(value)s" % locals()
312         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
313
314     def __str__(self):
315         return repr(self.value)
316
317
318 class CertNotSignedByParent(SfaFault):
319
320     def __init__(self, value, extra=None):
321         self.value = value
322         faultString = "Cert not signed by parent: %(value)s" % locals()
323         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
324
325     def __str__(self):
326         return repr(self.value)
327
328
329 class GidParentHrn(SfaFault):
330
331     def __init__(self, value, extra=None):
332         self.value = value
333         faultString = "Cert URN is not an extension of its parent: %(value)s" % locals(
334         )
335         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
336
337     def __str__(self):
338         return repr(self.value)
339
340
341 class GidInvalidParentHrn(SfaFault):
342
343     def __init__(self, value, extra=None):
344         self.value = value
345         faultString = "GID invalid parent hrn: %(value)s" % locals()
346         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
347
348     def __str__(self):
349         return repr(self.value)
350
351
352 class SliverDoesNotExist(SfaFault):
353
354     def __init__(self, value, extra=None):
355         self.value = value
356         faultString = "Sliver does not exist : %(value)s" % locals()
357         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
358
359     def __str__(self):
360         return repr(self.value)
361
362
363 class BadRequestHash(xmlrpc.client.Fault):
364
365     def __init__(self, hash=None, extra=None):
366         faultString = "bad request hash: " + str(hash)
367         xmlrpc.client.Fault.__init__(self, GENICODE.ERROR, faultString)
368
369
370 class MissingTrustedRoots(SfaFault):
371
372     def __init__(self, value, extra=None):
373         self.value = value
374         faultString = "Trusted root directory does not exist: %(value)s" % locals(
375         )
376         SfaFault.__init__(self, GENICODE.SERVERERROR, faultString, extra)
377
378     def __str__(self):
379         return repr(self.value)
380
381
382 class MissingSfaInfo(SfaFault):
383
384     def __init__(self, value, extra=None):
385         self.value = value
386         faultString = "Missing information: %(value)s" % locals()
387         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
388
389     def __str__(self):
390         return repr(self.value)
391
392
393 class InvalidRSpec(SfaFault):
394
395     def __init__(self, value, extra=None):
396         self.value = value
397         faultString = "Invalid RSpec: %(value)s" % locals()
398         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
399
400     def __str__(self):
401         return repr(self.value)
402
403
404 class InvalidRSpecVersion(SfaFault):
405
406     def __init__(self, value, extra=None):
407         self.value = value
408         faultString = "Invalid RSpec version: %(value)s" % locals()
409         SfaFault.__init__(self, GENICODE.BADVERSION, faultString, extra)
410
411     def __str__(self):
412         return repr(self.value)
413
414
415 class UnsupportedRSpecVersion(SfaFault):
416
417     def __init__(self, value, extra=None):
418         self.value = value
419         faultString = "Unsupported RSpec version: %(value)s" % locals()
420         SfaFault.__init__(self, GENICODE.UNSUPPORTED, faultString, extra)
421
422     def __str__(self):
423         return repr(self.value)
424
425
426 class InvalidRSpecElement(SfaFault):
427
428     def __init__(self, value, extra=None):
429         self.value = value
430         faultString = "Invalid RSpec Element: %(value)s" % locals()
431         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
432
433     def __str__(self):
434         return repr(self.value)
435
436
437 class InvalidXML(SfaFault):
438
439     def __init__(self, value, extra=None):
440         self.value = value
441         faultString = "Invalid XML Document: %(value)s" % locals()
442         SfaFault.__init__(self, GENICODE.BADARGS, faultString, extra)
443
444     def __str__(self):
445         return repr(self.value)
446
447
448 class AccountNotEnabled(SfaFault):
449
450     def __init__(self,  extra=None):
451         faultString = "Account Disabled"
452         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
453
454     def __str__(self):
455         return repr(self.value)
456
457
458 class CredentialNotVerifiable(SfaFault):
459
460     def __init__(self, value=None, extra=None):
461         self.value = value
462         faultString = "Unable to verify credential" % locals()
463         if value:
464             faultString += ": %s" % value
465         faultString += ", "
466         SfaFault.__init__(self, GENICODE.BADARGS, faultString, extra)
467
468     def __str__(self):
469         return repr(self.value)
470
471
472 class CertExpired(SfaFault):
473
474     def __init__(self, value, extra=None):
475         self.value = value
476         faultString = "%s cert is expired" % value
477         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
478
479
480 class SfatablesRejected(SfaFault):
481
482     def __init__(self, value, extra=None):
483         self.value = value
484         faultString = "%s rejected by sfatables"
485         SfaFault.__init__(self, GENICODE.FORBIDDEN, faultString, extra)
486
487
488 class UnsupportedOperation(SfaFault):
489
490     def __init__(self, value, extra=None):
491         self.value = value
492         faultString = "Unsupported operation: %s" % value
493         SfaFault.__init__(self, GENICODE.UNSUPPORTED, faultString, extra)