Coverage for nova/api/openstack/compute/migrations.py: 93%
100 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.
13from oslo_utils import timeutils
14from webob import exc
16from nova.api.openstack import api_version_request
17from nova.api.openstack import common
18from nova.api.openstack.compute.schemas import migrations as schema_migrations
19from nova.api.openstack.compute.views import migrations as migrations_view
20from nova.api.openstack import wsgi
21from nova.api import validation
22from nova.compute import api as compute
23from nova import exception
24from nova.i18n import _
25from nova.objects import base as obj_base
26from nova.objects import fields
27from nova.policies import migrations as migrations_policies
30class MigrationsController(wsgi.Controller):
31 """Controller for accessing migrations in OpenStack API."""
33 _view_builder_class = migrations_view.ViewBuilder
34 _collection_name = "servers/%s/migrations"
36 def __init__(self):
37 super(MigrationsController, self).__init__()
38 self.compute_api = compute.API()
40 def _output(self, req, migrations_obj, add_link=False,
41 add_uuid=False, add_user_project=False):
42 """Returns the desired output of the API from an object.
44 From a MigrationsList's object this method returns a list of
45 primitive objects with the only necessary fields.
46 """
47 detail_keys = ['memory_total', 'memory_processed', 'memory_remaining',
48 'disk_total', 'disk_processed', 'disk_remaining']
50 # TODO(Shaohe Feng) we should share the in-progress list.
51 live_migration_in_progress = ['queued', 'preparing',
52 'running', 'post-migrating']
54 # Note(Shaohe Feng): We need to leverage the oslo.versionedobjects.
55 # Then we can pass the target version to it's obj_to_primitive.
56 objects = obj_base.obj_to_primitive(migrations_obj)
57 objects = [x for x in objects if not x['hidden']]
58 for obj in objects:
59 del obj['deleted']
60 del obj['deleted_at']
61 del obj['hidden']
62 del obj['cross_cell_move']
63 del obj['dest_compute_id']
64 if not add_uuid:
65 del obj['uuid']
66 if 'memory_total' in obj: 66 ↛ 69line 66 didn't jump to line 69 because the condition on line 66 was always true
67 for key in detail_keys:
68 del obj[key]
69 if not add_user_project:
70 if 'user_id' in obj: 70 ↛ 72line 70 didn't jump to line 72 because the condition on line 70 was always true
71 del obj['user_id']
72 if 'project_id' in obj: 72 ↛ 77line 72 didn't jump to line 77 because the condition on line 72 was always true
73 del obj['project_id']
74 # NOTE(Shaohe Feng) above version 2.23, add migration_type for all
75 # kinds of migration, but we only add links just for in-progress
76 # live-migration.
77 if (add_link and
78 obj['migration_type'] ==
79 fields.MigrationType.LIVE_MIGRATION and
80 obj["status"] in live_migration_in_progress):
81 obj["links"] = self._view_builder._get_links(
82 req, obj["id"],
83 self._collection_name % obj['instance_uuid'])
84 elif add_link is False:
85 del obj['migration_type']
87 return objects
89 def _index(self, req, add_link=False, next_link=False, add_uuid=False,
90 sort_dirs=None, sort_keys=None, limit=None, marker=None,
91 allow_changes_since=False, allow_changes_before=False):
92 context = req.environ['nova.context']
93 context.can(migrations_policies.POLICY_ROOT % 'index')
94 search_opts = {}
95 search_opts.update(req.GET)
96 if 'changes-since' in search_opts:
97 if allow_changes_since: 97 ↛ 106line 97 didn't jump to line 106 because the condition on line 97 was always true
98 search_opts['changes-since'] = timeutils.parse_isotime(
99 search_opts['changes-since'])
100 else:
101 # Before microversion 2.59, the changes-since filter was not
102 # supported in the DB API. However, the schema allowed
103 # additionalProperties=True, so a user could pass it before
104 # 2.59 and filter by the updated_at field if we don't remove
105 # it from search_opts.
106 del search_opts['changes-since']
108 if 'changes-before' in search_opts:
109 if allow_changes_before: 109 ↛ 123line 109 didn't jump to line 123 because the condition on line 109 was always true
110 search_opts['changes-before'] = timeutils.parse_isotime(
111 search_opts['changes-before'])
112 changes_since = search_opts.get('changes-since')
113 if (changes_since and search_opts['changes-before'] <
114 search_opts['changes-since']):
115 msg = _('The value of changes-since must be less than '
116 'or equal to changes-before.')
117 raise exc.HTTPBadRequest(explanation=msg)
118 else:
119 # Before microversion 2.59 the schema allowed
120 # additionalProperties=True, so a user could pass
121 # changes-before before 2.59 and filter by the updated_at
122 # field if we don't remove it from search_opts.
123 del search_opts['changes-before']
125 if sort_keys:
126 try:
127 migrations = self.compute_api.get_migrations_sorted(
128 context, search_opts,
129 sort_dirs=sort_dirs, sort_keys=sort_keys,
130 limit=limit, marker=marker)
131 except exception.MarkerNotFound as e:
132 raise exc.HTTPBadRequest(explanation=e.format_message())
133 else:
134 migrations = self.compute_api.get_migrations(
135 context, search_opts)
137 add_user_project = api_version_request.is_supported(req, '2.80')
138 migrations = self._output(req, migrations, add_link,
139 add_uuid, add_user_project)
140 migrations_dict = {'migrations': migrations}
142 if next_link:
143 migrations_links = self._view_builder.get_links(req, migrations)
144 if migrations_links: 144 ↛ 145line 144 didn't jump to line 145 because the condition on line 144 was never true
145 migrations_dict['migrations_links'] = migrations_links
146 return migrations_dict
148 @wsgi.Controller.api_version("2.1", "2.22") # noqa
149 @wsgi.expected_errors(())
150 @validation.query_schema(schema_migrations.list_query_schema_v20,
151 "2.0", "2.22")
152 def index(self, req):
153 """Return all migrations using the query parameters as filters."""
154 return self._index(req)
156 @wsgi.Controller.api_version("2.23", "2.58") # noqa
157 @wsgi.expected_errors(())
158 @validation.query_schema(schema_migrations.list_query_schema_v20,
159 "2.23", "2.58")
160 def index(self, req): # noqa
161 """Return all migrations using the query parameters as filters."""
162 return self._index(req, add_link=True)
164 @wsgi.Controller.api_version("2.59", "2.65") # noqa
165 @wsgi.expected_errors(400)
166 @validation.query_schema(schema_migrations.list_query_params_v259,
167 "2.59", "2.65")
168 def index(self, req): # noqa
169 """Return all migrations using the query parameters as filters."""
170 limit, marker = common.get_limit_and_marker(req)
171 return self._index(req, add_link=True, next_link=True, add_uuid=True,
172 sort_keys=['created_at', 'id'],
173 sort_dirs=['desc', 'desc'],
174 limit=limit, marker=marker,
175 allow_changes_since=True)
177 @wsgi.Controller.api_version("2.66") # noqa
178 @wsgi.expected_errors(400)
179 @validation.query_schema(schema_migrations.list_query_params_v266,
180 "2.66", "2.79")
181 @validation.query_schema(schema_migrations.list_query_params_v280,
182 "2.80")
183 def index(self, req): # noqa
184 """Return all migrations using the query parameters as filters."""
185 limit, marker = common.get_limit_and_marker(req)
186 return self._index(req, add_link=True, next_link=True, add_uuid=True,
187 sort_keys=['created_at', 'id'],
188 sort_dirs=['desc', 'desc'],
189 limit=limit, marker=marker,
190 allow_changes_since=True,
191 allow_changes_before=True)