neutron_lib.db.model_query module

NOTE: This module is a temporary shim until networking projects move to

versioned objects at which point this module shouldn’t be needed.

neutron_lib.db.model_query.apply_filters(query, model, filters, context=None)

Apply filters to a query.

Parameters:
  • query – The query to apply filters to.

  • model – The model for the query.

  • filters – The filters to apply.

  • context – The context to use for the DB session.

Returns:

The query with filters applied to it.

neutron_lib.db.model_query.get_by_id(context, model, object_id, lazy_fields=None)

Query the said model with the given context for a specific object.

Parameters:
  • context – The context to use in the query.

  • model – The model to query.

  • object_id – The ID of the object to query for.

  • lazy_fields – list of fields for lazy loading

Returns:

The object with the give object_id for the said model.

neutron_lib.db.model_query.get_collection(context, model, dict_func, filters=None, fields=None, sorts=None, limit=None, marker_obj=None, page_reverse=False, lazy_fields=None)

Get a collection for a said model.

Parameters:
  • context – The context to use for the DB session.

  • model – The model for the collection.

  • dict_func – The function used to build the collection dict.

  • filters – The filters to apply.

  • fields – The fields to collect.

  • sorts – The sort keys to use.

  • limit – The limit for the query if applicable.

  • marker_obj – The marker object if applicable.

  • page_reverse – If reverse paging should be used.

  • lazy_fields – list of fields for lazy loading

Returns:

A list of dicts where each dict is an object in the collection.

neutron_lib.db.model_query.get_collection_count(context, model, filters=None, query_field=None)

Get the count for a specific collection.

Parameters:
  • context – The context to use for the DB session.

  • model – The model for the query.

  • filters – The filters to apply.

  • query_field – Column, in string format, from the “model”; the query will return only this parameter instead of the full model columns.

Returns:

The number of objects for said model with filters applied.

neutron_lib.db.model_query.get_collection_query(context, model, filters=None, sorts=None, limit=None, marker_obj=None, page_reverse=False, field=None, lazy_fields=None)

Get a collection query.

Parameters:
  • context – The context to use for the DB session.

  • model – The model to use.

  • filters – The filters to apply in the query.

  • sorts – The sort keys to use.

  • limit – The limit associated with the query.

  • marker_obj – The marker object if applicable.

  • page_reverse – If reverse paging should be used.

  • field – Column, in string format, from the “model”; the query will return only this parameter instead of the full model columns.

  • lazy_fields – list of fields for lazy loading

Returns:

A paginated query for the said model.

neutron_lib.db.model_query.get_hooks(model)

Retrieve the model query hooks for a model.

Parameters:

model – The DB Model to look up for query hooks.

Returns:

list of hooks

neutron_lib.db.model_query.get_rbac_actions(model)

Retrieve and combine all RBAC actions requested in a model

Parameters:

model – The DB Model to look up for query hooks.

Returns:

A set of RBAC actions defined in the model or the default RBAC actions (‘access_as_shared’, ‘access_as_readonly’)

neutron_lib.db.model_query.get_values(context, model, field, filters=None)
neutron_lib.db.model_query.query_with_hooks(context, model, field=None, lazy_fields=None)

Query with hooks using the said context and model.

Parameters:
  • context – The context to use for the DB session.

  • model – The model to query.

  • field – The column.

  • lazy_fields – list of fields for lazy loading

Returns:

The query with hooks applied to it.

neutron_lib.db.model_query.register_hook(model, name, query_hook, filter_hook, result_filters=None, rbac_actions=None)

Register a hook to be invoked when a query is executed.

Adds the hook components to the _model_query_hooks dict. Models are the keys of this dict, whereas the value is another dict mapping hook names to callables performing the hook or in the case of rbac_actions, a set of RBAC actions to filter the model.

Parameters:
  • model – The DB Model that the hook applies to.

  • name – A name for the hook.

  • query_hook – The method to be called to augment the query.

  • filter_hook – A method to be called to augment the query filter.

  • result_filters – A Method to be called to filter the query result.

  • rbac_actions – An iterable of RBAC actions or a single one (string).

Returns:

None.