prevents side-effect on foreign objects when appropriate
[plcapi.git] / PLC / Faults.py
1 #
2 # PLCAPI XML-RPC faults
3 #
4 # Aaron Klingaman <alk@absarokasoft.com>
5 # Mark Huang <mlhuang@cs.princeton.edu>
6 #
7 # Copyright (C) 2004-2006 The Trustees of Princeton University
8 # $Id: Faults.py,v 1.1 2006/09/06 15:36:06 mlhuang Exp $
9 #
10
11 import xmlrpclib
12
13 class PLCFault(xmlrpclib.Fault):
14     def __init__(self, faultCode, faultString, extra = None):
15         if extra:
16             faultString += ": " + extra
17         xmlrpclib.Fault.__init__(self, faultCode, faultString)
18
19 class PLCInvalidAPIMethod(PLCFault):
20     def __init__(self, method, role = None, extra = None):
21         faultString = "Invalid method " + method
22         if role:
23             faultString += " for role " + role
24         PLCFault.__init__(self, 100, faultString, extra)
25
26 class PLCInvalidArgumentCount(PLCFault):
27     def __init__(self, got, min, max = min, extra = None):
28         if min != max:
29             expected = "%d-%d" % (min, max)
30         else:
31             expected = "%d" % min
32         faultString = "Expected %s arguments, got %d" % \
33                       (expected, got)
34         PLCFault.__init__(self, 101, faultString, extra)
35
36 class PLCInvalidArgument(PLCFault):
37     def __init__(self, extra = None, name = None):
38         if name is not None:
39             faultString = "Invalid %s value" % name
40         else:
41             faultString = "Invalid argument"
42         PLCFault.__init__(self, 102, faultString, extra)
43
44 class PLCAuthenticationFailure(PLCFault):
45     def __init__(self, extra = None):
46         faultString = "Failed to authenticate call"
47         PLCFault.__init__(self, 103, faultString, extra)
48
49 class PLCLocalObjectRequired(PLCFault):
50     def __init__(self,method_name="anonymous",obj_name="anonymous",
51                  peer_id=None,extra=None):
52         faultString = "Method: <%s> - Object <%s> must be local"%(method_name,obj_name)
53         if peer_id is not None:
54             faultString += " (authoritative plc has peer_id %d)"%peer_id
55         PLCFault.__init__(self, 104, faultString, extra)
56
57 class PLCDBError(PLCFault):
58     def __init__(self, extra = None):
59         faultString = "Database error"
60         PLCFault.__init__(self, 106, faultString, extra)
61
62 class PLCPermissionDenied(PLCFault):
63     def __init__(self, extra = None):
64         faultString = "Permission denied"
65         PLCFault.__init__(self, 108, faultString, extra)
66
67 class PLCNotImplemented(PLCFault):
68     def __init__(self, extra = None):
69         faultString = "Not fully implemented"
70         PLCFault.__init__(self, 109, faultString, extra)
71
72 class PLCAPIError(PLCFault):
73     def __init__(self, extra = None):
74         faultString = "Internal API error"
75         PLCFault.__init__(self, 111, faultString, extra)
76
77 ####################
78 # shorthands to check various types of objects for localness (are we authoritative)
79 def PLCCheckLocalNode (node,method_name):
80     if node['peer_id'] is not None:
81         raise PLCLocalObjectRequired(method_name,node['hostname'],node['peer_id'])
82
83 def PLCCheckLocalPerson (person,method_name):
84     if person['peer_id'] is not None:
85         raise PLCLocalObjectRequired(method_name,person['email'],person['peer_id'])
86
87 def PLCCheckLocalSite (site,method_name):
88     if site['peer_id'] is not None:
89         raise PLCLocalObjectRequired(method_name,site['name'],site['peer_id'])
90
91 def PLCCheckLocalSlice (slice,method_name):
92     if slice['peer_id'] is not None:
93         raise PLCLocalObjectRequired(method_name,slice['name'],slice['peer_id'])
94
95 def PLCCheckLocalKey (key,method_name):
96     if key['peer_id'] is not None:
97         raise PLCLocalObjectRequired(method_name,key['key_id'],key['peer_id'])
98
99 def PLCCheckLocalSliceAttributeType (sliceAttributeType,method_name):
100     if sliceAttributeType['peer_id'] is not None:
101         raise PLCLocalObjectRequired(method_name,sliceAttributeType['name'],sliceAttributeType['peer_id'])
102
103 def PLCCheckLocalSliceAttribute (sliceAttribute,method_name):
104     if sliceAttribute['peer_id'] is not None:
105         raise PLCLocalObjectRequired(method_name,sliceAttribute['name'],sliceAttribute['peer_id'])
106
107