Skip to content

Commit

Permalink
feat: pass modular input script as parameter to input helper stream_e…
Browse files Browse the repository at this point in the history
…vents
  • Loading branch information
Benni0 committed Sep 16, 2024
1 parent bc6a36b commit 9cb9903
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 15 deletions.
4 changes: 2 additions & 2 deletions docs/inputs/helper.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ The input script `example_input_one` will import this module and call its method
The file must contain the following two functions:

- `validate_input(definition: smi.ValidationDefinition)`
- `stream_events(inputs: smi.InputDefinition, event_writer: smi.EventWriter)`
- `stream_events(inputs: smi.InputDefinition, event_writer: smi.EventWriter, modular_input: smi.Script)`

```python
from splunklib import modularinput as smi
Expand All @@ -47,7 +47,7 @@ def validate_input(definition: smi.ValidationDefinition):
...


def stream_events(inputs: smi.InputDefinition, event_writer: smi.EventWriter):
def stream_events(inputs: smi.InputDefinition, event_writer: smi.EventWriter, modular_input: smi.Script):
...
```

Expand Down
4 changes: 2 additions & 2 deletions splunk_add_on_ucc_framework/templates/input.module-template
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import json
from splunklib import modularinput as smi


def validate_input(definition: smi.ValidationDefinition):
def validate_input(definition: smi.ValidationDefinition, modular_input: smi.Script):
return


def stream_events(inputs: smi.InputDefinition, ew: smi.EventWriter):
def stream_events(inputs: smi.InputDefinition, ew: smi.EventWriter, modular_input: smi.Script):
input_items = [{'count': len(inputs.inputs)}]
for input_name, input_item in inputs.inputs.items():
input_item['name'] = input_name
Expand Down
10 changes: 9 additions & 1 deletion splunk_add_on_ucc_framework/templates/input.template
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import import_declare_test

{% if not input_helper_module -%}
{% if input_helper_module -%}
import inspect
{% else -%}
import json
{% endif -%}
import sys
Expand Down Expand Up @@ -42,13 +44,19 @@ class {{class_name}}(smi.Script):

def validate_input(self, definition: smi.ValidationDefinition):
{%- if input_helper_module %}
validate_input_signature = inspect.signature(validate_input)
if len(validate_input_signature.parameters) == 2:
return validate_input(definition, self)
return validate_input(definition)
{%- else %}
return
{%- endif %}

def stream_events(self, inputs: smi.InputDefinition, ew: smi.EventWriter):
{%- if input_helper_module %}
stream_events_signature = inspect.signature(stream_events)
if len(stream_events_signature.parameters) == 3:
return stream_events(inputs, ew, self)
return stream_events(inputs, ew)
{%- else %}
input_items = [{'count': len(inputs.inputs)}]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ def get_scheme(self):
return scheme

def validate_input(self, definition: smi.ValidationDefinition):
return validate_input(definition)
return validate_input(definition, self)

def stream_events(self, inputs: smi.InputDefinition, ew: smi.EventWriter):
return stream_events(inputs, ew)
return stream_events(inputs, ew, self)


if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def validate_input(definition: smi.ValidationDefinition):
return


def stream_events(inputs: smi.InputDefinition, event_writer: smi.EventWriter):
def stream_events(inputs: smi.InputDefinition, event_writer: smi.EventWriter, modular_input: smi.Script):
# inputs.inputs is a Python dictionary object like:
# {
# "demo_input://<input_name>": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ def get_scheme(self):
return scheme

def validate_input(self, definition: smi.ValidationDefinition):
return validate_input(definition)
return validate_input(definition, self)

def stream_events(self, inputs: smi.InputDefinition, ew: smi.EventWriter):
return stream_events(inputs, ew)
return stream_events(inputs, ew, self)


if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ def get_scheme(self):
return scheme

def validate_input(self, definition: smi.ValidationDefinition):
return validate_input(definition)
return validate_input(definition, self)

def stream_events(self, inputs: smi.InputDefinition, ew: smi.EventWriter):
return stream_events(inputs, ew)
return stream_events(inputs, ew, self)


if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def validate_input(definition: smi.ValidationDefinition):
return


def stream_events(inputs: smi.InputDefinition, ew: smi.EventWriter):
def stream_events(inputs: smi.InputDefinition, ew: smi.EventWriter, modular_input: smi.Script):
# INPUT 3
input_items = [{'count': len(inputs.inputs)}]
for input_name, input_item in inputs.inputs.items():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def validate_input(definition: smi.ValidationDefinition):
return


def stream_events(inputs: smi.InputDefinition, ew: smi.EventWriter):
def stream_events(inputs: smi.InputDefinition, ew: smi.EventWriter, modular_input: smi.Script):
# INPUT 1
input_items = [{'count': len(inputs.inputs)}]
for input_name, input_item in inputs.inputs.items():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def validate_input(definition: smi.ValidationDefinition):
return


def stream_events(inputs: smi.InputDefinition, ew: smi.EventWriter):
def stream_events(inputs: smi.InputDefinition, ew: smi.EventWriter, modular_input: smi.Script):
# INPUT 2
input_items = [{'count': len(inputs.inputs)}]
for input_name, input_item in inputs.inputs.items():
Expand Down

0 comments on commit 9cb9903

Please sign in to comment.