common/lib/aframe_utils.py
def execute_template(post_vars):
    """
    Refactored out of tools/views.py as this is used in a couple of different places (tools/views and screens/views)
    :param post_vars: copy of all variables passed in via the operation
    :return: response object of form {'output': whatever, 'status': 1 }
    """
    if 'template_id' not in post_vars and 'template_name' not in post_vars:
        error = {'output': 'missing required template_id or template_name parameter', 'status': 1}
        return error

    if 'template_id' in post_vars:
        template_id = int(post_vars['template_id'])
        config_template = ConfigTemplate.objects.get(pk=template_id)
    else:
        template_name = str(post_vars['template_name'])
        print("GETTING %s" % template_name)
        config_template = ConfigTemplate.objects.get(name=template_name)

    template_api = get_input_parameters_for_template(config_template)

    context = dict()

    try:
        print(str(template_api["input_parameters"]))
        input_parameters = template_api["input_parameters"]

        for j in input_parameters:
            print("setting context %s" % j)
            context[j] = str(post_vars[j])

    except Exception as ex:
        print(str(ex))
        error = {"output": "missing required parameters", "status": 1}
        return error

    compiled_template = engines['django'].from_string(config_template.template)
    # compiled_template = get_template_from_string(config_template.template)
    completed_template = str(compiled_template.render(context))

    print(completed_template)
    action_name = config_template.action_provider
    action_options = json.loads(config_template.action_provider_options)

    print(post_vars)

    for ao in action_options:
        print('checking ' + str(ao))
        if "action_options_" + str(ao) in post_vars:
            print("found a match!")
            new_val = post_vars["action_options_" + str(ao)]
            print(new_val)
            current_value = action_options[ao]["value"]
            print(current_value)
            action_options[ao]["value"] = re.sub("{{ .* }}", new_val, current_value)
            print(action_options[ao]["value"])

    # let's load any secrets if necessary
    provider_options = action_provider.get_options_for_provider(action_name)
    for opt in provider_options:
        if opt['type'] == 'secret':
            opt_name = opt['name']
            pw_lookup_key = action_options[opt_name]['value']
            pw_lookup_value = lookup_secret(pw_lookup_key)
            action_options[opt_name]['value'] = pw_lookup_value

    print("action name is: " + action_name)

    action = action_provider.get_provider_instance(action_name, action_options)
    if config_template.type == "per-endpoint":

        if "af_endpoint_ip" not in post_vars or "af_endpoint_id" not in post_vars:
            error = {"output": "missing required authentication parameters", "status": 1}
            return error

        endpoint = dict()
        endpoint["id"] = post_vars.get("af_endpoint_id", "")
        endpoint["name"] = post_vars.get("af_endpoint_name", "")
        endpoint["ip"] = post_vars.get("af_endpoint_ip", "")
        endpoint["username"] = post_vars.get("af_endpoint_username", "")
        endpoint["password"] = post_vars.get("af_endpoint_password", "")
        endpoint["type"] = post_vars.get("af_endpoint_type", "")

        action.set_endpoint(endpoint)

    try:
        results = action.execute_template(completed_template.strip().replace('\r\n', '\n'))
        response = {"output": results.strip(), "status": 0}

    except Exception as ex:
        print(str(ex))
        response = {"output": "Error executing template", "status": 1}

    return response

    Refactored out of tools/views.py as this is used in a couple of different places (tools/views and screens/views)
    :param post_vars: copy of all variables passed in via the operation
    :return: response object of form {'output': whatever, 'status': 1 }
    
Refactored out of tools/views.py as this is used in a couple of different places (tools/views and screens/views)
Refactored out of tools / views . py as this is used in a couple of different places ( tools / views and screens / views )
def execute_template(post_vars):
    if 'template_id' not in post_vars and 'template_name' not in post_vars:
        error = {'output': 'missing required template_id or template_name parameter', 'status': 1}
        return error
    if 'template_id' in post_vars:
        template_id = int(post_vars['template_id'])
        config_template = ConfigTemplate.objects.get(pk=template_id)
    else:
        template_name = str(post_vars['template_name'])
        print("GETTING %s" % template_name)
        config_template = ConfigTemplate.objects.get(name=template_name)
    template_api = get_input_parameters_for_template(config_template)
    context = dict()
    try:
        print(str(template_api["input_parameters"]))
        input_parameters = template_api["input_parameters"]
        for j in input_parameters:
            print("setting context %s" % j)
            context[j] = str(post_vars[j])
    except Exception as ex:
        print(str(ex))
        error = {"output": "missing required parameters", "status": 1}
        return error
    compiled_template = engines['django'].from_string(config_template.template)
    completed_template = str(compiled_template.render(context))
    print(completed_template)
    action_name = config_template.action_provider
    action_options = json.loads(config_template.action_provider_options)
    print(post_vars)
    for ao in action_options:
        print('checking ' + str(ao))
        if "action_options_" + str(ao) in post_vars:
            print("found a match!")
            new_val = post_vars["action_options_" + str(ao)]
            print(new_val)
            current_value = action_options[ao]["value"]
            print(current_value)
            action_options[ao]["value"] = re.sub("{{ .* }}", new_val, current_value)
            print(action_options[ao]["value"])
    provider_options = action_provider.get_options_for_provider(action_name)
    for opt in provider_options:
        if opt['type'] == 'secret':
            opt_name = opt['name']
            pw_lookup_key = action_options[opt_name]['value']
            pw_lookup_value = lookup_secret(pw_lookup_key)
            action_options[opt_name]['value'] = pw_lookup_value
    print("action name is: " + action_name)
    action = action_provider.get_provider_instance(action_name, action_options)
    if config_template.type == "per-endpoint":
        if "af_endpoint_ip" not in post_vars or "af_endpoint_id" not in post_vars:
            error = {"output": "missing required authentication parameters", "status": 1}
            return error
        endpoint = dict()
        endpoint["id"] = post_vars.get("af_endpoint_id", "")
        endpoint["name"] = post_vars.get("af_endpoint_name", "")
        endpoint["ip"] = post_vars.get("af_endpoint_ip", "")
        endpoint["username"] = post_vars.get("af_endpoint_username", "")
        endpoint["password"] = post_vars.get("af_endpoint_password", "")
        endpoint["type"] = post_vars.get("af_endpoint_type", "")
        action.set_endpoint(endpoint)
    try:
        results = action.execute_template(completed_template.strip().replace('\r\n', '\n'))
        response = {"output": results.strip(), "status": 0}
    except Exception as ex:
        print(str(ex))
        response = {"output": "Error executing template", "status": 1}
    return response
def execute_template ( post_vars ) : if 'template_id' not in post_vars and 'template_name' not in post_vars : error = { 'output' : 'missing required template_id or template_name parameter' , 'status' : 1 } return error if 'template_id' in post_vars : template_id = int ( post_vars [ 'template_id' ] ) config_template = ConfigTemplate . objects . get ( pk = template_id ) else : template_name = str ( post_vars [ 'template_name' ] ) print ( "GETTING %s" % template_name ) config_template = ConfigTemplate . objects . get ( name = template_name ) template_api = get_input_parameters_for_template ( config_template ) context = dict ( ) try : print ( str ( template_api [ "input_parameters" ] ) ) input_parameters = template_api [ "input_parameters" ] for j in input_parameters : print ( "setting context %s" % j ) context [ j ] = str ( post_vars [ j ] ) except Exception as ex : print ( str ( ex ) ) error = { "output" : "missing required parameters" , "status" : 1 } return error compiled_template = engines [ 'django' ] . from_string ( config_template . template ) completed_template = str ( compiled_template . render ( context ) ) print ( completed_template ) action_name = config_template . action_provider action_options = json . loads ( config_template . action_provider_options ) print ( post_vars ) for ao in action_options : print ( 'checking ' + str ( ao ) ) if "action_options_" + str ( ao ) in post_vars : print ( "found a match!" ) new_val = post_vars [ "action_options_" + str ( ao ) ] print ( new_val ) current_value = action_options [ ao ] [ "value" ] print ( current_value ) action_options [ ao ] [ "value" ] = re . sub ( "{{ .* }}" , new_val , current_value ) print ( action_options [ ao ] [ "value" ] ) provider_options = action_provider . get_options_for_provider ( action_name ) for opt in provider_options : if opt [ 'type' ] == 'secret' : opt_name = opt [ 'name' ] pw_lookup_key = action_options [ opt_name ] [ 'value' ] pw_lookup_value = lookup_secret ( pw_lookup_key ) action_options [ opt_name ] [ 'value' ] = pw_lookup_value print ( "action name is: " + action_name ) action = action_provider . get_provider_instance ( action_name , action_options ) if config_template . type == "per-endpoint" : if "af_endpoint_ip" not in post_vars or "af_endpoint_id" not in post_vars : error = { "output" : "missing required authentication parameters" , "status" : 1 } return error endpoint = dict ( ) endpoint [ "id" ] = post_vars . get ( "af_endpoint_id" , "" ) endpoint [ "name" ] = post_vars . get ( "af_endpoint_name" , "" ) endpoint [ "ip" ] = post_vars . get ( "af_endpoint_ip" , "" ) endpoint [ "username" ] = post_vars . get ( "af_endpoint_username" , "" ) endpoint [ "password" ] = post_vars . get ( "af_endpoint_password" , "" ) endpoint [ "type" ] = post_vars . get ( "af_endpoint_type" , "" ) action . set_endpoint ( endpoint ) try : results = action . execute_template ( completed_template . strip ( ) . replace ( '\r\n' , '\n' ) ) response = { "output" : results . strip ( ) , "status" : 0 } except Exception as ex : print ( str ( ex ) ) response = { "output" : "Error executing template" , "status" : 1 } return response
Refactored out of tools/views.py as this is used in a couple of different places (tools/views and screens/views)
Refactored out of tools / views . py as this is used in a couple of different places ( tools / views and screens / views )
"""
    Refactored out of tools/views.py as this is used in a couple of different places (tools/views and screens/views)
    :param post_vars: copy of all variables passed in via the operation
    :return: response object of form {'output': whatever, 'status': 1 }
    """ # compiled_template = get_template_from_string(config_template.template) # let's load any secrets if necessary
response object of form {'output': whatever, 'status': 1 }
response object of form { ' output ' : whatever ' status ' : 1 }
post_vars
copy of all variables passed in via the operation
copy of all variables passed in via the operation