Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
review: code fixed
Browse files Browse the repository at this point in the history
- Made minor changes to documentation.
- Standard `errors` and `fmt` were used for custom errors.
- field `Node` was renamed to `Host`.
- field `VmTags` was renamed to `Tags`
- package `virtual_machine` was renamed to `virtualmachine`
- `driver` and `testing` were moved to `common` location
castorsky authored and tenthirtyam committed Dec 9, 2024
1 parent 08527f1 commit 6fdd994
Showing 24 changed files with 618 additions and 587 deletions.
5 changes: 2 additions & 3 deletions .web-docs/README.md
Original file line number Diff line number Diff line change
@@ -51,9 +51,8 @@ packer plugins install github.com/hashicorp/vsphere

#### Data Sources

- [vsphere-virtual_machine](/packer/integrations/hashicorp/vsphere/latest/components/data-source/vsphere-virtual_machine) -
This datasource returns name of existing virtual machine that matches all defined filters to use
it as a builder source for `vsphere-clone`.
- [vsphere-virtualmachine](/packer/integrations/hashicorp/vsphere/latest/components/data-source/vsphere-virtualmachine) -
This data source returns the name of a virtual machine that matches all defined filters.

#### Post-Processors

155 changes: 0 additions & 155 deletions .web-docs/components/data-source/virtual_machine/README.md

This file was deleted.

160 changes: 160 additions & 0 deletions .web-docs/components/data-source/virtualmachine/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
Type: `vsphere-virtualmachine`
Artifact BuilderId: `vsphere.virtualmachine`

This data source retrieves information about existing virtual machines from vSphere
and return name of one virtual machine that matches all specified filters. This virtual
machine can be used in the vSphere Clone builder to select a template.

## Configuration Reference

### Filters Configuration

**Optional:**

<!-- Code generated from the comments of the Config struct in datasource/virtualmachine/data.go; DO NOT EDIT MANUALLY -->

- `name` (string) - Basic filter with glob support (e.g. `ubuntu_basic*`). Defaults to `*`.
Using strict globs will not reduce execution time because vSphere API
returns the full inventory. But can be used for better readability over
regular expressions.

- `name_regex` (string) - Extended name filter with regular expressions support
(e.g. `ubuntu[-_]basic[0-9]*`). Default is empty. The match of the
regular expression is checked by substring. Use `^` and `$` to define a
full string. For example, the `^[^_]+$` filter will search names
without any underscores. The expression must use
[Go Regex Syntax](https://pkg.go.dev/regexp/syntax).

- `template` (bool) - Filter to return only objects that are virtual machine templates.
Defaults to `false` and returns all virtual machines.

- `host` (string) - Filter to search virtual machines only on the specified ESX host.

- `tags` ([]Tag) - Filter to return only that virtual machines that have attached all
specifies tags. Specify one or more `tags` blocks to define list of tags
for the filter.

- `latest` (bool) - This filter determines how to handle multiple machines that were
matched with all previous filters. Machine creation time is being used
to find latest. By default, multiple matching machines results in an
error.

<!-- End of code generated from the comments of the Config struct in datasource/virtualmachine/data.go; -->


### Tags Filter Configuration

<!-- Code generated from the comments of the Tag struct in datasource/virtualmachine/data.go; DO NOT EDIT MANUALLY -->

HCL Example:

```hcl
tags {
category = "team"
name = "operations"
}
tags {
category = "sla"
name = "gold"
}
```

<!-- End of code generated from the comments of the Tag struct in datasource/virtualmachine/data.go; -->


**Required:**

<!-- Code generated from the comments of the Tag struct in datasource/virtualmachine/data.go; DO NOT EDIT MANUALLY -->

- `name` (string) - Name of the tag added to virtual machine which must pass the `tags`
filter.

- `category` (string) - Name of the tag category that contains the tag.

-> **Note:** Both `name` and `category` must be specified in the `tags`
filter.

<!-- End of code generated from the comments of the Tag struct in datasource/virtualmachine/data.go; -->


### Connection Configuration

**Optional:**

<!-- Code generated from the comments of the ConnectConfig struct in builder/vsphere/common/step_connect.go; DO NOT EDIT MANUALLY -->

- `vcenter_server` (string) - The fully qualified domain name or IP address of the vCenter Server
instance.

- `username` (string) - The username to authenticate with the vCenter Server instance.

- `password` (string) - The password to authenticate with the vCenter Server instance.

- `insecure_connection` (bool) - Do not validate the certificate of the vCenter Server instance.
Defaults to `false`.

-> **Note:** This option is beneficial in scenarios where the certificate
is self-signed or does not meet standard validation criteria.

- `datacenter` (string) - The name of the datacenter object in the vSphere inventory.

-> **Note:** Required if more than one datacenter object exists in the
vSphere inventory.

<!-- End of code generated from the comments of the ConnectConfig struct in builder/vsphere/common/step_connect.go; -->


## Output

<!-- Code generated from the comments of the DatasourceOutput struct in datasource/virtualmachine/data.go; DO NOT EDIT MANUALLY -->

- `vm_name` (string) - Name of the found virtual machine.

<!-- End of code generated from the comments of the DatasourceOutput struct in datasource/virtualmachine/data.go; -->


## Example Usage

This example demonstrates how to connect to vSphere cluster and search for the latest virtual machine
that matches the filters. The name of the machine is then output to the console as an output variable.
```hcl
data "vsphere-virtualmachine" "default" {
vcenter_server = "vcenter.example.com"
insecure_connection = true
username = "administrator@vsphere.local"
password = "VMware1!"
datacenter = "dc-01"
latest = true
tags {
category = "team"
name = "operations"
}
tags {
category = "sla"
name = "gold"
}
}
locals {
vm_name = data.vsphere-virtualmachine.default.vm_name
}
source "null" "example" {
communicator = "none"
}
build {
sources = [
"source.null.example"
]
provisioner "shell-local" {
inline = [
"echo vm_name: ${local.vm_name}",
]
}
}
```
2 changes: 1 addition & 1 deletion .web-docs/metadata.hcl
Original file line number Diff line number Diff line change
@@ -36,6 +36,6 @@ integration {
component {
type = "data-source"
name = "vSphere Virtual Machine"
slug = "vsphere-virtual_machine"
slug = "vsphere-virtualmachine"
}
}
1 change: 0 additions & 1 deletion builder/vsphere/driver/vm.go
Original file line number Diff line number Diff line change
@@ -449,7 +449,6 @@ func (vm *VirtualMachineDriver) Clone(ctx context.Context, config *CloneConfig)
Device: adapter.(types.BaseVirtualDevice),
Operation: types.VirtualDeviceConfigSpecOperationEdit,
}

configSpec.DeviceChange = append(configSpec.DeviceChange, config)
}

60 changes: 60 additions & 0 deletions datasource/common/driver/driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package driver

import (
"context"
"fmt"
"net/url"

"github.com/hashicorp/packer-plugin-vsphere/builder/vsphere/common"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vapi/rest"
)

type VCenterDriver struct {
Ctx context.Context
Client *govmomi.Client
RestClient *rest.Client
Finder *find.Finder
Datacenter *object.Datacenter
}

func NewDriver(config common.ConnectConfig) (*VCenterDriver, error) {
ctx := context.Background()

vcenterUrl, err := url.Parse(fmt.Sprintf("https://%v/sdk", config.VCenterServer))
if err != nil {
return nil, fmt.Errorf("failed to parse URL: %w", err)
}
vcenterUrl.User = url.UserPassword(config.Username, config.Password)

client, err := govmomi.NewClient(ctx, vcenterUrl, true)
if err != nil {
return nil, fmt.Errorf("failed to create govmomi Client: %w", err)
}

restClient := rest.NewClient(client.Client)
err = restClient.Login(ctx, vcenterUrl.User)
if err != nil {
return nil, fmt.Errorf("failed to login to REST API endpoint: %w", err)
}

finder := find.NewFinder(client.Client, true)
datacenter, err := finder.DatacenterOrDefault(ctx, config.Datacenter)
if err != nil {
return nil, fmt.Errorf("failed to find datacenter: %w", err)
}
finder.SetDatacenter(datacenter)

return &VCenterDriver{
Ctx: ctx,
Client: client,
RestClient: restClient,
Finder: finder,
Datacenter: datacenter,
}, nil
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package testing

import (
"context"
"fmt"

"github.com/pkg/errors"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vapi/tags"
)

// MarkSimulatedVmAsTemplate powers off the virtual machine before converting it to a template (because the simulator
// creates all virtual machines in an online state).
func MarkSimulatedVmAsTemplate(ctx context.Context, vm *object.VirtualMachine) error {
task, err := vm.PowerOff(ctx)
if err != nil {
return errors.Wrap(err, "failed to issue powering off command to the machine")
return fmt.Errorf("failed to issue powering off command to the machine: %w", err)
}
err = task.Wait(ctx)
if err != nil {
return errors.Wrap(err, "failed to power off the machine")
return fmt.Errorf("failed to power off the machine: %w", err)
}
err = vm.MarkAsTemplate(ctx)
if err != nil {
return errors.Wrap(err, "failed to mark VM as a template")
return fmt.Errorf("failed to mark virtual machine as a template: %w", err)
}
return nil
}

// Try to find category passed by name, create category if not found and return category ID.
// FindOrCreateCategory tries to find category passed by name, creates category if not found and returns category ID.
// Category will be created with "MULTIPLE" constraint.
func FindOrCreateCategory(ctx context.Context, man *tags.Manager, catName string) (string, error) {
categoryList, err := man.GetCategories(ctx)
if err != nil {
return "", errors.Wrap(err, "cannot get categories from cluster")
return "", fmt.Errorf("cannot return categories from cluster: %w", err)
}
for _, category := range categoryList {
if category.Name == catName {
@@ -38,16 +43,16 @@ func FindOrCreateCategory(ctx context.Context, man *tags.Manager, catName string
}
newCategoryID, err := man.CreateCategory(ctx, &tags.Category{Name: catName, Cardinality: "MULTIPLE"})
if err != nil {
return "", errors.Wrap(err, "cannot create category")
return "", fmt.Errorf("cannot create category: %w", err)
}
return newCategoryID, nil
}

// Try to find the tagName in category with catID, create if not found and return tag ID.
// FindOrCreateTag tries to find the tagName in category with catID, creates if not found and returns tag ID.
func FindOrCreateTag(ctx context.Context, man *tags.Manager, catID string, tagName string) (string, error) {
tagsInCategory, err := man.GetTagsForCategory(ctx, catID)
if err != nil {
return "", errors.Wrap(err, "cannot get tags for category")
return "", fmt.Errorf("cannot return tags for category: %w", err)
}
for _, tag := range tagsInCategory {
if tag.Name == tagName {
@@ -56,7 +61,7 @@ func FindOrCreateTag(ctx context.Context, man *tags.Manager, catID string, tagNa
}
newTagID, err := man.CreateTag(ctx, &tags.Tag{Name: tagName, CategoryID: catID})
if err != nil {
return "", errors.Wrap(err, "cannot create tag")
return "", fmt.Errorf("cannot create tag: %w", err)
}
return newTagID, nil
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package testing

import (
"context"
"crypto/tls"
"fmt"
"net/url"
"time"

"github.com/pkg/errors"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/object"
@@ -43,12 +46,12 @@ type VCenterSimulator struct {
func NewVCenterSimulator(model *simulator.Model) (*VCenterSimulator, error) {
ctx := context.Background()
if model == nil {
return nil, errors.New("model has not been initialized")
return nil, fmt.Errorf("model has not been initialized")
}

err := model.Create()
if err != nil {
return nil, errors.Wrap(err, "failed to create simulator model")
return nil, fmt.Errorf("failed to create simulator model: %w", err)
}
model.Service.RegisterEndpoints = true
model.Service.TLS = new(tls.Config)
@@ -57,29 +60,29 @@ func NewVCenterSimulator(model *simulator.Model) (*VCenterSimulator, error) {

u, err := url.Parse(server.URL.String())
if err != nil {
return nil, errors.Wrap(err, "failed to parse simulator URL")
return nil, fmt.Errorf("failed to parse simulator URL: %w", err)
}
password, _ := simulator.DefaultLogin.Password()
u.User = url.UserPassword(simulator.DefaultLogin.Username(), password)

client, err := govmomi.NewClient(ctx, u, true)
if err != nil {
return nil, errors.Wrap(err, "failed to connect to SOAP simulator")
return nil, fmt.Errorf("failed to connect to SOAP simulator: %w", err)
}

restClient := rest.NewClient(client.Client)
err = restClient.Login(ctx, simulator.DefaultLogin)
if err != nil {
return nil, errors.Wrap(err, "failed to login to REST simulator")
return nil, fmt.Errorf("failed to login to REST simulator: %w", err)
}

finder := find.NewFinder(client.Client, false)
dcs, err := finder.DatacenterList(ctx, "*")
if err != nil {
return nil, errors.Wrap(err, "failed to list datacenters")
return nil, fmt.Errorf("failed to list datacenters: %w", err)
}
if len(dcs) == 0 {
return nil, errors.Wrap(err, "datacenters were not found in the simulator")
return nil, fmt.Errorf("datacenters were not found in the simulator: %w", err)
}
finder.SetDatacenter(dcs[0])

@@ -110,7 +113,7 @@ func (sim *VCenterSimulator) CustomizeSimulator(vmsConfig []SimulatedVMConfig) e

vms, err := sim.Finder.VirtualMachineList(sim.Ctx, "*")
if err != nil {
return errors.Wrap(err, "failed to list VMs in cluster")
return fmt.Errorf("failed to list virtual machines in cluster: %w", err)
}

for i := 0; i < len(vmsConfig); i++ {
@@ -125,33 +128,33 @@ func (sim *VCenterSimulator) CustomizeSimulator(vmsConfig []SimulatedVMConfig) e
if vmsConfig[i].Name != "" {
task, err := vms[i].Reconfigure(sim.Ctx, vmConfig)
if err != nil {
return errors.Wrap(err, "failed to issue rename of VM command")
return fmt.Errorf("failed to issue rename of virtual machine command: %w", err)
}
if err = task.Wait(sim.Ctx); err != nil {
return errors.Wrap(err, "failed to rename VM")
return fmt.Errorf("failed to rename virtual machine: %w", err)
}
}

if vmsConfig[i].Template {
err = MarkSimulatedVmAsTemplate(sim.Ctx, vms[i])
if err != nil {
return errors.Wrap(err, "failed to convert VMs to templates")
return fmt.Errorf("failed to convert to templates: %w", err)
}
}

if vmsConfig[i].Tags != nil {
for _, tag := range vmsConfig[i].Tags {
catID, err := FindOrCreateCategory(sim.Ctx, tagMan, tag.Category)
if err != nil {
return errors.Wrap(err, "failed to find/create category")
return fmt.Errorf("failed to find/create category: %w", err)
}
tagID, err := FindOrCreateTag(sim.Ctx, tagMan, catID, tag.Name)
if err != nil {
return errors.Wrap(err, "failed to find/create tag")
return fmt.Errorf("failed to find/create tag: %w", err)
}
err = tagMan.AttachTag(sim.Ctx, tagID, vms[i].Reference())
if err != nil {
return errors.Wrap(err, "failed to attach tag to VM")
return fmt.Errorf("failed to attach tag to virtual machine: %w", err)
}
}
}
63 changes: 0 additions & 63 deletions datasource/virtual_machine/driver.go

This file was deleted.

132 changes: 0 additions & 132 deletions datasource/virtual_machine/filters.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -3,65 +3,76 @@

//go:generate packer-sdc struct-markdown
//go:generate packer-sdc mapstructure-to-hcl2 -type Config,Tag,DatasourceOutput
package virtual_machine
package virtualmachine

import (
"errors"
"fmt"

"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/packer-plugin-sdk/common"
"github.com/hashicorp/packer-plugin-sdk/hcl2helper"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/template/config"
vsCommon "github.com/hashicorp/packer-plugin-vsphere/builder/vsphere/common"
"github.com/pkg/errors"
vsphere "github.com/hashicorp/packer-plugin-vsphere/builder/vsphere/common"
"github.com/hashicorp/packer-plugin-vsphere/datasource/common/driver"
"github.com/zclconf/go-cty/cty"
)

// Example of multiple vm_tags blocks in HCL format:
// ```
// HCL Example:
//
// ```hcl
//
// vm_tags {
// tags {
// category = "team"
// name = "operations"
// }
// vm_tags {
// category = "SLA"
// tags {
// category = "sla"
// name = "gold"
// }
//
// ```
type Tag struct {
// Tag with this name must be attached to virtual machine which should pass the Tags Filter.
// Name of the tag added to virtual machine which must pass the `tags`
// filter.
Name string `mapstructure:"name" required:"true"`
// Name of the category that contains this tag. Both tag and category must be specified.
// Name of the tag category that contains the tag.
//
// -> **Note:** Both `name` and `category` must be specified in the `tags`
// filter.
Category string `mapstructure:"category" required:"true"`
}

type Config struct {
common.PackerConfig `mapstructure:",squash"`
vsCommon.ConnectConfig `mapstructure:",squash"`
common.PackerConfig `mapstructure:",squash"`
vsphere.ConnectConfig `mapstructure:",squash"`

// Basic filter with glob support (e.g. `nginx_basic*`). Defaults to `*`.
// Using strict globs will not reduce execution time because vSphere API returns the full inventory.
// But can be used for better readability over regular expressions.
// Basic filter with glob support (e.g. `ubuntu_basic*`). Defaults to `*`.
// Using strict globs will not reduce execution time because vSphere API
// returns the full inventory. But can be used for better readability over
// regular expressions.
Name string `mapstructure:"name"`
// Extended name filter with regular expressions support (e.g. `nginx[-_]basic[0-9]*`). Default is empty.
// The match of the regular expression is checked by substring. Use `^` and `$` to define a full string.
// E.g. the `^[^_]+$` filter will search names without any underscores.
// The expression must use [Go Regex Syntax](https://pkg.go.dev/regexp/syntax).
// Extended name filter with regular expressions support
// (e.g. `ubuntu[-_]basic[0-9]*`). Default is empty. The match of the
// regular expression is checked by substring. Use `^` and `$` to define a
// full string. For example, the `^[^_]+$` filter will search names
// without any underscores. The expression must use
// [Go Regex Syntax](https://pkg.go.dev/regexp/syntax).
NameRegex string `mapstructure:"name_regex"`
// Filter to return only objects that are virtual machine templates.
// Defaults to `false` and returns all VMs.
// Defaults to `false` and returns all virtual machines.
Template bool `mapstructure:"template"`
// Filter to search virtual machines only on the specified node.
Node string `mapstructure:"node"`
// Filter to return only that virtual machines that have attached all specifies tags.
// Specify one or more `vm_tags` blocks to define list of tags that will make up the filter.
// Should work since vCenter 6.7. To avoid incompatibility, REST client is being
// initialized only when at least one tag has been defined in the config.
VmTags []Tag `mapstructure:"vm_tags"`
// This filter determines how to handle multiple machines that were matched with all
// previous filters. Machine creation time is being used to find latest.
// By default, multiple matching machines results in an error.
// Filter to search virtual machines only on the specified ESX host.
Host string `mapstructure:"host"`
// Filter to return only that virtual machines that have attached all
// specifies tags. Specify one or more `tags` blocks to define list of tags
// for the filter.
Tags []Tag `mapstructure:"tags"`
// This filter determines how to handle multiple machines that were
// matched with all previous filters. Machine creation time is being used
// to find latest. By default, multiple matching machines results in an
// error.
Latest bool `mapstructure:"latest"`
}

@@ -98,8 +109,8 @@ func (d *Datasource) Configure(raws ...interface{}) error {
if d.config.Password == "" {
errs = packersdk.MultiErrorAppend(errs, errors.New("'password' is required"))
}
if len(d.config.VmTags) > 0 {
for _, tag := range d.config.VmTags {
if len(d.config.Tags) > 0 {
for _, tag := range d.config.Tags {
if tag.Name == "" || tag.Category == "" {
errs = packersdk.MultiErrorAppend(errs, errors.New("both name and category are required for tag"))
}
@@ -118,16 +129,16 @@ func (d *Datasource) OutputSpec() hcldec.ObjectSpec {
}

func (d *Datasource) Execute() (cty.Value, error) {
driver, err := newDriver(d.config)
dr, err := driver.NewDriver(d.config.ConnectConfig)
if err != nil {
return cty.NullVal(cty.EmptyObject), errors.Wrap(err, "failed to initialize driver")
return cty.NullVal(cty.EmptyObject), fmt.Errorf("failed to initialize driver: %w", err)
}

// This is the first level of filters
// (the finder with glob will return filtered list or drop an error if found nothing).
filteredVms, err := driver.finder.VirtualMachineList(driver.ctx, d.config.Name)
filteredVms, err := dr.Finder.VirtualMachineList(dr.Ctx, d.config.Name)
if err != nil {
return cty.NullVal(cty.EmptyObject), errors.Wrap(err, "failed to retrieve virtual machines list")
return cty.NullVal(cty.EmptyObject), fmt.Errorf("failed to retrieve virtual machines list: %w", err)
}

// Chain of other filters that will be executed only when defined
@@ -137,40 +148,40 @@ func (d *Datasource) Execute() (cty.Value, error) {
}

if len(filteredVms) > 0 && d.config.Template {
filteredVms, err = filterByTemplate(driver, filteredVms)
filteredVms, err = filterByTemplate(dr, filteredVms)
if err != nil {
return cty.NullVal(cty.EmptyObject), errors.Wrap(err, "failed to filter by template attribute")
return cty.NullVal(cty.EmptyObject), fmt.Errorf("failed to filter by template attribute: %w", err)
}
}

if len(filteredVms) > 0 && d.config.Node != "" {
filteredVms, err = filterByNode(driver, d.config, filteredVms)
if len(filteredVms) > 0 && d.config.Host != "" {
filteredVms, err = filterByHost(dr, d.config, filteredVms)
if err != nil {
return cty.NullVal(cty.EmptyObject), errors.Wrap(err, "failed to filter by node attribute")
return cty.NullVal(cty.EmptyObject), fmt.Errorf("failed to filter by host attribute: %w", err)
}
}

if len(filteredVms) > 0 && d.config.VmTags != nil {
filteredVms, err = filterByTags(driver, d.config.VmTags, filteredVms)
if len(filteredVms) > 0 && d.config.Tags != nil {
filteredVms, err = filterByTags(dr, d.config.Tags, filteredVms)
if err != nil {
return cty.NullVal(cty.EmptyObject), errors.Wrap(err, "failed to filter by tags")
return cty.NullVal(cty.EmptyObject), fmt.Errorf("failed to filter by tags: %w", err)
}
}

// No VMs passed the filter chain. Nothing to return.
if len(filteredVms) == 0 {
return cty.NullVal(cty.EmptyObject), errors.New("not a single VM matches the configured filters")
return cty.NullVal(cty.EmptyObject), errors.New("no virtual machine matches the filters")
}

if len(filteredVms) > 1 {
if d.config.Latest {
filteredVms, err = filterByLatest(driver, filteredVms)
filteredVms, err = filterByLatest(dr, filteredVms)
if err != nil {
return cty.NullVal(cty.EmptyObject), errors.Wrap(err, "failed to find the latest VM")
return cty.NullVal(cty.EmptyObject), fmt.Errorf("failed to find the latest virtual machine: %w", err)
}
} else {
// Too many machines passed the filter chain. Cannot decide which machine to return.
return cty.NullVal(cty.EmptyObject), errors.New("multiple VMs match the configured filters")
return cty.NullVal(cty.EmptyObject), errors.New("more than one virtual machine matched the filters")
}
}

Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
package virtual_machine
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package virtualmachine

import (
"testing"
"time"

vsCommon "github.com/hashicorp/packer-plugin-vsphere/builder/vsphere/common"
"github.com/hashicorp/packer-plugin-vsphere/builder/vsphere/common"
"github.com/vmware/govmomi/simulator"

dsTesting "github.com/hashicorp/packer-plugin-vsphere/datasource/virtual_machine/testing"
commonT "github.com/hashicorp/packer-plugin-vsphere/datasource/common/testing"
)

func TestExecute(t *testing.T) {
machinesToPrepare := []dsTesting.SimulatedVMConfig{
machinesToPrepare := []commonT.SimulatedVMConfig{
{
Name: "first-vm",
Tags: []dsTesting.Tag{
Tags: []commonT.Tag{
{
Category: "operating-system-class",
Name: "Linux",
},
},
}, {
Name: "second-vm",
Tags: []dsTesting.Tag{
Tags: []commonT.Tag{
{
Category: "operating-system-class",
Name: "Linux",
@@ -39,7 +42,7 @@ func TestExecute(t *testing.T) {
Template: true,
}, {
Name: "machine-three",
Tags: []dsTesting.Tag{
Tags: []commonT.Tag{
{
Category: "operating-system-class",
Name: "Linux",
@@ -57,7 +60,7 @@ func TestExecute(t *testing.T) {
model.Datacenter = 2
model.Machine = 8

vcSim, err := dsTesting.NewVCenterSimulator(model)
vcSim, err := commonT.NewVCenterSimulator(model)
if err != nil {
t.Fatalf("error creating vCenter simulator: %s", err)
}
@@ -69,7 +72,7 @@ func TestExecute(t *testing.T) {
}

simulatorPassword, _ := vcSim.Server.URL.User.Password()
connectConfig := vsCommon.ConnectConfig{
connectConfig := common.ConnectConfig{
VCenterServer: vcSim.Server.URL.Host,
Username: vcSim.Server.URL.User.Username(),
Password: simulatorPassword,
@@ -133,27 +136,27 @@ func TestExecute(t *testing.T) {
},
},
{
name: "found multiple machines at the node, error",
name: "found multiple machines at the host, error",
expectFailure: true,
expectVmName: "",
config: Config{
Node: "DC0_H0",
Host: "DC0_H0",
},
},
{
name: "cluster node not found, error",
name: "cluster host not found, error",
expectFailure: true,
expectVmName: "",
config: Config{
Node: "unexpected_node",
Host: "unexpected_host",
},
},
{
name: "found machine with defined set of tags, no error",
expectFailure: false,
expectVmName: "second-vm",
config: Config{
VmTags: []Tag{
Tags: []Tag{
{
Category: "security-team",
Name: "blue",
@@ -170,7 +173,7 @@ func TestExecute(t *testing.T) {
expectFailure: true,
expectVmName: "",
config: Config{
VmTags: []Tag{
Tags: []Tag{
{
Category: "operating-system-class",
Name: "Linux",
137 changes: 137 additions & 0 deletions datasource/virtualmachine/filters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package virtualmachine

import (
"fmt"
"regexp"
"time"

"github.com/hashicorp/packer-plugin-vsphere/datasource/common/driver"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/property"
"github.com/vmware/govmomi/vapi/tags"
"github.com/vmware/govmomi/vim25/mo"
)

// filterByNameRegex filters machines by matching their names against defined regular expression.
func filterByNameRegex(vmList []*object.VirtualMachine, nameRegex string) []*object.VirtualMachine {
re, _ := regexp.Compile(nameRegex)
result := make([]*object.VirtualMachine, 0)
for _, i := range vmList {
if re.MatchString(i.Name()) {
result = append(result, i)
}
}
return result
}

// filterByTemplate filters machines by template attribute. Only templates will pass the filter.
func filterByTemplate(driver *driver.VCenterDriver, vmList []*object.VirtualMachine) ([]*object.VirtualMachine, error) {
result := make([]*object.VirtualMachine, 0)
for _, i := range vmList {
isTemplate, err := i.IsTemplate(driver.Ctx)
if err != nil {
return nil, fmt.Errorf("error checking if virtual machine is a template: %w", err)
}

if isTemplate {
result = append(result, i)
}
}
return result, nil
}

// filterByHost filters machines by ESX host placement.
// Only machines that are stored on the defined host will pass the filter.
func filterByHost(driver *driver.VCenterDriver, config Config, vmList []*object.VirtualMachine) ([]*object.VirtualMachine, error) {
pc := property.DefaultCollector(driver.Client.Client)
obj, err := driver.Finder.HostSystem(driver.Ctx, config.Host)
if err != nil {
return nil, fmt.Errorf("error finding defined host system: %w", err)
}

var host mo.HostSystem
err = pc.RetrieveOne(driver.Ctx, obj.Reference(), []string{"vm"}, &host)
if err != nil {
return nil, fmt.Errorf("error retrieving properties of host system: %w", err)
}

var hostVms []mo.VirtualMachine
err = pc.Retrieve(driver.Ctx, host.Vm, []string{"name"}, &hostVms)
if err != nil {
return nil, fmt.Errorf("failed to get properties for the virtual machine: %w", err)
}

result := make([]*object.VirtualMachine, 0)
for _, filteredVm := range vmList {
vmName := filteredVm.Name()
for _, hostVm := range hostVms {
if vmName == hostVm.Name {
result = append(result, filteredVm)
}
}
}

return result, nil
}

// filterByTags filters machines by tags. Only machines that has all the tags from list will pass the filter.
func filterByTags(driver *driver.VCenterDriver, vmTags []Tag, vmList []*object.VirtualMachine) ([]*object.VirtualMachine, error) {
result := make([]*object.VirtualMachine, 0)
tagMan := tags.NewManager(driver.RestClient)
for _, filteredVm := range vmList {
realTagsList, err := tagMan.GetAttachedTags(driver.Ctx, filteredVm.Reference())
if err != nil {
return nil, fmt.Errorf("failed return tags for the virtual machine: %w", err)
}
matchedTagsCount := 0
for _, configTag := range vmTags {
configTagMatched := false
for _, realTag := range realTagsList {
if configTag.Name == realTag.Name {
category, err := tagMan.GetCategory(driver.Ctx, realTag.CategoryID)
if err != nil {
return nil, fmt.Errorf("failed to return tag category for tag: %w", err)
}
if configTag.Category == category.Name {
configTagMatched = true
break
}
}
}
if configTagMatched {
matchedTagsCount++
} else {
// If a single requested tag from config not matched then no need to proceed.
// Fail early.
break
}
}
if matchedTagsCount == len(vmTags) {
result = append(result, filteredVm)
}
}

return result, nil
}

// filterByLatest filters machines by creation date. This filter returns list with one element.
func filterByLatest(driver *driver.VCenterDriver, vmList []*object.VirtualMachine) ([]*object.VirtualMachine, error) {
var latestVM *object.VirtualMachine
var latestTimestamp time.Time
for _, elementVM := range vmList {
var vmConfig mo.VirtualMachine
err := elementVM.Properties(driver.Ctx, elementVM.Reference(), []string{"config"}, &vmConfig)
if err != nil {
return nil, fmt.Errorf("error retrieving config properties for the virtual machine: %w", err)
}
if vmConfig.Config.CreateDate.After(latestTimestamp) {
latestVM = elementVM
latestTimestamp = *vmConfig.Config.CreateDate
}
}
result := []*object.VirtualMachine{latestVM}
return result, nil
}
26 changes: 0 additions & 26 deletions docs-partials/datasource/virtual_machine/Config-not-required.mdx

This file was deleted.

7 changes: 0 additions & 7 deletions docs-partials/datasource/virtual_machine/Tag-required.mdx

This file was deleted.

29 changes: 29 additions & 0 deletions docs-partials/datasource/virtualmachine/Config-not-required.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!-- Code generated from the comments of the Config struct in datasource/virtualmachine/data.go; DO NOT EDIT MANUALLY -->

- `name` (string) - Basic filter with glob support (e.g. `ubuntu_basic*`). Defaults to `*`.
Using strict globs will not reduce execution time because vSphere API
returns the full inventory. But can be used for better readability over
regular expressions.

- `name_regex` (string) - Extended name filter with regular expressions support
(e.g. `ubuntu[-_]basic[0-9]*`). Default is empty. The match of the
regular expression is checked by substring. Use `^` and `$` to define a
full string. For example, the `^[^_]+$` filter will search names
without any underscores. The expression must use
[Go Regex Syntax](https://pkg.go.dev/regexp/syntax).

- `template` (bool) - Filter to return only objects that are virtual machine templates.
Defaults to `false` and returns all virtual machines.

- `host` (string) - Filter to search virtual machines only on the specified ESX host.

- `tags` ([]Tag) - Filter to return only that virtual machines that have attached all
specifies tags. Specify one or more `tags` blocks to define list of tags
for the filter.

- `latest` (bool) - This filter determines how to handle multiple machines that were
matched with all previous filters. Machine creation time is being used
to find latest. By default, multiple matching machines results in an
error.

<!-- End of code generated from the comments of the Config struct in datasource/virtualmachine/data.go; -->
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- Code generated from the comments of the DatasourceOutput struct in datasource/virtual_machine/data.go; DO NOT EDIT MANUALLY -->
<!-- Code generated from the comments of the DatasourceOutput struct in datasource/virtualmachine/data.go; DO NOT EDIT MANUALLY -->

- `vm_name` (string) - Name of the found virtual machine.

<!-- End of code generated from the comments of the DatasourceOutput struct in datasource/virtual_machine/data.go; -->
<!-- End of code generated from the comments of the DatasourceOutput struct in datasource/virtualmachine/data.go; -->
11 changes: 11 additions & 0 deletions docs-partials/datasource/virtualmachine/Tag-required.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- Code generated from the comments of the Tag struct in datasource/virtualmachine/data.go; DO NOT EDIT MANUALLY -->

- `name` (string) - Name of the tag added to virtual machine which must pass the `tags`
filter.

- `category` (string) - Name of the tag category that contains the tag.

-> **Note:** Both `name` and `category` must be specified in the `tags`
filter.

<!-- End of code generated from the comments of the Tag struct in datasource/virtualmachine/data.go; -->
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<!-- Code generated from the comments of the Tag struct in datasource/virtual_machine/data.go; DO NOT EDIT MANUALLY -->
<!-- Code generated from the comments of the Tag struct in datasource/virtualmachine/data.go; DO NOT EDIT MANUALLY -->

Example of multiple vm_tags blocks in HCL format:
```
HCL Example:

```hcl

vm_tags {
tags {
category = "team"
name = "operations"
}
vm_tags {
category = "SLA"
tags {
category = "sla"
name = "gold"
}

```

<!-- End of code generated from the comments of the Tag struct in datasource/virtual_machine/data.go; -->
<!-- End of code generated from the comments of the Tag struct in datasource/virtualmachine/data.go; -->
5 changes: 2 additions & 3 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -51,9 +51,8 @@ packer plugins install github.com/hashicorp/vsphere

#### Data Sources

- [vsphere-virtual_machine](/packer/integrations/hashicorp/vsphere/latest/components/data-source/vsphere-virtual_machine) -
This datasource returns name of existing virtual machine that matches all defined filters to use
it as a builder source for `vsphere-clone`.
- [vsphere-virtualmachine](/packer/integrations/hashicorp/vsphere/latest/components/data-source/vsphere-virtualmachine) -
This data source returns the name of a virtual machine that matches all defined filters.

#### Post-Processors

92 changes: 0 additions & 92 deletions docs/datasources/virtual_machine.mdx

This file was deleted.

89 changes: 89 additions & 0 deletions docs/datasources/virtualmachine.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
modeline: |
vim: set ft=pandoc:
description: |
This data source retrieves information about existing virtual machines from vSphere
and return name of one virtual machine that matches all specified filters. This virtual
machine can be used in the vSphere Clone builder to select a template.
page_title: vSphere Virtual Machine - Data Source
sidebar_title: vSphere Virtual Machine
---

# Virtual Machine Data Source

Type: `vsphere-virtualmachine`
Artifact BuilderId: `vsphere.virtualmachine`

This data source retrieves information about existing virtual machines from vSphere
and return name of one virtual machine that matches all specified filters. This virtual
machine can be used in the vSphere Clone builder to select a template.

## Configuration Reference

### Filters Configuration

**Optional:**

@include 'datasource/virtualmachine/Config-not-required.mdx'

### Tags Filter Configuration

@include 'datasource/virtualmachine/Tag.mdx'

**Required:**

@include 'datasource/virtualmachine/Tag-required.mdx'

### Connection Configuration

**Optional:**

@include 'builder/vsphere/common/ConnectConfig-not-required.mdx'

## Output

@include 'datasource/virtualmachine/DatasourceOutput.mdx'

## Example Usage

This example demonstrates how to connect to vSphere cluster and search for the latest virtual machine
that matches the filters. The name of the machine is then output to the console as an output variable.
```hcl
data "vsphere-virtualmachine" "default" {
vcenter_server = "vcenter.example.com"
insecure_connection = true
username = "administrator@vsphere.local"
password = "VMware1!"
datacenter = "dc-01"
latest = true
tags {
category = "team"
name = "operations"
}
tags {
category = "sla"
name = "gold"
}
}
locals {
vm_name = data.vsphere-virtualmachine.default.vm_name
}
source "null" "example" {
communicator = "none"
}
build {
sources = [
"source.null.example"
]
provisioner "shell-local" {
inline = [
"echo vm_name: ${local.vm_name}",
]
}
}
```
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ import (
"github.com/hashicorp/packer-plugin-vsphere/builder/vsphere/clone"
"github.com/hashicorp/packer-plugin-vsphere/builder/vsphere/iso"
"github.com/hashicorp/packer-plugin-vsphere/builder/vsphere/supervisor"
"github.com/hashicorp/packer-plugin-vsphere/datasource/virtual_machine"
"github.com/hashicorp/packer-plugin-vsphere/datasource/virtualmachine"
"github.com/hashicorp/packer-plugin-vsphere/post-processor/vsphere"
vsphereTemplate "github.com/hashicorp/packer-plugin-vsphere/post-processor/vsphere-template"
"github.com/hashicorp/packer-plugin-vsphere/version"
@@ -23,7 +23,7 @@ func main() {
pps.RegisterBuilder("iso", new(iso.Builder))
pps.RegisterBuilder("clone", new(clone.Builder))
pps.RegisterBuilder("supervisor", new(supervisor.Builder))
pps.RegisterDatasource("virtual_machine", new(virtual_machine.Datasource))
pps.RegisterDatasource("virtualmachine", new(virtualmachine.Datasource))
pps.RegisterPostProcessor(plugin.DEFAULT_NAME, new(vsphere.PostProcessor))
pps.RegisterPostProcessor("template", new(vsphereTemplate.PostProcessor))
pps.SetVersion(version.PluginVersion)

0 comments on commit 6fdd994

Please sign in to comment.