-
Notifications
You must be signed in to change notification settings - Fork 453
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #709 from terraform-providers/f-folder-data-source
Add folder data source
- Loading branch information
Showing
4 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package vsphere | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-vsphere/vsphere/internal/helper/folder" | ||
) | ||
|
||
func dataSourceVSphereFolder() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceVSphereFolderRead, | ||
Schema: map[string]*schema.Schema{ | ||
"path": { | ||
Type: schema.TypeString, | ||
Description: "The absolute path of the folder.", | ||
Required: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceVSphereFolderRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*VSphereClient).vimClient | ||
fo, err := folder.FromAbsolutePath(client, d.Get("path").(string)) | ||
if err != nil { | ||
return fmt.Errorf("cannot locate folder: %s", err) | ||
} | ||
|
||
d.SetId(fo.Reference().Value) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package vsphere | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
var testAccDataSourceVSphereFolderExpectedRegexp = regexp.MustCompile("^group-v") | ||
|
||
func TestAccDataSourceVSphereFolder_basic(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { | ||
testAccPreCheck(t) | ||
testAccDataSourceVSphereFolderPreCheck(t) | ||
}, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceVSphereFolderConfig(), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestMatchResourceAttr( | ||
"data.vsphere_folder.folder", | ||
"id", | ||
testAccDataSourceVSphereFolderExpectedRegexp, | ||
), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceVSphereFolderPreCheck(t *testing.T) { | ||
if os.Getenv("VSPHERE_FOLDER_V0_PATH") == "" { | ||
t.Skip("set VSPHERE_FOLDER_V0_PATH to run vsphere_folder acceptance tests") | ||
} | ||
if os.Getenv("VSPHERE_DATACENTER") == "" { | ||
t.Skip("set VSPHERE_DATACENTER to run vsphere_folder acceptance tests") | ||
} | ||
} | ||
|
||
func testAccDataSourceVSphereFolderConfig() string { | ||
return fmt.Sprintf(` | ||
data "vsphere_folder" "folder" { | ||
path = "/%s/vm/%s" | ||
} | ||
`, os.Getenv("VSPHERE_DATACENTER"), os.Getenv("VSPHERE_FOLDER_V0_PATH")) | ||
} | ||
|
||
const testAccDataSourceVSphereFolderConfigDefault = ` | ||
data "vsphere_datacenter" "dc" {} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
layout: "vsphere" | ||
page_title: "VMware vSphere: vsphere_folder" | ||
sidebar_current: "docs-vsphere-data-source-inventory-folder" | ||
description: |- | ||
Provides a VMware vSphere folder data source. This can be used to get the general attributes of a vSphere inventory folder. | ||
--- | ||
|
||
# vsphere\_folder | ||
|
||
The `vsphere_folder` data source can be used to get the general attributes of a | ||
vSphere inventory folder. Paths are absolute and include must include the | ||
datacenter. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "vsphere_folder" "folder" { | ||
path = "/dc1/datastore/folder1" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `path` - (Required) The absolute path of the folder. For example, given a | ||
default datacenter of `default-dc`, a folder of type `vm`, and a folder name | ||
of `terraform-test-folder`, the resulting path would be | ||
`/default-dc/vm/terraform-test-folder`. The valid folder types to be used in | ||
the path are: `vm`, `host`, `datacenter`, `datastore`, or `network`. | ||
|
||
## Attribute Reference | ||
|
||
The only attribute that this resource exports is the `id`, which is set to the | ||
[managed object ID][docs-about-morefs] of the folder. | ||
|
||
[docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider | ||
|