From 43fd59eb0c85dd08a6ce5a742a7f74485c09bf2f Mon Sep 17 00:00:00 2001 From: mamullen13316 Date: Tue, 12 Dec 2023 09:47:58 -0500 Subject: [PATCH] Feature: Add remove method (#39) --- pyproject.toml | 2 +- sophosfirewall_python/firewallapi.py | 31 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index b272eca..7e1fa3a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,7 +3,7 @@ name = "sophosfirewall-python" packages = [ { include = "sophosfirewall_python" }, ] -version = "0.1.22" +version = "0.1.23" 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 5d52ad1..b574983 100644 --- a/sophosfirewall_python/firewallapi.py +++ b/sophosfirewall_python/firewallapi.py @@ -41,6 +41,7 @@ class SophosFirewallOperatorError(Exception): """Error raised when an invalid operator is specified""" + class SophosFirewall: """Class used for interacting with the Sophos Firewall XML API""" @@ -252,6 +253,34 @@ def get_tag_with_filter( return resp.content.decode() return xmltodict.parse(resp.content.decode()) + def remove(self, xml_tag: str, name: str, output_format: str = "dict"): + """Remove an object from the firewall. + + Args: + xml_tag (str): The XML tag indicating the type of object to be removed. + name (str): The name of the object to be removed. + output_format (str): Output format. Valid options are "dict" or "xml". Defaults to dict. + """ + payload = f""" + + + {self.username} + {self.password} + + + <{xml_tag}> + {name} + + + + """ + resp = self._post(xmldata=payload) + self._error_check(resp, xml_tag) + if output_format == "xml": + return resp.content.decode() + return xmltodict.parse(resp.content.decode()) + + def _dict_to_lower(self, target_dict): """Convert the keys of a dictionary to lower-case @@ -285,6 +314,8 @@ def _error_check(self, api_response, xml_tag): raise SophosFirewallAPIError( str(xmltodict.parse(api_response.content.decode())) ) + + # METHODS FOR OBJECT RETRIEVAL (GET)