Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/data-sources/virtual_machine.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions vsphere/data_source_vsphere_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@
package vsphere

import (
"context"

Check failure on line 8 in vsphere/data_source_vsphere_virtual_machine.go

View workflow job for this annotation

GitHub Actions / lint

other declaration of context
"fmt"

Check failure on line 9 in vsphere/data_source_vsphere_virtual_machine.go

View workflow job for this annotation

GitHub Actions / lint

other declaration of fmt
"log"

Check failure on line 10 in vsphere/data_source_vsphere_virtual_machine.go

View workflow job for this annotation

GitHub Actions / lint

other declaration of log
"path"

"context"

Check failure on line 13 in vsphere/data_source_vsphere_virtual_machine.go

View workflow job for this annotation

GitHub Actions / lint

"context" imported and not used

Check failure on line 13 in vsphere/data_source_vsphere_virtual_machine.go

View workflow job for this annotation

GitHub Actions / lint

context redeclared in this block
"fmt"

Check failure on line 14 in vsphere/data_source_vsphere_virtual_machine.go

View workflow job for this annotation

GitHub Actions / lint

"fmt" imported and not used

Check failure on line 14 in vsphere/data_source_vsphere_virtual_machine.go

View workflow job for this annotation

GitHub Actions / lint

fmt redeclared in this block
"log"

Check failure on line 15 in vsphere/data_source_vsphere_virtual_machine.go

View workflow job for this annotation

GitHub Actions / lint

"log" imported and not used

Check failure on line 15 in vsphere/data_source_vsphere_virtual_machine.go

View workflow job for this annotation

GitHub Actions / lint

log redeclared in this block
"path"

Check failure on line 16 in vsphere/data_source_vsphere_virtual_machine.go

View workflow job for this annotation

GitHub Actions / lint

path redeclared in this block

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/property"
"github.com/vmware/govmomi/vim25/types"
"github.com/vmware/govmomi/vapi/tags"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/customattribute"
"github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/folder"
"github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/structure"
"github.com/vmware/terraform-provider-vsphere/vsphere/internal/helper/virtualmachine"
Expand Down Expand Up @@ -213,6 +223,18 @@
Computed: true,
Description: "Indicates whether a virtual Trusted Platform Module (TPM) device is present on the virtual machine.",
},
"tags": {
Type: schema.TypeList,
Computed: true,
Description: "The tags applied to this virtual machine",
Elem: &schema.Schema{Type: schema.TypeString},
},
"custom_attributes": {
Type: schema.TypeMap,
Computed: true,
Description: "The custom attributes applied to this virtual machine",
Elem: &schema.Schema{Type: schema.TypeString},
},
}

// Merge the VirtualMachineConfig structure so that we can include the number of
Expand Down Expand Up @@ -242,6 +264,9 @@

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)
Expand Down Expand Up @@ -349,6 +374,25 @@
}
_ = 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
}
Loading