def create_basic_vm(stub_config, placement_spec, standard_network): """ Create a basic VM. Using the provided PlacementSpec, create a VM with a selected Guest OS and provided name. Create a VM with the following configuration: * Create 2 disks and specify one of them on scsi0:0 since it's the boot disk * Specify 1 ethernet adapter using a Standard Portgroup backing * Setup for PXE install by selecting network as first boot device Use guest and system provided defaults for most configuration settings. """ guest_os = testbed.config['VM_GUESTOS'] boot_disk = Disk.CreateSpec(type=Disk.HostBusAdapterType.SCSI, scsi=ScsiAddressSpec(bus=0, unit=0), new_vmdk=Disk.VmdkCreateSpec()) data_disk = Disk.CreateSpec(new_vmdk=Disk.VmdkCreateSpec()) nic = Ethernet.CreateSpec(start_connected=True, backing=Ethernet.BackingSpec( type=Ethernet.BackingType.STANDARD_PORTGROUP, network=standard_network)) # TODO Should DISK be put before ETHERNET? Does the BIOS automatically try # the next device if the DISK is empty? boot_device_order = [ BootDevice.EntryCreateSpec(BootDevice.Type.ETHERNET), BootDevice.EntryCreateSpec(BootDevice.Type.DISK) ] vm_create_spec = VM.CreateSpec(name=vm_name, guest_os=guest_os, placement=placement_spec, disks=[boot_disk, data_disk], nics=[nic], boot_devices=boot_device_order) print('\n# Example: create_basic_vm: Creating a VM using spec\n-----') print(pp(vm_create_spec)) print('-----') vm_svc = VM(stub_config) vm = vm_svc.create(vm_create_spec) print("create_basic_vm: Created VM '{}' ({})".format(vm_name, vm)) vm_info = vm_svc.get(vm) print('vm.get({}) -> {}'.format(vm, pp(vm_info))) return vm
def run(): global vm vm = get_vm(stub_config, vm_name) if not vm: raise Exception('Sample requires an existing vm with name ({}). ' 'Please create the vm first.'.format(vm_name)) print("Using VM '{}' ({}) for BootDevice Sample".format(vm_name, vm)) # Create BootDevice stub used for making requests global boot_device_svc boot_device_svc = BootDevice(stub_config) print('\n# Example: Get current BootDevice configuration') boot_device_entries = boot_device_svc.get(vm) print('vm.hardware.boot.Device.get({}) -> {}'.format( vm, pp(boot_device_entries))) # Save current BootDevice info to verify that we have cleaned up properly global orig_boot_device_entries orig_boot_device_entries = boot_device_entries # Get device identifiers for Disks disk_svc = Disk(stub_config) disk_summaries = disk_svc.list(vm) print('vm.hardware.Disk.list({}) -> {}'.format(vm, pp(disk_summaries))) disks = [disk_summary.disk for disk_summary in disk_summaries] # Get device identifiers for Ethernet nics ethernet_svc = Ethernet(stub_config) nic_summaries = ethernet_svc.list(vm) print('vm.hardware.Ethernet.list({}) -> {}'.format(vm, pp(nic_summaries))) nics = [nic_summary.nic for nic_summary in nic_summaries] print('\n# Example: Set Boot Order to be Floppy, ' 'Disk1, Disk2, Disk3, Cdrom,') print('# Network (nic0), Network (nic1).') boot_device_entries = [ BootDevice.Entry(BootDevice.Type.FLOPPY), BootDevice.Entry(BootDevice.Type.DISK, disks=disks), BootDevice.Entry(BootDevice.Type.CDROM) ] for nic in nics: boot_device_entries.append( BootDevice.Entry(BootDevice.Type.ETHERNET, nic=nic)) print('vm.hardware.boot.Device.set({}, {})'.format(vm, boot_device_entries)) boot_device_svc.set(vm, boot_device_entries) boot_device_entries = boot_device_svc.get(vm) print('vm.hardware.boot.Device.get({}) -> {}'.format( vm, pp(boot_device_entries)))
def update_network(self): nic = '4000' vm = get_vm(self.client, vm_name=self.vm_name) network_info = self.client.vcenter.vm.hardware.Ethernet.get(vm=vm, nic=nic) dv_portgroup_name = self.network distributed_network = network_helper.get_network_backing( self.client, dv_portgroup_name, self.datacenter_name, Network.Type.DISTRIBUTED_PORTGROUP) print(distributed_network) nics = Ethernet.UpdateSpec( start_connected=True, mac_type='MANUAL', mac_address=network_info.mac_address, backing=Ethernet.BackingSpec( type=Ethernet.BackingType.DISTRIBUTED_PORTGROUP, network=distributed_network)) self.client.vcenter.vm.hardware.Ethernet.update(vm=vm, nic=nic, spec=nics)
def run(self): # Get a placement spec datacenter_name = testbed.config['VM_DATACENTER_NAME'] vm_folder_name = testbed.config['VM_FOLDER2_NAME'] datastore_name = testbed.config['VM_DATASTORE_NAME'] std_portgroup_name = testbed.config['STDPORTGROUP_NAME'] dv_portgroup_name = testbed.config['VDPORTGROUP1_NAME'] if not self.placement_spec: self.placement_spec = vm_placement_helper.get_placement_spec_for_resource_pool( self.client, datacenter_name, vm_folder_name, datastore_name) # Get a standard network backing if not self.standard_network: self.standard_network = network_helper.get_network_backing( self.client, std_portgroup_name, datacenter_name, Network.Type.STANDARD_PORTGROUP) # Get a distributed network backing if not self.distributed_network: self.distributed_network = network_helper.get_network_backing( self.client, dv_portgroup_name, datacenter_name, Network.Type.DISTRIBUTED_PORTGROUP) """ Create an exhaustive VM. Using the provided PlacementSpec, create a VM with a selected Guest OS and provided name. Create a VM with the following configuration: * Hardware Version = VMX_11 (for 6.0) * CPU (count = 2, coresPerSocket = 2, hotAddEnabled = false, hotRemoveEnabled = false) * Memory (size_mib = 2 GB, hotAddEnabled = false) * 3 Disks and specify each of the HBAs and the unit numbers * (capacity=40 GB, name=<some value>, spaceEfficient=true) * Specify 2 ethernet adapters, one using a Standard Portgroup backing and the other using a DISTRIBUTED_PORTGROUP networking backing. * nic1: Specify Ethernet (macType=MANUAL, macAddress=<some value>) * nic2: Specify Ethernet (macType=GENERATED) * 1 CDROM (type=ISO_FILE, file="os.iso", startConnected=true) * 1 Serial Port (type=NETWORK_SERVER, file="tcp://localhost/16000", startConnected=true) * 1 Parallel Port (type=HOST_DEVICE, startConnected=false) * 1 Floppy Drive (type=CLIENT_DEVICE) * Boot, type=BIOS * BootDevice order: CDROM, DISK, ETHERNET Use guest and system provided defaults for remaining configuration settings. """ guest_os = testbed.config['VM_GUESTOS'] iso_datastore_path = testbed.config['ISO_DATASTORE_PATH'] serial_port_network_location = \ testbed.config['SERIAL_PORT_NETWORK_SERVER_LOCATION'] GiB = 1024 * 1024 * 1024 GiBMemory = 1024 vm_create_spec = VM.CreateSpec( guest_os=guest_os, name=self.vm_name, placement=self.placement_spec, hardware_version=Hardware.Version.VMX_11, cpu=Cpu.UpdateSpec(count=2, cores_per_socket=1, hot_add_enabled=False, hot_remove_enabled=False), memory=Memory.UpdateSpec(size_mib=2 * GiBMemory, hot_add_enabled=False), disks=[ Disk.CreateSpec(type=Disk.HostBusAdapterType.SCSI, scsi=ScsiAddressSpec(bus=0, unit=0), new_vmdk=Disk.VmdkCreateSpec(name='boot', capacity=40 * GiB)), Disk.CreateSpec(new_vmdk=Disk.VmdkCreateSpec( name='data1', capacity=10 * GiB)), Disk.CreateSpec(new_vmdk=Disk.VmdkCreateSpec( name='data2', capacity=10 * GiB)) ], nics=[ Ethernet.CreateSpec( start_connected=True, mac_type=Ethernet.MacAddressType.MANUAL, mac_address='11:23:58:13:21:34', backing=Ethernet.BackingSpec( type=Ethernet.BackingType.STANDARD_PORTGROUP, network=self.standard_network)), Ethernet.CreateSpec( start_connected=True, mac_type=Ethernet.MacAddressType.GENERATED, backing=Ethernet.BackingSpec( type=Ethernet.BackingType.DISTRIBUTED_PORTGROUP, network=self.distributed_network)), ], cdroms=[ Cdrom.CreateSpec(start_connected=True, backing=Cdrom.BackingSpec( type=Cdrom.BackingType.ISO_FILE, iso_file=iso_datastore_path)) ], serial_ports=[ Serial.CreateSpec( start_connected=False, backing=Serial.BackingSpec( type=Serial.BackingType.NETWORK_SERVER, network_location=serial_port_network_location)) ], parallel_ports=[ Parallel.CreateSpec(start_connected=False, backing=Parallel.BackingSpec( type=Parallel.BackingType.HOST_DEVICE)) ], floppies=[ Floppy.CreateSpec(backing=Floppy.BackingSpec( type=Floppy.BackingType.CLIENT_DEVICE)) ], boot=Boot.CreateSpec(type=Boot.Type.BIOS, delay=0, enter_setup_mode=False), # TODO Should DISK be put before CDROM and ETHERNET? Does the BIOS # automatically try the next device if the DISK is empty? boot_devices=[ BootDevice.EntryCreateSpec(BootDevice.Type.CDROM), BootDevice.EntryCreateSpec(BootDevice.Type.DISK), BootDevice.EntryCreateSpec(BootDevice.Type.ETHERNET) ]) print( '# Example: create_exhaustive_vm: Creating a VM using spec\n-----') print(pp(vm_create_spec)) print('-----') vm = self.client.vcenter.VM.create(vm_create_spec) print("create_exhaustive_vm: Created VM '{}' ({})".format( self.vm_name, vm)) vm_info = self.client.vcenter.VM.get(vm) print('vm.get({}) -> {}'.format(vm, pp(vm_info))) return vm
def deploy_ovf_template(self): # Build the deployment target with resource pool ID and folder ID rp_filter_spec = ResourcePool.FilterSpec(names=set([self.resourcepoolname])) resource_pool_summaries = self.client.vcenter.ResourcePool.list(rp_filter_spec) if not resource_pool_summaries: raise ValueError("Resource pool with name '{}' not found". format(self.resourcepoolname)) resource_pool_id = resource_pool_summaries[0].resource_pool print('Resource pool ID: {}'.format(resource_pool_id)) folder_filter_spec = Folder.FilterSpec(names=set([self.foldername])) folder_summaries = self.client.vcenter.Folder.list(folder_filter_spec) if not folder_summaries: raise ValueError("Folder with name '{}' not found". format(self.foldername)) folder_id = folder_summaries[0].folder print('Folder ID: {}'.format(folder_id)) deployment_target = LibraryItem.DeploymentTarget( resource_pool_id=resource_pool_id, folder_id=folder_id ) # Find the library item find_spec = Item.FindSpec(name=self.lib_item_name) lib_item_ids = self.client.content.library.Item.find(find_spec) if not lib_item_ids: raise ValueError("Library item with name '{}' not found". format(self.lib_item_name)) lib_item_id = lib_item_ids[0] print('Library item ID: {}'.format(lib_item_id)) ovf_summary = self.client.vcenter.ovf.LibraryItem.filter( ovf_library_item_id=lib_item_id, target=deployment_target) print('Found an OVF template: {} to deploy.'.format(ovf_summary.name)) # Build the deployment spec deployment_spec = LibraryItem.ResourcePoolDeploymentSpec( name=self.vm_name, annotation=ovf_summary.annotation, accept_all_eula=True, network_mappings=None, storage_mappings=None, storage_provisioning=None, storage_profile_id=None, locale=None, flags=None, additional_parameters=None, default_datastore_id=None) # Deploy the ovf template result = self.client.vcenter.ovf.LibraryItem.deploy( lib_item_id, deployment_target, deployment_spec, client_token=generate_random_uuid()) # The type and ID of the target deployment is available in the deployment result. if result.succeeded: print('Deployment successful. VM Name: "{}", ID: "{}"' .format(self.vm_name, result.resource_id.id)) self.vm_id = result.resource_id.id error = result.error if error is not None: for warning in error.warnings: print('OVF warning: {}'.format(warning.message)) else: print('Deployment failed.') 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)))
#[Kingston120G] SHARED/RAC_SSD_1.vm vm_create_spec = VM.CreateSpec( guest_os='CENTOS_7_64', name='TESTVM', placement=placement_spec, hardware_version=Hardware.Version.VMX_13, cpu=Cpu.UpdateSpec(count=2, cores_per_socket=1, hot_add_enabled=False, hot_remove_enabled=False), memory=Memory.UpdateSpec(size_mib=2 * GiBMemory, hot_add_enabled=False), boot=Boot.CreateSpec(type=Boot.Type.BIOS, delay=0, enter_setup_mode=False), boot_devices=[ BootDevice.EntryCreateSpec(BootDevice.Type.DISK), BootDevice.EntryCreateSpec(BootDevice.Type.ETHERNET) ], nics=[ Ethernet.CreateSpec( start_connected=True, mac_type=Ethernet.MacAddressType.MANUAL, mac_address='11:23:58:13:21:34', pci_slot_number=192, backing=Ethernet.BackingSpec(type=Ethernet.BackingType.STANDARD_PORTGROUP, network=network1.network) ) ], scsi_adapters=[scsi_create_spec0, scsi_create_spec1], disks=[disk_create_spec0, disk_create_spec1] ) vm = client.vcenter.VM.create(vm_create_spec) print("create_exhaustive_vm: Created VM '{}' ({})".format(vm_create_spec.name, vm))
def run(self): # Get a placement spec datacenter_name = testbed.config['VM_DATACENTER_NAME'] vm_folder_name = testbed.config['VM_FOLDER2_NAME'] datastore_name = testbed.config['VM_DATASTORE_NAME'] std_portgroup_name = testbed.config['STDPORTGROUP_NAME'] if not self.placement_spec: self.placement_spec = vm_placement_helper.get_placement_spec_for_resource_pool( self.stub_config, datacenter_name, vm_folder_name, datastore_name) # Get a standard network backing standard_network = network_helper.get_standard_network_backing( self.stub_config, std_portgroup_name, datacenter_name) """ Create a basic VM. Using the provided PlacementSpec, create a VM with a selected Guest OS and provided name. Create a VM with the following configuration: * Create 2 disks and specify one of them on scsi0:0 since it's the boot disk * Specify 1 ethernet adapter using a Standard Portgroup backing * Setup for PXE install by selecting network as first boot device Use guest and system provided defaults for most configuration settings. """ guest_os = testbed.config['VM_GUESTOS'] boot_disk = Disk.CreateSpec(type=Disk.HostBusAdapterType.SCSI, scsi=ScsiAddressSpec(bus=0, unit=0), new_vmdk=Disk.VmdkCreateSpec()) data_disk = Disk.CreateSpec(new_vmdk=Disk.VmdkCreateSpec()) nic = Ethernet.CreateSpec( start_connected=True, backing=Ethernet.BackingSpec( type=Ethernet.BackingType.STANDARD_PORTGROUP, network=standard_network)) boot_device_order = [ BootDevice.EntryCreateSpec(BootDevice.Type.ETHERNET), BootDevice.EntryCreateSpec(BootDevice.Type.DISK) ] vm_create_spec = VM.CreateSpec(name=self.vm_name, guest_os=guest_os, placement=self.placement_spec, disks=[boot_disk, data_disk], nics=[nic], boot_devices=boot_device_order) print('\n# Example: create_basic_vm: Creating a VM using spec\n-----') print(pp(vm_create_spec)) print('-----') vm_svc = VM(self.stub_config) vm = vm_svc.create(vm_create_spec) print("create_basic_vm: Created VM '{}' ({})".format(self.vm_name, vm)) vm_info = vm_svc.get(vm) print('vm.get({}) -> {}'.format(vm, pp(vm_info))) return vm
def run(): global vm vm = get_vm(client, vm_name) if not vm: raise Exception('Sample requires an existing vm with name ({}). ' 'Please create the vm first.'.format(vm_name)) print("Using VM '{}' ({}) for Disk Sample".format(vm_name, vm)) # Get standard portgroup to use as backing for sample standard_network = network_helper.get_network_backing( client, testbed.config['STDPORTGROUP_NAME'], testbed.config['VM_DATACENTER_NAME'], Network.Type.STANDARD_PORTGROUP) # Get distributed portgroup to use as backing for sample distributed_network = network_helper.get_network_backing( client, testbed.config['VDPORTGROUP1_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) print('vm.hardware.Ethernet.list({}) -> {}'.format(vm, nic_summaries)) # Save current list of Ethernet adapters to verify that we have cleaned # up properly global orig_nic_summaries orig_nic_summaries = nic_summaries # Get information for each Ethernet on the VM for nic_summary in nic_summaries: nic = nic_summary.nic nic_info = client.vcenter.vm.hardware.Ethernet.get(vm=vm, nic=nic) print('vm.hardware.Ethernet.get({}, {}) -> {}'.format( vm, nic, nic_info)) global nics_to_delete print('\n# Example: Create Ethernet Nic using STANDARD_PORTGROUP with ' 'default settings') nic_create_spec = Ethernet.CreateSpec(backing=Ethernet.BackingSpec( type=Ethernet.BackingType.STANDARD_PORTGROUP, network=standard_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))) print('\n# Example: Create Ethernet Nic using DISTRIBUTED_PORTGROUP ' 'with defaults') nic_create_spec = Ethernet.CreateSpec(backing=Ethernet.BackingSpec( type=Ethernet.BackingType.DISTRIBUTED_PORTGROUP, network=distributed_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))) print('\n# Example: Create Ethernet Nic using STANDARD_' 'PORTGROUP specifying') print('# start_connected=True, allow_guest_control=True,') print('# mac_type, mac_Address, wake_on_lan_enabled') nic_create_spec = Ethernet.CreateSpec( start_connected=True, allow_guest_control=True, mac_type=Ethernet.MacAddressType.MANUAL, mac_address='01:23:45:67:89:10', wake_on_lan_enabled=True, backing=Ethernet.BackingSpec( type=Ethernet.BackingType.STANDARD_PORTGROUP, network=standard_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))) print('\n# Example: Create Ethernet Nic using DISTRIBUTED_PORTGROUP ' 'specifying') print('# start_connected=True, allow_guest_control=True,') print('# mac_type, mac_Address, wake_on_lan_enabled') nic_create_spec = Ethernet.CreateSpec( start_connected=True, allow_guest_control=True, mac_type=Ethernet.MacAddressType.MANUAL, mac_address='24:68:10:12:14:16', wake_on_lan_enabled=True, backing=Ethernet.BackingSpec( type=Ethernet.BackingType.DISTRIBUTED_PORTGROUP, network=distributed_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))) 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(backing=Ethernet.BackingSpec( type=Ethernet.BackingType.STANDARD_PORTGROUP, network=standard_network)) print('vm.hardware.Ethernet.update({}, {}, {})'.format( vm, nic, nic_update_spec)) client.vcenter.vm.hardware.Ethernet.update(vm, nic, nic_update_spec) nic_info = client.vcenter.vm.hardware.Ethernet.get(vm, nic) print('vm.hardware.Ethernet.get({}, {}) -> {}'.format( vm, nic, pp(nic_info))) print('\n# Example: Update Ethernet Nic wake_on_lan_enabled=False') print('# mac_type=GENERATED,') print('# start_connected=False,') print('# allow_guest_control=False') nic_update_spec = Ethernet.UpdateSpec( wake_on_lan_enabled=False, mac_type=Ethernet.MacAddressType.GENERATED, start_connected=False, allow_guest_control=False) print('vm.hardware.Ethernet.update({}, {}, {})'.format( vm, nic, nic_update_spec)) client.vcenter.vm.hardware.Ethernet.update(vm, nic, nic_update_spec) nic_info = client.vcenter.vm.hardware.Ethernet.get(vm, nic) print('vm.hardware.Ethernet.get({}, {}) -> {}'.format( vm, nic, pp(nic_info))) print('\n# Starting VM to run connect/disconnect sample') print('vm.Power.start({})'.format(vm)) client.vcenter.vm.Power.start(vm) nic_info = client.vcenter.vm.hardware.Ethernet.get(vm, nic) print('vm.hardware.Ethernet.get({}, {}) -> {}'.format( vm, nic, pp(nic_info))) print('\n# Example: Connect Ethernet Nic after powering on VM') client.vcenter.vm.hardware.Ethernet.connect(vm, nic) print('vm.hardware.Ethernet.connect({}, {})'.format(vm, nic)) nic_info = client.vcenter.vm.hardware.Ethernet.get(vm, nic) print('vm.hardware.Ethernet.get({}, {}) -> {}'.format( vm, nic, pp(nic_info))) print('\n# Example: Disconnect Ethernet Nic while VM is powered on') client.vcenter.vm.hardware.Ethernet.disconnect(vm, nic) print('vm.hardware.Ethernet.disconnect({}, {})'.format(vm, nic)) nic_info = client.vcenter.vm.hardware.Ethernet.get(vm, nic) print('vm.hardware.Ethernet.get({}, {}) -> {}'.format( vm, nic, pp(nic_info))) print('\n# Stopping VM after connect/disconnect sample') print('vm.Power.start({})'.format(vm)) client.vcenter.vm.Power.stop(vm) nic_info = client.vcenter.vm.hardware.Ethernet.get(vm, nic) print('vm.hardware.Ethernet.get({}, {}) -> {}'.format( vm, nic, pp(nic_info))) # List all Nics for a VM nic_summaries = client.vcenter.vm.hardware.Ethernet.list(vm=vm) print('vm.hardware.Ethernet.list({}) -> {}'.format(vm, nic_summaries))
def run(self): # Get a placement spec datacenter_name = 'Datacenter' # testbed.config['VM_DATACENTER_NAME'] # vm_folder_name = 'kong111_166' #testbed.config['VM_FOLDER2_NAME'] datastore_name = 'datastore-717' # testbed.config['VM_DATASTORE_NAME'] std_portgroup_name = 'VM Network' # testbed.config['STDPORTGROUP_NAME'] host = 'host-716' diskprovisioningtpye = DiskProvisioningType('eagerZeroedThick') if not self.placement_spec: # self.placement_spec = vm_placement_helper.get_placement_spec_for_resource_pool( # self.client, # datacenter_name, # vm_folder_name, # datastore_name) self.placement_spec = VM.PlacementSpec(host=host, datastore=datastore_name, folder='group-v3') # Get a standard network backing standard_network = network_helper.get_network_backing( self.client, std_portgroup_name, datacenter_name, Network.Type.STANDARD_PORTGROUP) """ Create a basic VM. Using the provided PlacementSpec, create a VM with a selected Guest OS and provided name. Create a VM with the following configuration: * Create 2 disks and specify one of them on scsi0:0 since it's the boot disk * Specify 1 ethernet adapter using a Standard Portgroup backing * Setup for PXE install by selecting network as first boot device Use guest and system provided defaults for most configuration settings. """ guest_os = 'CENTOS_6_64' # testbed.config['VM_GUESTOS'] hardware = '' # boot_disk = Disk.CreateSpec(type=Disk.HostBusAdapterType.SCSI, # scsi=ScsiAddressSpec(bus=0, unit=0), # new_vmdk=Disk.VmdkCreateSpec()) data_disk = Disk.CreateSpec(new_vmdk=Disk.VmdkCreateSpec( capacity=self.cap)) nic = Ethernet.CreateSpec( start_connected=True, backing=Ethernet.BackingSpec( type=Ethernet.BackingType.STANDARD_PORTGROUP, network=standard_network)) sata_adapt = Sata.CreateSpec() cpu = Cpu.UpdateSpec(count=self.cpu, cores_per_socket=1, hot_add_enabled=True, hot_remove_enabled=True) memory = Memory.UpdateSpec(size_mib=self.memory, hot_add_enabled=True) # boot_device_order = [ # BootDevice.EntryCreateSpec(BootDevice.Type.ETHERNET), # BootDevice.EntryCreateSpec(BootDevice.Type.DISK)] # host1=Host.list() # print (host1) placement = VM.PlacementSpec(host=host, datastore=datastore_name, folder='group-v3') vm_create_spec = VM.CreateSpec(name=self.vm_name, guest_os=guest_os, cpu=cpu, memory=memory, placement=placement, disks=[data_disk], nics=[nic], sata_adapters=[sata_adapt]) print('\n# Example: create_basic_vm: Creating a VM using spec\n-----') print(pp(vm_create_spec)) print('-----') vm = self.client.vcenter.VM.create(vm_create_spec) print("create_basic_vm: Created VM '{}' ({})".format(self.vm_name, vm)) vm_info = self.client.vcenter.VM.get(vm) print('vm.get({}) -> {}'.format(vm, pp(vm_info))) self.client.vcenter.vm.Power.start(vm) return vm