Move "is_private_ip" and "is_valid_ip_address" function from
libcloud.compute.base into libcloud.utils.networking module.


Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/33f5722e
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/33f5722e
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/33f5722e

Branch: refs/heads/trunk
Commit: 33f5722ebe231b6e7be0539bcc733734bd3c2005
Parents: c2438ac
Author: Tomaz Muraus <[email protected]>
Authored: Sun Nov 17 20:56:33 2013 +0100
Committer: Tomaz Muraus <[email protected]>
Committed: Sun Nov 17 21:00:52 2013 +0100

----------------------------------------------------------------------
 CHANGES                                |  4 ++
 libcloud/compute/base.py               | 48 +++--------------
 libcloud/compute/drivers/cloudstack.py |  3 +-
 libcloud/compute/drivers/ecp.py        |  2 +-
 libcloud/compute/drivers/joyent.py     |  2 +-
 libcloud/compute/drivers/nephoscale.py |  2 +-
 libcloud/utils/networking.py           | 80 +++++++++++++++++++++++++++++
 7 files changed, 97 insertions(+), 44 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/33f5722e/CHANGES
----------------------------------------------------------------------
diff --git a/CHANGES b/CHANGES
index e8efb4e..0453fcb 100644
--- a/CHANGES
+++ b/CHANGES
@@ -24,6 +24,10 @@ Changes with Apache Libcloud in development
       0.7.0 from the Node object.
       [Tomaz Muraus]
 
+    - Move "is_private_ip" and "is_valid_ip_address" function from
+      libcloud.compute.base into libcloud.utils.networking module.
+      [Tomaz Muraus]
+
 Changes with Apache Libcloud 0.14.0-beta3
 
   *) General

http://git-wip-us.apache.org/repos/asf/libcloud/blob/33f5722e/libcloud/compute/base.py
----------------------------------------------------------------------
diff --git a/libcloud/compute/base.py b/libcloud/compute/base.py
index ecaf87d..5ce5db6 100644
--- a/libcloud/compute/base.py
+++ b/libcloud/compute/base.py
@@ -22,7 +22,6 @@ import time
 import hashlib
 import os
 import socket
-import struct
 import binascii
 
 from libcloud.utils.py3 import b
@@ -35,6 +34,9 @@ from libcloud.common.base import ConnectionKey
 from libcloud.common.base import BaseDriver
 from libcloud.common.types import LibcloudError
 
+from libcloud.utils.networking import is_private_subnet
+from libcloud.utils.networking import is_valid_ip_address
+
 
 # How long to wait for the node to come online after creating it
 NODE_ONLINE_WAIT_TIMEOUT = 10 * 60
@@ -52,7 +54,11 @@ __all__ = [
     'NodeLocation',
     'NodeAuthSSHKey',
     'NodeAuthPassword',
-    'NodeDriver'
+    'NodeDriver',
+
+    # Deprecated, moved to libcloud.utils.networking
+    'is_private_subnet',
+    'is_valid_ip_address'
 ]
 
 
@@ -1219,44 +1225,6 @@ class NodeDriver(BaseDriver):
                               size_id=size_id)
 
 
-def is_private_subnet(ip):
-    """
-    Utility function to check if an IP address is inside a private subnet.
-
-    :type ip: ``str``
-    :param ip: IP address to check
-
-    :return: ``bool`` if the specified IP address is private.
-    """
-    priv_subnets = [{'subnet': '10.0.0.0', 'mask': '255.0.0.0'},
-                    {'subnet': '172.16.0.0', 'mask': '255.240.0.0'},
-                    {'subnet': '192.168.0.0', 'mask': '255.255.0.0'}]
-
-    ip = struct.unpack('I', socket.inet_aton(ip))[0]
-
-    for network in priv_subnets:
-        subnet = struct.unpack('I', socket.inet_aton(network['subnet']))[0]
-        mask = struct.unpack('I', socket.inet_aton(network['mask']))[0]
-
-        if (ip & mask) == (subnet & mask):
-            return True
-
-    return False
-
-
-def is_valid_ip_address(address, family=socket.AF_INET):
-    """
-    Check if the provided address is valid IPv4 or IPv6 adddress.
-
-    :return: ``bool`` True if the provided address is valid.
-    """
-    try:
-        socket.inet_pton(family, address)
-    except socket.error:
-        return False
-    return True
-
-
 if __name__ == '__main__':
     import doctest
     doctest.testmod()

http://git-wip-us.apache.org/repos/asf/libcloud/blob/33f5722e/libcloud/compute/drivers/cloudstack.py
----------------------------------------------------------------------
diff --git a/libcloud/compute/drivers/cloudstack.py 
b/libcloud/compute/drivers/cloudstack.py
index 84073b7..819fcda 100644
--- a/libcloud/compute/drivers/cloudstack.py
+++ b/libcloud/compute/drivers/cloudstack.py
@@ -23,8 +23,9 @@ from libcloud.utils.py3 import b
 from libcloud.compute.providers import Provider
 from libcloud.common.cloudstack import CloudStackDriverMixIn
 from libcloud.compute.base import Node, NodeDriver, NodeImage, NodeLocation,\
-    NodeSize, StorageVolume, is_private_subnet
+    NodeSize, StorageVolume
 from libcloud.compute.types import NodeState, LibcloudError
+from libcloud.utils.networking import is_private_subnet
 
 
 class CloudStackNode(Node):

http://git-wip-us.apache.org/repos/asf/libcloud/blob/33f5722e/libcloud/compute/drivers/ecp.py
----------------------------------------------------------------------
diff --git a/libcloud/compute/drivers/ecp.py b/libcloud/compute/drivers/ecp.py
index 86e77e2..021e7b0 100644
--- a/libcloud/compute/drivers/ecp.py
+++ b/libcloud/compute/drivers/ecp.py
@@ -36,7 +36,7 @@ from libcloud.common.base import Response, 
ConnectionUserAndKey
 from libcloud.compute.base import NodeDriver, NodeSize, NodeLocation
 from libcloud.compute.base import NodeImage, Node
 from libcloud.compute.types import Provider, NodeState, InvalidCredsError
-from libcloud.compute.base import is_private_subnet
+from libcloud.utils.networking import is_private_subnet
 
 #Defaults
 API_HOST = ''

http://git-wip-us.apache.org/repos/asf/libcloud/blob/33f5722e/libcloud/compute/drivers/joyent.py
----------------------------------------------------------------------
diff --git a/libcloud/compute/drivers/joyent.py 
b/libcloud/compute/drivers/joyent.py
index 1aa1a60..6d8142e 100644
--- a/libcloud/compute/drivers/joyent.py
+++ b/libcloud/compute/drivers/joyent.py
@@ -30,9 +30,9 @@ from libcloud.utils.py3 import b
 from libcloud.common.types import LibcloudError
 from libcloud.compute.providers import Provider
 from libcloud.common.base import JsonResponse, ConnectionUserAndKey
-from libcloud.compute.base import is_private_subnet
 from libcloud.compute.types import NodeState, InvalidCredsError
 from libcloud.compute.base import Node, NodeDriver, NodeImage, NodeSize
+from libcloud.utils.networking import is_private_subnet
 
 API_HOST_SUFFIX = '.api.joyentcloud.com'
 API_VERSION = '~6.5'

http://git-wip-us.apache.org/repos/asf/libcloud/blob/33f5722e/libcloud/compute/drivers/nephoscale.py
----------------------------------------------------------------------
diff --git a/libcloud/compute/drivers/nephoscale.py 
b/libcloud/compute/drivers/nephoscale.py
index c8fad3b..efe5089 100644
--- a/libcloud/compute/drivers/nephoscale.py
+++ b/libcloud/compute/drivers/nephoscale.py
@@ -30,12 +30,12 @@ from libcloud.utils.py3 import b
 from libcloud.utils.py3 import urlencode
 
 from libcloud.compute.providers import Provider
-from libcloud.compute.base import is_private_subnet
 from libcloud.common.base import JsonResponse, ConnectionUserAndKey
 from libcloud.compute.types import (NodeState, InvalidCredsError,
                                     LibcloudError)
 from libcloud.compute.base import (Node, NodeDriver, NodeImage, NodeSize,
                                    NodeLocation)
+from libcloud.utils.networking import is_private_subnet
 
 API_HOST = 'api.nephoscale.com'
 

http://git-wip-us.apache.org/repos/asf/libcloud/blob/33f5722e/libcloud/utils/networking.py
----------------------------------------------------------------------
diff --git a/libcloud/utils/networking.py b/libcloud/utils/networking.py
new file mode 100644
index 0000000..d9678ed
--- /dev/null
+++ b/libcloud/utils/networking.py
@@ -0,0 +1,80 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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 socket
+import struct
+
+__all__ = [
+    'is_private_subnet',
+    'is_public_subnet',
+    'is_valid_ip_address'
+]
+
+
+def is_private_subnet(ip):
+    """
+    Utility function to check if an IP address is inside a private subnet.
+
+    :type ip: ``str``
+    :param ip: IP address to check
+
+    :return: ``bool`` if the specified IP address is private.
+    """
+    priv_subnets = [{'subnet': '10.0.0.0', 'mask': '255.0.0.0'},
+                    {'subnet': '172.16.0.0', 'mask': '255.240.0.0'},
+                    {'subnet': '192.168.0.0', 'mask': '255.255.0.0'}]
+
+    ip = struct.unpack('I', socket.inet_aton(ip))[0]
+
+    for network in priv_subnets:
+        subnet = struct.unpack('I', socket.inet_aton(network['subnet']))[0]
+        mask = struct.unpack('I', socket.inet_aton(network['mask']))[0]
+
+        if (ip & mask) == (subnet & mask):
+            return True
+
+    return False
+
+
+def is_public_subnet(ip):
+    """
+    Utility function to check if an IP address is inside a public subnet.
+
+    :type ip: ``str``
+    :param ip: IP address to check
+
+    :return: ``bool`` if the specified IP address is public.
+    """
+    return not is_private_subnet(ip=ip)
+
+
+def is_valid_ip_address(address, family=socket.AF_INET):
+    """
+    Check if the provided address is valid IPv4 or IPv6 adddress.
+
+    :param address: IPv4 or IPv6 ddress to check.
+    :type address: ``str``
+
+    :param family: Address family (socket.AF_INTET / socket.AF_INET6).
+    :type family: ``int``
+
+    :return: ``bool`` True if the provided address is valid.
+    """
+    try:
+        socket.inet_pton(family, address)
+    except socket.error:
+        return False
+
+    return True

Reply via email to