infobloxopen / infoblox-client

Infoblox NIOS Python WAPI Client
Apache License 2.0
142 stars 104 forks source link

How to get the next available network in a network container? #358

Open conradmcha opened 1 year ago

conradmcha commented 1 year ago

Both of these don't work: network = conn.get_object('networkcontainer', {'network_container': '10.88.0.0/13', 'network_view':'default'})

and

network = objects.IPAllocation.next_available_ip_from_cidr('default', '10.88.0.0/13') not sure how to find the next /24 network in this container.

ljluestc commented 1 year ago
from infoblox_client import connector
from infoblox_client import objects

# Initialize the Infoblox connection
conn = connector.Connector(
    server="infoblox-server",
    username="your-username",
    password="your-password"
)

# Define the network container and subnet mask
network_container = '10.88.0.0/13'
subnet_mask = '255.255.255.0'  # /24 subnet mask

# Retrieve the network container object
network_containers = objects.NetworkContainer.search(conn, cidr=network_container)
if network_containers:
    network_container = network_containers[0]
else:
    print("Network container not found")
    exit()

# Find the next available /24 network within the network container
next_available_subnet = None

for subnet in network_container.subnets:
    if subnet['subnet'] == subnet_mask and subnet['status'] == 'Unused':
        next_available_subnet = subnet
        break

if next_available_subnet:
    print(f"Next available /24 network: {next_available_subnet['network']}")
else:
    print("No available /24 networks found")
hemanthKa677 commented 1 year ago

Hi @ljluestc , Thanks for the response, but I have tried tested that code, this is failing to get the next available network.

from infoblox_client import connector
from infoblox_client import objects

opts = {'host': 'infoblox-server', 'username': 'your-username', 'password': 'your-password'}

# Initialize the Infoblox connection
conn = connector.Connector(opts)

# Define the network container and subnet mask
network_container = '10.88.0.0/13'
netmask = 24 # /24 netmask

# Retrieve the network container object
network_containers = objects.NetworkContainer.search(conn, cidr=network_container)
if network_containers:
    ref = network_containers._ref
else:
    print("Network container not found")
    exit()

next_available_net = conn.call_func("next_available_network", ref, {'cidr': netmask})
print(next_available_net)

Hi @napstercc , I have updated the code mention, Please check and try above code , this will work.

srgoni commented 1 year ago

You can actually call this function on the container object, there's no need to use call_func:

cont = objects.NetworkContainer.search(....)
nxt = cont.next_available_network(payload={"cidr":netmask})
net = nxt['networks'][0]
pcamera123 commented 1 year ago

Question I'm only able to get this to return the very first container found, I am specifying a target container but it appears to only be returning the first container. Example would be targetSub=10.200.0.0/16 but script is returning 10.30.0.0/16

Disregard solved it by doing the below, if appDeployment == "AZ": ea = objects.EA({'datacenter': 'DC4 (Azure)'}) network_containers = objects.NetworkContainerV4.search(connection, search_extattrs=ea)

mask network_container = '10.88.0.0/13' netmask = 24 # /24 netmask

Retrieve the network container object

network_containers = objects.NetworkContainer.search(conn,