Removing GeniClient
[sfa.git] / sfa / util / geniclient.py
index 00553ff..d591dac 100644 (file)
@@ -9,55 +9,11 @@
 ### $Id$
 ### $URL$
 
-import xmlrpclib
-
+from sfa.trust.certificate import *
 from sfa.trust.gid import *
 from sfa.trust.credential import *
 from sfa.util.record import *
-from sfa.util.geniticket import *
-
-##
-# ServerException, ExceptionUnmarshaller
-#
-# Used to convert server exception strings back to an exception.
-#    from usenet, Raghuram Devarakonda
-
-class ServerException(Exception):
-    pass
-
-class ExceptionUnmarshaller(xmlrpclib.Unmarshaller):
-    def close(self):
-        try:
-            return xmlrpclib.Unmarshaller.close(self)
-        except xmlrpclib.Fault, e:
-            raise ServerException(e.faultString)
-
-##
-# GeniTransport
-#
-# A transport for XMLRPC that works on top of HTTPS
-
-class GeniTransport(xmlrpclib.Transport):
-    key_file = None
-    cert_file = None
-    def make_connection(self, host):
-        # create a HTTPS connection object from a host descriptor
-        # host may be a string, or a (host, x509-dict) tuple
-        import httplib
-        host, extra_headers, x509 = self.get_host_info(host)
-        try:
-            HTTPS = httplib.HTTPS()
-        except AttributeError:
-            raise NotImplementedError(
-                "your version of httplib doesn't support HTTPS"
-                )
-        else:
-            return httplib.HTTPS(host, None, key_file=self.key_file, cert_file=self.cert_file) #**(x509 or {}))
-
-    def getparser(self):
-        unmarshaller = ExceptionUnmarshaller()
-        parser = xmlrpclib.ExpatParser(unmarshaller)
-        return parser, unmarshaller
+from sfa.util.sfaticket import SfaTicket
 
 ##
 # The GeniClient class provides stubs for executing Geni operations. A given
@@ -69,7 +25,7 @@ class GeniTransport(xmlrpclib.Transport):
 # public key that is containing in the GID that the client is providing for
 # those functions that take a GID.
 
-class GeniClient():
+class GeniClient:
     ##
     # Create a new GeniClient object.
     #
@@ -77,15 +33,24 @@ class GeniClient():
     # @param key_file = private key file of client
     # @param cert_file = x.509 cert containing the client's public key. This
     #      could be a GID certificate, or any x.509 cert.
+    # @param protocol The ORPC protocol to use. Can be "soap" or "xmlrpc"
+
+    def __init__(self, url, key_file, cert_file, protocol="xmlrpc"):
+        self.url = url
+        self.key_file = key_file
+        self.cert_file = cert_file
+        self.key = Keypair(filename = self.key_file)
+    
+
+        if (protocol=="xmlrpc"):
+            import xmlrpcprotocol  
+            self.server = xmlrpcprotocol.get_server(self.url, self.key_file, self.cert_file)
+        elif (protocol=="soap"):
+            import soapprotocol
+            self.server = soapprotocol.get_server(self.url, self.key_file, self.cert_file)
+        else:
+            raise Exception("Attempted use of undefined protocol %s"%protocol)
 
-    def __init__(self, url, key_file, cert_file):
-       self.url = url
-       self.key_file = key_file
-       self.cert_file = cert_file
-       self.transport = GeniTransport()
-       self.transport.key_file = self.key_file
-       self.transport.cert_file = self.cert_file
-       self.server = xmlrpclib.ServerProxy(self.url, self.transport, allow_none=True)
 
     # -------------------------------------------------------------------------
     # Registry Interface
@@ -116,13 +81,18 @@ class GeniClient():
     #
     # @return a GID object
 
-    def get_gid(self, name):
-       gid_str_list = self.server.get_gid(name)
-       gid_list = []
-       for str in gid_str_list:
-           gid_list.append(GID(string=str))
-       return gid_list
+    #def get_gid(self, name):
+    #   gid_str_list = self.server.get_gid(name)
+    #   gid_list = []
+    #   for str in gid_str_list:
+    #       gid_list.append(GID(string=str))
+    #  return gid_list
+
 
+    def get_gid(self, cert, hrn, type, request_hash):
+        cert_string = cert.save_to_string(save_parents=True)
+        gid_str = self.server.get_gid(cert_string, hrn, type, request_hash)
+        return GID(string=gid_str)
     ##
     # Get_self_credential a degenerate version of get_credential used by a
     # client to get his initial credential when he doesn't have one. This is
@@ -168,8 +138,8 @@ class GeniClient():
     #
     # @return list of record objects
 
-    def list(self, cred, auth_hrn):
-        result_dict_list = self.server.list(cred.save_to_string(save_parents=True), auth_hrn)
+    def list(self, cred, auth_hrn, caller_cred=None):
+        result_dict_list = self.server.list(cred.save_to_string(save_parents=True), auth_hrn, caller_cred)
         result_rec_list = []
         for dict in result_dict_list:
              result_rec_list.append(GeniRecord(dict=dict))
@@ -183,14 +153,27 @@ class GeniClient():
     #
     #
     # @param cred credential object specifying rights of the caller
-    # @return record to register
+    # @param record to register
     #
     # @return GID object for the newly-registered record
 
-    def register(self, cred, record):
-        gid_str = self.server.register(cred.save_to_string(save_parents=True), record.as_dict())
+    def register(self, cred, record, caller_cred=None):
+        gid_str = self.server.register(cred.save_to_string(save_parents=True), record.as_dict(), caller_cred)
         return GID(string = gid_str)
 
+    
+    ##
+    # Register a peer object with the registry. 
+    #
+    #
+    # @param cred credential object specifying rights of the caller
+    # @param record to register
+    #
+    # @return GID object for the newly-registered record
+
+    def register_peer_object(self, cred, record, caller_cred=None):
+        return self.server.register_peer_object(cred.save_to_string(save_parents=True), record, caller_cred)
+
     ##
     # Remove an object from the registry. If the object represents a PLC object,
     # then the PLC records will also be removed.
@@ -199,8 +182,19 @@ class GeniClient():
     # @param type
     # @param hrn
 
-    def remove(self, cred, type, hrn):
-        result = self.server.remove(cred.save_to_string(save_parents=True), type, hrn)
+    def remove(self, cred, type, hrn, caller_cred=None):
+        return self.server.remove(cred.save_to_string(save_parents=True), type, hrn, caller_cred)
+
+    ##
+    # Remove a peer object from the registry. If the object represents a PLC object,
+    # then the PLC records will also be removed.
+    #
+    # @param cred credential object specifying rights of the caller
+    # @param type
+    # @param hrn
+
+    def remove_peer_object(self, cred, record, caller_cred=None):
+        result = self.server.remove_peer_object(cred.save_to_string(save_parents=True), record, caller_cred)
         return result
 
     ##
@@ -212,8 +206,8 @@ class GeniClient():
     # @param cred credential object specifying rights of the caller
     # @param name human readable name of object
 
-    def resolve(self, cred, name):
-        result_dict_list = self.server.resolve(cred.save_to_string(save_parents=True), name)
+    def resolve(self, cred, name, caller_cred=None):
+        result_dict_list = self.server.resolve(cred.save_to_string(save_parents=True), name, caller_cred)
         result_rec_list = []
         for dict in result_dict_list:
             if dict['type'] in ['authority']:
@@ -238,8 +232,8 @@ class GeniClient():
     # @param cred credential object specifying rights of the caller
     # @param record a record object to be updated
 
-    def update(self, cred, record):
-        result = self.server.update(cred.save_to_string(save_parents=True), record.as_dict())
+    def update(self, cred, record, caller_cred=None):
+        result = self.server.update(cred.save_to_string(save_parents=True), record.as_dict(), caller_cred)
         return result
 
 
@@ -252,14 +246,18 @@ class GeniClient():
     # @param cred a credential
     # @param hrn slice hrn
 
-    def get_resources(self, cred, hrn=None):
-        result = self.server.get_resources(cred.save_to_string(save_parents=True), hrn)
+    def get_resources(self, cred, hrn=None, caller_cred=None):
+        result = self.server.get_resources(cred.save_to_string(save_parents=True), hrn, caller_cred)
         return result
 
     def get_aggregates(self, cred, hrn=None):
-        result = self.server.get_resources(cred.save_to_string(save_parents=True), hrn)
+        result = self.server.get_aggregates(cred.save_to_string(save_parents=True), hrn)
         return result
 
+    def get_registries(self, cred, hrn=None):
+       result = self.server.get_registries(cred.save_to_string(save_parents=True), hrn)
+       return result
+
     ## get policy
     #
     # @param cred a credential
@@ -273,8 +271,8 @@ class GeniClient():
     # @param cred a credential
     # @param rspec resource specification defining how to instantiate the slice
     
-    def create_slice(self, cred, hrn, rspec):
-        result = self.server.create_slice(cred.save_to_string(save_parents=True), hrn, rspec)
+    def create_slice(self, cred, hrn, rspec, caller_cred=None):
+        result = self.server.create_slice(cred.save_to_string(save_parents=True), hrn, rspec, caller_cred)
         return result
 
 
@@ -282,8 +280,8 @@ class GeniClient():
     #
     # @param cred a credential
     # @param hrn slice to delete
-    def delete_slice(self, cred, hrn):
-        result = self.server.delete_slice(cred.save_to_string(save_parents=True), hrn)
+    def delete_slice(self, cred, hrn, caller_cred=None):
+        result = self.server.delete_slice(cred.save_to_string(save_parents=True), hrn, caller_cred)
         return result    
 
     # ------------------------------------------------------------------------
@@ -326,8 +324,8 @@ class GeniClient():
     # @param cred a credential identifying the caller (callerGID) and the slice
     #     (objectGID)
 
-    def delete_slice(self, cred, hrn):
-        result = self.server.delete_slice(cred.save_to_string(save_parents=True), hrn)
+    def delete_slice(self, cred, hrn, caller_cred=None):
+        result = self.server.delete_slice(cred.save_to_string(save_parents=True), hrn, caller_cred)
         return result
 
     ##
@@ -358,7 +356,7 @@ class GeniClient():
 
     def get_ticket(self, cred, name, rspec):
         ticket_str = self.server.get_ticket(cred.save_to_string(save_parents=True), name, rspec)
-        ticket = Ticket(string=ticket_str)
+        ticket = SfaTicket(string=ticket_str)
         return ticket
 
     ##
@@ -379,3 +377,6 @@ class GeniClient():
         return result
 
 
+    def remove_remote_object(self, cred, hrn, record):
+        result = self.server.remove_remote_object(cred.save_to_string(save_parents=True), hrn, record)
+        return result