def _setup_db_deviceagent(data): """Create the database for DeviceAgent table testing. Args: None Returns: result: Tuple of (idx_device, idx_agent) """ # Initialize key variables devicename = data['devicename'] description = data['device_description'] id_agent = data['id_agent'] agent_name = data['agent'] last_timestamp = data['timestamp'] # Add AgentName record to the database record = AgentName(name=general.encode(agent_name)) database = db.Database() database.add(record, 1031) # Add Agent record to the database record = Agent(id_agent=general.encode(id_agent)) database = db.Database() database.add(record, 1031) # Get idx_agent value from database agent_info = db_agent.GetIDAgent(id_agent) idx_agent = agent_info.idx_agent() # Add record to the database dev_record = Device(description=general.encode(description), devicename=general.encode(devicename)) database = db.Database() database.add(dev_record, 1034) # Get idx of newly added device device_info = db_device.GetDevice(devicename) idx_device = device_info.idx_device() # Update DeviceAgent table if hagent.device_agent_exists(idx_device, idx_agent) is False: # Add to DeviceAgent table da_record = DeviceAgent(idx_device=idx_device, idx_agent=idx_agent) database = db.Database() database.add(da_record, 1020) # Update DeviceAgent table with timestamp database = db.Database() session = database.session() record = session.query(DeviceAgent).filter( and_(DeviceAgent.idx_device == idx_device, DeviceAgent.idx_agent == idx_agent)).one() record.last_timestamp = last_timestamp database.commit(session, 1042) # Return result = (idx_device, idx_agent) return result
def idx_agent(self): """Insert new agent into database if necessary. Args: None Returns: idx_agent: IDX value of agent from database """ # Initialize key variables agent_name = self.agent_data['agent_name'] id_agent = self.agent_data['id_agent'] # Get information on agent from database agent_data = db_agent.GetIDAgent(id_agent) # Return if agent already exists in the table if agent_data.exists() is True: idx_agent = agent_data.idx_agent() return idx_agent # Get information on agent from database name_data = db_agentname.GetAgentName(agent_name) # Insert data into table if required # Get idx_agentname if name_data.exists() is False: record = AgentName( name=general.encode(agent_name)) database = db.Database() try: database.add(record, 1145) except pymysql.IntegrityError: # There may be a duplicate agent name if this is a brand # new database and there is a flurry of updates from multiple # agents. This is OK, pass. # # We are expecting a 'pymysql.err.IntegrityError' but for some # reason it could not be caught. pass new_name_data = db_agentname.GetAgentName(agent_name) idx_agentname = new_name_data.idx_agentname() else: idx_agentname = name_data.idx_agentname() # Add record to the database new_record = Agent( id_agent=general.encode(id_agent), idx_agentname=idx_agentname) database = db.Database() database.add(new_record, 1081) # Get idx_agent value from database new_agent_data = db_agent.GetIDAgent(id_agent) idx_agent = new_agent_data.idx_agent() return idx_agent
def idx_agent(self): """Insert new agent into database if necessary. Args: None Returns: idx_agent: IDX value of agent from database """ # Initialize key variables agent_name = self.agent_data['agent_name'] id_agent = self.agent_data['id_agent'] # Get information on agent from database agent_data = db_agent.GetIDAgent(id_agent) # Return if agent already exists in the table if agent_data.exists() is True: idx_agent = agent_data.idx_agent() return idx_agent # Get information on agent from database name_data = db_agentname.GetAgentName(agent_name) # Insert data into table if required # Get idx_agentname if name_data.exists() is False: record = AgentName(name=general.encode(agent_name)) database = db.Database() database.add(record, 1081) new_name_data = db_agentname.GetAgentName(agent_name) idx_agentname = new_name_data.idx_agentname() else: idx_agentname = name_data.idx_agentname() # Add record to the database new_record = Agent(id_agent=general.encode(id_agent), idx_agentname=idx_agentname) database = db.Database() database.add(new_record, 1081) # Get idx_agent value from database new_agent_data = db_agent.GetIDAgent(id_agent) idx_agent = new_agent_data.idx_agent() return idx_agent
def _insert_agent_device(self): """Insert first agent and device in the database. Args: None Returns: None """ # Initialize key variables idx_agentname = 1 idx_agent = 1 idx_device = 1 # Add agent name if db_agentname.idx_agentname_exists(idx_agentname) is False: # Generate a name add a record in the database record = AgentName( name=general.encode(self.reserved)) database = db.Database() database.add(record, 1019) # Add agent if db_agent.idx_agent_exists(idx_agent) is False: # Generate an Agent ID and add a record in the database record = Agent(id_agent=general.encode(self.reserved)) database = db.Database() database.add(record, 1109) # Add device if db_device.idx_device_exists(idx_device) is False: record = Device( description=general.encode(self.reserved), devicename=general.encode(self.reserved) ) database = db.Database() database.add(record, 1106) # Add to Agent / Device table if db_deviceagent.device_agent_exists(idx_device, idx_agent) is False: record = DeviceAgent(idx_device=idx_device, idx_agent=idx_agent) database = db.Database() database.add(record, 1107)