Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F8451936
D2568.id6486.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
D2568.id6486.diff
View Options
diff --git a/_modules/network.py b/_modules/network.py
new file mode 100644
--- /dev/null
+++ b/_modules/network.py
@@ -0,0 +1,23 @@
+# -*- coding: utf-8 -*-
+
+# -------------------------------------------------------------
+# Salt — Network execution module
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+# Project: Nasqueron
+# License: BSD-2-Clause
+# -------------------------------------------------------------
+
+
+def netmask_to_cidr_prefix(netmask):
+ """
+ Convert a netmask like 255.255.255.240 into a CIDR prefix like 24.
+
+ This can be useful for RHEL network scripts requiring PREFIX information.
+ """
+
+ # The CIDR prefix is the count of 1 bits in each octect.
+ # e.g. 255.255.255.240 can be split in octets [255, 255, 255, 240],
+ # then becomes ['0b11111111', '0b11111111', '0b11111111', '0b11110000'].
+ #
+ # There is 24 "1" in this expression, that's 24 is our CIDR prefix.
+ return sum([bin(int(octet)).count("1") for octet in netmask.split(".")])
diff --git a/_tests/modules/test_network.py b/_tests/modules/test_network.py
new file mode 100644
--- /dev/null
+++ b/_tests/modules/test_network.py
@@ -0,0 +1,29 @@
+from importlib.machinery import SourceFileLoader
+from unittest_data_provider import data_provider
+import unittest
+
+
+salt_test_case = SourceFileLoader("salt_test_case", "salt_test_case.py").load_module()
+network = SourceFileLoader("network", "../_modules/network.py").load_module()
+
+
+class Testinstance(unittest.TestCase, salt_test_case.SaltTestCase):
+ cidr_prefixes = lambda: (
+ ("255.255.255.255", 32),
+ ("255.255.255.254", 31),
+ ("255.255.255.252", 30),
+ ("255.255.255.240", 28),
+ ("255.255.255.224", 27),
+ ("255.255.255.0", 24),
+ ("255.252.0.0", 14),
+ )
+
+ @data_provider(cidr_prefixes)
+ def test_netmask_to_cidr_prefix(self, netmask, expected_prefix):
+ actual_prefix = network.netmask_to_cidr_prefix(netmask)
+
+ self.assertTrue(actual_prefix == expected_prefix)
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/_tests/modules/test_rust.py b/_tests/modules/test_rust.py
--- a/_tests/modules/test_rust.py
+++ b/_tests/modules/test_rust.py
@@ -1,13 +1,24 @@
from importlib.machinery import SourceFileLoader
import unittest
+from salt.modules import cmdmod
salt_test_case = SourceFileLoader("salt_test_case", "salt_test_case.py").load_module()
rust = SourceFileLoader("rust", "../_modules/rust.py").load_module()
-
class Testinstance(unittest.TestCase, salt_test_case.SaltTestCase):
+ def setUp(self):
+ self.initialize_mocks()
+ self.instance = rust
+
+ self.mock_salt()
+ self.salt["cmd.shell"] = cmdmod.shell
+
def test_get_rustc_triplet(self):
triplet = rust.get_rustc_triplet()
self.assertTrue(len(triplet.split("-")) > 2)
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/requirements.txt b/requirements.txt
--- a/requirements.txt
+++ b/requirements.txt
@@ -10,3 +10,4 @@
mock>=4.0.3,<5.0
json==3003
jsondiff==1.3.0
+unittest-data-provider>=1.0.1,<2.0
diff --git a/roles/core/network/files/RedHat/ifcfg-private b/roles/core/network/files/RedHat/ifcfg-private
--- a/roles/core/network/files/RedHat/ifcfg-private
+++ b/roles/core/network/files/RedHat/ifcfg-private
@@ -28,5 +28,5 @@
UUID={{ interface.uuid }}
DEVICE={{ interface.device }}
ONBOOT=yes
-PREFIX=24
+PREFIX={{ prefix }}
IPADDR={{ interface.address }}
diff --git a/roles/core/network/private.sls b/roles/core/network/private.sls
--- a/roles/core/network/private.sls
+++ b/roles/core/network/private.sls
@@ -36,6 +36,7 @@
- template: jinja
- context:
interface: {{ interface }}
+ prefix: {{ salt['network.netmask_to_cidr_prefix'](interface['netmask']) }}
{% endif %}
{% endif %}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Thu, May 15, 15:51 (17 h, 5 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2657941
Default Alt Text
D2568.id6486.diff (3 KB)
Attached To
Mode
D2568: Compute CIDR prefix from netmask for RHEL network scripts
Attached
Detach File
Event Timeline
Log In to Comment