def get_by_id_or_name(self, network_ids=[], network_names=[]): results = {} for did in network_ids: network = inventory.get_network(self.si_content, moid=did) if network and network.name not in results: results[network.name] = self.get_network_dict(network) for network in network_names: network = inventory.get_network(self.si_content, name=network) if network and network.name not in results: results[network.name] = self.get_network_dict(network) return list(results.values())
def run(self, vm_id, vm_name, network_name, nictype, stayconnected, wakeonlan, vsphere=None): """ Add Network Adapter to Virtual Machine Args: - vm_id: Moid of Virtual Machine to edit - vm_name: Name of Virtual Machine to edit - vsphere: Pre-configured vsphere connection details (config.yaml) - network_name: vsphere network to connect to - nictype: Nic type to add - stayconnected: Nic connected on boot - wakeonlan: Wake on Lan Returns: - dict: state true/false """ # create object itmes of key components checkinputs.one_of_two_strings(vm_id, vm_name, "ID or Name") self.establish_connection(vsphere) vm = inventory.get_virtualmachine(self.si_content, vm_id, vm_name) network_obj = inventory.get_network(self.si_content, name=network_name) vm_reconfig_spec = self.get_vm_reconfig_spec(network_obj, stayconnected, nictype, wakeonlan) add_vnic_task = vm.ReconfigVM_Task(spec=vm_reconfig_spec) successfully_added_vnic = self._wait_for_task(add_vnic_task) return {'state': successfully_added_vnic}
def run(self, vms, distributed_switch=None, mac_address=None, network_name=None, port_key=None, stay_connected=False, network_type='Flexible', wake_on_lan=False): network_type = network_type.lower() if network_type else None si = self.si si_content = si.RetrieveContent() vm_objs = [vim.VirtualMachine(moid, stub=si._stub) for moid in vms] # by checking the name property, the vms' existance is checked. [vm_obj.name for vm_obj in vm_objs] distributed_switch_obj = None if distributed_switch: distributed_switch_obj = vim.DistributedVirtualSwitch(distributed_switch, stub=si._stub) # by checking the name property, the distributed switch existence is checked. distributed_switch_obj.name network_obj = None if network_name: network_obj = inventory.get_network(si_content, name=network_name) # by checking the name property, the network existence is checked. network_obj.name result = [] for vm in vm_objs: vm_reconfig_spec = NewNetworkAdapter.get_vm_reconfig_spec(distributed_switch_obj, mac_address, network_obj, port_key, stay_connected, network_type, wake_on_lan) add_disk_task = vm.ReconfigVM_Task(spec=vm_reconfig_spec) successfully_added_network = self._wait_for_task(add_disk_task) result.append({ "vm_moid": vm._GetMoId(), "success": successfully_added_network }) return result
def run(self, vm_id, network_id, ip, subnet, gateway=None, domain=None): # convert ids to stubs virtualmachine = inventory.get_virtualmachine(self.si_content, vm_id) network = inventory.get_network(self.si_content, network_id) # add new vnic configspec = vim.vm.ConfigSpec() nic = vim.vm.device.VirtualDeviceSpec() nic.operation = vim.vm.device.VirtualDeviceSpec.Operation.add # or edit if a device exists nic.device = vim.vm.device.VirtualVmxnet3() nic.device.wakeOnLanEnabled = True nic.device.addressType = 'assigned' # docs recommend using unique negative integers as temporary keys. # See https://github.com/vmware/pyvmomi/blob/master/docs/vim/vm/device/VirtualDevice.rst nic.device.key = -1 nic.device.deviceInfo = vim.Description() nic.device.deviceInfo.label = 'Network Adapter-%s' % (ip) nic.device.deviceInfo.summary = 'summary' nic.device.backing = vim.vm.device.VirtualEthernetCard.DistributedVirtualPortBackingInfo() nic.device.backing.port = vim.dvs.PortConnection() nic.device.backing.port.switchUuid = network.config.distributedVirtualSwitch.uuid nic.device.backing.port.portgroupKey = network.config.key nic.device.connectable = vim.vm.device.VirtualDevice.ConnectInfo() nic.device.connectable.startConnected = True nic.device.connectable.allowGuestControl = True configspec.deviceChange = [nic] add_vnic_task = virtualmachine.ReconfigVM_Task(configspec) successfully_added_vnic = self._wait_for_task(add_vnic_task) # customize VM cust_item = vim.CustomizationSpecItem() cust_specinfo = vim.CustomizationSpecInfo() cust_specinfo.name = 'assignip-' + str(uuid.uuid4()) cust_specinfo.type = 'Linux' cust_item.info = cust_specinfo # fixed ip cust_spec = vim.vm.customization.Specification() cust_item.spec = cust_spec ip_adapter_mapping = vim.vm.customization.AdapterMapping() ip_adapter_mapping.adapter = vim.vm.customization.IPSettings() ip_adapter_mapping.adapter.ip = vim.vm.customization.FixedIp() ip_adapter_mapping.adapter.ip.ipAddress = ip ip_adapter_mapping.adapter.subnetMask = subnet ip_adapter_mapping.adapter.gateway = gateway ip_adapter_mapping.adapter.dnsDomain = domain cust_spec.nicSettingMap = [ip_adapter_mapping] cust_spec.identity = vim.vm.customization.LinuxPrep() cust_spec.identity.hostName = vim.vm.customization.PrefixNameGenerator() cust_spec.identity.hostName.base = 'st2' cust_spec.identity.domain = 'demo.net' cust_spec.globalIPSettings = vim.vm.customization.GlobalIPSettings() try: self.si_content.customizationSpecManager.CreateCustomizationSpec(cust_item) except: self.logger.exception('Failed to create customization spec.') raise return {'state': successfully_added_vnic}
def run(self, vm_id, network_id, ip, subnet, gateway=None, domain=None): # convert ids to stubs virtualmachine = inventory.get_virtualmachine(self.service_instance, vm_id) network = inventory.get_network(self.service_instance, network_id) # add new vnic configspec = vim.vm.ConfigSpec() nic = vim.vm.device.VirtualDeviceSpec() nic.operation = vim.vm.device.VirtualDeviceSpec.Operation.add # or edit if a device exists nic.device = vim.vm.device.VirtualVmxnet3() nic.device.wakeOnLanEnabled = True nic.device.addressType = 'assigned' # docs recommend using unique negative integers as temporary keys. # See https://github.com/vmware/pyvmomi/blob/master/docs/vim/vm/device/VirtualDevice.rst nic.device.key = -1 nic.device.deviceInfo = vim.Description() nic.device.deviceInfo.label = 'Network Adapter-%s' % (ip) nic.device.deviceInfo.summary = 'summary' nic.device.backing = vim.vm.device.VirtualEthernetCard.DistributedVirtualPortBackingInfo() nic.device.backing.port = vim.dvs.PortConnection() nic.device.backing.port.switchUuid = network.config.distributedVirtualSwitch.uuid nic.device.backing.port.portgroupKey = network.config.key nic.device.connectable = vim.vm.device.VirtualDevice.ConnectInfo() nic.device.connectable.startConnected = True nic.device.connectable.allowGuestControl = True configspec.deviceChange = [nic] add_vnic_task = virtualmachine.ReconfigVM_Task(configspec) successfully_added_vnic = self._wait_for_task(add_vnic_task) # customize VM cust_item = vim.CustomizationSpecItem() cust_specinfo = vim.CustomizationSpecInfo() cust_specinfo.name = 'assignip-' + str(uuid.uuid4()) cust_specinfo.type = 'Linux' cust_item.info = cust_specinfo # fixed ip cust_spec = vim.vm.customization.Specification() cust_item.spec = cust_spec ip_adapter_mapping = vim.vm.customization.AdapterMapping() ip_adapter_mapping.adapter = vim.vm.customization.IPSettings() ip_adapter_mapping.adapter.ip = vim.vm.customization.FixedIp() ip_adapter_mapping.adapter.ip.ipAddress = ip ip_adapter_mapping.adapter.subnetMask = subnet ip_adapter_mapping.adapter.gateway = gateway ip_adapter_mapping.adapter.dnsDomain = domain cust_spec.nicSettingMap = [ip_adapter_mapping] cust_spec.identity = vim.vm.customization.LinuxPrep() cust_spec.identity.hostName = vim.vm.customization.PrefixNameGenerator() cust_spec.identity.hostName.base = 'st2' cust_spec.identity.domain = 'demo.net' cust_spec.globalIPSettings = vim.vm.customization.GlobalIPSettings() try: self.service_instance.customizationSpecManager.CreateCustomizationSpec(cust_item) except: self.logger.exception('Failed to create customization spec.') raise return {'state': successfully_added_vnic}
def run(self, vm_id, network_id, vnic_key, ip, subnet, gateway=None, domain=None): # convert ids to stubs virtualmachine = inventory.get_virtualmachine(self.si_content, vm_id) network = inventory.get_network(self.si_content, network_id) vnic = self._get_vnic_device(virtualmachine, vnic_key) # add new vnic configspec = vim.vm.ConfigSpec() nic = vim.vm.device.VirtualDeviceSpec() nic.operation = vim.vm.device.VirtualDeviceSpec.Operation.edit nic.device = vim.vm.device.VirtualVmxnet3() nic.device.wakeOnLanEnabled = True nic.device.addressType = 'assigned' nic.device.key = vnic.key nic.device.deviceInfo = vim.Description() nic.device.deviceInfo.label = 'Network Adapter-%s' % (ip) nic.device.deviceInfo.summary = 'summary' nic.device.backing = vim.vm.device.VirtualEthernetCard.DistributedVirtualPortBackingInfo() nic.device.backing.port = vim.dvs.PortConnection() nic.device.backing.port.switchUuid = network.config.distributedVirtualSwitch.uuid nic.device.backing.port.portgroupKey = network.config.key nic.device.connectable = vim.vm.device.VirtualDevice.ConnectInfo() nic.device.connectable.startConnected = True nic.device.connectable.allowGuestControl = True configspec.deviceChange = [nic] add_vnic_task = virtualmachine.ReconfigVM_Task(configspec) successfully_added_vnic = self._wait_for_task(add_vnic_task) if not successfully_added_vnic: return self._format_result(successfully_added_vnic, 'Failed to update nic.') adaptermap = vim.vm.customization.AdapterMapping() adaptermap.adapter = vim.vm.customization.IPSettings() adaptermap.adapter.ip = vim.vm.customization.FixedIp() adaptermap.adapter.ip.ipAddress = ip adaptermap.adapter.subnetMask = subnet adaptermap.adapter.gateway = gateway adaptermap.adapter.dnsDomain = domain globalip = vim.vm.customization.GlobalIPSettings() ident = vim.vm.customization.LinuxPrep() ident.domain = domain ident.hostName = vim.vm.customization.FixedName() ident.hostName.name = virtualmachine.name customspec = vim.vm.customization.Specification() customspec.identity = ident customspec.nicSettingMap = [adaptermap] customspec.globalIPSettings = globalip try: customize_task = virtualmachine.Customize(spec=customspec) successfully_customized = self._wait_for_task(customize_task) except: self.logger.exception('Failed to create customization spec.') raise msg = 'Updated nic and assigned IP.' if successfully_customized else 'Failed to assign ip.' return self._format_result(successfully_customized, msg=msg)
def run(self, vm_id, vm_name, network_name, nictype, stayconnected, wakeonlan): # create object itmes of key components checkinputs.one_of_two_strings(vm_id, vm_name, "ID or Name") vm = inventory.get_virtualmachine(self.si_content, vm_id, vm_name) network_obj = inventory.get_network(self.si_content, name=network_name) vm_reconfig_spec = self.get_vm_reconfig_spec(network_obj, stayconnected, nictype, wakeonlan) add_vnic_task = vm.ReconfigVM_Task(spec=vm_reconfig_spec) successfully_added_vnic = self._wait_for_task(add_vnic_task) return {'state': successfully_added_vnic}
def run(self, vm_id, vm_name, network_name, nictype, stayconnected, wakeonlan, vsphere=None): """ Add Network Adapter to Virtual Machine Args: - vm_id: Moid of Virtual Machine to edit - vm_name: Name of Virtual Machine to edit - vsphere: Pre-configured vsphere connection details (config.yaml) - network_name: vsphere network to connect to - nictype: Nic type to add - stayconnected: Nic connected on boot - wakeonlan: Wake on Lan Returns: - dict: state true/false """ # create object itmes of key components checkinputs.one_of_two_strings(vm_id, vm_name, "ID or Name") self.establish_connection(vsphere) vm = inventory.get_virtualmachine(self.si_content, vm_id, vm_name) try: nettype = "dist" network_obj = inventory.get_distributedportgroup(self.si_content, name=network_name) except: nettype = "std" network_obj = inventory.get_network(self.si_content, name=network_name) vm_reconfig_spec = self.get_vm_reconfig_spec(network_obj, stayconnected, nictype, wakeonlan, nettype) add_vnic_task = vm.ReconfigVM_Task(spec=vm_reconfig_spec) successfully_added_vnic = self._wait_for_task(add_vnic_task) return {'state': successfully_added_vnic}
def run(self, vm_id, vm_name, network_adapter, network_name): # check means of finding the VM was provided checkinputs.one_of_two_strings(vm_id, vm_name, "ID or Name") # convert ids to stubs vm = inventory.get_virtualmachine(self.si_content, moid=vm_id, name=vm_name) network_obj = inventory.get_network(self.si_content, name=network_name) # find correct NIC for device in vm.config.hardware.device: if isinstance(device, vim.vm.device.VirtualEthernetCard)\ and device.deviceInfo.label == network_adapter: nic = device # Different test method due to fact that object # isn't instantiated if not found try: nic except: raise Exception('Unable to find Network Adapter provided') # Create object for new Specification new_spec = vim.vm.device.VirtualDeviceSpec() new_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.edit new_spec.device = nic # If network name provided assign new network # Room to expand the following to set additional flags/values if network_name: new_spec.device.backing.network = network_obj new_spec.device.backing.deviceName = network_obj.name new_spec.device.deviceInfo.summary = network_obj.name # format changes for config spec update dev_changes = [] dev_changes.append(new_spec) spec = vim.vm.ConfigSpec() spec.deviceChange = dev_changes task = vm.ReconfigVM_Task(spec) self._wait_for_task(task) return {'state': str(task.info.state)}
def run(self, vm_id, vm_name, network_adapter, network_name, vsphere=None): """ Edit Network Adapater on Virtual Machine Args: - vm_id: Moid of Virtual Machine to edit - vm_name: Name of Virtual Machine to edit - vsphere: Pre-configured vsphere connection details (config.yaml) - network_name: Network to attach adapter to - network_adapter: Name of Adapter to edit Returns: - dict: State true/false """ # check means of finding the VM was provided checkinputs.one_of_two_strings(vm_id, vm_name, "ID or Name") self.establish_connection(vsphere) # convert ids to stubs vm = inventory.get_virtualmachine(self.si_content, moid=vm_id, name=vm_name) try: nettype = "dist" network_obj = inventory.get_distributedportgroup(self.si_content, name=network_name) except: nettype = "std" network_obj = inventory.get_network(self.si_content, name=network_name) # find correct NIC for device in vm.config.hardware.device: if isinstance(device, vim.vm.device.VirtualEthernetCard)\ and device.deviceInfo.label == network_adapter: nic = device # Different test method due to fact that object # isn't instantiated if not found try: nic except: raise Exception('Unable to find Network Adapter provided') # Create object for new Specification new_spec = vim.vm.device.VirtualDeviceSpec() new_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.edit new_spec.device = nic # If network name provided assign new network # Room to expand the following to set additional flags/values if network_name: # Default functionality is to use the # Distributed Port Group over a standard group if nettype == "dist": new_spec.device.backing = \ vim.vm.device.VirtualEthernetCard\ .DistributedVirtualPortBackingInfo() new_spec.device.backing.port = vim.dvs.PortConnection() dvs_port_connection = vim.dvs.PortConnection() dvs_port_connection.portgroupKey = network_obj.key dvs_port_connection.switchUuid = \ network_obj.config.distributedVirtualSwitch.uuid new_spec.device.backing = \ vim.vm.device.VirtualEthernetCard\ .DistributedVirtualPortBackingInfo() new_spec.device.backing.port = dvs_port_connection else: new_spec.device.backing = \ vim.vm.device.VirtualEthernetCard.NetworkBackingInfo() new_spec.device.backing.network = network_obj new_spec.device.backing.deviceName = network_obj.name # format changes for config spec update dev_changes = [] dev_changes.append(new_spec) spec = vim.vm.ConfigSpec() spec.deviceChange = dev_changes task = vm.ReconfigVM_Task(spec) self._wait_for_task(task) return {'state': str(task.info.state)}
def run(self, vm_id, network_id, vnic_key, ip, subnet, gateway=None, domain=None): # convert ids to stubs virtualmachine = inventory.get_virtualmachine(self.si_content, vm_id) network = inventory.get_network(self.si_content, network_id) vnic = self._get_vnic_device(virtualmachine, vnic_key) # add new vnic configspec = vim.vm.ConfigSpec() nic = vim.vm.device.VirtualDeviceSpec() nic.operation = vim.vm.device.VirtualDeviceSpec.Operation.edit nic.device = vim.vm.device.VirtualVmxnet3() nic.device.wakeOnLanEnabled = True nic.device.addressType = 'assigned' nic.device.key = vnic.key nic.device.deviceInfo = vim.Description() nic.device.deviceInfo.label = 'Network Adapter-%s' % (ip) nic.device.deviceInfo.summary = 'summary' nic.device.backing = vim.vm.device.VirtualEthernetCard.DistributedVirtualPortBackingInfo( ) nic.device.backing.port = vim.dvs.PortConnection() nic.device.backing.port.switchUuid = network.config.distributedVirtualSwitch.uuid nic.device.backing.port.portgroupKey = network.config.key nic.device.connectable = vim.vm.device.VirtualDevice.ConnectInfo() nic.device.connectable.startConnected = True nic.device.connectable.allowGuestControl = True configspec.deviceChange = [nic] add_vnic_task = virtualmachine.ReconfigVM_Task(configspec) successfully_added_vnic = self._wait_for_task(add_vnic_task) if not successfully_added_vnic: return self._format_result(successfully_added_vnic, 'Failed to update nic.') adaptermap = vim.vm.customization.AdapterMapping() adaptermap.adapter = vim.vm.customization.IPSettings() adaptermap.adapter.ip = vim.vm.customization.FixedIp() adaptermap.adapter.ip.ipAddress = ip adaptermap.adapter.subnetMask = subnet adaptermap.adapter.gateway = gateway adaptermap.adapter.dnsDomain = domain globalip = vim.vm.customization.GlobalIPSettings() ident = vim.vm.customization.LinuxPrep() ident.domain = domain ident.hostName = vim.vm.customization.FixedName() ident.hostName.name = virtualmachine.name customspec = vim.vm.customization.Specification() customspec.identity = ident customspec.nicSettingMap = [adaptermap] customspec.globalIPSettings = globalip try: customize_task = virtualmachine.Customize(spec=customspec) successfully_customized = self._wait_for_task(customize_task) except: self.logger.exception('Failed to create customization spec.') raise msg = 'Updated nic and assigned IP.' if successfully_customized else 'Failed to assign ip.' return self._format_result(successfully_customized, msg=msg)