def get_user_info(user_id): user_id = user_id.replace('user/', '') LOG.log( TRACE, "[usermgnt.data.mF2C.data] [get_user_info] Getting info from user " + user_id + " ...") return cimi.get_resource_by_id("user/" + user_id)
def __up_policies(): global message try: LOG.log( TRACE, "[usermgnt.modules.policies] [__up_policies] Checking policies ..." ) # get current profile user_profile = data_adapter.get_current_user_profile() if user_profile is None: LOG.error( '[usermgnt.modules.policies] [__up_policies] user_profile not found / error' ) elif user_profile == -1: LOG.warning( '[usermgnt.modules.policies] [__up_policies] user_profile not found' ) else: LOG.log( TRACE, '[usermgnt.modules.policies] [__up_policies] user_profile found. checking values ...' ) if user_profile['resource_contributor']: message = message + "ALLOWED: resource_contributor is set to TRUE; " return True message = message + "NOT ALLOWED: resource_contributor is set to FALSE; " except: LOG.exception('[usermgnt.modules.policies] [__up_policies] Exception') return False
def __print_records(db): LOG.debug( '[usermgnt.data.standalone.db] [__print_records] Retrieving records from db...' ) records = db() for r in records: LOG.debug("db> " + str(r))
def update_resource(resource_id, content): try: LOG.log( TRACE, "[usermgnt.data.mF2C.cimi] [update_resource] (1) Updating resource [" + resource_id + "] with content [" + str(content) + "] ... ") # complete map and update resource content.update(common_update_map_fields()) LOG.debug( "[usermgnt.data.mF2C.cimi] [update_resource] (2) Updating resource [" + resource_id + "] with content [" + str(content) + "] ... ") res = requests.put(config.dic['CIMI_URL'] + '/' + resource_id, headers=CIMI_HEADER, verify=False, json=content) LOG.log( TRACE, "[usermgnt.data.mF2C.cimi] [update_resource] response: " + str(res) + ", " + str(res.json())) if res.status_code == 200: return get_resource_by_id(resource_id) LOG.error( "[usermgnt.data.mF2C.cimi] [update_resource] Request failed: " + str(res.status_code) + "; Returning None ...") except: LOG.exception( "[usermgnt.data.mF2C.cimi] [update_resource] Exception; Returning None ..." ) return None
def delete_resource_by_owner(resource, resources, owner_id): try: res = requests.get(config.dic['CIMI_URL'] + "/" + resource + "?$filter=acl/owner/principal='" + owner_id + "'", headers=CIMI_HEADER, verify=False) LOG.log( TRACE, "[usermgnt.data.mF2C.cimi] [delete_resource_by_owner] response: " + str(res)) # + ", " + str(res.json())) if res.status_code == 200 and len(res.json()[resources]) > 0: for r in res.json()[resources]: res2 = requests.delete(config.dic['CIMI_URL'] + "/" + r["id"], headers=CIMI_HEADER, verify=False) LOG.debug( "[usermgnt.data.mF2C.cimi] [delete_resource_by_owner] response: " + str(res2) + ", " + str(res2.json())) if res2.status_code == 200: LOG.debug( "[usermgnt.data.mF2C.cimi] [delete_resource_by_owner] Resource " + r["id"] + " deleted!") else: LOG.warning( "[usermgnt.data.mF2C.cimi] [delete_resource_by_owner] No " + resources + " found.") except: LOG.exception( "[usermgnt.data.mF2C.cimi] [delete_resource_by_owner] Exception.")
def gen_response_ko(message, key, value, key2=None, value2=None): dict = {'error': True, 'message': message} dict[key] = value if not (key2 is None) and not (value2 is None): dict[key2] = value2 LOG.log(TRACE, "Generate response KO; dict=" + str(dict)) return dict
def get_power(): device_id = get_current_device_id() # get 'my' device_id LOG.log( TRACE, "[usermgnt.data.mF2C.data] [get_power] Getting power status from device [" + device_id + "] ...") return cimi.get_power(device_id)
def register_user(data): LOG.debug( "[usermgnt.data.standalone.data] [register_user] Creating new Profile for user [data=" + str(data) + "] ...") return db.save_to_USER_PROFILE(config.dic['DEVICE_USER'], config.dic['HOST_IP'], data['service_consumer'], data['resource_contributor'])
def read_user_id(): LOG.log(TRACE, "[usermgnt.data.app.volume] [read_user_id] Reading user_id value from local VOLUME [" + config.dic['UM_WORKING_DIR_VOLUME'] + "] ...") try: with open(config.dic['UM_WORKING_DIR_VOLUME'] + "user_id.txt", "r") as file: return file.readline() except: LOG.error("[usermgnt.data.app.volume] [read_user_id] UM could not save 'user_id' value in " + config.dic['UM_WORKING_DIR_VOLUME'] + "user_id.txt. Returning None ...") return None
def save_user_id(user_id): try: LOG.log(TRACE, "[usermgnt.app.volume] [save_user_id] Storing user_id [" + user_id + "] value in local VOLUME [" + config.dic['UM_WORKING_DIR_VOLUME'] + "] ...") with open(config.dic['UM_WORKING_DIR_VOLUME'] + "user_id.txt", "w") as file: file.write(user_id) except: LOG.error("[usermgnt.data.app.volume] [save_user_id] UM could not save 'user_id' value in " + config.dic['UM_WORKING_DIR_VOLUME'] + "user_id.txt. Returning None ...") return None
def get_current_sharing_model(): LOG.info("[usermgnt.modules.um_sharing_model] [get_current_sharing_model] Getting current sharing model ...") sharing_model = data_adapter.get_current_sharing_model() if sharing_model is None: return common.gen_response(500, 'Error', 'sharing_model', 'not found / error', 'sharing_model', {}) elif sharing_model == -1: return common.gen_response_ko('Warning: Sharing-model not found', 'sharing_model', 'not found / error', 'sharing_model', {}) else: return common.gen_response_ok('Sharing-model found', 'sharing_model', sharing_model)
def gen_response(status, message, key, value, key2=None, value2=None): dict = {'error': True, 'message': message} dict[key] = value if not (key2 is None) and not (value2 is None): dict[key2] = value2 LOG.log(TRACE, 'Generate response ' + str(status) + "; dict=" + str(dict)) return Response(json.dumps(dict), status=status, content_type='application/json')
def get_sharing_model_by_id(sharing_model_id): LOG.info("[usermgnt.modules.um_sharing_model] [get_sharing_model_by_id] sharing_model_id=" + sharing_model_id) # get sharing_model sharing_model = data_adapter.get_sharing_model_by_id(sharing_model_id) if sharing_model is None: return common.gen_response(500, 'Exception', 'sharing_model_id', sharing_model_id, 'sharing_model', {}) elif sharing_model == -1: return common.gen_response_ko('Warning: Sharing-model not found', 'sharing_model_id', sharing_model_id, 'sharing_model', {}) else: return common.gen_response_ok('Sharing model found', 'sharing_model_id', sharing_model_id, 'sharing_model', sharing_model)
def delete_sharing_model_by_id(sharing_model_id): LOG.info("[usermgnt.modules.um_sharing_model] [delete_sharing_model_by_id] sharing_model_id=" + sharing_model_id) # deletes sharing_model res = data_adapter.delete_sharing_model_by_id(sharing_model_id) if res is None: return common.gen_response(500, 'Exception', 'sharing_model_id', sharing_model_id) elif res == -1: return common.gen_response_ko('Warning: Sharing-model not found', 'sharing_model_id', sharing_model_id) else: return common.gen_response_ok('Sharing-model deleted', 'sharing_model_id', sharing_model_id)
def delete_user_profile_by_id(profile_id): LOG.info( "[usermgnt.modules.um_profiling] [delete_user_profile_by_id] profile_id=" + profile_id) # delete profile if data_adapter.delete_user_profile_by_id(profile_id) is None: return common.gen_response(500, 'Error', 'profile_id', profile_id) else: return common.gen_response_ok('Profile deleted', 'profile_id', profile_id)
def update_sharing_model_by_id(sharing_model_id, data): LOG.info("[usermgnt.modules.um_sharing_model] [update_sharing_model_by_id] sharing_model_id=" + sharing_model_id + ", data=" + str(data)) # updates sharing_model sharing_model = data_adapter.update_sharing_model_by_id(sharing_model_id, data) if sharing_model is None: return common.gen_response(500, 'Exception', 'data', str(data), 'sharing_model', {}) elif sharing_model == -1: return common.gen_response_ko('Warning: Sharing model not found', 'sharing_model_id', sharing_model_id, 'sharing_model', {}) else: return common.gen_response_ok('Sharing-model updated', 'data', str(data), 'sharing_model', sharing_model)
def __stop(): LOG.info("[usermgnt.modules.um_assessment] [__stop] stop process") try: p_status = process.stop() return common.gen_response_ok('Assessment process stopped', 'status', p_status) except: LOG.exception('[usermgnt.modules.um_assessment] [__stop] Exception') return common.gen_response( 500, 'Exception when stopping the assessment process', 'status', '')
def status(): LOG.info("[usermgnt.modules.um_assessment] [status] get process status") try: p_status = process.get_status() return common.gen_response_ok('Assessment process status', 'status', p_status) except: LOG.exception('[usermgnt.modules.um_assessment] [status] Exception') return common.gen_response( 500, 'Exception getting the assessment process status', 'status', '')
def delete_user(user_id): user_id = user_id.replace('user/', '') LOG.info("[usermgnt.data.mF2C.data] [delete_user] Removing user [" + user_id + "] from mF2C ...") cimi.delete_resource_by_owner("sharing-model", "sharingModels", user_id) cimi.delete_resource_by_owner("user-profile", "userProfiles", user_id) cimi.delete_resource_by_owner("service", "services", user_id) cimi.delete_resource_by_owner("service-instance", "serviceInstances", user_id) return cimi.delete_resource("user/" + user_id)
def getCurrent(val): LOG.info("[usermgnt.modules.current] [getCurrent] Getting current " + val + " ...") if val == "user": return __getCurrentUser() elif val == "device": return __getCurrentDevice() elif val == "all": return __getCurrentAll() else: return common.gen_response(404, "Error", "parameter", val, "message", "Parameter '" + val + "' not allowed")
def delete_sharing_model_by_id(sharing_model_id): sharing_model_id = sharing_model_id.replace('sharing-model/', '') LOG.log( TRACE, "[usermgnt.data.mF2C.data] [delete_sharing_model_by_id] " + sharing_model_id) resp = cimi.get_resource_by_id("sharing-model/" + sharing_model_id) if resp and resp == -1: return -1 elif resp: resp = cimi.delete_resource(resp['id']) return resp return None
def update_sharing_model_by_id(sharing_model_id, data): sharing_model_id = sharing_model_id.replace('sharing-model/', '') LOG.log( TRACE, "[usermgnt.data.mF2C.data] [update_sharing_model_by_id] " + sharing_model_id + ", " + str(data)) resp = cimi.get_resource_by_id("sharing-model/" + sharing_model_id) if resp and resp == -1: return -1 elif resp: resp = cimi.update_resource(resp['id'], data) return resp return None
def get_agent_info(): LOG.log( TRACE, "[usermgnt.data.mF2C.data] [get_agent_info] Getting 'agent' resource ..." ) # get from AGENT resource agent = cimi.get_agent_info() LOG.debug("[usermgnt.data.mF2C.data] [get_agent_info] agent = " + str(agent)) if not agent is None and agent != -1: return agent else: return -1
def delete_user_profile_by_id(profile_id): profile_id = profile_id.replace('user-profile/', '') LOG.log( TRACE, "[usermgnt.data.mF2C.data] [delete_user_profile_by_id] Delete Profile [" + profile_id + "]") resp = cimi.get_resource_by_id("user-profile/" + profile_id) if resp and resp == -1: return None elif resp: resp = cimi.delete_resource(resp['id']) return resp return None
def get_agent_info(): try: res = requests.get(config.dic['CIMI_URL'] + "/agent", headers=CIMI_HEADER, verify=False) LOG.log( TRACE, "[usermgnt.data.mF2C.cimi] [get_agent_info] response: " + str(res) + ", " + str(res.json())) if res.status_code == 200 and res.json()['count'] == 0: LOG.warning( "[usermgnt.data.mF2C.cimi] [get_agent_info] 'agent' not found") return -1 elif res.status_code == 200: return res.json()['agents'][0] LOG.warning( "[usermgnt.data.mF2C.cimi] [get_agent_info] 'agent' not found; Returning -1 ..." ) return -1 except: LOG.error( "[usermgnt.data.mF2C.cimi] [get_agent_info] Exception; Returning None ..." ) return None
def get_user(user_id): LOG.debug("[usermgnt.modules.um_user] [get_user] user_id=" + str(user_id)) user = data_adapter.get_user_info(user_id) if user is None: return common.gen_response(500, 'Error or User not found', 'user_id', user_id, 'user', {}) elif user == -1: return common.gen_response( 403, "Warning: User " + user_id + " has no permissions on this device", 'user_id', user_id, 'user', {}) else: return common.gen_response_ok('User found', 'user_id', user_id, 'user', user)
def add_resource(resource_name, content): try: LOG.debug( "[usermgnt.data.mF2C.cimi] [add_resource] Adding new resource to [" + resource_name + "] with content [" + str(content) + "] ... ") # complete map and update resource content.update(common_new_map_fields()) #content.pop("user_id", None) res = requests.post(config.dic['CIMI_URL'] + '/' + resource_name, headers=CIMI_HEADER, verify=False, json=content) LOG.log( TRACE, "[usermgnt.data.mF2C.cimi] [add_resource] response: " + str(res) + ", " + str(res.json())) if res.status_code == 201: return get_resource_by_id(res.json()['resource-id']) LOG.error("[usermgnt.data.mF2C.cimi] [add_resource] Request failed: " + str(res.status_code) + "; Returning None ...") except: LOG.exception( "[usermgnt.data.mF2C.cimi] [add_resource] Exception; Returning None ..." ) return None
def get_user_profile_by_id(profile_id): LOG.debug( "[usermgnt.modules.um_profiling] [get_user_profile_by_id] profile_id=" + profile_id) user_profile = data_adapter.get_user_profile_by_id(profile_id) if user_profile is None: return common.gen_response(500, 'Error', 'profile_id', profile_id, 'profile', {}) elif user_profile == -1: return common.gen_response_ko('Warning: User profile not found', 'profile_id', profile_id, 'profile', {}) else: return common.gen_response_ok('User found', 'profile_id', profile_id, 'profile', user_profile)
def get_current_device_id(): LOG.info( "[usermgnt.data.standalone.data] [get_current_device_id] Getting 'my' device ID ..." ) # get from local volume device_id = vol.read_device_id() if device_id is not None and not device_id == "" and len(device_id) > 0: LOG.debug( "[usermgnt.data.standalone.data] [get_current_device_id] (LOCAL VOLUME) device_id = " + device_id) return device_id # get from config.dic['HOST_IP'] else: return config.dic['HOST_IP']
def update_user_profile_by_id(profile_id, data): LOG.debug( "[usermgnt.modules.um_profiling] [update_user_profile_by_id] profile_id=" + profile_id + ", data=" + str(data)) # update user user_profile = data_adapter.update_user_profile_by_id(profile_id, data) if user_profile is None: return common.gen_response(500, 'Error', 'profile_id', profile_id, 'profile', {}) elif user_profile == -1: return common.gen_response_ko('Warning: User profile not found', 'profile_id', profile_id, 'profile', {}) else: return common.gen_response_ok('User updated', 'profile_id', profile_id, 'profile', user_profile)