Skip to content

Commit 39fd3ae

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Add network segment resource"
2 parents fafc9e9 + ac8a9bc commit 39fd3ae

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
@@ -23,5 +23,6 @@ Network Resources
2323
v2/router
2424
v2/security_group
2525
v2/security_group_rule
26+
v2/segment
2627
v2/subnet
2728
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
@@ -29,6 +29,7 @@
2929
from openstack.network.v2 import router as _router
3030
from openstack.network.v2 import security_group as _security_group
3131
from openstack.network.v2 import security_group_rule as _security_group_rule
32+
from openstack.network.v2 import segment as _segment
3233
from openstack.network.v2 import subnet as _subnet
3334
from openstack.network.v2 import subnet_pool as _subnet_pool
3435
from openstack.network.v2 import vpn_service as _vpn_service
@@ -1498,6 +1499,58 @@ def security_group_rules(self, **query):
14981499
return self._list(_security_group_rule.SecurityGroupRule,
14991500
paginated=False, **query)
15001501

1502+
def find_segment(self, name_or_id, ignore_missing=True):
1503+
"""Find a single segment
1504+
1505+
.. caution::
1506+
BETA: This API is a work in progress and is subject to change.
1507+
1508+
:param name_or_id: The name or ID of a segment.
1509+
:param bool ignore_missing: When set to ``False``
1510+
:class:`~openstack.exceptions.ResourceNotFound` will be
1511+
raised when the resource does not exist.
1512+
When set to ``True``, None will be returned when
1513+
attempting to find a nonexistent resource.
1514+
:returns: One :class:`~openstack.network.v2.segment.Segment` or None
1515+
"""
1516+
return self._find(_segment.Segment, name_or_id,
1517+
ignore_missing=ignore_missing)
1518+
1519+
def get_segment(self, segment):
1520+
"""Get a single segment
1521+
1522+
.. caution::
1523+
BETA: This API is a work in progress and is subject to change.
1524+
1525+
:param segment: The value can be the ID of a segment or a
1526+
:class:`~openstack.network.v2.segment.Segment`
1527+
instance.
1528+
1529+
:returns: One :class:`~openstack.network.v2.segment.Segment`
1530+
:raises: :class:`~openstack.exceptions.ResourceNotFound`
1531+
when no resource can be found.
1532+
"""
1533+
return self._get(_segment.Segment, segment)
1534+
1535+
def segments(self, **query):
1536+
"""Return a generator of segments
1537+
1538+
.. caution::
1539+
BETA: This API is a work in progress and is subject to change.
1540+
1541+
:param kwargs \*\*query: Optional query parameters to be sent to limit
1542+
the resources being returned. Available parameters include:
1543+
1544+
* network_id: ID of the network that owns the segments
1545+
* network_type: Network type for the segments
1546+
* physical_network: Physical network name for the segments
1547+
* segmentation_id: Segmentation ID for the segments
1548+
1549+
:returns: A generator of segment objects
1550+
:rtype: :class:`~openstack.network.v2.segment.Segment`
1551+
"""
1552+
return self._list(_segment.Segment, paginated=False, **query)
1553+
15011554
def create_subnet(self, **attrs):
15021555
"""Create a new subnet from attributes
15031556

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
@@ -32,6 +32,7 @@
3232
from openstack.network.v2 import router
3333
from openstack.network.v2 import security_group
3434
from openstack.network.v2 import security_group_rule
35+
from openstack.network.v2 import segment
3536
from openstack.network.v2 import subnet
3637
from openstack.network.v2 import subnet_pool
3738
from openstack.network.v2 import vpn_service
@@ -511,6 +512,15 @@ def test_security_group_rules(self):
511512
security_group_rule.SecurityGroupRule,
512513
paginated=False)
513514

515+
def test_segment_find(self):
516+
self.verify_find(self.proxy.find_segment, segment.Segment)
517+
518+
def test_segment_get(self):
519+
self.verify_get(self.proxy.get_segment, segment.Segment)
520+
521+
def test_segments(self):
522+
self.verify_list(self.proxy.segments, segment.Segment, paginated=False)
523+
514524
def test_subnet_create_attrs(self):
515525
self.verify_create(self.proxy.create_subnet, subnet.Subnet)
516526

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)