Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate host and network when looking for network #9361

Merged
merged 3 commits into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion builder/vsphere/clone/step_clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
57 changes: 34 additions & 23 deletions builder/vsphere/driver/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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{
Expand Down
3 changes: 2 additions & 1 deletion builder/vsphere/iso/step_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Original file line number Diff line number Diff line change
@@ -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

Expand Down