Skip to content

Commit

Permalink
Merge pull request #709 from terraform-providers/f-folder-data-source
Browse files Browse the repository at this point in the history
Add folder data source
  • Loading branch information
bill-rich authored Feb 16, 2019
2 parents 6799504 + 17d00c8 commit 21328d6
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
33 changes: 33 additions & 0 deletions vsphere/data_source_vsphere_folder.go
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
}
55 changes: 55 additions & 0 deletions vsphere/data_source_vsphere_folder_test.go
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" {}
`
1 change: 1 addition & 0 deletions vsphere/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func Provider() terraform.ResourceProvider {
"vsphere_datastore": dataSourceVSphereDatastore(),
"vsphere_datastore_cluster": dataSourceVSphereDatastoreCluster(),
"vsphere_distributed_virtual_switch": dataSourceVSphereDistributedVirtualSwitch(),
"vsphere_folder": dataSourceVSphereFolder(),
"vsphere_host": dataSourceVSphereHost(),
"vsphere_network": dataSourceVSphereNetwork(),
"vsphere_resource_pool": dataSourceVSphereResourcePool(),
Expand Down
39 changes: 39 additions & 0 deletions website/docs/d/folder.html.markdown
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

0 comments on commit 21328d6

Please sign in to comment.