diff --git a/vsphere/data_source_vsphere_datacenter.go b/vsphere/data_source_vsphere_datacenter.go new file mode 100644 index 000000000..d75d07117 --- /dev/null +++ b/vsphere/data_source_vsphere_datacenter.go @@ -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 +} diff --git a/vsphere/data_source_vsphere_datacenter_test.go b/vsphere/data_source_vsphere_datacenter_test.go new file mode 100644 index 000000000..ff4c56849 --- /dev/null +++ b/vsphere/data_source_vsphere_datacenter_test.go @@ -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" {} +` diff --git a/vsphere/provider.go b/vsphere/provider.go index 5f2e07233..2aafa226e 100644 --- a/vsphere/provider.go +++ b/vsphere/provider.go @@ -72,6 +72,10 @@ func Provider() terraform.ResourceProvider { "vsphere_license": resourceVSphereLicense(), }, + DataSourcesMap: map[string]*schema.Resource{ + "vsphere_datacenter": dataSourceVSphereDatacenter(), + }, + ConfigureFunc: providerConfigure, } } diff --git a/website/docs/d/datacenter.html.markdown b/website/docs/d/datacenter.html.markdown new file mode 100644 index 000000000..f03e4877a --- /dev/null +++ b/website/docs/d/datacenter.html.markdown @@ -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`). diff --git a/website/vsphere.erb b/website/vsphere.erb index 7f82e7966..15228e46b 100644 --- a/website/vsphere.erb +++ b/website/vsphere.erb @@ -10,26 +10,35 @@ VMware vSphere Provider +