-
Notifications
You must be signed in to change notification settings - Fork 29
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
base: develop
Are you sure you want to change the base?
Changes from all commits
b77d3c1
7e81dfe
182d30e
39fbb56
833de40
6a042e3
98c69f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 %} | ||
|
@@ -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(): | ||
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import json | ||
import sys | ||
import inspect | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you need this inspect here? |
||
|
||
from splunklib import modularinput as smi | ||
|
||
|
@@ -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 |
---|---|---|
@@ -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") |
There was a problem hiding this comment.
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.