diff --git a/filter_plugins/get_cymais_path.py b/filter_plugins/get_cymais_path.py new file mode 100644 index 00000000..f0fa6391 --- /dev/null +++ b/filter_plugins/get_cymais_path.py @@ -0,0 +1,50 @@ +# filter_plugins/get_cymais_path.py + +""" +This plugin provides filters to extract the CyMaIS directory and file identifiers +from a given role name. It assumes the role name is structured as 'dir_file'. +If the structure is invalid (e.g., missing or too many underscores), it raises an error. +These filters are used to support internal processing within CyMaIS. +""" + +from ansible.errors import AnsibleFilterError + + +class CymaisPathExtractor: + """Extracts directory and file parts from role names in the format 'dir_file'.""" + + def __init__(self, value): + self.value = value + self._parts = self._split_value() + + def _split_value(self): + parts = self.value.split("_") + if len(parts) != 2: + raise AnsibleFilterError( + f"Invalid format: '{self.value}' must contain exactly one underscore (_)" + ) + return parts + + def get_dir(self): + return self._parts[0] + + def get_file(self): + return self._parts[1] + + +def get_cymais_dir(value): + return CymaisPathExtractor(value).get_dir() + + +def get_cymais_file(value): + return CymaisPathExtractor(value).get_file() + + +class FilterModule(object): + """Ansible filter plugin for CyMaIS path parsing.""" + + def filters(self): + return { + "get_cymais_dir": get_cymais_dir, + "get_cymais_file": get_cymais_file, + } diff --git a/tests/unit/filter_plugins/test_get_cymais_path.py b/tests/unit/filter_plugins/test_get_cymais_path.py new file mode 100644 index 00000000..2a5daf7c --- /dev/null +++ b/tests/unit/filter_plugins/test_get_cymais_path.py @@ -0,0 +1,46 @@ +# tests/unit/filter_plugins/test_get_cymais_path.py + +import unittest +import sys +import os + +# Ensure the filter_plugins directory is in the import path +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../filter_plugins'))) + +from get_cymais_path import get_cymais_dir, get_cymais_file +from ansible.errors import AnsibleFilterError + + +class TestGetCymaisPath(unittest.TestCase): + def test_valid_input(self): + """Test valid input with exactly one underscore""" + self.assertEqual(get_cymais_dir("web_app"), "web") + self.assertEqual(get_cymais_file("web_app"), "app") + + self.assertEqual(get_cymais_dir("sys_timer"), "sys") + self.assertEqual(get_cymais_file("sys_timer"), "timer") + + def test_invalid_no_underscore(self): + """Test input with no underscore raises error""" + with self.assertRaises(AnsibleFilterError): + get_cymais_dir("invalid") + with self.assertRaises(AnsibleFilterError): + get_cymais_file("invalid") + + def test_invalid_multiple_underscores(self): + """Test input with more than one underscore raises error""" + with self.assertRaises(AnsibleFilterError): + get_cymais_dir("too_many_parts_here") + with self.assertRaises(AnsibleFilterError): + get_cymais_file("too_many_parts_here") + + def test_empty_string(self): + """Test empty string input raises error""" + with self.assertRaises(AnsibleFilterError): + get_cymais_dir("") + with self.assertRaises(AnsibleFilterError): + get_cymais_file("") + + +if __name__ == '__main__': + unittest.main()