Validate host and network when looking for network (#9361)

This commit is contained in:
Sylvia Moss 2020-06-05 11:01:58 +02:00 committed by GitHub
parent 671aba9def
commit 1422085ede
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 27 deletions

View File

@ -21,7 +21,8 @@ type CloneConfig struct {
DiskSize int64 `mapstructure:"disk_size"`
// Create VM as a linked clone from latest snapshot. Defaults to `false`.
LinkedClone bool `mapstructure:"linked_clone"`
// Set network VM will be connected to.
// Set the network in which the VM will be connected to. If no network is specified, `host`
// must be specified to allow Packer to look for the available network.
Network string `mapstructure:"network"`
// VM notes.
Notes string `mapstructure:"notes"`

View File

@ -619,30 +619,10 @@ func addNetwork(d *Driver, devices object.VirtualDeviceList, config *CreateConfi
return nil, errors.New("no network adapters have been defined")
}
var network object.NetworkReference
for _, nic := range config.NICs {
if nic.Network == "" {
h, err := d.FindHost(config.Host)
if err != nil {
return nil, err
}
i, err := h.Info("network")
if err != nil {
return nil, err
}
if len(i.Network) > 1 {
return nil, fmt.Errorf("Host has multiple networks. Specify it explicitly")
}
network = object.NewNetwork(d.client.Client, i.Network[0])
} else {
var err error
network, err = d.finder.Network(d.ctx, nic.Network)
if err != nil {
return nil, err
}
network, err := findNetwork(nic.Network, config.Host, d)
if err != nil {
return nil, err
}
backing, err := network.EthernetCardBackingInfo(d.ctx)
@ -667,6 +647,37 @@ func addNetwork(d *Driver, devices object.VirtualDeviceList, config *CreateConfi
return devices, nil
}
func findNetwork(network string, host string, d *Driver) (object.NetworkReference, error) {
if network != "" {
var err error
network, err := d.finder.Network(d.ctx, network)
if err != nil {
return nil, err
}
return network, nil
}
if host != "" {
h, err := d.FindHost(host)
if err != nil {
return nil, err
}
i, err := h.Info("network")
if err != nil {
return nil, err
}
if len(i.Network) > 1 {
return nil, fmt.Errorf("Host has multiple networks. Specify it explicitly")
}
return object.NewNetwork(d.client.Client, i.Network[0]), nil
}
return nil, fmt.Errorf("Couldn't find network; 'host' and 'network' not specified. At least one of the two must be specified.")
}
func newVGPUProfile(vGPUProfile string) types.VirtualPCIPassthrough {
return types.VirtualPCIPassthrough{
VirtualDevice: types.VirtualDevice{

View File

@ -31,7 +31,8 @@ import (
// ],
// ```
type NIC struct {
// Set network VM will be connected to.
// Set the network in which the VM will be connected to. If no network is specified, `host`
// must be specified to allow Packer to look for the available network.
Network string `mapstructure:"network"`
// Set VM network card type. Example `vmxnet3`.
NetworkCard string `mapstructure:"network_card" required:"true"`

View File

@ -6,7 +6,8 @@
- `linked_clone` (bool) - Create VM as a linked clone from latest snapshot. Defaults to `false`.
- `network` (string) - Set network VM will be connected to.
- `network` (string) - Set the network in which the VM will be connected to. If no network is specified, `host`
must be specified to allow Packer to look for the available network.
- `notes` (string) - VM notes.

View File

@ -1,6 +1,7 @@
<!-- Code generated from the comments of the NIC struct in builder/vsphere/iso/step_create.go; DO NOT EDIT MANUALLY -->
- `network` (string) - Set network VM will be connected to.
- `network` (string) - Set the network in which the VM will be connected to. If no network is specified, `host`
must be specified to allow Packer to look for the available network.
- `mac_address` (string) - Set network card MAC address