Mastercard / terraform-provider-restapi

A terraform provider to manage objects in a RESTful API
Other
808 stars 217 forks source link

Resource destroy fails with custom 'destroy_path' and 'id' #219

Open louhisuo opened 1 year ago

louhisuo commented 1 year ago

I am working with below resource which gets created without any issues but never gets destroyed. Resource object has custom destroy_path which differs from create_path and non typical ìd (in this example case npool/vms/test-vm-bootvolume) which constructed of ZFS dataset path (npool/vms) and zvol name (test-vm-bootvolume).

resource "restapi_object" "zvol" {
    provider = restapi.truenas
    path = "/pool/dataset"

    data = jsonencode({
        name = var.vm_instance.zvol.name
        type = "VOLUME"
        volsize = 1073741824 # 1GiB
        volblocksize = "8K"
    })

    create_path = "/pool/dataset"
    destroy_path = "/pool/dataset/id/{id}"
    read_path = "/pool/dataset/id/{id}"
    update_path = "/pool/dataset/id/{id}"
}

{id} required to destroy resource is same same as 'name' submitted during create and with terraform show as seen from sniplet below I can confirm that name and id matches after resource is created. I have some doubts if I should use id_attribute and did some experiments with that as well without success.

    data            = jsonencode(
        {
            name         = "npool/vms/test-vm-bootvolume"         <--- THIS
            type         = "VOLUME"
            volblocksize = "8K"
            volsize      = 1073741824
        }
    )
    destroy_path    = "/pool/dataset/id/{id}"
    id              = "npool/vms/test-vm-bootvolume"       <--- THIS
    path            = "/pool/dataset"
    read_path       = "/pool/dataset/id/{id}"
    update_path     = "/pool/dataset/id/{id}"

Any advice how to get destroy resource working?

RedStalker commented 1 year ago

Hello @kullervo610

As discussed in issue #205 there are no ways (for now) to somehow parse the ID returned form API. Maybe someone will find a workaround for this or it will be fixed on the provider's side

DRuggeri commented 1 year ago

Apologies for the slow reply. I haven't attempted this in a while, but I think you could use native variable interpolation in Terraform for this rather than the provider's interpolation. Something like this?

resource "restapi_object" "zvol" {
    provider = restapi.truenas
    path = "/pool/dataset"

    data = jsonencode({
        name = var.vm_instance.zvol.name
        type = "VOLUME"
        volsize = 1073741824 # 1GiB
        volblocksize = "8K"
    })

    create_path = "/pool/dataset"
    destroy_path = "/pool/dataset/id/${var.vm_instance.zvol.name}"
    read_path = "/pool/dataset/id/${var.vm_instance.zvol.name}"
    update_path = "/pool/dataset/id/${var.vm_instance.zvol.name}"
}

This may just work because you can predict the path based on an input