Skip to content

feat: include "self" parameter in REST handler helper functions #1768

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

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions docs/inputs/helper.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,20 @@ def stream_events(inputs: smi.InputDefinition, event_writer: smi.EventWriter):
```

The two methods' bodies should be filled by the developer.

Alternatively, if you want to have access to the instance of the input script class,
you can also add the `self` parameter to the methods:

```python
from splunklib import modularinput as smi


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


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

Instead of `self`, you can also use any other name, but it must be the first parameter.
15 changes: 13 additions & 2 deletions splunk_add_on_ucc_framework/templates/input.template
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would have to make changes to input.module-template file also.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import import_declare_test
import json
{% endif -%}
import sys
import inspect

from splunklib import modularinput as smi
{%- if input_helper_module %}
Expand Down Expand Up @@ -42,14 +43,14 @@ class {{class_name}}(smi.Script):

def validate_input(self, definition: smi.ValidationDefinition):
{%- if input_helper_module %}
return validate_input(definition)
return self.call_with_args(validate_input, definition)
{%- else %}
return
{%- endif %}

def stream_events(self, inputs: smi.InputDefinition, ew: smi.EventWriter):
{%- if input_helper_module %}
return stream_events(inputs, ew)
return self.call_with_args(stream_events, inputs, ew)
{%- else %}
input_items = [{'count': len(inputs.inputs)}]
for input_name, input_item in inputs.inputs.items():
Expand All @@ -62,6 +63,16 @@ class {{class_name}}(smi.Script):
ew.write_event(event)
{%- endif %}

{% if input_helper_module -%}
def call_with_args(self, method, *args):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The drawback with this implementation is - when the add-on has this code and is deployed in Splunk environment and when the modinput is invoked, it will check whether the input_helper module has 2 parameters or 3, and then execute it.
I think, we could do this same during the ucc-gen build process itself and write this file accordingly. Perhaps, something like this. The drawback here is the *pyc that would be generated, which we would have to clean-up.
What do you think?

method_args_list = inspect.getfullargspec(method).args

if len(method_args_list) == len(args) + 1:
return method(self, *args)

return method(*args)
{%- endif %}


if __name__ == '__main__':
exit_code = {{class_name}}().run(sys.argv)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import json
import sys
import inspect
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need this inspect here?


from splunklib import modularinput as smi

Expand Down Expand Up @@ -42,6 +43,8 @@ def stream_events(self, inputs: smi.InputDefinition, ew: smi.EventWriter):
ew.write_event(event)




if __name__ == '__main__':
exit_code = EXAMPLE_INPUT_ONE().run(sys.argv)
sys.exit(exit_code)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import json
import sys
import inspect

from splunklib import modularinput as smi

Expand Down Expand Up @@ -42,6 +43,8 @@ def stream_events(self, inputs: smi.InputDefinition, ew: smi.EventWriter):
ew.write_event(event)




if __name__ == '__main__':
exit_code = EXAMPLE_INPUT_TWO().run(sys.argv)
sys.exit(exit_code)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import import_declare_test

import sys
import inspect

from splunklib import modularinput as smi
from helper_one import stream_events, validate_input
Expand Down Expand Up @@ -124,12 +125,20 @@ def get_scheme(self):
return scheme

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

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

def call_with_args(self, method, *args):
method_args_list = inspect.getfullargspec(method).args

if len(method_args_list) == len(args) + 1:
return method(self, *args)

return method(*args)


if __name__ == '__main__':
exit_code = EXAMPLE_INPUT_ONE().run(sys.argv)
sys.exit(exit_code)
sys.exit(exit_code)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import import_declare_test

import sys
import inspect

from splunklib import modularinput as smi
from helper_two import stream_events, validate_input
Expand Down Expand Up @@ -100,12 +101,20 @@ def get_scheme(self):
return scheme

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

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

def call_with_args(self, method, *args):
method_args_list = inspect.getfullargspec(method).args

if len(method_args_list) == len(args) + 1:
return method(self, *args)

return method(*args)


if __name__ == '__main__':
exit_code = EXAMPLE_INPUT_TWO().run(sys.argv)
sys.exit(exit_code)
sys.exit(exit_code)
62 changes: 62 additions & 0 deletions tests/unit/test_templates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import importlib.util
import sys
from textwrap import dedent
from unittest.mock import MagicMock

import pytest

from splunk_add_on_ucc_framework import utils


class Script:
pass


@pytest.mark.parametrize("with_self", [True, False])
def test_input_helpers(tmp_path, monkeypatch, with_self):
# To avoid issues with imports, we need to monkeypatch the sys.modules with a different dict
monkeypatch.setattr(sys, "modules", {k: v for k, v in sys.modules.items()})

content = (
utils.get_j2_env()
.get_template("input.template")
.render(
input_name="MyInput",
class_name="MyClass",
description="My Input Description",
entity=[],
input_helper_module="my_input_helper",
)
)

(tmp_path / "my_input.py").write_text(content)

if with_self:
self_arg = "self, "
else:
self_arg = ""

(tmp_path / "my_input_helper.py").write_text(
dedent(
f"""
def validate_input({self_arg}definition):
return definition


def stream_events({self_arg}inputs, event_writer):
return inputs, event_writer
"""
)
)

for module in ["import_declare_test"]:
# mock module in sys.modules - set to MagicMock
mock_module = MagicMock()
mock_module.__file__ = str(tmp_path / f"{module}.py")
monkeypatch.setitem(sys.modules, module, mock_module)

monkeypatch.syspath_prepend(str(tmp_path))
my_obj = importlib.import_module("my_input").MyClass()

assert my_obj.validate_input("arg1") == "arg1"
assert my_obj.stream_events("arg1", "arg2") == ("arg1", "arg2")
Loading