equinix / terraform-provider-metal

DEPRECATED Equinix Metal standalone TF provider. Use the unified Equinix Terraform Provider.
https://registry.terraform.io/providers/equinix/equinix/latest/docs/guides/migration_guide_equinix_metal
Mozilla Public License 2.0
14 stars 11 forks source link

Plugin panics on terraform destroy after failed terraform apply #195

Closed arianvp closed 2 years ago

arianvp commented 3 years ago

Community Note

Terraform Version

Terraform v1.0.8
on linux_amd64
+ provider registry.terraform.io/equinix/metal v3.2.0
+ provider registry.terraform.io/hashicorp/null v3.1.0
+ provider registry.terraform.io/hashicorp/random v3.1.0
+ provider registry.terraform.io/poseidon/ct v0.6.1

Affected Resource(s)

Terraform Configuration Files

https://github.com/arianvp/packet-ipv6-kubernetes/blob/metal/provision.tf

Debug Output

https://gist.github.com/arianvp/6c670ca5be1f4541c1c12d9b67942409

Panic Output

https://gist.github.com/arianvp/6c670ca5be1f4541c1c12d9b67942409

Expected Behavior

Resources get destroyed

Actual Behavior

Terraform panics

Steps to Reproduce

  1. terraform apply
  2. get a:
    
    metal_device.worker[1]: Creating...
    metal_device.worker[0]: Creating...
    ╷
    │ Error: Sorry! You have reached the maximum number of t1.small.x86 devices for this project (5). The Equinix Metal team has been notified of this and will be in touch shortly. If this is urgent, please email support@equinixmetal.com to request additional deployment capacity.
    │ 
    │ 
    │   with metal_device.worker[0],
    │   on provision.tf line 111, in resource "metal_device" "worker":
    │  111: resource "metal_device" "worker" {
    │ 
    ╵
    ╷
    │ Error: Sorry! You have reached the maximum number of t1.small.x86 devices for this project (5). The Equinix Metal team has been notified of this and will be in touch shortly. If this is urgent, please email support@equinixmetal.com to request additional deployment capacity.
    │ 
    │ 
    │   with metal_device.worker[1],
    │   on provision.tf line 111, in resource "metal_device" "worker":
    │  111: resource "metal_device" "worker" {

2. Ran out of capacity of `t1.small` instances
3. Decided to run  `terraform destroy` to destroy the entire project
4. Terraform panics

### References

<!---
Information about referencing Github Issues: https://help.github.com/articles/basic-writing-and-formatting-syntax/#referencing-issues-and-pull-requests

Are there any other GitHub issues (open or closed) or pull requests that should be linked here? Vendor documentation? For example:
--->

* #0000
t0mk commented 3 years ago

Hey @arianvp thanks for reporting and for the logs, I'll take a look.

Please remove X-Auth-Token lines from the 2nd gist, it's your EM token.

@displague The issue template says

Note: the debug log will contain your API key, please review the log and delete sensitive secrets before submitting.

Maybe we can change it to:

Note: the debug log will contain your API key, please search the log for "X-Auth-Token" (HTTP request header holding the API token) and remove it.
t0mk commented 3 years ago

The nil deref comes from a very common cast from the Read method of device resource. The meta interface is passed to every provider method. It's quite strange, I've never seen it before. I also don't think that any provider checks the meta interface for nil.

func resourceMetalDeviceRead(d *schema.ResourceData, meta interface{}) error {
    client := meta.(*packngo.Client)  // <<<<<<<<<<<<<<<<< HERE
arianvp commented 3 years ago

@t0mk I've deleted the Auth Token from my account. thanks for the heads up :)

t0mk commented 3 years ago

Relevant search: https://github.com/search?q=respond+to+the+plugin.%28%2AGRPCProvider%29.UpgradeResourceState+call&type=issues

t0mk commented 3 years ago

@arianvp reading code and issues didn't help, so I will try to hit the limit for a project.

t0mk commented 3 years ago

@arianvp @displague I spent some time debugging, and I got similar panic when getting HTTP error with empty error slice, like this:

---[ REQUEST ]---------------------------------------
DELETE /metal/v1/projects/0a286170-17fc-43a0-9b68-4ddfb41c4f4c HTTP/1.1
Host: api.equinix.com
User-Agent: HashiCorp Terraform/0.11+compatible (+https://www.terraform.io) Terraform Plugin SDK/2.7.0 terraform-provider-metal/dev packngo/v0.19.1
Connection: close
Accept: application/json
Content-Type: application/json
X-Consumer-Token: aZ9GmqHTPtxevvFq9SK3Pi2yr9YCbRzduCSXF2SNem5sjB91mDq7Th3ZwTtRqMWZ
Accept-Encoding: gzip

-----------------------------------------------------: timestamp=2021-10-18T18:19:22.374+0300
2021-10-18T18:19:23.206+0300 [INFO]  provider.terraform-provider-metal: 2021/10/18 18:19:23 [DEBUG] Equinix Metal API Response Details:
---[ RESPONSE ]--------------------------------------
HTTP/1.1 422 Unprocessable Entity
Connection: close
Content-Length: 13
Cache-Control: no-cache
Content-Type: application/json; charset=utf-8
Date: Mon, 18 Oct 2021 15:19:23 GMT
Strict-Transport-Security: max-age=15724800; includeSubDomains
X-Request-Id: 6e1b741e5249ce3f03812ebad8fd1770

{
 "errors": []
}

Terraform SDK then soemhow receives an empty error, or even some struct that is not the error actually.

The error handling is really hard to debug at this point, but I'll try to fix it and submit a PR with a fix. Once that's in, we will re-release and you can try again.

displague commented 2 years ago

Fix released in v3.2.1