diff --git a/clone/step_clone.go b/clone/step_clone.go index 3db1010e0..294368b95 100644 --- a/clone/step_clone.go +++ b/clone/step_clone.go @@ -13,6 +13,7 @@ type CloneConfig struct { Template string `mapstructure:"template"` DiskSize int64 `mapstructure:"disk_size"` LinkedClone bool `mapstructure:"linked_clone"` + Network string `mapstructure:"network"` Notes string `mapstructure:"notes"` } @@ -68,6 +69,7 @@ func (s *StepCloneVM) Run(ctx context.Context, state multistep.StateBag) multist ResourcePool: s.Location.ResourcePool, Datastore: s.Location.Datastore, LinkedClone: s.Config.LinkedClone, + Network: s.Config.Network, Annotation: s.Config.Notes, }) if err != nil { diff --git a/driver/vm.go b/driver/vm.go index 3390621ef..93c52432e 100644 --- a/driver/vm.go +++ b/driver/vm.go @@ -24,6 +24,7 @@ type CloneConfig struct { ResourcePool string Datastore string LinkedClone bool + Network string Annotation string } @@ -222,10 +223,41 @@ func (template *VirtualMachine) Clone(ctx context.Context, config *CloneConfig) cloneSpec.Snapshot = tpl.Snapshot.CurrentSnapshot } + var configSpec types.VirtualMachineConfigSpec + cloneSpec.Config = &configSpec + if config.Annotation != "" { - var configSpec types.VirtualMachineConfigSpec configSpec.Annotation = config.Annotation - cloneSpec.Config = &configSpec + } + + if config.Network != "" { + net, err := template.driver.finder.Network(ctx, config.Network) + if err != nil { + return nil, err + } + backing, err := net.EthernetCardBackingInfo(ctx) + if err != nil { + return nil, err + } + + devices, err := template.vm.Device(ctx) + if err != nil { + return nil, err + } + + adapter, err := findNetworkAdapter(devices) + if err != nil { + return nil, err + } + + adapter.GetVirtualEthernetCard().Backing = backing + + config := &types.VirtualDeviceConfigSpec{ + Device: adapter.(types.BaseVirtualDevice), + Operation: types.VirtualDeviceConfigSpecOperationEdit, + } + + configSpec.DeviceChange = append(configSpec.DeviceChange, config) } task, err := template.vm.Clone(template.driver.ctx, folder.folder, config.Name, cloneSpec) @@ -630,3 +662,12 @@ func (vm *VirtualMachine) AddConfigParams(params map[string]string) error { _, err = task.WaitForResult(vm.driver.ctx, nil) return err } + +func findNetworkAdapter(l object.VirtualDeviceList) (types.BaseVirtualEthernetCard, error) { + c := l.SelectByType((*types.VirtualEthernetCard)(nil)) + if len(c) == 0 { + return nil, errors.New("no network adapter device found") + } + + return c[0].(types.BaseVirtualEthernetCard), nil +}