From 545934f16d923d8e3aebf4572276a1fe5498fdd3 Mon Sep 17 00:00:00 2001 From: Lan Qi song Date: Wed, 15 Apr 2015 21:34:26 +0800 Subject: [PATCH] Support update a service Add the support to update a service. Using the following command: magnum service-update [] It depends on this patch: https://review.openstack.org/#/c/174208/ Change-Id: I432df6d94394bf00b298bfe6cbd7994812bc7106 Partial-Bug: #1444383 --- magnumclient/tests/test_shell_args.py | 11 +++++++++++ magnumclient/tests/v1/test_shell.py | 16 ++++++++++++++++ magnumclient/v1/shell.py | 26 ++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/magnumclient/tests/test_shell_args.py b/magnumclient/tests/test_shell_args.py index ca5b415b..c4369b35 100644 --- a/magnumclient/tests/test_shell_args.py +++ b/magnumclient/tests/test_shell_args.py @@ -526,6 +526,17 @@ class TestCommandLineArgument(utils.TestCase): self._test_arg_failure('service-show', self._few_argument_error) self.assertFalse(mock_show.called) + @mock.patch('magnumclient.v1.services.ServiceManager.update') + def test_service_update_success(self, mock_update): + self._test_arg_success('service-update xxx replace xxx=xxx') + self.assertTrue(mock_update.called) + self.assertEqual(1, mock_update.call_count) + + @mock.patch('magnumclient.v1.services.ServiceManager.update') + def test_service_update_failure_no_arg(self, mock_update): + self._test_arg_failure('service-update', self._few_argument_error) + self.assertFalse(mock_update.called) + @mock.patch('magnumclient.v1.containers.ContainerManager.list') def test_container_list_success(self, mock_list): self._test_arg_success('container-list') diff --git a/magnumclient/tests/v1/test_shell.py b/magnumclient/tests/v1/test_shell.py index b072690e..c307b1b6 100644 --- a/magnumclient/tests/v1/test_shell.py +++ b/magnumclient/tests/v1/test_shell.py @@ -278,6 +278,22 @@ class ShellTest(base.TestCase): client_mock.services.create.assert_called_once_with( manifest_url=manifest_url, bay_uuid=bay.uuid) + def test_do_service_update(self): + client_mock = mock.MagicMock() + args = mock.MagicMock() + service_id = 'id' + args.service = service_id + op = 'replace' + args.op = op + attributes = 'manifest={}' + args.attributes = attributes + shell.magnum_utils.args_array_to_patch = mock.MagicMock() + patch = [{'path': '/manifest', 'value': '{}', 'op': 'replace'}] + shell.magnum_utils.args_array_to_patch.return_value = patch + + shell.do_service_update(client_mock, args) + client_mock.services.update.assert_called_once_with(service_id, patch) + def test_do_service_delete(self): client_mock = mock.MagicMock() args = mock.MagicMock() diff --git a/magnumclient/v1/shell.py b/magnumclient/v1/shell.py index c6a09c17..048ee153 100644 --- a/magnumclient/v1/shell.py +++ b/magnumclient/v1/shell.py @@ -421,6 +421,32 @@ def do_service_create(cs, args): _show_service(service) +@utils.arg('service', metavar='', help="UUID or name of service") +@utils.arg( + 'op', + metavar='', + choices=['add', 'replace', 'remove'], + help="Operations: 'add', 'replace' or 'remove'") +@utils.arg( + 'attributes', + metavar='', + nargs='+', + action='append', + default=[], + help="Attributes to add/replace or remove " + "(only PATH is necessary on remove)") +def do_service_update(cs, args): + """Update information about the given service.""" + patch = magnum_utils.args_array_to_patch(args.op, args.attributes[0]) + p = patch[0] + if p['path'] == '/manifest' and os.path.isfile(p['value']): + with open(p['value'], 'r') as f: + p['value'] = f.read() + + service = cs.services.update(args.service, patch) + _show_service(service) + + @utils.arg('services', metavar='', nargs='+',