Switch server image create to SDK

Switch the server image create command from novaclient to SDK. Use the
SDK versions of test fakes to support fake Server resources. Also,
fetch updated image *after* waiting. If a user requests that we wait
(--wait) for a server image to become active before returning, then we
should probably return the final image. If we don't then the image can
appear to be in a non-active state when it fact it's active. Correct
this by fetching the image after the wait call.

Change-Id: I83a403c035add9ab041ed6d59b5b29e42267f143
This commit is contained in:
Thrivikram Mudunuri 2021-10-28 18:05:03 -04:00
parent f824e13bc5
commit 9acbd3e105
No known key found for this signature in database
GPG Key ID: 62A420BADBFB4875
3 changed files with 25 additions and 24 deletions

View File

@ -73,25 +73,23 @@ class CreateServerImage(command.ShowOne):
self.app.stdout.write('\rProgress: %s' % progress) self.app.stdout.write('\rProgress: %s' % progress)
self.app.stdout.flush() self.app.stdout.flush()
compute_client = self.app.client_manager.compute compute_client = self.app.client_manager.sdk_connection.compute
image_client = self.app.client_manager.image
server = utils.find_resource( server = compute_client.find_server(
compute_client.servers, parsed_args.server, ignore_missing=False,
parsed_args.server,
) )
if parsed_args.name: if parsed_args.name:
image_name = parsed_args.name image_name = parsed_args.name
else: else:
image_name = server.name image_name = server.name
image_id = compute_client.servers.create_image( image_id = compute_client.create_server_image(
server.id, server.id,
image_name, image_name,
parsed_args.properties, parsed_args.properties,
) ).id
image_client = self.app.client_manager.image
image = image_client.find_image(image_id)
if parsed_args.wait: if parsed_args.wait:
if utils.wait_for_status( if utils.wait_for_status(
@ -105,6 +103,8 @@ class CreateServerImage(command.ShowOne):
_('Error creating server image: %s'), parsed_args.server) _('Error creating server image: %s'), parsed_args.server)
raise exceptions.CommandError raise exceptions.CommandError
image = image_client.find_image(image_id, ignore_missing=False)
if self.app.client_manager._api_version['image'] == '1': if self.app.client_manager._api_version['image'] == '1':
info = {} info = {}
info.update(image._info) info.update(image._info)

View File

@ -27,8 +27,9 @@ class TestServerImage(compute_fakes.TestComputev2):
super(TestServerImage, self).setUp() super(TestServerImage, self).setUp()
# Get a shortcut to the compute client ServerManager Mock # Get a shortcut to the compute client ServerManager Mock
self.servers_mock = self.app.client_manager.compute.servers self.app.client_manager.sdk_connection = mock.Mock()
self.servers_mock.reset_mock() self.app.client_manager.sdk_connection.compute = mock.Mock()
self.sdk_client = self.app.client_manager.sdk_connection.compute
# Get a shortcut to the image client ImageManager Mock # Get a shortcut to the image client ImageManager Mock
self.images_mock = self.app.client_manager.image self.images_mock = self.app.client_manager.image
@ -41,14 +42,14 @@ class TestServerImage(compute_fakes.TestComputev2):
self.methods = {} self.methods = {}
def setup_servers_mock(self, count): def setup_servers_mock(self, count):
servers = compute_fakes.FakeServer.create_servers( servers = compute_fakes.FakeServer.create_sdk_servers(
attrs=self.attrs, attrs=self.attrs,
methods=self.methods, methods=self.methods,
count=count, count=count,
) )
# This is the return value for utils.find_resource() # This is the return value for compute_client.find_server()
self.servers_mock.get = compute_fakes.FakeServer.get_servers( self.sdk_client.find_server = compute_fakes.FakeServer.get_servers(
servers, servers,
0, 0,
) )
@ -104,8 +105,8 @@ class TestServerImageCreate(TestServerImage):
) )
self.images_mock.find_image = mock.Mock(side_effect=images) self.images_mock.find_image = mock.Mock(side_effect=images)
self.servers_mock.create_image = mock.Mock( self.sdk_client.create_server_image = mock.Mock(
return_value=images[0].id, return_value=images[0],
) )
return images return images
@ -126,8 +127,7 @@ class TestServerImageCreate(TestServerImage):
# data to be shown. # data to be shown.
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
# ServerManager.create_image(server, image_name, metadata=) self.sdk_client.create_server_image.assert_called_with(
self.servers_mock.create_image.assert_called_with(
servers[0].id, servers[0].id,
servers[0].name, servers[0].name,
None, None,
@ -157,8 +157,7 @@ class TestServerImageCreate(TestServerImage):
# data to be shown. # data to be shown.
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
# ServerManager.create_image(server, image_name, metadata=) self.sdk_client.create_server_image.assert_called_with(
self.servers_mock.create_image.assert_called_with(
servers[0].id, servers[0].id,
'img-nam', 'img-nam',
{'key': 'value'}, {'key': 'value'},
@ -188,8 +187,7 @@ class TestServerImageCreate(TestServerImage):
parsed_args, parsed_args,
) )
# ServerManager.create_image(server, image_name, metadata=) self.sdk_client.create_server_image.assert_called_with(
self.servers_mock.create_image.assert_called_with(
servers[0].id, servers[0].id,
servers[0].name, servers[0].name,
None, None,
@ -221,8 +219,7 @@ class TestServerImageCreate(TestServerImage):
# data to be shown. # data to be shown.
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
# ServerManager.create_image(server, image_name, metadata=) self.sdk_client.create_server_image.assert_called_with(
self.servers_mock.create_image.assert_called_with(
servers[0].id, servers[0].id,
servers[0].name, servers[0].name,
None, None,

View File

@ -0,0 +1,4 @@
---
features:
- |
Migrate openstack server image create from novaclient to sdk.