def _best_flavor(self, properties): log.info(_('Choosing the best flavor for given attributes.')) # Check whether user exported all required environment variables. flavors = nova_flavors.get_flavors() # start with all flavors match_all = flavors.keys() # TODO(anyone): Handle the case where the value contains something like # get_input instead of a value. # flavors that fit the CPU count cpu = properties.get(self.NUM_CPUS) if cpu is None: self._log_compute_msg(self.NUM_CPUS, 'flavor') match_cpu = self._match_flavors(match_all, flavors, self.NUM_CPUS, cpu) # flavors that fit the mem size mem = properties.get(self.MEM_SIZE) if mem: mem = translator.common.utils.MemoryUnit.convert_unit_size_to_num( mem, 'MB') else: self._log_compute_msg(self.MEM_SIZE, 'flavor') match_cpu_mem = self._match_flavors(match_cpu, flavors, self.MEM_SIZE, mem) # flavors that fit the disk size disk = properties.get(self.DISK_SIZE) if disk: disk = translator.common.utils.MemoryUnit.\ convert_unit_size_to_num(disk, 'GB') else: self._log_compute_msg(self.DISK_SIZE, 'flavor') match_cpu_mem_disk = self._match_flavors(match_cpu_mem, flavors, self.DISK_SIZE, disk) # if multiple match, pick the flavor with the least memory # the selection can be based on other heuristic, e.g. pick one with the # least total resource if len(match_cpu_mem_disk) > 1: return self._least_flavor(match_cpu_mem_disk, flavors, 'mem_size') elif len(match_cpu_mem_disk) == 1: return match_cpu_mem_disk[0] else: return None