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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions vsphere/resource_vsphere_datacenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/customattribute"
"github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/datacenter"
"github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/folder"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/methods"
Expand Down Expand Up @@ -285,6 +286,20 @@ func resourceVSphereDatacenterImport(d *schema.ResourceData, meta interface{}) (
if err != nil {
return nil, err
}

// determine a folder if one is present
f, err := folder.ParentFromPath(client, p, folder.VSphereFolderTypeDatacenter, dc)
if err != nil {
return nil, fmt.Errorf("cannot locate folder: %s", err)
}

path := strings.TrimPrefix(f.InventoryPath, "/")
if path != "" {
if err := d.Set("folder", path); err != nil {
return nil, err
}
}

d.SetId(dc.Name())
return []*schema.ResourceData{d}, nil
}
37 changes: 29 additions & 8 deletions vsphere/resource_vsphere_datacenter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"path"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand All @@ -13,23 +14,27 @@ import (

const testAccCheckVSphereDatacenterResourceName = "vsphere_datacenter.testDC"

const testAccCheckVSphereDatacenterConfig = `
func testAccCheckVSphereDatacenterConfig(name string) string {
return fmt.Sprintf(`
resource "vsphere_datacenter" "testDC" {
name = "testDC"
name = "%s"
}
`, name)
}
`

const testAccCheckVSphereDatacenterConfigSubfolder = `
func testAccCheckVSphereDatacenterConfigSubfolder(name, folder string) string {
return fmt.Sprintf(`
resource "vsphere_folder" "folder" {
path = "%s"
type = "datacenter"
}

resource "vsphere_datacenter" "testDC" {
name = "testDC"
name = "%s"
folder = vsphere_folder.folder.path
}
`
`, folder, name)
}

const testAccCheckVSphereDatacenterConfigTags = `
resource "vsphere_tag_category" "testacc-category" {
Expand Down Expand Up @@ -129,37 +134,53 @@ resource "vsphere_datacenter" "testDC" {

// Create a datacenter on the root folder
func TestAccResourceVSphereDatacenter_createOnRootFolder(t *testing.T) {
name := "testDC"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckVSphereDatacenterDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckVSphereDatacenterConfig,
Config: testAccCheckVSphereDatacenterConfig(name),
Check: resource.ComposeTestCheckFunc(testAccCheckVSphereDatacenterExists(testAccCheckVSphereDatacenterResourceName, true)),
},
{
ResourceName: testAccCheckVSphereDatacenterResourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateIdPrefix: "/",
ImportStateId: name,
},
},
})
}

// Create a datacenter on a subfolder
func TestAccResourceVSphereDatacenter_createOnSubfolder(t *testing.T) {
dcFolder := os.Getenv("TF_VAR_VSPHERE_DC_FOLDER")
name := "testDC"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckVSphereDatacenterDestroy,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(testAccCheckVSphereDatacenterConfigSubfolder, dcFolder),
Config: testAccCheckVSphereDatacenterConfigSubfolder(name, dcFolder),
Check: resource.ComposeTestCheckFunc(
testAccCheckVSphereDatacenterExists(
testAccCheckVSphereDatacenterResourceName,
true,
),
),
},
{
ResourceName: testAccCheckVSphereDatacenterResourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateIdPrefix: "/",
ImportStateId: path.Join(dcFolder, name),
},
},
})
}
Expand Down