Page MenuHomeVyOS Platform

Add merge_defaults as Config method
Closed, ResolvedPublicENHANCEMENT

Description

Config mode scripts vary in the level of control required when merging default values; this adds a middle level of control between automatic merging and explicit merging (example 2, below):

(1) automatic

def get_config(config=None):
    ...
    base = ['system', 'conntrack']
    conntrack = conf.get_config_dict(base, key_mangling=('-', '_'),
                                     get_first_key=True,
                                     with_recursive_defaults=True)
    return conntrack

(2) conditional

def get_config(config=None):
    ...
    base = ['load-balancing', 'reverse-proxy']
    lb = conf.get_config_dict(base,
                              get_first_key=True,
                              key_mangling=('-', '_'),
                              no_tag_node_value_mangle=True)
    ...
    if lb: 
        lb = conf.merge_defaults(lb, recursive=True)
    return lb

(3) explicit

def get_config(config=none):
    ...
    base = ['system', 'flow-accounting']
    if not conf.exists(base):
        return None
    flow_accounting = conf.get_config_dict(base, key_mangling=('-', '_'), get_first_key=True)
    
    default_values = conf.get_config_defaults(**flow_accounting.kwargs,
                                              recursive=True)
    # delete individual flow type defaults - should only be added if user
    # sets this feature
    for flow_type in ['sflow', 'netflow']:
        if flow_type not in flow_accounting and flow_type in default_values:
            del default_values[flow_type]
            
    flow_accounting = config_dict_merge(default_values, flow_accounting)

Note that as a a variation of the check for flow_type, above, one can also use the test in T5330 --- this was added for use in the verify stage, but is useful in get_config as well, as it doesn't require checking both dicts, config and default:

def get_config(config=None):
    ...
    base = ['load-balancing', 'wan']
    lb = conf.get_config_dict(base, key_mangling=('-', '_'),
                              no_tag_node_value_mangle=True,
                              get_first_key=True,
                              with_recursive_defaults=True)

    # prune limit key if not set by user
    for rule in lb.get('rule', []):
        if lb.from_defaults(['rule', rule, 'limit']):
            del lb['rule'][rule]['limit']

    return lb

The merge_defaults method has been tested and will be part of the PR for T5319.

WIP:

https://github.com/vyos/vyos-1x/compare/current...jestabro:with-defaults

Details

Difficulty level
Normal (likely a few hours)
Version
vyos-1.4
Why the issue appeared?
Will be filled on close
Is it a breaking change?
Perfectly compatible
Issue type
Improvement (missing useful functionality)