def get_vm_from_ip(self, ip): """ Gets the name of a vm from its IP. Args: ip: The ip address of the vm. Returns: The vm name for the corresponding IP.""" vms = self.content.searchIndex.FindAllByIp(ip=ip, vmSearch=True) # As vsphere remembers the last IP a vm had, when we search we get all # of them. Consequently we need to store them all in a dict and then sort # them to find out which one has the latest boot time. I am going out on # a limb and saying that searching for several vms and querying each object # is quicker than finding all machines and recording the bootTime and ip address # of each, before iterating through all of them to weed out the ones we care # about, but I could be wrong. boot_times = {} for vm in vms: if vm.name not in boot_times: boot_times[vm.name] = datetime.fromtimestamp(0) try: boot_times[vm.name] = vm.summary.runtime.bootTime except Exception: pass if boot_times: newest_boot_time = sorted(boot_times.items(), key=operator.itemgetter(1), reverse=True)[0] newest_vm = newest_boot_time[0] return VMWareVirtualMachine(system=self, name=newest_vm.name, raw=newest_vm) else: raise VMNotFoundViaIP('The requested IP is not known as a VM')
def get_vm_from_ip(self, ip): """ Gets a vm from its IP. Args: ip: The ip address of the vm. Returns: wrapanapi.systems.rhevm.RHEVMVirtualMachine object """ vms = self.list_vms() for vm in vms: if ip in vm.all_ips: return vm raise VMNotFoundViaIP("IP '{}' is not known as a VM".format(ip))