From 1f63034441a63b968185e1678049c01205b8e6d7 Mon Sep 17 00:00:00 2001 From: whoami-rajat Date: Wed, 14 Sep 2022 11:01:31 +0100 Subject: [PATCH] compute: Require image when rebuilding a volume-backed server A volume-backed server will have no image attribute (or rather the image property will be set to the empty string). As such, if you want to try rebuild you will need to specify an image [*]. Enforce this. [*] Before microversion 2.93, this must be the same image. However, we don't touch on that here. This will be addressed later. Change-Id: I6842dabd7acb4e3a78f894e55e616625757eb6a4 Story: 2010297 Task: 46290 --- openstackclient/compute/v2/server.py | 14 +++++++++++--- .../tests/unit/compute/v2/test_server.py | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/openstackclient/compute/v2/server.py b/openstackclient/compute/v2/server.py index 0bcad8fc3c..24c158ae93 100644 --- a/openstackclient/compute/v2/server.py +++ b/openstackclient/compute/v2/server.py @@ -3111,13 +3111,21 @@ class RebuildServer(command.ShowOne): server = utils.find_resource( compute_client.servers, parsed_args.server) - # If parsed_args.image is not set, default to the currently used one. + # If parsed_args.image is not set and if the instance is image backed, + # default to the currently used one. If the instance is volume backed, + # it is not trivial to fetch the current image and probably better + # to error out in this case and ask user to supply the image. if parsed_args.image: image = image_client.find_image( parsed_args.image, ignore_missing=False) else: - image_id = server.to_dict().get('image', {}).get('id') - image = image_client.get_image(image_id) + if not server.image: + msg = _( + 'The --image option is required when rebuilding a ' + 'volume-backed server' + ) + raise exceptions.CommandError(msg) + image = image_client.get_image(server.image['id']) kwargs = {} diff --git a/openstackclient/tests/unit/compute/v2/test_server.py b/openstackclient/tests/unit/compute/v2/test_server.py index ba86dff33f..3daa4cc75e 100644 --- a/openstackclient/tests/unit/compute/v2/test_server.py +++ b/openstackclient/tests/unit/compute/v2/test_server.py @@ -5781,6 +5781,25 @@ class TestServerRebuild(TestServer): self.get_image_mock.assert_called_with(self.image.id) self.server.rebuild.assert_called_with(self.image, None) + def test_rebuild_with_volume_backed_server_no_image(self): + # the volume-backed server will have the image attribute set to an + # empty string, not null/None + self.server.image = '' + + arglist = [ + self.server.id, + ] + verifylist = [ + ('server', self.server.id), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + exc = self.assertRaises( + exceptions.CommandError, + self.cmd.take_action, + parsed_args) + self.assertIn('The --image option is required', str(exc)) + def test_rebuild_with_name(self): name = 'test-server-xxx' arglist = [