VCF Automation Blog

from Stefan Schnell


Get Configuration Variable Value with Python


"""
@module de.stschnell

@author Stefan Schnell <mail@stefan-schnell.de>
@license MIT
@version 0.5.0

@runtime python:3.11

@memoryLimit 128000000

@inputType in_userName {string}
@inputType in_password {SecureString}

@outputType Properties
"""


import json
from util.http import Http

http = Http()


def getConfigurationVariableValue(
    vcoUrl: str,
    bearerToken: str,
    configurationFolder: str,
    configurationName: str,
    configurationVariableName: str,
    configurationVariableType: str
) -> dict:
    """ Gets a value of a variable from a configuration

    @param {string} vcoUrl - URL of Aria orchestrator
    @param {string} bearerToken
    @param {string} configurationFolder - Path of the configuration
    @param {string} configurationName - Name of the configuration
    @param {string} configurationVariableName - Name of the variable
    @param {string} configurationVariableType - Type of the variable
    @returns {dict or None}
    """

    returnValue: dict | None = None

    try:

        configurations: dict = http.request(
            url = vcoUrl + "/api/configurations",
            bearerToken = bearerToken
        )

        found: bool = False
        for configuration in configurations["link"]:
            id: str | None = None
            for attribute in configuration["attributes"]:
                if attribute["name"] == "name" and \
                attribute["value"] == configurationName:
                    found = True
                if attribute["name"] == "id":
                    id = attribute["value"]

            if found == True and id != None:

                categoryId: str = http.request(
                    url = vcoUrl + "/api/configurations/" + id,
                    bearerToken = bearerToken
                )["category-id"]

                categoryPath: str = http.request(
                    url = vcoUrl + "/api/categories/" + categoryId,
                    bearerToken = bearerToken
                )["path"]

                if categoryPath != configurationFolder:
                    found = False

            if found:
                break

        if id != None:

            config: dict = http.request(
                url = vcoUrl + "/api/configurations/" + id,
                bearerToken = bearerToken
            )

            for attribute in config["attributes"]:
                if attribute["name"] == configurationVariableName:
                    returnValue = \
                        attribute["value"][configurationVariableType.lower()]
                    break

    except Exception as err:
        raise ValueError(f"An error occurred at get variable value - {err}") \
            from err

    return returnValue


def handler(context: dict, inputs: dict) -> dict:
    """ Aria Automation standard handler, the main function.
    """

    vcoUrl: str = context["vcoUrl"]
    bearerToken: str = context["getToken"]()

    # @example
    configurationFolder: str = "Configurations"
    configurationName: str = "Host Security Hardening Options"
    configurationVariableName: str = "authSource"
    configurationVariableType: str = "string"
    # In the HOL, this delivers local.
    # Types are: string, boolean, number, date, secure-string, array,
    # sdk-object, properties, encrypted-string, mime-attachment and
    # regex.
    # Hint: Do not use a leading slash for the folder variable.

    outputs: dict = {}

    try:

        output: dict | None = getConfigurationVariableValue(
            vcoUrl,
            bearerToken,
            configurationFolder,
            configurationName,
            configurationVariableName,
            configurationVariableType
        )

        outputs = {
            "status": "done",
            "error": None,
            "result": output
        }

    except Exception as err:

        outputs = {
            "status": "incomplete",
            "error": repr(err),
            "result": None
        }

    return outputs

vcf automation orchestrator python action to get configuration variable value

References