Skip to content

Commit

Permalink
👔 Optimize delete_global_xyz() when ADOM blocks deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
patrikspiess committed Sep 26, 2024
1 parent 85b7fbf commit 5c8c7e8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
48 changes: 36 additions & 12 deletions fotoobo/fortinet/fortimanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,16 @@ def delete_global_address(self, address: str, dry: bool = False) -> dict[str, An

if blocked_adoms:
log.warning("'%s' blocked by ADOM '%s'", address, ",".join(blocked_adoms))
result = address_object
result["status"] = {
"code": 601,
"message": f"Used in ADOM {','.join(blocked_adoms)}",
}

# Try to delete the global address object
url: str = f"/pm/config/global/obj/firewall/address/{address}"
result = self.api_delete(url).json()["result"][0]
else:
# Try to delete the global address object
url: str = f"/pm/config/global/obj/firewall/address/{address}"
result = self.api_delete(url).json()["result"][0]

else:
log.info("DRY-RUN: Would remove global address '%s'", address)
Expand Down Expand Up @@ -398,10 +404,16 @@ def delete_global_address_group(self, group: str, dry: bool = False) -> dict[str

if blocked_adoms:
log.warning("'%s' blocked by ADOM '%s'", group, ",".join(blocked_adoms))
result = address_group_object
result["status"] = {
"code": 601,
"message": f"Used in ADOM {','.join(blocked_adoms)}",
}

# Try to delete the global address group object
url: str = f"/pm/config/global/obj/firewall/addrgrp/{group}"
result = self.api_delete(url).json()["result"][0]
else:
# Try to delete the global address group object
url: str = f"/pm/config/global/obj/firewall/addrgrp/{group}"
result = self.api_delete(url).json()["result"][0]

else:
log.info("DRY-RUN: Would remove global address group '%s'", group)
Expand Down Expand Up @@ -462,10 +474,16 @@ def delete_global_service(self, service: str, dry: bool = False) -> dict[str, An

if blocked_adoms:
log.warning("'%s' blocked by ADOM '%s'", service, ",".join(blocked_adoms))
result = service_object
result["status"] = {
"code": 601,
"message": f"Used in ADOM {','.join(blocked_adoms)}",
}

# Try to delete the global service object
url: str = f"/pm/config/global/obj/firewall/service/custom/{service}"
result = self.api_delete(url).json()["result"][0]
else:
# Try to delete the global service object
url: str = f"/pm/config/global/obj/firewall/service/custom/{service}"
result = self.api_delete(url).json()["result"][0]

else:
log.info("DRY-RUN: Would remove global service '%s'", service)
Expand Down Expand Up @@ -526,10 +544,16 @@ def delete_global_service_group(self, group: str, dry: bool = False) -> dict[str

if blocked_adoms:
log.warning("'%s' blocked by ADOM '%s'", group, ",".join(blocked_adoms))
result = service_group_object
result["status"] = {
"code": 601,
"message": f"Used in ADOM {','.join(blocked_adoms)}",
}

# Try to delete the global service group object
url: str = f"/pm/config/global/obj/firewall/service/group/{group}"
result = self.api_delete(url).json()["result"][0]
else:
# Try to delete the global service group object
url: str = f"/pm/config/global/obj/firewall/service/group/{group}"
result = self.api_delete(url).json()["result"][0]

else:
log.info("DRY-RUN: Would remove global service group '%s'", group)
Expand Down
8 changes: 4 additions & 4 deletions tests/fortinet/test_fortimanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ def test_delete_global_address(
MagicMock(return_value={"status": delete_adom_address_status}),
)
fmg = FortiManager("host", "", "")
assert fmg.delete_global_address("dummy")["status"]["code"] in [0, 7]
assert fmg.delete_global_address("dummy")["status"]["code"] in [0, 7, 601]

@staticmethod
@pytest.mark.usefixtures("api_get_ok", "api_delete_ok")
Expand Down Expand Up @@ -436,7 +436,7 @@ def test_delete_global_address_group(
MagicMock(return_value={"status": delete_adom_address_group_status}),
)
fmg = FortiManager("host", "", "")
assert fmg.delete_global_address_group("dummy")["status"]["code"] in [0, 7]
assert fmg.delete_global_address_group("dummy")["status"]["code"] in [0, 7, 601]

@staticmethod
@pytest.mark.usefixtures("api_get_ok", "api_delete_ok")
Expand Down Expand Up @@ -490,7 +490,7 @@ def test_delete_global_service(
MagicMock(return_value={"status": delete_adom_service_status}),
)
fmg = FortiManager("host", "", "")
assert fmg.delete_global_service("dummy")["status"]["code"] in [0, 7]
assert fmg.delete_global_service("dummy")["status"]["code"] in [0, 7, 601]

@staticmethod
@pytest.mark.usefixtures("api_get_ok", "api_delete_ok")
Expand Down Expand Up @@ -544,7 +544,7 @@ def test_delete_global_service_group(
MagicMock(return_value={"status": delete_adom_service_group_status}),
)
fmg = FortiManager("host", "", "")
assert fmg.delete_global_service_group("dummy")["status"]["code"] in [0, 7]
assert fmg.delete_global_service_group("dummy")["status"]["code"] in [0, 7, 601]

@staticmethod
@pytest.mark.usefixtures("api_get_ok", "api_delete_ok")
Expand Down

0 comments on commit 5c8c7e8

Please sign in to comment.