Skip to content

Commit 43fee66

Browse files
committed
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
1 parent 13244eb commit 43fee66

File tree

7 files changed

+266
-0
lines changed

7 files changed

+266
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Network Resources
1414
v2/metering_label
1515
v2/metering_label_rule
1616
v2/network
17+
v2/network_ip_availability
1718
v2/pool
1819
v2/pool_member
1920
v2/port
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
openstack.network.v2.network_ip_availability
2+
===========================================
3+
4+
.. automodule:: openstack.network.v2.network_ip_availability
5+
6+
The NetworkIPAvailability Class
7+
-------------------------------
8+
9+
The ``NetworkIPAvailability`` class inherits from :class:`~openstack.resource.Resource`.
10+
11+
.. autoclass:: openstack.network.v2.network_ip_availability.NetworkIPAvailability
12+
:members:

openstack/network/v2/_proxy.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from openstack.network.v2 import metering_label as _metering_label
2121
from openstack.network.v2 import metering_label_rule as _metering_label_rule
2222
from openstack.network.v2 import network as _network
23+
from openstack.network.v2 import network_ip_availability
2324
from openstack.network.v2 import pool as _pool
2425
from openstack.network.v2 import pool_member as _pool_member
2526
from openstack.network.v2 import port as _port
@@ -757,6 +758,50 @@ def update_network(self, network, **attrs):
757758
"""
758759
return self._update(_network.Network, network, **attrs)
759760

761+
def find_network_ip_availability(self, name_or_id, ignore_missing=True):
762+
"""Find IP availability of a network
763+
764+
:param name_or_id: The name or ID of a network.
765+
:param bool ignore_missing: When set to ``False``
766+
:class:`~openstack.exceptions.ResourceNotFound` will be
767+
raised when the resource does not exist.
768+
When set to ``True``, None will be returned when
769+
attempting to find a nonexistent resource.
770+
:returns: One :class:`~openstack.network.v2.network_ip_availability.
771+
NetworkIPAvailability` or None
772+
"""
773+
return self._find(network_ip_availability.NetworkIPAvailability,
774+
name_or_id,
775+
ignore_missing=ignore_missing)
776+
777+
def get_network_ip_availability(self, network):
778+
"""Get IP availability of a network
779+
780+
:param network:
781+
The value can be the ID of a network or a
782+
:class:`~openstack.network.v2.network.Network` instance.
783+
784+
:returns: One :class:`~openstack.network.v2.network_ip_availability.
785+
NetworkIPAvailability`
786+
:raises: :class:`~openstack.exceptions.ResourceNotFound`
787+
when no resource can be found.
788+
"""
789+
return self._get(network_ip_availability.NetworkIPAvailability,
790+
network)
791+
792+
def network_ip_availabilities(self, **query):
793+
"""Return a generator of network ip availabilities
794+
795+
:param kwargs \*\*query: Optional query parameters to be sent to limit
796+
the resources being returned.
797+
798+
:returns: A generator of network ip availability objects
799+
:rtype: :class:`~openstack.network.v2.network_ip_availability.
800+
NetworkIPAvailability`
801+
"""
802+
return self._list(network_ip_availability.NetworkIPAvailability,
803+
paginated=False, **query)
804+
760805
def create_pool(self, **attrs):
761806
"""Create a new pool from attributes
762807
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 NetworkIPAvailability(resource.Resource):
18+
resource_key = 'network_ip_availability'
19+
resources_key = 'network_ip_availabilities'
20+
base_path = '/network-ip-availabilities'
21+
service = network_service.NetworkService()
22+
23+
# capabilities
24+
allow_create = False
25+
allow_retrieve = True
26+
allow_update = False
27+
allow_delete = False
28+
allow_list = True
29+
30+
# Properties
31+
#: Network ID to use when listing network IP availability.
32+
network_id = resource.prop('network_id')
33+
#: Network Name for the particular network IP availability.
34+
network_name = resource.prop('network_name')
35+
#: The Subnet IP Availability of all subnets of a network.
36+
#: *Type: list*
37+
subnet_ip_availability = resource.prop('subnet_ip_availability', type=list)
38+
#: The ID of the project this network IP availability is associated with.
39+
project_id = resource.prop('tenant_id')
40+
#: The total ips of a network.
41+
#: *Type: int*
42+
total_ips = resource.prop('total_ips', type=int)
43+
#: The used or consumed ip of a network
44+
#: *Type: int*
45+
used_ips = resource.prop('used_ips', type=int)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 network
16+
from openstack.network.v2 import port
17+
from openstack.network.v2 import subnet
18+
from openstack.tests.functional import base
19+
20+
21+
class TestNetworkIPAvailability(base.BaseFunctionalTest):
22+
23+
NET_NAME = uuid.uuid4().hex
24+
SUB_NAME = uuid.uuid4().hex
25+
PORT_NAME = uuid.uuid4().hex
26+
UPDATE_NAME = uuid.uuid4().hex
27+
IPV4 = 4
28+
CIDR = "10.100.0.0/24"
29+
NET_ID = None
30+
SUB_ID = None
31+
PORT_ID = None
32+
33+
@classmethod
34+
def setUpClass(cls):
35+
super(TestNetworkIPAvailability, cls).setUpClass()
36+
net = cls.conn.network.create_network(name=cls.NET_NAME)
37+
assert isinstance(net, network.Network)
38+
cls.assertIs(cls.NET_NAME, net.name)
39+
cls.NET_ID = net.id
40+
sub = cls.conn.network.create_subnet(name=cls.SUB_NAME,
41+
ip_version=cls.IPV4,
42+
network_id=cls.NET_ID,
43+
cidr=cls.CIDR)
44+
assert isinstance(sub, subnet.Subnet)
45+
cls.assertIs(cls.SUB_NAME, sub.name)
46+
cls.SUB_ID = sub.id
47+
prt = cls.conn.network.create_port(name=cls.PORT_NAME,
48+
network_id=cls.NET_ID)
49+
assert isinstance(prt, port.Port)
50+
cls.assertIs(cls.PORT_NAME, prt.name)
51+
cls.PORT_ID = prt.id
52+
53+
@classmethod
54+
def tearDownClass(cls):
55+
sot = cls.conn.network.delete_port(cls.PORT_ID)
56+
cls.assertIs(None, sot)
57+
sot = cls.conn.network.delete_subnet(cls.SUB_ID)
58+
cls.assertIs(None, sot)
59+
sot = cls.conn.network.delete_network(cls.NET_ID)
60+
cls.assertIs(None, sot)
61+
62+
def test_find(self):
63+
sot = self.conn.network.find_network_ip_availability(self.NET_ID)
64+
self.assertEqual(self.NET_ID, sot.network_id)
65+
66+
def test_get(self):
67+
sot = self.conn.network.get_network_ip_availability(self.NET_ID)
68+
self.assertEqual(self.NET_ID, sot.network_id)
69+
self.assertEqual(self.NET_NAME, sot.network_name)
70+
71+
def test_list(self):
72+
ids = [o.network_id for o in
73+
self.conn.network.network_ip_availabilities()]
74+
self.assertIn(self.NET_ID, ids)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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 network_ip_availability
16+
17+
IDENTIFIER = 'IDENTIFIER'
18+
EXAMPLE = {
19+
'network_id': IDENTIFIER,
20+
'network_name': 'private',
21+
'subnet_ip_availability': [],
22+
'tenant_id': '5',
23+
'total_ips': 6,
24+
'used_ips': 10,
25+
}
26+
27+
EXAMPLE_WITH_OPTIONAL = {
28+
'network_id': IDENTIFIER,
29+
'network_name': 'private',
30+
'subnet_ip_availability': [{"used_ips": 3, "subnet_id":
31+
"2e4db1d6-ab2d-4bb1-93bb-a003fdbc9b39",
32+
"subnet_name": "private-subnet",
33+
"ip_version": 6, "cidr": "fd91:c3ba:e818::/64",
34+
"total_ips": 18446744073709551614}],
35+
'tenant_id': '2',
36+
'total_ips': 1844,
37+
'used_ips': 6,
38+
}
39+
40+
41+
class TestNetworkIPAvailability(testtools.TestCase):
42+
43+
def test_basic(self):
44+
sot = network_ip_availability.NetworkIPAvailability()
45+
self.assertEqual('network_ip_availability', sot.resource_key)
46+
self.assertEqual('network_ip_availabilities', sot.resources_key)
47+
self.assertEqual('/network-ip-availabilities', sot.base_path)
48+
self.assertEqual('network', sot.service.service_type)
49+
self.assertFalse(sot.allow_create)
50+
self.assertTrue(sot.allow_retrieve)
51+
self.assertFalse(sot.allow_update)
52+
self.assertFalse(sot.allow_delete)
53+
self.assertTrue(sot.allow_list)
54+
55+
def test_make_it(self):
56+
sot = network_ip_availability.NetworkIPAvailability(EXAMPLE)
57+
self.assertEqual(EXAMPLE['network_id'],
58+
sot.network_id)
59+
self.assertEqual(EXAMPLE['network_name'], sot.network_name)
60+
self.assertEqual(EXAMPLE['subnet_ip_availability'],
61+
sot.subnet_ip_availability)
62+
self.assertEqual(EXAMPLE['tenant_id'], sot.project_id)
63+
self.assertEqual(EXAMPLE['total_ips'], sot.total_ips)
64+
self.assertEqual(EXAMPLE['used_ips'], sot.used_ips)
65+
66+
def test_make_it_with_optional(self):
67+
sot = network_ip_availability.NetworkIPAvailability(
68+
EXAMPLE_WITH_OPTIONAL)
69+
self.assertEqual(EXAMPLE_WITH_OPTIONAL['network_id'], sot.network_id)
70+
self.assertEqual(EXAMPLE_WITH_OPTIONAL['network_name'],
71+
sot.network_name)
72+
self.assertEqual(EXAMPLE_WITH_OPTIONAL['subnet_ip_availability'],
73+
sot.subnet_ip_availability)
74+
self.assertEqual(EXAMPLE_WITH_OPTIONAL['tenant_id'], sot.project_id)
75+
self.assertEqual(EXAMPLE_WITH_OPTIONAL['total_ips'], sot.total_ips)
76+
self.assertEqual(EXAMPLE_WITH_OPTIONAL['used_ips'], sot.used_ips)

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from openstack.network.v2 import metering_label
2424
from openstack.network.v2 import metering_label_rule
2525
from openstack.network.v2 import network
26+
from openstack.network.v2 import network_ip_availability
2627
from openstack.network.v2 import pool
2728
from openstack.network.v2 import pool_member
2829
from openstack.network.v2 import port
@@ -270,6 +271,18 @@ def test_networks(self):
270271
def test_network_update(self):
271272
self.verify_update(self.proxy.update_network, network.Network)
272273

274+
def test_network_ip_availability_find(self):
275+
self.verify_find(self.proxy.find_network_ip_availability,
276+
network_ip_availability.NetworkIPAvailability)
277+
278+
def test_network_ip_availability_get(self):
279+
self.verify_get(self.proxy.get_network_ip_availability,
280+
network_ip_availability.NetworkIPAvailability)
281+
282+
def test_network_ip_availabilities(self):
283+
self.verify_list(self.proxy.network_ip_availabilities,
284+
network_ip_availability.NetworkIPAvailability)
285+
273286
def test_pool_member_create_attrs(self):
274287
self.verify_create(self.proxy.create_pool_member,
275288
pool_member.PoolMember,

0 commit comments

Comments
 (0)