browningluke / terraform-provider-opnsense

OPNsense Terraform Provider
https://registry.terraform.io/providers/browningluke/opnsense/latest
MIT License
52 stars 12 forks source link

Support for resource imports #40

Closed ikaruswill closed 9 months ago

ikaruswill commented 9 months ago

Hi again!

Background My conundrum is I already have an OPNsense instance up and running and I'd like to move a majority of its configuration to be managed by this terraform provider.

I started off with creating aliases in Terraform but quickly realized that aliases, though unique by name, are not identified by the provider by name, but rather by the id attribute, and as such, new aliases of the same name, are identified as non-existent, thus results in a create action, which will fail the terraform apply as an alias with the same name already exists.

Request

browningluke commented 9 months ago

Hey there @ikaruswill.

aliases, though unique by name, are not identified by the provider by name, but rather by the id attribute

Due to the way the OPNsense API works, every resource that gets created is assigned an id, which this provider just grabs and then uses - even for resources that have other unique attributes. I have considered allowing for imports using these unique attributes, but it would require some code changes to the opnsense-go library. I currently don't have plans to implement this, but if it truly would be a valuable feature, then I will consider it.

Regarding importing, although it isn't documented (for some reason - I will investigate as to why), it should still work if you provide the ID (see below). As this has to use the ID, grabbing it from OPNsense is a bit of a pain, but hopefully not too annoying. I should (and will) include this in the documentation in an upcoming release.

Importing resources (an alias for example)

Editing a resource with the browser network monitor open lets you grab the ID of the resource. In this case: 8573647b-7cc5-4de4-812b-c5062a0c84b1

image



Then in TF, importing into a resource should work like this:

image
browningluke commented 9 months ago

The docs now include importing for all resources (in v0.10.0). Unfortunately it still involves using the OPNsense resource id, but hopefully it should be enough to get your existing setup imported.

Let me know if you run into anymore issues with this, and I'll re-open the issue!

ikaruswill commented 9 months ago

Thanks a lot. It was really helpful. I see the docs now in 0.10.0. Importing with id is exactly what I was looking for, even if it's less convenient than using other visible unique identifiers, it's still a path forward!

Modifying the opnsense-go library feels like a bit too much work for a functionality already present with the id. Appreciate your quick work, I'll proceed to import my existing setup. Have a good weekend!