def append_trigger_message(self, component_id, component_type, message_code, data=""): """ Creates a current status message and saves it in the database :param component_id: The component id - uuid canonical string :param message_code: Message type :param data: Current Status Message Additional Info :return: True is successful, False otherwise """ rt = True try: message_data = Current_Status() message_data.component_id = component_id message_data.component_type = component_type message_data.creation_time = datetime.now() message_data.message_id = message_code message_data.additional_info = data message_data.supressed = 0 message_data.viewed = 'false' self.__trigger_messages.append(message_data) except Exception, e: rt = False
def save_current_status_message(component_id, component_type, message_code, data=""): result = True try: db.session.begin() message = get_current_status_message(component_id,message_code) if message is not None: message_data = message message_data.creation_time = datetime.now() db.session.merge(message_data) else: message_data = Current_Status() message_data.component_id = component_id message_data.component_type = component_type message_data.creation_time = datetime.now() message_data.message_id = message_code message_data.additional_info = data message_data.supressed = 0 message_data.viewed = 'false' db.session.add(message_data) db.session.commit() except: db.session.rollback() result = False return result
def save_current_status_message(component_id, component_type, message_code, data=""): """Saves or merges the status_message Args: component_id(uuid canonical string) message_code(uuid cannonical string):Message id data (str): Message data component_type(str): Component tyep """ result = True try: message = get_current_status_message(component_id, message_code) db.session.begin() if message is not None: message_data = message #message_data.creation_time = datetime.now() message_data.additional_info = data db.session.merge(message_data) else: message_data = Current_Status() message_data.id = uuid.uuid4().bytes message_data.component_id = get_bytes_from_uuid(component_id) message_data.component_type = component_type message_data.creation_time = datetime.utcnow() message_data.message_id = get_bytes_from_uuid(message_code) message_data.additional_info = data message_data.supressed = 0 message_data.viewed = 0 db.session.add(message_data) db.session.commit() except Exception as error: db.session.rollback() result = False return result
def save_current_status_message(component_id, component_type, message_code, data=""): """Saves or merges the status_message Args: component_id(uuid canonical string) message_code(uuid cannonical string):Message id data (str): Message data component_type(str): Component tyep """ result = True try: message = get_current_status_message(component_id, message_code) db.session.begin() if message is not None: message_data = message #message_data.creation_time = datetime.now() message_data.additional_info = data db.session.merge(message_data) else: message_data = Current_Status() message_data.id = uuid.uuid4().bytes message_data.component_id = get_bytes_from_uuid(component_id) message_data.component_type = component_type message_data.creation_time = datetime.utcnow() message_data.message_id = get_bytes_from_uuid(message_code) message_data.additional_info = data message_data.supressed = 0 message_data.viewed = 0 db.session.add(message_data) db.session.commit() except Exception as error: db.session.rollback() result = False return result
def db_insert_current_status_message(message_id, component_id, component_type, additional_info, replace, created=None): """Inserts a new notification on the system. The related message id should exists. Args: message_id (str:uuid string): Message id related with the notification component_id(str:uuid string): Component id related with the notification (Could be none for external messages) component_type(str): Component type. Allowed values: ('net','host','user','sensor','server','system','external') additional_info (str:json): Additional information you want to store. Returns: success(bool): True if the operation went well, False otherwise msg(str): A message string that will contain some kind of information in case of error""" if created is None: created = datetime.utcnow() if component_type not in [ 'net', 'host', 'user', 'sensor', 'server', 'system', 'external' ]: return False, "Invalid component_type" if component_type != "external" and component_id is None: return False, "Component id cannot be none for the given component_type" msg_id_binary = get_bytes_from_uuid(message_id) success, status_message = get_status_message_from_id( message_id=msg_id_binary, is_admin=True, serialize=False) if not success: return False, "The given message_id doesn't exist" if status_message is None: return False, "The given message_id doesn't exist. Message is None" component_id_binary = get_bytes_from_uuid(component_id) if (component_id_binary is None or component_id_binary == "") and component_type != "external": return False, "Invalid component_id" if replace is True: success, msg = delete_current_status_messages([msg_id_binary]) if not success: return success, "Unable to remove previous messages for the given message ID." try: db.session.begin() current_status_message = Current_Status() current_status_message.id = uuid4().bytes current_status_message.component_type = component_type current_status_message.creation_time = created current_status_message.message_id = msg_id_binary current_status_message.additional_info = additional_info current_status_message.suppressed = 0 current_status_message.viewed = 0 current_status_message.component_id = component_id_binary db.session.add(current_status_message) db.session.commit() except Exception, e: db.session.rollback() return False, "%s" % str(e)
def db_insert_current_status_message(message_id, component_id, component_type, additional_info, replace, created=None): """Inserts a new notification on the system. The related message id should exists. Args: message_id (str:uuid string): Message id related with the notification component_id(str:uuid string): Component id related with the notification (Could be none for external messages) component_type(str): Component type. Allowed values: ('net','host','user','sensor','server','system','external') additional_info (str:json): Additional information you want to store. Returns: success(bool): True if the operation went well, False otherwise msg(str): A message string that will contain some kind of information in case of error""" if created is None: created = datetime.utcnow() if component_type not in ['net', 'host', 'user', 'sensor', 'server', 'system', 'external']: return False, "Invalid component_type" if component_type != "external" and component_id is None: return False, "Component id cannot be none for the given component_type" msg_id_binary = get_bytes_from_uuid(message_id) success, status_message = get_status_message_from_id(message_id=msg_id_binary, is_admin=True, serialize=False) if not success: return False, "The given message_id doesn't exist" if status_message is None: return False, "The given message_id doesn't exist. Message is None" component_id_binary = get_bytes_from_uuid(component_id) if (component_id_binary is None or component_id_binary == "") and component_type != "external": return False, "Invalid component_id" if replace is True: success, msg = delete_current_status_messages([msg_id_binary]) if not success: return success, "Unable to remove previous messages for the given message ID." try: db.session.begin() current_status_message = Current_Status() current_status_message.id = uuid4().bytes current_status_message.component_type = component_type current_status_message.creation_time = created current_status_message.message_id = msg_id_binary current_status_message.additional_info = additional_info current_status_message.suppressed = 0 current_status_message.viewed = 0 current_status_message.component_id = component_id_binary db.session.add(current_status_message) db.session.commit() except Exception, e: db.session.rollback() return False, "%s" % str(e)
def append_trigger_message(self, component_id, component_type, message_code, data=""): """ Creates a current status message and saves it in the database :param component_id: The component id - uuid canonical string :param message_code: Message type :param data: Current Status Message Additional Info :return: True is successful, False otherwise """ rt = True try: message_data = Current_Status() message_data.component_id = component_id message_data.component_type = component_type message_data.creation_time = datetime.now() message_data.message_id = message_code message_data.additional_info = data message_data.supressed = 0 message_data.viewed = 'false' self.__trigger_messages.append(message_data) except Exception, e: rt = False
def load_mcserver_messages(message_list): """Adds or updates messages coming from the mcserver Args: message_list[Status_Message] Returns: success (bool): True if successful, False elsewhere result (str): Error message (if any) """ result = "" success = True try: db.session.begin() for msg in message_list: msg_id_str = str(msg['msg_id']) msg_id_binary = get_bytes_from_uuid(msg_id_str) additional_info_json = "" if msg['additional_info'] is not None and msg[ 'additional_info'] != "": try: additional_info_json = json.dumps(msg['additional_info']) except Exception as e: api_log.warning( "Message with an invalid additional_info %s - %s" % (msg_id_str, str(e))) additional_info_json = "" success, status_message = get_status_message_from_id( message_id=msg_id_binary, is_admin=True, serialize=False) if success: #update values: status_message.level = Status_Message.get_level_integer_from_string( str(msg['level'])) status_message.title = msg['title'] status_message.description = msg['description'] status_message.type = msg['type'] success, current_status_message = get_current_status_from_message_id( msg_id_str) if not success or len(current_status_message) != 1: api_log.error( "Invalid external message %s. Current_Status: %s, tuples(%s)" % (msg_id_str, success, len(current_status_message))) continue current_status_message[ 0].additional_info = additional_info_json db.session.merge(current_status_message[0]) db.session.merge(status_message) else: new_msg = Status_Message() new_msg.id = msg_id_binary new_msg.level = Status_Message.get_level_integer_from_string( str(msg['level'])) new_msg.title = msg['title'] new_msg.description = msg['description'] new_msg.type = msg['type'] new_msg.expire = datetime.strptime(msg['valid_to'], "%Y-%m-%dT%H:%M:%S") new_msg.actions = "" new_msg.alternative_actions = "" new_msg.source = "external" current_status_message = Current_Status() current_status_message.id = uuid4().bytes current_status_message.component_type = 'external' current_status_message.creation_time = datetime.strptime( msg['valid_from'], "%Y-%m-%dT%H:%M:%S") current_status_message.message_id = new_msg.id current_status_message.additional_info = "" current_status_message.suppressed = 0 current_status_message.viewed = 0 current_status_message.additional_info = additional_info_json db.session.add(new_msg) db.session.add(current_status_message) db.session.commit() except Exception, e: success = False result = "[load_mcserver_messages(] Error: %s" % str(e) db.session.rollback()
def load_mcserver_messages(message_list): """Adds or updates messages coming from the mcserver Args: message_list[Status_Message] Returns: success (bool): True if successful, False elsewhere result (str): Error message (if any) """ result = "" success = True try: db.session.begin() for msg in message_list: msg_id_str = str(msg['msg_id']) msg_id_binary = get_bytes_from_uuid(msg_id_str) additional_info_json = "" if msg['additional_info'] is not None and msg['additional_info'] != "": try: additional_info_json = json.dumps(msg['additional_info']) except Exception as e: api_log.warning("Message with an invalid additional_info %s - %s" % (msg_id_str, str(e))) additional_info_json = "" success, status_message = get_status_message_from_id(message_id=msg_id_binary, is_admin=True, serialize=False) if success: #update values: status_message.level = Status_Message.get_level_integer_from_string(str(msg['level'])) status_message.title = msg['title'] status_message.description = msg['description'] status_message.type = msg['type'] success, current_status_message = get_current_status_from_message_id(msg_id_str) if not success or len(current_status_message) != 1: api_log.error("Invalid external message %s. Current_Status: %s, tuples(%s)" % ( msg_id_str, success, len(current_status_message))) continue current_status_message[0].additional_info = additional_info_json db.session.merge(current_status_message[0]) db.session.merge(status_message) else: new_msg = Status_Message() new_msg.id = msg_id_binary new_msg.level = Status_Message.get_level_integer_from_string(str(msg['level'])) new_msg.title = msg['title'] new_msg.description = msg['description'] new_msg.type = msg['type'] new_msg.expire = datetime.strptime(msg['valid_to'], "%Y-%m-%dT%H:%M:%S") new_msg.actions = "" new_msg.alternative_actions = "" new_msg.source = "external" current_status_message = Current_Status() current_status_message.id = uuid4().bytes current_status_message.component_type = 'external' current_status_message.creation_time = datetime.strptime(msg['valid_from'], "%Y-%m-%dT%H:%M:%S") current_status_message.message_id = new_msg.id current_status_message.additional_info = "" current_status_message.suppressed = 0 current_status_message.viewed = 0 current_status_message.additional_info = additional_info_json db.session.add(new_msg) db.session.add(current_status_message) db.session.commit() except Exception, e: success = False result = "[load_mcserver_messages(] Error: %s" % str(e) db.session.rollback()