awlsring / terraform-provider-headscale

A terraform provider for Headscale
https://registry.terraform.io/providers/awlsring/headscale/latest
Apache License 2.0
22 stars 2 forks source link

Error deleting Headscale user due to "User not empty: node(s) found" when using Terraform #8

Closed Ajaiswal2610 closed 1 month ago

Ajaiswal2610 commented 1 month ago

Description: When creating a user with Terraform along with their pre-auth keys for use in a VM instance template, the user is successfully created and associated with the instance. However, when Terraform deletes the VM, it deletes the associated pre-auth keys successfully but fails to delete the user. headscale plugin should delete all nodes under the user that need to be deleted

` Error deleting user
Could not remove user, unexpected error: [DELETE /api/v1/user/{name}][500]
HeadscaleService_DeleteUser default &{Code:2 Details:[] Message:User not empty: node(s) found}

`

Please find me a resolution , my Infrastructure servers are failing

awlsring commented 1 month ago

Yeah playing with this I see via the API, a user can have no nodes assigned when the delete request comes in, otherwise it returns an error like you shared.

The method I found here is to delete all nodes, the call delete on the user as there is no force function on the API I see. I'll need to brush up on the terraform docs to see if I can make running this require some prompt, as just deleting a nodes on a user by accident could be pretty bad.

Im busy with many real work things at the moment, so I won't be able to address this until October. Its on my radar and I'll pick it up in that month.

awlsring commented 1 month ago

Actually was able to carve some timeout for this tonight.

My solution here is to add a force_delete option to a user. When terraform goes to delete a user, if it has nodes assigned to it and this is false, it will not do anything. If this is true, it will forcefully delete all nodes the delete the user.

For you to use this, you will first need to first update your resource with to have the property force_delete and run terraform apply, settings this value to true. Once that is done, you should be able to delete the resource.

Ajaiswal2610 commented 1 month ago

Actually was able to carve some timeout for this tonight.

My solution here is to add a force_delete option to a user. When terraform goes to delete a user, if it has nodes assigned to it and this is false, it will not do anything. If this is true, it will forcefully delete all nodes the delete the user.

For you to use this, you will first need to first update your resource with to have the property force_delete and run terraform apply, settings this value to true. Once that is done, you should be able to delete the resource.

is this already implemented ??