Skip to content

Commit

Permalink
Merge pull request hashicorp#144 from terraform-providers/f-datacente…
Browse files Browse the repository at this point in the history
…r-data-source

New data source: vsphere_datacenter
  • Loading branch information
vancluever authored Aug 30, 2017
2 parents 658941d + d23cba6 commit 0aa17ed
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 21 deletions.
35 changes: 35 additions & 0 deletions vsphere/data_source_vsphere_datacenter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package vsphere

import (
"fmt"

"github.com/hashicorp/terraform/helper/schema"
"github.com/vmware/govmomi"
)

func dataSourceVSphereDatacenter() *schema.Resource {
return &schema.Resource{
Read: dataSourceVSphereDatacenterRead,

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Description: "The name of the datacenter. This can be a name or path. If not provided, the default datacenter is used.",
Optional: true,
},
},
}
}

func dataSourceVSphereDatacenterRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*govmomi.Client)
datacenter := d.Get("name").(string)
dc, err := getDatacenter(client, datacenter)
if err != nil {
return fmt.Errorf("error fetching datacenter: %s", err)
}
id := dc.Reference().Value
d.SetId(id)

return nil
}
90 changes: 90 additions & 0 deletions vsphere/data_source_vsphere_datacenter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package vsphere

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

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

var testAccDataSourceVSphereDatacenterExpectedRegexp = regexp.MustCompile("^datacenter-")

func TestAccDataSourceVSphereDatacenter(t *testing.T) {
var tp *testing.T
testAccDataSourceVSphereDatacenterCases := []struct {
name string
testCase resource.TestCase
}{
{
"basic",
resource.TestCase{
PreCheck: func() {
testAccPreCheck(tp)
testAccDataSourceVSphereDatacenterPreCheck(tp)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceVSphereDatacenterConfig(),
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr(
"data.vsphere_datacenter.dc",
"id",
testAccDataSourceVSphereDatacenterExpectedRegexp,
),
),
},
},
},
},
{
"default",
resource.TestCase{
PreCheck: func() {
testAccPreCheck(tp)
testAccDataSourceVSphereDatacenterPreCheck(tp)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceVSphereDatacenterConfigDefault,
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr(
"data.vsphere_datacenter.dc",
"id",
testAccDataSourceVSphereDatacenterExpectedRegexp,
),
),
},
},
},
},
}

for _, tc := range testAccDataSourceVSphereDatacenterCases {
t.Run(tc.name, func(t *testing.T) {
tp = t
resource.Test(t, tc.testCase)
})
}
}

func testAccDataSourceVSphereDatacenterPreCheck(t *testing.T) {
if os.Getenv("VSPHERE_DATACENTER") == "" {
t.Skip("set VSPHERE_DATACENTER to run vsphere_datacenter acceptance tests")
}
}

func testAccDataSourceVSphereDatacenterConfig() string {
return fmt.Sprintf(`
data "vsphere_datacenter" "dc" {
name = "%s"
}
`, os.Getenv("VSPHERE_DATACENTER"))
}

const testAccDataSourceVSphereDatacenterConfigDefault = `
data "vsphere_datacenter" "dc" {}
`
30 changes: 30 additions & 0 deletions vsphere/datacenter_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package vsphere

import (
"fmt"

"github.com/vmware/govmomi"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/object"
"golang.org/x/net/context"
)

// getDatacenter gets the higher-level datacenter object for the datacenter
// name supplied by dc.
//
// The default datacenter is denoted by using an empty string. When working
// with ESXi directly, the default datacenter is always selected.
func getDatacenter(c *govmomi.Client, dc string) (*object.Datacenter, error) {
finder := find.NewFinder(c.Client, true)
t := c.ServiceContent.About.ApiType
switch t {
case "HostAgent":
return finder.DefaultDatacenter(context.TODO())
case "VirtualCenter":
if dc != "" {
return finder.Datacenter(context.TODO(), dc)
}
return finder.DefaultDatacenter(context.TODO())
}
return nil, fmt.Errorf("unsupported ApiType: %s", t)
}
4 changes: 4 additions & 0 deletions vsphere/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ func Provider() terraform.ResourceProvider {
"vsphere_license": resourceVSphereLicense(),
},

DataSourcesMap: map[string]*schema.Resource{
"vsphere_datacenter": dataSourceVSphereDatacenter(),
},

ConfigureFunc: providerConfigure,
}
}
Expand Down
12 changes: 0 additions & 12 deletions vsphere/resource_vsphere_folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,3 @@ func deleteFolder(client *govmomi.Client, f *folder) error {
}
return nil
}

// getDatacenter gets datacenter object
func getDatacenter(c *govmomi.Client, dc string) (*object.Datacenter, error) {
finder := find.NewFinder(c.Client, true)
if dc != "" {
d, err := finder.Datacenter(context.TODO(), dc)
return d, err
} else {
d, err := finder.DefaultDatacenter(context.TODO())
return d, err
}
}
41 changes: 41 additions & 0 deletions website/docs/d/datacenter.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
layout: "vsphere"
page_title: "VMware vSphere: vsphere_datacenter"
sidebar_current: "docs-vsphere-data-source-datacenter"
description: |-
A data source that can be used to get the ID of a datacenter.
---

# vsphere\_datacenter

The `vsphere_datacenter` data source can be used to discover the ID of a
vSphere datacenter. This can then be used with resources or data sources that
require a datacenter, such as the [`vsphere_host`][data-source-vsphere-host]
data source.

[data-source-vsphere-host]: /docs/providers/vsphere/d/host.html

## Example Usage

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

## Argument Reference

The following arguments are supported:

* `name` - (String) The name of the datacenter. This can be a name or path. If
not provided, the default datacenter is used.

~> **NOTE:** When used against ESXi, this data source _always_ fetches the
server's "default" datacenter, which is a special datacenter unrelated to the
default datacenter that exists in vCenter. Hence, the `name` attribute is
completely ignored.

## Attribute Reference

The only exported attribute is `id`, which is the managed object ID of this
datacenter.
27 changes: 18 additions & 9 deletions website/vsphere.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,35 @@
<a href="/docs/providers/vsphere/index.html">VMware vSphere Provider</a>
</li>

<li<%= sidebar_current("docs-vsphere-data-source") %>>
<a href="#">Data Sources</a>
<ul class="nav nav-visible">
<li<%= sidebar_current("docs-vsphere-data-source-datacenter") %>>
<a href="/docs/providers/vsphere/d/datacenter.html">vsphere_datacenter</a>
</li>
</ul>
</li>

<li<%= sidebar_current("docs-vsphere-resource") %>>
<a href="#">Resources</a>
<ul class="nav nav-visible">
<li<%= sidebar_current("docs-vsphere-resource-virtual-machine") %>>
<a href="/docs/providers/vsphere/r/virtual_machine.html">vsphere_virtual_machine</a>
</li>
<li<%= sidebar_current("docs-vsphere-resource-folder") %>>
<a href="/docs/providers/vsphere/r/folder.html">vsphere_folder</a>
<li<%= sidebar_current("docs-vsphere-resource-datacenter") %>>
<a href="/docs/providers/vsphere/r/datacenter.html">vsphere_datacenter</a>
</li>
<li<%= sidebar_current("docs-vsphere-resource-file") %>>
<a href="/docs/providers/vsphere/r/file.html">vsphere_file</a>
</li>
<li<%= sidebar_current("docs-vsphere-resource-virtual-disk") %>>
<a href="/docs/providers/vsphere/r/virtual_disk.html">vsphere_virtual_disk</a>
<li<%= sidebar_current("docs-vsphere-resource-folder") %>>
<a href="/docs/providers/vsphere/r/folder.html">vsphere_folder</a>
</li>
<li<%= sidebar_current("docs-vsphere-resource-license") %>>
<a href="/docs/providers/vsphere/r/license.html">vsphere_license</a>
</li>
<li<%= sidebar_current("docs-vsphere-resource-datacenter") %>>
<a href="/docs/providers/vsphere/r/datacenter.html">vsphere_datacenter</a>
<li<%= sidebar_current("docs-vsphere-resource-virtual-disk") %>>
<a href="/docs/providers/vsphere/r/virtual_disk.html">vsphere_virtual_disk</a>
</li>
<li<%= sidebar_current("docs-vsphere-resource-virtual-machine") %>>
<a href="/docs/providers/vsphere/r/virtual_machine.html">vsphere_virtual_machine</a>
</li>
</ul>
</li>
Expand Down

0 comments on commit 0aa17ed

Please sign in to comment.