diff --git a/driver/vm.go b/driver/vm.go index 28d8a3c0d..9609e8c90 100644 --- a/driver/vm.go +++ b/driver/vm.go @@ -49,6 +49,7 @@ type CreateConfig struct { GuestOS string // example: otherGuest Network string // "" for default network Overwrite bool + NetworkCard string // example: vmxnet3 } func (d *Driver) NewVM(ref *types.ManagedObjectReference) *VirtualMachine { @@ -153,6 +154,15 @@ func (vm *VirtualMachine) Info(params ...string) (*mo.VirtualMachine, error) { return &info, nil } +func (vm *VirtualMachine) Devices() (object.VirtualDeviceList, error) { + vmInfo, err := vm.Info("config.hardware.device") + if err != nil { + return nil, err + } + + return vmInfo.Config.Hardware.Device, nil +} + func (template *VirtualMachine) Clone(config *CloneConfig) (*VirtualMachine, error) { folder, err := template.driver.FindFolder(config.Folder) if err != nil { @@ -406,7 +416,7 @@ func addNetwork(d *Driver, devices object.VirtualDeviceList, config *CreateConfi return nil, err } - device, err := object.EthernetCardTypes().CreateEthernetCard("", backing) + device, err := object.EthernetCardTypes().CreateEthernetCard(config.NetworkCard, backing) if err != nil { return nil, err } diff --git a/iso/builder_acc_test.go b/iso/builder_acc_test.go index da45a1d2b..7f07e85f2 100644 --- a/iso/builder_acc_test.go +++ b/iso/builder_acc_test.go @@ -12,7 +12,7 @@ func TestISOBuilderAcc_default(t *testing.T) { builderT.Test(t, builderT.TestCase{ Builder: &Builder{}, Template: commonT.RenderConfig(config), - Check: checkDefault(t, config["vm_name"].(string), config["host"].(string), "datastore1"), + Check: checkDefault(t, config["vm_name"].(string), config["host"].(string), "datastore1"), }) } @@ -29,6 +29,7 @@ func defaultConfig() map[string]interface{} { "ssh_password": "jetbrains", "vm_name": commonT.NewVMName(), + "communicator": "none", // do not start the VM without any bootable devices } return config @@ -159,3 +160,37 @@ func cdromConfig() string { config["iso_path"] = "[datastore1] alpine-standard-3.6.2-x86_64.iso" return commonT.RenderConfig(config) } + +func TestISOBuilderAcc_networkCard(t *testing.T) { + builderT.Test(t, builderT.TestCase{ + Builder: &Builder{}, + Template: networkCardConfig(), + Check: checkNetworkCard(t), + }) +} + +func networkCardConfig() string { + config := defaultConfig() + config["network_card"] = "vmxnet3" + return commonT.RenderConfig(config) +} + +func checkNetworkCard(t *testing.T) builderT.TestCheckFunc { + return func(artifacts []packer.Artifact) error { + d := commonT.TestConn(t) + + vm := commonT.GetVM(t, d, artifacts) + devices, err := vm.Devices() + if err != nil { + t.Fatalf("Cannot read VM properties: %v", err) + } + for _, device := range devices { + if devices.TypeName(device) == "VirtualVmxnet3" { + return nil + } + } + t.Errorf("Cannot find selected network card") + + return nil + } +} diff --git a/iso/step_create.go b/iso/step_create.go index b1d5232c6..819ff7d36 100644 --- a/iso/step_create.go +++ b/iso/step_create.go @@ -21,6 +21,7 @@ type CreateConfig struct { Datastore string `mapstructure:"datastore"` GuestOSType string `mapstructure:"guest_os_type"` Network string `mapstructure:"network"` + NetworkCard string `mapstructure:"network_card"` } func (c *CreateConfig) Prepare() []error { @@ -77,6 +78,7 @@ func (s *StepCreateVM) Run(state multistep.StateBag) multistep.StepAction { Datastore: s.config.Datastore, GuestOS: s.config.GuestOSType, Network: s.config.Network, + NetworkCard: s.config.NetworkCard, }) if err != nil {