Merge "Add network segment resource"

This commit is contained in:
Jenkins 2016-05-20 12:14:12 +00:00 committed by Gerrit Code Review
commit 39fd3aee03
7 changed files with 240 additions and 0 deletions

View File

@ -23,5 +23,6 @@ Network Resources
v2/router
v2/security_group
v2/security_group_rule
v2/segment
v2/subnet
v2/subnet_pool

View File

@ -0,0 +1,12 @@
openstack.network.v2.segment
============================
.. automodule:: openstack.network.v2.segment
The Segment Class
-----------------
The ``Segment`` class inherits from :class:`~openstack.resource.Resource`.
.. autoclass:: openstack.network.v2.segment.Segment
:members:

View File

@ -29,6 +29,7 @@ from openstack.network.v2 import quota as _quota
from openstack.network.v2 import router as _router
from openstack.network.v2 import security_group as _security_group
from openstack.network.v2 import security_group_rule as _security_group_rule
from openstack.network.v2 import segment as _segment
from openstack.network.v2 import subnet as _subnet
from openstack.network.v2 import subnet_pool as _subnet_pool
from openstack.network.v2 import vpn_service as _vpn_service
@ -1498,6 +1499,58 @@ class Proxy(proxy.BaseProxy):
return self._list(_security_group_rule.SecurityGroupRule,
paginated=False, **query)
def find_segment(self, name_or_id, ignore_missing=True):
"""Find a single segment
.. caution::
BETA: This API is a work in progress and is subject to change.
:param name_or_id: The name or ID of a segment.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.network.v2.segment.Segment` or None
"""
return self._find(_segment.Segment, name_or_id,
ignore_missing=ignore_missing)
def get_segment(self, segment):
"""Get a single segment
.. caution::
BETA: This API is a work in progress and is subject to change.
:param segment: The value can be the ID of a segment or a
:class:`~openstack.network.v2.segment.Segment`
instance.
:returns: One :class:`~openstack.network.v2.segment.Segment`
:raises: :class:`~openstack.exceptions.ResourceNotFound`
when no resource can be found.
"""
return self._get(_segment.Segment, segment)
def segments(self, **query):
"""Return a generator of segments
.. caution::
BETA: This API is a work in progress and is subject to change.
:param kwargs \*\*query: Optional query parameters to be sent to limit
the resources being returned. Available parameters include:
* network_id: ID of the network that owns the segments
* network_type: Network type for the segments
* physical_network: Physical network name for the segments
* segmentation_id: Segmentation ID for the segments
:returns: A generator of segment objects
:rtype: :class:`~openstack.network.v2.segment.Segment`
"""
return self._list(_segment.Segment, paginated=False, **query)
def create_subnet(self, **attrs):
"""Create a new subnet from attributes

View File

@ -0,0 +1,45 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from openstack.network import network_service
from openstack import resource
class Segment(resource.Resource):
""".. caution:: This API is a work in progress and is subject to change."""
resource_key = 'segment'
resources_key = 'segments'
base_path = '/segments'
service = network_service.NetworkService()
# capabilities
allow_create = False
allow_retrieve = True
allow_update = False
allow_delete = False
allow_list = True
# TODO(rtheis): Add description and name properties when support
# is available.
# Properties
#: The ID of the network associated with this segment.
network_id = resource.prop('network_id')
#: The type of network associated with this segment, such as
#: ``flat``, ``gre``, ``vlan`` or ``vxlan``.
network_type = resource.prop('network_type')
#: The name of the physical network associated with this segment.
physical_network = resource.prop('physical_network')
#: The segmentation ID for this segment. The network type
#: defines the segmentation model, VLAN ID for ``vlan`` network type
#: and tunnel ID for ``gre`` and ``vxlan`` network types. *Type: int*
segmentation_id = resource.prop('segmentation_id', type=int)

View File

@ -0,0 +1,72 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import unittest
import uuid
from openstack.network.v2 import network
from openstack.network.v2 import segment
from openstack.tests.functional import base
# NOTE(rtheis): Routed networks is still a WIP and not enabled by default.
@unittest.skip("bp/routed-networks")
class TestSegment(base.BaseFunctionalTest):
NETWORK_NAME = uuid.uuid4().hex
NETWORK_TYPE = None
PHYSICAL_NETWORK = None
SEGMENTATION_ID = None
NETWORK_ID = None
SEGMENT_ID = None
@classmethod
def setUpClass(cls):
super(TestSegment, cls).setUpClass()
# Create a network to hold the segment.
net = cls.conn.network.create_network(name=cls.NETWORK_NAME)
assert isinstance(net, network.Network)
cls.assertIs(cls.NETWORK_NAME, net.name)
cls.NETWORK_ID = net.id
# Get the segment for the network.
for seg in cls.conn.network.segments():
assert isinstance(seg, segment.Segment)
if cls.NETWORK_ID == seg.network_id:
cls.NETWORK_TYPE = seg.network_type
cls.PHYSICAL_NETWORK = seg.physical_network
cls.SEGMENTATION_ID = seg.segmentation_id
cls.SEGMENT_ID = seg.id
break
@classmethod
def tearDownClass(cls):
sot = cls.conn.network.delete_network(cls.NETWORK_ID,
ignore_missing=False)
cls.assertIs(None, sot)
def test_find(self):
sot = self.conn.network.find_segment(self.SEGMENT_ID)
self.assertEqual(self.SEGMENT_ID, sot.id)
def test_get(self):
sot = self.conn.network.get_segment(self.SEGMENT_ID)
self.assertEqual(self.SEGMENT_ID, sot.id)
self.assertEqual(self.NETWORK_ID, sot.network_id)
self.assertEqual(self.NETWORK_TYPE, sot.network_type)
self.assertEqual(self.PHYSICAL_NETWORK, sot.physical_network)
self.assertEqual(self.SEGMENTATION_ID, sot.segmentation_id)
def test_list(self):
ids = [o.id for o in self.conn.network.segments()]
self.assertIn(self.SEGMENT_ID, ids)

View File

@ -32,6 +32,7 @@ from openstack.network.v2 import quota
from openstack.network.v2 import router
from openstack.network.v2 import security_group
from openstack.network.v2 import security_group_rule
from openstack.network.v2 import segment
from openstack.network.v2 import subnet
from openstack.network.v2 import subnet_pool
from openstack.network.v2 import vpn_service
@ -511,6 +512,15 @@ class TestNetworkProxy(test_proxy_base.TestProxyBase):
security_group_rule.SecurityGroupRule,
paginated=False)
def test_segment_find(self):
self.verify_find(self.proxy.find_segment, segment.Segment)
def test_segment_get(self):
self.verify_get(self.proxy.get_segment, segment.Segment)
def test_segments(self):
self.verify_list(self.proxy.segments, segment.Segment, paginated=False)
def test_subnet_create_attrs(self):
self.verify_create(self.proxy.create_subnet, subnet.Subnet)

View File

@ -0,0 +1,47 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import testtools
from openstack.network.v2 import segment
IDENTIFIER = 'IDENTIFIER'
EXAMPLE = {
'id': IDENTIFIER,
'network_id': '1',
'network_type': 'vxlan',
'physical_network': None,
'segmentation_id': 2,
}
class TestSegment(testtools.TestCase):
def test_basic(self):
sot = segment.Segment()
self.assertEqual('segment', sot.resource_key)
self.assertEqual('segments', sot.resources_key)
self.assertEqual('/segments', sot.base_path)
self.assertEqual('network', sot.service.service_type)
self.assertFalse(sot.allow_create)
self.assertTrue(sot.allow_retrieve)
self.assertFalse(sot.allow_update)
self.assertFalse(sot.allow_delete)
self.assertTrue(sot.allow_list)
def test_make_it(self):
sot = segment.Segment(EXAMPLE)
self.assertEqual(EXAMPLE['id'], sot.id)
self.assertEqual(EXAMPLE['network_id'], sot.network_id)
self.assertEqual(EXAMPLE['network_type'], sot.network_type)
self.assertEqual(EXAMPLE['physical_network'], sot.physical_network)
self.assertEqual(EXAMPLE['segmentation_id'], sot.segmentation_id)