python3 - 2to3 + miscell obvious tweaks
[sfa.git] / sfa / iotlab / iotlabshell.py
index c51a7d6..605fa98 100644 (file)
@@ -6,7 +6,7 @@ from iotlabcli import auth
 from iotlabcli import rest
 from iotlabcli import helpers
 from iotlabcli import experiment
-from urllib2 import HTTPError
+from urllib.error import HTTPError
 
 
 class IotLABShell(object):
@@ -52,7 +52,7 @@ class IotLABShell(object):
             nodes_dict[node['network_address']] = node
         return nodes_dict
 
-    def get_users(self):
+    def get_users(self, email=None):
         """
         Get all LDAP users
         :returns: users with LDAP attributes
@@ -79,7 +79,10 @@ class IotLABShell(object):
         logger.warning("iotlashell get_users")
         users_dict = {}
         try:
-            users = self.api.method('admin/users')
+            if email:
+                users = self.api.method('admin/users?email=%s' % email)
+            else:
+                users = self.api.method('admin/users')
         except HTTPError as err:
             logger.warning("iotlashell get_users error %s" % err.reason)
             return {'error': err.reason}
@@ -94,6 +97,7 @@ class IotLABShell(object):
         """
         # pylint:disable=W0212,R0913,E1123
         logger.warning("iotlashell reserve_nodes")
+        logger.info("login=%s, exp_name=%s, nodes_list=%s, start_time=%s, duration=%s" % (login, exp_name, nodes_list, start_time, duration))
         exp_file = helpers.FilesDict()
         _experiment = experiment._Experiment(exp_name, duration, start_time)
         _experiment.type = 'physical'
@@ -105,6 +109,7 @@ class IotLABShell(object):
                                    files=exp_file)
         except HTTPError as err:
             logger.warning("iotlashell reserve_nodes error %s" % err.reason)
+            logger.error(err)
             return {'error': err.reason}
 
     def get_reserved_nodes(self):
@@ -138,11 +143,6 @@ class IotLABShell(object):
                            err.reason)
             return {'error': err.reason}
         for exp in experiments['items']:
-            # BUG IN OAR REST API : job with reservation didn't return
-            # resources attribute list. We use another request for
-            # finding job resources
-            exp_nodes = self.api.method('admin/experiments/%d' % exp['id'])
-            exp['resources'] = exp_nodes['nodes']
             # BUG ASAP jobs without date information
             if exp['date'] == "as soon as possible":
                 exp['date'] = 0
@@ -152,19 +152,40 @@ class IotLABShell(object):
     def add_user(self, slice_user):
         """
         Add LDAP user
+        {
+            "firstName":"loic",
+            "lastName":"test",
+            "email":"loic.test@lip6.fr",
+            "organization":"SFA",
+            "city":"To be defined",
+            "country":"To be defined",
+            "motivations":"SFA federation",
+            "category":"Academic",
+            "type": "SA",
+            "sshPublicKey": "ssh-rsa AAAAB3Nz..."
+        }       
         """
+        
         # pylint:disable=E1123
         logger.warning("iotlashell add_user")
+        logger.warning("slice_user: %s" % slice_user)
+        if 'urn' in slice_user:
+            organization = slice_user['urn']
+        else:
+            organization = "SFA federation"
         # single account creation
         user = {"type": "SA",
                 "city": "To be defined",
                 "country": "To be defined",
-                "motivations": "SFA federation"}
+                "motivations": "SFA federation",
+                "organization": organization,
+                "category":"Academic"
+                }
         email = slice_user['email']
         user['email'] = email
         user['sshPublicKey'] = slice_user['keys'][0]
         # ex : onelab.inria
-        user['structure'] = slice_user['slice_record']['authority']
+        user['structure'] = slice_user['urn'].split('+')[1].replace(':','.')
         email = (email.split('@'))[0]
         user['firstName'] = email.split('.')[0]
         try: