sfi myslice uses annotation for authentication when uploadong onto manifold
[sfa.git] / sfa / client / manifolduploader.py
index 0c9b1be..f8ca0f9 100755 (executable)
@@ -7,17 +7,21 @@
 # install a separate tool; so duplicating this code is suboptimal in
 # terms of code sharing but acceptable for hopefully easier use
 #
+# As of Nov. 2013, the signature for the forward API call has changed
+# and now requires authentication to be passed as an annotation
+# We take this chance to make things much simpler here by dropping
+# support for multiple API versions/flavours
+#
 # As of April 2013, manifold is moving from old-fashioned API known as
 # v1, that offers an AddCredential API call, towards a new API v2 that
 # manages credentials with the same set of Get/Update calls as other
 # objects
 # 
-# Since this code targets the future we favour v2, however in case
-# this won't work the v1 way is attempted too
-#
 
-## this for now points at demo.myslice.info, but sounds like a
-## better default for the long run
+# mostly this is intended to be used through 'sfi myslice'
+# so the defaults below are of no real importance
+# this for now points at demo.myslice.info, but sounds like a
+# better default for the long run
 DEFAULT_URL = "http://myslice.onelab.eu:7080"
 DEFAULT_PLATFORM = 'ple'
 
@@ -29,87 +33,101 @@ class ManifoldUploader:
 
     # platform is a name internal to the manifold deployment, 
     # that maps to a testbed, like e.g. 'ple'
-    def __init__ (self, url=None, platform=None, username=None, password=None, debug=False):
+    def __init__ (self, logger, url=None, platform=None, username=None, password=None, ):
         self._url=url
         self._platform=platform
         self._username=username
         self._password=password
-        self.debug=debug
+        self.logger=logger
+        self._proxy=None
 
     def username (self):
-        if not self._username: 
+        if not self._username:
             self._username=raw_input("Enter your manifold username: ")
-        return self._username            
+        return self._username
 
     def password (self):
-        if not self._password: 
+        if not self._password:
             username=self.username()
             self._password=getpass.getpass("Enter password for manifold user %s: "%username)
-        return self._password            
+        return self._password
 
     def platform (self):
-        if not self._platform: 
+        if not self._platform:
             self._platform=raw_input("Enter your manifold platform [%s]: "%DEFAULT_PLATFORM)
             if self._platform.strip()=="": self._platform = DEFAULT_PLATFORM
-        return self._platform            
+        return self._platform
 
     def url (self):
-        if not self._url: 
+        if not self._url:
             self._url=raw_input("Enter the URL for your manifold API [%s]: "%DEFAULT_URL)
             if self._url.strip()=="": self._url = DEFAULT_URL
-        return self._url            
+        return self._url
+
+    def prompt_all(self):
+        self.username(); self.password(); self.platform(); self.url()
+
+    # looks like the current implementation of manifold server
+    # won't be happy with several calls issued in the same session
+    # so we do not cache this one
+    def proxy (self):
+#        if not self._proxy:
+#            url=self.url()
+#            self.logger.info("Connecting manifold url %s"%url)
+#            self._proxy = xmlrpclib.ServerProxy(url, allow_none = True)
+#        return self._proxy
+        url=self.url()
+        self.logger.debug("Connecting manifold url %s"%url)
+        return xmlrpclib.ServerProxy(url, allow_none = True)
 
     # does the job for one credential
-    # expects the credential (string) and an optional filename (for messaging)
+    # expects the credential (string) and an optional message (e.g. hrn) for reporting
     # return True upon success and False otherwise
-    def upload (self, delegated_credential, filename=None):
-        url=self.url()
+    def upload (self, delegated_credential, message=None):
         platform=self.platform()
         username=self.username()
         password=self.password()
         auth = {'AuthMethod': 'password', 'Username': username, 'AuthString': password}
+        if not message: message=""
 
         try:
-            manifold = xmlrpclib.Server(url, allow_none = 1)
+            manifold=self.proxy()
             # the code for a V2 interface
-            query= { 'action':       'update',
-                     'fact_table':   'local:account',
-                     'filters':      [ ['platform', '=', platform] ] ,
-                     'params':       {'credential': delegated_credential, },
+            query = { 'action':     'update',
+                     'object':     'local:account',
+                     'filters':    [ ['platform', '=', platform] ] ,
+                     'params':     {'credential': delegated_credential, },
                      }
+            annotation = {'authentication': auth, }
+            # in principle the xmlrpc call should not raise an exception
+            # but fill in error code and messages instead
+            # however this is only theoretical so let's be on the safe side
             try:
-                retcod2=manifold.Update (auth, query)
+                self.logger.debug("Using new v2 method forward+annotation@%s %s"%(platform,message))
+                retcod2=manifold.forward (query, annotation)
             except Exception,e:
                 # xxx we need a constant constant for UNKNOWN, how about using 1
                 MANIFOLD_UNKNOWN=1
-                retcod2={'code':MANIFOLD_UNKNOWN,'output':"%s"%e}
+                retcod2={'code':MANIFOLD_UNKNOWN,'description':"%s"%e}
             if retcod2['code']==0:
-                if filename: print filename,
-                print 'v2 upload OK'
-                return True
-            #print delegated_credential, "upload failed,",retcod['output'], \
-            #    "with code",retcod['code']
-            # the code for V1
-            try:
-                retcod1=manifold.AddCredential(auth, delegated_credential, platform)
-            except Exception,e:
-                retcod1=e
-            if retcod1==1:
-                if filename: print filename,
-                print 'v1 upload OK'
+                info=""
+                if message: info += message+" "
+                info += 'v2 upload OK'
+                self.logger.info(info)
                 return True
             # everything has failed, let's report
-            if filename: print "Could not upload",filename
-            else: print "Could not upload credential"
-            print "  V2 Update returned code",retcod2['code'],"and error",retcod2['output']
-            print "  V1 AddCredential returned code",retcod1,"(expected 1)"
+            self.logger.error("Could not upload %s"%(message if message else "credential"))
+            self.logger.info("  V2 Update returned code %s and error >>%s<<"%(retcod2['code'],retcod2['description']))
+            self.logger.debug("****** full retcod2")
+            for (k,v) in retcod2.items(): self.logger.debug("**** %s: %s"%(k,v))
             return False
         except Exception, e:
-            if filename: print "Could not upload",filename,e
-            else: print "Could not upload credential",e
-            if self.debug:
+            if message: self.logger.error("Could not upload %s %s"%(message,e))
+            else:        self.logger.error("Could not upload credential %s"%e)
+            if self.logger.debugEnabled():
                 import traceback
                 traceback.print_exc()
+            return False
 
 ### this is mainly for unit testing this class but can come in handy as well
 def main ():
@@ -125,17 +143,21 @@ def main ():
                          help='the manifold username')
     parser.add_argument ('-P','--password',dest='password',action='store',default=None,
                          help='the manifold password')
-    parser.add_argument ('-d','--debug',dest='debug',action='store_true',default=False,
-                         help='turn on debug mode')
+    parser.add_argument ('-v','--verbose',dest='verbose',action='count',default=0,
+                         help='more and more verbose')
     args = parser.parse_args ()
     
+    from sfa.util.sfalogging import sfi_logger
+    sfi_logger.enable_console()
+    sfi_logger.setLevelFromOptVerbose(args.verbose)
     uploader = ManifoldUploader (url=args.url, platform=args.platform,
                                  username=args.username, password=args.password,
-                                 debug=args.debug)
+                                 logger=sfi_logger)
+
     for filename in args.credential_files:
         with file(filename) as f:
             result=uploader.upload (f.read(),filename)
-            if args.debug: print '... result',result
+            sfi_logger.info('... result=%s'%result)
 
 if __name__ == '__main__':
     main()