mirror of
https://github.com/vmware/vsphere-automation-sdk-python.git
synced 2024-11-25 10:50:00 -05:00
Merge pull request #127 from tianhao64/master
Add opaque network to deployed ovf
This commit is contained in:
commit
e4ecfea184
@ -16,9 +16,12 @@
|
||||
__author__ = 'VMware, Inc.'
|
||||
__vcenter_version__ = 'VMware Cloud on AWS'
|
||||
|
||||
from pprint import pprint as pp
|
||||
|
||||
from com.vmware.content.library_client import Item
|
||||
from com.vmware.vcenter.ovf_client import LibraryItem
|
||||
from com.vmware.vcenter_client import ResourcePool, Folder
|
||||
from com.vmware.vcenter_client import ResourcePool, Folder, Network
|
||||
from com.vmware.vcenter.vm.hardware_client import Ethernet
|
||||
from vmware.vapi.vsphere.client import create_vsphere_client
|
||||
|
||||
from samples.vsphere.common import sample_cli, sample_util
|
||||
@ -58,6 +61,10 @@ class DeployOvfTemplate:
|
||||
help='(Optional) The name of the folder to be used. '
|
||||
'Default: "Workloads"')
|
||||
|
||||
parser.add_argument('--opaquenetworkname',
|
||||
help='(Optional) The name of the opaque network to be added '
|
||||
'to the deployed vm')
|
||||
|
||||
args = sample_util.process_cli_args(parser.parse_args())
|
||||
|
||||
self.vm_id = None
|
||||
@ -65,6 +72,7 @@ class DeployOvfTemplate:
|
||||
self.vm_name = args.vmname
|
||||
self.resourcepoolname = args.resourcepoolname
|
||||
self.foldername = args.foldername
|
||||
self.opaquenetworkname = args.opaquenetworkname
|
||||
self.cleardata = args.cleardata
|
||||
|
||||
# Connect to vAPI Endpoint on vCenter Server
|
||||
@ -145,6 +153,32 @@ class DeployOvfTemplate:
|
||||
for error in result.error.errors:
|
||||
print('OVF error: {}'.format(error.message))
|
||||
|
||||
# Add an opaque network portgroup to the deployed VM
|
||||
if self.opaquenetworkname:
|
||||
filter = Network.FilterSpec(
|
||||
names=set([self.opaquenetworkname]),
|
||||
types=set([Network.Type.OPAQUE_NETWORK]))
|
||||
network_summaries = self.client.vcenter.Network.list(filter=filter)
|
||||
if not network_summaries:
|
||||
raise ValueError("Opaque network {} can not find".format(
|
||||
self.opaquenetworkname))
|
||||
network = network_summaries[0].network
|
||||
|
||||
nic_create_spec = Ethernet.CreateSpec(
|
||||
start_connected=True,
|
||||
mac_type=Ethernet.MacAddressType.GENERATED,
|
||||
backing=Ethernet.BackingSpec(
|
||||
type=Ethernet.BackingType.OPAQUE_NETWORK,
|
||||
network=network))
|
||||
print('vm.hardware.Ethernet.create({}, {}) -> {}'.format(
|
||||
self.vm_id, nic_create_spec, network))
|
||||
nic = self.client.vcenter.vm.hardware.Ethernet.create(
|
||||
self.vm_id, nic_create_spec)
|
||||
|
||||
nic_info = self.client.vcenter.vm.hardware.Ethernet.get(self.vm_id, nic)
|
||||
print('vm.hardware.Ethernet.get({}, {}) -> {}'.format(
|
||||
self.vm_id, nic, pp(nic_info)))
|
||||
|
||||
def delete_vm(self):
|
||||
if self.cleardata:
|
||||
self.client.vcenter.VM.delete(self.vm_id)
|
||||
|
@ -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
|
||||
|
@ -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"]
|
||||
|
@ -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(
|
||||
|
Loading…
Reference in New Issue
Block a user