cloudflare / terraform-provider-cloudflare

Cloudflare Terraform Provider
https://registry.terraform.io/providers/cloudflare/cloudflare
Mozilla Public License 2.0
770 stars 596 forks source link

allow_overwrite argument not allowing DNS record to be overwritten in resource_cloudflare_record #1493

Closed Skyrail closed 2 years ago

Skyrail commented 2 years ago

Confirmation

Terraform and Cloudflare provider version

Terraform v1.1.7
on darwin_arm64
+ provider registry.terraform.io/cloudflare/cloudflare v3.9.1
+ provider registry.terraform.io/hashicorp/aws v4.1.0

Affected resource(s)

cloudflare_record

Terraform configuration files

terraform {

  required_providers {
    cloudflare = {
      source  = "cloudflare/cloudflare"
      version = "3.9.1"
    }
  }

  required_version = ">= 0.14.9"
}

########################################################################
# Provider
########################################################################

provider "cloudflare" {
  api_token  = var.cloudflare_api_token
  account_id = var.cloudflare_account_id
}

########################################################################
# Variables
########################################################################

variable "cloudflare_api_token" {
  type        = string
  description = "The CloudFlare API key"
}

variable "cloudflare_account_id" {
  type        = string
  description = "The CloudFlare account number"
}

########################################################################
# Resource
########################################################################

resource "cloudflare_record" "record" {
  allow_overwrite = true
  proxied         = false
  name            = "<NAME OF RECORD THAT ALREADY EXISTS>"
  value           = "<VALUE OF RECORD THAT ALREADY EXISTS>"
  ttl             = 60
  type            = "CNAME"
  zone_id         = "<ZONE_ID>"
}

Debug output

2022-03-04T14:05:14.440Z [INFO] Terraform version: 1.1.7 2022-03-04T14:05:14.440Z [INFO] Go runtime version: go1.17.2 2022-03-04T14:05:14.440Z [INFO] CLI args: []string{"terraform", "apply", "-auto-approve"} 2022-03-04T14:05:14.440Z [DEBUG] Attempting to open CLI config file: /Users/aled/.terraformrc 2022-03-04T14:05:14.440Z [INFO] Loading CLI configuration from /Users/aled/.terraformrc 2022-03-04T14:05:14.441Z [INFO] Loading CLI configuration from /Users/aled/.terraform.d/credentials.tfrc.json 2022-03-04T14:05:14.441Z [DEBUG] Explicit provider installation configuration is set 2022-03-04T14:05:14.442Z [INFO] CLI command args: []string{"apply", "-auto-approve"} 2022-03-04T14:05:14.445Z [DEBUG] New state was assigned lineage "d154efc1-d00c-d2bd-9567-b585b3d7c8c0" 2022-03-04T14:05:14.457Z [DEBUG] checking for provisioner in "." 2022-03-04T14:05:14.461Z [DEBUG] checking for provisioner in "/opt/homebrew/bin" 2022-03-04T14:05:14.462Z [INFO] backend/local: starting Apply operation 2022-03-04T14:05:14.466Z [DEBUG] created provider logger: level=debug 2022-03-04T14:05:14.466Z [INFO] provider: configuring client automatic mTLS 2022-03-04T14:05:14.474Z [DEBUG] provider: starting plugin: path=.terraform/providers/registry.terraform.io/cloudflare/cloudflare/3.9.1/darwin_arm64/terraform-provider-cloudflare_v3.9.1 args=[.terraform/providers/registry.terraform.io/cloudflare/cloudflare/3.9.1/darwin_arm64/terraform-provider-cloudflare_v3.9.1] 2022-03-04T14:05:14.476Z [DEBUG] provider: plugin started: path=.terraform/providers/registry.terraform.io/cloudflare/cloudflare/3.9.1/darwin_arm64/terraform-provider-cloudflare_v3.9.1 pid=93366 2022-03-04T14:05:14.476Z [DEBUG] provider: waiting for RPC address: path=.terraform/providers/registry.terraform.io/cloudflare/cloudflare/3.9.1/darwin_arm64/terraform-provider-cloudflare_v3.9.1 2022-03-04T14:05:14.489Z [INFO] provider.terraform-provider-cloudflare_v3.9.1: configuring server automatic mTLS: timestamp=2022-03-04T14:05:14.489Z 2022-03-04T14:05:14.496Z [DEBUG] provider.terraform-provider-cloudflare_v3.9.1: plugin address: address=/var/folders/zt/1hnkb2rn4kx05dhztp2kf91w0000gp/T/plugin3622745510 network=unix timestamp=2022-03-04T14:05:14.496Z 2022-03-04T14:05:14.496Z [DEBUG] provider: using plugin: version=5 2022-03-04T14:05:14.517Z [DEBUG] provider.stdio: received EOF, stopping recv loop: err="rpc error: code = Unavailable desc = transport is closing" 2022-03-04T14:05:14.517Z [DEBUG] provider: plugin process exited: path=.terraform/providers/registry.terraform.io/cloudflare/cloudflare/3.9.1/darwin_arm64/terraform-provider-cloudflare_v3.9.1 pid=93366 2022-03-04T14:05:14.517Z [DEBUG] provider: plugin exited 2022-03-04T14:05:14.517Z [DEBUG] Building and walking validate graph 2022-03-04T14:05:14.518Z [DEBUG] ProviderTransformer: "cloudflare_record.record" (terraform.NodeValidatableResource) needs provider["registry.terraform.io/cloudflare/cloudflare"] 2022-03-04T14:05:14.518Z [DEBUG] ReferenceTransformer: "var.cloudflare_api_token" references: [] 2022-03-04T14:05:14.518Z [DEBUG] ReferenceTransformer: "var.cloudflare_account_id" references: [] 2022-03-04T14:05:14.518Z [DEBUG] ReferenceTransformer: "provider[\"registry.terraform.io/cloudflare/cloudflare\"]" references: [var.cloudflare_api_token var.cloudflare_account_id] 2022-03-04T14:05:14.518Z [DEBUG] ReferenceTransformer: "cloudflare_record.record" references: [] 2022-03-04T14:05:14.518Z [DEBUG] Starting graph walk: walkValidate 2022-03-04T14:05:14.519Z [DEBUG] created provider logger: level=debug 2022-03-04T14:05:14.519Z [INFO] provider: configuring client automatic mTLS 2022-03-04T14:05:14.526Z [DEBUG] provider: starting plugin: path=.terraform/providers/registry.terraform.io/cloudflare/cloudflare/3.9.1/darwin_arm64/terraform-provider-cloudflare_v3.9.1 args=[.terraform/providers/registry.terraform.io/cloudflare/cloudflare/3.9.1/darwin_arm64/terraform-provider-cloudflare_v3.9.1] 2022-03-04T14:05:14.527Z [DEBUG] provider: plugin started: path=.terraform/providers/registry.terraform.io/cloudflare/cloudflare/3.9.1/darwin_arm64/terraform-provider-cloudflare_v3.9.1 pid=93367 2022-03-04T14:05:14.527Z [DEBUG] provider: waiting for RPC address: path=.terraform/providers/registry.terraform.io/cloudflare/cloudflare/3.9.1/darwin_arm64/terraform-provider-cloudflare_v3.9.1 2022-03-04T14:05:14.535Z [INFO] provider.terraform-provider-cloudflare_v3.9.1: configuring server automatic mTLS: timestamp=2022-03-04T14:05:14.535Z 2022-03-04T14:05:14.542Z [DEBUG] provider.terraform-provider-cloudflare_v3.9.1: plugin address: address=/var/folders/zt/1hnkb2rn4kx05dhztp2kf91w0000gp/T/plugin250247033 network=unix timestamp=2022-03-04T14:05:14.542Z 2022-03-04T14:05:14.542Z [DEBUG] provider: using plugin: version=5 2022-03-04T14:05:14.565Z [DEBUG] provider.stdio: received EOF, stopping recv loop: err="rpc error: code = Unavailable desc = transport is closing" 2022-03-04T14:05:14.566Z [DEBUG] provider: plugin process exited: path=.terraform/providers/registry.terraform.io/cloudflare/cloudflare/3.9.1/darwin_arm64/terraform-provider-cloudflare_v3.9.1 pid=93367 2022-03-04T14:05:14.566Z [DEBUG] provider: plugin exited 2022-03-04T14:05:14.566Z [INFO] backend/local: apply calling Plan 2022-03-04T14:05:14.566Z [DEBUG] Building and walking plan graph for NormalMode 2022-03-04T14:05:14.566Z [DEBUG] ProviderTransformer: "cloudflare_record.record (expand)" (terraform.nodeExpandPlannableResource) needs provider["registry.terraform.io/cloudflare/cloudflare"] 2022-03-04T14:05:14.566Z [DEBUG] ReferenceTransformer: "cloudflare_record.record (expand)" references: [] 2022-03-04T14:05:14.566Z [DEBUG] ReferenceTransformer: "var.cloudflare_api_token" references: [] 2022-03-04T14:05:14.566Z [DEBUG] ReferenceTransformer: "var.cloudflare_account_id" references: [] 2022-03-04T14:05:14.566Z [DEBUG] ReferenceTransformer: "provider[\"registry.terraform.io/cloudflare/cloudflare\"]" references: [var.cloudflare_account_id var.cloudflare_api_token] 2022-03-04T14:05:14.566Z [DEBUG] Starting graph walk: walkPlan 2022-03-04T14:05:14.566Z [DEBUG] created provider logger: level=debug 2022-03-04T14:05:14.566Z [INFO] provider: configuring client automatic mTLS 2022-03-04T14:05:14.574Z [DEBUG] provider: starting plugin: path=.terraform/providers/registry.terraform.io/cloudflare/cloudflare/3.9.1/darwin_arm64/terraform-provider-cloudflare_v3.9.1 args=[.terraform/providers/registry.terraform.io/cloudflare/cloudflare/3.9.1/darwin_arm64/terraform-provider-cloudflare_v3.9.1] 2022-03-04T14:05:14.575Z [DEBUG] provider: plugin started: path=.terraform/providers/registry.terraform.io/cloudflare/cloudflare/3.9.1/darwin_arm64/terraform-provider-cloudflare_v3.9.1 pid=93368 2022-03-04T14:05:14.575Z [DEBUG] provider: waiting for RPC address: path=.terraform/providers/registry.terraform.io/cloudflare/cloudflare/3.9.1/darwin_arm64/terraform-provider-cloudflare_v3.9.1 2022-03-04T14:05:14.583Z [INFO] provider.terraform-provider-cloudflare_v3.9.1: configuring server automatic mTLS: timestamp=2022-03-04T14:05:14.583Z 2022-03-04T14:05:14.590Z [DEBUG] provider: using plugin: version=5 2022-03-04T14:05:14.591Z [DEBUG] provider.terraform-provider-cloudflare_v3.9.1: plugin address: address=/var/folders/zt/1hnkb2rn4kx05dhztp2kf91w0000gp/T/plugin1075312668 network=unix timestamp=2022-03-04T14:05:14.590Z 2022-03-04T14:05:14.608Z [WARN] ValidateProviderConfig from "provider[\"registry.terraform.io/cloudflare/cloudflare\"]" changed the config value, but that value is unused 2022-03-04T14:05:14.609Z [INFO] provider.terraform-provider-cloudflare_v3.9.1: 2022/03/04 14:05:14 [INFO] Cloudflare Client configured for user:: timestamp=2022-03-04T14:05:14.609Z 2022-03-04T14:05:14.609Z [INFO] provider.terraform-provider-cloudflare_v3.9.1: 2022/03/04 14:05:14 [INFO] Using specified account id in Cloudflare provider: timestamp=2022-03-04T14:05:14.609Z 2022-03-04T14:05:14.609Z [INFO] provider.terraform-provider-cloudflare_v3.9.1: 2022/03/04 14:05:14 [INFO] Cloudflare Client configured for user:: timestamp=2022-03-04T14:05:14.609Z 2022-03-04T14:05:14.609Z [DEBUG] Resource instance state not found for node "cloudflare_record.record", instance cloudflare_record.record 2022-03-04T14:05:14.609Z [DEBUG] ReferenceTransformer: "cloudflare_record.record" references: [] 2022-03-04T14:05:14.610Z [DEBUG] refresh: cloudflare_record.record: no state, so not refreshing 2022-03-04T14:05:14.612Z [DEBUG] provider.stdio: received EOF, stopping recv loop: err="rpc error: code = Unavailable desc = transport is closing" 2022-03-04T14:05:14.612Z [DEBUG] provider: plugin process exited: path=.terraform/providers/registry.terraform.io/cloudflare/cloudflare/3.9.1/darwin_arm64/terraform-provider-cloudflare_v3.9.1 pid=93368 2022-03-04T14:05:14.612Z [DEBUG] provider: plugin exited

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:

Terraform will perform the following actions:

cloudflare_record.record will be created

Plan: 1 to add, 0 to change, 0 to destroy. 2022-03-04T14:05:14.613Z [INFO] backend/local: apply calling Apply 2022-03-04T14:05:14.613Z [DEBUG] Building and walking apply graph for NormalMode plan 2022-03-04T14:05:14.614Z [DEBUG] Resource state not found for node "cloudflare_record.record", instance cloudflare_record.record 2022-03-04T14:05:14.614Z [DEBUG] ProviderTransformer: "cloudflare_record.record (expand)" (terraform.nodeExpandApplyableResource) needs provider["registry.terraform.io/cloudflare/cloudflare"] 2022-03-04T14:05:14.614Z [DEBUG] ProviderTransformer: "cloudflare_record.record" (terraform.NodeApplyableResourceInstance) needs provider["registry.terraform.io/cloudflare/cloudflare"] 2022-03-04T14:05:14.614Z [DEBUG] ReferenceTransformer: "var.cloudflare_api_token" references: [] 2022-03-04T14:05:14.614Z [DEBUG] ReferenceTransformer: "var.cloudflare_account_id" references: [] 2022-03-04T14:05:14.614Z [DEBUG] ReferenceTransformer: "cloudflare_record.record" references: [] 2022-03-04T14:05:14.614Z [DEBUG] ReferenceTransformer: "provider[\"registry.terraform.io/cloudflare/cloudflare\"]" references: [var.cloudflare_api_token var.cloudflare_account_id] 2022-03-04T14:05:14.614Z [DEBUG] ReferenceTransformer: "cloudflare_record.record (expand)" references: [] 2022-03-04T14:05:14.614Z [DEBUG] Starting graph walk: walkApply 2022-03-04T14:05:14.614Z [DEBUG] created provider logger: level=debug 2022-03-04T14:05:14.614Z [INFO] provider: configuring client automatic mTLS 2022-03-04T14:05:14.621Z [DEBUG] provider: starting plugin: path=.terraform/providers/registry.terraform.io/cloudflare/cloudflare/3.9.1/darwin_arm64/terraform-provider-cloudflare_v3.9.1 args=[.terraform/providers/registry.terraform.io/cloudflare/cloudflare/3.9.1/darwin_arm64/terraform-provider-cloudflare_v3.9.1] 2022-03-04T14:05:14.623Z [DEBUG] provider: plugin started: path=.terraform/providers/registry.terraform.io/cloudflare/cloudflare/3.9.1/darwin_arm64/terraform-provider-cloudflare_v3.9.1 pid=93369 2022-03-04T14:05:14.623Z [DEBUG] provider: waiting for RPC address: path=.terraform/providers/registry.terraform.io/cloudflare/cloudflare/3.9.1/darwin_arm64/terraform-provider-cloudflare_v3.9.1 2022-03-04T14:05:14.631Z [INFO] provider.terraform-provider-cloudflare_v3.9.1: configuring server automatic mTLS: timestamp=2022-03-04T14:05:14.630Z 2022-03-04T14:05:14.638Z [DEBUG] provider: using plugin: version=5 2022-03-04T14:05:14.638Z [DEBUG] provider.terraform-provider-cloudflare_v3.9.1: plugin address: address=/var/folders/zt/1hnkb2rn4kx05dhztp2kf91w0000gp/T/plugin3039069510 network=unix timestamp=2022-03-04T14:05:14.638Z 2022-03-04T14:05:14.654Z [WARN] ValidateProviderConfig from "provider[\"registry.terraform.io/cloudflare/cloudflare\"]" changed the config value, but that value is unused 2022-03-04T14:05:14.655Z [INFO] provider.terraform-provider-cloudflare_v3.9.1: 2022/03/04 14:05:14 [INFO] Cloudflare Client configured for user:: timestamp=2022-03-04T14:05:14.655Z 2022-03-04T14:05:14.655Z [INFO] provider.terraform-provider-cloudflare_v3.9.1: 2022/03/04 14:05:14 [INFO] Using specified account id 93d56280567ca77a1c20b61e11235ca4 in Cloudflare provider: timestamp=2022-03-04T14:05:14.655Z 2022-03-04T14:05:14.655Z [INFO] provider.terraform-provider-cloudflare_v3.9.1: 2022/03/04 14:05:14 [INFO] Cloudflare Client configured for user:: timestamp=2022-03-04T14:05:14.655Z cloudflare_record.record: Creating... 2022-03-04T14:05:14.657Z [INFO] Starting apply for cloudflare_record.record 2022-03-04T14:05:14.657Z [DEBUG] cloudflare_record.record: applying the planned Create change 2022-03-04T14:05:14.657Z [INFO] provider.terraform-provider-cloudflare_v3.9.1: 2022/03/04 14:05:14 [DEBUG] setting computed for "metadata" from ComputedKeys: timestamp=2022-03-04T14:05:14.657Z 2022-03-04T14:05:14.658Z [INFO] provider.terraform-provider-cloudflare_v3.9.1: 2022/03/04 14:05:14 [DEBUG] Data found in config: []interface {}{}: timestamp=2022-03-04T14:05:14.658Z 2022-03-04T14:05:14.658Z [INFO] provider.terraform-provider-cloudflare_v3.9.1: 2022/03/04 14:05:14 [DEBUG] Cloudflare Record create configuration: cloudflare.DNSRecord{CreatedOn:time.Date(1, time.January, 1, 0, 0, 0, 0, time.UTC), ModifiedOn:time.Date(1, time.January, 1, 0, 0, 0, 0, time.UTC), Type:"CNAME", Name:"NAME_PLACEHOLDER", Content:"VALUE_PLACEHOLDER", Meta:interface {}(nil), Data:interface {}(nil), ID:"", ZoneID:"ZONE_ID_PLACEHOLDER", ZoneName:"", Priority:(uint16)(nil), TTL:60, Proxied:(bool)(0x140005854b8), Proxiable:false, Locked:false}: timestamp=2022-03-04T14:05:14.658Z 2022-03-04T14:05:14.658Z [INFO] provider.terraform-provider-cloudflare_v3.9.1: 2022/03/04 14:05:14 [DEBUG] Waiting for state to become: [success]: timestamp=2022-03-04T14:05:14.658Z 2022-03-04T14:05:14.658Z [INFO] provider.terraform-provider-cloudflare_v3.9.1: 2022/03/04 14:05:14 [DEBUG] Cloudflare API Request Details: ---[ REQUEST ]--------------------------------------- POST /client/v4/zones/ZONE_ID_PLACEHOLDER/dns_records HTTP/1.1 Host: api.cloudflare.com User-Agent: terraform/1.1.7 terraform-plugin-sdk/2.10.1 terraform-provider-cloudflare/3.9.1 Content-Length: 284 Authorization: [redacted] Content-Type: application/json Accept-Encoding: gzip

{ "created_on": "0001-01-01T00:00:00Z", "modified_on": "0001-01-01T00:00:00Z", "type": "CNAME", "name": "NAME_PLACEHOLDER", "content": "VALUE_PLACEHOLDER", "zone_id": "ZONE_ID_PLACEHOLDER", "ttl": 60, "proxied": false } -----------------------------------------------------: timestamp=2022-03-04T14:05:14.658Z 2022-03-04T14:05:16.097Z [INFO] provider.terraform-provider-cloudflare_v3.9.1: 2022/03/04 14:05:16 [DEBUG] Cloudflare API Response Details: ---[ RESPONSE ]-------------------------------------- HTTP/2.0 400 Bad Request Cf-Cache-Status: DYNAMIC Cf-Ray: 6e6b2a688d22d21c-MAN Content-Type: application/json Date: Fri, 04 Mar 2022 14:05:16 GMT Expect-Ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" Server: cloudflare Set-Cookie: cflb=0H28vgHxwvgAQtjUGU56Rb8iNWZVUvXhkoF7b2QJepu; SameSite=Lax; path=/; expires=Fri, 04-Mar-22 16:35:17 GMT; HttpOnly Set-Cookie: cfruid=dbf78c390c068c8cb8fe096a69d912b5d0483089-1646402716; path=/; domain=.api.cloudflare.com; HttpOnly; Secure; SameSite=None Vary: Accept-Encoding X-Envoy-Upstream-Service-Time: 21

{ "result": null, "success": false, "errors": [ { "code": 1004, "message": "DNS Validation Error", "error_chain": [ { "code": 81053, "message": "An A, AAAA, or CNAME record with that host already exists." } ] } ], "messages": [] } -----------------------------------------------------: timestamp=2022-03-04T14:05:16.097Z 2022-03-04T14:05:16.119Z [ERROR] vertex "cloudflare_record.record" error: failed to create DNS record: HTTP status 400: DNS Validation Error (1004) ╷ │ Error: failed to create DNS record: HTTP status 400: DNS Validation Error (1004) │ │ with cloudflare_record.record, │ on main.tf line 43, in resource "cloudflare_record" "record": │ 43: resource "cloudflare_record" "record" { │ ╵ 2022-03-04T14:05:16.143Z [DEBUG] provider.stdio: received EOF, stopping recv loop: err="rpc error: code = Unavailable desc = transport is closing" 2022-03-04T14:05:16.144Z [DEBUG] provider: plugin process exited: path=.terraform/providers/registry.terraform.io/cloudflare/cloudflare/3.9.1/darwin_arm64/terraform-provider-cloudflare_v3.9.1 pid=93369 2022-03-04T14:05:16.144Z [DEBUG] provider: plugin exited

Panic output

No response

Expected output

Record to be created, albeit with warning stating that it already exists and is being overwritten.

Actual output

╷ │ Error: failed to create DNS record: HTTP status 400: DNS Validation Error (1004) │ │ with cloudflare_record.record, │ on main.tf line 43, in resource "cloudflare_record" "record": │ 43: resource "cloudflare_record" "record" { │

Steps to reproduce

  1. Ensure the record already exists in the given CloudFlare Zone
  2. Copy configuration code above into new main.tf
  3. terraform init
  4. terraform apply -auto-approve
  5. Enter details
  6. Error

Additional factoids

I believe the error comes from this line, in this file: https://github.com/cloudflare/terraform-provider-cloudflare/blob/master/cloudflare/resource_cloudflare_record.go#L121

The error returned by the API is as expected:

{
 "result": null,
 "success": false,
 "errors": [
  {
   "code": 1004,
   "message": "DNS Validation Error",
   "error_chain": [
    {
     "code": 81053,
     "message": "An A, AAAA, or CNAME record with that host already exists."
    }
   ]
  }
 ],
 "messages": []
}

But it looks like the code only checks the top level error message, not the error chain - as evidenced that the displayed error: DNS Validation Error (1004)

If the error_chain message(s) were checked then the 'already exist' check would match as expected and the allow_overwrite check would trigger as expected (we know it's not being triggered at all as the error comes from line 153, not 147 or 150)

I'm afraid my Go knowledge is weak so I'm sorry I couldn't try and fix it myself!

References

No response

jacobbednarz commented 2 years ago

i've raised this with the service team as error_chain is not included in our standard v4 envelope so it cannot be used publicly. this used to be in the errors object and working so we'll get this looked at.

Skyrail commented 2 years ago

Champion, thanks for the quick triage @jacobbednarz!

jacobbednarz commented 2 years ago

this has been addressed by the service team and should no longer be an issue.

$ curl 'https://api.cloudflare.com/client/v4/zones/3cbc67338be58abe667692ddbc386541/dns_records' \
       -H 'Authorization: Bearer $CF_API_TOKEN' \
       -H 'Content-Type: application/json' \
       -d '{"type":"A","name":"example.com","content":"127.0.0.1","ttl":3600,"priority":10,"proxied":false}'

{"result":{"id":"bfe2745f0220d4686a05af2c6254b603","zone_id":"3cbc67338be58abe667692ddbc386541","zone_name":"wordpress.lol","name":"example.com.wordpress.lol","type":"A","content":"127.0.0.1","proxiable":false,"proxied":false,"ttl":3600,"locked":false,"meta":{"auto_added":false,"managed_by_apps":false,"managed_by_argo_tunnel":false,"source":"primary"},"created_on":"2022-03-07T20:21:59.721782Z","modified_on":"2022-03-07T20:21:59.721782Z"},"success":true,"errors":[],"messages":[]}
$ curl -s 'https://api.cloudflare.com/client/v4/zones/3cbc67338be58abe667692ddbc386541/dns_records' \
       -H 'Authorization: Bearer $CF_API_TOKEN' \
       -H 'Content-Type: application/json' \
       -d '{"type":"A","name":"example.com","content":"127.0.0.1","ttl":3600,"priority":10,"proxied":false}' | jq .

{
  "result": null,
  "success": false,
  "errors": [
    {
      "code": 81057,
      "message": "Record already exists."
    }
  ],
  "messages": []
}
Skyrail commented 2 years ago

Really appreciate the rapid response on this, thank you @jacobbednarz for sending it to the right team.

I can confirm that the record is now imported although another error is thrown as follows:

╷
│ Error: empty non-retryable error received. This is a bug with the Terraform provider and should be reported as a GitHub issue in the provider repository.
│
│   with cloudflare_record.record,
│   on main.tf line 43, in resource "cloudflare_record" "record":
│   43: resource "cloudflare_record" "record" {
│
╵

This is running the same example as given in the original bug report - even with a brand new terraform init state. The full debug output is available in this gist.

It appears that it now correctly detects that the record exists and overwrites it but the above error occurs and the resource becomes tainted in the TF state.

On re-running terraform apply -auto-approve it destroys/re-creates the record, as the record is tainted. Output for that is available in this gist but is likely only happening because of the error in the first run.

jacobbednarz commented 2 years ago

hmm, this is really odd as you can see in the output, we are overwriting it and Terraform sends the PATCH to update the resource. i'll see if i can replicate this one locally on a fresh install before opening a new issue.

jacobbednarz commented 2 years ago

looks like this was working from the API side however, Terraform was choking as it always hit the NonRetryableError condition due to passing the function in it's entirety.

i've pushed a fix via #1496 if you'd like to give it a test run.

Skyrail commented 2 years ago

That fix works a treat, overwrites the record as expected with no errors - thanks @jacobbednarz - happy with that 👍🏻

cloudflare_record.record: Creating...
cloudflare_record.record: Creation complete after 5s [id=fd2c4f25e45ea702bb0489e44ad4dac2]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.