Changing get_user_record into a normal object method.
[sfa.git] / sfa / iotlab / iotlabdriver.py
index 04efeef..6cd292a 100644 (file)
@@ -16,7 +16,7 @@ from sfa.iotlab.iotlabaggregate import IotlabAggregate, iotlab_xrn_to_hostname
 from sfa.iotlab.iotlabslices import IotlabSlices
 
 
-from sfa.iotlab.iotlabapi import IotlabTestbedAPI
+from sfa.iotlab.iotlabshell import IotlabShell
 
 
 class IotlabDriver(Driver):
@@ -40,7 +40,7 @@ class IotlabDriver(Driver):
         """
         Driver.__init__(self, config)
         self.config = config
-        self.iotlab_api = IotlabTestbedAPI(config)
+        self.testbed_shell = IotlabShell(config)
         self.cache = None
 
     def augment_records_with_testbed_info(self, record_list):
@@ -80,12 +80,25 @@ class IotlabDriver(Driver):
 
         try:
             for record in record_list:
+
+                if str(record['type']) == 'node':
+                    # look for node info using GetNodes
+                    # the record is about one node only
+                    filter_dict = {'hrn': [record['hrn']]}
+                    node_info = self.testbed_shell.GetNodes(filter_dict)
+                    # the node_info is about one node only, but it is formatted
+                    # as a list
+                    record.update(node_info[0])
+                    logger.debug("IOTLABDRIVER.PY \t \
+                                  fill_record_info NODE" % (record))
+
                 #If the record is a SFA slice record, then add information
                 #about the user of this slice. This kind of
                 #information is in the Iotlab's DB.
                 if str(record['type']) == 'slice':
-                    if 'reg_researchers' in record and \
-                        isinstance(record['reg_researchers'], list):
+                    if 'reg_researchers' in record and isinstance(record
+                                                            ['reg_researchers'],
+                                                            list):
                         record['reg_researchers'] = \
                             record['reg_researchers'][0].__dict__
                         record.update(
@@ -104,7 +117,7 @@ class IotlabDriver(Driver):
                              'key_ids': ''})
 
                     #Get iotlab slice record and oar job id if any.
-                    recslice_list = self.iotlab_api.GetSlices(
+                    recslice_list = self.testbed_shell.GetSlices(
                         slice_filter=str(record['hrn']),
                         slice_filter_type='slice_hrn')
 
@@ -118,9 +131,9 @@ class IotlabDriver(Driver):
                             fill_record_info oar_job_id %s "
                                          % (rec['oar_job_id']))
 
-                            record['node_ids'] = [self.iotlab_api.root_auth +
-                                                  hostname for hostname in
-                                                  rec['node_ids']]
+                            record['node_ids'] = [self.testbed_shell.root_auth +
+                                                  '.' + hostname for hostname
+                                                  in rec['node_ids']]
                     except KeyError:
                         pass
 
@@ -132,7 +145,7 @@ class IotlabDriver(Driver):
                     #The record is a SFA user record.
                     #Get the information about his slice from Iotlab's DB
                     #and add it to the user record.
-                    recslice_list = self.iotlab_api.GetSlices(
+                    recslice_list = self.testbed_shell.GetSlices(
                         slice_filter=record['record_id'],
                         slice_filter_type='record_id_user')
 
@@ -165,7 +178,7 @@ class IotlabDriver(Driver):
                                      'hrn': recslice_list[0]['hrn']})
 
                     #GetPersons takes [] as filters
-                    user_iotlab = self.iotlab_api.GetPersons([record])
+                    user_iotlab = self.testbed_shell.GetPersons([record])
 
                     record.update(user_iotlab[0])
                     #For client_helper.py compatibility
@@ -201,7 +214,7 @@ class IotlabDriver(Driver):
         """
 
         #First get the slice with the slice hrn
-        slice_list = self.iotlab_api.GetSlices(slice_filter=slice_hrn,
+        slice_list = self.testbed_shell.GetSlices(slice_filter=slice_hrn,
                                                slice_filter_type='slice_hrn')
 
         if len(slice_list) == 0:
@@ -214,7 +227,7 @@ class IotlabDriver(Driver):
         slice_nodes_list = []
         slice_nodes_list = one_slice['node_ids']
         #Get all the corresponding nodes details
-        nodes_all = self.iotlab_api.GetNodes(
+        nodes_all = self.testbed_shell.GetNodes(
             {'hostname': slice_nodes_list},
             ['node_id', 'hostname', 'site', 'boot_state'])
         nodeall_byhostname = dict([(one_node['hostname'], one_node)
@@ -284,8 +297,7 @@ class IotlabDriver(Driver):
                          % (resources, res))
             return result
 
-    @staticmethod
-    def get_user_record(hrn):
+    def get_user_record(self, hrn):
         """
 
         Returns the user record based on the hrn from the SFA DB .
@@ -352,7 +364,7 @@ class IotlabDriver(Driver):
 
             if not lease.get('lease_id'):
                 if get_authority(lease['component_id']) == \
-                        self.iotlab_api.root_auth:
+                        self.testbed_shell.root_auth:
                     single_requested_lease['hostname'] = \
                         iotlab_xrn_to_hostname(\
                             lease.get('component_id').strip())
@@ -363,7 +375,7 @@ class IotlabDriver(Driver):
                     #the lease to the requested leases list
                     duration_in_seconds = \
                         int(single_requested_lease['duration'])
-                    if duration_in_seconds >= self.iotlab_api.GetMinExperimentDurationInSec():
+                    if duration_in_seconds >= self.testbed_shell.GetMinExperimentDurationInGranularity():
                         requested_lease_list.append(single_requested_lease)
 
         return requested_lease_list
@@ -383,27 +395,27 @@ class IotlabDriver(Driver):
 
         """
 
-        requested_job_dict = {}
+        requested_xp_dict = {}
         for lease in requested_lease_list:
 
             #In case it is an asap experiment start_time is empty
             if lease['start_time'] == '':
                 lease['start_time'] = '0'
 
-            if lease['start_time'] not in requested_job_dict:
+            if lease['start_time'] not in requested_xp_dict:
                 if isinstance(lease['hostname'], str):
                     lease['hostname'] = [lease['hostname']]
 
-                requested_job_dict[lease['start_time']] = lease
+                requested_xp_dict[lease['start_time']] = lease
 
             else:
-                job_lease = requested_job_dict[lease['start_time']]
+                job_lease = requested_xp_dict[lease['start_time']]
                 if lease['duration'] == job_lease['duration']:
                     job_lease['hostname'].append(lease['hostname'])
 
-        return requested_job_dict
+        return requested_xp_dict
 
-    def _process_requested_jobs(self, rspec):
+    def _process_requested_xp_dict(self, rspec):
         """
         Turns the requested leases and information into a dictionary
             of requested jobs, grouped by starting time.
@@ -414,13 +426,13 @@ class IotlabDriver(Driver):
 
         """
         requested_lease_list = self._get_requested_leases_list(rspec)
-        logger.debug("IOTLABDRIVER _process_requested_jobs \
+        logger.debug("IOTLABDRIVER _process_requested_xp_dict \
             requested_lease_list  %s" % (requested_lease_list))
-        job_dict = self._group_leases_by_start_time(requested_lease_list)
-        logger.debug("IOTLABDRIVER _process_requested_jobs  job_dict\
-        %s" % (job_dict))
+        xp_dict = self._group_leases_by_start_time(requested_lease_list)
+        logger.debug("IOTLABDRIVER _process_requested_xp_dict  xp_dict\
+        %s" % (xp_dict))
 
-        return job_dict
+        return xp_dict
 
     def create_sliver(self, slice_urn, slice_hrn, creds, rspec_string,
                       users, options):
@@ -490,7 +502,7 @@ class IotlabDriver(Driver):
 
         #requested_slivers = [node.get('component_id') \
                     #for node in rspec.version.get_nodes_with_slivers()\
-                    #if node.get('authority_id') is self.iotlab_api.root_auth]
+                    #if node.get('authority_id') is self.testbed_shell.root_auth]
         #l = [ node for node in rspec.version.get_nodes_with_slivers() ]
         #logger.debug("SLADRIVER \tcreate_sliver requested_slivers \
                                     #requested_slivers %s  listnodes %s" \
@@ -498,14 +510,14 @@ class IotlabDriver(Driver):
         #verify_slice_nodes returns nodes, but unused here. Removed SA 13/08/12.
         #slices.verify_slice_nodes(sfa_slice, requested_slivers, peer)
 
-        requested_job_dict = self._process_requested_jobs(rspec)
+        requested_xp_dict = self._process_requested_xp_dict(rspec)
 
-        logger.debug("IOTLABDRIVER.PY \tcreate_sliver  requested_job_dict %s "
-                     % (requested_job_dict))
+        logger.debug("IOTLABDRIVER.PY \tcreate_sliver  requested_xp_dict %s "
+                     % (requested_xp_dict))
         #verify_slice_leases returns the leases , but the return value is unused
         #here. Removed SA 13/08/12
         slices.verify_slice_leases(sfa_slice,
-                                   requested_job_dict, peer)
+                                   requested_xp_dict, peer)
 
         return aggregate.get_rspec(slice_xrn=slice_urn,
                                    login=sfa_slice['login'],
@@ -533,7 +545,7 @@ class IotlabDriver(Driver):
              delete_sliver .
         """
 
-        sfa_slice_list = self.iotlab_api.GetSlices(
+        sfa_slice_list = self.testbed_shell.GetSlices(
             slice_filter=slice_hrn,
             slice_filter_type='slice_hrn')
 
@@ -551,7 +563,7 @@ class IotlabDriver(Driver):
             logger.debug("IOTLABDRIVER.PY delete_sliver peer %s \
                 \r\n \t sfa_slice %s " % (peer, sfa_slice))
             try:
-                self.iotlab_api.DeleteSliceFromNodes(sfa_slice)
+                self.testbed_shell.DeleteSliceFromNodes(sfa_slice)
                 return True
             except:
                 return False
@@ -646,7 +658,7 @@ class IotlabDriver(Driver):
 
         # get data from db
 
-        slices = self.iotlab_api.GetSlices()
+        slices = self.testbed_shell.GetSlices()
         logger.debug("IOTLABDRIVER.PY \tlist_slices hrn %s \r\n \r\n"
                      % (slices))
         slice_hrns = [iotlab_slice['hrn'] for iotlab_slice in slices]
@@ -687,27 +699,24 @@ class IotlabDriver(Driver):
 
     def update(self, old_sfa_record, new_sfa_record, hrn, new_key):
         """
-        No site or node record update allowed in Iotlab.
-            The only modifications authorized here are key deletion/addition
-            on an existing user and password change.
-            On an existing user, CAN NOT BE MODIFIED:
-            'first_name', 'last_name', 'email'
-            DOES NOT EXIST IN SENSLAB:
-            'phone', 'url', 'bio','title', 'accepted_aup',
-            A slice is bound to its user, so modifying the user's ssh key should
-            modify the slice's GID after an import procedure.
+        No site or node record update allowed in Iotlab. The only modifications
+        authorized here are key deletion/addition on an existing user and
+        password change. On an existing user, CAN NOT BE MODIFIED: 'first_name',
+        'last_name', 'email'. DOES NOT EXIST IN SENSLAB: 'phone', 'url', 'bio',
+        'title', 'accepted_aup'. A slice is bound to its user, so modifying the
+        user's ssh key should nmodify the slice's GID after an import procedure.
 
         :param old_sfa_record: what is in the db for this hrn
-        :param new_sfa_record: what was passed to the Update call
+        :param new_sfa_record: what was passed to the update call
         :param new_key: the new user's public key
         :param hrn: the user's sfa hrn
-        :type old_sfa_record: dictionary
-        :type new_sfa_record: dictionary
-        :type pub_key: string
+        :type old_sfa_record: dict
+        :type new_sfa_record: dict
+        :type new_key: string
         :type hrn: string
 
-         TODO: needs review
-        .. seealso::: update in driver.py.
+        TODO: needs review
+        .. seealso:: update in driver.py.
 
         """
         pointer = old_sfa_record['pointer']
@@ -726,11 +735,11 @@ class IotlabDriver(Driver):
 
             if new_key:
                 # must check this key against the previous one if it exists
-                persons = self.iotlab_api.GetPersons([old_sfa_record])
+                persons = self.testbed_shell.GetPersons([old_sfa_record])
                 person = persons[0]
                 keys = [person['pkey']]
                 #Get all the person's keys
-                keys_dict = self.iotlab_api.GetKeys(keys)
+                keys_dict = self.testbed_shell.GetKeys(keys)
 
                 # Delete all stale keys, meaning the user has only one key
                 #at a time
@@ -743,8 +752,8 @@ class IotlabDriver(Driver):
                 else:
                     #remove all the other keys
                     for key in keys_dict:
-                        self.iotlab_api.DeleteKey(person, key)
-                    self.iotlab_api.AddPersonKey(
+                        self.testbed_shell.DeleteKey(person, key)
+                    self.testbed_shell.AddPersonKey(
                         person, {'sshPublicKey': person['pkey']},
                         {'sshPublicKey': new_key})
         return True
@@ -752,9 +761,8 @@ class IotlabDriver(Driver):
     def remove(self, sfa_record):
         """
 
-        Removes users only. Mark the user as disabled in
-            LDAP. The user and his slice are then deleted from the
-            db by running an import on the registry.
+        Removes users only. Mark the user as disabled in LDAP. The user and his
+        slice are then deleted from the db by running an import on the registry.
 
         :param sfa_record: record is the existing sfa record in the db
         :type sfa_record: dict
@@ -762,6 +770,7 @@ class IotlabDriver(Driver):
         ..warning::As fas as the slice is concerned, here only the leases are
             removed from the slice. The slice is record itself is not removed
             from the db.
+
         TODO: needs review
 
         TODO : REMOVE SLICE FROM THE DB AS WELL? SA 14/05/2013,
@@ -773,16 +782,16 @@ class IotlabDriver(Driver):
         if sfa_record_type == 'user':
 
             #get user from iotlab ldap
-            person = self.iotlab_api.GetPersons(sfa_record)
+            person = self.testbed_shell.GetPersons(sfa_record)
             #No registering at a given site in Iotlab.
             #Once registered to the LDAP, all iotlab sites are
             #accesible.
             if person:
                 #Mark account as disabled in ldap
-                return self.iotlab_api.DeletePerson(sfa_record)
+                return self.testbed_shell.DeletePerson(sfa_record)
 
         elif sfa_record_type == 'slice':
-            if self.iotlab_api.GetSlices(slice_filter=hrn,
+            if self.testbed_shell.GetSlices(slice_filter=hrn,
                                          slice_filter_type='slice_hrn'):
-                ret = self.iotlab_api.DeleteSlice(sfa_record)
+                ret = self.testbed_shell.DeleteSlice(sfa_record)
             return True