def __print_records(db): LOG.log( TRACE, '[lifecycle.data.app.db] [__print_records] Retrieving records from db...' ) records = db() for r in records: LOG.log(TRACE, "db> " + str(r))
def gen_response_ok(message, key, value, key2=None, value2=None): dict = {'error': False, 'message': message} dict[key] = value if not (key2 is None) and not (value2 is None): dict[key2] = value2 LOG.log( TRACE, "[lifecycle.common.common] [gen_response_ok] Generate response OK; dict=" + str(dict)) return dict
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, '[lifecycle.common.common] [gen_response] Generate response ' + str(status) + "; dict=" + str(dict)) return Response(json.dumps(dict), status=status, content_type='application/json')
def del_all_service_instances(): try: service_instances = get_all_service_instances() for si in service_instances: LOG.log(TRACE, "[lifecycle.data.mf2c.cimi] [del_all_service_instances] Deleting " + si['id'] + " ... ") resource_id = si['id'].replace(RSRC_SERVICE_INSTANCE + '/', '') res = requests.delete(config.dic['CIMI_URL'] + '/' + RSRC_SERVICE_INSTANCE + '/' + resource_id, headers=CIMI_HEADER, verify=False) LOG.log(TRACE, "[lifecycle.data.mf2c.cimi] [del_all_service_instances] [all] response: " + str(res) + ", " + str(res.json())) return True except: LOG.exception("[lifecycle.data.mf2c.cimi] [del_all_service_instances] Exception; Returning False ...") return False
def get_id_from_device(deviceID): try: res = requests.get(config.dic['CIMI_URL'] + "/device?$filter=deviceID=\"" + deviceID + "\"", headers=CIMI_HEADER, verify=False) LOG.log(TRACE, "[lifecycle.data.mf2c.cimi] [get_id_from_device] response: " + str(res) + ", " + str(res.json())) if res.status_code == 200 and len(res.json()['devices']) > 0: return res.json()['devices'][0]['id'] else: LOG.warning("[lifecycle.data.mf2c.cimi] [get_id_from_device] No device found; Returning -1 ...") return -1 except: LOG.exception("[lifecycle.data.mf2c.cimi] [get_id_from_device] Exception; Returning None ...") return None
def del_service_instance_by_id(resource_id): try: resource_id = resource_id.replace(RSRC_SERVICE_INSTANCE + '/', '') res = requests.delete(config.dic['CIMI_URL'] + '/' + RSRC_SERVICE_INSTANCE + '/' + resource_id, headers=CIMI_HEADER, verify=False) LOG.log(TRACE, "[lifecycle.data.mf2c.cimi] [del_service_instance_by_id] [" + resource_id + "] response: " + str(res) + ", " + str(res.json())) if res.status_code == 200: return res.json() LOG.warning("[lifecycle.data.mf2c.cimi] [del_service_instance_by_id] Service instance not deleted. Id=" + resource_id + "; Returning None ...") except: LOG.exception("[lifecycle.data.mf2c.cimi] [del_service_instance_by_id] Exception (parameters: resource_id=" + resource_id + "); Returning None ...") return None
def get_sharing_model_by_device(device_id): try: device_id = device_id.replace('device/', '') res = requests.get(config.dic['CIMI_URL'] + "/sharing-model?$filter=device_id=\"device/" + device_id + "\"", headers=CIMI_HEADER, verify=False) LOG.log(TRACE, "[lifecycle.data.mf2c.cimi] [get_sharing_model_by_device] [" + device_id + "] response: " + str(res) + ", " + str(res.json())) if res.status_code == 200 and len(res.json()['sharingModels']) > 0: return res.json()['sharingModels'][0] else: LOG.warning("[lifecycle.data.mf2c.cimi] [get_sharing_model_by_device] Sharing-model not found [device_id=" + device_id + "]; Returning -1 ...") return -1 except: LOG.exception("[lifecycle.data.mf2c.cimi] [get_sharing_model_by_device] Exception (parameters: device_id=" + device_id + "); Returning None ...") return None
def get_service_instance_by_id(id): try: id = id.replace(RSRC_SERVICE_INSTANCE + '/', '') res = requests.get(config.dic['CIMI_URL'] + '/' + RSRC_SERVICE_INSTANCE + '/' + id, headers=CIMI_HEADER, verify=False) LOG.log(TRACE, "[lifecycle.data.mf2c.cimi] [get_service_instance_by_id] [" + id + "] response: " + str(res) + ", " + str(res.json())) if res.status_code == 200 and 'id' in res.json(): return res.json() elif res.status_code == 200: LOG.warning("[lifecycle.data.mf2c.cimi] [get_service_instance_by_id] No service instance retrieved. Id=" + id + "; Returning -1 ...") return -1 except: LOG.exception("[lifecycle.data.mf2c.cimi] [get_service_instance_by_id] Exception (parameters: id=" + id + "); Returning None ...") return None
def get_current(val): LOG.log(TRACE, "[lifecycle.connectors.atos.user_manager] [get_current] Getting current " + val + " from localhost - UM: Checking avialability ...") try: LOG.info("[lifecycle.connectors.atos.user_manager] [get_current] HTTP GET: " + str(config.dic['URL_AC_USER_MANAGEMENT']) + "/current/" + val) r = requests.get(str(config.dic['URL_AC_USER_MANAGEMENT']) + "/current/" + val, verify=config.dic['VERIFY_SSL']) LOG.log(TRACE, "[lifecycle.connectors.atos.user_manager] [get_current] response: " + str(r) + ", " + str(r.json())) json_data = json.loads(r.text) LOG.debug("[lifecycle.connectors.atos.user_manager] [get_current] json_data=" + str(json_data)) if r.status_code == 200: return json_data LOG.error("[lifecycle.connectors.atos.user_manager] [get_current] Error: status_code=" + str(r.status_code) + "; Returning None ...") except: LOG.exception("[lifecycle.connectors.atos.user_manager] [get_current] Exception; Returning None ...") return None
def get_user_profile(): LOG.log(TRACE, "[lifecycle.connectors.atos.user_manager] [get_user_profile] Getting user-profile from localhost ...") try: LOG.info("[lifecycle.connectors.atos.user_manager] [get_user_profile] HTTP GET: " + str(config.dic['URL_AC_USER_MANAGEMENT']) + "/user-profile") r = requests.get(str(config.dic['URL_AC_USER_MANAGEMENT']) + "/user-profile", verify=config.dic['VERIFY_SSL']) LOG.log(TRACE, "[lifecycle.connectors.atos.user_manager] [get_user_profile] response: " + str(r) + ", " + str(r.json())) json_data = json.loads(r.text) LOG.debug("[lifecycle.connectors.atos.user_manager] [get_user_profile] json_data=" + str(json_data)) if r.status_code == 200: return json_data LOG.error("[lifecycle.connectors.atos.user_manager] [get_user_profile] Error: status_code=" + str(r.status_code) + "; Returning None ...") except: LOG.exception("[lifecycle.connectors.atos.user_manager] [get_user_profile] Exception; Returning None ...") return None
def check_avialability(): LOG.log(TRACE, "[lifecycle.connectors.atos.user_manager] [check_avialability] localhost - local UM: Checking avialability ...") try: LOG.log(TRACE, "[lifecycle.connectors.atos.user_manager] [check_avialability] HTTP GET: " + str(config.dic['URL_AC_USER_MANAGEMENT']) + "/check") r = requests.get(str(config.dic['URL_AC_USER_MANAGEMENT']) + "/check", verify=config.dic['VERIFY_SSL']) LOG.log(TRACE, "[lifecycle.connectors.atos.user_manager] [check_avialability] response: " + str(r) + ", " + str(r.json())) json_data = json.loads(r.text) LOG.log(TRACE, "[lifecycle.connectors.atos.user_manager] [check_avialability] json_data=" + str(json_data)) if r.status_code == 200 and not json_data['result'] is None: return json_data LOG.log(TRACE, "[lifecycle.connectors.atos.user_manager] [check_avialability] Error: status_code=" + str(r.status_code) + "; Returning None ...") except: LOG.exception("[lifecycle.connectors.atos.user_manager] [check_avialability] Exception; Returning None ...") return None
def set_um_properties(apps=0): LOG.log(TRACE, "[lifecycle.connectors.atos.user_manager] [set_um_properties] localhost - local UM: Updating UM properties ...") try: LOG.info("[lifecycle.connectors.atos.user_manager] [set_um_properties] HTTP PUT: " + str(config.dic['URL_AC_USER_MANAGEMENT']) + "/sharing-model") r = requests.put(str(config.dic['URL_AC_USER_MANAGEMENT']) + "/sharing-model", json={"apps_running": apps}, verify=config.dic['VERIFY_SSL']) LOG.log(TRACE, "[lifecycle.connectors.atos.user_manager] [set_um_properties] response: " + str(r) + ", " + str(r.json())) if r.status_code == 200: json_data = json.loads(r.text) LOG.debug('[lifecycle.connectors.atos.user_manager] [set_um_properties] json_data=' + str(json_data)) return json_data LOG.error("[lifecycle.connectors.atos.user_manager] [set_um_properties] Error: status_code=" + str(r.status_code) + "; Returning None ...") except: LOG.exception("[lifecycle.connectors.atos.user_manager] [set_um_properties] Exception; Returning None ...") return None
def get_agent(): try: res = requests.get(config.dic['CIMI_URL'] + "/agent", headers=CIMI_HEADER, verify=False) LOG.log(TRACE, "[lifecycle.data.mf2c.cimi] [get_agent] response: " + str(res) + ", " + str(res.json())) if res.status_code == 200 and res.json()['count'] == 0: LOG.warning("[lifecycle.data.mf2c.cimi] [get_agent] 'agent' not found") return -1 elif res.status_code == 200: return res.json()['agents'][0] LOG.warning("[lifecycle.data.mf2c.cimi] [get_agent] 'agent' not found; Returning -1 ...") return -1 except: LOG.error("[lifecycle.data.mf2c.cimi] [get_agent] Exception; Returning None ...") return None
def add_service_instance(content, user): try: content.update(common_new_map_fields_user(user)) #common_new_map_fields()) # complete map and update resource LOG.log(TRACE, "[lifecycle.data.mf2c.cimi] [add_service_instance] [content=" + str(content) + "] [user="******"] ... ") res = requests.post(config.dic['CIMI_URL'] + '/' + RSRC_SERVICE_INSTANCE, headers=CIMI_HEADER, verify=False, json=content) LOG.log(TRACE, "[lifecycle.data.mf2c.cimi] [add_service_instance] response: " + str(res) + ", " + str(res.json())) if res.status_code == 201: id = res.json()['resource-id'].replace(RSRC_SERVICE_INSTANCE + '/', '') return get_service_instance_by_id(id) LOG.warning("[lifecycle.data.mf2c.cimi] [add_service_instance] Service instance not added. content=" + str(content) + "; Returning None ...") except: LOG.exception("[lifecycle.data.mf2c.cimi] [add_service_instance] Exception while adding new resource to service instances!; Returning None ...") return None
def get_service_instance_report(id): try: res = requests.get(config.dic['CIMI_URL'] + "/" + RSRC_SERVICE_INSTANCE_REPORT + "?$filter=operation_id='" + id + "'", headers=CIMI_HEADER, verify=False) LOG.log(TRACE, "[lifecycle.data.mf2c.cimi] [get_service_instance_report] [" + id + "] response: " + str(res) + ", " + str(res.json())) if res.status_code == 200 and len(res.json()['serviceOperationReports']) > 0: return res.json()['serviceOperationReports'][0] elif res.status_code == 200: LOG.warning("[lifecycle.data.mf2c.cimi] [get_service_instance_report] Report not found [service-instance=" + id + "]; Returning empty dict ...") return {} LOG.warning("[lifecycle.data.mf2c.cimi] [get_service_instance_report] No report retrieved. id=" + id + "; Returning None ...") except: LOG.exception("[lifecycle.data.mf2c.cimi] [get_service_instance_report] Exception (parameters: id=" + id + "); Returning None ...") return None
def update_service_instance(resource_id, content): try: resource_id = resource_id.replace(RSRC_SERVICE_INSTANCE + '/', '') content.update(common_update_map_fields_user(content['user']))#common_update_map_fields()) # complete map and update resource #content.update(patch_update_map_fields()) LOG.log(TRACE, "[lifecycle.data.mf2c.cimi] [update_service_instance] [content=" + str(content) + "] ... ") res = requests.put(config.dic['CIMI_URL'] + '/' + RSRC_SERVICE_INSTANCE + '/' + resource_id, headers=CIMI_HEADER, verify=False, json=content) LOG.log(TRACE, "[lifecycle.data.mf2c.cimi] [update_service_instance] response: " + str(res) + ", " + str(res.json())) if res.status_code == 200: return get_service_instance_by_id(resource_id) LOG.warning("[lifecycle.data.mf2c.cimi] [update_service_instance] Service instance not updated. resource_id=" + resource_id + ", content=" + str(content) + "; Returning None ...") except: LOG.exception("[lifecycle.data.mf2c.cimi] [update_service_instance] Exception; Returning None ...") return None
def save_to_DB_DOCKER_PORTS(port, mapped_to): LOG.log( TRACE, '[lifecycle.data.app.db] [save_to_DB_DOCKER_PORTS] Saving record ...') try: record = get_from_DB_DOCKER_PORTS(port) if record is None: DB_DOCKER_PORTS.insert(port=port, mapped_to=mapped_to) # save changes on disk DB_DOCKER_PORTS.commit() # debug DB __print_records(DB_DOCKER_PORTS) return True else: LOG.warning( '[lifecycle.data.app.db] [save_to_DB_DOCKER_PORTS] Port already added to DB' ) return False except: LOG.exception( '[lifecycle.data.app.db] [save_to_DB_DOCKER_PORTS] Exception') return False