josenk / terraform-provider-esxi

Terraform-provider-esxi plugin
GNU General Public License v3.0
540 stars 154 forks source link

terraform import esxi_vswitch.test test followed by terraform plan/apply does not seem to work #151

Closed rgl closed 2 years ago

rgl commented 2 years ago

Describe the bug

Doing a terraform import esxi_vswitch.test test followed by terraform plan/apply wants to re-create the vswitch.

To Reproduce

Steps to reproduce the behavior:

  1. terraform import esxi_vswitch.test test
  2. terraform plan/apply

Expected behavior

Expected a noop plan after an import.

Terraform file

resource "esxi_vswitch" "test" {
  name = "test"
}

Desktop (please complete the following information):

Additional context

Here's a transcript:

# see that the vswitch does in fact exist, because it fails to be created by terraform without a previous terraform state.
vagrant@ubuntu:~/test-terraform-esxi$ terraform plan -out=tfplan

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

Terraform will perform the following actions:

  # esxi_vswitch.test will be created
  + resource "esxi_vswitch" "test" {
      + forged_transmits    = (known after apply)
      + id                  = (known after apply)
      + link_discovery_mode = (known after apply)
      + mac_changes         = (known after apply)
      + mtu                 = (known after apply)
      + name                = "test"
      + ports               = (known after apply)
      + promiscuous_mode    = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Saved the plan to: tfplan

To perform exactly these actions, run the following command to apply:
    terraform apply "tfplan"

vagrant@ubuntu:~/test-terraform-esxi$ time terraform apply tfplan
esxi_vswitch.test: Creating...
╷
│ Error: Failed to add vswitch: test, it already exists
│ 
│ 
│   with esxi_vswitch.test,
│   on main.tf line 20, in resource "esxi_vswitch" "test":
│   20: resource "esxi_vswitch" "test" {
│ 
╵

real    0m1.527s
user    0m0.583s
sys     0m0.067s

# now import the existing vswitch.
vagrant@ubuntu:~/test-terraform-esxi$ terraform import esxi_vswitch.test test
esxi_vswitch.test: Importing from ID "test"...
esxi_vswitch.test: Import prepared!
  Prepared esxi_vswitch for import
esxi_vswitch.test: Refreshing state... [id=test]

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.

# plan the changes, and notice it tries to re-create the vswitch, but it shouldn't.
vagrant@ubuntu:~/test-terraform-esxi$ terraform plan -out=tfplan
esxi_vswitch.test: Refreshing state... [id=test]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated
with the following symbols:
-/+ destroy and then create replacement

Terraform will perform the following actions:

  # esxi_vswitch.test must be replaced
-/+ resource "esxi_vswitch" "test" {
      ~ forged_transmits    = false -> (known after apply)
      ~ id                  = "test" -> (known after apply)
      ~ link_discovery_mode = "listen" -> (known after apply)
      ~ mac_changes         = false -> (known after apply)
      ~ mtu                 = 1500 -> (known after apply)
      + name                = "test" # forces replacement
      ~ ports               = 128 -> (known after apply)
      ~ promiscuous_mode    = false -> (known after apply)
    }

Plan: 1 to add, 0 to change, 1 to destroy.

It seems the import does not set the vswitch name inside the terraform state:

vagrant@ubuntu:~/test-terraform-esxi$ diff -u terraform.tfstate.old terraform.tfstate
--- terraform.tfstate.old       2021-09-23 11:59:24.320724512 +0100
+++ terraform.tfstate   2021-09-23 12:34:15.408757178 +0100
@@ -1,8 +1,8 @@
 {
   "version": 4,
   "terraform_version": "1.0.7",
-  "serial": 2,
-  "lineage": "763c9095-9c2a-e5a9-a594-9172ef6c66f6",
+  "serial": 1,
+  "lineage": "57293f7d-0464-7581-3c1f-e8db9d4989be",
   "outputs": {},
   "resources": [
     {
@@ -19,13 +19,13 @@
             "link_discovery_mode": "listen",
             "mac_changes": false,
             "mtu": 1500,
-            "name": null,
+            "name": "test",
             "ports": 128,
             "promiscuous_mode": false,
             "uplink": []
           },
           "sensitive_attributes": [],
-          "private": "eyJzY2hlbWFfdmVyc2lvbiI6IjAifQ=="
+          "private": "bnVsbA=="
         }
       ]
     }