From 1422085edef0fb81f9b95472afd7947ac933b95e Mon Sep 17 00:00:00 2001 From: Sylvia Moss Date: Fri, 5 Jun 2020 11:01:58 +0200 Subject: [PATCH] Validate host and network when looking for network (#9361) --- builder/vsphere/clone/step_clone.go | 3 +- builder/vsphere/driver/vm.go | 57 +++++++++++-------- builder/vsphere/iso/step_create.go | 3 +- .../clone/CloneConfig-not-required.mdx | 3 +- .../builder/vsphere/iso/NIC-not-required.mdx | 3 +- 5 files changed, 42 insertions(+), 27 deletions(-) diff --git a/builder/vsphere/clone/step_clone.go b/builder/vsphere/clone/step_clone.go index 86f2ab7c9..9f5cd5f62 100644 --- a/builder/vsphere/clone/step_clone.go +++ b/builder/vsphere/clone/step_clone.go @@ -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"` diff --git a/builder/vsphere/driver/vm.go b/builder/vsphere/driver/vm.go index 0296f57b5..e310719dc 100644 --- a/builder/vsphere/driver/vm.go +++ b/builder/vsphere/driver/vm.go @@ -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{ diff --git a/builder/vsphere/iso/step_create.go b/builder/vsphere/iso/step_create.go index ea3e71bdd..fdcce3c8e 100644 --- a/builder/vsphere/iso/step_create.go +++ b/builder/vsphere/iso/step_create.go @@ -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"` diff --git a/website/pages/partials/builder/vsphere/clone/CloneConfig-not-required.mdx b/website/pages/partials/builder/vsphere/clone/CloneConfig-not-required.mdx index de4d6c8a7..8e07b5e05 100644 --- a/website/pages/partials/builder/vsphere/clone/CloneConfig-not-required.mdx +++ b/website/pages/partials/builder/vsphere/clone/CloneConfig-not-required.mdx @@ -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. \ No newline at end of file diff --git a/website/pages/partials/builder/vsphere/iso/NIC-not-required.mdx b/website/pages/partials/builder/vsphere/iso/NIC-not-required.mdx index 32b56d663..8b8fa9d9d 100644 --- a/website/pages/partials/builder/vsphere/iso/NIC-not-required.mdx +++ b/website/pages/partials/builder/vsphere/iso/NIC-not-required.mdx @@ -1,6 +1,7 @@ -- `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