'cloneVM_Task configure cloned VM to another distributed virtual port

How do I use VirtualMachineConfigSpec to set device change and configure my new clone to another distributed virtual port? My cloned VM network adapter type is VirtualVmxnet3, I would like to configure the 10.xx.xxx.xx-x_vm_ddc (dv port name) dv switch upon successful clone.

Following clone method takes a source vm (powered off) as template for new clone:

public ResponseEntity<?> cloneRhcosVm(@PathVariable String vcenter, @RequestBody VmClone vmClone) {
  VirtualMachine vm = null;
  VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec();
  VirtualMachineConfigSpec configSpec = new VirtualMachineConfigSpec();
  VirtualMachineRelocateSpec relocSpec = new VirtualMachineRelocateSpec();
  VirtualMachineBootOptions boot = new VirtualMachineBootOptions();
  VirtualMachineBootOptionsBootableCdromDevice isoBoot = new
  VirtualMachineBootOptionsBootableCdromDevice();
  VirtualMachineBootOptionsBootableDevice[] bootOrder = new
  VirtualMachineBootOptionsBootableDevice[]{isoBoot};
  List<VirtualDeviceConfigSpec> vdSpecAll = new ArrayList<VirtualDeviceConfigSpec>();
  VirtualDeviceConfigSpec[] vdSpecArray = new VirtualDeviceConfigSpec[]{};

  try {
    String sourceVmName = vmClone.getSourceVmName();
    String cloneVmName = vmClone.getTargetVmName();
    vm = vmService.getVirtualMachine(vcenter, sourceVmName);
    int cloneVmCPUs = vmClone.getTargetVmCPUs();
    long cloneVmMemoryMB = (long) vmClone.getTargetVmMemoryGB() * Constants.MB_TO_GB;
    String cloneVmNicName = vmClone.getTargetVmNicName();
    boot.setBootOrder(bootOrder);

    // Get target nic (dv switch) virutal device
    VirtualDeviceConfigSpec nicSpec = getTargetNicVDConfigSpec(vm, cloneVmNicName);
    if (nicSpec != null) {
      vdSpecAll.add(nicSpec);
    }
    vdSpecArray = vdSpecAll.toArray(new VirtualDeviceConfigSpec[vdSpecAll.size()]);
    configSpec.setDeviceChange(vdSpecArray);

    configSpec.setName(cloneVmName);
    configSpec.setNumCPUs(cloneVmCPUs);
    configSpec.setNumCoresPerSocket(cloneVmCPUs);
    configSpec.setMemoryMB(cloneVmMemoryMB);
    configSpec.setBootOptions(boot);

    cloneSpec.setConfig(configSpec);
    cloneSpec.setLocation(relocSpec);
    cloneSpec.setPowerOn(true);
    cloneSpec.setTemplate(false);

    Task task = vm.cloneVM_Task((Folder) vm.getParent(), cloneVmName, cloneSpec);
    TaskInfo taskInfo = task.getTaskInfo();
    ...
}

Following method used to configure new nic virtual device: targetNicName is the new 10.xx.xxx.xx-x_vm_ddc I want to set to after cloning

private VirtualDeviceConfigSpec getTargetNicVDConfigSpec(VirtualMachine vm, String targetNicName) {
  VirtualDevice[] vds = vm.getConfig().getHardware().getDevice();
  VirtualDeviceConfigSpec nicSpec = new VirtualDeviceConfigSpec();
  VirtualDeviceConnectInfo deviceConnInfo = new VirtualDeviceConnectInfo();
  String adapter1 = "Network adapter 1";

  for (int i = 0; i < vds.length; i++) {
    if ((vds[i] instanceof VirtualEthernetCard) && (vds[i].getDeviceInfo().getLabel().equalsIgnoreCase(adapter1))) {
      VirtualEthernetCard nic = (VirtualEthernetCard) vds[i];
      VirtualEthernetCardNetworkBackingInfo nicBacking = (VirtualEthernetCardNetworkBackingInfo) nic.getBacking();

      deviceConnInfo.setConnected(true);
      nicBacking.setDeviceName(targetNicName);
      nic.setBacking(nicBacking);
      nic.setConnectable(deviceConnInfo);
      nicSpec.setOperation(VirtualDeviceConfigSpecOperation.edit);
      nicSpec.setDevice(nic);
      return nicSpec;
    }
  }
  return null;
}

Above code works but the new network adapter is not being configured as a dvSwitch as I was expecting by setting the new device name.

I found that my network adapter virtual device (vd) is of class VirtualVmxnet3 and the vd's backing info is of class VirtualEthernetCardDistributedVirtualPortBackingInfo, however these classes do not have any method that works like .setDevice(nicName) to configure the network adapter to new dvSwitch by passing name 10.xx.xxx.xx-x_vm_ddc.

Any clues if it's possible to clone and configure to another dv port?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source