def set_config()

in source/idea/idea-administrator/src/ideaadministrator/app_main.py [0:0]


def set_config(cluster_name: str, aws_profile: str, aws_region: str, force: bool, entries):
    """
    set config entries on the dynamodb table, this does not change configurations in local file system

    \b
    entry must be of below format: Key=KEY_NAME,Type=[str|int|float|bool|list<str>|list<int>|list<float>|list<bool>],Value=[VALUE|[VALUE1,VALUE2,...]] ...
    config key names cannot contain: comma(,), colon(:)

    Examples:

    \b
    1) To set a string config type:
    ./res-admin.sh config set Key=global-settings.string_val,Type=string,Value=stringcontent --cluster-name YOUR_CLUSTER_NAME --aws-region YOUR_AWS_REGION

    \b
    2) To set an integer config type:
    ./res-admin.sh config set Key=global-settings.int_val,Type=int,Value=12 --cluster-name YOUR_CLUSTER_NAME --aws-region YOUR_AWS_REGION

    \b
    3) To set a config with list of strings:
    ./res-admin.sh config set "Key=my_config.string_list,Type=list<str>,Value=value1,value2" --cluster-name YOUR_CLUSTER_NAME --aws-region YOUR_AWS_REGION

    \b
    4) Update multiple config entries:
    ./res-admin.sh config set Key=global-settings.string_val,Type=string,Value=stringcontent \\
                               "Key=global-settings.integer_list,Type=list<int>,Value=1,2" \\
                               "Key=global-settings.string_list,Type=list<str>,Value=str1,str2" \\
                               --cluster-name YOUR_CLUSTER_NAME \\
                               --aws-region YOUR_AWS_REGION
    """

    config_entries = []
    for index, entry in enumerate(entries):
        tokens = entry.split(',', 2)
        key = tokens[0].split('Key=')[1].strip()
        data_type = tokens[1].split('Type=')[1].strip()
        value = tokens[2].split('Value=')[1].strip()

        if Utils.is_empty(key):
            raise exceptions.cluster_config_error(f'[{index}] Key is required')
        if ',' in key or ':' in key:
            raise exceptions.cluster_config_error(f'[{index}] Invalid Key: {key}. comma(,) and colon(:) are not allowed in key names.')
        if Utils.is_empty(data_type):
            raise exceptions.cluster_config_error(f'[{index}] Type is required')

        is_list = False
        if data_type in ('str', 'string'):
            data_type = 'str'
        elif data_type in ('int', 'integer'):
            data_type = 'int'
        elif data_type in ('bool', 'boolean'):
            data_type = 'bool'
        elif data_type in ('float', 'decimal'):
            data_type = 'float'
        elif data_type in ('list<str>', 'list<string>'):
            data_type = 'str'
            is_list = True
        elif data_type in ('list<int>', 'list<integer>'):
            data_type = 'int'
            is_list = True
        elif data_type in ('list<bool>', 'list<boolean>'):
            data_type = 'bool'
            is_list = True
        elif data_type in ('list<float>', 'list<decimal>'):
            data_type = 'float'
            is_list = True
        else:
            raise exceptions.cluster_config_error(f'[{index}] Type: {data_type} not supported')

        if is_list:
            tokens = value.split(',')
            value = []
            for token in tokens:
                if Utils.is_empty(token):
                    continue
                value.append(token.strip())
            if data_type == 'int':
                for val in value:
                    if not Utils.is_int(val):
                        raise exceptions.cluster_config_error(f'[{index}] Value: {value} is not a valid list<{data_type}>')
                value = Utils.get_as_int_list(value)
            elif data_type == 'float':
                for val in value:
                    if not Utils.is_float(val):
                        raise exceptions.cluster_config_error(f'[{index}] Value: {value} is not a valid list<{data_type}>')
                value = Utils.get_as_float_list(value)
            elif data_type == 'int':
                value = Utils.get_as_bool_list(value)
            else:
                value = Utils.get_as_string_list(value)
        else:
            if data_type == 'int':
                if not Utils.is_int(value):
                    raise exceptions.cluster_config_error(f'[{index}] Value: {value} is not a valid {data_type}')
                value = Utils.get_as_int(value)
            elif data_type == 'float':
                if not Utils.is_float(value):
                    raise exceptions.cluster_config_error(f'[{index}] Value: {value} is not a valid {data_type}')
                value = Utils.get_as_float(value)
            elif data_type == 'bool':
                value = Utils.get_as_bool(value)
            else:
                value = Utils.get_as_string(value)

        config_entries.append({
            'key': key,
            'value': value
        })

    context = SocaCliContext()

    table = PrettyTable(['Key', 'Value'])
    table.align = 'l'
    for config_entry in config_entries:
        key = Utils.get_value_as_string('key', config_entry, '-')
        value = Utils.get_any_value('value', config_entry, '-')
        if isinstance(value, list):
            value = Utils.to_yaml(value)
        table.add_row([key, value])

    print(table)
    if not force:
        confirm = context.prompt('Are you sure you want to update above config entries?')
        if not confirm:
            context.info('Abort!')
            raise SystemExit

    db = ClusterConfigDB(
        cluster_name=cluster_name,
        aws_region=aws_region,
        aws_profile=aws_profile
    )
    for config_entry in config_entries:
        db.set_config_entry(config_entry['key'], config_entry['value'])