deeper pass on xmlrpclib vs xmlrpc.client as well as configparser
[sfa.git] / sfa / util / faults.py
index d1f070c..702b685 100644 (file)
@@ -1,25 +1,60 @@
+#----------------------------------------------------------------------
+# Copyright (c) 2008 Board of Trustees, Princeton University
 #
-# SFA API faults
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and/or hardware specification (the "Work") to
+# deal in the Work without restriction, including without limitation the
+# rights to use, copy, modify, merge, publish, distribute, sublicense,
+# and/or sell copies of the Work, and to permit persons to whom the Work
+# is furnished to do so, subject to the following conditions:
 #
+# The above copyright notice and this permission notice shall be
+# included in all copies or substantial portions of the Work.
+#
+# THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
+# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
+# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
+# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
+# OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS 
+# IN THE WORK.
+#----------------------------------------------------------------------
+#
+# SFA API faults
 #
 
-### $Id$
-### $URL$
-
-import xmlrpclib
+from sfa.util.genicode import GENICODE
+from sfa.util.py23 import xmlrpc_client
 
-class SfaFault(xmlrpclib.Fault):
+class SfaFault(xmlrpc_client.Fault):
     def __init__(self, faultCode, faultString, extra = None):
         if extra:
-            faultString += ": " + extra
-        xmlrpclib.Fault.__init__(self, faultCode, faultString)
+            faultString += ": " + str(extra)
+        xmlrpc_client.Fault.__init__(self, faultCode, faultString)
+
+class Forbidden(SfaFault):
+    def __init__(self,  extra = None):
+        faultString = "FORBIDDEN" 
+        SfaFault.__init__(self, GENICODE.FORBIDDEN, faultString, extra)   
+
+class BadArgs(SfaFault):
+    def __init__(self,  extra = None):
+        faultString = "BADARGS"
+        SfaFault.__init__(self, GENICODE.BADARGS, faultString, extra)
+
+
+class CredentialMismatch(SfaFault):
+    def __init__(self,  extra = None):
+        faultString = "Credential mismatch"
+        SfaFault.__init__(self, GENICODE.CREDENTIAL_MISMATCH, faultString, extra) 
 
 class SfaInvalidAPIMethod(SfaFault):
     def __init__(self, method, interface = None, extra = None):
         faultString = "Invalid method " + method
         if interface:
             faultString += " for interface " + interface
-        SfaFault.__init__(self, 100, faultString, extra)
+        SfaFault.__init__(self, GENICODE.UNSUPPORTED, faultString, extra)
 
 class SfaInvalidArgumentCount(SfaFault):
     def __init__(self, got, min, max = min, extra = None):
@@ -29,7 +64,7 @@ class SfaInvalidArgumentCount(SfaFault):
             expected = "%d" % min
         faultString = "Expected %s arguments, got %d" % \
                       (expected, got)
-        SfaFault.__init__(self, 101, faultString, extra)
+        SfaFault.__init__(self, GENICODE.BADARGS, faultString, extra)
 
 class SfaInvalidArgument(SfaFault):
     def __init__(self, extra = None, name = None):
@@ -37,38 +72,40 @@ class SfaInvalidArgument(SfaFault):
             faultString = "Invalid %s value" % name
         else:
             faultString = "Invalid argument"
-        SfaFault.__init__(self, 102, faultString, extra)
+        SfaFault.__init__(self, GENICODE.BADARGS, faultString, extra)
 
 class SfaAuthenticationFailure(SfaFault):
     def __init__(self, extra = None):
         faultString = "Failed to authenticate call"
-        SfaFault.__init__(self, 103, faultString, extra)
+        SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
 
 class SfaDBError(SfaFault):
     def __init__(self, extra = None):
         faultString = "Database error"
-        SfaFault.__init__(self, 106, faultString, extra)
+        SfaFault.__init__(self, GENICODE.DBERROR, faultString, extra)
 
 class SfaPermissionDenied(SfaFault):
     def __init__(self, extra = None):
         faultString = "Permission denied"
-        SfaFault.__init__(self, 108, faultString, extra)
+        SfaFault.__init__(self, GENICODE.FORBIDDEN, faultString, extra)
 
 class SfaNotImplemented(SfaFault):
-    def __init__(self, extra = None):
-        faultString = "Not fully implemented"
-        SfaFault.__init__(self, 109, faultString, extra)
+    def __init__(self, interface=None, extra = None):
+        faultString = "Not implemented"
+        if interface:
+            faultString += " at interface " + interface 
+        SfaFault.__init__(self, GENICODE.UNSUPPORTED, faultString, extra)
 
 class SfaAPIError(SfaFault):
     def __init__(self, extra = None):
-        faultString = "Internal API error"
-        SfaFault.__init__(self, 111, faultString, extra)
+        faultString = "Internal SFA API error"
+        SfaFault.__init__(self, GENICODE.SERVERERROR, faultString, extra)
 
 class MalformedHrnException(SfaFault):
     def __init__(self, value, extra = None):
         self.value = value
         faultString = "Malformed HRN: %(value)s" % locals()
-        SfaFault.__init__(self, 102, faultString, extra)
+        SfaFault.__init__(self, GENICODE.ERROR, extra)
     def __str__(self):
         return repr(self.value)
 
@@ -76,47 +113,40 @@ class TreeException(SfaFault):
     def __init__(self, value, extra = None):
         self.value = value
         faultString = "Tree Exception: %(value)s, " % locals()
-        SfaFault.__init__(self, 111, faultString, extra)
+        SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
     def __str__(self):
         return repr(self.value)
 
-class NonExistingRecord(SfaFault):
+class SearchFailed(SfaFault):
     def __init__(self, value, extra = None):
         self.value = value
-        faultString = "Non exsiting record %(value)s, " % locals()
-        SfaFault.__init__(self, 111, faultString, extra)
+        faultString = "%s does not exist here " % self.value
+        SfaFault.__init__(self, GENICODE.SEARCHFAILED, faultString, extra)
     def __str__(self):
         return repr(self.value)
 
-class ExistingRecord(SfaFault):
-    def __init__(self, value, extra = None):
-        self.value = value
-        faultString = "Existing record: %(value)s, " % locals()
-        SfaFault.__init__(self, 111, faultString, extra)
-    def __str__(self):
-        return repr(self.value)
-        
-class NonexistingCredType(SfaFault):
+class NonExistingRecord(SfaFault):
     def __init__(self, value, extra = None):
         self.value = value
-        faultString = "Non existing record: %(value)s, " % locals()
-        SfaFault.__init__(self, 111, faultString, extra)
+        faultString = "Non exsiting record %(value)s, " % locals()
+        SfaFault.__init__(self, GENICODE.SEARCHFAILED, faultString, extra)
     def __str__(self):
         return repr(self.value)
 
-class NonexistingFile(SfaFault):
-    def __init__(self, value):
+class ExistingRecord(SfaFault):
+    def __init__(self, value, extra = None):
         self.value = value
-        faultString = "Non existing file: %(value)s, " % locals()
-        SfaFault.__init__(self, 111, faultString, extra)
+        faultString = "Existing record: %(value)s, " % locals()
+        SfaFault.__init__(self, GENICODE.REFUSED, faultString, extra)
     def __str__(self):
         return repr(self.value)
 
+    
 class InvalidRPCParams(SfaFault):
-    def __init__(self, value):
+    def __init__(self, value, extra = None):
         self.value = value
         faultString = "Invalid RPC Params: %(value)s, " % locals()
-        SfaFault.__init__(self, 102, faultString, extra)
+        SfaFault.__init__(self, GENICODE.RPCERROR, faultString, extra)
     def __str__(self):
         return repr(self.value)
 
@@ -126,7 +156,7 @@ class ConnectionKeyGIDMismatch(SfaFault):
     def __init__(self, value, extra = None):
         self.value = value
         faultString = "Connection Key GID mismatch: %(value)s" % locals()
-        SfaFault.__init__(self, 102, faultString, extra) 
+        SfaFault.__init__(self, GENICODE.ERROR, faultString, extra) 
     def __str__(self):
         return repr(self.value)
 
@@ -134,7 +164,7 @@ class MissingCallerGID(SfaFault):
     def __init__(self, value, extra = None):
         self.value = value
         faultString = "Missing Caller GID: %(value)s" % locals()
-        SfaFault.__init__(self, 102, faultString, extra) 
+        SfaFault.__init__(self, GENICODE.ERROR, faultString, extra) 
     def __str__(self):
         return repr(self.value)
 
@@ -142,15 +172,15 @@ class RecordNotFound(SfaFault):
     def __init__(self, value, extra = None):
         self.value = value
         faultString = "Record not found: %(value)s" % locals()
-        SfaFault.__init__(self, 102, faultString, extra)
-    #def __str__(self):
-    #    return repr(self.value)
+        SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
+    def __str__(self):
+        return repr(self.value)
 
 class UnknownSfaType(SfaFault):
     def __init__(self, value, extra = None):
         self.value = value
         faultString = "Unknown SFA Type: %(value)s" % locals()
-        SfaFault.__init__(self, 102, faultString, extra)
+        SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
     def __str__(self):
         return repr(self.value)
 
@@ -158,7 +188,7 @@ class MissingAuthority(SfaFault):
     def __init__(self, value, extra = None):
         self.value = value
         faultString = "Missing authority: %(value)s" % locals()
-        SfaFault.__init__(self, 102, faultString, extra)
+        SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
     def __str__(self):
         return repr(self.value)
 
@@ -166,7 +196,7 @@ class PlanetLabRecordDoesNotExist(SfaFault):
     def __init__(self, value, extra = None):
         self.value = value
         faultString = "PlanetLab record does not exist : %(value)s" % locals()
-        SfaFault.__init__(self, 102, faultString, extra)
+        SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
     def __str__(self):
         return repr(self.value)
 
@@ -174,7 +204,7 @@ class PermissionError(SfaFault):
     def __init__(self, value, extra = None):
         self.value = value
         faultString = "Permission error: %(value)s" % locals()
-        SfaFault.__init__(self, 108, faultString, extra)
+        SfaFault.__init__(self, GENICODE.FORBIDDEN, faultString, extra)
     def __str__(self):
         return repr(self.value)
 
@@ -182,7 +212,7 @@ class InsufficientRights(SfaFault):
     def __init__(self, value, extra = None):
         self.value = value
         faultString = "Insufficient rights: %(value)s" % locals()
-        SfaFault.__init__(self, 108, faultString, extra)
+        SfaFault.__init__(self, GENICODE.FORBIDDEN, faultString, extra)
     def __str__(self):
         return repr(self.value)
 
@@ -190,7 +220,7 @@ class MissingDelegateBit(SfaFault):
     def __init__(self, value, extra = None):
         self.value = value
         faultString = "Missing delegate bit: %(value)s" % locals()
-        SfaFault.__init__(self, 108, faultString, extra)
+        SfaFault.__init__(self, GENICODE.FORBIDDEN, faultString, extra)
     def __str__(self):
         return repr(self.value)
 
@@ -198,7 +228,7 @@ class ChildRightsNotSubsetOfParent(SfaFault):
     def __init__(self, value, extra = None):
         self.value = value
         faultString = "Child rights not subset of parent: %(value)s" % locals()
-        SfaFault.__init__(self, 103, faultString, extra)
+        SfaFault.__init__(self, GENICODE.FORBIDDEN, faultString, extra)
     def __str__(self):
         return repr(self.value)
 
@@ -206,7 +236,7 @@ class CertMissingParent(SfaFault):
     def __init__(self, value, extra = None):
         self.value = value
         faultString = "Cert missing parent: %(value)s" % locals()
-        SfaFault.__init__(self, 103, faultString, extra)
+        SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
     def __str__(self):
         return repr(self.value)
 
@@ -214,15 +244,23 @@ class CertNotSignedByParent(SfaFault):
     def __init__(self, value, extra = None):
         self.value = value
         faultString = "Cert not signed by parent: %(value)s" % locals()
-        SfaFault.__init__(self, 103, faultString, extra)
+        SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
     def __str__(self):
         return repr(self.value)
-
+    
+class GidParentHrn(SfaFault):
+    def __init__(self, value, extra = None):
+        self.value = value
+        faultString = "Cert URN is not an extension of its parent: %(value)s" % locals()
+        SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
+    def __str__(self):
+        return repr(self.value)
+        
 class GidInvalidParentHrn(SfaFault):
     def __init__(self, value, extra = None):
         self.value = value
         faultString = "GID invalid parent hrn: %(value)s" % locals()
-        SfaFault.__init__(self, 102, faultString, extra)
+        SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
     def __str__(self):
         return repr(self.value)
 
@@ -230,20 +268,20 @@ class SliverDoesNotExist(SfaFault):
     def __init__(self, value, extra = None):
         self.value = value
         faultString = "Sliver does not exist : %(value)s" % locals()
-        SfaFault.__init__(self, 102, faultString, extra)
+        SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
     def __str__(self):
         return repr(self.value)
 
-class BadRequestHash(xmlrpclib.Fault):
-   def __init__(self, hash = None):
+class BadRequestHash(xmlrpc_client.Fault):
+    def __init__(self, hash = None, extra = None):
         faultString = "bad request hash: " + str(hash)
-        xmlrpclib.Fault.__init__(self, 902, faultString)
+        xmlrpc_client.Fault.__init__(self, GENICODE.ERROR, faultString)
 
 class MissingTrustedRoots(SfaFault):
     def __init__(self, value, extra = None):
         self.value = value
         faultString = "Trusted root directory does not exist: %(value)s" % locals()
-        SfaFault.__init__(self, 102, faultString, extra) 
+        SfaFault.__init__(self, GENICODE.SERVERERROR, faultString, extra) 
     def __str__(self):
         return repr(self.value)
 
@@ -251,7 +289,7 @@ class MissingSfaInfo(SfaFault):
     def __init__(self, value, extra = None):
         self.value = value
         faultString = "Missing information: %(value)s" % locals()
-        SfaFault.__init__(self, 102, faultString, extra) 
+        SfaFault.__init__(self, GENICODE.ERROR, faultString, extra) 
     def __str__(self):
         return repr(self.value)
 
@@ -259,7 +297,75 @@ class InvalidRSpec(SfaFault):
     def __init__(self, value, extra = None):
         self.value = value
         faultString = "Invalid RSpec: %(value)s" % locals()
-        SfaFault.__init__(self, 108, faultString, extra)
+        SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
+    def __str__(self):
+        return repr(self.value)
+
+class InvalidRSpecVersion(SfaFault):
+    def __init__(self, value, extra = None):
+        self.value = value
+        faultString = "Invalid RSpec version: %(value)s" % locals()
+        SfaFault.__init__(self, GENICODE.BADVERSION, faultString, extra)
+    def __str__(self):
+        return repr(self.value)
+
+class UnsupportedRSpecVersion(SfaFault):
+    def __init__(self, value, extra = None):
+        self.value = value
+        faultString = "Unsupported RSpec version: %(value)s" % locals()
+        SfaFault.__init__(self, GENICODE.UNSUPPORTED, faultString, extra)
     def __str__(self):
         return repr(self.value)
 
+class InvalidRSpecElement(SfaFault):
+    def __init__(self, value, extra = None):
+        self.value = value
+        faultString = "Invalid RSpec Element: %(value)s" % locals()
+        SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
+    def __str__(self):
+        return repr(self.value)
+
+class InvalidXML(SfaFault):
+    def __init__(self, value, extra = None):
+        self.value = value
+        faultString = "Invalid XML Document: %(value)s" % locals()
+        SfaFault.__init__(self, GENICODE.BADARGS, faultString, extra)
+    def __str__(self):
+        return repr(self.value)
+
+class AccountNotEnabled(SfaFault):
+    def __init__(self,  extra = None):
+        faultString = "Account Disabled"
+        SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
+    def __str__(self):
+        return repr(self.value)
+
+class CredentialNotVerifiable(SfaFault):
+    def __init__(self, value=None, extra = None):
+        self.value = value
+        faultString = "Unable to verify credential" %locals()
+        if value:
+            faultString += ": %s" % value
+        faultString += ", " 
+        SfaFault.__init__(self, GENICODE.BADARGS, faultString, extra)
+    def __str__(self):
+        return repr(self.value)
+
+class CertExpired(SfaFault):
+    def __init__(self, value, extra=None):
+        self.value = value
+        faultString = "%s cert is expired" % value
+        SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
+  
+class SfatablesRejected(SfaFault):
+    def __init__(self, value, extra=None):
+        self.value =value
+        faultString = "%s rejected by sfatables"
+        SfaFault.__init__(self, GENICODE.FORBIDDEN, faultString, extra) 
+
+class UnsupportedOperation(SfaFault):
+    def __init__(self, value, extra=None):
+        self.value = value
+        faultString = "Unsupported operation: %s" % value
+        SfaFault.__init__(self, GENICODE.UNSUPPORTED, faultString, extra) 
+