Skip to content

Commit ac8a9bc

Browse files
committed
Add network segment resource
Add network segment resource in support of routed networks. This patch set includes the following: - Network segment resource - Proxy read interfaces for the network segment resource - Documentation updates - Unit tests - Functional tests (currently skipped) The documentation provides a caution notice since this support is a work in progress and is subject to change. Change-Id: I6166a49ec52206314f3766aa55523e7be54db348 Partially-Implements: blueprint routed-networks
1 parent 13e94f5 commit ac8a9bc

File tree

7 files changed

+240
-0
lines changed

7 files changed

+240
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ Network Resources
2222
v2/router
2323
v2/security_group
2424
v2/security_group_rule
25+
v2/segment
2526
v2/subnet
2627
v2/subnet_pool
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
openstack.network.v2.segment
2+
============================
3+
4+
.. automodule:: openstack.network.v2.segment
5+
6+
The Segment Class
7+
-----------------
8+
9+
The ``Segment`` class inherits from :class:`~openstack.resource.Resource`.
10+
11+
.. autoclass:: openstack.network.v2.segment.Segment
12+
:members:

openstack/network/v2/_proxy.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from openstack.network.v2 import router as _router
2929
from openstack.network.v2 import security_group as _security_group
3030
from openstack.network.v2 import security_group_rule as _security_group_rule
31+
from openstack.network.v2 import segment as _segment
3132
from openstack.network.v2 import subnet as _subnet
3233
from openstack.network.v2 import subnet_pool as _subnet_pool
3334
from openstack.network.v2 import vpn_service as _vpn_service
@@ -1444,6 +1445,58 @@ def security_group_rules(self, **query):
14441445
return self._list(_security_group_rule.SecurityGroupRule,
14451446
paginated=False, **query)
14461447

1448+
def find_segment(self, name_or_id, ignore_missing=True):
1449+
"""Find a single segment
1450+
1451+
.. caution::
1452+
BETA: This API is a work in progress and is subject to change.
1453+
1454+
:param name_or_id: The name or ID of a segment.
1455+
:param bool ignore_missing: When set to ``False``
1456+
:class:`~openstack.exceptions.ResourceNotFound` will be
1457+
raised when the resource does not exist.
1458+
When set to ``True``, None will be returned when
1459+
attempting to find a nonexistent resource.
1460+
:returns: One :class:`~openstack.network.v2.segment.Segment` or None
1461+
"""
1462+
return self._find(_segment.Segment, name_or_id,
1463+
ignore_missing=ignore_missing)
1464+
1465+
def get_segment(self, segment):
1466+
"""Get a single segment
1467+
1468+
.. caution::
1469+
BETA: This API is a work in progress and is subject to change.
1470+
1471+
:param segment: The value can be the ID of a segment or a
1472+
:class:`~openstack.network.v2.segment.Segment`
1473+
instance.
1474+
1475+
:returns: One :class:`~openstack.network.v2.segment.Segment`
1476+
:raises: :class:`~openstack.exceptions.ResourceNotFound`
1477+
when no resource can be found.
1478+
"""
1479+
return self._get(_segment.Segment, segment)
1480+
1481+
def segments(self, **query):
1482+
"""Return a generator of segments
1483+
1484+
.. caution::
1485+
BETA: This API is a work in progress and is subject to change.
1486+
1487+
:param kwargs \*\*query: Optional query parameters to be sent to limit
1488+
the resources being returned. Available parameters include:
1489+
1490+
* network_id: ID of the network that owns the segments
1491+
* network_type: Network type for the segments
1492+
* physical_network: Physical network name for the segments
1493+
* segmentation_id: Segmentation ID for the segments
1494+
1495+
:returns: A generator of segment objects
1496+
:rtype: :class:`~openstack.network.v2.segment.Segment`
1497+
"""
1498+
return self._list(_segment.Segment, paginated=False, **query)
1499+
14471500
def create_subnet(self, **attrs):
14481501
"""Create a new subnet from attributes
14491502

openstack/network/v2/segment.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.network import network_service
14+
from openstack import resource
15+
16+
17+
class Segment(resource.Resource):
18+
""".. caution:: This API is a work in progress and is subject to change."""
19+
resource_key = 'segment'
20+
resources_key = 'segments'
21+
base_path = '/segments'
22+
service = network_service.NetworkService()
23+
24+
# capabilities
25+
allow_create = False
26+
allow_retrieve = True
27+
allow_update = False
28+
allow_delete = False
29+
allow_list = True
30+
31+
# TODO(rtheis): Add description and name properties when support
32+
# is available.
33+
34+
# Properties
35+
#: The ID of the network associated with this segment.
36+
network_id = resource.prop('network_id')
37+
#: The type of network associated with this segment, such as
38+
#: ``flat``, ``gre``, ``vlan`` or ``vxlan``.
39+
network_type = resource.prop('network_type')
40+
#: The name of the physical network associated with this segment.
41+
physical_network = resource.prop('physical_network')
42+
#: The segmentation ID for this segment. The network type
43+
#: defines the segmentation model, VLAN ID for ``vlan`` network type
44+
#: and tunnel ID for ``gre`` and ``vxlan`` network types. *Type: int*
45+
segmentation_id = resource.prop('segmentation_id', type=int)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 unittest
14+
import uuid
15+
16+
from openstack.network.v2 import network
17+
from openstack.network.v2 import segment
18+
from openstack.tests.functional import base
19+
20+
21+
# NOTE(rtheis): Routed networks is still a WIP and not enabled by default.
22+
@unittest.skip("bp/routed-networks")
23+
class TestSegment(base.BaseFunctionalTest):
24+
25+
NETWORK_NAME = uuid.uuid4().hex
26+
NETWORK_TYPE = None
27+
PHYSICAL_NETWORK = None
28+
SEGMENTATION_ID = None
29+
NETWORK_ID = None
30+
SEGMENT_ID = None
31+
32+
@classmethod
33+
def setUpClass(cls):
34+
super(TestSegment, cls).setUpClass()
35+
36+
# Create a network to hold the segment.
37+
net = cls.conn.network.create_network(name=cls.NETWORK_NAME)
38+
assert isinstance(net, network.Network)
39+
cls.assertIs(cls.NETWORK_NAME, net.name)
40+
cls.NETWORK_ID = net.id
41+
42+
# Get the segment for the network.
43+
for seg in cls.conn.network.segments():
44+
assert isinstance(seg, segment.Segment)
45+
if cls.NETWORK_ID == seg.network_id:
46+
cls.NETWORK_TYPE = seg.network_type
47+
cls.PHYSICAL_NETWORK = seg.physical_network
48+
cls.SEGMENTATION_ID = seg.segmentation_id
49+
cls.SEGMENT_ID = seg.id
50+
break
51+
52+
@classmethod
53+
def tearDownClass(cls):
54+
sot = cls.conn.network.delete_network(cls.NETWORK_ID,
55+
ignore_missing=False)
56+
cls.assertIs(None, sot)
57+
58+
def test_find(self):
59+
sot = self.conn.network.find_segment(self.SEGMENT_ID)
60+
self.assertEqual(self.SEGMENT_ID, sot.id)
61+
62+
def test_get(self):
63+
sot = self.conn.network.get_segment(self.SEGMENT_ID)
64+
self.assertEqual(self.SEGMENT_ID, sot.id)
65+
self.assertEqual(self.NETWORK_ID, sot.network_id)
66+
self.assertEqual(self.NETWORK_TYPE, sot.network_type)
67+
self.assertEqual(self.PHYSICAL_NETWORK, sot.physical_network)
68+
self.assertEqual(self.SEGMENTATION_ID, sot.segmentation_id)
69+
70+
def test_list(self):
71+
ids = [o.id for o in self.conn.network.segments()]
72+
self.assertIn(self.SEGMENT_ID, ids)

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from openstack.network.v2 import router
3232
from openstack.network.v2 import security_group
3333
from openstack.network.v2 import security_group_rule
34+
from openstack.network.v2 import segment
3435
from openstack.network.v2 import subnet
3536
from openstack.network.v2 import subnet_pool
3637
from openstack.network.v2 import vpn_service
@@ -497,6 +498,15 @@ def test_security_group_rules(self):
497498
security_group_rule.SecurityGroupRule,
498499
paginated=False)
499500

501+
def test_segment_find(self):
502+
self.verify_find(self.proxy.find_segment, segment.Segment)
503+
504+
def test_segment_get(self):
505+
self.verify_get(self.proxy.get_segment, segment.Segment)
506+
507+
def test_segments(self):
508+
self.verify_list(self.proxy.segments, segment.Segment, paginated=False)
509+
500510
def test_subnet_create_attrs(self):
501511
self.verify_create(self.proxy.create_subnet, subnet.Subnet)
502512

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 testtools
14+
15+
from openstack.network.v2 import segment
16+
17+
IDENTIFIER = 'IDENTIFIER'
18+
EXAMPLE = {
19+
'id': IDENTIFIER,
20+
'network_id': '1',
21+
'network_type': 'vxlan',
22+
'physical_network': None,
23+
'segmentation_id': 2,
24+
}
25+
26+
27+
class TestSegment(testtools.TestCase):
28+
29+
def test_basic(self):
30+
sot = segment.Segment()
31+
self.assertEqual('segment', sot.resource_key)
32+
self.assertEqual('segments', sot.resources_key)
33+
self.assertEqual('/segments', sot.base_path)
34+
self.assertEqual('network', sot.service.service_type)
35+
self.assertFalse(sot.allow_create)
36+
self.assertTrue(sot.allow_retrieve)
37+
self.assertFalse(sot.allow_update)
38+
self.assertFalse(sot.allow_delete)
39+
self.assertTrue(sot.allow_list)
40+
41+
def test_make_it(self):
42+
sot = segment.Segment(EXAMPLE)
43+
self.assertEqual(EXAMPLE['id'], sot.id)
44+
self.assertEqual(EXAMPLE['network_id'], sot.network_id)
45+
self.assertEqual(EXAMPLE['network_type'], sot.network_type)
46+
self.assertEqual(EXAMPLE['physical_network'], sot.physical_network)
47+
self.assertEqual(EXAMPLE['segmentation_id'], sot.segmentation_id)

0 commit comments

Comments
 (0)