Skip to content
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

feat: ADDON-67423 improve backend error responses #1068

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
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
25 changes: 20 additions & 5 deletions splunk_add_on_ucc_framework/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import os
import sys
from defusedxml import ElementTree
from typing import Sequence
from typing import Sequence, List

from splunk_add_on_ucc_framework import global_config as global_config_lib

Expand Down Expand Up @@ -88,9 +88,16 @@
PANEL_ERRORS_IN_THE_ADDON_TEMPLATE = """ <row>
<panel>
<title>Errors in the add-on</title>
<input type="radio" token="sourcetype_token">
<label>Select a source type</label>
<default>add-on</default>
<choice value="index = _internal source=*{addon_name}* ERROR">add-on</choice>
<choice value="index = _internal source=/opt/splunk/var/log/splunk/splunkd.log \
log_level=ERROR component=ModularInputs (scheme IN ({input_names}))">splunkd</choice>
</input>
<event>
<search>
<query>index=_internal source=*{addon_name}* ERROR</query>
<query>$sourcetype_token$</query>
<earliest>$log_time.earliest$</earliest>
<latest>$log_time.latest$</latest>
</search>
Expand All @@ -107,7 +114,10 @@


def generate_dashboard_content(
addon_name: str, panel_names: Sequence[str], custom_components: str
addon_name: str,
panel_names: Sequence[str],
input_names: List[str],
custom_components: str,
) -> str:
content = DASHBOARD_START
for panel_name in panel_names:
Expand All @@ -120,7 +130,8 @@ def generate_dashboard_content(
)
elif panel_name == PANEL_ERRORS_IN_THE_ADDON:
content += PANEL_ERRORS_IN_THE_ADDON_TEMPLATE.format(
addon_name=addon_name.lower()
addon_name=addon_name.lower(),
input_names=",".join([name + "*" for name in input_names]),
)
elif panel_name == PANEL_CUSTOM:
content += custom_components
Expand Down Expand Up @@ -154,7 +165,11 @@ def generate_dashboard(
)
custom_components = get_custom_xml_content(dashboard_components_path)

content = generate_dashboard_content(addon_name, panel_names, custom_components)
input_names = [el.get("name") for el in global_config.inputs]

content = generate_dashboard_content(
addon_name, panel_names, input_names, custom_components
)
with open(dashboard_xml_file_path, "w") as dashboard_xml_file:
dashboard_xml_file.write(content)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,15 @@
<row>
<panel>
<title>Errors in the add-on</title>
<input type="radio" token="sourcetype_token">
<label>Select a source type</label>
<default>add-on</default>
<choice value="index = _internal source=*splunk_ta_uccexample* ERROR">add-on</choice>
<choice value="index = _internal source=/opt/splunk/var/log/splunk/splunkd.log log_level=ERROR component=ModularInputs (scheme IN (example_input_one*,example_input_two*,example_input_three*,example_input_four*))">splunkd</choice>
</input>
<event>
<search>
<query>index=_internal source=*splunk_ta_uccexample* ERROR</query>
<query>$sourcetype_token$</query>
<earliest>$log_time.earliest$</earliest>
<latest>$log_time.latest$</latest>
</search>
Expand Down
20 changes: 17 additions & 3 deletions tests/unit/test_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,16 @@
<row>
<panel>
<title>Errors in the add-on</title>
<input type="radio" token="sourcetype_token">
<label>Select a source type</label>
<default>add-on</default>
<choice value="index = _internal source=*{}* ERROR">add-on</choice>
<choice value="index = _internal source=/opt/splunk/var/log/splunk/splunkd.log \
log_level=ERROR component=ModularInputs (scheme IN ({}))">splunkd</choice>
</input>
<event>
<search>
<query>index=_internal source=*splunk_ta_uccexample* ERROR</query>
<query>$sourcetype_token$</query>
<earliest>$log_time.earliest$</earliest>
<latest>$log_time.latest$</latest>
</search>
Expand Down Expand Up @@ -106,7 +113,12 @@ def test_generate_dashboard_when_dashboard_does_not_exist(
str(dashboard_xml_file_path),
)

expected_content = default_dashboard_content_start + default_dashboard_content_end
expected_content = (
default_dashboard_content_start.format(
"splunk_ta_uccexample", "example_input_one*,example_input_two*"
)
+ default_dashboard_content_end
)

with open(dashboard_xml_file_path) as dashboard_xml_file:
assert expected_content == dashboard_xml_file.read()
Expand Down Expand Up @@ -164,7 +176,9 @@ def test_generate_dashboard_with_custom_components(setup, tmp_path):
)

expected_content = (
default_dashboard_content_start
default_dashboard_content_start.format(
"splunk_ta_uccexample", "example_input_one*"
)
+ custom_dashboard_components
+ default_dashboard_content_end
)
Expand Down
52 changes: 52 additions & 0 deletions tests/unit/testdata/valid_config_with_custom_dashboard.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,58 @@
"title": "Configuration",
"description": "Set up your add-on"
},
"inputs": {
"title": "Inputs",
"description": "Manage your data inputs",
"table": {
"actions": [
"edit",
"enable",
"delete",
"clone"
],
"header": [
{
"label": "Name",
"field": "name"
}
],
"moreInfo": [
{
"label": "Name",
"field": "name"
}
]
},
"services": [
{
"name": "example_input_one",
"title": "Example Input One",
"entity": [
{
"type": "text",
"label": "Name",
"validators": [
{
"type": "regex",
"errorMsg": "Input Name must begin with a letter and consist exclusively of alphanumeric characters and underscores.",
"pattern": "^[a-zA-Z]\\w*$"
},
{
"type": "string",
"errorMsg": "Length of input name should be between 1 and 100",
"minLength": 100,
"maxLength": 1
}
],
"field": "name",
"help": "A unique name for the data input.",
"required": true
}
]
}
]
},
"dashboard": {
"panels": [
{
Expand Down
Loading