diff --git a/docs/data-sources/virtual_machine.md b/docs/data-sources/virtual_machine.md index 0daf03f9a..dc8f89c1b 100644 --- a/docs/data-sources/virtual_machine.md +++ b/docs/data-sources/virtual_machine.md @@ -74,7 +74,10 @@ The following arguments are supported: scan for disk attributes and controller types on. Default: `1`. * `nvme_controller_scan_count` - (Optional) The number of NVMe controllers to scan for disk attributes and controller types on. Default: `1`. - +* `custom_attributes` - A map of custom attribute IDs to their corresponding values assigned to the virtual machine. + The map keys represent the custom attribute IDs and the values represent the attribute values. +* `tags` - A list of tag IDs attached to the virtual machine. + Each value represents the unique identifier of a tag assigned to the virtual machine. [docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider ~> **NOTE:** For best results, ensure that all the disks on any templates you diff --git a/vsphere/data_source_vsphere_virtual_machine.go b/vsphere/data_source_vsphere_virtual_machine.go index 26ecbf422..f8f1816fb 100644 --- a/vsphere/data_source_vsphere_virtual_machine.go +++ b/vsphere/data_source_vsphere_virtual_machine.go @@ -5,6 +5,7 @@ package vsphere import ( + "context" "fmt" "log" "path" @@ -17,6 +18,11 @@ import ( "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/structure" "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/virtualmachine" "github.com/vmware/terraform-provider-vsphere/vsphere/internal/virtualdevice" + + "github.com/vmware/govmomi/property" + "github.com/vmware/govmomi/vapi/tags" + "github.com/vmware/govmomi/vim25/mo" + "github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/customattribute" ) func dataSourceVSphereVirtualMachine() *schema.Resource { @@ -213,6 +219,16 @@ func dataSourceVSphereVirtualMachine() *schema.Resource { Computed: true, Description: "Indicates whether a virtual Trusted Platform Module (TPM) device is present on the virtual machine.", }, + "tags": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "custom_attributes": { + Type: schema.TypeMap, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, } // Merge the VirtualMachineConfig structure so that we can include the number of @@ -242,6 +258,9 @@ func dataSourceVSphereVirtualMachine() *schema.Resource { func dataSourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*Client).vimClient + providerClient := meta.(*Client) + restClient := providerClient.restClient + ctx := context.Background() uuid := d.Get("uuid").(string) moid := d.Get("moid").(string) name := d.Get("name").(string) @@ -349,6 +368,25 @@ func dataSourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{ } _ = d.Set("vtpm", isVTPMPresent) + var moVM mo.VirtualMachine + pc := property.DefaultCollector(client.Client) + + if err := pc.RetrieveOne( + ctx, + vm.Reference(), + []string{"customValue"}, + &moVM, + ); err != nil { + return err + } + + customattribute.ReadFromResource(&moVM.ManagedEntity, d) + + tagManager := tags.NewManager(restClient) + if err := readTagsForResource(tagManager, vm, d); err != nil { + return fmt.Errorf("error reading tags for VM: %s", err) + } + log.Printf("[DEBUG] VM search for %q completed successfully (UUID %q)", name, props.Config.Uuid) return nil }