mirror of
https://github.com/vmware/vsphere-automation-sdk-python.git
synced 2024-11-22 09:39:58 -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.'
|
__author__ = 'VMware, Inc.'
|
||||||
__vcenter_version__ = 'VMware Cloud on AWS'
|
__vcenter_version__ = 'VMware Cloud on AWS'
|
||||||
|
|
||||||
|
from pprint import pprint as pp
|
||||||
|
|
||||||
from com.vmware.content.library_client import Item
|
from com.vmware.content.library_client import Item
|
||||||
from com.vmware.vcenter.ovf_client import LibraryItem
|
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 vmware.vapi.vsphere.client import create_vsphere_client
|
||||||
|
|
||||||
from samples.vsphere.common import sample_cli, sample_util
|
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. '
|
help='(Optional) The name of the folder to be used. '
|
||||||
'Default: "Workloads"')
|
'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())
|
args = sample_util.process_cli_args(parser.parse_args())
|
||||||
|
|
||||||
self.vm_id = None
|
self.vm_id = None
|
||||||
@ -65,6 +72,7 @@ class DeployOvfTemplate:
|
|||||||
self.vm_name = args.vmname
|
self.vm_name = args.vmname
|
||||||
self.resourcepoolname = args.resourcepoolname
|
self.resourcepoolname = args.resourcepoolname
|
||||||
self.foldername = args.foldername
|
self.foldername = args.foldername
|
||||||
|
self.opaquenetworkname = args.opaquenetworkname
|
||||||
self.cleardata = args.cleardata
|
self.cleardata = args.cleardata
|
||||||
|
|
||||||
# Connect to vAPI Endpoint on vCenter Server
|
# Connect to vAPI Endpoint on vCenter Server
|
||||||
@ -145,6 +153,32 @@ class DeployOvfTemplate:
|
|||||||
for error in result.error.errors:
|
for error in result.error.errors:
|
||||||
print('OVF error: {}'.format(error.message))
|
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):
|
def delete_vm(self):
|
||||||
if self.cleardata:
|
if self.cleardata:
|
||||||
self.client.vcenter.VM.delete(self.vm_id)
|
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
|
from samples.vsphere.vcenter.helper import datacenter_helper
|
||||||
|
|
||||||
|
|
||||||
def get_standard_network_backing(client,
|
def get_network_backing(client,
|
||||||
std_porggroup_name,
|
porggroup_name,
|
||||||
datacenter_name):
|
datacenter_name,
|
||||||
|
portgroup_type):
|
||||||
"""
|
"""
|
||||||
Gets a standard portgroup network backing for a given Datacenter
|
Gets a standard portgroup network backing for a given Datacenter
|
||||||
Note: The method assumes that there is only one standard portgroup
|
Note: The method assumes that there is only one standard portgroup
|
||||||
@ -33,45 +34,16 @@ def get_standard_network_backing(client,
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
filter = Network.FilterSpec(datacenters=set([datacenter]),
|
filter = Network.FilterSpec(datacenters=set([datacenter]),
|
||||||
names=set([std_porggroup_name]),
|
names=set([porggroup_name]),
|
||||||
types=set([Network.Type.STANDARD_PORTGROUP]))
|
types=set([portgroup_type]))
|
||||||
network_summaries = client.vcenter.Network.list(filter=filter)
|
network_summaries = client.vcenter.Network.list(filter=filter)
|
||||||
|
|
||||||
if len(network_summaries) > 0:
|
if len(network_summaries) > 0:
|
||||||
network = network_summaries[0].network
|
network = network_summaries[0].network
|
||||||
print("Selecting Standard Portgroup Network '{}' ({})".
|
print("Selecting {} Portgroup Network '{}' ({})".
|
||||||
format(std_porggroup_name, network))
|
format(portgroup_type, porggroup_name, network))
|
||||||
return network
|
return network
|
||||||
else:
|
else:
|
||||||
print("Standard Portgroup Network not found in Datacenter '{}'".
|
print("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 '{}'".
|
|
||||||
format(datacenter_name))
|
format(datacenter_name))
|
||||||
return None
|
return None
|
||||||
|
@ -42,8 +42,8 @@ config["CLUSTER1_NAME"] = "Cluster1"
|
|||||||
|
|
||||||
config["VDSWITCH1_NAME"] = "DSwitch1"
|
config["VDSWITCH1_NAME"] = "DSwitch1"
|
||||||
config["VDPORTGROUP1_NAME"] = "Static_Portgroup_on_DSwitch_1"
|
config["VDPORTGROUP1_NAME"] = "Static_Portgroup_on_DSwitch_1"
|
||||||
|
|
||||||
config["STDPORTGROUP_NAME"] = "VM Network"
|
config["STDPORTGROUP_NAME"] = "VM Network"
|
||||||
|
config["OPAQUEPORTGROUP1_NAME"] = ""
|
||||||
|
|
||||||
# The main datacenter and datastore that will be used for the VM tests
|
# The main datacenter and datastore that will be used for the VM tests
|
||||||
config["VM_DATACENTER_NAME"] = config["DATACENTER2_NAME"]
|
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 vmware.vapi.vsphere.client import create_vsphere_client
|
||||||
|
|
||||||
from com.vmware.vcenter.vm.hardware_client import Ethernet
|
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 parse_cli_args_vm
|
||||||
from samples.vsphere.common.sample_util import pp
|
from samples.vsphere.common.sample_util import pp
|
||||||
from samples.vsphere.vcenter.helper import network_helper
|
from samples.vsphere.vcenter.helper import network_helper
|
||||||
@ -69,16 +71,27 @@ def run():
|
|||||||
print("Using VM '{}' ({}) for Disk Sample".format(vm_name, vm))
|
print("Using VM '{}' ({}) for Disk Sample".format(vm_name, vm))
|
||||||
|
|
||||||
# Get standard portgroup to use as backing for sample
|
# 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,
|
client,
|
||||||
testbed.config['STDPORTGROUP_NAME'],
|
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
|
# 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,
|
client,
|
||||||
testbed.config['VDPORTGROUP1_NAME'],
|
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')
|
print('\n# Example: List all Ethernet adapters for a VM')
|
||||||
nic_summaries = client.vcenter.vm.hardware.Ethernet.list(vm=vm)
|
nic_summaries = client.vcenter.vm.hardware.Ethernet.list(vm=vm)
|
||||||
@ -168,6 +181,21 @@ def run():
|
|||||||
print('vm.hardware.Ethernet.get({}, {}) -> {}'.
|
print('vm.hardware.Ethernet.get({}, {}) -> {}'.
|
||||||
format(vm, nic, pp(nic_info)))
|
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
|
# Change the last nic that was created
|
||||||
print('\n# Example: Update Ethernet Nic with different backing')
|
print('\n# Example: Update Ethernet Nic with different backing')
|
||||||
nic_update_spec = Ethernet.UpdateSpec(
|
nic_update_spec = Ethernet.UpdateSpec(
|
||||||
|
Loading…
Reference in New Issue
Block a user