namecheap / terraform-provider-namecheap

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

How do I import an already created/setup domain? #19

Closed MostHated closed 5 years ago

MostHated commented 5 years ago

Hi there, I am just wondering how I go about configuring an already existing domain? I purchase them through Namecheap often so it already exists there, but I wanted to use Terraform to set the name servers to different things using just the "basic DNS" which provides nameservers only and I point those to Cloudflare, GCP, AWS, DigitalOcean, etc depending on various things. I tried to use namecheap_domain so I could try to import, but it said that didn't exist. Any suggestions on what I do in this situation?

Thanks, -MH

adamdecaf commented 5 years ago

Importing isn't supported by the provider (yet), but can be added. The Terraform schema.Resource struct offers an Importer field to handle that logic. I can try and add something in the next day or two.

https://www.terraform.io/docs/extend/resources.html#importers https://godoc.org/github.com/hashicorp/terraform/helper/schema#Resource

MostHated commented 5 years ago

That would be great. I always just buy them through Namecheap but never actually use their settings, I always just change the NS to something else. I have a few dozen domains there, so it would be super useful to be able to import them all and be able to mass change their records all at once. Huge time saver. Do you know off hand if configuring the "Basic DNS" which only has a few NS records and that is it, is different from configuring the "Advanced DNS" which is just normal dns settings that you would use for NS, A, MX, etc? I am wondering if there might be some sort of flag that needs to be set on the domains own entry. Something like

(This is, of course, just an example because I have no idea how it actually works or is structured)

resource "namecheap_domain" "mydomain" {
  domain  = "${var.domain}"
  type    = "BasicDNS"
}

I definitely appreciate the work you have put into this. I only recently started using TF for CloudFlare, so when I came across this one, I was really excited about the amount of time I could save between the two. : D

MostHated commented 5 years ago

Whenever you happen to have the time to make the update, if you would not mind sending a quick reply here so I know, or just close this, I would be most grateful!

Thanks again, -MH

adamdecaf commented 5 years ago

I think I've got a PR for this. It works on one of my domains (root A record and subdomain), but I'd like others to try it. Can y'all pull that branch down, or would a pre-release binary be easier?

https://github.com/adamdecaf/terraform-provider-namecheap/pull/21

MostHated commented 5 years ago

I can snag the branch and have a go at it. Is it 11.x or 12.x?

adamdecaf commented 5 years ago

It's on Terraform 0.12.0

MostHated commented 5 years ago

Never mind, it would be easier if you just made a build, lol. I am not getting import errors on the package saying there is a circular dependency.

adamdecaf commented 5 years ago

@MostHated Sure thing, try out 1.4.0-rc1

https://github.com/adamdecaf/terraform-provider-namecheap/releases/tag/1.4.0-rc1

MostHated commented 5 years ago

I am wondering if there is some sort of necessary differentiation between the different types of DNS. All of my domains use "Basic" / "Custom" DNS, which as seen below simply gives you the option to point the nameservers to another provider, nothing more. I believe you have to change it to a different mode in order to get the normal entries like A, CNAME, MX, etc.

I attempted to run the following setup.


variable "domain" {
  default = "mydomain.com"
}

provider "namecheap" {
  username    = "${var.username}"
  api_user    = "${var.api-user}"
  token       = "${var.api-access-token}"
  ip          = "${var.ip}"
  use_sandbox = "${var.use-sandbox}"
}

resource "namecheap_record" "mydomain-ns1" {
  domain  = "${var.domain}"
  name    = "ns1"
  address = "dave.ns.cloudflare.com"
  type    = "NS"
}

resource "namecheap_record" "mydomain-ns2" {
  domain  = "${var.domain}"
  name    = "ns2"
  address = "joan.ns.cloudflare.com"
  type    = "NS"
}

and then I ran this based on your example.

terraform import -var-file=variables.tfvars namecheap_record.mydomain-ns1 'mydomain.com/NS/dave.ns.cloudflares.com' 

but unfortunately I got the error of:

terraform import -var-file=variables.tfvars namecheap_record.mydomain-ns2 'mydomain.com/NS/dave.ns.cloudflares.com' 
namecheap_record.mydomain-ns2: Importing from ID "mydomain.com/NS/dave.ns.cloudflares.com"...
namecheap_record.mydomain-ns2: Import complete!
  Imported namecheap_record
namecheap_record.mydomain-ns2: Refreshing state... [id=mydomain.com/NS/dave.ns.cloudflares.com]

Error: read: problem finding record for mydomain.com/NS/dave.ns.cloudflares.com: Could not find the record with hash 2978406536

It looks like a state file was created, though not sure if that was just automatic during init or not, but it is currently empty.

Any suggestions?

adamdecaf commented 5 years ago

Try cloudflare.com instead of cloudflares.com

MostHated commented 5 years ago

I was initially excited that it might have been simply a silly mistake that was keeping it from working, but strangely it still came back with the same error after fixing and trying again. I tried to see what would happen if I just applied it, but it just sat there repeating "Still creating" over and over for quite some time.

adamdecaf commented 5 years ago

Is there a terraform.tfstate file from before? You might try renaming that and doing the import again.

MostHated commented 5 years ago

No, there wasn't one initially. When I started I started fresh with a new folder and 0 files. So it was created during this process of trying it out.

adamdecaf commented 5 years ago

Strange.. Can you share the output of an import and re-apply that doesn't complete?

MostHated commented 5 years ago

MostHated commented 5 years ago

This is interesting. I was looking through the namecheap api as well as your code and I came across this in your repo.

func resourceNameCheapNSUpdate(d *schema.ResourceData, meta interface{}) error {
    return resourceNameCheapNSCreate(d, meta)
}

When I saw that I figured it was worth a try.

resource "namecheap_ns" "mydomain-ns1" {
  domain = "${var.domain}"
  servers = ["dave.ns.cloudflare.com", "joan.ns.cloudflare.com", "test.ns.namecheap.com"]
}

It didn't let me import it, but when I ran the above, what do you know... it worked!

Looking at the last update of that file, it looks like it has been there for some time?

adamdecaf commented 5 years ago

Oh right, namecheap_ns has been there to set the NS records. I thought you could set the NS records with namecheap_record, but I suppose not. Does this solve the issue for you?

MostHated commented 5 years ago

Based on a few tests that I have run, it looks like everything is good. :+1: Now I just have to work with the fella who made the G-Suite provider. :smile:

I am pretty close to having things setup just as I wished, I appreciate the assistance.