cullenmcdermott / terraform-provider-porkbun

Mozilla Public License 2.0
37 stars 7 forks source link

Running Terraform destroy against records that don't exist causes it to run forever #25

Open cullenmcdermott opened 1 year ago

cullenmcdermott commented 1 year ago

If tfstate is out of sync and contains records that don't actually exist, and you run terraform destroy it runs forever because they don't exist. This also impacts create (terraform returns no changes). This would also probably break if you tried to update the record.

cullenmcdermott commented 1 year ago

Looks like this comes back as a 503 so I can't differentiate between this and a rate limit. Will have to do more digging.

cullenmcdermott commented 1 year ago

So actually when manually deleting a record and then running destroy I get an expected error

2022-12-30T20:14:19.247-0700 [INFO]  backend/local: apply calling Apply
2022-12-30T20:14:19.248-0700 [INFO]  provider: configuring client automatic mTLS
2022-12-30T20:14:19.273-0700 [INFO]  provider.terraform-provider-porkbun: configuring server automatic mTLS: timestamp=2022-12-30T20:14:19.273-0700
porkbun_dns_record.test[0]: Destroying... [id=285145526]
2022-12-30T20:14:19.337-0700 [INFO]  Starting apply for porkbun_dns_record.test[0]
2022-12-30T20:14:20.175-0700 [ERROR] provider.terraform-provider-porkbun: Response contains error diagnostic: tf_proto_version=6.3 tf_rpc=ApplyResourceChange tf_
provider_addr=registry.terraform.io/hashicorp/scaffolding tf_req_id=caa0ee66-41b0-35e7-d11f-203bf4148567 @caller=/home/cullen/go/pkg/mod/github.com/hashicorp/ter
raform-plugin-go@v0.14.0/tfprotov6/internal/diag/diagnostics.go:55 @module=sdk.proto diagnostic_detail="Error: received error is not retryable: status: 400 messa
ge: {"status":"ERROR","message":"Invalid record ID."}" diagnostic_severity=ERROR diagnostic_summary="Error deleting record" tf_resource_type=porkbun_dns_record t
imestamp=2022-12-30T20:14:20.175-0700
2022-12-30T20:14:20.182-0700 [ERROR] vertex "porkbun_dns_record.test[0] (destroy)" error: Error deleting record
╷
│ Error: Error deleting record
│ 
│ Error: received error is not retryable: status: 400 message: {"status":"ERROR","message":"Invalid record ID."}
╵

Making an update to the resource in main.tf and then running apply results in the same error.

The issue I saw originally related to the 503 is strange. I'll test more tomorrow with curl to make sure its not some weird state corruption.

cullenmcdermott commented 1 year ago

There might be some strange interaction between the rate limits and my delete logic

2023-01-08T12:31:52.667-0700 [ERROR] provider.terraform-provider-porkbun: Response contains error diagnostic: @module=sdk.proto diagnostic_detail="Error: received error is not retryable: status: 400 message: {"status":"ERROR","message":"Invalid record ID."}" tf_proto_version=6.3 tf_rpc=ApplyResourceChange diagnostic_severity=ERROR diagnostic_summary="Error deleting record" tf_provider_addr=registry.terraform.io/hashicorp/scaffolding tf_req_id=fd930350-f204-c6fe-f1a4-56a57416ec9a tf_resource_type=porkbun_dns_record @caller=/home/cullen/go/pkg/mod/github.com/hashicorp/terraform-plugin-go@v0.14.0/tfprotov6/internal/diag/diagnostics.go:55 timestamp=2023-01-08T12:31:52.667-0700
2023-01-08T12:31:52.677-0700 [ERROR] vertex "porkbun_dns_record.test[0] (destroy)" error: Error deleting record
porkbun_dns_record.test[12]: Destroying... [id=287435305]
2023-01-08T12:31:52.678-0700 [INFO]  Starting apply for porkbun_dns_record.test[12]
2023-01-08T12:31:52.740-0700 [WARN]  unexpected data: registry.terraform.io/cullenmcdermott/porkbun:stdout="retrying after error: status: 503 message: <html>
<head><title>503 Service Temporarily Unavailable</title></head>
<body>
<center><h1>503 Service Temporarily Unavailable</h1></center>
<hr><center>openresty</center>
</body>
</html>"

If we are trying to delete a record but the ID doesn't exist, we should probably just assume that's ok and remove it from state and finish.

bhean commented 1 year ago

I'm not sure if this is an error or the expected behavior... I think (maybe I'm wrong) that if you try to delete a record that is no longer exists using other Terraform providers, you get the error as well... so I'm not sure if assume that's OK and remove it from the state is the best option 🤔

dezren39 commented 3 months ago

if there are failures, the tfstate records the id=0 of an attempt, but on next apply, it tries to destroy the id=0 records, on destroy it fails because the api requires a real id.

  # porkbun_dns_record.record["example.com_@_MX_20_0"] is tainted, so it must be replaced
-/+ resource "porkbun_dns_record" "record" {
      ~ id      = "0" -> (known after apply)
porkbun_dns_record.record["example.com_@_MX_20_0"]: Destroying... [id=0]
│ Error: Error deleting record
│ 
│ Error: status: 400 message: {"status":"ERROR","message":"You need to pass an ID."}

issue is that yeah it's tainted, because of probably #64, however, there is no way to fix this from the cli. makes me want a script to wipe out all dns records for a specific domain, but that's outside terraform. when id=0 i'm not sure how best to handle it. probably needs to call out tainted, then warn about how there may need to be manual cleanup, then no-op past the record and try to create again or maybe throw and warn about manual cleanup. just not sure how to actually resolve the tainted state in this situation.

edit: i fixed the tainted records with a pile of bash scripts. more notes here: