Java Example of Configuring the Virtual Machine Network

This example is based on the code in the
EthernetConfiguration.java
sample file.
For a complete and up-to-date version of the sample code, see the vSphere Automation SDK Java samples at GitHub.
... private String vmName; private String datacenterName; private String stdPortgroupName; private String distPortgroupName; private String vmId; private List<String> createdNics = new ArrayList<String>(); private Power powerService; private Ethernet ethernetService; ... this.ethernetService = vapiAuthHelper.getStubFactory().createStub( Ethernet.class, this.sessionStubConfig); // Get the virtual machine ID this.vmId = VmHelper.getVM(vapiAuthHelper.getStubFactory(), sessionStubConfig, vmName); // List all Ethernet adapters of the virtual machine List<EthernetTypes.Summary> nicSummaries = this.ethernetService.list( this.vmId); System.out.println("\n\n#### List of all Ethernet NICS on the VM:\n" + nicSummaries); // Get info for each Ethernet adapter on the VM System.out.println("\n\n####Print info for each Ethernet NIC on the" + " vm."); for (EthernetTypes.Summary ethSummary : nicSummaries) { EthernetTypes.Info ethInfo = this.ethernetService.get(vmId, ethSummary.getNic()); System.out.println(ethInfo); } // Create Ethernet NIC by using STANDARD_PORTGROUP with default settings String stdNetworkId = NetworkHelper.getStandardNetworkBacking( this.vapiAuthHelper.getStubFactory(), sessionStubConfig, this.datacenterName, this.stdPortgroupName); EthernetTypes.CreateSpec nicCreateSpec = new EthernetTypes.CreateSpec.Builder().setBacking( new EthernetTypes.BackingSpec.Builder( EthernetTypes.BackingType.STANDARD_PORTGROUP) .setNetwork(stdNetworkId).build()).build(); String nicId = this.ethernetService.create(this.vmId, nicCreateSpec); this.createdNics.add(nicId); EthernetTypes.Info nicInfo = this.ethernetService.get(this.vmId, nicId); // Update the Ethernet NIC with a different backing EthernetTypes.UpdateSpec nicUpdateSpec = new EthernetTypes.UpdateSpec.Builder().setBacking( new EthernetTypes.BackingSpec.Builder(EthernetTypes.BackingType.STANDARD_PORTGROUP) .setNetwork(stdNetworkId).build()).build(); this.ethernetService.update(this.vmId, lastNicId, nicUpdateSpec); nicInfo = this.ethernetService.get(this.vmId, lastNicId); // Update the Ethernet NIC configuration nicUpdateSpec = new EthernetTypes.UpdateSpec.Builder() .setAllowGuestControl(false) .setStartConnected(false) .setWakeOnLanEnabled(false) .build(); this.ethernetService.update(this.vmId, lastNicId, nicUpdateSpec); nicInfo = this.ethernetService.get(this.vmId, lastNicId); // Powering on the VM to connect the virtual Ethernet adapter to its backing this.powerService.start(this.vmId); nicInfo = this.ethernetService.get(this.vmId, lastNicId); // Connect Ethernet NIC after powering on the VM this.ethernetService.connect(this.vmId, lastNicId); nicInfo = this.ethernetService.get(this.vmId, lastNicId); // Disconnect Ethernet NIC after powering on VM this.ethernetService.disconnect(this.vmId, lastNicId); nicInfo = this.ethernetService.get(this.vmId, lastNicId); ...