-
Notifications
You must be signed in to change notification settings - Fork 35
feat: generation of python files for custom search command #1697
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
hetangmodi-crest
merged 8 commits into
feat/add-validations-for-csc
from
feat/generation-of-python-files-for-csc
May 9, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b6a294e
feat: generation of python files
hetangmodi-crest 57a9f41
tests: added respective smoke and unit test case
hetangmodi-crest 7f28185
Merge branch 'feat/add-validations-for-csc' into feat/generation-of-p…
hetangmodi-crest 2744a17
fix: fix unit test case
hetangmodi-crest 6977f59
chore: renamed template file
hetangmodi-crest 00d5945
Merge branch 'feat/add-validations-for-csc' into feat/generation-of-p…
hetangmodi-crest 148302f
chore: use variable instead of updating key
hetangmodi-crest 370b48a
Merge branch 'feat/add-validations-for-csc' into feat/generation-of-p…
hetangmodi-crest File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
splunk_add_on_ucc_framework/generators/python_files/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # | ||
| # Copyright 2025 Splunk Inc. | ||
| # | ||
| # Licensed 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. | ||
| # | ||
| from ..file_generator import FileGenerator | ||
| from .create_custom_command_python import CustomCommandPy | ||
|
|
||
| __all__ = ["FileGenerator", "CustomCommandPy"] |
115 changes: 115 additions & 0 deletions
115
splunk_add_on_ucc_framework/generators/python_files/create_custom_command_python.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| # | ||
| # Copyright 2025 Splunk Inc. | ||
| # | ||
| # Licensed 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. | ||
| # | ||
| from typing import Any, Dict, List | ||
|
|
||
| from splunk_add_on_ucc_framework.generators.file_generator import FileGenerator | ||
|
|
||
|
|
||
| class CustomCommandPy(FileGenerator): | ||
| __description__ = "Generates Python files for custom search commands provided in the globalConfig." | ||
|
|
||
| def argument_generator( | ||
| self, argument_list: List[str], arg: Dict[str, Any] | ||
| ) -> List[str]: | ||
| validate_str = "" | ||
| validate = arg.get("validate", {}) | ||
| if validate: | ||
| validate_type = validate["type"] | ||
| if validate_type in ("Integer", "Float"): | ||
| min_val = validate.get("minimum") | ||
| max_val = validate.get("maximum") | ||
| args = [] | ||
| if min_val is not None: | ||
| args.append(f"minimum={min_val}") | ||
| if max_val is not None: | ||
| args.append(f"maximum={max_val}") | ||
| validate_args = ", ".join(args) | ||
| validate_str = ( | ||
| f", validate=validators.{validate_type}({validate_args})" | ||
| if args | ||
| else f", validate=validators.{validate_type}()" | ||
| ) | ||
| elif validate_type: | ||
| validate_str = f", validate=validators.{validate_type}()" | ||
|
|
||
| if arg["default"] is None: | ||
| arg_str = ( | ||
| f"{arg['name']} = Option(name='{arg['name']}', " | ||
| f"require={arg.get('require')}" | ||
| f"{validate_str})" | ||
| ) | ||
| else: | ||
| arg_str = ( | ||
| f"{arg['name']} = Option(name='{arg['name']}', " | ||
| f"require={arg.get('require')}" | ||
| f"{validate_str}, " | ||
| f"default='{arg.get('default', '')}')" | ||
| ) | ||
| argument_list.append(arg_str) | ||
| return argument_list | ||
|
|
||
| def _set_attributes(self, **kwargs: Any) -> None: | ||
| self.commands_info = [] | ||
sgoral-splunk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| for command in self._global_config.custom_search_commands: | ||
| argument_list: List[str] = [] | ||
| imported_file_name = command["fileName"].replace(".py", "") | ||
| template = command["commandType"].replace(" ", "_") + ".template" | ||
| for argument in command["arguments"]: | ||
| argument_dict = { | ||
| "name": argument["name"], | ||
| "require": argument.get("required", False), | ||
| "validate": argument.get("validate"), | ||
| "default": argument.get("defaultValue"), | ||
| } | ||
| self.argument_generator(argument_list, argument_dict) | ||
| self.commands_info.append( | ||
| { | ||
| "imported_file_name": imported_file_name, | ||
| "file_name": command["commandName"], | ||
| "class_name": command["commandName"].title(), | ||
| "description": command.get("description"), | ||
| "syntax": command.get("syntax"), | ||
| "template": template, | ||
| "list_arg": argument_list, | ||
| } | ||
| ) | ||
|
|
||
| def generate(self) -> Dict[str, str]: | ||
| if not self.commands_info: | ||
| return {} | ||
|
|
||
| generated_files = {} | ||
| for command_info in self.commands_info: | ||
| file_name = command_info["file_name"] + ".py" | ||
| file_path = self.get_file_output_path(["bin", file_name]) | ||
| self.set_template_and_render( | ||
| template_file_path=["custom_command"], | ||
| file_name=command_info["template"], | ||
| ) | ||
| rendered_content = self._template.render( | ||
| imported_file_name=command_info["imported_file_name"], | ||
| class_name=command_info["class_name"], | ||
| description=command_info["description"], | ||
| syntax=command_info["syntax"], | ||
| list_arg=command_info["list_arg"], | ||
| ) | ||
| self.writer( | ||
| file_name=file_name, | ||
| file_path=file_path, | ||
| content=rendered_content, | ||
| ) | ||
| generated_files.update({file_name: file_path}) | ||
| return generated_files | ||
33 changes: 33 additions & 0 deletions
33
splunk_add_on_ucc_framework/templates/custom_command/dataset_processing.template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import sys | ||
| import import_declare_test | ||
|
|
||
| from splunklib.searchcommands import \ | ||
| dispatch, EventingCommand, Configuration, Option, validators | ||
| from {{imported_file_name}} import transform | ||
|
|
||
| @Configuration() | ||
| class {{class_name}}Command(EventingCommand): | ||
| {% if syntax or description%} | ||
| """ | ||
|
|
||
| {% if syntax %} | ||
| ##Syntax | ||
| {{syntax}} | ||
| {% endif %} | ||
|
|
||
| {% if description %} | ||
| ##Description | ||
| {{description}} | ||
| {% endif %} | ||
|
|
||
| """ | ||
| {% endif %} | ||
|
|
||
| {% for arg in list_arg %} | ||
| {{arg}} | ||
| {% endfor %} | ||
|
|
||
| def transform(self, events): | ||
| return transform(self, events) | ||
|
|
||
| dispatch({{class_name}}Command, sys.argv, sys.stdin, sys.stdout, __name__) |
32 changes: 32 additions & 0 deletions
32
splunk_add_on_ucc_framework/templates/custom_command/generating.template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import sys | ||
| import import_declare_test | ||
|
|
||
| from splunklib.searchcommands import \ | ||
| dispatch, GeneratingCommand, Configuration, Option, validators | ||
| from {{imported_file_name}} import generate | ||
|
|
||
| @Configuration() | ||
| class {{class_name}}Command(GeneratingCommand): | ||
| {% if syntax or description%} | ||
| """ | ||
|
|
||
| {% if syntax %} | ||
| ##Syntax | ||
| {{syntax}} | ||
| {% endif %} | ||
|
|
||
| {% if description %} | ||
| ##Description | ||
| {{description}} | ||
| {% endif %} | ||
|
|
||
| """ | ||
| {% endif %} | ||
| {% for arg in list_arg %} | ||
| {{arg}} | ||
| {% endfor %} | ||
|
|
||
| def generate(self): | ||
| return generate(self) | ||
|
|
||
| dispatch({{class_name}}Command, sys.argv, sys.stdin, sys.stdout, __name__) |
33 changes: 33 additions & 0 deletions
33
splunk_add_on_ucc_framework/templates/custom_command/streaming.template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import sys | ||
| import import_declare_test | ||
|
|
||
| from splunklib.searchcommands import \ | ||
| dispatch, StreamingCommand, Configuration, Option, validators | ||
| from {{imported_file_name}} import stream | ||
|
|
||
| @Configuration() | ||
| class {{class_name}}Command(StreamingCommand): | ||
| {% if syntax or description%} | ||
| """ | ||
|
|
||
| {% if syntax %} | ||
| ##Syntax | ||
| {{syntax}} | ||
| {% endif %} | ||
|
|
||
| {% if description %} | ||
| ##Description | ||
| {{description}} | ||
| {% endif %} | ||
|
|
||
| """ | ||
| {% endif %} | ||
|
|
||
| {% for arg in list_arg %} | ||
| {{arg}} | ||
| {% endfor %} | ||
|
|
||
| def stream(self, events): | ||
| return stream(self, events) | ||
|
|
||
| dispatch({{class_name}}Command, sys.argv, sys.stdin, sys.stdout, __name__) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
..._addons/expected_output_global_config_everything/Splunk_TA_UCCExample/bin/countmatches.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| def stream(self, records): | ||
| for record in records: | ||
| # write custom logic for the search command | ||
| yield record |
17 changes: 17 additions & 0 deletions
17
.../expected_output_global_config_everything/Splunk_TA_UCCExample/bin/countmatchescommand.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import sys | ||
| import import_declare_test | ||
|
|
||
| from splunklib.searchcommands import \ | ||
| dispatch, StreamingCommand, Configuration, Option, validators | ||
| from countmatches import stream | ||
|
|
||
| @Configuration() | ||
| class CountmatchescommandCommand(StreamingCommand): | ||
|
|
||
| fieldname = Option(name='fieldname', require=True, validate=validators.Fieldname()) | ||
| pattern = Option(name='pattern', require=True, validate=validators.RegularExpression()) | ||
|
|
||
| def stream(self, events): | ||
| return stream(self, events) | ||
|
|
||
| dispatch(CountmatchescommandCommand, sys.argv, sys.stdin, sys.stdout, __name__) |
36 changes: 36 additions & 0 deletions
36
...pected_addons/expected_output_global_config_everything/Splunk_TA_UCCExample/bin/filter.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| def transform(self, records): | ||
| contains = self.contains | ||
| replace_array = self.replace_array | ||
|
|
||
| if contains and replace_array: | ||
| arr = replace_array.split(",") | ||
| if len(arr) != 2: | ||
| raise ValueError("Please provide only two arguments, separated by comma for 'replace'") | ||
|
|
||
| for record in records: | ||
| _raw = record.get("_raw") | ||
| if contains in _raw: | ||
| record["_raw"] = _raw.replace(arr[0], arr[1]) | ||
| yield record | ||
| return | ||
|
|
||
| if contains: | ||
| for record in records: | ||
| _raw = record.get("_raw") | ||
| if contains in _raw: | ||
| yield record | ||
| return | ||
|
|
||
| if replace_array: | ||
| arr = replace_array.split(",") | ||
| if len(arr) != 2: | ||
| raise ValueError("Please provide only two arguments, separated by comma for 'replace'") | ||
|
|
||
| for record in records: | ||
| _raw = record.get("_raw") | ||
| record["_raw"] = _raw.replace(arr[0], arr[1]) | ||
| yield record | ||
| return | ||
|
|
||
| for record in records: | ||
| yield record |
26 changes: 26 additions & 0 deletions
26
...addons/expected_output_global_config_everything/Splunk_TA_UCCExample/bin/filtercommand.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import sys | ||
| import import_declare_test | ||
|
|
||
| from splunklib.searchcommands import \ | ||
| dispatch, EventingCommand, Configuration, Option, validators | ||
| from filter import transform | ||
|
|
||
| @Configuration() | ||
| class FiltercommandCommand(EventingCommand): | ||
| """ | ||
|
|
||
| ##Syntax | ||
| | filtercommand contains='value1' replace='value to be replaced,value to replace with' | ||
|
|
||
| ##Description | ||
| It filters records from the events stream returning only those which has :code:`contains` in them and replaces :code:`replace_array[0]` with :code:`replace_array[1]`. | ||
|
|
||
| """ | ||
|
|
||
| contains = Option(name='contains', require=False) | ||
| replace_array = Option(name='replace_array', require=False) | ||
|
|
||
| def transform(self, events): | ||
| return transform(self, events) | ||
|
|
||
| dispatch(FiltercommandCommand, sys.argv, sys.stdin, sys.stdout, __name__) |
8 changes: 8 additions & 0 deletions
8
..._addons/expected_output_global_config_everything/Splunk_TA_UCCExample/bin/generatetext.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import time | ||
| import logging | ||
|
|
||
|
|
||
| def generate(self): | ||
| logging.debug("Generating %d events with text %s" % (self.count, self.text)) | ||
| for i in range(1, self.count + 1): | ||
| yield {'_serial': i, '_time': time.time(), '_raw': str(i) + '. ' + self.text} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.