diff --git a/vsphere/data_source_vsphere_datacenter.go b/vsphere/data_source_vsphere_datacenter.go index 11f87ed01..3c847b68a 100644 --- a/vsphere/data_source_vsphere_datacenter.go +++ b/vsphere/data_source_vsphere_datacenter.go @@ -4,9 +4,13 @@ package vsphere import ( + "context" "fmt" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/vmware/govmomi/find" + "github.com/vmware/govmomi/view" + "github.com/vmware/govmomi/vim25/mo" ) func dataSourceVSphereDatacenter() *schema.Resource { @@ -19,11 +23,18 @@ func dataSourceVSphereDatacenter() *schema.Resource { Description: "The name of the datacenter. This can be a name or path. Can be omitted if there is only one datacenter in your inventory.", Optional: true, }, + "virtual_machines": { + Type: schema.TypeSet, + Description: "The list of all vms included in the datacenter.", + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, }, } } func dataSourceVSphereDatacenterRead(d *schema.ResourceData, meta interface{}) error { + ctx := context.Background() client := meta.(*Client).vimClient datacenter := d.Get("name").(string) dc, err := getDatacenter(client, datacenter) @@ -32,6 +43,26 @@ func dataSourceVSphereDatacenterRead(d *schema.ResourceData, meta interface{}) e } id := dc.Reference().Value d.SetId(id) - + finder := find.NewFinder(client.Client) + finder.SetDatacenter(dc) + viewManager := view.NewManager(client.Client) + view, err := viewManager.CreateContainerView(ctx, dc.Reference(), []string{"VirtualMachine"}, true) + if err != nil { + return fmt.Errorf("error fetching datacenter: %s", err) + } + defer view.Destroy(ctx) + var vms []mo.VirtualMachine + err = view.Retrieve(ctx, []string{"VirtualMachine"}, []string{"name"}, &vms) + if err != nil { + return fmt.Errorf("error fetching virtual machines: %s", err) + } + vmNames := []string{} + for v := range vms { + vmNames = append(vmNames, vms[v].Name) + } + err = d.Set("virtual_machines", vmNames) + if err != nil { + return fmt.Errorf("error setting virtual_machines: %s", err) + } return nil } diff --git a/vsphere/data_source_vsphere_datacenter_test.go b/vsphere/data_source_vsphere_datacenter_test.go index 2a94e1869..f7d1e3154 100644 --- a/vsphere/data_source_vsphere_datacenter_test.go +++ b/vsphere/data_source_vsphere_datacenter_test.go @@ -66,6 +66,30 @@ func testAccDataSourceVSphereDatacenterPreCheck(t *testing.T) { } } +func TestAccDataSourceVSphereDatacenter_getVirtualMachines(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { + RunSweepers() + testAccPreCheck(t) + testAccDataSourceVSphereDatacenterPreCheck(t) + }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceVSphereDatacenterConfigGetVirtualMachines(), + Check: resource.ComposeTestCheckFunc( + resource.TestMatchResourceAttr( + "data.vsphere_datacenter.dc", + "id", + testAccDataSourceVSphereDatacenterExpectedRegexp, + ), + testCheckOutputBool("found_virtual_machines", "true"), + ), + }, + }, + }) +} + func testAccDataSourceVSphereDatacenterConfig() string { return fmt.Sprintf(` data "vsphere_datacenter" "dc" { @@ -77,3 +101,15 @@ data "vsphere_datacenter" "dc" { const testAccDataSourceVSphereDatacenterConfigDefault = ` data "vsphere_datacenter" "dc" {} ` + +func testAccDataSourceVSphereDatacenterConfigGetVirtualMachines() string { + return fmt.Sprintf(` +data "vsphere_datacenter" "dc" { + name = "%s" +} +output "found_virtual_machines" { + value = "${length(data.vsphere_datacenter.dc.virtual_machines) >= 1 ? "true" : "false" }" +} +`, os.Getenv("TF_VAR_VSPHERE_DATACENTER"), + ) +} diff --git a/website/docs/d/datacenter.html.markdown b/website/docs/d/datacenter.html.markdown index 6484b04d8..bf5787753 100644 --- a/website/docs/d/datacenter.html.markdown +++ b/website/docs/d/datacenter.html.markdown @@ -38,7 +38,7 @@ instance. Hence, the `name` attribute is completely ignored. ## Attribute Reference -The only exported attribute is `id`, which is the [managed object -ID][docs-about-morefs] of this datacenter. +- `id`, which is the [managed objectID][docs-about-morefs] of this datacenter. +- `virtual_machines`, returns a list of virtual machine names in the specified datacenter. [docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider