Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/generated_files.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The following table describes the files generated by UCC framework.
| alert_actions.conf | output/<YOUR_ADDON_NAME>/default | Generates `alert_actions.conf` and `alert_actions.conf.spec` file for the custom alert actions defined in globalConfig |
| eventtypes.conf | output/<YOUR_ADDON_NAME>/default | Generates `eventtypes.conf` file if the sourcetype is mentioned in Adaptive Response of custom alert action in globalConfig |
| tags.conf | output/<YOUR_ADDON_NAME>/default | Generates `tags.conf` file based on the `eventtypes.conf` created for custom alert actions. |
| commands.conf | output/<YOUR_ADDON_NAME>/default | Generates `commands.conf` for custom commands provided in the globalConfig. |
| searchbnf.conf | output/<YOUR_ADDON_NAME>/default | Generates `searchbnf.conf` for custom search commands provided in the globalConfig. |
| _account.conf | output/&lt;YOUR_ADDON_NAME&gt;/README | Generates `<YOUR_ADDON_NAME>_account.conf.spec` file for the configuration mentioned in globalConfig |
| _settings.conf | output/&lt;YOUR_ADDON_NAME&gt;/README | Generates `<YOUR_ADDON_NAME>_settings.conf.spec` file for the Proxy, Logging or Custom Tab mentioned in globalConfig |
Expand Down
2 changes: 2 additions & 0 deletions splunk_add_on_ucc_framework/generators/conf_files/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from .create_web_conf import WebConf
from .create_account_conf import AccountConf
from .create_settings_conf import SettingsConf
from .create_commands_conf import CommandsConf
from .create_searchbnf_conf import SearchbnfConf

__all__ = [
Expand All @@ -38,5 +39,6 @@
"InputsConf",
"AccountConf",
"SettingsConf",
"CommandsConf",
"SearchbnfConf",
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#
# 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

from splunk_add_on_ucc_framework.generators.file_generator import FileGenerator


class CommandsConf(FileGenerator):
__description__ = (
"Generates `commands.conf` for custom commands provided in the globalConfig."
)

def _set_attributes(self, **kwargs: Any) -> None:
self.conf_file = "commands.conf"
if self._global_config.has_custom_search_commands():
self.command_names = []
for command in self._global_config.custom_search_commands:
self.command_names.append(command["commandName"])

def generate(self) -> Dict[str, str]:
if not self._global_config.has_custom_search_commands():
return {}

file_path = self.get_file_output_path(["default", self.conf_file])
self.set_template_and_render(
template_file_path=["conf_files"], file_name="commands_conf.template"
)
rendered_content = self._template.render(
command_names=self.command_names,
)
self.writer(
file_name=self.conf_file,
file_path=file_path,
content=rendered_content,
)
return {self.conf_file: file_path}
2 changes: 2 additions & 0 deletions splunk_add_on_ucc_framework/generators/file_const.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
WebConf,
AccountConf,
SettingsConf,
CommandsConf,
SearchbnfConf,
)

Expand All @@ -60,6 +61,7 @@ class FileClass(NamedTuple):
),
FileClass("eventtypes.conf", EventtypesConf, ["default"]),
FileClass("tags.conf", TagsConf, ["default"]),
FileClass("commands.conf", CommandsConf, ["default"]),
FileClass("searchbnf.conf", SearchbnfConf, ["default"]),
FileClass("_account.conf", AccountConf, ["README"]),
FileClass("_settings.conf", SettingsConf, ["README"]),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% for name in command_names -%}
[{{name}}]
filename = {{name}}.py
chunked = true
python.version = python3
{% endfor -%}
1 change: 1 addition & 0 deletions tests/smoke/test_ucc_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ def test_ucc_generate_with_everything(caplog):
("default", "splunk_ta_uccexample_settings.conf"),
("default", "web.conf"),
("default", "server.conf"),
("default", "commands.conf"),
("default", "searchbnf.conf"),
("default", "data", "ui", "alerts", "test_alert.html"),
("default", "data", "ui", "nav", "default.xml"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[generatetextcommand]
filename = generatetextcommand.py
chunked = true
python.version = python3

[filtercommand]
filename = filtercommand.py
chunked = true
python.version = python3

[countmatchescommand]
filename = countmatchescommand.py
chunked = true
python.version = python3
2 changes: 2 additions & 0 deletions tests/unit/generators/conf_files/test_conf_files_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def test___init__conf():
"InputsConf",
"AccountConf",
"SettingsConf",
"CommandsConf",
"SearchbnfConf",
]
expected_modules = [
Expand All @@ -25,6 +26,7 @@ def test___init__conf():
"create_web_conf",
"create_account_conf",
"create_settings_conf",
"create_commands_conf",
"create_searchbnf_conf",
]
import splunk_add_on_ucc_framework.generators.conf_files as conf
Expand Down
79 changes: 79 additions & 0 deletions tests/unit/generators/conf_files/test_create_commands_conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from splunk_add_on_ucc_framework.generators.conf_files import CommandsConf
from splunk_add_on_ucc_framework import __file__ as ucc_framework_file
import os.path
from textwrap import dedent

UCC_DIR = os.path.dirname(ucc_framework_file)


def test_set_attributes_without_custom_command(
global_config_only_configuration, input_dir, output_dir, ucc_dir, ta_name
):
commands_conf = CommandsConf(
global_config_only_configuration,
input_dir,
output_dir,
ucc_dir=ucc_dir,
addon_name=ta_name,
)
assert not hasattr(commands_conf, "command_names")


def test_set_attributes(
global_config_all_json, input_dir, output_dir, ucc_dir, ta_name
):
commands_conf = CommandsConf(
global_config_all_json,
input_dir,
output_dir,
ucc_dir=ucc_dir,
addon_name=ta_name,
)
assert commands_conf.conf_file == "commands.conf"
assert commands_conf.command_names == ["generatetextcommand"]


def test_generate_conf_without_custom_command(
global_config_only_configuration, input_dir, output_dir, ucc_dir, ta_name
):
commands_conf = CommandsConf(
global_config_only_configuration,
input_dir,
output_dir,
ucc_dir=ucc_dir,
addon_name=ta_name,
)
file_paths = commands_conf.generate()

# Assert that no files are returned since no custom command is configured
assert file_paths == {}


def test_commands_conf_generation(
global_config_all_json, input_dir, output_dir, ta_name
):
commands_conf = CommandsConf(
global_config_all_json,
input_dir,
output_dir,
addon_name=ta_name,
ucc_dir=UCC_DIR,
)
file_paths = commands_conf.generate()

assert file_paths is not None
assert file_paths.keys() == {"commands.conf"}
assert file_paths["commands.conf"].endswith("test_addon/default/commands.conf")

with open(file_paths["commands.conf"]) as fp:
content = fp.read()

expected_content = dedent(
"""
[generatetextcommand]
filename = generatetextcommand.py
chunked = true
python.version = python3
"""
).lstrip()
assert content == expected_content
1 change: 1 addition & 0 deletions tests/unit/generators/test_doc_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def test_generate_docs():
| alert_actions.conf | output/&lt;YOUR_ADDON_NAME&gt;/default | Generates `alert_actions.conf` and `alert_actions.conf.spec` file for the custom alert actions defined in globalConfig |
| eventtypes.conf | output/&lt;YOUR_ADDON_NAME&gt;/default | Generates `eventtypes.conf` file if the sourcetype is mentioned in Adaptive Response of custom alert action in globalConfig |
| tags.conf | output/&lt;YOUR_ADDON_NAME&gt;/default | Generates `tags.conf` file based on the `eventtypes.conf` created for custom alert actions. |
| commands.conf | output/&lt;YOUR_ADDON_NAME&gt;/default | Generates `commands.conf` for custom commands provided in the globalConfig. |
| searchbnf.conf | output/&lt;YOUR_ADDON_NAME&gt;/default | Generates `searchbnf.conf` for custom search commands provided in the globalConfig. |
| _account.conf | output/&lt;YOUR_ADDON_NAME&gt;/README | Generates `<YOUR_ADDON_NAME>_account.conf.spec` file for the configuration mentioned in globalConfig |
| _settings.conf | output/&lt;YOUR_ADDON_NAME&gt;/README | Generates `<YOUR_ADDON_NAME>_settings.conf.spec` file for the Proxy, Logging or Custom Tab mentioned in globalConfig |
Expand Down
1 change: 1 addition & 0 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def test_get_j2_env():
"conf_files/settings_conf.template",
"conf_files/tags_conf.template",
"web_conf.template",
"conf_files/commands_conf.template",
"conf_files/searchbnf_conf.template",
]
assert sorted(expected_list_of_templates) == sorted(list_of_templates)
Expand Down
Loading