vultr / terraform-provider-vultr

Terraform Vultr provider
https://www.terraform.io/docs/providers/vultr/
Mozilla Public License 2.0
191 stars 65 forks source link

[QUESTION] - Declaring an already-existing domain is rejected, with no clear migration path #314

Open io7m opened 1 year ago

io7m commented 1 year ago

Describe the bug I'm not certain if this is a bug or a feature request.

I have a domain and a set of instances in Vultr, and I'm trying to seamlessly migrate to using Terraform to manage them. I have a domain, let's call it example.com, with a lot of records in it. I'm trying to migrate to using Terraform to manage the domain, so the first step has been to declare it in the Terraform file:

resource "vultr_dns_domain" "example_domain" {
  domain = "example.com"
}

Unfortunately, terraform plan then tries to create a plan that will create that domain, even though it already exists. Running the plan will fail as you might expect:

vultr_dns_domain.example_domain: Creating...
╷
│ Error: error while creating domain : {"error":"Unable to create domain: Domain already exists","status":400}
│ 
│   with vultr_dns_domain.example_domain,
│   on domain.tf line 39, in resource "vultr_dns_domain" "example_domain":
│   39: resource "vultr_dns_domain" "example_domain" {
│ 
╵

To Reproduce Steps to reproduce the behavior:

  1. Create a domain in the Vultr control panel.
  2. Declare the same domain in a Terraform file.
  3. terraform plan
  4. terraform apply

Expected behavior I would expect Terraform to see that the domain already exists, and avoid creating it. It might create a plan to remove any records it doesn't know about, but that's not essential. In fact, it seems to be the general behavior of the Vultr provider to ignore extra values it doesn't know about (like extra VPS instances existing already).

Screenshots

Desktop (please complete the following information where applicable:


$ terraform --version
Terraform v1.3.7
on linux_amd64
+ provider registry.terraform.io/vultr/vultr v2.12.0

Additional context

optik-aper commented 1 year ago

Hello @io7m! Apologies for the slow response. Most resources can be imported into terraform state using the terraform import process/command. Just to be sure I tested it out like so:

main.tf:

resource "vultr_dns_domain" "fake-domain" {
  domain = "whattestvultr.dev"
}

data "vultr_dns_domain" "fake-data" {
  domain = "whattestvultr.dev"
}

output "domain_date" {
  value = data.vultr_dns_domain.fake-data.date_created
}

And then, import the existing domain into Terraform:

terraform import vultr_dns_domain.fake-domain whattestvultr.dev                                                                      1 ↵ 13:00
vultr_dns_domain.fake-domain: Importing from ID "whattestvultr.dev"...
vultr_dns_domain.fake-domain: Import prepared!
  Prepared vultr_dns_domain for import
vultr_dns_domain.fake-domain: Refreshing state... [id=whattestvultr.dev]

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

Finally, just check that the output shows:

Outputs:

domain_date = "2023-02-21T20:02:52+00:00"

That should cover your use case but let me know if not!