From 3e483c53b01134f2cedc273a90bf54d59e799015 Mon Sep 17 00:00:00 2001 From: Karel van Klink <karel.vanklink@geant.org> Date: Fri, 5 Apr 2024 15:31:30 +0200 Subject: [PATCH] Infoblox error behaviour more loose Don't raise exception when allocating existing host/network or deleting non-existent ones --- gso/services/infoblox.py | 15 +++++------- gso/workflows/iptrunk/migrate_iptrunk.py | 9 +------ test/services/test_infoblox.py | 30 +++++++++++------------- 3 files changed, 21 insertions(+), 33 deletions(-) diff --git a/gso/services/infoblox.py b/gso/services/infoblox.py index ca01bcab..06ee5719 100644 --- a/gso/services/infoblox.py +++ b/gso/services/infoblox.py @@ -19,10 +19,6 @@ class AllocationError(Exception): """Raised when Infoblox failed to allocate a resource.""" -class DeletionError(Exception): - """Raised when Infoblox failed to delete a resource.""" - - def _setup_connection() -> tuple[connector.Connector, IPAMParams]: """Set up a new connection with an Infoblox instance. @@ -158,7 +154,7 @@ def delete_network(ip_network: ipaddress.IPv4Network | ipaddress.IPv6Network) -> network.delete() else: msg = f"Could not find network {ip_network}, nothing has been deleted." - raise DeletionError(msg) + logger.warning(msg) def allocate_host( @@ -253,8 +249,9 @@ def create_host_by_ip( :term:`GSO`. """ if not hostname_available(hostname): - msg = f"Cannot allocate new host, FQDN {hostname} already taken." - raise AllocationError(msg) + msg = f"FQDN {hostname} already taken, nothing to be done." + logger.warning(msg) + return conn, oss = _setup_connection() ipv6_object = objects.IP.create(ip=str(ipv6_address), mac=NULL_MAC, configure_for_dhcp=False) @@ -331,7 +328,7 @@ def delete_host_by_ip(ip_addr: ipaddress.IPv4Address | ipaddress.IPv6Address) -> host.delete() else: msg = f"Could not find host at {ip_addr}, nothing has been deleted." - raise DeletionError(msg) + logger.warning(msg) def delete_host_by_fqdn(fqdn: str) -> None: @@ -348,4 +345,4 @@ def delete_host_by_fqdn(fqdn: str) -> None: host.delete() else: msg = f"Could not find host at {fqdn}, nothing has been deleted." - raise DeletionError(msg) + logger.warning(msg) diff --git a/gso/workflows/iptrunk/migrate_iptrunk.py b/gso/workflows/iptrunk/migrate_iptrunk.py index 540505d0..96ee4abb 100644 --- a/gso/workflows/iptrunk/migrate_iptrunk.py +++ b/gso/workflows/iptrunk/migrate_iptrunk.py @@ -15,7 +15,6 @@ from orchestrator.forms import FormPage from orchestrator.forms.validators import Choice, Label, UniqueConstrainedList from orchestrator.targets import Target from orchestrator.types import FormGenerator, State, UUIDstr -from orchestrator.utils.errors import ProcessFailureError from orchestrator.utils.json import json_dumps from orchestrator.workflow import StepList, conditional, done, init, inputstep from orchestrator.workflows.steps import resync, store_process_subscription, unsync @@ -28,7 +27,6 @@ from gso.products.product_blocks.iptrunk import IptrunkInterfaceBlock from gso.products.product_types.iptrunk import Iptrunk from gso.products.product_types.router import Router from gso.services import infoblox -from gso.services.infoblox import DeletionError from gso.services.lso_client import execute_playbook, lso_interaction from gso.services.netbox_client import NetboxClient from gso.services.subscriptions import get_active_router_subscriptions @@ -609,12 +607,7 @@ def update_ipam(subscription: Iptrunk, replace_index: int, new_node: Router, new v6_addr = subscription.iptrunk.iptrunk_ipv6_network[replace_index + 1] # Out with the old - try: - infoblox.delete_host_by_ip(subscription.iptrunk.iptrunk_ipv4_network[replace_index]) - except DeletionError as e: - msg = "Failed to delete record from Infoblox." - raise ProcessFailureError(msg) from e - + infoblox.delete_host_by_ip(subscription.iptrunk.iptrunk_ipv4_network[replace_index]) # And in with the new new_fqdn = f"{new_lag_interface}-0.{new_node.router.router_fqdn}" comment = str(subscription.subscription_id) diff --git a/test/services/test_infoblox.py b/test/services/test_infoblox.py index bebcdae6..3a733260 100644 --- a/test/services/test_infoblox.py +++ b/test/services/test_infoblox.py @@ -1,4 +1,5 @@ import ipaddress +import logging import re from os import PathLike @@ -7,7 +8,7 @@ import responses from requests import codes from gso.services import infoblox -from gso.services.infoblox import AllocationError, DeletionError +from gso.services.infoblox import AllocationError def _set_up_network_responses(): @@ -216,17 +217,16 @@ def test_delete_good_network(data_config_filename: PathLike): @responses.activate -def test_delete_non_existent_network(data_config_filename: PathLike): +def test_delete_non_existent_network(data_config_filename: PathLike, caplog): responses.add( method=responses.GET, url="https://10.0.0.1/wapi/v2.12/network?network=10.255.255.0%2F26&_return_fields=comment%2Cextattrs%2Cnetwork%" "2Cnetwork_view", json=[], ) - - with pytest.raises(DeletionError) as e: - infoblox.delete_network(ipaddress.IPv4Network("10.255.255.0/26")) - assert e.value.args[0] == "Could not find network 10.255.255.0/26, nothing has been deleted." + caplog.set_level(logging.WARNING) + infoblox.delete_network(ipaddress.IPv4Network("10.255.255.0/26")) + assert "Could not find network 10.255.255.0/26, nothing has been deleted." in caplog.text @responses.activate @@ -277,21 +277,19 @@ def test_delete_good_host(data_config_filename: PathLike): @responses.activate -def test_delete_bad_host(data_config_filename: PathLike): +def test_delete_bad_host(data_config_filename: PathLike, caplog): responses.add( method=responses.GET, url=re.compile(r".+"), json=[], ) + caplog.set_level(logging.WARNING) - with pytest.raises(DeletionError) as e: - infoblox.delete_host_by_ip(ipaddress.IPv4Address("10.255.255.1")) - assert e.value.args[0] == "Could not find host at 10.255.255.1, nothing has been deleted." + infoblox.delete_host_by_ip(ipaddress.IPv4Address("10.255.255.1")) + assert "Could not find host at 10.255.255.1, nothing has been deleted." in caplog.text - with pytest.raises(DeletionError) as e: - infoblox.delete_host_by_ip(ipaddress.IPv6Address("dead:beef::1")) - assert e.value.args[0] == "Could not find host at dead:beef::1, nothing has been deleted." + infoblox.delete_host_by_ip(ipaddress.IPv6Address("dead:beef::1")) + assert "Could not find host at dead:beef::1, nothing has been deleted." in caplog.text - with pytest.raises(DeletionError) as e: - infoblox.delete_host_by_fqdn("fake.host.net") - assert e.value.args[0] == "Could not find host at fake.host.net, nothing has been deleted." + infoblox.delete_host_by_fqdn("fake.host.net") + assert "Could not find host at fake.host.net, nothing has been deleted." in caplog.text -- GitLab