forked from hashicorp/terraform-provider-vsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
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 hashicorp#144 from terraform-providers/f-datacente…
…r-data-source New data source: vsphere_datacenter
- Loading branch information
Showing
7 changed files
with
218 additions
and
21 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,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 | ||
} |
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,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" {} | ||
` |
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,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) | ||
} |
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
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,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. |
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