From b53749eca9f0f26cf3e374abf2e0d6f105358106 Mon Sep 17 00:00:00 2001 From: James Green Date: Fri, 21 Jun 2024 11:55:55 +0100 Subject: [PATCH 1/2] refactored mac addresses into lists, rather than strings --- src/juniper_ap_staging.py | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/src/juniper_ap_staging.py b/src/juniper_ap_staging.py index a6195c0..b1d6c55 100644 --- a/src/juniper_ap_staging.py +++ b/src/juniper_ap_staging.py @@ -60,13 +60,13 @@ def format_site_and_mac_reservations(ap_csv: list['dict'] if existing_site: # Update the mac_addresses field by appending the new MAC address if mac_address not in existing_site["mac_addresses"]: - existing_site["mac_addresses"] += f",{mac_address}" + existing_site["mac_addresses"].append(mac_address.lower()) else: # Create a new dictionary with the desired keys and values new_item = { "" "SiteName": site_name, - "mac_addresses": mac_address + "mac_addresses": [mac_address.lower()] } # Append the new dictionary to the output list site_and_mac_reservations.append(new_item) @@ -103,9 +103,7 @@ def get_site_payload_for_inventory_assign_to_site( { "op": "assign", "site_id": site['site_id'], - "macs": [ - site['mac_addresses'] - ], + "macs": site['mac_addresses'], "no_reassign": False, "disable_auto_config": False, "managed": False @@ -137,7 +135,7 @@ def build_rename_ap_payload( for site in inventory_payloads: for mac in site['macs']: inventory_item = find_inventory_item_by_mac_address( - mist_inventory, mac.lower()) + mist_inventory, mac) csv_item = find_csv_item_by_mac_address(ap_csv, mac) payload.append( { @@ -149,26 +147,35 @@ def build_rename_ap_payload( return payload +def find_value_in_list_of_dicts(list_of_dicts, key, value): + for dictionary in list_of_dicts: + if dictionary.get(key) == value: + return dictionary + raise ValueError(f"Value '{value}' not found for key '{key}' in the list of dictionaries.") + def find_inventory_item_by_mac_address( mist_inventory: list['dict'], mac: str ) -> dict: - for item in mist_inventory: - if item['mac'] == mac: - return item - raise ValueError(f"Unable to find item with Mac Address: '{mac}'") + + try: + result = find_value_in_list_of_dicts(mist_inventory, "mac", mac) + return result + except ValueError as e: + print(e) def find_csv_item_by_mac_address( ap_csv: list['dict'], mac: str ) -> list[dict]: - for item in ap_csv: - if item['MAC Address'].replace(":", "") == mac: - return item - raise ValueError( - f"Unable to find item name with Mac Address: '{mac}' in csv") + + try: + result = find_value_in_list_of_dicts(ap_csv, "MAC Address", mac.upper()) + return result + except ValueError as e: + print(e) def juniper_ap_assign( From b009012382ee4f2b8274e0a03ecbc26267a80114 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Fri, 21 Jun 2024 10:57:27 +0000 Subject: [PATCH 2/2] Commit changes made by code formatters --- src/juniper_ap_staging.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/juniper_ap_staging.py b/src/juniper_ap_staging.py index b1d6c55..d809c04 100644 --- a/src/juniper_ap_staging.py +++ b/src/juniper_ap_staging.py @@ -147,11 +147,13 @@ def build_rename_ap_payload( return payload + def find_value_in_list_of_dicts(list_of_dicts, key, value): for dictionary in list_of_dicts: if dictionary.get(key) == value: return dictionary - raise ValueError(f"Value '{value}' not found for key '{key}' in the list of dictionaries.") + raise ValueError( + f"Value '{value}' not found for key '{key}' in the list of dictionaries.") def find_inventory_item_by_mac_address( @@ -171,11 +173,12 @@ def find_csv_item_by_mac_address( mac: str ) -> list[dict]: - try: - result = find_value_in_list_of_dicts(ap_csv, "MAC Address", mac.upper()) - return result - except ValueError as e: - print(e) + try: + result = find_value_in_list_of_dicts( + ap_csv, "MAC Address", mac.upper()) + return result + except ValueError as e: + print(e) def juniper_ap_assign(