Skip to content

feat: add datasource for virtual machines#498

Merged
kp2099 merged 2 commits intovmware:mainfrom
castorsky:feature/datasource-virtual_machine
Dec 15, 2025
Merged

feat: add datasource for virtual machines#498
kp2099 merged 2 commits intovmware:mainfrom
castorsky:feature/datasource-virtual_machine

Conversation

@castorsky
Copy link
Copy Markdown
Contributor

I have implemented one of datasources which was requested in #196.

This datasource searches the vSphere cluster for some VMs that matches specified filters and returns VM name for one (and only one) machine. I have chosen such behavior because it will be straightforward for vsphere-clone builder - there will be no need for user to manipulate lists in locals.

I have included some tests and documentation for datasource.

By strange coincidence issues for vSphere and Proxmox datasources share the same ID. =)

Partially closes #196

@castorsky castorsky requested a review from a team as a code owner December 5, 2024 01:56
@tenthirtyam tenthirtyam changed the title feature: datasource for virtual machines [issue 196] feat: datasource for virtual machines Dec 5, 2024
@tenthirtyam tenthirtyam added the enhancement Enhancement label Dec 5, 2024
@tenthirtyam tenthirtyam self-requested a review December 5, 2024 02:27
@tenthirtyam tenthirtyam added this to the v1.5.0 milestone Dec 5, 2024
@tenthirtyam
Copy link
Copy Markdown
Collaborator

Thanks @castorsky. I’ll review this week.

@tenthirtyam tenthirtyam modified the milestones: Backlog, v2.1.0 May 22, 2025
@tenthirtyam
Copy link
Copy Markdown
Collaborator

I'll pick this one up from Lucas. For now, I've placed this into the v2.1.0 milestone so that we can release v2.0.0 items that have breaking changes first.

@tenthirtyam tenthirtyam force-pushed the feature/datasource-virtual_machine branch from 4dc2048 to b62df9c Compare June 5, 2025 16:29
@tenthirtyam tenthirtyam requested a review from Copilot October 15, 2025 02:29
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements a new vSphere virtual machine data source that searches for VMs in a vSphere cluster based on various filters and returns the name of a single matching virtual machine. The data source is designed to integrate with the vSphere Clone builder by providing a straightforward way to select templates without requiring users to manipulate lists in locals.

Key changes:

  • Added a new virtual machine data source with comprehensive filtering capabilities
  • Implemented test coverage using vSphere simulator
  • Added complete documentation and examples

Reviewed Changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
main.go Registers the new virtual machine data source
datasource/virtualmachine/data.go Core data source implementation with configuration and execution logic
datasource/virtualmachine/filters.go Filtering functions for name, regex, template, host, and tag matching
datasource/virtualmachine/data_test.go Comprehensive test suite using vSphere simulator
datasource/common/driver/driver.go Shared vCenter connection driver for data sources
datasource/common/testing/ Testing utilities for vSphere simulator setup
docs/ Documentation files and partials for the new data source
.web-docs/ Web documentation components and metadata
builder/vsphere/driver/vm.go Removed unnecessary blank line

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@tenthirtyam tenthirtyam force-pushed the feature/datasource-virtual_machine branch 4 times, most recently from 7e725a5 to 9cbac1d Compare October 15, 2025 03:46
@tenthirtyam
Copy link
Copy Markdown
Collaborator

tenthirtyam commented Oct 15, 2025

Hi @castorsky 👋🏻 - Apologies for the long delay! I've picked this one back up (finally). I've squashed the previous commits and have added a follow-up commit that refactors the implementation and unifies the driver usage. More updates to come!

Ryan Johnson, VMware by Broadcom

@tenthirtyam tenthirtyam self-assigned this Oct 15, 2025
@tenthirtyam tenthirtyam added the datasource Datasource label Oct 15, 2025
@tenthirtyam tenthirtyam changed the title feat: datasource for virtual machines [wip] feat: datasource for virtual machines Oct 15, 2025
@tenthirtyam tenthirtyam force-pushed the feature/datasource-virtual_machine branch 2 times, most recently from 26a5edf to eeaccac Compare October 23, 2025 03:17
Signed-off-by: Castor Sky <csky57@gmail.com>
@tenthirtyam tenthirtyam force-pushed the feature/datasource-virtual_machine branch 3 times, most recently from 343b11c to 388878a Compare December 10, 2025 14:56
Refactors the implementation and unifies the driver usage.

Signed-off-by: Ryan Johnson <ryan@tenthirtyam.org>
@tenthirtyam tenthirtyam force-pushed the feature/datasource-virtual_machine branch from 388878a to d5d1e04 Compare December 10, 2025 15:18
@tenthirtyam tenthirtyam changed the title [wip] feat: datasource for virtual machines feat: add datasource for virtual machines Dec 10, 2025
Copy link
Copy Markdown
Collaborator

@tenthirtyam tenthirtyam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🥳

I've performed end-to-end testing using the examples below:

Filter by Tags and Get Latest

data "vsphere-virtualmachine" "example" {
  vcenter_server = "vc01.example.com"
  username       = "administrator@vsphere.local"
  password       = "VMware1!"
  datacenter     = "dc01"
  latest         = true

  tag {
    category = "color"
    name     = "blue"
  }

  tag {
    category = "shape"
    name     = "circle"
  }
}

locals {
  vm_name = data.vsphere-virtualmachine.example.vm_name
}

source "null" "example" {
    communicator = "none"
}

build {
  sources = [
    "source.null.example"
  ]

  provisioner "shell-local" {
    inline = [
      "echo vm_name: ${local.vm_name}",
    ]
  }
}

Filter by Name with Glob Pattern

data "vsphere-virtualmachine" "example" {
  vcenter_server = "vc01.example.com"
  username       = "administrator@vsphere.local"
  password       = "VMware1!"
  datacenter     = "dc01"
  name           = "linux-debian-13-*"
  latest         = true
}

locals {
  vm_result = data.vsphere-virtualmachine.example.vm_name
}

source "null" "example" {
    communicator = "none"
}

build {
  sources = [
    "source.null.example"
  ]

  provisioner "shell-local" {
    inline = [
      "echo vm_name: ${local.vm_result}",
    ]
  }
}

Filter by Name with Regular Expression

data "vsphere-virtualmachine" "example" {
  vcenter_server = "vc01.example.com"
  username       = "administrator@vsphere.local"
  password       = "VMware1!"
  datacenter     = "dc01"
  name_regex     = "^linux-debian-[0-9]+-.+$"
  latest         = true
}

locals {
  vm_result = data.vsphere-virtualmachine.example.vm_name
}

Filter for VM Templates Only

data "vsphere-virtualmachine" "example" {
  vcenter_server = "vc01.example.com"
  username       = "administrator@vsphere.local"
  password       = "VMware1!"
  datacenter     = "dc01"
  name           = "linux-debian-13-*"
  template       = true
}

locals {
  vm_result = data.vsphere-virtualmachine.example.vm_name
}

source "null" "example" {
    communicator = "none"
}

build {
  sources = [
    "source.null.example"
  ]

  provisioner "shell-local" {
    inline = [
      "echo vm_name: ${local.vm_result}",
    ]
  }
}

Filter by ESX Host

data "vsphere-virtualmachine" "example" {
  vcenter_server = "vc01.example.com"
  username       = "administrator@vsphere.local"
  password       = "VMware1!"
  datacenter     = "dc01"
  host           = "esxi-01.example.com"
  name           = "linux-debian-13-*"
  latest         = true
}

locals {
  vm_result = data.vsphere-virtualmachine.example.vm_name
}

source "null" "example" {
    communicator = "none"
}

build {
  sources = [
    "source.null.example"
  ]

  provisioner "shell-local" {
    inline = [
      "echo vm_name: ${local.vm_result}",
    ]
  }
}

@tenthirtyam tenthirtyam requested a review from kp2099 December 10, 2025 18:49
@tenthirtyam
Copy link
Copy Markdown
Collaborator

Now pending approval and merge by @kp2099.

@kp2099 kp2099 merged commit 42c302d into vmware:main Dec 15, 2025
14 checks passed
@github-actions
Copy link
Copy Markdown

I'm going to lock this pull request because it has been closed for 30 days. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 29, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

datasource Datasource enhancement Enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for data sources

5 participants