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

Fix ErrorResponse interface to satisfy TF SDK #197

Closed t0mk closed 2 years ago

t0mk commented 3 years ago

This PR fixes the situation where the EM API returns HTTP 422, and errors: [] in the body. 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": []
}

Hopefully fixes #195

t0mk commented 2 years ago

@displague Looks good, and seem to work. I think we can't run this withou TF_ACC=1 because resource.Test is checking it inside. I didn't find any other provider which would be using mock to this extent.

Although, if I would be about to put effort into fixing the error mess, I would rather remove the ErrorResponse type from packngo, and treat body of HTTP error responses only as string. I would also remove the ErrorResponse struct here from the provider. That would clean the code a bit, because there wouldn't be casting of ErrorResponse, packngo.ErrorResponse and http.Response. Once the code would be cleaner, it would be easier to fix issues like this.