vSphere,通过API(govmomi)将现有硬盘添加到虚拟机

vSphere,通过API(govmomi)将现有硬盘添加到虚拟机,go,vmware,vsphere,Go,Vmware,Vsphere,我正在尝试将现有硬盘从一个虚拟机添加到另一个虚拟机。 我使用golang和此api: 首先,我从源vm获取磁盘,如下所示: for _, device := range devices { currentDeviceLabel := device.GetVirtualDevice().DeviceInfo.GetDescription().Label if strings.Contains(strings.ToLower(currentDeviceLabel),

我正在尝试将现有硬盘从一个虚拟机添加到另一个虚拟机。 我使用golang和此api:

首先,我从源vm获取磁盘,如下所示:

for _, device := range devices {
        currentDeviceLabel := device.GetVirtualDevice().DeviceInfo.GetDescription().Label
        if strings.Contains(strings.ToLower(currentDeviceLabel), "hard disk"){
        disks = append(disks, device)
    }    
return disks
然后我尝试将收到的磁盘添加到其他VM:

func addDisk(vm *object.VirtualMachine, disk types.BaseVirtualDevice) {

    ctx, cancel := context.WithCancel(context.Background())
    defer cancel()

    spec := types.VirtualMachineConfigSpec{

        DeviceChange : []types.BaseVirtualDeviceConfigSpec {

            &types.VirtualDeviceConfigSpec{

                Operation: types.VirtualDeviceConfigSpecOperationAdd,
                FileOperation: types.VirtualDeviceConfigSpecFileOperationCreate,
                Device: disk,
            },

        },
    }

    result, err := vm.Reconfigure(ctx, spec)
    if err != nil {
        log.Fatal(fmt.Sprintf("err: %s", err.Error()))

    }
我从vSphere获取错误:

Cannot complete the operation because the file or folder [xxxxx] xxxxx/xxxxx.vmdk already exists
我做错了什么?谢谢

我在这里得到了答案:

工作代码:

spec := types.VirtualMachineConfigSpec{}
config := &types.VirtualDeviceConfigSpec{
    Device:    disk,
    Operation: types.VirtualDeviceConfigSpecOperationAdd,
}
spec.DeviceChange = append(spec.DeviceChange, config)

result, err := vm.Reconfigure(ctx, spec)
if err != nil {
    log.Fatal(fmt.Sprintf("err: %s", err.Error()))

}