namecheap / terraform-provider-namecheap

Terraform provider for Namecheap
Apache License 2.0
147 stars 30 forks source link

If you don't own the domain, state gets jacked up #24

Closed OGProgrammer closed 3 years ago

OGProgrammer commented 5 years ago

Edge case where you change a domain name from something you did own, to something you don't. Things get "stuck" in a broken state.

Terraform Version

0.12

Namecheap provider version

latest tagged release

Affected Resource(s)

Please list the resources as a list, for example:

Terraform Configuration Files

N/A

Debug Output

See above

Panic Output

See above

Expected Behavior

What should have happened? I'm not exactly sure haha, maybe it can see there was an ownership error with the domain name and prevent writing it to the state file?

Actual Behavior

💥

Steps to Reproduce

Error: Failed to create namecheap Record: Could not find the record with hash 2759530686

Now only way to fix this is to manually edit the terraform.tfstate file and remove the offending hash record or fix the domain name manually.

Important Factoids

I am a computer

References

N/A

adamdecaf commented 5 years ago

Can you share what gets messed up? Is the resource added to the state file when it shouldn't be?

OGProgrammer commented 5 years ago

tf state looks like this after you apply a incorrect domain


    {
      "mode": "managed",
      "type": "namecheap_record",
      "name": "www-adipdx-com",
      "provider": "provider.namecheap",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "address": "1.1.1.xxx",
            "domain": "adipix.com",
            "hostname": "www.adipdx.com",
            "id": "1331301xxx",
            "mx_pref": 10,
            "name": "www",
            "timeouts": null,
            "ttl": 1799,
            "type": "A"
          },
          "depends_on": [
            "xxxxx"
          ]
        }
      ]
    },

I have to change the line "domain": "adipix.com" and fix the domain name manually , otherwise I keep getting hash errors.

OGProgrammer commented 5 years ago

Same thing seems to happen when you put in an invalid CNAME record also. I just added a CNAME record but forgot the period at the end of the address and was getting the same hash errors unless I manually removed them from state file.

OGProgrammer commented 5 years ago

Did not work and corrupted state file. (note address period not there)

resource "namecheap_record" "aws-dkim2-shouthouse-com" {
  name = "ru3x3psqyrikzewp4xxxxxxxxfclzw._domainkey"
  domain = "shouthouse.com"
  address = "ru3x3psqyrikzewp4dxxxxxfclzw.dkim.amazonses.com"
  type = "CNAME"
}

Worked

resource "namecheap_record" "aws-dkim2-shouthouse-com" {
  name = "ru3x3psqyrikzewp4xxxxxxxxfclzw._domainkey"
  domain = "shouthouse.com"
  address = "ru3x3psqyrikzewp4dxxxxxfclzw.dkim.amazonses.com."
  type = "CNAME"
}
OGProgrammer commented 5 years ago

These are "low priority" in my opinion but can be really confusing to someone new with terraform/this provider.

adamdecaf commented 5 years ago

I created https://github.com/adamdecaf/namecheap/pull/7 and https://github.com/adamdecaf/terraform-provider-namecheap/pull/25 to look at both issues here. I haven't tried them out, but any thoughts?

OGProgrammer commented 5 years ago

Not sure dude, when I have time I can look but this isn't a blocker for me. Recorded issue to help others who may run into this in the meantime until we can figure out what to do.

AlexFBP commented 4 years ago

I have almost the same Error: Failed to create namecheap Record: Could not find the record with hash but when creating MX records, corrupting my state (Even by commenting MX records/resources, appears that error on terraform plan)

So, I ran terraform show and my 2 MX registers were marked as (tainted), so i had to delete them with

terraform state rm namecheap_record.TAINTED-MX-RECORD

as I have my state backed up in Terraform. In meantime, I left the MX registers commented out.

(My/this comment to this issue, should be in another Issue or here is Ok?)

AlexFBP commented 4 years ago

Update to my last message: Worked, also as with CNAME registers: There must be a final "." in MX registers.

Is this possible to evaluate if the final period is present in CNAME/MX registers to throw an error in the terraform plan phase?

aj-foster commented 4 years ago

Thank you @AlexFBP — that terraform state rm command helped resolve things quickly when I forgot the trailing period in a CNAME record just now while working with remote state.

AlexFBP commented 3 years ago

Additional complement to my previous comments:

For a recently bought domain, seems that it is needed at first time to set up manually for it in Advanced Settings > Mail Settings: Custom MX and set a valid MX record. Otherwise, no matter what you do and check, you'll get the "Error: Failed to create namecheap Record: Could not find the record with hash"

After that initially/manually created MX record, you can reply its DNS record within Terraform, or, if was created as a "dummy" MX record, you can delete it later, manually and directly from Namecheap web interface

(version used: 1.6.0)

ifree92 commented 3 years ago

Due to complete project refactoring, I'm closing this issue. Refer to #46 to get more details.

@AlexFBP Unfortunately, the previous version did not support MX records at all. You're welcome to try a new https://registry.terraform.io/providers/namecheap/namecheap/latest v2 major release.

I trust https://registry.terraform.io/providers/namecheap/namecheap/latest/docs/guides/namecheap_domain_records_guide#email-type this will be helpful for you to set MX records.

Cheers 🥂