Skip to content

Commit

Permalink
fix: while configuring host inside the group without explicitly speci…
Browse files Browse the repository at this point in the history
…fying its port, and later adding expicite port, the previous configuration with the default port from the inventory will be deleted (#807)
  • Loading branch information
wojtekzyla authored Aug 7, 2023
1 parent 4785453 commit 59e26a0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 13 deletions.
23 changes: 17 additions & 6 deletions splunk_connect_for_snmp/common/inventory_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,29 @@ def gen_walk_task(ir: InventoryRecord, profile=None, group=None):
return task_config


def return_hosts_from_deleted_groups(previous_groups, new_groups):
def return_hosts_from_deleted_groups(
previous_groups, new_groups, inventory_group_port_mapping
):
inventory_lines_to_delete = []
for group_name in previous_groups.keys():
previous_groups_keys = get_groups_keys(previous_groups[group_name])
previous_groups_keys = get_groups_keys(
previous_groups[group_name], group_name, inventory_group_port_mapping
)
if group_name not in new_groups:
inventory_lines_to_delete += previous_groups_keys
else:
new_groups_keys = get_groups_keys(new_groups[group_name])
new_groups_keys = get_groups_keys(
new_groups[group_name], group_name, inventory_group_port_mapping
)
deleted_hosts = set(previous_groups_keys) - set(new_groups_keys)
inventory_lines_to_delete += deleted_hosts
return inventory_lines_to_delete


def get_groups_keys(list_of_groups):
def get_groups_keys(list_of_groups, group_name, inventory_group_port_mapping):
group_port = inventory_group_port_mapping.get(group_name, 161)
groups_keys = [
f"{transform_address_to_key(element.get('address'), element.get('port', 161))}"
f"{transform_address_to_key(element.get('address'), element.get('port', group_port))}"
for element in list_of_groups
]
return groups_keys
Expand All @@ -84,6 +91,7 @@ def __init__(self, group_manager: GroupsManager, logger):
self.group_manager = group_manager
self.logger = logger
self.hosts_from_groups: dict = {}
self.inventory_group_port_mapping: dict = {}
self.single_hosts: List[dict] = []

def get_all_hosts(self):
Expand All @@ -103,7 +111,7 @@ def get_all_hosts(self):
self.logger.warning(
f"Record: {host} has been already configured in group. Skipping..."
)
return self.inventory_records
return self.inventory_records, self.inventory_group_port_mapping

def process_line(self, source_record):
address = source_record["address"]
Expand All @@ -122,6 +130,9 @@ def get_group_hosts(self, source_object, group_name):
group_list = list(groups)
if group_list:
groups_object_list = list(group_list[0].values())
self.inventory_group_port_mapping[group_name] = (
source_object["port"] if source_object["port"] else 161
)
for group_object in groups_object_list[0]:
host_group_object = copy.copy(source_object)
for key in group_object.keys():
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 @@ -89,11 +89,11 @@ def load():
mongo_client, periodic_obj, logger
)
logger.info(f"Loading inventory from {INVENTORY_PATH}")
inventory_lines = inventory_processor.get_all_hosts()
inventory_lines, inventory_group_port_mapping = inventory_processor.get_all_hosts()

# Function to delete inventory records that are
hosts_from_groups_to_delete = return_hosts_from_deleted_groups(
previous_groups, new_groups
previous_groups, new_groups, inventory_group_port_mapping
)
for host in hosts_from_groups_to_delete:
inventory_record_manager.delete(host)
Expand Down
45 changes: 40 additions & 5 deletions test/common/test_inventory_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ def test_return_hosts_from_deleted_groups_one_host(self):
}

self.assertEqual(
return_hosts_from_deleted_groups(previous_groups, new_groups),
return_hosts_from_deleted_groups(
previous_groups,
new_groups,
{"group1": {"port": 161}, "switches": {"port": 161}},
),
["1.1.1.1:162"],
)

Expand All @@ -95,7 +99,11 @@ def test_return_hosts_from_deleted_groups_whole_group(self):
}

self.assertEqual(
return_hosts_from_deleted_groups(previous_groups, new_groups),
return_hosts_from_deleted_groups(
previous_groups,
new_groups,
{"group1": 161, "switches": 161},
),
["12.22.23.33", "1.1.1.1:162"],
)

Expand All @@ -115,22 +123,49 @@ def test_return_hosts_from_deleted_groups_one_host_and_group(self):
}

self.assertEqual(
return_hosts_from_deleted_groups(previous_groups, new_groups),
return_hosts_from_deleted_groups(
previous_groups,
new_groups,
{"group1": 161, "switches": 161},
),
["123.0.0.1", "178.8.8.1:999", "1.1.1.1:162"],
)

def test_return_hosts_empty(self):
previous_groups = {}
new_groups = {}
self.assertEqual(
return_hosts_from_deleted_groups(previous_groups, new_groups), []
return_hosts_from_deleted_groups(previous_groups, new_groups, {}), []
)

def test_return_hosts_new_ones(self):
previous_groups = {}
new_groups = {"switches": [{"address": "12.22.23.33", "port": 161}]}
self.assertEqual(
return_hosts_from_deleted_groups(previous_groups, new_groups), []
return_hosts_from_deleted_groups(
previous_groups, new_groups, {"switches": {"port": 161}}
),
[],
)

def test_return_deleted_host_without_port_config(self):
previous_groups = {
"group1": [
{"address": "123.0.0.1", "port": 161},
{"address": "178.8.8.1"},
]
}
new_groups = {
"group1": [
{"address": "123.0.0.1", "port": 161},
{"address": "178.8.8.1", "port": 162},
]
}
self.assertEqual(
["178.8.8.1:1161"],
return_hosts_from_deleted_groups(
previous_groups, new_groups, {"group1": 1161}
),
)

def test_get_group_hosts(self):
Expand Down

0 comments on commit 59e26a0

Please sign in to comment.