diff --git a/doc/source/command-objects/network-agent.rst b/doc/source/command-objects/network-agent.rst index b95f2e5074..947e07cf6c 100644 --- a/doc/source/command-objects/network-agent.rst +++ b/doc/source/command-objects/network-agent.rst @@ -35,6 +35,19 @@ List network agents .. code:: bash openstack network agent list + [--agent-type ] + [--host ] + +.. option:: --agent-type + + List only agents with the specified agent type. + The supported agent types are: dhcp, open-vswitch, + linux-bridge, ofa, l3, loadbalancer, metering, + metadata, macvtap, nic. + +.. option:: --host + + List only agents running on the specified host network agent set ----------------- diff --git a/openstackclient/network/v2/network_agent.py b/openstackclient/network/v2/network_agent.py index 6570849962..b3411166fa 100644 --- a/openstackclient/network/v2/network_agent.py +++ b/openstackclient/network/v2/network_agent.py @@ -72,6 +72,25 @@ class DeleteNetworkAgent(command.Command): class ListNetworkAgent(command.Lister): _description = _("List network agents") + def get_parser(self, prog_name): + parser = super(ListNetworkAgent, self).get_parser(prog_name) + parser.add_argument( + '--agent-type', + metavar='', + choices=["dhcp", "open-vswitch", "linux-bridge", "ofa", "l3", + "loadbalancer", "metering", "metadata", "macvtap", "nic"], + help=_("List only agents with the specified agent type. " + "The supported agent types are: dhcp, open-vswitch, " + "linux-bridge, ofa, l3, loadbalancer, metering, " + "metadata, macvtap, nic.") + ) + parser.add_argument( + '--host', + metavar='', + help=_("List only agents running on the specified host") + ) + return parser + def take_action(self, parsed_args): client = self.app.client_manager.network columns = ( @@ -92,7 +111,27 @@ class ListNetworkAgent(command.Lister): 'State', 'Binary' ) - data = client.agents() + + key_value = { + 'dhcp': 'DHCP agent', + 'open-vswitch': 'Open vSwitch agent', + 'linux-bridge': 'Linux bridge agent', + 'ofa': 'OFA driver agent', + 'l3': 'L3 agent', + 'loadbalancer': 'Loadbalancer agent', + 'metering': 'Metering agent', + 'metadata': 'Metadata agent', + 'macvtap': 'Macvtap agent', + 'nic': 'NIC Switch agent' + } + + filters = {} + if parsed_args.agent_type is not None: + filters['agent_type'] = key_value[parsed_args.agent_type] + if parsed_args.host is not None: + filters['host'] = parsed_args.host + + data = client.agents(**filters) return (column_headers, (utils.get_item_properties( s, columns, formatters=_formatters, diff --git a/openstackclient/tests/unit/network/v2/test_network_agent.py b/openstackclient/tests/unit/network/v2/test_network_agent.py index 9f5b442a82..9fd395b48b 100644 --- a/openstackclient/tests/unit/network/v2/test_network_agent.py +++ b/openstackclient/tests/unit/network/v2/test_network_agent.py @@ -130,6 +130,7 @@ class TestListNetworkAgent(TestNetworkAgent): ) data = [] for agent in network_agents: + agent.agent_type = 'DHCP agent' data.append(( agent.id, agent.agent_type, @@ -159,6 +160,40 @@ class TestListNetworkAgent(TestNetworkAgent): self.assertEqual(self.columns, columns) self.assertEqual(self.data, list(data)) + def test_network_agents_list_agent_type(self): + arglist = [ + '--agent-type', 'dhcp', + ] + verifylist = [ + ('agent_type', 'dhcp'), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + self.network.agents.assert_called_once_with(**{ + 'agent_type': self.network_agents[0].agent_type, + }) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, list(data)) + + def test_network_agents_list_host(self): + arglist = [ + '--host', self.network_agents[0].host, + ] + verifylist = [ + ('host', self.network_agents[0].host), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + self.network.agents.assert_called_once_with(**{ + 'host': self.network_agents[0].host, + }) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, list(data)) + class TestSetNetworkAgent(TestNetworkAgent): diff --git a/releasenotes/notes/bug-1641868-97c284e33f944c2d.yaml b/releasenotes/notes/bug-1641868-97c284e33f944c2d.yaml new file mode 100644 index 0000000000..0eb7e11da9 --- /dev/null +++ b/releasenotes/notes/bug-1641868-97c284e33f944c2d.yaml @@ -0,0 +1,6 @@ +--- +features: + - | + Add filters ``--agent-type`` and ``--host`` + to ``network agent list`` command + [Bug `1641868 `_]