@require_context
@pick_context_manager_reader_allow_async
def instance_get_all_by_filters_sort(context, filters, limit=None, marker=None, columns_to_join=None, sort_keys=None, sort_dirs=None):
 if (limit == 0):
  return []
 (sort_keys, sort_dirs) = process_sort_params(sort_keys, sort_dirs, default_dir='desc')
 if (columns_to_join is None):
  columns_to_join_new = ['info_cache', 'security_groups']
  manual_joins = ['metadata', 'system_metadata']
 else:
  (manual_joins, columns_to_join_new) = _manual_join_columns(columns_to_join)
 query_prefix = context.session.query(models.Instance)
 for column in columns_to_join_new:
  if ('extra.' in column):
   query_prefix = query_prefix.options(undefer(column))
  else:
   query_prefix = query_prefix.options(joinedload(column))
 filters = filters.copy()
 if ('changes-since' in filters):
  changes_since = timeutils.normalize_time(filters['changes-since'])
  query_prefix = query_prefix.filter((models.Instance.updated_at >= changes_since))
 if ('deleted' in filters):
  deleted = filters.pop('deleted')
  if deleted:
   if filters.pop('soft_deleted', True):
    delete = or_((models.Instance.deleted == models.Instance.id), (models.Instance.vm_state == vm_states.SOFT_DELETED))
    query_prefix = query_prefix.filter(delete)
   else:
    query_prefix = query_prefix.filter((models.Instance.deleted == models.Instance.id))
  else:
   query_prefix = query_prefix.filter_by(deleted=0)
   if (not filters.pop('soft_deleted', False)):
    not_soft_deleted = or_((models.Instance.vm_state != vm_states.SOFT_DELETED), (models.Instance.vm_state == null()))
    query_prefix = query_prefix.filter(not_soft_deleted)
 if ('cleaned' in filters):
  cleaned = (1 if filters.pop('cleaned') else 0)
  query_prefix = query_prefix.filter((models.Instance.cleaned == cleaned))
 if ('tags' in filters):
  tags = filters.pop('tags')
  first_tag = tags.pop(0)
  query_prefix = query_prefix.join(models.Instance.tags)
  query_prefix = query_prefix.filter((models.Tag.tag == first_tag))
  for tag in tags:
   tag_alias = aliased(models.Tag)
   query_prefix = query_prefix.join(tag_alias, models.Instance.tags)
   query_prefix = query_prefix.filter((tag_alias.tag == tag))
 if ('tags-any' in filters):
  tags = filters.pop('tags-any')
  tag_alias = aliased(models.Tag)
  query_prefix = query_prefix.join(tag_alias, models.Instance.tags)
  query_prefix = query_prefix.filter(tag_alias.tag.in_(tags))
 if ('not-tags' in filters):
  tags = filters.pop('not-tags')
  first_tag = tags.pop(0)
  subq = query_prefix.session.query(models.Tag.resource_id)
  subq = subq.join(models.Instance.tags)
  subq = subq.filter((models.Tag.tag == first_tag))
  for tag in tags:
   tag_alias = aliased(models.Tag)
   subq = subq.join(tag_alias, models.Instance.tags)
   subq = subq.filter((tag_alias.tag == tag))
  query_prefix = query_prefix.filter((~ models.Instance.uuid.in_(subq)))
 if ('not-tags-any' in filters):
  tags = filters.pop('not-tags-any')
  query_prefix = query_prefix.filter((~ models.Instance.tags.any(models.Tag.tag.in_(tags))))
 if (not context.is_admin):
  if context.project_id:
   filters['project_id'] = context.project_id
  else:
   filters['user_id'] = context.user_id
 exact_match_filter_names = ['project_id', 'user_id', 'image_ref', 'vm_state', 'instance_type_id', 'uuid', 'metadata', 'host', 'task_state', 'system_metadata']
 query_prefix = _exact_instance_filter(query_prefix, filters, exact_match_filter_names)
 if (query_prefix is None):
  return []
 query_prefix = _regex_instance_filter(query_prefix, filters)
 query_prefix = _tag_instance_filter(context, query_prefix, filters)
 if (marker is not None):
  try:
   marker = _instance_get_by_uuid(context.elevated(read_deleted='yes'), marker)
  except exception.InstanceNotFound:
   raise exception.MarkerNotFound(marker=marker)
 try:
  query_prefix = sqlalchemyutils.paginate_query(query_prefix, models.Instance, limit, sort_keys, marker=marker, sort_dirs=sort_dirs)
 except db_exc.InvalidSortKey:
  raise exception.InvalidSortKey()
 return _instances_fill_metadata(context, query_prefix.all(), manual_joins)

@ require_context @ pick_context_manager_reader_allow_async def instance_get_all_by_filters_sort ( context , filters , limit = None , marker = None , columns_to_join = None , sort_keys = None , sort_dirs = None ) : if ( limit == 0 ) : return [ ] ( sort_keys , sort_dirs ) = process_sort_params ( sort_keys , sort_dirs , default_dir = 'desc' ) if ( columns_to_join is None ) : columns_to_join_new = [ 'info_cache' , 'security_groups' ] manual_joins = [ 'metadata' , 'system_metadata' ] else : ( manual_joins , columns_to_join_new ) = _manual_join_columns ( columns_to_join ) query_prefix = context . session . query ( models . Instance ) for column in columns_to_join_new : if ( 'extra.' in column ) : query_prefix = query_prefix . options ( undefer ( column ) ) else : query_prefix = query_prefix . options ( joinedload ( column ) ) filters = filters . copy ( ) if ( 'changes-since' in filters ) : changes_since = timeutils . normalize_time ( filters [ 'changes-since' ] ) query_prefix = query_prefix . filter ( ( models . Instance . updated_at >= changes_since ) ) if ( 'deleted' in filters ) : deleted = filters . pop ( 'deleted' ) if deleted : if filters . pop ( 'soft_deleted' , True ) : delete = or_ ( ( models . Instance . deleted == models . Instance . id ) , ( models . Instance . vm_state == vm_states . SOFT_DELETED ) ) query_prefix = query_prefix . filter ( delete ) else : query_prefix = query_prefix . filter ( ( models . Instance . deleted == models . Instance . id ) ) else : query_prefix = query_prefix . filter_by ( deleted = 0 ) if ( not filters . pop ( 'soft_deleted' , False ) ) : not_soft_deleted = or_ ( ( models . Instance . vm_state != vm_states . SOFT_DELETED ) , ( models . Instance . vm_state == null ( ) ) ) query_prefix = query_prefix . filter ( not_soft_deleted ) if ( 'cleaned' in filters ) : cleaned = ( 1 if filters . pop ( 'cleaned' ) else 0 ) query_prefix = query_prefix . filter ( ( models . Instance . cleaned == cleaned ) ) if ( 'tags' in filters ) : tags = filters . pop ( 'tags' ) first_tag = tags . pop ( 0 ) query_prefix = query_prefix . join ( models . Instance . tags ) query_prefix = query_prefix . filter ( ( models . Tag . tag == first_tag ) ) for tag in tags : tag_alias = aliased ( models . Tag ) query_prefix = query_prefix . join ( tag_alias , models . Instance . tags ) query_prefix = query_prefix . filter ( ( tag_alias . tag == tag ) ) if ( 'tags-any' in filters ) : tags = filters . pop ( 'tags-any' ) tag_alias = aliased ( models . Tag ) query_prefix = query_prefix . join ( tag_alias , models . Instance . tags ) query_prefix = query_prefix . filter ( tag_alias . tag . in_ ( tags ) ) if ( 'not-tags' in filters ) : tags = filters . pop ( 'not-tags' ) first_tag = tags . pop ( 0 ) subq = query_prefix . session . query ( models . Tag . resource_id ) subq = subq . join ( models . Instance . tags ) subq = subq . filter ( ( models . Tag . tag == first_tag ) ) for tag in tags : tag_alias = aliased ( models . Tag ) subq = subq . join ( tag_alias , models . Instance . tags ) subq = subq . filter ( ( tag_alias . tag == tag ) ) query_prefix = query_prefix . filter ( ( ~ models . Instance . uuid . in_ ( subq ) ) ) if ( 'not-tags-any' in filters ) : tags = filters . pop ( 'not-tags-any' ) query_prefix = query_prefix . filter ( ( ~ models . Instance . tags . any ( models . Tag . tag . in_ ( tags ) ) ) ) if ( not context . is_admin ) : if context . project_id : filters [ 'project_id' ] = context . project_id else : filters [ 'user_id' ] = context . user_id exact_match_filter_names = [ 'project_id' , 'user_id' , 'image_ref' , 'vm_state' , 'instance_type_id' , 'uuid' , 'metadata' , 'host' , 'task_state' , 'system_metadata' ] query_prefix = _exact_instance_filter ( query_prefix , filters , exact_match_filter_names ) if ( query_prefix is None ) : return [ ] query_prefix = _regex_instance_filter ( query_prefix , filters ) query_prefix = _tag_instance_filter ( context , query_prefix , filters ) if ( marker is not None ) : try : marker = _instance_get_by_uuid ( context . elevated ( read_deleted = 'yes' ) , marker ) except exception . InstanceNotFound : raise exception . MarkerNotFound ( marker = marker ) try : query_prefix = sqlalchemyutils . paginate_query ( query_prefix , models . Instance , limit , sort_keys , marker = marker , sort_dirs = sort_dirs ) except db_exc . InvalidSortKey : raise exception . InvalidSortKey ( ) return _instances_fill_metadata ( context , query_prefix . all ( ) , manual_joins )
get all instances that match all filters sorted by multiple keys .