You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by to...@apache.org on 2014/08/06 13:32:54 UTC
git commit: Add utility join_ipv4_segments and
increment_ipv4_segments functions to libcloud.utils.networking module.
Repository: libcloud
Updated Branches:
refs/heads/trunk e63adedf0 -> ff191dfee
Add utility join_ipv4_segments and increment_ipv4_segments functions to
libcloud.utils.networking module.
Patch by Andrew Mann, Tomaz Muraus.
Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/ff191dfe
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/ff191dfe
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/ff191dfe
Branch: refs/heads/trunk
Commit: ff191dfee89d2786a7c1e5d9a3c3b380263b01b1
Parents: e63aded
Author: Tomaz Muraus <to...@apache.org>
Authored: Wed Aug 6 13:22:18 2014 +0200
Committer: Tomaz Muraus <to...@apache.org>
Committed: Wed Aug 6 13:32:46 2014 +0200
----------------------------------------------------------------------
libcloud/test/test_utils.py | 25 +++++++++++++++++++++
libcloud/utils/networking.py | 47 ++++++++++++++++++++++++++++++++++++++-
2 files changed, 71 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/libcloud/blob/ff191dfe/libcloud/test/test_utils.py
----------------------------------------------------------------------
diff --git a/libcloud/test/test_utils.py b/libcloud/test/test_utils.py
index 58b3506..4c1b6c0 100644
--- a/libcloud/test/test_utils.py
+++ b/libcloud/test/test_utils.py
@@ -42,6 +42,8 @@ from libcloud.utils.misc import get_secure_random_string
from libcloud.utils.networking import is_public_subnet
from libcloud.utils.networking import is_private_subnet
from libcloud.utils.networking import is_valid_ip_address
+from libcloud.utils.networking import join_ipv4_segments
+from libcloud.utils.networking import increment_ipv4_segments
from libcloud.storage.drivers.dummy import DummyIterator
@@ -355,6 +357,29 @@ class NetworkingUtilsTestCase(unittest.TestCase):
family=socket.AF_INET6)
self.assertFalse(status)
+ def test_join_ipv4_segments(self):
+ values = [
+ (('127', '0', '0', '1'), '127.0.0.1'),
+ (('255', '255', '255', '0'), '255.255.255.0'),
+ ]
+
+ for segments, joined_ip in values:
+ result = join_ipv4_segments(segments=segments)
+ self.assertEqual(result, joined_ip)
+
+ def test_increment_ipv4_segments(self):
+ values = [
+ (('127', '0', '0', '1'), '127.0.0.2'),
+ (('255', '255', '255', '0'), '255.255.255.1'),
+ (('254', '255', '255', '255'), '255.0.0.0'),
+ (('100', '1', '0', '255'), '100.1.1.0'),
+ ]
+
+ for segments, incremented_ip in values:
+ result = increment_ipv4_segments(segments=segments)
+ result = join_ipv4_segments(segments=result)
+ self.assertEqual(result, incremented_ip)
+
if __name__ == '__main__':
sys.exit(unittest.main())
http://git-wip-us.apache.org/repos/asf/libcloud/blob/ff191dfe/libcloud/utils/networking.py
----------------------------------------------------------------------
diff --git a/libcloud/utils/networking.py b/libcloud/utils/networking.py
index f7dca9b..d508d24 100644
--- a/libcloud/utils/networking.py
+++ b/libcloud/utils/networking.py
@@ -19,7 +19,9 @@ import struct
__all__ = [
'is_private_subnet',
'is_public_subnet',
- 'is_valid_ip_address'
+ 'is_valid_ip_address',
+ 'join_ipv4_segments',
+ 'increment_ipv4_segments'
]
@@ -78,3 +80,46 @@ def is_valid_ip_address(address, family=socket.AF_INET):
return False
return True
+
+
+def join_ipv4_segments(segments):
+ """
+ Helper method to join ip numeric segment pieces back into a full
+ ip address.
+
+ :param segments: IPv4 segments to join.
+ :type segments: ``list`` or ``tuple``
+
+ :return: IPv4 address.
+ :rtype: ``str``
+ """
+ return '.'.join([str(s) for s in segments])
+
+
+def increment_ipv4_segments(segments):
+ """
+ Increment an ip address given in quad segments based on ipv4 rules
+
+ :param segments: IPv4 segments to increment.
+ :type segments: ``list`` or ``tuple``
+
+ :return: Incremented segments.
+ :rtype: ``list``
+ """
+ segments = [int(segment) for segment in segments]
+
+ segments[3] += 1
+
+ if segments[3] == 256:
+ segments[3] = 0
+ segments[2] += 1
+
+ if segments[2] == 256:
+ segments[2] = 0
+ segments[1] += 1
+
+ if segments[1] == 256:
+ segments[1] = 0
+ segments[0] += 1
+
+ return segments