Add backup-create to OSC

This change adds database support to the python-openstackclient
project for the backup-create command.

The trove command backup-create is now:
    openstack database backup create

Change-Id: I0dedce04efa9d2af4455bb6a94a96b6cbfc77bab
Partially-Implements: blueprint trove-support-in-python-openstackclient
This commit is contained in:
zhanggang 2017-12-21 21:43:13 -05:00
parent 896e1cb143
commit 504918fc63
4 changed files with 120 additions and 0 deletions

View File

@ -0,0 +1,5 @@
---
features:
- |
The command ``trove backup-create`` is now available to use in
the python-openstackclient CLI as ``openstack database backup create``

View File

@ -30,6 +30,7 @@ openstack.cli.extension =
database = troveclient.osc.plugin
openstack.database.v1 =
database_backup_create= troveclient.osc.v1.database_backups:CreateDatabaseBackup
database_backup_delete = troveclient.osc.v1.database_backups:DeleteDatabaseBackup
database_backup_list = troveclient.osc.v1.database_backups:ListDatabaseBackups
database_backup_show = troveclient.osc.v1.database_backups:ShowDatabaseBackup

View File

@ -120,3 +120,56 @@ class DeleteDatabaseBackup(command.Command):
msg = (_("Failed to delete backup %(backup)s: %(e)s")
% {'backup': parsed_args.backup, 'e': e})
raise exceptions.CommandError(msg)
class CreateDatabaseBackup(command.ShowOne):
_description = _("Creates a backup of an instance.")
def get_parser(self, prog_name):
parser = super(CreateDatabaseBackup, self).get_parser(prog_name)
parser.add_argument(
'instance',
metavar='<instance>',
help=_('ID or name of the instance.')
)
parser.add_argument(
'name',
metavar='<name>',
help=_('Name of the backup.')
)
parser.add_argument(
'--description',
metavar='<description>',
default=None,
help=_('An optional description for the backup.')
)
parser.add_argument(
'--parent',
metavar='<parent>',
default=None,
help=_('Optional ID of the parent backup to perform an'
' incremental backup from.')
)
parser.add_argument(
'--incremental',
action='store_true',
default=False,
help=_('Create an incremental backup based on the last'
' full or incremental backup. It will create a'
' full backup if no existing backup found.')
)
return parser
def take_action(self, parsed_args):
manager = self.app.client_manager.database
database_backups = manager.backups
instance = osc_utils.find_resource(manager.instances,
parsed_args.instance)
backup = database_backups.create(parsed_args.name,
instance,
description=parsed_args.description,
parent_id=parsed_args.parent,
incremental=parsed_args.incremental)
backup = set_attributes_for_print_detail(backup)
return zip(*sorted(six.iteritems(backup)))

View File

@ -115,3 +115,64 @@ class TestDatabaseBackupDelete(TestBackups):
self.assertRaises(exceptions.CommandError,
self.cmd.take_action,
parsed_args)
class TestBackupCreate(TestBackups):
values = ('2015-05-16T14:22:28', 'mysql', '5.6', 'v-56', None, 'bk-1234',
'1234',
'http://backup_srvr/database_backups/bk-1234.xbstream.gz.enc',
'bkp_1', None, 0.11, 'COMPLETED', '2015-05-16T14:23:08')
def setUp(self):
super(TestBackupCreate, self).setUp()
self.cmd = database_backups.CreateDatabaseBackup(self.app, None)
self.data = self.fake_backups.get_backup_bk_1234()
self.backup_client.create.return_value = self.data
self.columns = (
'created',
'datastore',
'datastore_version',
'datastore_version_id',
'description',
'id',
'instance_id',
'locationRef',
'name',
'parent_id',
'size',
'status',
'updated',
)
def test_backup_create_return_value(self):
args = ['1234', 'bk-1234']
parsed_args = self.check_parser(self.cmd, args, [])
columns, data = self.cmd.take_action(parsed_args)
self.assertEqual(self.columns, columns)
self.assertEqual(self.values, data)
@mock.patch.object(utils, 'find_resource')
def test_backup_create(self, mock_find):
args = ['1234', 'bk-1234-1']
mock_find.return_value = args[0]
parsed_args = self.check_parser(self.cmd, args, [])
self.cmd.take_action(parsed_args)
self.backup_client.create.assert_called_with('bk-1234-1',
'1234',
description=None,
parent_id=None,
incremental=False)
@mock.patch.object(utils, 'find_resource')
def test_incremental_backup_create(self, mock_find):
args = ['1234', 'bk-1234-2', '--description', 'backup 1234',
'--parent', '1234-1', '--incremental']
mock_find.return_value = args[0]
parsed_args = self.check_parser(self.cmd, args, [])
self.cmd.take_action(parsed_args)
self.backup_client.create.assert_called_with('bk-1234-2',
'1234',
description='backup 1234',
parent_id='1234-1',
incremental=True)