diff --git a/gso/oss-params-example.json b/gso/oss-params-example.json index 007b899bf956cca6cdf148d79001637d181b5939..2ef1cd14749760e423c59676896c32638286989b 100644 --- a/gso/oss-params-example.json +++ b/gso/oss-params-example.json @@ -18,31 +18,36 @@ "V4": {"containers": [], "networks": ["1.1.0.0/24"], "mask": 0}, "V6": {"containers": [], "networks": ["dead:beef::/64"], "mask": 0}, "domain_name": ".lo", - "dns_view": "default" + "dns_view": "default", + "network_view": "default" }, "TRUNK": { "V4": {"containers": ["1.1.1.0/24"], "networks": [], "mask": 31}, "V6": {"containers": ["dead:beef::/64"], "networks": [], "mask": 126}, "domain_name": ".trunk", - "dns_view": "default" + "dns_view": "default", + "network_view": "default" }, "GEANT_IP": { "V4": {"containers": ["1.1.2.0/24"], "networks": [], "mask": 31}, "V6": {"containers": ["dead:beef::/64"], "networks": [], "mask": 126}, "domain_name": ".geantip", - "dns_view": "default" + "dns_view": "default", + "network_view": "default" }, "SI": { "V4": {"containers": ["1.1.3.0/24"], "networks": [], "mask": 31}, "V6": {"containers": ["dead:beef::/64"], "networks": [], "mask": 126}, "domain_name": ".si", - "dns_view": "default" + "dns_view": "default", + "network_view": "default" }, "LT_IAS": { "V4": {"containers": ["1.1.4.0/24"], "networks": [], "mask": 31}, "V6": {"containers": ["dead:beef::/64"], "networks": [], "mask": 126}, "domain_name": ".ltias", - "dns_view": "default" + "dns_view": "default", + "network_view": "default" } }, "MONITORING": { diff --git a/gso/services/infoblox.py b/gso/services/infoblox.py index 0c7176deb5dd41b6a1c6d7cf5dcea30a2992a4ba..6838ad9eada39ad5fe3999f0f28096e04216388a 100644 --- a/gso/services/infoblox.py +++ b/gso/services/infoblox.py @@ -41,29 +41,32 @@ def _setup_connection() -> tuple[connector.Connector, IPAMParams]: def _allocate_network( - conn: connector.Connector, dns_view: str, netmask: int, containers: list[str], comment: str | None = "" + conn: connector.Connector, + dns_view: str, + network_view: str, + netmask: int, + containers: list[str], + comment: str | None = "", ) -> ipaddress.IPv4Network | ipaddress.IPv6Network: """Allocate a new network in Infoblox. The function will go over all given containers, and try to allocate a network within the available IP space. If no space is available, this method raises an :class:`AllocationError`. - :param conn: An active Infoblox connection. - :type conn: :class:`infoblox_client.connector.Connector` - :param dns_view: The Infoblox ``dns_view`` in which the network should be allocated. - :type dns_view: str - :param netmask: The netmask of the desired network. Can be up to 32 for v4 networks, and 128 for v6 networks. - :type netmask: int - :param containers: A list of network containers in which the network should be allocated, given in :term:`CIDR` - notation. - :type containers: list[str] - :param comment: Optionally, a comment can be added to the network allocation. - :type comment: str, optional + :param :class:`infoblox_client.connector.Connector` conn: An active Infoblox connection. + :param str dns_view: The Infoblox ``dns_view`` in which the network should be allocated. + :param str network_view: The Infoblox ``network_view`` where the network should be allocated. + :param int netmask: The netmask of the desired network. Can be up to 32 for v4 networks, and 128 for v6 networks. + :param list [str] containers: A list of network containers in which the network should be allocated, given in + :term:`CIDR` notation. + :param str comment: Optionally, a comment can be added to the network allocation. """ for container in [ipaddress.ip_network(con) for con in containers]: for network in container.subnets(new_prefix=netmask): if objects.Network.search(conn, network=str(network)) is None: - created_net = objects.Network.create(conn, network=str(network), dns_view=dns_view, comment=comment) + created_net = objects.Network.create( + conn, network=str(network), dns_view=dns_view, network_view=network_view, comment=comment + ) if created_net.response != "Infoblox Object already Exists": return ipaddress.ip_network(created_net.network) msg = f"IP container {container} appears to be full." @@ -104,8 +107,9 @@ def allocate_v4_network(service_type: str, comment: str | None = "") -> ipaddres netmask = getattr(oss, service_type).V4.mask containers = getattr(oss, service_type).V4.containers dns_view = getattr(oss, service_type).dns_view + network_view = getattr(oss, service_type).network_view - return ipaddress.IPv4Network(_allocate_network(conn, dns_view, netmask, containers, comment)) + return ipaddress.IPv4Network(_allocate_network(conn, dns_view, network_view, netmask, containers, comment)) def allocate_v6_network(service_type: str, comment: str | None = "") -> ipaddress.IPv6Network: @@ -123,8 +127,9 @@ def allocate_v6_network(service_type: str, comment: str | None = "") -> ipaddres netmask = getattr(oss, service_type).V6.mask containers = getattr(oss, service_type).V6.containers dns_view = getattr(oss, service_type).dns_view + network_view = getattr(oss, service_type).network_view - return ipaddress.IPv6Network(_allocate_network(conn, dns_view, netmask, containers, comment)) + return ipaddress.IPv6Network(_allocate_network(conn, dns_view, network_view, netmask, containers, comment)) def find_network_by_cidr( @@ -184,10 +189,11 @@ def allocate_host( allocation_networks_v4 = getattr(oss, service_type).V4.networks allocation_networks_v6 = getattr(oss, service_type).V6.networks dns_view = getattr(oss, service_type).dns_view + network_view = getattr(oss, service_type).network_view created_v6 = None for ipv6_range in allocation_networks_v6: - v6_alloc = objects.IPAllocation.next_available_ip_from_cidr(dns_view, str(ipv6_range)) + v6_alloc = objects.IPAllocation.next_available_ip_from_cidr(network_view, str(ipv6_range)) ipv6_object = objects.IP.create(ip=v6_alloc, mac=NULL_MAC, configure_for_dhcp=False) try: new_host = objects.HostRecord.create( @@ -197,6 +203,7 @@ def allocate_host( aliases=cname_aliases, comment=comment, dns_view=dns_view, + network_view=network_view, ) created_v6 = ipaddress.IPv6Address(new_host.ipv6addr) except InfobloxCannotCreateObject: @@ -209,7 +216,7 @@ def allocate_host( created_v4 = None for ipv4_range in allocation_networks_v4: - v4_alloc = objects.IPAllocation.next_available_ip_from_cidr(dns_view, str(ipv4_range)) + v4_alloc = objects.IPAllocation.next_available_ip_from_cidr(network_view, str(ipv4_range)) ipv4_object = objects.IP.create(ip=v4_alloc, mac=NULL_MAC, configure_for_dhcp=False) new_host = objects.HostRecord.search(conn, name=hostname) new_host.ipv4addrs = [ipv4_object] @@ -240,7 +247,8 @@ def create_host_by_ip( :param str hostname: The :term:`FQDN` of the new host. :param IPv4Address ipv4_address: The IPv4 address of the new host. :param IPv6Address ipv6_address: The IPv6 address of the new host. - :param str service_type: The relevant service type, used to deduce the correct ``dns_view`` in Infoblox. + :param str service_type: The relevant service type, used to deduce the correct ``dns_view`` and ``network_view`` in + Infoblox. :param str comment: The comment stored in this Infoblox record, most likely the relevant ``subscription_id`` in :term:`GSO`. """ @@ -252,9 +260,12 @@ def create_host_by_ip( ipv6_object = objects.IP.create(ip=ipv6_address, mac=NULL_MAC, configure_for_dhcp=False) ipv4_object = objects.IP.create(ip=ipv4_address, mac=NULL_MAC, configure_for_dhcp=False) dns_view = getattr(oss, service_type).dns_view + network_view = getattr(oss, service_type).network_view # This needs to be done in two steps, otherwise only one of the IP addresses is stored. - objects.HostRecord.create(conn, ip=ipv6_object, name=hostname, comment=comment, dns_view=dns_view) + objects.HostRecord.create( + conn, ip=ipv6_object, name=hostname, comment=comment, dns_view=dns_view, network_view=network_view + ) new_host = find_host_by_fqdn(hostname) new_host.ipv4addrs = [ipv4_object] new_host.update() diff --git a/gso/settings.py b/gso/settings.py index 0189986d9cdf15c347393f2a55c1f23944922b36..2055e96c78e9177aad9f2b06b2871666b4eced70 100644 --- a/gso/settings.py +++ b/gso/settings.py @@ -81,6 +81,7 @@ class ServiceNetworkParams(BaseSettings): V6: V6NetworkParams domain_name: str dns_view: str + network_view: str class IPAMParams(BaseSettings): diff --git a/test/conftest.py b/test/conftest.py index 895b4798208a936c137ecd32358cd38cbeb42617..b0e8ddbd1ff3a99521d1540d92f2ad519f253131 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -120,6 +120,7 @@ def configuration_data() -> dict: }, "domain_name": ".geant.net", "dns_view": "default", + "network_view": "default", }, "TRUNK": { "V4": { @@ -134,6 +135,7 @@ def configuration_data() -> dict: }, "domain_name": ".trunk", "dns_view": "default", + "network_view": "default", }, "GEANT_IP": { "V4": { @@ -148,6 +150,7 @@ def configuration_data() -> dict: }, "domain_name": ".geantip", "dns_view": "default", + "network_view": "default", }, "SI": { "V4": { @@ -158,6 +161,7 @@ def configuration_data() -> dict: "V6": {"containers": [], "networks": [], "mask": 126}, "domain_name": ".geantip", "dns_view": "default", + "network_view": "default", }, "LT_IAS": { "V4": { @@ -172,6 +176,7 @@ def configuration_data() -> dict: }, "domain_name": ".geantip", "dns_view": "default", + "network_view": "default", }, }, "MONITORING": {