From 1e89827e4dff4cf6d5a6f4e1ef0db88e7523331b Mon Sep 17 00:00:00 2001 From: Adrian Vladu Date: Wed, 24 Jun 2020 16:55:15 +0300 Subject: [PATCH] Move ip_netmask_to_cidr to network utils Move baseopenstackservice._ip_netmask_to_cidr to utils/network so that it can be reused by other network parsers. Change-Id: Iacca02cda75fd5d5b80f6200e7d2f26a3381b737 --- .../metadata/services/baseopenstackservice.py | 14 +++----------- cloudbaseinit/tests/utils/test_network.py | 18 ++++++++++++++++++ cloudbaseinit/utils/network.py | 9 +++++++++ 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/cloudbaseinit/metadata/services/baseopenstackservice.py b/cloudbaseinit/metadata/services/baseopenstackservice.py index f0e488b9..efcb7d20 100644 --- a/cloudbaseinit/metadata/services/baseopenstackservice.py +++ b/cloudbaseinit/metadata/services/baseopenstackservice.py @@ -16,7 +16,6 @@ import json import posixpath -import netaddr from oslo_log import log as oslo_logging from cloudbaseinit import conf as cloudbaseinit_conf @@ -25,6 +24,7 @@ from cloudbaseinit.metadata.services import base from cloudbaseinit.models import network as network_model from cloudbaseinit.utils import debiface from cloudbaseinit.utils import encoding +from cloudbaseinit.utils import network as network_utils from cloudbaseinit.utils import x509constants NETWORK_LINK_TYPE_PHYSICAL = "phy" @@ -101,14 +101,6 @@ class BaseOpenStackService(base.BaseMetadataService): return debiface.parse(content) - @staticmethod - def _ip_netmask_to_cidr(ip_address, netmask): - if netmask is None: - return ip_address - prefix_len = netaddr.IPNetwork( - u"%s/%s" % (ip_address, netmask)).prefixlen - return u"%s/%s" % (ip_address, prefix_len) - @staticmethod def _parse_network_data_links(links_data): links = [] @@ -195,7 +187,7 @@ class BaseOpenStackService(base.BaseMetadataService): link_id = network_data.get("link") ip_address = network_data.get("ip_address") netmask = network_data.get("netmask") - address_cidr = BaseOpenStackService._ip_netmask_to_cidr( + address_cidr = network_utils.ip_netmask_to_cidr( ip_address, netmask) routes = [] @@ -203,7 +195,7 @@ class BaseOpenStackService(base.BaseMetadataService): gateway = route_data.get("gateway") network = route_data.get("network") netmask = route_data.get("netmask") - network_cidr = BaseOpenStackService._ip_netmask_to_cidr( + network_cidr = network_utils.ip_netmask_to_cidr( network, netmask) route = network_model.Route( diff --git a/cloudbaseinit/tests/utils/test_network.py b/cloudbaseinit/tests/utils/test_network.py index d685dca8..b99469d6 100644 --- a/cloudbaseinit/tests/utils/test_network.py +++ b/cloudbaseinit/tests/utils/test_network.py @@ -96,3 +96,21 @@ class NetworkUtilsTest(unittest.TestCase): res = network.get_local_ip("fake address") self.assertEqual(res, "fake name") mock_socket().connect.assert_called_with(("fake address", 8000)) + + def _test_ip_netmask_to_cidr(self, expected_result, fake_ip_address, + fake_netmask): + result = network.ip_netmask_to_cidr(fake_ip_address, fake_netmask) + self.assertEqual(expected_result, result) + + def test_ip_netmask_to_cidr(self): + fake_ip_address = '10.1.1.1' + expected_result = '10.1.1.1/24' + fake_netmask = '255.255.255.0' + self._test_ip_netmask_to_cidr(expected_result, fake_ip_address, + fake_netmask) + + def test_ip_netmask_to_cidr_empty_netmask(self): + fake_ip_address = '10.1.1.1' + fake_netmask = None + self._test_ip_netmask_to_cidr(fake_ip_address, fake_ip_address, + fake_netmask) diff --git a/cloudbaseinit/utils/network.py b/cloudbaseinit/utils/network.py index 81fca7b4..c312f611 100644 --- a/cloudbaseinit/utils/network.py +++ b/cloudbaseinit/utils/network.py @@ -14,6 +14,7 @@ import binascii +import netaddr import socket import struct import sys @@ -89,3 +90,11 @@ def netmask6_to_4_truncate(netmask6): mask = "1" * length + "0" * (32 - length) network_address = struct.pack("!L", int(mask, 2)) return socket.inet_ntoa(network_address) + + +def ip_netmask_to_cidr(ip_address, netmask): + if not netmask: + return ip_address + prefix_len = netaddr.IPNetwork( + u"%s/%s" % (ip_address, netmask)).prefixlen + return u"%s/%s" % (ip_address, prefix_len)