<s> from pyVmomi import vim <EOL> from vmwarelib import inventory <EOL> from vmwarelib . actions import BaseAction <EOL> class NewNetworkAdapter ( BaseAction ) : <EOL> @ staticmethod <EOL> def get_vm_reconfig_spec ( distributed_switch_obj , mac_address , network_obj , port_key , <EOL> stay_connected , network_type , wake_on_lan ) : <EOL> if network_type == '<STR_LIT>' : <EOL> virtual_network = vim . vm . device . VirtualE1000 ( ) <EOL> elif network_type == '<STR_LIT>' : <EOL> virtual_network = vim . vm . device . VirtualPCNet32 ( ) <EOL> lance_option = vim . vm . device . VirtualPCNet32Option ( ) <EOL> lance_option . supportsMorphing = True <EOL> elif network_type == '<STR_LIT>' : <EOL> virtual_network = vim . vm . device . VirtualVmxnet ( ) <EOL> elif network_type == '<STR_LIT>' : <EOL> virtual_network = vim . vm . device . VirtualVmxnet2 ( ) <EOL> elif network_type == '<STR_LIT>' : <EOL> virtual_network = vim . vm . device . VirtualVmxnet3 ( ) <EOL> else : <EOL> virtual_network = vim . vm . device . VirtualEthernetCard ( ) <EOL> virtual_network . wakeOnLanEnabled = wake_on_lan <EOL> connect_info = vim . vm . device . VirtualDevice . ConnectInfo ( ) <EOL> connect_info . startConnected = stay_connected <EOL> virtual_network . connectable = connect_info <EOL> if mac_address : <EOL> virtual_network . macAddress = mac_address <EOL> virtual_network . addressType = vim . vm . device . VirtualEthernetCardOption . MacTypes . manual <EOL> backing_info = None <EOL> if distributed_switch_obj : <EOL> dvs_port_connection = vim . dvs . PortConnection ( ) <EOL> dvs_port_connection . switchUuid = distributed_switch_obj . uuid <EOL> if port_key : <EOL> dvs_port_connection . portKey = port_key <EOL> port_criteria = vim . dvs . PortCriteria ( ) <EOL> port_criteria . portKey = port_key <EOL> ports = distributed_switch_obj . FetchDVPorts ( port_criteria ) <EOL> dvs_port_connection . portgroupKey = ports [ <NUM_LIT:0> ] . portgroupKey <EOL> backing_info = vim . vm . device . VirtualEthernetCard . DistributedVirtualPortBackingInfo ( ) <EOL> backing_info . port = dvs_port_connection <EOL> if network_obj : <EOL> backing_info = vim . vm . device . VirtualEthernetCard . NetworkBackingInfo ( ) <EOL> backing_info . network = network_obj <EOL> backing_info . deviceName = network_obj . name <EOL> virtual_network . backing = backing_info <EOL> network_spec = vim . vm . device . VirtualDeviceSpec ( ) <EOL> network_spec . device = virtual_network <EOL> network_spec . operation = vim . vm . device . VirtualDeviceSpec . Operation . add <EOL> vm_reconfig_spec = vim . vm . ConfigSpec ( ) <EOL> vm_reconfig_spec . deviceChange = [ network_spec ] <EOL> return vm_reconfig_spec <EOL> def run ( self , vms , distributed_switch = None , mac_address = None , network_name = None , port_key = None , <EOL> stay_connected = False , network_type = '<STR_LIT>' , wake_on_lan = False ) : <EOL> network_type = network_type . lower ( ) if network_type else None <EOL> si = self . si <EOL> si_content = si . RetrieveContent ( ) <EOL> vm_objs = [ vim . VirtualMachine ( moid , stub = si . _stub ) for moid in vms ] <EOL> [ vm_obj . name for vm_obj in vm_objs ] <EOL> distributed_switch_obj = None <EOL> if distributed_switch : <EOL> distributed_switch_obj = vim . DistributedVirtualSwitch ( distributed_switch , stub = si . _stub ) <EOL> distributed_switch_obj . name <EOL> network_obj = None <EOL> if network_name : <EOL> network_obj = inventory . get_network ( si_content , name = network_name ) <EOL> network_obj . name <EOL> result = [ ] <EOL> for vm in vm_objs : <EOL> vm_reconfig_spec = NewNetworkAdapter . get_vm_reconfig_spec ( distributed_switch_obj , <EOL> mac_address , network_obj , port_key , stay_connected , network_type , wake_on_lan ) <EOL> add_disk_task = vm . ReconfigVM_Task ( spec = vm_reconfig_spec ) <EOL> successfully_added_network = self . _wait_for_task ( add_disk_task ) <EOL> result . append ( { <EOL> "<STR_LIT>" : vm . _GetMoId ( ) , <EOL> "<STR_LIT:success>" : successfully_added_network <EOL> } ) <EOL> return result </s>
