Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update resourceVSphereDatacenterImport to include any datacenter folder(s) in path #1607

Merged
merged 2 commits into from
Mar 31, 2022
Merged

Conversation

tenthirtyam
Copy link
Collaborator

Description

Updates resourceVSphereDatacenterImport to include the datacenter folder(s) in which the datacenter object may exist.

  • If found in a datacenter folder, the leading "/" is removed and the inventory path is used for the folder attribute during the import.

  • If the datacenter does not reside in a folder, the value is left empty during the import.

Release Note

r/datacenter - Updates resourceVSphereDatacenterImport to include the datacenter folder in which the datacenter object may exist. [GH-XXXX]

References

Closes #1374


Test 1 (With Datacenter Folders)

main.tf

provider "vsphere" {
  vsphere_server       = var.vsphere_server
  user                 = var.vsphere_username
  password             = var.vsphere_password
  allow_unverified_ssl = var.vsphere_insecure
}

resource "vsphere_folder" "folder1" {
  path = "hello"
  type = "datacenter"
}

resource "vsphere_folder" "folder2" {
  depends_on = [
    vsphere_folder.folder1,
  ]
  path = "hello/there"
  type = "datacenter"
}

resource "vsphere_datacenter" "datacenter" {
  depends_on = [
    vsphere_folder.folder1,
    vsphere_folder.folder2,
  ]
  name   = "world"
  folder = "hello/there"
}
terraform import vsphere_folder.folder1 /hello
vsphere_folder.folder1: Importing from ID "/hello"...
vsphere_folder.folder1: Import prepared!
  Prepared vsphere_folder for import
vsphere_folder.folder1: Refreshing state... [id=group-d50057]

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.terraform import vsphere_folder.folder2 /hello/there
vsphere_folder.folder2: Importing from ID "/hello/there"...
vsphere_folder.folder2: Import prepared!
  Prepared vsphere_folder for import
vsphere_folder.folder2: Refreshing state... [id=group-d50058]

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.terraform import vsphere_datacenter.datacenter /hello/world
vsphere_datacenter.datacenter: Importing from ID "/hello/world"...
vsphere_datacenter.datacenter: Import prepared!
  Prepared vsphere_datacenter for import
vsphere_datacenter.datacenter: Refreshing state... [id=world]

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.
  "resources": [
    {
      "mode": "managed",
      "type": "vsphere_datacenter",
      "name": "datacenter",
      "provider": "provider[\"local/hashicorp/vsphere\"]",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "custom_attributes": {},
            "folder": "hello/there",
            "id": "world",
            "moid": "datacenter-50073",
            "name": "world",
            "tags": []
          },
          "sensitive_attributes": [],
          "private": "eyJzY2hlbWFfdmVyc2lvbiI6IjAifQ=="
        }
      ]
    },

Test 2 (Without Datacenter Folders)

main.tf

provider "vsphere" {
  vsphere_server       = var.vsphere_server
  user                 = var.vsphere_username
  password             = var.vsphere_password
  allow_unverified_ssl = var.vsphere_insecure
}

resource "vsphere_folder" "folder1" {
  path = "hello"
  type = "datacenter"
}

resource "vsphere_folder" "folder2" {
  depends_on = [
    vsphere_folder.folder1,
  ]
  path = "hello/there"
  type = "datacenter"
}

resource "vsphere_datacenter" "datacenter" {
  depends_on = [
    vsphere_folder.folder1,
    vsphere_folder.folder2,
  ]
  name   = "world"
}
terraform import vsphere_folder.folder1 /hello
vsphere_folder.folder1: Importing from ID "/hello"...
vsphere_folder.folder1: Import prepared!
  Prepared vsphere_folder for import
vsphere_folder.folder1: Refreshing state... [id=group-d50064]

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.terraform import vsphere_folder.folder2 /hello/there
vsphere_folder.folder2: Importing from ID "/hello/there"...
vsphere_folder.folder2: Import prepared!
  Prepared vsphere_folder for import
vsphere_folder.folder2: Refreshing state... [id=group-d50065]

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.terraform import vsphere_datacenter.datacenter /world
vsphere_datacenter.datacenter: Importing from ID "/world"...
vsphere_datacenter.datacenter: Import prepared!
  Prepared vsphere_datacenter for import
vsphere_datacenter.datacenter: Refreshing state... [id=world]

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.terraform plan
vsphere_folder.folder1: Refreshing state... [id=group-d50078]
vsphere_folder.folder2: Refreshing state... [id=group-d50079]
vsphere_datacenter.datacenter: Refreshing state... [id=world]

No changes. Your infrastructure matches the configuration.

Terraform has compared your real infrastructure against your configuration and found no
differences, so no changes are needed.
  "resources": [
    {
      "mode": "managed",
      "type": "vsphere_datacenter",
      "name": "datacenter",
      "provider": "provider[\"local/hashicorp/vsphere\"]",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "custom_attributes": {},
            "folder": "",
            "id": "world",
            "moid": "datacenter-50066",
            "name": "world",
            "tags": []
          },
          "sensitive_attributes": [],
          "private": "eyJzY2hlbWFfdmVyc2lvbiI6IjAifQ=="
        }
      ]
    },

Updates `resourceVSphereDatacenterImport` to include the datacenter folder in  which the datacenter object may exist. If found in a datacenter folder, the leading "/" is removed and the inventory path is used for the `folder` attribute during the import. If the datacenter does not reside in a folder, the value is left empty during the import. #1374

Signed-off-by: Ryan Johnson <[email protected]>
@tenthirtyam tenthirtyam added bug Type: Bug needs-review Status: Pull Request Needs Review labels Mar 1, 2022
@tenthirtyam tenthirtyam added this to the v2.2.0 milestone Mar 1, 2022
@tenthirtyam tenthirtyam requested a review from appilon March 1, 2022 00:41
@tenthirtyam tenthirtyam self-assigned this Mar 1, 2022
@tenthirtyam tenthirtyam changed the title UpdateresourceVSphereDatacenterImport to import folder Update resourceVSphereDatacenterImport to import folder Mar 1, 2022
@github-actions github-actions bot added provider Type: Provider size/xs Relative Sizing: Extra-Small labels Mar 1, 2022
@tenthirtyam tenthirtyam changed the title Update resourceVSphereDatacenterImport to import folder Update resourceVSphereDatacenterImport to include any datacenter folder(s) in path Mar 1, 2022
@tenthirtyam tenthirtyam changed the title Update resourceVSphereDatacenterImport to include any datacenter folder(s) in path fix: Update resourceVSphereDatacenterImport to include any datacenter folder(s) in path Mar 11, 2022
@hashicorp-cla
Copy link

hashicorp-cla commented Mar 12, 2022

CLA assistant check
All committers have signed the CLA.

@appilon
Copy link
Contributor

appilon commented Mar 31, 2022

I am looking at this now, it is very odd, Import functions are typically a straight passthrough, in that the imported resource should have all the state specified in the config that was prepared for the import. It should not be necessary to perform any of these transformations. I was able to reproduce the issue in an acceptance test so I am going to dive in.

UPDATE: ☝️ No this is not accurate, the import function is expected to correctly set the ID that would be used in the Read function, it is the Read function's responsibility to use the ID to correctly set all fields in state.

It would be most correct to have the Read function take into account sub folders, based on what I have seen from the govmomi API though that may be impractical. If it's not possible then a solution like what this PR proposes is an okay fix.

@appilon
Copy link
Contributor

appilon commented Mar 31, 2022

Given the behavior of ignoring the folder is present between the Read function and StateRefreshFunc used in I guess polling waiting for creation, and the fact that folder is ForceNew: true I think it's easiest to keep this fix in the import function like you have it. I'll push my acceptance tests to this branch and hopefully merge soon.

Add import verification to acceptance tests
@github-actions github-actions bot added size/s Relative Sizing: Small and removed size/xs Relative Sizing: Extra-Small labels Mar 31, 2022
@appilon
Copy link
Contributor

appilon commented Mar 31, 2022

Import verification added to the tests:

=== RUN   TestAccResourceVSphereDatacenter_createOnRootFolder
--- PASS: TestAccResourceVSphereDatacenter_createOnRootFolder (29.89s)
=== RUN   TestAccResourceVSphereDatacenter_createOnSubfolder
--- PASS: TestAccResourceVSphereDatacenter_createOnSubfolder (36.39s)

@appilon appilon merged commit 3b66ceb into hashicorp:main Mar 31, 2022
@tenthirtyam tenthirtyam deleted the gh-1374 branch March 31, 2022 21:05
@github-actions
Copy link

github-actions bot commented May 1, 2022

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 1, 2022
@tenthirtyam tenthirtyam removed the needs-review Status: Pull Request Needs Review label Jun 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Type: Bug provider Type: Provider size/s Relative Sizing: Small
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Import of vsphere_datacenter resource does not register folder argument
3 participants