Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
conradmcha opened this issue Dec 9, 2022 · 4 comments
Open

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

conradmcha opened this issue Dec 9, 2022 · 4 comments

Comments

@conradmcha
Copy link

conradmcha commented Dec 9, 2022

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
Copy link

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
Copy link

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
Copy link

srgoni commented Oct 18, 2023

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
Copy link

pcamera123 commented Oct 20, 2023

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,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants