Add support for Network IP Availability

There was feature added in neutron to get the network availability
for which neutron client patch [1] and sever patch [2] are merged.
this patch will add sdk for openstack client side

[1]. https://review.openstack.org/#/c/269926/
[2]. https://review.openstack.org/#/c/212955/

Change-Id: I3b40d8edea87c068c4e8133e436511765064d5f8
This commit is contained in:
Manjeet Singh Bhatia 2016-04-13 22:11:52 +00:00
parent 13244eb46f
commit 43fee66d9d
7 changed files with 266 additions and 0 deletions

@ -14,6 +14,7 @@ Network Resources
v2/metering_label
v2/metering_label_rule
v2/network
v2/network_ip_availability
v2/pool
v2/pool_member
v2/port

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

@ -20,6 +20,7 @@ from openstack.network.v2 import load_balancer as _load_balancer
from openstack.network.v2 import metering_label as _metering_label
from openstack.network.v2 import metering_label_rule as _metering_label_rule
from openstack.network.v2 import network as _network
from openstack.network.v2 import network_ip_availability
from openstack.network.v2 import pool as _pool
from openstack.network.v2 import pool_member as _pool_member
from openstack.network.v2 import port as _port
@ -757,6 +758,50 @@ class Proxy(proxy.BaseProxy):
"""
return self._update(_network.Network, network, **attrs)
def find_network_ip_availability(self, name_or_id, ignore_missing=True):
"""Find IP availability of a network
:param name_or_id: The name or ID of a network.
: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.network_ip_availability.
NetworkIPAvailability` or None
"""
return self._find(network_ip_availability.NetworkIPAvailability,
name_or_id,
ignore_missing=ignore_missing)
def get_network_ip_availability(self, network):
"""Get IP availability of a network
:param network:
The value can be the ID of a network or a
:class:`~openstack.network.v2.network.Network` instance.
:returns: One :class:`~openstack.network.v2.network_ip_availability.
NetworkIPAvailability`
:raises: :class:`~openstack.exceptions.ResourceNotFound`
when no resource can be found.
"""
return self._get(network_ip_availability.NetworkIPAvailability,
network)
def network_ip_availabilities(self, **query):
"""Return a generator of network ip availabilities
:param kwargs \*\*query: Optional query parameters to be sent to limit
the resources being returned.
:returns: A generator of network ip availability objects
:rtype: :class:`~openstack.network.v2.network_ip_availability.
NetworkIPAvailability`
"""
return self._list(network_ip_availability.NetworkIPAvailability,
paginated=False, **query)
def create_pool(self, **attrs):
"""Create a new pool from attributes

@ -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 NetworkIPAvailability(resource.Resource):
resource_key = 'network_ip_availability'
resources_key = 'network_ip_availabilities'
base_path = '/network-ip-availabilities'
service = network_service.NetworkService()
# capabilities
allow_create = False
allow_retrieve = True
allow_update = False
allow_delete = False
allow_list = True
# Properties
#: Network ID to use when listing network IP availability.
network_id = resource.prop('network_id')
#: Network Name for the particular network IP availability.
network_name = resource.prop('network_name')
#: The Subnet IP Availability of all subnets of a network.
#: *Type: list*
subnet_ip_availability = resource.prop('subnet_ip_availability', type=list)
#: The ID of the project this network IP availability is associated with.
project_id = resource.prop('tenant_id')
#: The total ips of a network.
#: *Type: int*
total_ips = resource.prop('total_ips', type=int)
#: The used or consumed ip of a network
#: *Type: int*
used_ips = resource.prop('used_ips', type=int)

@ -0,0 +1,74 @@
# 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 uuid
from openstack.network.v2 import network
from openstack.network.v2 import port
from openstack.network.v2 import subnet
from openstack.tests.functional import base
class TestNetworkIPAvailability(base.BaseFunctionalTest):
NET_NAME = uuid.uuid4().hex
SUB_NAME = uuid.uuid4().hex
PORT_NAME = uuid.uuid4().hex
UPDATE_NAME = uuid.uuid4().hex
IPV4 = 4
CIDR = "10.100.0.0/24"
NET_ID = None
SUB_ID = None
PORT_ID = None
@classmethod
def setUpClass(cls):
super(TestNetworkIPAvailability, cls).setUpClass()
net = cls.conn.network.create_network(name=cls.NET_NAME)
assert isinstance(net, network.Network)
cls.assertIs(cls.NET_NAME, net.name)
cls.NET_ID = net.id
sub = cls.conn.network.create_subnet(name=cls.SUB_NAME,
ip_version=cls.IPV4,
network_id=cls.NET_ID,
cidr=cls.CIDR)
assert isinstance(sub, subnet.Subnet)
cls.assertIs(cls.SUB_NAME, sub.name)
cls.SUB_ID = sub.id
prt = cls.conn.network.create_port(name=cls.PORT_NAME,
network_id=cls.NET_ID)
assert isinstance(prt, port.Port)
cls.assertIs(cls.PORT_NAME, prt.name)
cls.PORT_ID = prt.id
@classmethod
def tearDownClass(cls):
sot = cls.conn.network.delete_port(cls.PORT_ID)
cls.assertIs(None, sot)
sot = cls.conn.network.delete_subnet(cls.SUB_ID)
cls.assertIs(None, sot)
sot = cls.conn.network.delete_network(cls.NET_ID)
cls.assertIs(None, sot)
def test_find(self):
sot = self.conn.network.find_network_ip_availability(self.NET_ID)
self.assertEqual(self.NET_ID, sot.network_id)
def test_get(self):
sot = self.conn.network.get_network_ip_availability(self.NET_ID)
self.assertEqual(self.NET_ID, sot.network_id)
self.assertEqual(self.NET_NAME, sot.network_name)
def test_list(self):
ids = [o.network_id for o in
self.conn.network.network_ip_availabilities()]
self.assertIn(self.NET_ID, ids)

@ -0,0 +1,76 @@
# 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 network_ip_availability
IDENTIFIER = 'IDENTIFIER'
EXAMPLE = {
'network_id': IDENTIFIER,
'network_name': 'private',
'subnet_ip_availability': [],
'tenant_id': '5',
'total_ips': 6,
'used_ips': 10,
}
EXAMPLE_WITH_OPTIONAL = {
'network_id': IDENTIFIER,
'network_name': 'private',
'subnet_ip_availability': [{"used_ips": 3, "subnet_id":
"2e4db1d6-ab2d-4bb1-93bb-a003fdbc9b39",
"subnet_name": "private-subnet",
"ip_version": 6, "cidr": "fd91:c3ba:e818::/64",
"total_ips": 18446744073709551614}],
'tenant_id': '2',
'total_ips': 1844,
'used_ips': 6,
}
class TestNetworkIPAvailability(testtools.TestCase):
def test_basic(self):
sot = network_ip_availability.NetworkIPAvailability()
self.assertEqual('network_ip_availability', sot.resource_key)
self.assertEqual('network_ip_availabilities', sot.resources_key)
self.assertEqual('/network-ip-availabilities', 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 = network_ip_availability.NetworkIPAvailability(EXAMPLE)
self.assertEqual(EXAMPLE['network_id'],
sot.network_id)
self.assertEqual(EXAMPLE['network_name'], sot.network_name)
self.assertEqual(EXAMPLE['subnet_ip_availability'],
sot.subnet_ip_availability)
self.assertEqual(EXAMPLE['tenant_id'], sot.project_id)
self.assertEqual(EXAMPLE['total_ips'], sot.total_ips)
self.assertEqual(EXAMPLE['used_ips'], sot.used_ips)
def test_make_it_with_optional(self):
sot = network_ip_availability.NetworkIPAvailability(
EXAMPLE_WITH_OPTIONAL)
self.assertEqual(EXAMPLE_WITH_OPTIONAL['network_id'], sot.network_id)
self.assertEqual(EXAMPLE_WITH_OPTIONAL['network_name'],
sot.network_name)
self.assertEqual(EXAMPLE_WITH_OPTIONAL['subnet_ip_availability'],
sot.subnet_ip_availability)
self.assertEqual(EXAMPLE_WITH_OPTIONAL['tenant_id'], sot.project_id)
self.assertEqual(EXAMPLE_WITH_OPTIONAL['total_ips'], sot.total_ips)
self.assertEqual(EXAMPLE_WITH_OPTIONAL['used_ips'], sot.used_ips)

@ -23,6 +23,7 @@ from openstack.network.v2 import load_balancer
from openstack.network.v2 import metering_label
from openstack.network.v2 import metering_label_rule
from openstack.network.v2 import network
from openstack.network.v2 import network_ip_availability
from openstack.network.v2 import pool
from openstack.network.v2 import pool_member
from openstack.network.v2 import port
@ -270,6 +271,18 @@ class TestNetworkProxy(test_proxy_base.TestProxyBase):
def test_network_update(self):
self.verify_update(self.proxy.update_network, network.Network)
def test_network_ip_availability_find(self):
self.verify_find(self.proxy.find_network_ip_availability,
network_ip_availability.NetworkIPAvailability)
def test_network_ip_availability_get(self):
self.verify_get(self.proxy.get_network_ip_availability,
network_ip_availability.NetworkIPAvailability)
def test_network_ip_availabilities(self):
self.verify_list(self.proxy.network_ip_availabilities,
network_ip_availability.NetworkIPAvailability)
def test_pool_member_create_attrs(self):
self.verify_create(self.proxy.create_pool_member,
pool_member.PoolMember,