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

Add folder data source #709

Merged
merged 1 commit into from
Feb 16, 2019
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
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