Skip to content

Commit 86c5470

Browse files
dannosliwcdstephenfin
authored andcommitted
Use the SDK in server migration list
Update server migration list to use the OpenStack SDK instead of directly using the nova interface. Change-Id: I40dc95ee47e7c33ebf596f8ad437228b4bb0ab33
1 parent 6cd72f6 commit 86c5470

5 files changed

Lines changed: 97 additions & 84 deletions

File tree

openstackclient/compute/v2/server_migration.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import uuid
1616

1717
from novaclient import api_versions
18+
from openstack import utils as sdk_utils
1819
from osc_lib.command import command
1920
from osc_lib import exceptions
2021
from osc_lib import utils
@@ -130,22 +131,22 @@ def print_migrations(self, parsed_args, compute_client, migrations):
130131
# the same as the column header names.
131132
columns = [
132133
'source_node', 'dest_node', 'source_compute', 'dest_compute',
133-
'dest_host', 'status', 'instance_uuid', 'old_instance_type_id',
134-
'new_instance_type_id', 'created_at', 'updated_at',
134+
'dest_host', 'status', 'server_id', 'old_flavor_id',
135+
'new_flavor_id', 'created_at', 'updated_at',
135136
]
136137

137138
# Insert migrations UUID after ID
138-
if compute_client.api_version >= api_versions.APIVersion("2.59"):
139+
if sdk_utils.supports_microversion(compute_client, "2.59"):
139140
column_headers.insert(0, "UUID")
140141
columns.insert(0, "uuid")
141142

142-
if compute_client.api_version >= api_versions.APIVersion("2.23"):
143+
if sdk_utils.supports_microversion(compute_client, "2.23"):
143144
column_headers.insert(0, "Id")
144145
columns.insert(0, "id")
145146
column_headers.insert(len(column_headers) - 2, "Type")
146147
columns.insert(len(columns) - 2, "migration_type")
147148

148-
if compute_client.api_version >= api_versions.APIVersion("2.80"):
149+
if sdk_utils.supports_microversion(compute_client, "2.80"):
149150
if parsed_args.project:
150151
column_headers.insert(len(column_headers) - 2, "Project")
151152
columns.insert(len(columns) - 2, "project_id")
@@ -159,19 +160,23 @@ def print_migrations(self, parsed_args, compute_client, migrations):
159160
)
160161

161162
def take_action(self, parsed_args):
162-
compute_client = self.app.client_manager.compute
163+
compute_client = self.app.client_manager.sdk_connection.compute
163164
identity_client = self.app.client_manager.identity
164165

165-
search_opts = {
166-
'host': parsed_args.host,
167-
'status': parsed_args.status,
168-
}
166+
search_opts = {}
167+
168+
if parsed_args.host is not None:
169+
search_opts['host'] = parsed_args.host
170+
171+
if parsed_args.status is not None:
172+
search_opts['status'] = parsed_args.status
169173

170174
if parsed_args.server:
171-
search_opts['instance_uuid'] = utils.find_resource(
172-
compute_client.servers,
173-
parsed_args.server,
174-
).id
175+
server = compute_client.find_server(parsed_args.server)
176+
if server is None:
177+
msg = _('Unable to find server: %s') % parsed_args.server
178+
raise exceptions.CommandError(msg)
179+
search_opts['instance_uuid'] = server.id
175180

176181
if parsed_args.type:
177182
migration_type = parsed_args.type
@@ -181,7 +186,7 @@ def take_action(self, parsed_args):
181186
search_opts['migration_type'] = migration_type
182187

183188
if parsed_args.marker:
184-
if compute_client.api_version < api_versions.APIVersion('2.59'):
189+
if not sdk_utils.supports_microversion(compute_client, "2.59"):
185190
msg = _(
186191
'--os-compute-api-version 2.59 or greater is required to '
187192
'support the --marker option'
@@ -190,16 +195,17 @@ def take_action(self, parsed_args):
190195
search_opts['marker'] = parsed_args.marker
191196

192197
if parsed_args.limit:
193-
if compute_client.api_version < api_versions.APIVersion('2.59'):
198+
if not sdk_utils.supports_microversion(compute_client, "2.59"):
194199
msg = _(
195200
'--os-compute-api-version 2.59 or greater is required to '
196201
'support the --limit option'
197202
)
198203
raise exceptions.CommandError(msg)
199204
search_opts['limit'] = parsed_args.limit
205+
search_opts['paginated'] = False
200206

201207
if parsed_args.changes_since:
202-
if compute_client.api_version < api_versions.APIVersion('2.59'):
208+
if not sdk_utils.supports_microversion(compute_client, "2.59"):
203209
msg = _(
204210
'--os-compute-api-version 2.59 or greater is required to '
205211
'support the --changes-since option'
@@ -208,7 +214,7 @@ def take_action(self, parsed_args):
208214
search_opts['changes_since'] = parsed_args.changes_since
209215

210216
if parsed_args.changes_before:
211-
if compute_client.api_version < api_versions.APIVersion('2.66'):
217+
if not sdk_utils.supports_microversion(compute_client, "2.66"):
212218
msg = _(
213219
'--os-compute-api-version 2.66 or greater is required to '
214220
'support the --changes-before option'
@@ -217,7 +223,7 @@ def take_action(self, parsed_args):
217223
search_opts['changes_before'] = parsed_args.changes_before
218224

219225
if parsed_args.project:
220-
if compute_client.api_version < api_versions.APIVersion('2.80'):
226+
if not sdk_utils.supports_microversion(compute_client, "2.80"):
221227
msg = _(
222228
'--os-compute-api-version 2.80 or greater is required to '
223229
'support the --project option'
@@ -231,7 +237,7 @@ def take_action(self, parsed_args):
231237
).id
232238

233239
if parsed_args.user:
234-
if compute_client.api_version < api_versions.APIVersion('2.80'):
240+
if not sdk_utils.supports_microversion(compute_client, "2.80"):
235241
msg = _(
236242
'--os-compute-api-version 2.80 or greater is required to '
237243
'support the --user option'
@@ -244,7 +250,7 @@ def take_action(self, parsed_args):
244250
parsed_args.user_domain,
245251
).id
246252

247-
migrations = compute_client.migrations.list(**search_opts)
253+
migrations = list(compute_client.migrations(**search_opts))
248254

249255
return self.print_migrations(parsed_args, compute_client, migrations)
250256

openstackclient/tests/functional/compute/v2/test_server.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,3 +1267,9 @@ def test_server_add_remove_volume(self):
12671267

12681268
raw_output = self.openstack('server volume list ' + server_name)
12691269
self.assertEqual('\n', raw_output)
1270+
1271+
def test_server_migration_list(self):
1272+
# Verify that the command does not raise an exception when we list
1273+
# migrations, including when we specify a query.
1274+
self.openstack('server migration list')
1275+
self.openstack('server migration list --limit 1')

0 commit comments

Comments
 (0)