Support rebuild instance CLI

Change-Id: Ieae9216033d1c9ce0078554c7519bc891f50dc35
This commit is contained in:
Lingxian Kong
2020-08-06 20:46:00 +12:00
parent c23da586e9
commit 09aff4df80
5 changed files with 58 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
---
features:
- |
Support to rebuild instance.
.. code-block:: console
openstack database instance rebuild <instance> <new-image-id>

View File

@@ -75,6 +75,7 @@ openstack.database.v1 =
database_instance_update = troveclient.osc.v1.database_instances:UpdateDatabaseInstance
database_instance_upgrade = troveclient.osc.v1.database_instances:UpgradeDatabaseInstance
database_instance_reboot = troveclient.osc.v1.database_instances:RebootDatabaseInstance
database_instance_rebuild = troveclient.osc.v1.database_instances:RebuildDatabaseInstance
database_instance_detach = troveclient.osc.v1.database_instances:DetachDatabaseInstanceReplica
database_instance_eject = troveclient.osc.v1.database_instances:EjectDatabaseInstanceReplicaSource
database_instance_promote = troveclient.osc.v1.database_instances:PromoteDatabaseInstanceToReplicaSource

View File

@@ -725,3 +725,31 @@ class RebootDatabaseInstance(command.Command):
mgmt_instance_mgr = self.app.client_manager.database.mgmt_instances
mgmt_instance_mgr.reboot(instance_id)
class RebuildDatabaseInstance(command.Command):
_description = _("Rebuilds an instance(the Nova server).")
def get_parser(self, prog_name):
parser = super(RebuildDatabaseInstance, self).get_parser(prog_name)
parser.add_argument(
'instance',
metavar='<instance>',
type=str,
help=_('ID or name of the instance.'))
parser.add_argument(
'image',
metavar='<image-id>',
help=_('ID of the new guest image.'))
return parser
def take_action(self, parsed_args):
instance_id = parsed_args.instance
if not uuidutils.is_uuid_like(instance_id):
instance_mgr = self.app.client_manager.database.instances
instance_id = osc_utils.find_resource(instance_mgr, instance_id)
mgmt_instance_mgr = self.app.client_manager.database.mgmt_instances
mgmt_instance_mgr.rebuild(instance_id, parsed_args.image)

View File

@@ -446,3 +446,19 @@ class TestDatabaseInstanceReboot(TestInstances):
self.cmd.take_action(parsed_args)
self.mgmt_client.reboot.assert_called_with('instance1')
class TestDatabaseInstanceRebuild(TestInstances):
def setUp(self):
super(TestDatabaseInstanceRebuild, self).setUp()
self.cmd = database_instances.RebuildDatabaseInstance(self.app, None)
@mock.patch.object(utils, 'find_resource')
def test_instance_rebuild(self, mock_find):
args = ['instance1', 'image_id']
mock_find.return_value = args[0]
parsed_args = self.check_parser(self.cmd, args, [])
self.cmd.take_action(parsed_args)
self.mgmt_client.rebuild.assert_called_with('instance1', 'image_id')

View File

@@ -102,6 +102,11 @@ class Management(base.ManagerWithFind):
body = {'reset-task-status': {}}
self._action(instance_id, body)
def rebuild(self, instance_id, image_id):
"""Rebuild the underlying OS."""
body = {'rebuild': {'image_id': image_id}}
self._action(instance_id, body)
class MgmtClusters(base.ManagerWithFind):
"""Manage :class:`Cluster` resources."""