lord-kyron / terraform-provider-phpipam

Terrform provider for PHPIPAM
https://registry.terraform.io/providers/lord-kyron/phpipam/latest
Apache License 2.0
54 stars 30 forks source link

Question -> Resource "phpipam_address" "newip" with "phpipam_first_free_address" #55

Closed diegoaugusto-santana closed 1 year ago

diegoaugusto-santana commented 1 year ago

Hello everybody! I apologize if this is a very obvious question, I am in a more initial step related to Terraform and I need to deliver a functionality where it searches phpIPAM for the first free address of a subnet.

In the tests I'm doing, following the documentation, whenever I run a new terraform apply, in my view, it should every time reserve a new address in the specified subnet or am I wrong? The current behavior when I run terraform apply, it manages to read the next_address correctly, but it returns no changes and also does not create a new resource. Observation: In the first execution, terraform works perfectly and already reserves the address, but in the next ones, it has this behavior that confuses me.

Maybe in the prints you can understand better:

phpIPAM withou any address inserted by Terraform: image

After running the first terraform apply, as mentioned, the resource was correctly reserved. image

From that point on, as I mentioned, from what I understand, each new terraform apply, the Terraform would be reserving a new IP.

image

Note that it does not try to create a new IP record and instead it edits the current record previously created, just changing the hostname, also note that it managed to read the next IP correctly, which would be 10.0.0.4

diegoaugusto-santana commented 1 year ago

Note that in the resource data "phpipam_subnet" "subnet" on line 18, I chose to create a var and assign the value of the subnet ID to it, it worked perfectly, but I also tested deactivating this var and passing the subnet_address and subnet_mask according to the doc and I ended up having the same problem.

lord-kyron commented 1 year ago

@pavel-z1 Some idea here?

diegoaugusto-santana commented 1 year ago

As a workaround, i'm removing every time the .tfstate file and Work. I can see this comportment coming from tfstate, cause when I already have one, the Terraform try edit my resource previously created, instead created a New one.

lord-kyron commented 1 year ago

@diegoaugusto-santana , I think you are using the wrong approach. You are getting data for the first free IP address, and then using the phpipam_address resource. Instead, you should first gather data for the subnet you want to work with , and then use the phpipam_first_free_address resource. Here is my example:


data "phpipam_subnet" "subnet" {
  subnet_address = var.subnet
  subnet_mask    = var.subnet_mask
}

resource "phpipam_first_free_address" "new_ip" {
  count = var.counter
  subnet_id   = data.phpipam_subnet.subnet.subnet_id
  hostname    = "${var.hostname}"
  description = var.description

  lifecycle {
    ignore_changes = [
      subnet_id,
    ]
  }
}

output "phpipam_new_address" {
  value = phpipam_first_free_address.new_ip.*.ip_address
}
diegoaugusto-santana commented 1 year ago

Yes, I ser this after open the issue, actually I using like you mentioned