Skip to content
Snippets Groups Projects

Feature/add infoblox service

Merged Karel van Klink requested to merge feature/add-infoblox-service into develop
2 files
+ 136
44
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 47
10
@@ -121,6 +121,16 @@ def allocate_v6_network(service_type: str, comment: str | None = "") -> ipaddres
return ipaddress.IPv6Network(_allocate_network(conn, dns_view, netmask, containers, comment))
def find_network_by_cidr(ip_network: ipaddress.IPv4Network | ipaddress.IPv6Network) -> objects.Network | None:
"""Find a network in Infoblox by its {term}`CIDR`.
:param ip_network: The {term}`CIDR` that is searched.
:type ip_network: ipaddress.IPv4Network | ipaddress.IPv6Network
"""
conn, _ = _setup_connection()
return objects.Network.search(conn, cidr=str(ip_network))
def delete_network(ip_network: ipaddress.IPv4Network | ipaddress.IPv6Network) -> None:
"""Delete a network in Infoblox.
@@ -130,8 +140,7 @@ def delete_network(ip_network: ipaddress.IPv4Network | ipaddress.IPv6Network) ->
:param ip_network: The network that should get deleted.
:type ip_network: ipaddress.IPv4Network | ipaddress.IPv6Network
"""
conn, _ = _setup_connection()
network = objects.Network.search(conn, cidr=str(ip_network))
network = find_network_by_cidr(ip_network)
if network:
network.delete()
else:
@@ -139,7 +148,7 @@ def delete_network(ip_network: ipaddress.IPv4Network | ipaddress.IPv6Network) ->
def allocate_host(
hostname: str, service_type: str, cname_aliases: list[str], comment: str | None = ""
hostname: str, service_type: str, cname_aliases: list[str], comment: str
) -> tuple[ipaddress.IPv4Address, ipaddress.IPv6Address]:
"""Allocate a new host record in Infoblox.
@@ -154,8 +163,9 @@ def allocate_host(
:param cname_aliases: A list of any {term}`CNAME` aliases that should be associated with this host. Most often this
will be a single loopback address.
:type cname_aliases: list[str]
:param comment: Optionally, a comment can be added to the host record in Infoblox.
:type comment: str, optional
:param comment: A comment that is added to the host record in Infoblox, should be the `subscription_id` of the new
{class}`Router` subscription.
:type comment: str
"""
if not hostname_available(hostname):
raise AllocationError(f"Cannot allocate new host, FQDN {hostname} already taken.")
@@ -199,6 +209,32 @@ def allocate_host(
return created_v4, created_v6
def find_host_by_ip(ip_addr: ipaddress.IPv4Address | ipaddress.IPv6Address) -> objects.HostRecord | None:
"""Find a host record in Infoblox by its associated IP address.
:param ip_addr: The IP address of a host that is searched for.
:type ip_addr: ipaddress.IPv4Address | ipaddress.IPv6Address
"""
conn, _ = _setup_connection()
if ip_addr.version == 4:
return objects.HostRecord.search(
conn, ipv4addr=ip_addr, return_fields=["ipv4addrs", "name", "view", "aliases", "comment"]
)
return objects.HostRecord.search(
conn, ipv6addr=ip_addr, return_fields=["ipv6addrs", "name", "view", "aliases", "comment"]
)
def find_host_by_fqdn(fqdn: str) -> objects.HostRecord | None:
"""Find a host record by its associated {term}`FQDN`.
:param fqdn: The {term}`FQDN` of a host that is searched for.
:type fqdn: str
"""
conn, _ = _setup_connection()
return objects.HostRecord.search(conn, name=fqdn, return_fields=["ipv4addrs", "name", "view", "aliases", "comment"])
def delete_host_by_ip(ip_addr: ipaddress.IPv4Address | ipaddress.IPv6Address) -> None:
"""Delete a host from Infoblox.
@@ -208,8 +244,7 @@ def delete_host_by_ip(ip_addr: ipaddress.IPv4Address | ipaddress.IPv6Address) ->
:param ip_addr: The IP address of the host record that should get deleted.
:type ip_addr: ipaddress.IPv4Address | ipaddress.IPv6Address
"""
conn, _ = _setup_connection()
host = objects.HostRecord.search(conn, ipv4addr=ip_addr)
host = find_host_by_ip(ip_addr)
if host:
host.delete()
else:
@@ -225,6 +260,8 @@ def delete_host_by_fqdn(fqdn: str) -> None:
:param fqdn: The FQDN of the host record that should get deleted.
:type fqdn: str
"""
conn, _ = _setup_connection()
host = objects.HostRecord.search(conn, name=fqdn)
host.delete()
host = find_host_by_fqdn(fqdn)
if host:
host.delete()
else:
raise DeletionError(f"Could not find host at {fqdn}, nothing has been deleted.")
Loading