From 722be75f9cffec7242d893cac20d40c570af32d6 Mon Sep 17 00:00:00 2001 From: Huanxuan Ao Date: Wed, 10 Aug 2016 15:26:45 +0800 Subject: [PATCH] Implement "network agent set" command Add "network agent set" command in network v2 to set network agent properties. Also add the unit test, doc, functional test and release note. Change-Id: Iebaee4c60f8c6b43f538c1b82a38b93178b9ce53 Implements: bp implement-network-agents Co-Authored-By: Michael Gugino --- doc/source/command-objects/network-agent.rst | 30 ++++++++ .../tests/network/v2/test_network_agent.py | 9 +++ openstackclient/network/v2/network_agent.py | 41 ++++++++++ .../tests/network/v2/test_network_agent.py | 75 +++++++++++++++++++ ...ement-network-agents-5eba48796318f094.yaml | 4 +- setup.cfg | 1 + 6 files changed, 158 insertions(+), 2 deletions(-) diff --git a/doc/source/command-objects/network-agent.rst b/doc/source/command-objects/network-agent.rst index 32cb371cc1..15195a3398 100644 --- a/doc/source/command-objects/network-agent.rst +++ b/doc/source/command-objects/network-agent.rst @@ -36,6 +36,36 @@ List network agents os network agent list +network agent set +----------------- + +Set network agent properties + +.. program:: network agent set +.. code:: bash + + os network agent set + [--description ] + [--enable | --disable] + + +.. option:: --description + + Set network agent description + +.. option:: --enable + + Enable network agent + +.. option:: --disable + + Disable network agent + +.. _network_agent_set-network-agent: +.. describe:: + + Network agent to modify (ID only) + network agent show ------------------ diff --git a/functional/tests/network/v2/test_network_agent.py b/functional/tests/network/v2/test_network_agent.py index e01ead4229..f574c50c94 100644 --- a/functional/tests/network/v2/test_network_agent.py +++ b/functional/tests/network/v2/test_network_agent.py @@ -30,3 +30,12 @@ class NetworkAgentTests(test.TestCase): opts = self.get_opts(self.FIELDS) raw_output = self.openstack('network agent show ' + self.IDs[0] + opts) self.assertEqual(self.IDs[0] + "\n", raw_output) + + def test_network_agent_set(self): + opts = self.get_opts(['admin_state_up']) + self.openstack('network agent set --disable ' + self.IDs[0]) + raw_output = self.openstack('network agent show ' + self.IDs[0] + opts) + self.assertEqual("DOWN\n", raw_output) + self.openstack('network agent set --enable ' + self.IDs[0]) + raw_output = self.openstack('network agent show ' + self.IDs[0] + opts) + self.assertEqual("UP\n", raw_output) diff --git a/openstackclient/network/v2/network_agent.py b/openstackclient/network/v2/network_agent.py index 1fb70a50a0..fdb34bb79b 100644 --- a/openstackclient/network/v2/network_agent.py +++ b/openstackclient/network/v2/network_agent.py @@ -99,6 +99,47 @@ class ListNetworkAgent(command.Lister): ) for s in data)) +class SetNetworkAgent(command.Command): + """Set network agent properties""" + + def get_parser(self, prog_name): + parser = super(SetNetworkAgent, self).get_parser(prog_name) + parser.add_argument( + 'network_agent', + metavar="", + help=(_("Network agent to modify (ID only)")) + ) + parser.add_argument( + '--description', + metavar='', + help=_("Set network agent description") + ) + admin_group = parser.add_mutually_exclusive_group() + admin_group.add_argument( + '--enable', + action='store_true', + help=_("Enable network agent") + ) + admin_group.add_argument( + '--disable', + action='store_true', + help=_("Disable network agent") + ) + return parser + + def take_action(self, parsed_args): + client = self.app.client_manager.network + obj = client.get_agent(parsed_args.network_agent, ignore_missing=False) + attrs = {} + if parsed_args.description is not None: + attrs['description'] = str(parsed_args.description) + if parsed_args.enable: + attrs['admin_state_up'] = True + if parsed_args.disable: + attrs['admin_state_up'] = False + client.update_agent(obj, **attrs) + + class ShowNetworkAgent(command.ShowOne): """Display network agent details""" diff --git a/openstackclient/tests/network/v2/test_network_agent.py b/openstackclient/tests/network/v2/test_network_agent.py index 3cf9a53081..269d4e1d6d 100644 --- a/openstackclient/tests/network/v2/test_network_agent.py +++ b/openstackclient/tests/network/v2/test_network_agent.py @@ -160,6 +160,81 @@ class TestListNetworkAgent(TestNetworkAgent): self.assertEqual(self.data, list(data)) +class TestSetNetworkAgent(TestNetworkAgent): + + _network_agent = ( + network_fakes.FakeNetworkAgent.create_one_network_agent()) + + def setUp(self): + super(TestSetNetworkAgent, self).setUp() + self.network.update_agent = mock.Mock(return_value=None) + self.network.get_agent = mock.Mock(return_value=self._network_agent) + + # Get the command object to test + self.cmd = network_agent.SetNetworkAgent(self.app, self.namespace) + + def test_set_nothing(self): + arglist = [ + self._network_agent.id, + ] + verifylist = [ + ('network_agent', self._network_agent.id), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + result = self.cmd.take_action(parsed_args) + + attrs = {} + self.network.update_agent.assert_called_once_with( + self._network_agent, **attrs) + self.assertIsNone(result) + + def test_set_all(self): + arglist = [ + '--description', 'new_description', + '--enable', + self._network_agent.id, + ] + verifylist = [ + ('description', 'new_description'), + ('enable', True), + ('disable', False), + ('network_agent', self._network_agent.id), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + result = self.cmd.take_action(parsed_args) + + attrs = { + 'description': 'new_description', + 'admin_state_up': True, + } + self.network.update_agent.assert_called_once_with( + self._network_agent, **attrs) + self.assertIsNone(result) + + def test_set_with_disable(self): + arglist = [ + '--disable', + self._network_agent.id, + ] + verifylist = [ + ('enable', False), + ('disable', True), + ('network_agent', self._network_agent.id), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + result = self.cmd.take_action(parsed_args) + + attrs = { + 'admin_state_up': False, + } + self.network.update_agent.assert_called_once_with( + self._network_agent, **attrs) + self.assertIsNone(result) + + class TestShowNetworkAgent(TestNetworkAgent): _network_agent = ( diff --git a/releasenotes/notes/bp-implement-network-agents-5eba48796318f094.yaml b/releasenotes/notes/bp-implement-network-agents-5eba48796318f094.yaml index 4ca2510367..d9224c311f 100644 --- a/releasenotes/notes/bp-implement-network-agents-5eba48796318f094.yaml +++ b/releasenotes/notes/bp-implement-network-agents-5eba48796318f094.yaml @@ -1,5 +1,5 @@ --- features: - - Add ``network agent delete``, ``network agent list`` and - ``network agent show`` commands. + - Add ``network agent delete``, ``network agent list``, ``network agent show`` + and ``network agent set`` commands. [Blueprint `implement-network-agents `_] diff --git a/setup.cfg b/setup.cfg index d0c2648066..72a4589bea 100644 --- a/setup.cfg +++ b/setup.cfg @@ -357,6 +357,7 @@ openstack.network.v2 = network_agent_delete = openstackclient.network.v2.network_agent:DeleteNetworkAgent network_agent_list = openstackclient.network.v2.network_agent:ListNetworkAgent + network_agent_set = openstackclient.network.v2.network_agent:SetNetworkAgent network_agent_show = openstackclient.network.v2.network_agent:ShowNetworkAgent network_create = openstackclient.network.v2.network:CreateNetwork