From cb5da4a8e794842cbaaf59bc497f74b4089c9b0d Mon Sep 17 00:00:00 2001 From: Shashank Venkat Date: Fri, 14 Feb 2025 13:52:00 +0530 Subject: [PATCH 01/21] Scaffold generic module through add subcommand --- src/ansible_creator/arg_parser.py | 40 ++++++++++++ .../plugins/module/__init__.py.j2 | 0 .../plugins/module/hello_world.py.j2 | 65 +++++++++++++++++++ src/ansible_creator/subcommands/add.py | 12 +++- 4 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 src/ansible_creator/resources/collection_project/plugins/module/__init__.py.j2 create mode 100644 src/ansible_creator/resources/collection_project/plugins/module/hello_world.py.j2 diff --git a/src/ansible_creator/arg_parser.py b/src/ansible_creator/arg_parser.py index 27817127..7f082341 100644 --- a/src/ansible_creator/arg_parser.py +++ b/src/ansible_creator/arg_parser.py @@ -231,7 +231,31 @@ def _add_resource(self, subparser: SubParser[ArgumentParser]) -> None: self._add_resource_devfile(subparser=subparser) self._add_resource_role(subparser=subparser) self._add_resource_execution_env(subparser=subparser) + #self._add_resource_module(subparser=subparser) + + def _add_resource_module(self, subparser: SubParser[ArgumentParser]) -> None: + """Add generic modules files to an existing Ansible project. + Args: + subparser: The subparser to add generic modules files to + """ + parser = subparser.add_parser( + "module", + help="Add a generic module to an existing Ansible collection.", + formatter_class=CustomHelpFormatter, + ) + parser.add_argument( + "module_name", + help="The name of the module to add.", + ) + parser.add_argument( + "path", + default="./", + help="The path to the Ansible collection. The default is the current working directory.", + ) + self._add_overwrite(parser) + self._add_args_common(parser) + def _add_resource_devcontainer(self, subparser: SubParser[ArgumentParser]) -> None: """Add devcontainer files to an existing Ansible project. @@ -353,6 +377,7 @@ def _add_plugin(self, subparser: SubParser[ArgumentParser]) -> None: self._add_plugin_action(subparser=subparser) self._add_plugin_filter(subparser=subparser) self._add_plugin_lookup(subparser=subparser) + self._add_plugin_modules(subparser=subparser) def _add_plugin_action(self, subparser: SubParser[ArgumentParser]) -> None: """Add an action plugin to an existing Ansible collection project. @@ -384,6 +409,21 @@ def _add_plugin_filter(self, subparser: SubParser[ArgumentParser]) -> None: self._add_overwrite(parser) self._add_args_plugin_common(parser) + def _add_plugin_modules(self, subparser: SubParser[ArgumentParser]) -> None: + """Add a module plugin to an existing Ansible collection project. + + Args: + subparser: The subparser to add module plugin to + """ + parser = subparser.add_parser( + "module", + help="Add a module plugin to an existing Ansible collection.", + formatter_class=CustomHelpFormatter, + ) + self._add_args_common(parser) + self._add_overwrite(parser) + self._add_args_plugin_common(parser) + def _add_plugin_lookup(self, subparser: SubParser[ArgumentParser]) -> None: """Add a lookup plugin to an existing Ansible collection project. diff --git a/src/ansible_creator/resources/collection_project/plugins/module/__init__.py.j2 b/src/ansible_creator/resources/collection_project/plugins/module/__init__.py.j2 new file mode 100644 index 00000000..e69de29b diff --git a/src/ansible_creator/resources/collection_project/plugins/module/hello_world.py.j2 b/src/ansible_creator/resources/collection_project/plugins/module/hello_world.py.j2 new file mode 100644 index 00000000..d4fe2ea4 --- /dev/null +++ b/src/ansible_creator/resources/collection_project/plugins/module/hello_world.py.j2 @@ -0,0 +1,65 @@ +{# module_plugin_template.j2 #} +{%- set module_name = plugin_name | default("hello_world") -%} +{%- set author = author | default("Your Name") -%} +{%- set description = description | default("A custom module plugin for Ansible.") -%} +{%- set license = license | default("GPL-3.0-or-later") -%} +# {{ module_name }}.py - {{ description }} +# Author: {{ author }} +# License: {{ license }} + +from __future__ import absolute_import, annotations, division, print_function + + +__metaclass__ = type # pylint: disable=C0103 + +from typing import TYPE_CHECKING + + +if TYPE_CHECKING: + from typing import Callable + + +DOCUMENTATION = """ + name: {{ module_name }} + author: {{ author }} + version_added: "1.0.0" + short_description: {{ description }} + description: + - This is a demo module plugin designed to return Hello message. + options: + name: + description: Value specified here is appended to the Hello message. + type: str +""" + +EXAMPLES = """ +# {{ module_name }} module example +{% raw %} +- name: Display a hello message + ansible.builtin.debug: + msg: "{{ 'ansible-creator' {%- endraw %} | {{ module_name }} }}" +""" + + +def _hello_world(name: str) -> str: + """Returns Hello message. + + Args: + name: The name to greet. + + Returns: + str: The greeting message. + """ + return "Hello, " + name + + +class ModuleModule: + """module plugin.""" + + def modules(self) -> dict[str, Callable[[str], str]]: + """Map module plugin names to their functions. + + Returns: + dict: The module plugin functions. + """ + return {"{{ module_name }}": _hello_world} diff --git a/src/ansible_creator/subcommands/add.py b/src/ansible_creator/subcommands/add.py index 7fdf5050..565833d8 100644 --- a/src/ansible_creator/subcommands/add.py +++ b/src/ansible_creator/subcommands/add.py @@ -208,7 +208,9 @@ def _plugin_scaffold(self, plugin_path: Path) -> None: elif self._plugin_type == "lookup": template_data = self._get_plugin_template_data() self._perform_lookup_plugin_scaffold(template_data, plugin_path) - + elif self._plugin_type == "module": + template_data = self._get_plugin_template_data() + self._perform_module_plugin_scaffold(template_data, plugin_path) else: msg = f"Unsupported plugin type: {self._plugin_type}" raise CreatorError(msg) @@ -235,6 +237,14 @@ def _perform_filter_plugin_scaffold( resources = (f"collection_project.plugins.{self._plugin_type}",) self._perform_plugin_scaffold(resources, template_data, plugin_path) + def _perform_module_plugin_scaffold( + self, + template_data: TemplateData, + plugin_path: Path, + ) -> None: + resources = (f"collection_project.plugins.{self._plugin_type}",) + self._perform_plugin_scaffold(resources, template_data, plugin_path) + def _perform_lookup_plugin_scaffold( self, template_data: TemplateData, From a6ab28d2aaba0e4684761015d69f0b3199c519e4 Mon Sep 17 00:00:00 2001 From: Shashank Venkat Date: Fri, 14 Feb 2025 13:58:53 +0530 Subject: [PATCH 02/21] Removed add_resource_module function --- src/ansible_creator/arg_parser.py | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/src/ansible_creator/arg_parser.py b/src/ansible_creator/arg_parser.py index 7f082341..c8883900 100644 --- a/src/ansible_creator/arg_parser.py +++ b/src/ansible_creator/arg_parser.py @@ -231,30 +231,6 @@ def _add_resource(self, subparser: SubParser[ArgumentParser]) -> None: self._add_resource_devfile(subparser=subparser) self._add_resource_role(subparser=subparser) self._add_resource_execution_env(subparser=subparser) - #self._add_resource_module(subparser=subparser) - - def _add_resource_module(self, subparser: SubParser[ArgumentParser]) -> None: - """Add generic modules files to an existing Ansible project. - - Args: - subparser: The subparser to add generic modules files to - """ - parser = subparser.add_parser( - "module", - help="Add a generic module to an existing Ansible collection.", - formatter_class=CustomHelpFormatter, - ) - parser.add_argument( - "module_name", - help="The name of the module to add.", - ) - parser.add_argument( - "path", - default="./", - help="The path to the Ansible collection. The default is the current working directory.", - ) - self._add_overwrite(parser) - self._add_args_common(parser) def _add_resource_devcontainer(self, subparser: SubParser[ArgumentParser]) -> None: """Add devcontainer files to an existing Ansible project. From bca85eb4c94a43a8911509344f1ce48dedd05d79 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 14 Feb 2025 08:38:36 +0000 Subject: [PATCH 03/21] chore: auto fixes from pre-commit.com hooks --- src/ansible_creator/arg_parser.py | 4 ++-- src/ansible_creator/subcommands/add.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ansible_creator/arg_parser.py b/src/ansible_creator/arg_parser.py index c8883900..cb4d8b89 100644 --- a/src/ansible_creator/arg_parser.py +++ b/src/ansible_creator/arg_parser.py @@ -231,7 +231,7 @@ def _add_resource(self, subparser: SubParser[ArgumentParser]) -> None: self._add_resource_devfile(subparser=subparser) self._add_resource_role(subparser=subparser) self._add_resource_execution_env(subparser=subparser) - + def _add_resource_devcontainer(self, subparser: SubParser[ArgumentParser]) -> None: """Add devcontainer files to an existing Ansible project. @@ -399,7 +399,7 @@ def _add_plugin_modules(self, subparser: SubParser[ArgumentParser]) -> None: self._add_args_common(parser) self._add_overwrite(parser) self._add_args_plugin_common(parser) - + def _add_plugin_lookup(self, subparser: SubParser[ArgumentParser]) -> None: """Add a lookup plugin to an existing Ansible collection project. diff --git a/src/ansible_creator/subcommands/add.py b/src/ansible_creator/subcommands/add.py index 565833d8..afa74a2d 100644 --- a/src/ansible_creator/subcommands/add.py +++ b/src/ansible_creator/subcommands/add.py @@ -244,7 +244,7 @@ def _perform_module_plugin_scaffold( ) -> None: resources = (f"collection_project.plugins.{self._plugin_type}",) self._perform_plugin_scaffold(resources, template_data, plugin_path) - + def _perform_lookup_plugin_scaffold( self, template_data: TemplateData, From 17aeb88881220ec74a5f458b5cf80ea9c7fdd5d3 Mon Sep 17 00:00:00 2001 From: Shashank Venkat Date: Thu, 20 Feb 2025 13:45:35 +0530 Subject: [PATCH 04/21] Changed Module dir to Sample_Module AND Updated test_add.py --- .../{module => sample_module}/__init__.py.j2 | 0 .../hello_world.py.j2 | 2 +- src/ansible_creator/subcommands/add.py | 6 +- tests/units/test_add.py | 75 +++++++++++++++++++ 4 files changed, 80 insertions(+), 3 deletions(-) rename src/ansible_creator/resources/collection_project/plugins/{module => sample_module}/__init__.py.j2 (100%) rename src/ansible_creator/resources/collection_project/plugins/{module => sample_module}/hello_world.py.j2 (98%) diff --git a/src/ansible_creator/resources/collection_project/plugins/module/__init__.py.j2 b/src/ansible_creator/resources/collection_project/plugins/sample_module/__init__.py.j2 similarity index 100% rename from src/ansible_creator/resources/collection_project/plugins/module/__init__.py.j2 rename to src/ansible_creator/resources/collection_project/plugins/sample_module/__init__.py.j2 diff --git a/src/ansible_creator/resources/collection_project/plugins/module/hello_world.py.j2 b/src/ansible_creator/resources/collection_project/plugins/sample_module/hello_world.py.j2 similarity index 98% rename from src/ansible_creator/resources/collection_project/plugins/module/hello_world.py.j2 rename to src/ansible_creator/resources/collection_project/plugins/sample_module/hello_world.py.j2 index d4fe2ea4..c7493506 100644 --- a/src/ansible_creator/resources/collection_project/plugins/module/hello_world.py.j2 +++ b/src/ansible_creator/resources/collection_project/plugins/sample_module/hello_world.py.j2 @@ -53,7 +53,7 @@ def _hello_world(name: str) -> str: return "Hello, " + name -class ModuleModule: +class SampleModule: """module plugin.""" def modules(self) -> dict[str, Callable[[str], str]]: diff --git a/src/ansible_creator/subcommands/add.py b/src/ansible_creator/subcommands/add.py index afa74a2d..2a35b6ac 100644 --- a/src/ansible_creator/subcommands/add.py +++ b/src/ansible_creator/subcommands/add.py @@ -210,7 +210,9 @@ def _plugin_scaffold(self, plugin_path: Path) -> None: self._perform_lookup_plugin_scaffold(template_data, plugin_path) elif self._plugin_type == "module": template_data = self._get_plugin_template_data() - self._perform_module_plugin_scaffold(template_data, plugin_path) + module_path=self._add_path / "plugins" / "sample_module" + module_path.mkdir(parents=True, exist_ok=True) + self._perform_module_plugin_scaffold(template_data, module_path) else: msg = f"Unsupported plugin type: {self._plugin_type}" raise CreatorError(msg) @@ -242,7 +244,7 @@ def _perform_module_plugin_scaffold( template_data: TemplateData, plugin_path: Path, ) -> None: - resources = (f"collection_project.plugins.{self._plugin_type}",) + resources = (f"collection_project.plugins.sample_module",) self._perform_plugin_scaffold(resources, template_data, plugin_path) def _perform_lookup_plugin_scaffold( diff --git a/tests/units/test_add.py b/tests/units/test_add.py index eb244116..1d04a594 100644 --- a/tests/units/test_add.py +++ b/tests/units/test_add.py @@ -674,6 +674,81 @@ def mock_update_galaxy_dependency() -> None: cmp_result2 = cmp(expected_module_file, effective_module_file, shallow=False) assert cmp_result1, cmp_result2 +def test_run_success_add_plugin_module( + capsys: pytest.CaptureFixture[str], + tmp_path: Path, + cli_args: ConfigDict, + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Test Add.run(). + + Successfully add plugin to path + + Args: + capsys: Pytest fixture to capture stdout and stderr. + tmp_path: Temporary directory path. + cli_args: Dictionary, partial Add class object. + monkeypatch: Pytest monkeypatch fixture. + """ + cli_args["plugin_type"] = "module" + add = Add( + Config(**cli_args), + ) + + # Mock the "_check_collection_path" method + def mock_check_collection_path() -> None: + """Mock function to skip checking collection path.""" + + monkeypatch.setattr( + Add, + "_check_collection_path", + staticmethod(mock_check_collection_path), + ) + add.run() + result = capsys.readouterr().out + assert re.search("Note: Lookup plugin added to", result) is not None + + expected_file = tmp_path / "plugins" / "sample_module" / "hello_world.py" + effective_file = ( + FIXTURES_DIR + / "collection" + / "testorg" + / "testcol" + / "plugins" + / "sample_module" + / "hello_world.py" + ) + cmp_result = cmp(expected_file, effective_file, shallow=False) + assert cmp_result + + conflict_file = tmp_path / "plugins" / "sample_module" / "hello_world.py" + conflict_file.write_text("Author: Your Name") + + # expect a CreatorError when the response to overwrite is no. + monkeypatch.setattr("builtins.input", lambda _: "n") + fail_msg = ( + "The destination directory contains files that will be overwritten." + " Please re-run ansible-creator with --overwrite to continue." + ) + with pytest.raises( + CreatorError, + match=fail_msg, + ): + add.run() + + # expect a warning followed by lookup plugin addition msg + # when response to overwrite is yes. + monkeypatch.setattr("builtins.input", lambda _: "y") + add.run() + result = capsys.readouterr().out + assert ( + re.search( + "already exists", + result, + ) + is not None + ), result + assert re.search("Note: Lookup plugin added to", result) is not None def test_run_error_plugin_no_overwrite( capsys: pytest.CaptureFixture[str], From 92501e072d3118323af11e8a47bbee9131393008 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 20 Feb 2025 08:16:59 +0000 Subject: [PATCH 05/21] chore: auto fixes from pre-commit.com hooks --- src/ansible_creator/subcommands/add.py | 4 ++-- tests/units/test_add.py | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ansible_creator/subcommands/add.py b/src/ansible_creator/subcommands/add.py index 2a35b6ac..7f2d6e74 100644 --- a/src/ansible_creator/subcommands/add.py +++ b/src/ansible_creator/subcommands/add.py @@ -210,7 +210,7 @@ def _plugin_scaffold(self, plugin_path: Path) -> None: self._perform_lookup_plugin_scaffold(template_data, plugin_path) elif self._plugin_type == "module": template_data = self._get_plugin_template_data() - module_path=self._add_path / "plugins" / "sample_module" + module_path = self._add_path / "plugins" / "sample_module" module_path.mkdir(parents=True, exist_ok=True) self._perform_module_plugin_scaffold(template_data, module_path) else: @@ -244,7 +244,7 @@ def _perform_module_plugin_scaffold( template_data: TemplateData, plugin_path: Path, ) -> None: - resources = (f"collection_project.plugins.sample_module",) + resources = ("collection_project.plugins.sample_module",) self._perform_plugin_scaffold(resources, template_data, plugin_path) def _perform_lookup_plugin_scaffold( diff --git a/tests/units/test_add.py b/tests/units/test_add.py index 1d04a594..94417648 100644 --- a/tests/units/test_add.py +++ b/tests/units/test_add.py @@ -674,6 +674,7 @@ def mock_update_galaxy_dependency() -> None: cmp_result2 = cmp(expected_module_file, effective_module_file, shallow=False) assert cmp_result1, cmp_result2 + def test_run_success_add_plugin_module( capsys: pytest.CaptureFixture[str], tmp_path: Path, @@ -750,6 +751,7 @@ def mock_check_collection_path() -> None: ), result assert re.search("Note: Lookup plugin added to", result) is not None + def test_run_error_plugin_no_overwrite( capsys: pytest.CaptureFixture[str], tmp_path: Path, From febf1c7b334825a1e08734ad21a737e0761b853f Mon Sep 17 00:00:00 2001 From: Shashank Venkat Date: Thu, 20 Feb 2025 14:00:41 +0530 Subject: [PATCH 06/21] updated test_add.py --- tests/units/test_add.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/units/test_add.py b/tests/units/test_add.py index 94417648..5d83b12e 100644 --- a/tests/units/test_add.py +++ b/tests/units/test_add.py @@ -707,7 +707,7 @@ def mock_check_collection_path() -> None: ) add.run() result = capsys.readouterr().out - assert re.search("Note: Lookup plugin added to", result) is not None + assert re.search("Note: Module plugin added to", result) is not None expected_file = tmp_path / "plugins" / "sample_module" / "hello_world.py" effective_file = ( From cd0e54476506b968330c30a771ce1ce5e4b511c6 Mon Sep 17 00:00:00 2001 From: Shashank Venkat Date: Thu, 20 Feb 2025 14:49:53 +0530 Subject: [PATCH 07/21] Added sample_module dir under test/fixtures --- .../testcol/plugins/sample_module/__init__.py | 0 .../plugins/sample_module/hello_world.py | 35 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 tests/fixtures/collection/testorg/testcol/plugins/sample_module/__init__.py create mode 100644 tests/fixtures/collection/testorg/testcol/plugins/sample_module/hello_world.py diff --git a/tests/fixtures/collection/testorg/testcol/plugins/sample_module/__init__.py b/tests/fixtures/collection/testorg/testcol/plugins/sample_module/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/fixtures/collection/testorg/testcol/plugins/sample_module/hello_world.py b/tests/fixtures/collection/testorg/testcol/plugins/sample_module/hello_world.py new file mode 100644 index 00000000..92b4dfa3 --- /dev/null +++ b/tests/fixtures/collection/testorg/testcol/plugins/sample_module/hello_world.py @@ -0,0 +1,35 @@ +# hello_world.py +# GNU General Public License v3.0+ + +DOCUMENTATION = """ + module: hello_world + author: Your Name (@username) + version_added: "1.0.0" + short_description: A custom module plugin for Ansible. + description: + - This is a custom module plugin to provide module functionality. + options: + prefix: + description: + - A string that is added as a prefix to the message passed to the module. + type: str + msg: + description: The message to display in the output. + type: str + with_prefix: + description: + - A boolean flag indicating whether to include the prefix in the message. + type: bool + notes: + - This is a scaffold template. Customize the plugin to fit your needs. +""" + +EXAMPLES = """ +- name: Example Module Plugin + hosts: localhost + tasks: + - name: Example hello_world plugin + with_prefix: + prefix: "Hello, World" + msg: "Ansible!" +""" From 37821e287237ddf47be3f913d8b4d5666d6b6b6b Mon Sep 17 00:00:00 2001 From: Shashank Venkat Date: Thu, 20 Feb 2025 15:06:10 +0530 Subject: [PATCH 08/21] updated hello_world.py under tests/fixtures/plugins/sample_module --- .../plugins/sample_module/hello_world.py | 73 +++++++++++++------ 1 file changed, 49 insertions(+), 24 deletions(-) diff --git a/tests/fixtures/collection/testorg/testcol/plugins/sample_module/hello_world.py b/tests/fixtures/collection/testorg/testcol/plugins/sample_module/hello_world.py index 92b4dfa3..40d6e736 100644 --- a/tests/fixtures/collection/testorg/testcol/plugins/sample_module/hello_world.py +++ b/tests/fixtures/collection/testorg/testcol/plugins/sample_module/hello_world.py @@ -1,35 +1,60 @@ -# hello_world.py -# GNU General Public License v3.0+ +# hello_world.py - A custom module plugin for Ansible. +# Author: Your Name +# License: GPL-3.0-or-later + +from __future__ import absolute_import, annotations, division, print_function + + +__metaclass__ = type # pylint: disable=C0103 + +from typing import TYPE_CHECKING + + +if TYPE_CHECKING: + from typing import Callable + DOCUMENTATION = """ - module: hello_world - author: Your Name (@username) + name: hello_world + author: Your Name version_added: "1.0.0" short_description: A custom module plugin for Ansible. description: - - This is a custom module plugin to provide module functionality. + - This is a demo module plugin designed to return Hello message. options: - prefix: - description: - - A string that is added as a prefix to the message passed to the module. + name: + description: Value specified here is appended to the Hello message. type: str - msg: - description: The message to display in the output. - type: str - with_prefix: - description: - - A boolean flag indicating whether to include the prefix in the message. - type: bool - notes: - - This is a scaffold template. Customize the plugin to fit your needs. """ EXAMPLES = """ -- name: Example Module Plugin - hosts: localhost - tasks: - - name: Example hello_world plugin - with_prefix: - prefix: "Hello, World" - msg: "Ansible!" +# hello_world module example + +- name: Display a hello message + ansible.builtin.debug: + msg: "{{ 'ansible-creator' | hello_world }}" """ + + +def _hello_world(name: str) -> str: + """Returns Hello message. + + Args: + name: The name to greet. + + Returns: + str: The greeting message. + """ + return "Hello, " + name + + +class SampleModule: + """module plugin.""" + + def modules(self) -> dict[str, Callable[[str], str]]: + """Map module plugin names to their functions. + + Returns: + dict: The module plugin functions. + """ + return {"hello_world": _hello_world} From 5d68a41207791be67c660986ac27d2c6626aca68 Mon Sep 17 00:00:00 2001 From: Shashank Venkat Date: Thu, 20 Feb 2025 15:11:51 +0530 Subject: [PATCH 09/21] updated test_add.py --- tests/units/test_add.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/units/test_add.py b/tests/units/test_add.py index 5d83b12e..7089ca5b 100644 --- a/tests/units/test_add.py +++ b/tests/units/test_add.py @@ -749,7 +749,7 @@ def mock_check_collection_path() -> None: ) is not None ), result - assert re.search("Note: Lookup plugin added to", result) is not None + assert re.search("Note: Module plugin added to", result) is not None def test_run_error_plugin_no_overwrite( From 087a9ca282d9e46de305af9ba4d78963398dc1b0 Mon Sep 17 00:00:00 2001 From: Shashank Venkat Date: Thu, 20 Feb 2025 16:33:09 +0530 Subject: [PATCH 10/21] Updated test_add.py and arg_parser.py --- src/ansible_creator/arg_parser.py | 4 ++-- .../plugins/sample_module/hello_world.py.j2 | 2 +- .../testorg/testcol/plugins/sample_module/hello_world.py | 4 ++-- tests/units/test_add.py | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ansible_creator/arg_parser.py b/src/ansible_creator/arg_parser.py index cb4d8b89..e1806fe7 100644 --- a/src/ansible_creator/arg_parser.py +++ b/src/ansible_creator/arg_parser.py @@ -353,7 +353,7 @@ def _add_plugin(self, subparser: SubParser[ArgumentParser]) -> None: self._add_plugin_action(subparser=subparser) self._add_plugin_filter(subparser=subparser) self._add_plugin_lookup(subparser=subparser) - self._add_plugin_modules(subparser=subparser) + self._add_plugin_module(subparser=subparser) def _add_plugin_action(self, subparser: SubParser[ArgumentParser]) -> None: """Add an action plugin to an existing Ansible collection project. @@ -385,7 +385,7 @@ def _add_plugin_filter(self, subparser: SubParser[ArgumentParser]) -> None: self._add_overwrite(parser) self._add_args_plugin_common(parser) - def _add_plugin_modules(self, subparser: SubParser[ArgumentParser]) -> None: + def _add_plugin_module(self, subparser: SubParser[ArgumentParser]) -> None: """Add a module plugin to an existing Ansible collection project. Args: diff --git a/src/ansible_creator/resources/collection_project/plugins/sample_module/hello_world.py.j2 b/src/ansible_creator/resources/collection_project/plugins/sample_module/hello_world.py.j2 index c7493506..dc3afa03 100644 --- a/src/ansible_creator/resources/collection_project/plugins/sample_module/hello_world.py.j2 +++ b/src/ansible_creator/resources/collection_project/plugins/sample_module/hello_world.py.j2 @@ -1,6 +1,6 @@ {# module_plugin_template.j2 #} {%- set module_name = plugin_name | default("hello_world") -%} -{%- set author = author | default("Your Name") -%} +{%- set author = author | default("Your Name (@username)") -%} {%- set description = description | default("A custom module plugin for Ansible.") -%} {%- set license = license | default("GPL-3.0-or-later") -%} # {{ module_name }}.py - {{ description }} diff --git a/tests/fixtures/collection/testorg/testcol/plugins/sample_module/hello_world.py b/tests/fixtures/collection/testorg/testcol/plugins/sample_module/hello_world.py index 40d6e736..949aa2de 100644 --- a/tests/fixtures/collection/testorg/testcol/plugins/sample_module/hello_world.py +++ b/tests/fixtures/collection/testorg/testcol/plugins/sample_module/hello_world.py @@ -1,5 +1,5 @@ # hello_world.py - A custom module plugin for Ansible. -# Author: Your Name +# Author: Your Name (@username) # License: GPL-3.0-or-later from __future__ import absolute_import, annotations, division, print_function @@ -16,7 +16,7 @@ DOCUMENTATION = """ name: hello_world - author: Your Name + author: Your Name (@username) version_added: "1.0.0" short_description: A custom module plugin for Ansible. description: diff --git a/tests/units/test_add.py b/tests/units/test_add.py index 7089ca5b..468283a3 100644 --- a/tests/units/test_add.py +++ b/tests/units/test_add.py @@ -737,7 +737,7 @@ def mock_check_collection_path() -> None: ): add.run() - # expect a warning followed by lookup plugin addition msg + # expect a warning followed by module plugin addition msg # when response to overwrite is yes. monkeypatch.setattr("builtins.input", lambda _: "y") add.run() From 79747e9af0d4a2db7696e7aa7b8eb799e9620ccc Mon Sep 17 00:00:00 2001 From: Shashank Venkat Date: Thu, 20 Feb 2025 17:04:18 +0530 Subject: [PATCH 11/21] Updated docs --- .vscode/launch.json | 7 ++++--- docs/installing.md | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index a6a60f98..57b3a84b 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -31,9 +31,10 @@ "module": "ansible_creator", "args": [ "add", - "resource", - "devcontainer", - "/home/user/..path/to/your/existing_project" + "plugin", + "module", + "plugin_name", + "/Users/shvenkat/trial" ], "cwd": "${workspaceFolder}/src", "justMyCode": false diff --git a/docs/installing.md b/docs/installing.md index d866661d..d4f37a5c 100644 --- a/docs/installing.md +++ b/docs/installing.md @@ -330,3 +330,28 @@ $ ansible-creator add resource devfile /home/user/..path/to/your/existing_projec ``` This command will scaffold the devfile.yaml file at `/home/user/..path/to/your/existing_project` + +### Add support to scaffold a plugin in an existing project + +The `add plugin` command enables you to add a plugin to an already existing project. Use the following command template: + +```console +$ ansible-creator add plugin +``` + +#### Positional Arguments + +| Parameter | Description | +| ------------ | ---------------------------------------------------------- | +| action | Add an action plugin to an existing Ansible project. | +| filter | Add a filter plugin to an existing Ansible project. | +| lookup | Add a lookup plugin to an existing Ansible project. | +| module | Add a generic module plugin to an existing Ansible project.| + +#### Example + +```console +$ ansible-creator add plugin module module_name /home/user/..path/to/your/existing_project +``` + +This command will scaffold the devfile.yaml file at `/home/user/..path/to/your/existing_project` \ No newline at end of file From 949626307dce2fc96459f9e5196a5872c66b349b Mon Sep 17 00:00:00 2001 From: Shashank Venkat Date: Thu, 20 Feb 2025 17:06:11 +0530 Subject: [PATCH 12/21] changed launch.json --- .vscode/launch.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 57b3a84b..cbc10c7f 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -34,7 +34,7 @@ "plugin", "module", "plugin_name", - "/Users/shvenkat/trial" + "/home/user/..path/to/your/new_playbook_project" ], "cwd": "${workspaceFolder}/src", "justMyCode": false From 9c0736a17dbbc9ecf1f63739eb1a10c27632c4c1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 20 Feb 2025 11:39:45 +0000 Subject: [PATCH 13/21] chore: auto fixes from pre-commit.com hooks --- docs/installing.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/installing.md b/docs/installing.md index d4f37a5c..05ad21d8 100644 --- a/docs/installing.md +++ b/docs/installing.md @@ -341,12 +341,12 @@ $ ansible-creator add plugin #### Positional Arguments -| Parameter | Description | -| ------------ | ---------------------------------------------------------- | -| action | Add an action plugin to an existing Ansible project. | -| filter | Add a filter plugin to an existing Ansible project. | -| lookup | Add a lookup plugin to an existing Ansible project. | -| module | Add a generic module plugin to an existing Ansible project.| +| Parameter | Description | +| --------- | ----------------------------------------------------------- | +| action | Add an action plugin to an existing Ansible project. | +| filter | Add a filter plugin to an existing Ansible project. | +| lookup | Add a lookup plugin to an existing Ansible project. | +| module | Add a generic module plugin to an existing Ansible project. | #### Example @@ -354,4 +354,4 @@ $ ansible-creator add plugin $ ansible-creator add plugin module module_name /home/user/..path/to/your/existing_project ``` -This command will scaffold the devfile.yaml file at `/home/user/..path/to/your/existing_project` \ No newline at end of file +This command will scaffold the devfile.yaml file at `/home/user/..path/to/your/existing_project` From 7421b6b225d8453d97dc0cdc0d00c842ae2a6541 Mon Sep 17 00:00:00 2001 From: Shashank Venkat Date: Thu, 20 Feb 2025 17:28:09 +0530 Subject: [PATCH 14/21] Reverted launch.json changes --- .vscode/launch.json | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index cbc10c7f..a6a60f98 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -31,10 +31,9 @@ "module": "ansible_creator", "args": [ "add", - "plugin", - "module", - "plugin_name", - "/home/user/..path/to/your/new_playbook_project" + "resource", + "devcontainer", + "/home/user/..path/to/your/existing_project" ], "cwd": "${workspaceFolder}/src", "justMyCode": false From e752cd95ca41059ae0625411f99e857c76190579 Mon Sep 17 00:00:00 2001 From: Shashank Venkat Date: Thu, 20 Feb 2025 17:51:00 +0530 Subject: [PATCH 15/21] Updated docs --- docs/installing.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/installing.md b/docs/installing.md index 05ad21d8..e8a09696 100644 --- a/docs/installing.md +++ b/docs/installing.md @@ -331,22 +331,22 @@ $ ansible-creator add resource devfile /home/user/..path/to/your/existing_projec This command will scaffold the devfile.yaml file at `/home/user/..path/to/your/existing_project` -### Add support to scaffold a plugin in an existing project +### Add support to scaffold plugins in an existing ansible collection -The `add plugin` command enables you to add a plugin to an already existing project. Use the following command template: +The `add plugin` command enables you to add a plugin to an to an existing collection project. Use the following command template: ```console -$ ansible-creator add plugin +$ ansible-creator add plugin ``` #### Positional Arguments -| Parameter | Description | -| --------- | ----------------------------------------------------------- | -| action | Add an action plugin to an existing Ansible project. | -| filter | Add a filter plugin to an existing Ansible project. | -| lookup | Add a lookup plugin to an existing Ansible project. | -| module | Add a generic module plugin to an existing Ansible project. | +| Parameter | Description | +| --------- | -------------------------------------------------------------- | +| action | Add an action plugin to an existing Ansible Collection. | +| filter | Add a filter plugin to an existing Ansible Collection. | +| lookup | Add a lookup plugin to an existing Ansible Collection. | +| module | Add a generic module plugin to an existing Ansible Collection. | #### Example From 4200a7b33ddd1c8f539a2d5aa9bdbbc967634fe6 Mon Sep 17 00:00:00 2001 From: Shashank Venkat Date: Thu, 20 Feb 2025 18:13:38 +0530 Subject: [PATCH 16/21] Fixed docs --- docs/installing.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/installing.md b/docs/installing.md index e8a09696..8dddd3c7 100644 --- a/docs/installing.md +++ b/docs/installing.md @@ -351,7 +351,7 @@ $ ansible-creator add plugin #### Example ```console -$ ansible-creator add plugin module module_name /home/user/..path/to/your/existing_project +$ ansible-creator add plugin module plugin_name /home/user/..path/to/your/existing_project ``` -This command will scaffold the devfile.yaml file at `/home/user/..path/to/your/existing_project` +This command will scaffold a generic module plugin at `/home/user/..path/to/your/existing_project` From d474e2d5f369594e3a20f48631dec6f2a1ffc380 Mon Sep 17 00:00:00 2001 From: Shashank Venkat Date: Tue, 25 Feb 2025 10:50:05 +0530 Subject: [PATCH 17/21] Fixed docs, args_parser.py and add.py --- docs/installing.md | 4 ++-- src/ansible_creator/arg_parser.py | 23 ++++++++++++----------- src/ansible_creator/subcommands/add.py | 10 +++++----- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/docs/installing.md b/docs/installing.md index 8dddd3c7..02baa149 100644 --- a/docs/installing.md +++ b/docs/installing.md @@ -333,7 +333,7 @@ This command will scaffold the devfile.yaml file at `/home/user/..path/to/your/e ### Add support to scaffold plugins in an existing ansible collection -The `add plugin` command enables you to add a plugin to an to an existing collection project. Use the following command template: +The `add plugin` command enables you to add a plugin to an existing collection project. Use the following command template: ```console $ ansible-creator add plugin @@ -351,7 +351,7 @@ $ ansible-creator add plugin #### Example ```console -$ ansible-creator add plugin module plugin_name /home/user/..path/to/your/existing_project +$ ansible-creator add plugin module test_plugin /home/user/..path/to/your/existing_project ``` This command will scaffold a generic module plugin at `/home/user/..path/to/your/existing_project` diff --git a/src/ansible_creator/arg_parser.py b/src/ansible_creator/arg_parser.py index e1806fe7..a165af06 100644 --- a/src/ansible_creator/arg_parser.py +++ b/src/ansible_creator/arg_parser.py @@ -385,36 +385,37 @@ def _add_plugin_filter(self, subparser: SubParser[ArgumentParser]) -> None: self._add_overwrite(parser) self._add_args_plugin_common(parser) - def _add_plugin_module(self, subparser: SubParser[ArgumentParser]) -> None: - """Add a module plugin to an existing Ansible collection project. + def _add_plugin_lookup(self, subparser: SubParser[ArgumentParser]) -> None: + """Add a lookup plugin to an existing Ansible collection project. Args: - subparser: The subparser to add module plugin to + subparser: The subparser to add lookup plugin to """ parser = subparser.add_parser( - "module", - help="Add a module plugin to an existing Ansible collection.", + "lookup", + help="Add a lookup plugin to an existing Ansible collection.", formatter_class=CustomHelpFormatter, ) self._add_args_common(parser) self._add_overwrite(parser) self._add_args_plugin_common(parser) - def _add_plugin_lookup(self, subparser: SubParser[ArgumentParser]) -> None: - """Add a lookup plugin to an existing Ansible collection project. + + def _add_plugin_module(self, subparser: SubParser[ArgumentParser]) -> None: + """Add a module plugin to an existing Ansible collection project. Args: - subparser: The subparser to add lookup plugin to + subparser: The subparser to add module plugin to """ parser = subparser.add_parser( - "lookup", - help="Add a lookup plugin to an existing Ansible collection.", + "module", + help="Add a module plugin to an existing Ansible collection.", formatter_class=CustomHelpFormatter, ) self._add_args_common(parser) self._add_overwrite(parser) self._add_args_plugin_common(parser) - + def _add_overwrite(self, parser: ArgumentParser) -> None: """Add overwrite and no-overwrite arguments to the parser. diff --git a/src/ansible_creator/subcommands/add.py b/src/ansible_creator/subcommands/add.py index 7f2d6e74..f7cec732 100644 --- a/src/ansible_creator/subcommands/add.py +++ b/src/ansible_creator/subcommands/add.py @@ -239,22 +239,22 @@ def _perform_filter_plugin_scaffold( resources = (f"collection_project.plugins.{self._plugin_type}",) self._perform_plugin_scaffold(resources, template_data, plugin_path) - def _perform_module_plugin_scaffold( + def _perform_lookup_plugin_scaffold( self, template_data: TemplateData, plugin_path: Path, ) -> None: - resources = ("collection_project.plugins.sample_module",) + resources = (f"collection_project.plugins.{self._plugin_type}",) self._perform_plugin_scaffold(resources, template_data, plugin_path) - def _perform_lookup_plugin_scaffold( + def _perform_module_plugin_scaffold( self, template_data: TemplateData, plugin_path: Path, ) -> None: - resources = (f"collection_project.plugins.{self._plugin_type}",) + resources = ("collection_project.plugins.sample_module",) self._perform_plugin_scaffold(resources, template_data, plugin_path) - + def _perform_plugin_scaffold( self, resources: tuple[str, ...], From 080e277d15f08e6f7c4d79a8fa7831e168c0b322 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 25 Feb 2025 05:20:53 +0000 Subject: [PATCH 18/21] chore: auto fixes from pre-commit.com hooks --- src/ansible_creator/arg_parser.py | 3 +-- src/ansible_creator/subcommands/add.py | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ansible_creator/arg_parser.py b/src/ansible_creator/arg_parser.py index a165af06..3e3fb9fc 100644 --- a/src/ansible_creator/arg_parser.py +++ b/src/ansible_creator/arg_parser.py @@ -400,7 +400,6 @@ def _add_plugin_lookup(self, subparser: SubParser[ArgumentParser]) -> None: self._add_overwrite(parser) self._add_args_plugin_common(parser) - def _add_plugin_module(self, subparser: SubParser[ArgumentParser]) -> None: """Add a module plugin to an existing Ansible collection project. @@ -415,7 +414,7 @@ def _add_plugin_module(self, subparser: SubParser[ArgumentParser]) -> None: self._add_args_common(parser) self._add_overwrite(parser) self._add_args_plugin_common(parser) - + def _add_overwrite(self, parser: ArgumentParser) -> None: """Add overwrite and no-overwrite arguments to the parser. diff --git a/src/ansible_creator/subcommands/add.py b/src/ansible_creator/subcommands/add.py index f7cec732..2abbd2a1 100644 --- a/src/ansible_creator/subcommands/add.py +++ b/src/ansible_creator/subcommands/add.py @@ -254,7 +254,7 @@ def _perform_module_plugin_scaffold( ) -> None: resources = ("collection_project.plugins.sample_module",) self._perform_plugin_scaffold(resources, template_data, plugin_path) - + def _perform_plugin_scaffold( self, resources: tuple[str, ...], From 7dc038130e44d7eebc34f419b307dfc276c263de Mon Sep 17 00:00:00 2001 From: Shashank Venkat Date: Tue, 25 Feb 2025 11:00:43 +0530 Subject: [PATCH 19/21] Updated add.py --- src/ansible_creator/subcommands/add.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ansible_creator/subcommands/add.py b/src/ansible_creator/subcommands/add.py index 2abbd2a1..775418d0 100644 --- a/src/ansible_creator/subcommands/add.py +++ b/src/ansible_creator/subcommands/add.py @@ -210,9 +210,7 @@ def _plugin_scaffold(self, plugin_path: Path) -> None: self._perform_lookup_plugin_scaffold(template_data, plugin_path) elif self._plugin_type == "module": template_data = self._get_plugin_template_data() - module_path = self._add_path / "plugins" / "sample_module" - module_path.mkdir(parents=True, exist_ok=True) - self._perform_module_plugin_scaffold(template_data, module_path) + self._perform_module_plugin_scaffold(template_data, plugin_path) else: msg = f"Unsupported plugin type: {self._plugin_type}" raise CreatorError(msg) @@ -252,8 +250,10 @@ def _perform_module_plugin_scaffold( template_data: TemplateData, plugin_path: Path, ) -> None: + module_path = self._add_path / "plugins" / "sample_module" + module_path.mkdir(parents=True, exist_ok=True) resources = ("collection_project.plugins.sample_module",) - self._perform_plugin_scaffold(resources, template_data, plugin_path) + self._perform_plugin_scaffold(resources, template_data, module_path) def _perform_plugin_scaffold( self, From 686180804d2c1c29c9a177a6ca23b8901edcd49f Mon Sep 17 00:00:00 2001 From: Shashank Venkat Date: Tue, 25 Feb 2025 12:03:53 +0530 Subject: [PATCH 20/21] Update add.py --- src/ansible_creator/subcommands/add.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/ansible_creator/subcommands/add.py b/src/ansible_creator/subcommands/add.py index 775418d0..92d485c3 100644 --- a/src/ansible_creator/subcommands/add.py +++ b/src/ansible_creator/subcommands/add.py @@ -208,6 +208,7 @@ def _plugin_scaffold(self, plugin_path: Path) -> None: elif self._plugin_type == "lookup": template_data = self._get_plugin_template_data() self._perform_lookup_plugin_scaffold(template_data, plugin_path) + elif self._plugin_type == "module": template_data = self._get_plugin_template_data() self._perform_module_plugin_scaffold(template_data, plugin_path) @@ -250,10 +251,10 @@ def _perform_module_plugin_scaffold( template_data: TemplateData, plugin_path: Path, ) -> None: - module_path = self._add_path / "plugins" / "sample_module" - module_path.mkdir(parents=True, exist_ok=True) + plugin_path = self._add_path / "plugins" / "sample_module" + plugin_path.mkdir(parents=True, exist_ok=True) resources = ("collection_project.plugins.sample_module",) - self._perform_plugin_scaffold(resources, template_data, module_path) + self._perform_plugin_scaffold(resources, template_data, plugin_path) def _perform_plugin_scaffold( self, From 720f66ae5b29fc9a73c66a214d3019ee2347488d Mon Sep 17 00:00:00 2001 From: Shashank Venkat Date: Tue, 25 Feb 2025 12:12:35 +0530 Subject: [PATCH 21/21] Reverted changes in add.py --- src/ansible_creator/subcommands/add.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ansible_creator/subcommands/add.py b/src/ansible_creator/subcommands/add.py index 92d485c3..57b5c522 100644 --- a/src/ansible_creator/subcommands/add.py +++ b/src/ansible_creator/subcommands/add.py @@ -211,6 +211,8 @@ def _plugin_scaffold(self, plugin_path: Path) -> None: elif self._plugin_type == "module": template_data = self._get_plugin_template_data() + plugin_path = self._add_path / "plugins" / "sample_module" + plugin_path.mkdir(parents=True, exist_ok=True) self._perform_module_plugin_scaffold(template_data, plugin_path) else: msg = f"Unsupported plugin type: {self._plugin_type}" @@ -251,8 +253,6 @@ def _perform_module_plugin_scaffold( template_data: TemplateData, plugin_path: Path, ) -> None: - plugin_path = self._add_path / "plugins" / "sample_module" - plugin_path.mkdir(parents=True, exist_ok=True) resources = ("collection_project.plugins.sample_module",) self._perform_plugin_scaffold(resources, template_data, plugin_path)