def search_task(self, criteria=None): self._ensure_alive() tasks = [] criteria = criteria or Criteria.true() search_for_criteria(criteria) try: for task in self._tasks: if match_object(criteria, task): tasks.append(task) except Exception as ex: # pylint: disable=broad-except return f_return_error(ex) random.shuffle(tasks) return self._prepare_pages(tasks)
def search_content(self, criteria=None): self._ensure_alive() criteria = criteria or Criteria.true() out = [] # Pass the criteria through the code used by the real client to build # up the Pulp query. We don't actually *use* the resulting query since # we're not accessing a real Pulp server. The point is to ensure the # same validation and error behavior as used by the real client also # applies to the fake. prepared_search = search_for_criteria(criteria, Unit) available_type_ids = set(self._type_ids) missing_type_ids = set(prepared_search.type_ids or []) - available_type_ids if missing_type_ids: return f_return_error( PulpException( "following type ids are not supported on the server: %s" % ",".join(missing_type_ids))) for unit in self._all_units: if (prepared_search.type_ids and unit.content_type_id not in prepared_search.type_ids): continue if match_object(criteria, unit): out.append(unit) # callers should not make any assumption about the order of returned # values. Encourage that by returning output in unpredictable order random.shuffle(out) return self._prepare_pages(out)
def _search_repo_units(self, repo_id, criteria): criteria = criteria or Criteria.true() # Pass the criteria through the same handling as used by the real client # for serialization, to ensure we reject criteria also rejected by real client # and also accumulate unit_fields. prepared_search = search_for_criteria(criteria, Unit) repo_f = self.get_repository(repo_id) if repo_f.exception(): return repo_f with self._state.lock: repo_units = self._state.repo_units(repo_id) out = [] try: for unit in repo_units: if match_object(criteria, unit): unit = units.with_filtered_fields(unit, prepared_search.unit_fields) out.append(unit) except Exception as ex: # pylint: disable=broad-except return f_return_error(ex) random.shuffle(out) return self._prepare_pages(out)
def search_distributor(self, criteria=None): criteria = criteria or Criteria.true() distributors = [] search_for_criteria(criteria, Distributor) try: for repo in self._repositories: for distributor in repo.distributors: if match_object(criteria, distributor): distributors.append( attr.evolve(distributor, repo_id=repo.id)) except Exception as ex: # pylint: disable=broad-except return f_return_error(ex) random.shuffle(distributors) return self._prepare_pages(distributors)
def search_repository(self, criteria=None): criteria = criteria or Criteria.true() repos = [] # Pass the criteria through the code used by the real client to build # up the Pulp query. We don't actually *use* the resulting query since # we're not accessing a real Pulp server. The point is to ensure the # same validation and error behavior as used by the real client also # applies to the fake. search_for_criteria(criteria, Repository) try: for repo in self._repositories: if match_object(criteria, repo): repos.append(self._attach_repo(repo)) except Exception as ex: # pylint: disable=broad-except return f_return_error(ex) # callers should not make any assumption about the order of returned # values. Encourage that by returning output in unpredictable order random.shuffle(repos) return self._prepare_pages(repos)
def _search_repo_units(self, repo_id, criteria): criteria = criteria or Criteria.true() # Pass the criteria through the same handling as used by the real client # for serialization, to ensure we reject criteria also rejected by real client. # We don't actually use the result, this is only for validation. search_for_criteria(criteria, Unit) repo_f = self.get_repository(repo_id) if repo_f.exception(): return repo_f repo_units = self._repo_units(repo_id) out = [] try: for unit in repo_units: if match_object(criteria, unit): out.append(unit) except Exception as ex: # pylint: disable=broad-except return f_return_error(ex) random.shuffle(out) return self._prepare_pages(out)
def _do_unassociate(self, repo_id, criteria=None): repo_f = self.get_repository(repo_id) if repo_f.exception(): return repo_f with self._state.lock: current = self._state.repo_unit_keys.get(repo_id, set()) units_with_key = [ {"key": key, "unit": self._state.units_by_key[key]} for key in current ] removed_units = set() kept_keys = set() criteria = criteria or Criteria.true() # validating the criteria here like in actual scenario. pulp_search = search_for_criteria( criteria, type_hint=Unit, unit_type_accum=None ) # raise an error if criteria with filters doesn't include type_ids if pulp_search.filters and not pulp_search.type_ids: raise ValueError( "Criteria to remove_content must specify at least one unit type!" ) for unit_with_key in units_with_key: unit = unit_with_key["unit"] if match_object(criteria, unit): removed_units.add(unit) else: kept_keys.add(unit_with_key["key"]) self._state.repo_unit_keys[repo_id] = kept_keys task = Task( id=self._state.next_task_id(), repo_id=repo_id, completed=True, succeeded=True, units=units.with_key_only(removed_units), ) return f_return([task])
def _do_unassociate(self, repo_id, criteria=None): repo_f = self.get_repository(repo_id) if repo_f.exception(): return repo_f current = self._repo_unit_keys.get(repo_id, set()) units_with_key = [{ "key": key, "unit": self._units_by_key[key] } for key in current] removed_units = set() kept_keys = set() # use type hint=Unit so that if type_ids are the goal here # then we will get back a properly prepared PulpSearch with # a populated type_ids field pulp_search = search_for_criteria(criteria, type_hint=Unit, type_ids_accum=None) for unit_with_key in units_with_key: unit = unit_with_key["unit"] if (pulp_search.type_ids is None or unit.content_type_id in pulp_search.type_ids): removed_units.add(unit) else: kept_keys.add(unit_with_key["key"]) self._repo_unit_keys[repo_id] = kept_keys task = Task( id=self._next_task_id(), repo_id=repo_id, completed=True, succeeded=True, units=units.with_key_only(removed_units), ) return f_return([task])