Skip to content

Commit 0736336

Browse files
Michael GuginoHuanxuan Ao
andcommitted
Implement network agents functionality
python-neutronclient implements the following command set: agent-list, agent-show, agent-delete These commands display and modify various network agents and their information. python-openstacksdk has supported the api calls for these commands, but python-openstackclient does not implement these commands. This commit adds support for the following commands: openstack network agent list openstack network agent show <agent> openstack network agent delete <agent> Change-Id: I83ede6f89c37e7bdc38d7e9e7bb9d80e94c8becc Implements: blueprint implement-network-agents Depends-On: I9755637f76787d5fac8ff295ae273b308fcb98d0 Co-Authored-By: Huanxuan Ao <huanxuan.ao@easystack.cn>
1 parent 5f62572 commit 0736336

8 files changed

Lines changed: 500 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
=============
2+
network agent
3+
=============
4+
5+
A **network agent** is an agent that handles various tasks used to
6+
implement virtual networks. These agents include neutron-dhcp-agent,
7+
neutron-l3-agent, neutron-metering-agent, and neutron-lbaas-agent,
8+
among others. The agent is available when the alive status of the
9+
agent is "True".
10+
11+
Network v2
12+
13+
network agent delete
14+
--------------------
15+
16+
Delete network agent(s)
17+
18+
.. program:: network agent delete
19+
.. code:: bash
20+
21+
os network agent delete
22+
<network-agent> [<network-agent> ...]
23+
24+
.. _network_agent_delete-network-agent:
25+
.. describe:: <network-agent>
26+
27+
Network agent(s) to delete (ID only)
28+
29+
network agent list
30+
------------------
31+
32+
List network agents
33+
34+
.. program:: network agent list
35+
.. code:: bash
36+
37+
os network agent list
38+
39+
network agent show
40+
------------------
41+
42+
Display network agent details
43+
44+
.. program:: network agent show
45+
.. code:: bash
46+
47+
os network agent show
48+
<network-agent>
49+
50+
.. _network_agent_show-network-agent:
51+
.. describe:: <network-agent>
52+
53+
Network agent to display (ID only)

doc/source/commands.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ referring to both Compute and Volume quotas.
109109
* ``mapping``: (**Identity**) a definition to translate identity provider attributes to Identity concepts
110110
* ``module``: (**Internal**) - installed Python modules in the OSC process
111111
* ``network``: (**Compute**, **Network**) - a virtual network for connecting servers and other resources
112+
* ``network agent``: (**Network**) - A network agent is an agent that handles various tasks used to implement virtual networks
112113
* ``network rbac``: (**Network**) - an RBAC policy for network resources
113114
* ``network segment``: (**Network**) - a segment of a virtual network
114115
* ``object``: (**Object Storage**) a single file in the Object Storage
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
13+
from functional.common import test
14+
15+
16+
class NetworkAgentTests(test.TestCase):
17+
"""Functional tests for network agent. """
18+
IDs = None
19+
HEADERS = ['ID']
20+
FIELDS = ['id']
21+
22+
@classmethod
23+
def test_network_agent_list(cls):
24+
opts = cls.get_opts(cls.HEADERS)
25+
raw_output = cls.openstack('network agent list' + opts)
26+
# get the list of network agent IDs.
27+
cls.IDs = raw_output.split('\n')
28+
29+
def test_network_agent_show(self):
30+
opts = self.get_opts(self.FIELDS)
31+
raw_output = self.openstack('network agent show ' + self.IDs[0] + opts)
32+
self.assertEqual(self.IDs[0] + "\n", raw_output)
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
#
13+
14+
"""Network agent action implementations"""
15+
16+
import logging
17+
18+
from osc_lib.command import command
19+
from osc_lib import exceptions
20+
from osc_lib import utils
21+
22+
from openstackclient.i18n import _
23+
24+
25+
LOG = logging.getLogger(__name__)
26+
27+
28+
def _format_admin_state(state):
29+
return 'UP' if state else 'DOWN'
30+
31+
32+
_formatters = {
33+
'admin_state_up': _format_admin_state,
34+
'configurations': utils.format_dict,
35+
}
36+
37+
38+
class DeleteNetworkAgent(command.Command):
39+
"""Delete network agent(s)"""
40+
41+
def get_parser(self, prog_name):
42+
parser = super(DeleteNetworkAgent, self).get_parser(prog_name)
43+
parser.add_argument(
44+
'network_agent',
45+
metavar="<network-agent>",
46+
nargs='+',
47+
help=(_("Network agent(s) to delete (ID only)"))
48+
)
49+
return parser
50+
51+
def take_action(self, parsed_args):
52+
client = self.app.client_manager.network
53+
result = 0
54+
55+
for agent in parsed_args.network_agent:
56+
try:
57+
obj = client.get_agent(agent, ignore_missing=False)
58+
client.delete_agent(obj)
59+
except Exception as e:
60+
result += 1
61+
LOG.error(_("Failed to delete network agent with "
62+
"ID '%(agent)s': %(e)s"),
63+
{'agent': agent, 'e': e})
64+
65+
if result > 0:
66+
total = len(parsed_args.network_agent)
67+
msg = (_("%(result)s of %(total)s network agents failed "
68+
"to delete.") % {'result': result, 'total': total})
69+
raise exceptions.CommandError(msg)
70+
71+
72+
class ListNetworkAgent(command.Lister):
73+
"""List network agents"""
74+
75+
def take_action(self, parsed_args):
76+
client = self.app.client_manager.network
77+
columns = (
78+
'id',
79+
'agent_type',
80+
'host',
81+
'availability_zone',
82+
'alive',
83+
'admin_state_up',
84+
'binary'
85+
)
86+
column_headers = (
87+
'ID',
88+
'Agent Type',
89+
'Host',
90+
'Availability Zone',
91+
'Alive',
92+
'State',
93+
'Binary'
94+
)
95+
data = client.agents()
96+
return (column_headers,
97+
(utils.get_item_properties(
98+
s, columns, formatters=_formatters,
99+
) for s in data))
100+
101+
102+
class ShowNetworkAgent(command.ShowOne):
103+
"""Display network agent details"""
104+
105+
def get_parser(self, prog_name):
106+
parser = super(ShowNetworkAgent, self).get_parser(prog_name)
107+
parser.add_argument(
108+
'network_agent',
109+
metavar="<network-agent>",
110+
help=(_("Network agent to display (ID only)"))
111+
)
112+
return parser
113+
114+
def take_action(self, parsed_args):
115+
client = self.app.client_manager.network
116+
obj = client.get_agent(parsed_args.network_agent, ignore_missing=False)
117+
columns = tuple(sorted(list(obj.keys())))
118+
data = utils.get_item_properties(obj, columns, formatters=_formatters,)
119+
return columns, data

openstackclient/tests/network/v2/fakes.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,73 @@ def get_ports(ports=None, count=2):
491491
return mock.MagicMock(side_effect=ports)
492492

493493

494+
class FakeNetworkAgent(object):
495+
"""Fake one or more network agents."""
496+
497+
@staticmethod
498+
def create_one_network_agent(attrs=None):
499+
"""Create a fake network agent
500+
501+
:param Dictionary attrs:
502+
A dictionary with all attributes
503+
:return:
504+
A FakeResource object, with id, agent_type, and so on.
505+
"""
506+
attrs = attrs or {}
507+
508+
# Set default attributes
509+
agent_attrs = {
510+
'id': 'agent-id-' + uuid.uuid4().hex,
511+
'agent_type': 'agent-type-' + uuid.uuid4().hex,
512+
'host': 'host-' + uuid.uuid4().hex,
513+
'availability_zone': 'zone-' + uuid.uuid4().hex,
514+
'alive': True,
515+
'admin_state_up': True,
516+
'binary': 'binary-' + uuid.uuid4().hex,
517+
'configurations': {'subnet': 2, 'networks': 1},
518+
}
519+
agent_attrs.update(attrs)
520+
agent = fakes.FakeResource(info=copy.deepcopy(agent_attrs),
521+
loaded=True)
522+
return agent
523+
524+
@staticmethod
525+
def create_network_agents(attrs=None, count=2):
526+
"""Create multiple fake network agents.
527+
528+
:param Dictionary attrs:
529+
A dictionary with all attributes
530+
:param int count:
531+
The number of network agents to fake
532+
:return:
533+
A list of FakeResource objects faking the network agents
534+
"""
535+
agents = []
536+
for i in range(0, count):
537+
agents.append(FakeNetworkAgent.create_one_network_agent(attrs))
538+
539+
return agents
540+
541+
@staticmethod
542+
def get_network_agents(agents=None, count=2):
543+
"""Get an iterable MagicMock object with a list of faked network agents.
544+
545+
If network agents list is provided, then initialize the Mock object
546+
with the list. Otherwise create one.
547+
548+
:param List agents:
549+
A list of FakeResource objects faking network agents
550+
:param int count:
551+
The number of network agents to fake
552+
:return:
553+
An iterable Mock object with side_effect set to a list of faked
554+
network agents
555+
"""
556+
if agents is None:
557+
agents = FakeNetworkAgent.create_network_agents(count)
558+
return mock.MagicMock(side_effect=agents)
559+
560+
494561
class FakeNetworkRBAC(object):
495562
"""Fake one or more network rbac policies."""
496563

0 commit comments

Comments
 (0)