Skip to content

Commit 05e1535

Browse files
committed
feat: add support for shortdesc, tags and examples in searchbnf.conf
1 parent 1b02b4f commit 05e1535

8 files changed

Lines changed: 197 additions & 0 deletions

File tree

docs/custom_search_commands.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ python.version = python3
6363
| requiredSearchAssistant | boolean | Specifies whether search assistance is required for the custom search command. Default: false. |
6464
| usage | string | Defines the usage of custom search command. It can be one of `public`, `private` and `deprecated`. |
6565
| description | string | Provide description of the custom search command. |
66+
| shortdesc | string | A one sentence description of the search command, used for searchbnf.conf |
6667
| syntax | string | Provide syntax for custom search command |
68+
| tags | string | One or more words that users might type into the search bar which are similar to the command name. |
69+
| examples | array[objects] | List of example search strings, used for searchbnf.conf |
6770

6871
To generate a custom search command, the following attributes must be defined in globalConfig: `commandName`, `commandType`, `fileName`, and `arguments`. Based on the provided commandType, UCC will generate a template Python file and integrate the user-defined logic into it.
6972

@@ -133,6 +136,26 @@ For example:
133136

134137
```
135138

139+
## Examples (for search command usage)
140+
141+
| Property | Type | Description |
142+
| ------------------------------------------------ | ------ | ------------------------------------------------ |
143+
| search<span class="required-asterisk">\*</span> | string | Example search command |
144+
| comment<span class="required-asterisk">\*</span> | string | Provide description of the example search string |
145+
146+
Each search command can have multiple examples, which are shown displayed in the search assistant. The Compact mode, only shows the first example. In the Full mode, the top three examples are displayed.
147+
148+
For example:
149+
150+
```json
151+
"examples": [
152+
{
153+
"search": "generatetextcommand count=5 text=\"Hallo There\"",
154+
"comment": "Generates 5 \"Hallo There\" events enumerated starting by 1"
155+
}
156+
]
157+
```
158+
136159
## Example
137160

138161
``` json
@@ -161,6 +184,12 @@ For example:
161184
"name": "text",
162185
"required": true
163186
}
187+
],
188+
"examples": [
189+
{
190+
"search": "generatetextcommand count=5 text=\"Hallo There\"",
191+
"comment": "Generates 5 \"Hallo There\" events enumerated starting by 1"
192+
}
164193
]
165194
},
166195
],

splunk_add_on_ucc_framework/generators/conf_files/create_searchbnf_conf.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ def __init__(
3434
searchbnf_dict = {
3535
"command_name": command["commandName"],
3636
"description": command["description"],
37+
"shortdesc": command.get("shortdesc", None),
3738
"syntax": command["syntax"],
3839
"usage": command["usage"],
40+
"tags": command.get("tags", None),
41+
"examples": command.get("examples", []),
3942
}
4043
self.searchbnf_info.append(searchbnf_dict)
4144

splunk_add_on_ucc_framework/global_config_validator.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,8 +730,11 @@ def _validate_custom_search_commands(self) -> None:
730730

731731
if (command.get("requiredSearchAssistant", False) is False) and (
732732
command.get("description")
733+
or command.get("shortdesc")
733734
or command.get("usage")
734735
or command.get("syntax")
736+
or command.get("tags")
737+
or command.get("examples")
735738
):
736739
logger.warning(
737740
"requiredSearchAssistant is set to false "

splunk_add_on_ucc_framework/schema/schema.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,10 @@
461461
"type": "string",
462462
"description": "Description of the custom search command. It is an required attribute for searchbnf.conf."
463463
},
464+
"shortdesc": {
465+
"type": "string",
466+
"description": "Short description or the custom search command, used as shortdesc in searchbnf.conf"
467+
},
464468
"syntax": {
465469
"type": "string",
466470
"maxLength": 100,
@@ -475,12 +479,23 @@
475479
"deprecated"
476480
]
477481
},
482+
"tags": {
483+
"type": "string",
484+
"description": "One or more words that users might type into the search bar which are similar to the command name."
485+
},
478486
"arguments": {
479487
"type": "array",
480488
"items": {
481489
"$ref": "#/definitions/arguments"
482490
},
483491
"minItems": 1
492+
},
493+
"examples": {
494+
"type": "array",
495+
"items": {
496+
"$ref": "#/definitions/searchExample"
497+
},
498+
"minItems": 1
484499
}
485500
},
486501
"required": [
@@ -526,6 +541,25 @@
526541
],
527542
"additionalProperties": false
528543
},
544+
"searchExample": {
545+
"type": "object",
546+
"description": "Search example used for searchbnf.conf",
547+
"properties": {
548+
"search": {
549+
"type": "string",
550+
"description": "Example search string"
551+
},
552+
"comment": {
553+
"type": "string",
554+
"description": "Comment for the search string which is used for searchbnf.conf"
555+
}
556+
},
557+
"required": [
558+
"search",
559+
"comment"
560+
],
561+
"additionalProperties": false
562+
},
529563
"CustomSearchCommandValidator": {
530564
"type": "object",
531565
"description": "It is used to validate the values of arguments for custom search command",

splunk_add_on_ucc_framework/templates/conf_files/searchbnf_conf.template

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,15 @@
22
[{{info["command_name"]}}-command]
33
syntax = {{info["syntax"]}}
44
description = {{info["description"]}}
5+
{% if info["shortdesc"]%}
6+
shortdesc = {{info["shortdesc"]}}
7+
{% endif %}
58
usage = {{info["usage"]}}
9+
{% if info["tags"]%}
10+
tags = {{info["tags"]}}
11+
{% endif %}
12+
{% for example in info["examples"] %}
13+
example{{ loop.index }} = {{ example["search"] }}
14+
comment{{ loop.index }} = {{ example["comment"] }}
15+
{% endfor -%}
616
{% endfor -%}

tests/unit/generators/conf_files/test_create_searchbnf_conf.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@ def custom_search_command_without_search_assistance():
1414
]
1515

1616

17+
@fixture
18+
def custom_search_command_without_optional_search_assistance_params():
19+
return [
20+
{
21+
"commandName": "generatetextcommand",
22+
"commandType": "generating",
23+
"fileName": "generatetext.py",
24+
"requiredSearchAssistant": True,
25+
"description": "This command generates COUNT occurrences of a TEXT string.",
26+
"syntax": "generatetextcommand count=<event_count> text=<string>",
27+
"usage": "public",
28+
}
29+
]
30+
31+
1732
def test_init_without_custom_command(
1833
global_config_only_configuration,
1934
input_dir,
@@ -42,8 +57,20 @@ def test_init(
4257
{
4358
"command_name": "generatetextcommand",
4459
"description": "This command generates COUNT occurrences of a TEXT string.",
60+
"shortdesc": "Command for generating string events.",
4561
"syntax": "generatetextcommand count=<event_count> text=<string>",
4662
"usage": "public",
63+
"tags": "text generator",
64+
"examples": [
65+
{
66+
"search": '| generatetextcommand count=5 text="example string"',
67+
"comment": 'Generates 5 events with text="example string"',
68+
},
69+
{
70+
"search": '| generatetextcommand count=10 text="another example string"',
71+
"comment": 'Generates 10 events with text="another example string"',
72+
},
73+
],
4774
}
4875
]
4976

@@ -65,6 +92,33 @@ def test_init_without_search_assistance(
6592
assert searchbnf_conf.searchbnf_info == []
6693

6794

95+
def test_init_without_optional_search_assistance_params(
96+
global_config_all_json,
97+
input_dir,
98+
output_dir,
99+
custom_search_command_without_optional_search_assistance_params,
100+
):
101+
global_config_all_json._content["customSearchCommand"] = (
102+
custom_search_command_without_optional_search_assistance_params
103+
)
104+
searchbnf_conf = SearchbnfConf(
105+
global_config_all_json,
106+
input_dir,
107+
output_dir,
108+
)
109+
assert searchbnf_conf.searchbnf_info == [
110+
{
111+
"command_name": "generatetextcommand",
112+
"description": "This command generates COUNT occurrences of a TEXT string.",
113+
"syntax": "generatetextcommand count=<event_count> text=<string>",
114+
"usage": "public",
115+
"examples": [],
116+
"shortdesc": None,
117+
"tags": None,
118+
}
119+
]
120+
121+
68122
def test_generate_conf_without_custom_command(
69123
global_config_only_configuration,
70124
input_dir,
@@ -82,6 +136,46 @@ def test_generate_conf_without_custom_command(
82136

83137

84138
def test_generate_conf(global_config_all_json, input_dir, output_dir):
139+
ta_name = global_config_all_json.product
140+
searchbnf_conf = SearchbnfConf(
141+
global_config_all_json,
142+
input_dir,
143+
output_dir,
144+
)
145+
output = searchbnf_conf.generate()
146+
exp_fname = "searchbnf.conf"
147+
expected_content = dedent(
148+
"""
149+
[generatetextcommand-command]
150+
syntax = generatetextcommand count=<event_count> text=<string>
151+
description = This command generates COUNT occurrences of a TEXT string.
152+
shortdesc = Command for generating string events.
153+
usage = public
154+
tags = text generator
155+
example1 = | generatetextcommand count=5 text="example string"
156+
comment1 = Generates 5 events with text="example string"
157+
example2 = | generatetextcommand count=10 text="another example string"
158+
comment2 = Generates 10 events with text="another example string"
159+
"""
160+
).lstrip()
161+
assert output == [
162+
{
163+
"file_name": exp_fname,
164+
"file_path": f"{output_dir}/{ta_name}/default/{exp_fname}",
165+
"content": expected_content,
166+
}
167+
]
168+
169+
170+
def test_generate_conf_without_optional_search_assistance_params(
171+
global_config_all_json,
172+
input_dir,
173+
output_dir,
174+
custom_search_command_without_optional_search_assistance_params,
175+
):
176+
global_config_all_json._content["customSearchCommand"] = (
177+
custom_search_command_without_optional_search_assistance_params
178+
)
85179
ta_name = global_config_all_json.product
86180
searchbnf_conf = SearchbnfConf(
87181
global_config_all_json,

tests/unit/test_global_config.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,10 @@ def test_global_config_custom_search_commands(global_config_all_json):
7676
"commandType": "generating",
7777
"requiredSearchAssistant": True,
7878
"description": "This command generates COUNT occurrences of a TEXT string.",
79+
"shortdesc": "Command for generating string events.",
7980
"syntax": "generatetextcommand count=<event_count> text=<string>",
8081
"usage": "public",
82+
"tags": "text generator",
8183
"arguments": [
8284
{
8385
"name": "count",
@@ -86,6 +88,16 @@ def test_global_config_custom_search_commands(global_config_all_json):
8688
},
8789
{"name": "text", "required": True},
8890
],
91+
"examples": [
92+
{
93+
"search": '| generatetextcommand count=5 text="example string"',
94+
"comment": 'Generates 5 events with text="example string"',
95+
},
96+
{
97+
"search": '| generatetextcommand count=10 text="another example string"',
98+
"comment": 'Generates 10 events with text="another example string"',
99+
},
100+
],
89101
}
90102
]
91103
assert expected_result == custom_search_commands

tests/unit/testdata/valid_config.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,8 +1377,10 @@
13771377
"commandType": "generating",
13781378
"requiredSearchAssistant": true,
13791379
"description": "This command generates COUNT occurrences of a TEXT string.",
1380+
"shortdesc": "Command for generating string events.",
13801381
"syntax": "generatetextcommand count=<event_count> text=<string>",
13811382
"usage": "public",
1383+
"tags": "text generator",
13821384
"arguments": [
13831385
{
13841386
"name": "count",
@@ -1393,6 +1395,16 @@
13931395
"name": "text",
13941396
"required": true
13951397
}
1398+
],
1399+
"examples": [
1400+
{
1401+
"search": "| generatetextcommand count=5 text=\"example string\"",
1402+
"comment": "Generates 5 events with text=\"example string\""
1403+
},
1404+
{
1405+
"search": "| generatetextcommand count=10 text=\"another example string\"",
1406+
"comment": "Generates 10 events with text=\"another example string\""
1407+
}
13961408
]
13971409
}
13981410
]

0 commit comments

Comments
 (0)