Add function cidr_to_netmask to utils
cidr_to_netmask util function to returns netmask from cidr subnet notation. Change-Id: Id76d0aa698bbd844376075dfc2ff0d9add67cf83 Closes-Bug: #1273663
This commit is contained in:
parent
bf1a399809
commit
43357850ca
|
@ -729,3 +729,53 @@ class SSHPoolTestCase(test.TestCase):
|
|||
third_id = ssh.id
|
||||
|
||||
self.assertNotEqual(first_id, third_id)
|
||||
|
||||
|
||||
class CidrToNetmaskTestCase(test.TestCase):
|
||||
"""Unit test for cidr to netmask."""
|
||||
|
||||
def test_cidr_to_netmask_01(self):
|
||||
cidr = '10.0.0.0/0'
|
||||
expected_netmask = '0.0.0.0'
|
||||
result = utils.cidr_to_netmask(cidr)
|
||||
self.assertEqual(expected_netmask, result)
|
||||
|
||||
def test_cidr_to_netmask_02(self):
|
||||
cidr = '10.0.0.0/24'
|
||||
expected_netmask = '255.255.255.0'
|
||||
result = utils.cidr_to_netmask(cidr)
|
||||
self.assertEqual(expected_netmask, result)
|
||||
|
||||
def test_cidr_to_netmask_03(self):
|
||||
cidr = '10.0.0.0/5'
|
||||
expected_netmask = '248.0.0.0'
|
||||
result = utils.cidr_to_netmask(cidr)
|
||||
self.assertEqual(expected_netmask, result)
|
||||
|
||||
def test_cidr_to_netmask_04(self):
|
||||
cidr = '10.0.0.0/32'
|
||||
expected_netmask = '255.255.255.255'
|
||||
result = utils.cidr_to_netmask(cidr)
|
||||
self.assertEqual(expected_netmask, result)
|
||||
|
||||
def test_cidr_to_netmask_05(self):
|
||||
cidr = '10.0.0.1'
|
||||
expected_netmask = '255.255.255.255'
|
||||
result = utils.cidr_to_netmask(cidr)
|
||||
self.assertEqual(expected_netmask, result)
|
||||
|
||||
def test_cidr_to_netmask_invalid_01(self):
|
||||
cidr = '10.0.0.0/33'
|
||||
self.assertRaises(exception.InvalidInput, utils.cidr_to_netmask, cidr)
|
||||
|
||||
def test_cidr_to_netmask_invalid_02(self):
|
||||
cidr = ''
|
||||
self.assertRaises(exception.InvalidInput, utils.cidr_to_netmask, cidr)
|
||||
|
||||
def test_cidr_to_netmask_invalid_03(self):
|
||||
cidr = '10.0.0.0/33'
|
||||
self.assertRaises(exception.InvalidInput, utils.cidr_to_netmask, cidr)
|
||||
|
||||
def test_cidr_to_netmask_invalid_04(self):
|
||||
cidr = '10.0.0.555/33'
|
||||
self.assertRaises(exception.InvalidInput, utils.cidr_to_netmask, cidr)
|
||||
|
|
|
@ -25,6 +25,7 @@ import errno
|
|||
import functools
|
||||
import hashlib
|
||||
import inspect
|
||||
import netaddr
|
||||
import os
|
||||
import paramiko
|
||||
import pyclbr
|
||||
|
@ -1214,14 +1215,9 @@ def to_bytes(text, default=0):
|
|||
|
||||
|
||||
def cidr_to_netmask(cidr):
|
||||
"""
|
||||
Convert cidr notation to the netmask string
|
||||
|
||||
:param cidr: integer which represents cidr notation
|
||||
:rtype: string
|
||||
"""
|
||||
cidr = int(cidr)
|
||||
bits = 0
|
||||
for i in xrange(32 - cidr, 32):
|
||||
bits |= (1 << i)
|
||||
return socket.inet_ntoa(struct.pack('>I', bits))
|
||||
"""Convert cidr to netmask."""
|
||||
try:
|
||||
network = netaddr.IPNetwork(cidr)
|
||||
return str(network.netmask)
|
||||
except netaddr.AddrFormatError:
|
||||
raise exception.InvalidInput(_("Invalid cidr supplied %s") % cidr)
|
||||
|
|
Loading…
Reference in New Issue