From d4ed6325a2e2b4d1533347ad92c0963bf223d594 Mon Sep 17 00:00:00 2001 From: mamullen13316 Date: Thu, 21 Dec 2023 12:37:06 -0500 Subject: [PATCH] feat: Add replace option to update_ip_hostgroup (#48) --- pyproject.toml | 2 +- sophosfirewall_python/firewallapi.py | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 3a4ccb2..9f9d03b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,7 +3,7 @@ name = "sophosfirewall-python" packages = [ { include = "sophosfirewall_python" }, ] -version = "0.1.28" +version = "0.1.29" description = "Python SDK for Sophos Firewall" authors = ["Matt Mullen "] readme = "README.md" diff --git a/sophosfirewall_python/firewallapi.py b/sophosfirewall_python/firewallapi.py index 7ec2a2d..a4adf71 100644 --- a/sophosfirewall_python/firewallapi.py +++ b/sophosfirewall_python/firewallapi.py @@ -995,13 +995,14 @@ def update_ip_hostgroup( name (str): IP Host Group name. description (str): IP Host Group description. host_list (str): List of IP Hosts to be added to or removed from the Host List. - action (str): Add or Remove from Host list. Specify None to disable updating Host List. Defaults to Add. + action (str): Add/Remove from Host List, or Replace existing Host List. Specify None to disable updating Host List. Defaults to Add. debug (bool, optional): Enable debug mode. Defaults to False. Returns: dict: XML response converted to Python dictionary """ # Get the existing Host list first, if any + resp = self.get_ip_hostgroup(name=name) if "HostList" in resp["Response"]["IPHostGroup"]: exist_list = ( @@ -1009,6 +1010,10 @@ def update_ip_hostgroup( ) else: exist_list = None + + if action.lower() == "replace": + exist_list = None + new_host_list = [] if exist_list: if isinstance(exist_list, str): @@ -1019,8 +1024,10 @@ def update_ip_hostgroup( if action: if action.lower() == "add" and not ip_host in new_host_list: new_host_list.append(ip_host) - elif action == "remove".lower() and ip_host in new_host_list: + elif action.lower() == "remove" and ip_host in new_host_list: new_host_list.remove(ip_host) + elif action.lower() == "replace": + new_host_list.append(ip_host) if not description: description = resp.get("Response").get("IPHostGroup").get("Description")