Skip to content

Commit

Permalink
Set folder only if one is detected
Browse files Browse the repository at this point in the history
Add import verification to acceptance tests
  • Loading branch information
appilon committed Mar 31, 2022
1 parent 15c2fb3 commit 3b66ceb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
8 changes: 5 additions & 3 deletions vsphere/resource_vsphere_datacenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,15 +287,17 @@ func resourceVSphereDatacenterImport(d *schema.ResourceData, meta interface{}) (
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 err := d.Set("folder", path); err != nil {
return nil, err
if path != "" {
if err := d.Set("folder", path); err != nil {
return nil, err
}
}

d.SetId(dc.Name())
Expand Down
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

0 comments on commit 3b66ceb

Please sign in to comment.