diff --git a/neutron_lib/db/utils.py b/neutron_lib/db/utils.py index 8133335c8..28d6ffd30 100644 --- a/neutron_lib/db/utils.py +++ b/neutron_lib/db/utils.py @@ -167,3 +167,21 @@ def model_query_scope_is_project(context, model): # query will be scoped to a single project_id return ((not context.is_admin and hasattr(model, 'project_id')) and (not context.is_advsvc and hasattr(model, 'project_id'))) + + +def model_query(context, model): + """Query the context for the said model. + + :param context: The context to use for the query. + :param model: The model to query for. + :returns: A query from the said context for the said model. + """ + query = context.session.query(model) + # define basic filter condition for model query + query_filter = None + if model_query_scope_is_project(context, model): + query_filter = (model.tenant_id == context.tenant_id) + + if query_filter is not None: + query = query.filter(query_filter) + return query diff --git a/releasenotes/notes/rehome-modelquery-2079a43163def870.yaml b/releasenotes/notes/rehome-modelquery-2079a43163def870.yaml new file mode 100644 index 000000000..49ec3c52b --- /dev/null +++ b/releasenotes/notes/rehome-modelquery-2079a43163def870.yaml @@ -0,0 +1,3 @@ +--- +features: + - The ``model_query`` function is now available in ``neutron_lib.db.utils``.