Skip to content

Commit

Permalink
feat: add virtual_machines list in d/datacenter
Browse files Browse the repository at this point in the history
* Add a list of virtual machine names under the specified
datacenter

Signed-off-by: nikfot <[email protected]>
  • Loading branch information
nikfot committed Oct 11, 2024
1 parent adf6714 commit 85e9e1a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
33 changes: 32 additions & 1 deletion vsphere/data_source_vsphere_datacenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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
}
36 changes: 36 additions & 0 deletions vsphere/data_source_vsphere_datacenter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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"),
)
}
6 changes: 4 additions & 2 deletions website/docs/d/datacenter.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 85e9e1a

Please sign in to comment.