Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
50 changes: 50 additions & 0 deletions vsphere/data_source_vsphere_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

package vsphere

import (

Check failure on line 7 in vsphere/data_source_vsphere_virtual_machine.go

View workflow job for this annotation

GitHub Actions / lint

File is not properly formatted (gofmt)
"fmt"
"log"
"path"
"context"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand All @@ -17,6 +19,8 @@
"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/vim25/mo"
"github.com/vmware/govmomi/vapi/tags"
)

func dataSourceVSphereVirtualMachine() *schema.Resource {
Expand Down Expand Up @@ -213,6 +217,16 @@
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
Expand Down Expand Up @@ -242,6 +256,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 @@ -348,7 +365,40 @@
}
}
_ = d.Set("vtpm", isVTPMPresent)

tagManager := tags.NewManager(restClient)
tagIDs, err := tagManager.ListAttachedTags(ctx, vm.Reference())
if err != nil {
return err
}
tagValues := make([]string, 0, len(tagIDs))
for _, tagID := range tagIDs {
tag, err := tagManager.GetTag(ctx, tagID)
if err != nil {
return err
}
tagValues = append(tagValues, fmt.Sprintf("%s", tag.ID))

Check failure on line 380 in vsphere/data_source_vsphere_virtual_machine.go

View workflow job for this annotation

GitHub Actions / lint

S1025: the argument is already a string, there's no need to use fmt.Sprintf (staticcheck)
}
if err := d.Set("tags", tagValues); err != nil {
return err
}

var vmMo mo.VirtualMachine
err = vm.Properties(ctx, vm.Reference(), []string{"customValue"}, &vmMo)
if err != nil {
return err
}
customAttrs := make(map[string]string)
for _, base := range vmMo.CustomValue {
v, ok := base.(*types.CustomFieldStringValue)
if !ok {
continue
}
customAttrs[strconv.Itoa(int(v.Key))] = v.Value
}
if err := d.Set("custom_attributes", customAttrs); err != nil {
return err
}
log.Printf("[DEBUG] VM search for %q completed successfully (UUID %q)", name, props.Config.Uuid)
return nil
}
Loading