import re


def parse_tf(constants : dict) -> set:
    """Read user configured variables in variables tf and return the entire set
    Args:
        constants: config read from config.yml
    Returns:
        all variables defined in variables.tf
    """
    magma_root = constants['magma_root']
    tf_root = f'{magma_root}/orc8r/cloud/deploy/terraform'

    # parse input variables
    orc8r_var_fn_list = [
        f'{tf_root}/orc8r-aws/variables.tf',
        f'{tf_root}/orc8r-helm-aws/variables.tf'
    ]
    var_search_re = re.compile(r'variable\s\"(?P<variable_name>\w+?)\"\s\{')
    actual_vars = set()
    for fn in orc8r_var_fn_list:
        with open(fn) as f:
            # parse variables
            for line in f.readlines():
                m = var_search_re.search(line)
                if m and m.group('variable_name'):
                    actual_vars.add(m.group('variable_name'))

    # parse output variables
    orc8r_var_fn_list = [
        f'{tf_root}/orc8r-aws/outputs.tf',
    ]

    # remove variables which are set through outputs
    output_search_re = re.compile(r'output\s\"(?P<variable_name>\w+?)\"\s\{')
    for fn in orc8r_var_fn_list:
        with open(fn) as f:
            # parse variables
            for line in f.readlines():
                m = output_search_re.search(line)
                if m and m.group('variable_name'):
                    output_var = m.group('variable_name')
                    if output_var in actual_vars:
                        actual_vars.remove(output_var)
    return actual_vars