From 912fb0bcbfded03eb5a2f071c2f45aded058b671 Mon Sep 17 00:00:00 2001 From: Tianhao He Date: Thu, 31 Jan 2019 16:47:42 +0800 Subject: [PATCH] update ethernet sample and add opaque network Signed-off-by: Tianhao He --- .../vsphere/vcenter/helper/network_helper.py | 46 ++++--------------- samples/vsphere/vcenter/setup/testbed.py | 2 +- .../vsphere/vcenter/vm/hardware/ethernet.py | 36 +++++++++++++-- 3 files changed, 42 insertions(+), 42 deletions(-) diff --git a/samples/vsphere/vcenter/helper/network_helper.py b/samples/vsphere/vcenter/helper/network_helper.py index 90357a34..c3a6c605 100644 --- a/samples/vsphere/vcenter/helper/network_helper.py +++ b/samples/vsphere/vcenter/helper/network_helper.py @@ -19,9 +19,10 @@ from com.vmware.vcenter_client import Network from samples.vsphere.vcenter.helper import datacenter_helper -def get_standard_network_backing(client, - std_porggroup_name, - datacenter_name): +def get_network_backing(client, + porggroup_name, + datacenter_name, + portgroup_type): """ Gets a standard portgroup network backing for a given Datacenter Note: The method assumes that there is only one standard portgroup @@ -33,45 +34,16 @@ def get_standard_network_backing(client, return None filter = Network.FilterSpec(datacenters=set([datacenter]), - names=set([std_porggroup_name]), - types=set([Network.Type.STANDARD_PORTGROUP])) + names=set([porggroup_name]), + types=set([portgroup_type])) network_summaries = client.vcenter.Network.list(filter=filter) if len(network_summaries) > 0: network = network_summaries[0].network - print("Selecting Standard Portgroup Network '{}' ({})". - format(std_porggroup_name, network)) + print("Selecting {} Portgroup Network '{}' ({})". + format(portgroup_type, porggroup_name, network)) return network else: - print("Standard Portgroup Network not found in Datacenter '{}'". - format(datacenter_name)) - return None - - -def get_distributed_network_backing(client, - dv_portgroup_name, - datacenter_name): - """ - Gets a distributed portgroup network backing for a given Datacenter - Note: The method assumes that there is only one distributed portgroup - and datacenter with the mentioned names. - """ - datacenter = datacenter_helper.get_datacenter(client, datacenter_name) - if not datacenter: - print("Datacenter '{}' not found".format(datacenter_name)) - return None - - filter = Network.FilterSpec(datacenters=set([datacenter]), - names=set([dv_portgroup_name]), - types=set([Network.Type.DISTRIBUTED_PORTGROUP])) - network_summaries = client.vcenter.Network.list(filter=filter) - - if len(network_summaries) > 0: - network = network_summaries[0].network - print("Selecting Distributed Portgroup Network '{}' ({})". - format(dv_portgroup_name, network)) - return network - else: - print("Distributed Portgroup Network not found in Datacenter '{}'". + print("Portgroup Network not found in Datacenter '{}'". format(datacenter_name)) return None diff --git a/samples/vsphere/vcenter/setup/testbed.py b/samples/vsphere/vcenter/setup/testbed.py index 81a2dc7e..3318d6e5 100644 --- a/samples/vsphere/vcenter/setup/testbed.py +++ b/samples/vsphere/vcenter/setup/testbed.py @@ -42,8 +42,8 @@ config["CLUSTER1_NAME"] = "Cluster1" config["VDSWITCH1_NAME"] = "DSwitch1" config["VDPORTGROUP1_NAME"] = "Static_Portgroup_on_DSwitch_1" - config["STDPORTGROUP_NAME"] = "VM Network" +config["OPAQUEPORTGROUP1_NAME"] = "" # The main datacenter and datastore that will be used for the VM tests config["VM_DATACENTER_NAME"] = config["DATACENTER2_NAME"] diff --git a/samples/vsphere/vcenter/vm/hardware/ethernet.py b/samples/vsphere/vcenter/vm/hardware/ethernet.py index 89260471..2906f978 100644 --- a/samples/vsphere/vcenter/vm/hardware/ethernet.py +++ b/samples/vsphere/vcenter/vm/hardware/ethernet.py @@ -19,6 +19,8 @@ __vcenter_version__ = '6.5+' from vmware.vapi.vsphere.client import create_vsphere_client from com.vmware.vcenter.vm.hardware_client import Ethernet +from com.vmware.vcenter_client import Network + from samples.vsphere.common.sample_util import parse_cli_args_vm from samples.vsphere.common.sample_util import pp from samples.vsphere.vcenter.helper import network_helper @@ -69,16 +71,27 @@ def run(): print("Using VM '{}' ({}) for Disk Sample".format(vm_name, vm)) # Get standard portgroup to use as backing for sample - standard_network = network_helper.get_standard_network_backing( + standard_network = network_helper.get_network_backing( client, testbed.config['STDPORTGROUP_NAME'], - testbed.config['VM_DATACENTER_NAME']) + testbed.config['VM_DATACENTER_NAME'], + Network.Type.STANDARD_PORTGROUP) # Get distributed portgroup to use as backing for sample - distributed_network = network_helper.get_distributed_network_backing( + distributed_network = network_helper.get_network_backing( client, testbed.config['VDPORTGROUP1_NAME'], - testbed.config['VM_DATACENTER_NAME']) + testbed.config['VM_DATACENTER_NAME'], + Network.Type.DISTRIBUTED_PORTGROUP) + + # Get opaque portgroup to use as backing for sample + opaque_network = None + if testbed.config['OPAQUEPORTGROUP1_NAME']: + opaque_network = network_helper.get_network_backing( + client, + testbed.config['OPAQUEPORTGROUP1_NAME'], + testbed.config['VM_DATACENTER_NAME'], + Network.Type.OPAQUE_NETWORK) print('\n# Example: List all Ethernet adapters for a VM') nic_summaries = client.vcenter.vm.hardware.Ethernet.list(vm=vm) @@ -168,6 +181,21 @@ def run(): print('vm.hardware.Ethernet.get({}, {}) -> {}'. format(vm, nic, pp(nic_info))) + if opaque_network: + print('\n# Example: Create Ethernet Nic using OPAQUE PORTGROUP with ' + 'default settings') + nic_create_spec = Ethernet.CreateSpec( + backing=Ethernet.BackingSpec( + type=Ethernet.BackingType.OPAQUE_NETWORK, + network=opaque_network)) + nic = client.vcenter.vm.hardware.Ethernet.create(vm, nic_create_spec) + print('vm.hardware.Ethernet.create({}, {}) -> {}'. + format(vm, nic_create_spec, nic)) + nics_to_delete.append(nic) + nic_info = client.vcenter.vm.hardware.Ethernet.get(vm, nic) + print('vm.hardware.Ethernet.get({}, {}) -> {}'. + format(vm, nic, pp(nic_info))) + # Change the last nic that was created print('\n# Example: Update Ethernet Nic with different backing') nic_update_spec = Ethernet.UpdateSpec(