Coverage for nova/db/utils.py: 100%
37 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-04-17 15:08 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-04-17 15:08 +0000
1# Licensed under the Apache License, Version 2.0 (the "License"); you may
2# not use this file except in compliance with the License. You may obtain
3# a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10# License for the specific language governing permissions and limitations
11# under the License.
13import functools
14import inspect
16import nova.context
17from nova import exception
18from nova.i18n import _
21def require_context(f):
22 """Decorator to require *any* user or admin context.
24 This does no authorization for user or project access matching, see
25 :py:func:`nova.context.authorize_project_context` and
26 :py:func:`nova.context.authorize_user_context`.
28 The first argument to the wrapped function must be the context.
30 """
32 @functools.wraps(f)
33 def wrapper(*args, **kwargs):
34 nova.context.require_context(args[0])
35 return f(*args, **kwargs)
36 wrapper.__signature__ = inspect.signature(f)
37 return wrapper
40def process_sort_params(
41 sort_keys,
42 sort_dirs,
43 default_keys=['created_at', 'id'],
44 default_dir='asc',
45):
46 """Process the sort parameters to include default keys.
48 Creates a list of sort keys and a list of sort directions. Adds the default
49 keys to the end of the list if they are not already included.
51 When adding the default keys to the sort keys list, the associated
52 direction is:
54 1. The first element in the 'sort_dirs' list (if specified), else
55 2. 'default_dir' value (Note that 'asc' is the default value since this is
56 the default in sqlalchemy.utils.paginate_query)
58 :param sort_keys: List of sort keys to include in the processed list
59 :param sort_dirs: List of sort directions to include in the processed list
60 :param default_keys: List of sort keys that need to be included in the
61 processed list, they are added at the end of the list if not already
62 specified.
63 :param default_dir: Sort direction associated with each of the default
64 keys that are not supplied, used when they are added to the processed
65 list
66 :returns: list of sort keys, list of sort directions
67 :raise exception.InvalidInput: If more sort directions than sort keys
68 are specified or if an invalid sort direction is specified
69 """
70 # Determine direction to use for when adding default keys
71 if sort_dirs and len(sort_dirs) != 0:
72 default_dir_value = sort_dirs[0]
73 else:
74 default_dir_value = default_dir
76 # Create list of keys (do not modify the input list)
77 if sort_keys:
78 result_keys = list(sort_keys)
79 else:
80 result_keys = []
82 # If a list of directions is not provided, use the default sort direction
83 # for all provided keys
84 if sort_dirs:
85 result_dirs = []
86 # Verify sort direction
87 for sort_dir in sort_dirs:
88 if sort_dir not in ('asc', 'desc'):
89 msg = _("Unknown sort direction, must be 'desc' or 'asc'")
90 raise exception.InvalidInput(reason=msg)
91 result_dirs.append(sort_dir)
92 else:
93 result_dirs = [default_dir_value for _sort_key in result_keys]
95 # Ensure that the key and direction length match
96 while len(result_dirs) < len(result_keys):
97 result_dirs.append(default_dir_value)
98 # Unless more direction are specified, which is an error
99 if len(result_dirs) > len(result_keys):
100 msg = _("Sort direction size exceeds sort key size")
101 raise exception.InvalidInput(reason=msg)
103 # Ensure defaults are included
104 for key in default_keys:
105 if key not in result_keys:
106 result_keys.append(key)
107 result_dirs.append(default_dir_value)
109 return result_keys, result_dirs