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.
This data source fetches the MOID for a vSphere datacenter, and is intended for use in other up-and-coming resources that will use a managed object reference to a datacenter to fetch other resources (like host systems), or tell resources where they need to be deployed to (may eventually be used in vsphere_virtual_machine, for example).
- Loading branch information
1 parent
701a445
commit effd718
Showing
5 changed files
with
202 additions
and
9 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,41 @@ | ||
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, | ||
}, | ||
"datacenter_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Description: "The managed object ID of the datacenter.", | ||
Computed: 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) | ||
d.Set("datacenter_id", 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,100 @@ | ||
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, | ||
), | ||
resource.TestMatchResourceAttr( | ||
"data.vsphere_datacenter.dc", | ||
"datacenter_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, | ||
), | ||
resource.TestMatchResourceAttr( | ||
"data.vsphere_datacenter.dc", | ||
"datacenter_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
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,39 @@ | ||
--- | ||
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. It can also be used to fetch the "default datacenter" on | ||
ESXi, however this can be done with [`vsphere_host`][data-source-vsphere-host] | ||
as well. | ||
|
||
[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:** `name` is ignored on ESXi, and is not required. | ||
|
||
## Attribute Reference | ||
|
||
* `id` - The managed object ID of this datacenter. | ||
* `datacenter_id` - (String) The managed object ID of this datacenter (same as | ||
`id`). |
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