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 <michael.gugino@walmart.com>
This commit is contained in:
Huanxuan Ao 2016-08-10 15:26:45 +08:00
parent 0b91368164
commit 722be75f9c
6 changed files with 158 additions and 2 deletions

View File

@ -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 <description>]
[--enable | --disable]
<network-agent>
.. option:: --description <discription>
Set network agent description
.. option:: --enable
Enable network agent
.. option:: --disable
Disable network agent
.. _network_agent_set-network-agent:
.. describe:: <network-agent>
Network agent to modify (ID only)
network agent show
------------------

View File

@ -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)

View File

@ -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="<network-agent>",
help=(_("Network agent to modify (ID only)"))
)
parser.add_argument(
'--description',
metavar='<description>',
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"""

View File

@ -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 = (

View File

@ -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 <https://blueprints.launchpad.net/python-openstackclient/+spec/implement-network-agents>`_]

View File

@ -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