From 85e9e1aeea08f0c0735b26ddfc896e9ec289535a Mon Sep 17 00:00:00 2001 From: nikfot Date: Fri, 4 Oct 2024 14:32:43 +0300 Subject: [PATCH] feat: add virtual_machines list in d/datacenter * Add a list of virtual machine names under the specified datacenter Signed-off-by: nikfot --- vsphere/data_source_vsphere_datacenter.go | 33 ++++++++++++++++- .../data_source_vsphere_datacenter_test.go | 36 +++++++++++++++++++ website/docs/d/datacenter.html.markdown | 6 ++-- 3 files changed, 72 insertions(+), 3 deletions(-) diff --git a/vsphere/data_source_vsphere_datacenter.go b/vsphere/data_source_vsphere_datacenter.go index 11f87ed01..a03009804 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: "List of all virtual machines included in the vSphere datacenter object.", + 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..580866c8a 100644 --- a/website/docs/d/datacenter.html.markdown +++ b/website/docs/d/datacenter.html.markdown @@ -38,7 +38,9 @@ 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. +The following attributes are exported: + +* `id` - The [managed objectID][docs-about-morefs] of the vSphere datacenter object. +* `virtual_machines` - List of all virtual machines included in the vSphere datacenter object. [docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider