def _check_nodes_types(self, other): if type(self) is not type(other): raise TypeError( 'Unsupported operand types: {} and {}'.format( type(self), type(other))) if self.cloud_management is not other.cloud_management: raise error.NodeCollectionError( 'NodeCollections have different cloud_managements: ' '{} and {}'.format(self.cloud_management, other.cloud_management)) if self.power_management is not other.power_management: raise error.NodeCollectionError( 'NodeCollections have different power_managements: ' '{} and {}'.format(self.power_management, other.power_management))
def filter(self, criteria_fn): hosts = list(filter(criteria_fn, self._hosts)) if hosts: return self._make_instance(hosts) else: raise error.NodeCollectionError( 'No nodes found according to criterion')
def pick(self, count=1): """Pick one Node out of collection :return: NodeCollection consisting just one node """ if count > len(self._hosts): msg = 'Cannot pick {} from {} node(s)'.format( count, len(self._hosts)) raise error.NodeCollectionError(msg) return self._make_instance(random.sample(self._hosts, count))
def get_nodes(self, fqdns=None): """Get nodes in the cloud This function returns NodesCollection representing all nodes in the cloud or only those that were specified by FQDNs. :param fqdns: list of FQDNs or None to retrieve all nodes :return: NodesCollection """ hosts = self._get_cloud_hosts() if fqdns: LOG.debug('Trying to find nodes with FQDNs: %s', fqdns) hosts = list() for fqdn in fqdns: if fqdn in self.fqdn_to_hosts: hosts.append(self.fqdn_to_hosts[fqdn]) else: raise error.NodeCollectionError( 'Node with FQDN \'%s\' not found!' % fqdn) LOG.debug('The following nodes were found: %s', hosts) return self.NODE_CLS(cloud_management=self, power_management=self.power_management, hosts=hosts)