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

Provide an alternative way to retrieve available public ips from a subnet #36

Closed
julien-jean opened this issue Jan 4, 2022 · 4 comments
Labels
question Further information is requested

Comments

@julien-jean
Copy link

julien-jean commented Jan 4, 2022

Hello there !

I've been lurking about the issue about getting the first free available ip address from phpipam (phpipam/phpipam#2960) and I was wondering if there were some alternative solution.

Basically, I'd like to implement a new datasource, let's say subnet_free_addresses and would expose the available IP addresses by the following process:

  1. fetch a given subnet as CIDR format
  2. cast the CIDR address as a list of IP addresses
  3. fetch the list of addresses of the given subnet
  4. remove all used address from the list computed in step 2

Having a PHPIPAM endpoint that would retrieve a subnet available ips would drastically simplify that process as it would remove all CIDR computations.

Once that data source would be available, we could write our modules like so :

data "phpipam_subnet_free_addresses" "free_addresses" {
  subnet_id = 12
}

resource "vmware_vm" "my_vms" {
  count = 3
  // .... //
  ipv4_addr = data.phpipam_subnet_free_addresses.free_addresses[count.index].ipv4_addr
}

It feels to me like it is a good alternative to the first_free_address endpoint usage which does not work in our case. What do you think about it ?

If the solution seems convenient to you, I'd like to suggest a pull request that would implement such solution.

Bye !

@julien-jean
Copy link
Author

julien-jean commented Jan 7, 2022

I ended up with something like this seems fully functional. I'm quite new to phpipam so I don't know if there are some edge cases where it won't work but it fits our internal needs for now.

Let's say I need to get 10 free IP addresses :

data "phpipam_subnet" "my_subnet" {
  subnet_address = "192.168.1.0"
  subnet_mask    = 24
}

# this new data source retrieves the nth free addresses of a subnet 
# and fails if there is not enough available addresses
data "phpipam_nth_free_addresses" "ten_addresses" {
  number    = 10
  subnet_id = data.phpipam_subnet.my_subnet.subnet_id
}

output "some_free_addresses" {
  value = data.phpipam_nth_free_addresses.ten_addresses
}

# reuse the ten_addresses source the way you want when spinning VMs 
# don't forget to use the `phpipam_address` resource for each IP address !

I ended by adding a number property because retrieving all the free ip addresses of a subnet can be quite complex and irrelevant (thus I guess that anyone should be able to compute the number of desired IPs).

I'm cleaning my work then I'll create a pull request !

@pavel-z1
Copy link
Collaborator

Hi @julien-jean

The task with multiple IPs has been a long time ago for me.
I can't remember all the nuances of the phpipam problem with receiving free_address in loop.

In my case, previously, I needed to get the last IP address in a loop from different subnets, not from the same subnet.
I was able to solve this with terraforms using loops and explicit use of subnet data source indexes.
I don't think my code would be readable here.

Feel free to create your Pull Request. Maybe new feature will be useful to other users of this terraform provider.

@pavel-z1 pavel-z1 added the question Further information is requested label Jun 14, 2022
@lord-kyron
Copy link
Owner

@julien-jean Will you provide some pull request about this issue or I can close this ?

@lord-kyron
Copy link
Owner

Closed because of inactivity

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

No branches or pull requests

3 participants