Skip to content

Commit

Permalink
chore: remove internal logger
Browse files Browse the repository at this point in the history
  • Loading branch information
ajasnosz committed Apr 5, 2024
1 parent 6d34bdf commit c5ad5f7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 30 deletions.
28 changes: 13 additions & 15 deletions splunk_connect_for_snmp/common/inventory_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,22 +104,21 @@ def get_groups_keys(list_of_groups, group_name, inventory_group_port_mapping):


class InventoryProcessor:
def __init__(self, group_manager: GroupsManager, logger, inventory_ui_collection):
def __init__(self, group_manager: GroupsManager, inventory_ui_collection):
self.inventory_records: List[dict] = []
self.group_manager = group_manager
self.logger = logger
self.hosts_from_groups: dict = {}
self.inventory_group_port_mapping: dict = {}
self.single_hosts: List[dict] = []
self.inventory_ui_collection = inventory_ui_collection

def get_all_hosts(self):
if CONFIG_FROM_MONGO:
self.logger.info("Loading inventory from inventory_ui collection")
logger.info("Loading inventory from inventory_ui collection")
ir_reader = list(self.inventory_ui_collection.find({}, {"_id": 0}))
else:
with open(INVENTORY_PATH, encoding="utf-8") as csv_file:
self.logger.info(f"Loading inventory from {INVENTORY_PATH}")
logger.info(f"Loading inventory from {INVENTORY_PATH}")
ir_reader = list(DictReader(csv_file))
for inventory_line in ir_reader:
self.process_line(inventory_line)
Expand All @@ -131,7 +130,7 @@ def get_all_hosts(self):
if was_present is None:
self.inventory_records.append(source_record)
else:
self.logger.warning(
logger.warning(
f"Record: {host} has been already configured in group. Skipping..."
)
return self.inventory_records, self.inventory_group_port_mapping
Expand All @@ -140,7 +139,7 @@ def process_line(self, source_record):
address = source_record["address"]
# Inventory record is commented out
if address.startswith("#"):
self.logger.warning(f"Record: {address} is commented out. Skipping...")
logger.warning(f"Record: {address} is commented out. Skipping...")
# Address is an IP address
elif address[0].isdigit():
self.single_hosts.append(source_record)
Expand All @@ -162,7 +161,7 @@ def get_group_hosts(self, source_object, group_name):
if key in ALLOWED_KEYS_VALUES:
host_group_object[key] = group_object[key]
else:
self.logger.warning(
logger.warning(
f"Key {key} is not allowed to be changed from the group level"
)
address = str(group_object["address"])
Expand All @@ -172,27 +171,26 @@ def get_group_hosts(self, source_object, group_name):
host_group_object["group"] = group_name
self.inventory_records.append(host_group_object)
else:
self.logger.warning(
logger.warning(
f"Group {group_name} doesn't exist in the configuration. Treating {group_name} as a hostname"
)
self.single_hosts.append(source_object)


class InventoryRecordManager:
def __init__(self, mongo_client, periodic_objects_collection, logger):
def __init__(self, mongo_client, periodic_objects_collection):
self.targets_collection = mongo_client.sc4snmp.targets
self.inventory_collection = mongo_client.sc4snmp.inventory
self.attributes_collection = mongo_client.sc4snmp.attributes
self.periodic_object_collection = periodic_objects_collection
self.logger = logger

def delete(self, target):
address, port = transform_key_to_address(target)
self.periodic_object_collection.delete_all_tasks_of_host(target)
self.inventory_collection.delete_one({"address": address, "port": port})
self.targets_collection.delete_many({"address": target})
self.attributes_collection.delete_many({"address": target})
self.logger.info(f"Deleting record: {target}")
logger.info(f"Deleting record: {target}")

def update(
self, inventory_record, new_source_record, runtime_profiles, expiry_time_changed
Expand All @@ -207,13 +205,13 @@ def update(
upsert=True,
)
if status.matched_count == 0:
self.logger.info(f"New Record {inventory_record} {status.upserted_id}")
logger.info(f"New Record {inventory_record} {status.upserted_id}")
elif status.modified_count == 1 and status.upserted_id is None:
self.logger.info(f"Modified Record {inventory_record}")
logger.info(f"Modified Record {inventory_record}")
else:
self.logger.info(f"Unchanged Record {inventory_record}")
logger.info(f"Unchanged Record {inventory_record}")
if expiry_time_changed:
self.logger.info(
logger.info(
f"Task expiry time was modified, generating new tasks for record {inventory_record}"
)
else:
Expand Down
4 changes: 2 additions & 2 deletions splunk_connect_for_snmp/inventory/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ def load():

inventory_ui_collection = mongo_client.sc4snmp.inventory_ui
inventory_processor = InventoryProcessor(
groups_manager, logger, inventory_ui_collection
groups_manager, inventory_ui_collection
)
inventory_record_manager = InventoryRecordManager(
mongo_client, periodic_obj, logger
mongo_client, periodic_obj
)
if CONFIG_FROM_MONGO:
logger.info(f"Loading inventory from inventory_ui collection")
Expand Down
26 changes: 13 additions & 13 deletions test/common/test_inventory_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def test_get_group_hosts(self):
"delete": "",
},
]
inventory_processor = InventoryProcessor(group_manager, Mock(), Mock())
inventory_processor = InventoryProcessor(group_manager, Mock())
group_manager.return_element.return_value = [
{
"group1": [
Expand Down Expand Up @@ -239,7 +239,7 @@ def test_get_group_hosts_hostname(self):
"SmartProfiles": "f",
"delete": "",
}
inventory_processor = InventoryProcessor(group_manager, logger, Mock())
inventory_processor = InventoryProcessor(group_manager, Mock())
group_manager.return_element.return_value = []
inventory_processor.get_group_hosts(
group_object, "ec2-54-91-99-115.compute-1.amazonaws.com"
Expand All @@ -253,7 +253,7 @@ def test_get_group_hosts_hostname(self):
def test_process_line_comment(self):
logger = Mock()
source_record = {"address": "#54.234.85.76"}
inventory_processor = InventoryProcessor(Mock(), logger, Mock())
inventory_processor = InventoryProcessor(Mock(), Mock())
inventory_processor.process_line(source_record)
logger.warning.assert_called_with(
"Record: #54.234.85.76 is commented out. Skipping..."
Expand All @@ -272,13 +272,13 @@ def test_process_line_comment(self):
)
def test_process_line_host(self, m_inventory):
source_record = {"address": "54.234.85.76"}
inventory_processor = InventoryProcessor(Mock(), Mock(), Mock())
inventory_processor = InventoryProcessor(Mock(), Mock())
inventory_processor.get_all_hosts()
self.assertEqual(inventory_processor.inventory_records, [source_record])

def test_process_line_group(self):
source_record = {"address": "group1"}
inventory_processor = InventoryProcessor(Mock(), Mock(), Mock())
inventory_processor = InventoryProcessor(Mock(), Mock())
inventory_processor.get_group_hosts = Mock()
inventory_processor.process_line(source_record)
inventory_processor.get_group_hosts.assert_called_with(source_record, "group1")
Expand Down Expand Up @@ -307,7 +307,7 @@ def test_ignore_line_host_configured_in_group(self, m_load_element):
]
group_manager = Mock()
group_manager.return_element.return_value = returned_group
inventory_processor = InventoryProcessor(group_manager, Mock(), Mock())
inventory_processor = InventoryProcessor(group_manager, Mock())
expected = [
{
"address": "0.0.0.0",
Expand Down Expand Up @@ -353,7 +353,7 @@ def test_ignore_line_host_configured_in_group(self, m_load_element):

def test_return_walk_profile(self):
inventory_profiles = ["walk1", "generic_switch"]
inventory_record_manager = InventoryRecordManager(Mock(), Mock(), Mock())
inventory_record_manager = InventoryRecordManager(Mock(), Mock())
self.assertEqual(
inventory_record_manager.return_walk_profile(
self.profiles, inventory_profiles
Expand All @@ -363,7 +363,7 @@ def test_return_walk_profile(self):

def test_return_walk_profile_more_than_one(self):
inventory_profiles = ["walk1", "test_33", "generic_switch"]
inventory_record_manager = InventoryRecordManager(Mock(), Mock(), Mock())
inventory_record_manager = InventoryRecordManager(Mock(), Mock())
self.assertEqual(
inventory_record_manager.return_walk_profile(
self.profiles, inventory_profiles
Expand All @@ -373,7 +373,7 @@ def test_return_walk_profile_more_than_one(self):

def test_return_walk_profile_no_walk_in_inventory(self):
inventory_profiles = ["generic_switch"]
inventory_record_manager = InventoryRecordManager(Mock(), Mock(), Mock())
inventory_record_manager = InventoryRecordManager(Mock(), Mock())
self.assertEqual(
inventory_record_manager.return_walk_profile(
self.profiles, inventory_profiles
Expand All @@ -383,7 +383,7 @@ def test_return_walk_profile_no_walk_in_inventory(self):

def test_return_walk_profile_no_walk_in_config(self):
inventory_profiles = ["generic_switch", "walk2"]
inventory_record_manager = InventoryRecordManager(Mock(), Mock(), Mock())
inventory_record_manager = InventoryRecordManager(Mock(), Mock())
self.assertEqual(
inventory_record_manager.return_walk_profile(
self.profiles, inventory_profiles
Expand All @@ -393,20 +393,20 @@ def test_return_walk_profile_no_walk_in_config(self):

def test_return_walk_profile_no_config(self):
inventory_profiles = ["generic_switch", "walk2"]
inventory_record_manager = InventoryRecordManager(Mock(), Mock(), Mock())
inventory_record_manager = InventoryRecordManager(Mock(), Mock())
self.assertEqual(
inventory_record_manager.return_walk_profile({}, inventory_profiles), None
)

def test_return_walk_profile_no_config_no_inventory(self):
inventory_profiles = []
inventory_record_manager = InventoryRecordManager(Mock(), Mock(), Mock())
inventory_record_manager = InventoryRecordManager(Mock(), Mock())
self.assertEqual(
inventory_record_manager.return_walk_profile({}, inventory_profiles), None
)

def test_return_walk_profile_no_inventory(self):
inventory_record_manager = InventoryRecordManager(Mock(), Mock(), Mock())
inventory_record_manager = InventoryRecordManager(Mock(), Mock())
self.assertEqual(
inventory_record_manager.return_walk_profile(self.profiles, []), None
)

0 comments on commit c5ad5f7

Please sign in to comment.