|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +//go:generate packer-sdc struct-markdown |
| 5 | +//go:generate packer-sdc mapstructure-to-hcl2 -type Config,Tag,DatasourceOutput |
| 6 | +package virtual_machine |
| 7 | + |
| 8 | +import ( |
| 9 | + "github.com/hashicorp/hcl/v2/hcldec" |
| 10 | + "github.com/hashicorp/packer-plugin-sdk/common" |
| 11 | + "github.com/hashicorp/packer-plugin-sdk/hcl2helper" |
| 12 | + packersdk "github.com/hashicorp/packer-plugin-sdk/packer" |
| 13 | + "github.com/hashicorp/packer-plugin-sdk/template/config" |
| 14 | + vsCommon "github.com/hashicorp/packer-plugin-vsphere/builder/vsphere/common" |
| 15 | + "github.com/pkg/errors" |
| 16 | + "github.com/zclconf/go-cty/cty" |
| 17 | +) |
| 18 | + |
| 19 | +// Example of multiple vm_tags blocks in HCL format: |
| 20 | +// ``` |
| 21 | +// |
| 22 | +// vm_tags { |
| 23 | +// category = "team" |
| 24 | +// name = "operations" |
| 25 | +// } |
| 26 | +// vm_tags { |
| 27 | +// category = "SLA" |
| 28 | +// name = "gold" |
| 29 | +// } |
| 30 | +// |
| 31 | +// ``` |
| 32 | +type Tag struct { |
| 33 | + // Tag with this name must be attached to virtual machine which should pass the Tags Filter. |
| 34 | + Name string `mapstructure:"name" required:"true"` |
| 35 | + // Name of the category that contains this tag. Both tag and category must be specified. |
| 36 | + Category string `mapstructure:"category" required:"true"` |
| 37 | +} |
| 38 | + |
| 39 | +type Config struct { |
| 40 | + common.PackerConfig `mapstructure:",squash"` |
| 41 | + vsCommon.ConnectConfig `mapstructure:",squash"` |
| 42 | + |
| 43 | + // Basic filter with glob support (e.g. `nginx_basic*`). Defaults to `*`. |
| 44 | + // Using strict globs will not reduce execution time because vSphere API returns the full inventory. |
| 45 | + // But can be used for better readability over regular expressions. |
| 46 | + Name string `mapstructure:"name"` |
| 47 | + // Extended name filter with regular expressions support (e.g. `nginx[-_]basic[0-9]*`). Default is empty. |
| 48 | + // The match of the regular expression is checked by substring. Use `^` and `$` to define a full string. |
| 49 | + // E.g. the `^[^_]+$` filter will search names without any underscores. |
| 50 | + // The expression must use [Go Regex Syntax](https://pkg.go.dev/regexp/syntax). |
| 51 | + NameRegex string `mapstructure:"name_regex"` |
| 52 | + // Filter to return only objects that are virtual machine templates. |
| 53 | + // Defaults to `false` and returns all VMs. |
| 54 | + Template bool `mapstructure:"template"` |
| 55 | + // Filter to search virtual machines only on the specified node. |
| 56 | + Node string `mapstructure:"node"` |
| 57 | + // Filter to return only that virtual machines that have attached all specifies tags. |
| 58 | + // Specify one or more `vm_tags` blocks to define list of tags that will make up the filter. |
| 59 | + // Should work since vCenter 6.7. To avoid incompatibility, REST client is being |
| 60 | + // initialized only when at least one tag has been defined in the config. |
| 61 | + VmTags []Tag `mapstructure:"vm_tags"` |
| 62 | + // This filter determines how to handle multiple machines that were matched with all |
| 63 | + // previous filters. Machine creation time is being used to find latest. |
| 64 | + // By default, multiple matching machines results in an error. |
| 65 | + Latest bool `mapstructure:"latest"` |
| 66 | +} |
| 67 | + |
| 68 | +type Datasource struct { |
| 69 | + config Config |
| 70 | +} |
| 71 | + |
| 72 | +type DatasourceOutput struct { |
| 73 | + // Name of the found virtual machine. |
| 74 | + VmName string `mapstructure:"vm_name"` |
| 75 | +} |
| 76 | + |
| 77 | +func (d *Datasource) ConfigSpec() hcldec.ObjectSpec { |
| 78 | + return d.config.FlatMapstructure().HCL2Spec() |
| 79 | +} |
| 80 | + |
| 81 | +func (d *Datasource) Configure(raws ...interface{}) error { |
| 82 | + err := config.Decode(&d.config, nil, raws...) |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + |
| 87 | + if d.config.Name == "" { |
| 88 | + d.config.Name = "*" |
| 89 | + } |
| 90 | + |
| 91 | + var errs *packersdk.MultiError |
| 92 | + if d.config.VCenterServer == "" { |
| 93 | + errs = packersdk.MultiErrorAppend(errs, errors.New("'vcenter_server' is required")) |
| 94 | + } |
| 95 | + if d.config.Username == "" { |
| 96 | + errs = packersdk.MultiErrorAppend(errs, errors.New("'username' is required")) |
| 97 | + } |
| 98 | + if d.config.Password == "" { |
| 99 | + errs = packersdk.MultiErrorAppend(errs, errors.New("'password' is required")) |
| 100 | + } |
| 101 | + if len(d.config.VmTags) > 0 { |
| 102 | + for _, tag := range d.config.VmTags { |
| 103 | + if tag.Name == "" || tag.Category == "" { |
| 104 | + errs = packersdk.MultiErrorAppend(errs, errors.New("both name and category are required for tag")) |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if errs != nil && len(errs.Errors) > 0 { |
| 110 | + return errs |
| 111 | + } |
| 112 | + |
| 113 | + return nil |
| 114 | +} |
| 115 | + |
| 116 | +func (d *Datasource) OutputSpec() hcldec.ObjectSpec { |
| 117 | + return (&DatasourceOutput{}).FlatMapstructure().HCL2Spec() |
| 118 | +} |
| 119 | + |
| 120 | +func (d *Datasource) Execute() (cty.Value, error) { |
| 121 | + driver, err := newDriver(d.config) |
| 122 | + if err != nil { |
| 123 | + return cty.NullVal(cty.EmptyObject), errors.Wrap(err, "failed to initialize driver") |
| 124 | + } |
| 125 | + |
| 126 | + // This is the first level of filters |
| 127 | + // (the finder with glob will return filtered list or drop an error if found nothing). |
| 128 | + filteredVms, err := driver.finder.VirtualMachineList(driver.ctx, d.config.Name) |
| 129 | + if err != nil { |
| 130 | + return cty.NullVal(cty.EmptyObject), errors.Wrap(err, "failed to retrieve virtual machines list") |
| 131 | + } |
| 132 | + |
| 133 | + // Chain of other filters that will be executed only when defined |
| 134 | + // and previous filter in chain left some virtual machines in the list. |
| 135 | + if d.config.NameRegex != "" { |
| 136 | + filteredVms = filterByNameRegex(filteredVms, d.config.NameRegex) |
| 137 | + } |
| 138 | + |
| 139 | + if len(filteredVms) > 0 && d.config.Template { |
| 140 | + filteredVms, err = filterByTemplate(driver, filteredVms) |
| 141 | + if err != nil { |
| 142 | + return cty.NullVal(cty.EmptyObject), errors.Wrap(err, "failed to filter by template attribute") |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + if len(filteredVms) > 0 && d.config.Node != "" { |
| 147 | + filteredVms, err = filterByNode(driver, d.config, filteredVms) |
| 148 | + if err != nil { |
| 149 | + return cty.NullVal(cty.EmptyObject), errors.Wrap(err, "failed to filter by node attribute") |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + if len(filteredVms) > 0 && d.config.VmTags != nil { |
| 154 | + filteredVms, err = filterByTags(driver, d.config.VmTags, filteredVms) |
| 155 | + if err != nil { |
| 156 | + return cty.NullVal(cty.EmptyObject), errors.Wrap(err, "failed to filter by tags") |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + // No VMs passed the filter chain. Nothing to return. |
| 161 | + if len(filteredVms) == 0 { |
| 162 | + return cty.NullVal(cty.EmptyObject), errors.New("not a single VM matches the configured filters") |
| 163 | + } |
| 164 | + |
| 165 | + if len(filteredVms) > 1 { |
| 166 | + if d.config.Latest { |
| 167 | + filteredVms, err = filterByLatest(driver, filteredVms) |
| 168 | + if err != nil { |
| 169 | + return cty.NullVal(cty.EmptyObject), errors.Wrap(err, "failed to find the latest VM") |
| 170 | + } |
| 171 | + } else { |
| 172 | + // Too many machines passed the filter chain. Cannot decide which machine to return. |
| 173 | + return cty.NullVal(cty.EmptyObject), errors.New("multiple VMs match the configured filters") |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + output := DatasourceOutput{ |
| 178 | + VmName: filteredVms[0].Name(), |
| 179 | + } |
| 180 | + |
| 181 | + return hcl2helper.HCL2ValueFromConfig(output, d.OutputSpec()), nil |
| 182 | +} |
0 commit comments