Skip to content

Commit 83aef9c

Browse files
author
Michael Gugino
committed
Implement network agents
This commit adds api support for network agents. Change-Id: I9755637f76787d5fac8ff295ae273b308fcb98d0 Implements: blueprint implement-network-agents
1 parent 13e94f5 commit 83aef9c

9 files changed

Lines changed: 287 additions & 1 deletion

File tree

‎doc/source/users/guides/network.rst‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ provide external network access for servers on project networks.
7676

7777
Full example: `network resource list`_
7878

79+
List Network Agents
80+
------------
81+
82+
A **network agent** is a plugin that handles various tasks used to
83+
implement virtual networks. These agents include neutron-dhcp-agent,
84+
neutron-l3-agent, neutron-metering-agent, and neutron-lbaas-agent,
85+
among others.
86+
87+
.. literalinclude:: ../examples/network/list.py
88+
:pyobject: list_network_agents
89+
90+
Full example: `network resource list`_
91+
7992
Create Network
8093
--------------
8194

@@ -100,4 +113,4 @@ Full example: `network resource delete`_
100113

101114
.. _network resource create: http://git.openstack.org/cgit/openstack/python-openstacksdk/tree/examples/network/create.py
102115
.. _network resource delete: http://git.openstack.org/cgit/openstack/python-openstacksdk/tree/examples/network/delete.py
103-
.. _network resource list: http://git.openstack.org/cgit/openstack/python-openstacksdk/tree/examples/network/list.py
116+
.. _network resource list: http://git.openstack.org/cgit/openstack/python-openstacksdk/tree/examples/network/list.py

‎doc/source/users/resources/network/index.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Network Resources
55
:maxdepth: 1
66

77
v2/address_scope
8+
v2/agent
89
v2/availability_zone
910
v2/extension
1011
v2/floating_ip
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
openstack.network.v2.network
2+
============================
3+
4+
.. automodule:: openstack.network.v2.agent
5+
6+
The Agent Class
7+
-----------------
8+
9+
The ``Agent`` class inherits from :class:`~openstack.resource.Resource`.
10+
11+
.. autoclass:: openstack.network.v2.agent.Agent
12+
:members:

‎examples/network/list.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,10 @@ def list_routers(conn):
5050

5151
for router in conn.network.routers():
5252
print(router)
53+
54+
55+
def list_network_agents(conn):
56+
print("List Network Agents:")
57+
58+
for agent in conn.network.agents():
59+
print(agent)

‎openstack/network/v2/_proxy.py‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# under the License.
1212

1313
from openstack.network.v2 import address_scope as _address_scope
14+
from openstack.network.v2 import agent as _agent
1415
from openstack.network.v2 import availability_zone
1516
from openstack.network.v2 import extension
1617
from openstack.network.v2 import floating_ip as _floating_ip
@@ -122,6 +123,59 @@ def update_address_scope(self, address_scope, **attrs):
122123
address_scope,
123124
**attrs)
124125

126+
def agents(self, **query):
127+
"""Return a generator of network agents
128+
129+
:param kwargs \*\*query: Optional query parameters to be sent to limit
130+
the resources being returned.
131+
132+
:returns: A generator of agents
133+
:rtype: :class:`~openstack.network.v2.agent.Agent`
134+
"""
135+
return self._list(_agent.Agent, paginated=False, **query)
136+
137+
def delete_agent(self, agent, ignore_missing=True):
138+
"""Delete a network agent
139+
140+
:param agent: The value can be the ID of a agent or a
141+
:class:`~openstack.network.v2.agent.Agent` instance.
142+
:param bool ignore_missing: When set to ``False``
143+
:class:`~openstack.exceptions.ResourceNotFound` will be
144+
raised when the agent does not exist.
145+
When set to ``True``, no exception will be set when
146+
attempting to delete a nonexistent agent.
147+
148+
:returns: ``None``
149+
"""
150+
self._delete(_agent.Agent, agent,
151+
ignore_missing=ignore_missing)
152+
153+
def get_agent(self, agent, ignore_missing=True):
154+
"""Get a single network agent
155+
156+
:param agent: The value can be the ID of a agent or a
157+
:class:`~openstack.network.v2.agent.Agent` instance.
158+
159+
:returns: One :class:`~openstack.network.v2.agent.Agent`
160+
:rtype: :class:`~openstack.network.v2.agent.Agent`
161+
:raises: :class:`~openstack.exceptions.ResourceNotFound`
162+
when no resource can be found.
163+
"""
164+
return self._get(_agent.Agent, agent)
165+
166+
def update_agent(self, agent, **attrs):
167+
"""Update a network agent
168+
169+
:param agent: The value can be the ID of a agent or a
170+
:class:`~openstack.network.v2.agent.Agent` instance.
171+
:attrs kwargs: The attributes to update on the agent represented
172+
by ``value``.
173+
174+
:returns: One :class:`~openstack.network.v2.agent.Agent`
175+
:rtype: :class:`~openstack.network.v2.agent.Agent`
176+
"""
177+
return self._update(_agent.Agent, agent, **attrs)
178+
125179
def availability_zones(self):
126180
"""Return a generator of availability zones
127181

‎openstack/network/v2/agent.py‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 openstack import format
14+
from openstack.network import network_service
15+
from openstack import resource
16+
17+
18+
class Agent(resource.Resource):
19+
resource_key = 'agent'
20+
resources_key = 'agents'
21+
base_path = '/agents'
22+
service = network_service.NetworkService()
23+
24+
# capabilities
25+
allow_create = False
26+
allow_retrieve = True
27+
allow_update = True
28+
allow_delete = True
29+
allow_list = True
30+
31+
# Properties
32+
#: The type of network agent.
33+
agent_type = resource.prop('agent_type')
34+
#: Availability zone for the network agent.
35+
availability_zone = resource.prop('availability_zone')
36+
#: The name of the network agent's application binary.
37+
binary = resource.prop('binary')
38+
#: Network agent configuration data specific to the agent_type.
39+
configuration = resource.prop('configurations')
40+
#: Timestamp when the network agent was created.
41+
#: *Type: datetime object parsed from ISO 8601 formatted string*
42+
created_at = resource.prop('created_at', type=format.ISO8601)
43+
#: The network agent description.
44+
description = resource.prop('description')
45+
#: Timestamp when the network agent's heartbeat was last seen.
46+
#: *Type: datetime object parsed from ISO 8601 formatted string*
47+
last_heartbeat_at = resource.prop('heartbeat_timestamp',
48+
type=format.ISO8601)
49+
#: The host the agent is running on.
50+
host = resource.prop('host')
51+
#: The administrative state of the network agent, which is up
52+
#: ``True`` or down ``False``. *Type: bool*
53+
is_admin_state_up = resource.prop('admin_state_up', type=bool)
54+
#: Whether or not the network agent is alive.
55+
#: *Type: bool*
56+
is_alive = resource.prop('alive', type=bool)
57+
#: Timestamp when the network agent was last started.
58+
#: *Type: datetime object parsed from ISO 8601 formatted string*
59+
started_at = resource.prop('started_at', type=format.ISO8601)
60+
#: The messaging queue topic the network agent subscribes to.
61+
topic = resource.prop('topic')
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
import uuid
14+
15+
from openstack.network.v2 import agent
16+
from openstack.tests.functional import base
17+
18+
19+
class TestAgent(base.BaseFunctionalTest):
20+
21+
AGENT = None
22+
DESC = 'test descrition'
23+
24+
def validate_uuid(self, s):
25+
try:
26+
uuid.UUID(s)
27+
except Exception:
28+
return False
29+
return True
30+
31+
@classmethod
32+
def setUpClass(cls):
33+
super(TestAgent, cls).setUpClass()
34+
agent_list = list(cls.conn.network.agents())
35+
cls.AGENT = agent_list[0]
36+
assert isinstance(cls.AGENT, agent.Agent)
37+
38+
def test_list(self):
39+
agent_list = list(self.conn.network.agents())
40+
self.AGENT = agent_list[0]
41+
assert isinstance(self.AGENT, agent.Agent)
42+
self.assertTrue(self.validate_uuid(self.AGENT.id))
43+
44+
def test_get(self):
45+
sot = self.conn.network.get_agent(self.AGENT.id)
46+
self.assertEqual(self.AGENT.id, sot.id)
47+
48+
def test_update(self):
49+
sot = self.conn.network.update_agent(self.AGENT.id,
50+
description=self.DESC)
51+
self.assertEqual(self.DESC, sot.description)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
import datetime
14+
15+
import testtools
16+
17+
from openstack.network.v2 import agent
18+
19+
IDENTIFIER = 'IDENTIFIER'
20+
EXAMPLE = {
21+
'admin_state_up': True,
22+
'agent_type': 'Test Agent',
23+
'alive': True,
24+
'availability_zone': 'az1',
25+
'binary': 'test-binary',
26+
'configurations': {'attr1': 'value1', 'attr2': 'value2'},
27+
'created_at': '2016-03-09T12:14:57.233772',
28+
'description': 'test description',
29+
'heartbeat_timestamp': '2016-08-09T12:14:57.233772',
30+
'host': 'test-host',
31+
'id': IDENTIFIER,
32+
'started_at': '2016-07-09T12:14:57.233772',
33+
'topic': 'test-topic'
34+
}
35+
36+
37+
class TestAgent(testtools.TestCase):
38+
39+
def test_basic(self):
40+
sot = agent.Agent()
41+
self.assertEqual('agent', sot.resource_key)
42+
self.assertEqual('agents', sot.resources_key)
43+
self.assertEqual('/agents', sot.base_path)
44+
self.assertEqual('network', sot.service.service_type)
45+
self.assertFalse(sot.allow_create)
46+
self.assertTrue(sot.allow_retrieve)
47+
self.assertTrue(sot.allow_update)
48+
self.assertTrue(sot.allow_delete)
49+
self.assertTrue(sot.allow_list)
50+
51+
def test_make_it(self):
52+
sot = agent.Agent(EXAMPLE)
53+
self.assertTrue(sot.is_admin_state_up)
54+
self.assertEqual(EXAMPLE['agent_type'], sot.agent_type)
55+
self.assertTrue(sot.is_alive)
56+
self.assertEqual(EXAMPLE['availability_zone'],
57+
sot.availability_zone)
58+
self.assertEqual(EXAMPLE['binary'], sot.binary)
59+
self.assertEqual(EXAMPLE['configurations'], sot.configuration)
60+
dt = datetime.datetime(2016, 3, 9, 12, 14, 57, 233772).replace(
61+
tzinfo=None)
62+
self.assertEqual(dt, sot.created_at.replace(tzinfo=None))
63+
self.assertEqual(EXAMPLE['description'], sot.description)
64+
dt = datetime.datetime(2016, 8, 9, 12, 14, 57, 233772).replace(
65+
tzinfo=None)
66+
self.assertEqual(dt,
67+
sot.last_heartbeat_at.replace(tzinfo=None))
68+
self.assertEqual(EXAMPLE['host'], sot.host)
69+
self.assertEqual(EXAMPLE['id'], sot.id)
70+
dt = datetime.datetime(2016, 7, 9, 12, 14, 57, 233772).replace(
71+
tzinfo=None)
72+
self.assertEqual(dt, sot.started_at.replace(tzinfo=None))
73+
self.assertEqual(EXAMPLE['topic'], sot.topic)

‎openstack/tests/unit/network/v2/test_proxy.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from openstack.network.v2 import _proxy
1616
from openstack.network.v2 import address_scope
17+
from openstack.network.v2 import agent
1718
from openstack.network.v2 import availability_zone
1819
from openstack.network.v2 import extension
1920
from openstack.network.v2 import floating_ip
@@ -73,6 +74,19 @@ def test_address_scope_update(self):
7374
self.verify_update(self.proxy.update_address_scope,
7475
address_scope.AddressScope)
7576

77+
def test_agent_delete(self):
78+
self.verify_delete(self.proxy.delete_agent, agent.Agent, True)
79+
80+
def test_agent_get(self):
81+
self.verify_get(self.proxy.get_agent, agent.Agent)
82+
83+
def test_agents(self):
84+
self.verify_list(self.proxy.agents, agent.Agent,
85+
paginated=False)
86+
87+
def test_agent_update(self):
88+
self.verify_update(self.proxy.update_agent, agent.Agent)
89+
7690
def test_availability_zones(self):
7791
self.verify_list_no_kwargs(self.proxy.availability_zones,
7892
availability_zone.AvailabilityZone,

0 commit comments

Comments
 (0)