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

d/vapp_container: Add vapp_container data source #617

Merged
merged 1 commit into from
Sep 11, 2018
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
43 changes: 43 additions & 0 deletions vsphere/data_source_vsphere_vapp_container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package vsphere

import (
"fmt"

"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-vsphere/vsphere/internal/helper/vappcontainer"
)

func dataSourceVSphereVAppContainer() *schema.Resource {
return &schema.Resource{
Read: dataSourceVSphereVAppContainerRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the vApp container.",
},
"datacenter_id": {
Type: schema.TypeString,
Required: true,
Description: "The Managed Object ID of the datacenter.",
},
},
}
}

func dataSourceVSphereVAppContainerRead(d *schema.ResourceData, meta interface{}) error {
client, err := resourceVSphereVAppContainerClient(meta)
if err != nil {
return err
}
dc, err := datacenterFromID(client, d.Get("datacenter_id").(string))
if err != nil {
return fmt.Errorf("cannot locate datacenter: %s", err)
}
vc, err := vappcontainer.FromPath(client, d.Get("name").(string), dc)
if err != nil {
return fmt.Errorf("cannot locate vApp Container: %s", err)
}
d.SetId(vc.Reference().Value)
return nil
}
113 changes: 113 additions & 0 deletions vsphere/data_source_vsphere_vapp_container_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package vsphere

import (
"fmt"
"os"
"regexp"
"testing"

"github.com/hashicorp/terraform/helper/resource"
)

func TestAccDataSourceVSphereVAppContainer_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccDataSourceVSphereVAppContainerPreCheck(t)
testAccSkipIfEsxi(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceVSphereVAppContainerConfig(),
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr("data.vsphere_vapp_container.container", "id", regexp.MustCompile("^resgroup-")),
),
},
},
})
}

func TestAccDataSourceVSphereVAppContainer_path(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccDataSourceVSphereVAppContainerPreCheck(t)
testAccSkipIfEsxi(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceVSphereVAppContainerPathConfig(),
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr("data.vsphere_vapp_container.container", "id", regexp.MustCompile("^resgroup-")),
),
},
},
})
}

func testAccDataSourceVSphereVAppContainerPreCheck(t *testing.T) {
if os.Getenv("VSPHERE_DATACENTER") == "" {
t.Skip("set VSPHERE_DATACENTER to run vsphere_vapp_container acceptance tests")
}
if os.Getenv("VSPHERE_CLUSTER") == "" {
t.Skip("set VSPHERE_CLUSTER to run vsphere_vapp_container acceptance tests")
}
if os.Getenv("VSPHERE_VAPP_CONTAINER") == "" {
t.Skip("set VSPHERE_VAPP_CONTAINER to run vsphere_vapp_container acceptance tests")
}
}

func testAccDataSourceVSphereVAppContainerConfig() string {
return fmt.Sprintf(`
variable "datacenter" {
default = "%s"
}

variable "vapp_container" {
default = "%s"
}

data "vsphere_datacenter" "dc" {
name = "${var.datacenter}"
}

data "vsphere_vapp_container" "container" {
name = "${var.vapp_container}"
datacenter_id = "${data.vsphere_datacenter.dc.id}"
}
`,
os.Getenv("VSPHERE_DATACENTER"),
os.Getenv("VSPHERE_VAPP_CONTAINER"),
)
}

func testAccDataSourceVSphereVAppContainerPathConfig() string {
return fmt.Sprintf(`
variable "datacenter" {
default = "%s"
}

variable "cluster" {
default = "%s"
}

variable "vapp_container" {
default = "%s"
}

data "vsphere_datacenter" "dc" {
name = "${var.datacenter}"
}

data "vsphere_vapp_container" "container" {
name = "/${var.datacenter}/vm/${var.vapp_container}"
datacenter_id = "${data.vsphere_datacenter.dc.id}"
}
`,
os.Getenv("VSPHERE_DATACENTER"),
os.Getenv("VSPHERE_CLUSTER"),
os.Getenv("VSPHERE_VAPP_CONTAINER"),
)
}
1 change: 1 addition & 0 deletions vsphere/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func Provider() terraform.ResourceProvider {
"vsphere_resource_pool": dataSourceVSphereResourcePool(),
"vsphere_tag": dataSourceVSphereTag(),
"vsphere_tag_category": dataSourceVSphereTagCategory(),
"vsphere_vapp_container": dataSourceVSphereVAppContainer(),
"vsphere_virtual_machine": dataSourceVSphereVirtualMachine(),
"vsphere_vmfs_disks": dataSourceVSphereVmfsDisks(),
},
Expand Down
45 changes: 45 additions & 0 deletions website/docs/d/vapp_container.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
layout: "vsphere"
page_title: "VMware vSphere: vsphere_vapp_container"
sidebar_current: "docs-vsphere-data-source-resource-pool"
description: |-
Provides a vSphere vApp container data source. This can be used to get the general attributes of a vSphere vApp container.
---

# vsphere\_resource\_pool

The `vsphere_vapp_container` data source can be used to discover the ID of a
vApp container in vSphere. This is useful to fetch the ID of a vApp container
that you want to use to create virtual machines in using the
[`vsphere_virtual_machine`][docs-virtual-machine-resource] resource.

[docs-virtual-machine-resource]: /docs/providers/vsphere/r/virtual_machine.html

## Example Usage

```hcl
data "vsphere_datacenter" "datacenter" {
name = "dc1"
}

data "vsphere_vapp_container" "pool" {
name = "vapp-container-1"
datacenter_id = "${data.vsphere_datacenter.datacenter.id}"
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Required) The name of the vApp container. This can be a name or
path.
* `datacenter_id` - (Required) The [managed object reference
ID][docs-about-morefs] of the datacenter the vApp container is located in.

[docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider

## Attribute Reference

Currently, the only exported attribute from this data source is `id`, which
represents the ID of the vApp container that was looked up.