Support redefining network in vsphere-clone builder

Fixes #226
This commit is contained in:
Vladislav Rassokhin 2019-03-13 23:07:30 +03:00 committed by Michael Kuzmin
parent 0417062d9f
commit 395ce39611
2 changed files with 45 additions and 2 deletions

View File

@ -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 {

View File

@ -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
}