All Apps and Add-ons

How to add custom validation on Configuration Page- Add-on Settings tab.

kamlesh_vaghela
SplunkTrust
SplunkTrust

Hi Splunkers,

I have an issue regarding validation on Alert action Configuration Page.
I have created app from Splunk AOB with a single alert action. AOB generates all files we required. TA_testing_add_on_rh_settings.py is one of them.

Here, we can validate single input value but I want to validate all values together. below is my code.

import ta_testing_add_on_declare

from splunktaucclib.rest_handler.endpoint import (
    field,
    validator,
    RestModel,
    MultipleModel,
)
from splunktaucclib.rest_handler import admin_external, util
from splunk_aoblib.rest_migration import ConfigMigrationHandler

util.remove_http_proxy_env_vars()


fields_additional_parameters = [
        field.RestField(
        'url',
        required=False,
        encrypted=False,
        default='',
        validator=validator.String(
            max_len=8192, 
            min_len=0, 
        )
    ),
    field.RestField(
        'user_name',
        required=False,
        encrypted=False,
        default='',
        validator=validator.String(
            max_len=8192, 
            min_len=0, 
        )
    ), 
    field.RestField(
        'password',
        required=False,
        encrypted=True,
        default='',
        validator=validator.String(
            max_len=8192, 
            min_len=0, 
        )
    )
]
model_additional_parameters = RestModel(fields_additional_parameters, name='additional_parameters')


endpoint = MultipleModel(
    'ta_testing_add_on_settings',
    models=[
        model_additional_parameters
    ],
)


if __name__ == '__main__':
    admin_external.handle(
        endpoint,
        handler=ConfigMigrationHandler,
    )

This code is totally generated from AOB. There is no custom modification.

Here I found we can do field level validation by using :

validator=validator.String(
            max_len=8192, 
            min_len=0, 
)

and we can add custom validation using UserDefined class.

My questions are:
1) How can we use UserDefined validator?
2) How can we access field values, like(url/username/password), to do custom validations which verify the username/password by login with url?

Any hint??

Thanks

Tags (1)
0 Karma

chli_splunk
Splunk Employee
Splunk Employee

You can try following 2 options
1. Hack TA_testing_add_on_rh_settings.py. You can update it manually, but it's not recommended unless you dont want to reload it in AoB, otherwise there might be some errors due to inconsistent status.
2. Validate inputs in your Python code. AoB provides a UI to edit your Python codes.

# get some fields
url = helper.settings["server_uri"]

# add your validate code here
if not success:
    # write events
    helper.addevent("error msg", sourcetype="sample_sourcetype")
    helper.writeevents(index="summary", host="localhost", source="localhost")
    return

# if success
helper.log_info("Alert action sdfsdfsd started.")
0 Karma
.conf21 CFS Extended through 5/20!

Don't miss your chance
to share your Splunk
wisdom in-person or
virtually at .conf21!

Call for Speakers has
been extended through
Thursday, 5/20!