VMware Cloud Community
ebbot654
Contributor
Contributor

VSphere API - Assigning network when cloning

Hello.

I am cloning virtual machines using the Vim API for Perl, vCenter version 6.5. It clones successfully with my provided VirtualMachineCloneSpec, but the configuration of VLAN/Network does not work as expected.

I specify the network device change in location attribute like this:

my $backing = VirtualEthernetCardNetworkBackingInfo->new(

     deviceName => $nic_label,

     useAutoDetect => 0,

     network => $net->[0]->{'mo_ref'}

);

$location_spec{'deviceChange'} = [

     VirtualDeviceConfigSpec->new(

          device => VirtualEthernetCard->new(

               backing => $backing,

               key => $nic_key

          ),

          operation => VirtualDeviceConfigSpecOperation-new('edit')

     )

];

This is only a small part of the code. The $location_spec object is later supplied as the "clone_spec->location" as a VirtualMachineRelocateSpec object.

Don't worry about the details I left out. My question is:

Should this method of changing the Network in the clone spec work?

It sure seems that way in the docs:

Under CloneSpec -> config:

Deprecated. as of vSphere API 6.0. Use deviceChange in location instead for specifying any virtual device changes for disks and networks. All other VM configuration changes should use ReconfigVM_Task API after the clone operation finishes.

And CloneSpec -> location -> deviceChange

The supported device changes are:

  • For VirtualEthernetCard, it has to be used in device to specify the target network backing.
  • All other specification are ignored

0 Kudos
1 Reply
jasnyder
Hot Shot
Hot Shot

Yes, you are able to reassign the network to which the VM is connected at the same time as a clone operation.

What you have will work for a standard port group but not for a distributed virtual port group.

This will work for a dvPortGroup:

my $backing = VirtualEthernetCardDistributedVirtualPortBackingInfo->new (

        port => DistributedVirtualSwitchPortConnection->new(portgroupKey => 'dvportgroup-54',

                                    switchUuid => 'aa c1 1e 50 d2 51 9b 19-5d 53 f3 68 02 29 f0 57'

                                )

        );

my $configSpec = VirtualDeviceConfigSpec->new(

          device => VirtualEthernetCard->new(

               backing => $backing,

               key => 4000,        

          ),

          operation => VirtualDeviceConfigSpecOperation->new('edit')

     );

Following what you did, this will either create a standard port group if the one named does not exist or connect it to an existing one if it does:

my $backing = VirtualEthernetCardNetworkBackingInfo->new(

     deviceName => 'std-VMPublic',

     useAutoDetect => 0

);

my $configSpec = VirtualDeviceConfigSpec->new(

          device => VirtualEthernetCard->new(

               backing => $backing,

               key => 4000

          ),

          operation => VirtualDeviceConfigSpecOperation->new('edit')

     );

I also omitted a lot of the code, but this should be enough to match to your example.  All I did different was instead of using a VirtualEthernetCardNetworkBackingInfo object for the backing, I used a VirtualEthernetCardDistributedVirtualPortBackingInfo object.  I hardcoded MOID and UUID for example's sake.

0 Kudos