Page MenuHomeVyOS Platform

Load (import) config from JSON
Open, WishlistPublicFEATURE REQUEST

Description

The API supports exporting config as JSON. Could you please add support to load JSON config as well?
Currently the load supports only the vyos config format, specs of which are unknown.

If this is for some reason not possible, could you please point me to how could I generate vyos config file from exported JSON?
Thanks

Details

Difficulty level
Unknown (require assessment)
Version
-
Why the issue appeared?
Will be filled on close
Is it a breaking change?
Perfectly compatible
Issue type
Feature (new functionality)

Event Timeline

A quick solution that convert JSON to config set

#!/usr/bin/env python3

import json
import os
from itertools import chain, starmap

# Get the absolute path for the directory where this file is located "here"
here = os.path.abspath(os.path.dirname(__file__))

with open(os.path.join(here, "my.json")) as file:
    json_text = file.read()

json_data = json.loads(json_text)


def flatten_json_iterative_solution(dictionary):
    """Flatten a nested json file"""

    def unpack(parent_key, parent_value):
        """Unpack one level of nesting in json file"""
        # Unpack one level only!

        if isinstance(parent_value, dict):
            for key, value in parent_value.items():
                temp1 = parent_key + '_' + key
                yield temp1, value
        elif isinstance(parent_value, list):
            i = 0
            for value in parent_value:
                temp2 = parent_key + '_'+str(i)
                i += 1
                yield temp2, value
        else:
            yield parent_key, parent_value


    # Keep iterating until the termination condition is satisfied
    while True:
        # Keep unpacking the json file until all values are atomic elements (not dictionary or list)
        dictionary = dict(chain.from_iterable(starmap(unpack, dictionary.items())))
        # Terminate condition: not any value in the json file is dictionary or list
        if not any(isinstance(value, dict) for value in dictionary.values()) and \
           not any(isinstance(value, list) for value in dictionary.values()):
            break

    return dictionary

c = flatten_json_iterative_solution(json_data)

for k, v in c.items():
    print('set', k, v)

It gets data from the config JSON file"my.json", parse it and convert it to vyos syntax.
Output example

sever@sever:~/scripts/from-json-to-config$ ./test.py 
set high-availability_vrrp_group_BND028_advertise-interval 5
set high-availability_vrrp_group_BND028_interface eth1
set high-availability_vrrp_group_BND028_priority 250
set high-availability_vrrp_group_BND028_virtual-address 100.64.28.1/29
set high-availability_vrrp_group_BND028_vrid 28
set interfaces_ethernet_eth0_address 192.168.122.16/24
set interfaces_ethernet_eth0_description WAN
set interfaces_ethernet_eth0_hw-id 52:54:00:bf:fb:77
set interfaces_ethernet_eth1_hw-id 52:54:00:e8:86:f7
set system_config-management_commit-revisions 100
set system_console_device_ttyS0_speed 115200
set system_host-name r6-roll
set system_login_user_foo_authentication_encrypted-password $6$ertU59lKfma.gl91$vynXmhbee3GZF81zz4/z0
set system_login_user_vyos_authentication_encrypted-password $6$JkevvrMzWvcfM$pV9t/I16CEuhSp6ZYdzZUowg4ghl/yf/
set system_login_user_vyos_authentication_plaintext-password 
set system_login_user_vyos_authentication_public-keys_sever@sever_key AAAAB3NzaC1yc2EAAAADAQABAAABgQCj8sRksLQXSPk=
set system_login_user_vyos_authentication_public-keys_sever@sever_type ssh-rsa
set system_name-server_0 1.1.1.1
set system_name-server_1 8.8.8.8
set system_name-server_2 8.8.4.4
set system_syslog_global_facility_all_level info
set system_syslog_global_facility_protocols_level debug
set system_time-zone Europe/Kiev

Need to clean values for "multinode" as we don't expect to see 0,1,2 in "name-server"

set system_name-server_0 1.1.1.1
set system_name-server_1 8.8.8.8
set system_name-server_2 8.8.4.4

May be it will be usefull

erkin set Issue type to Feature (new functionality).Aug 29 2021, 12:01 PM
erkin removed a subscriber: Active contributors.

Implemented in 1.4-1.5 T5248

IMO, what was implemented there isn't sufficient; as far as I can tell what has been implemented only allows you to load config to a single section and will error out if you try to set "path": []. I would like to take the entire config json file exported by show | json and be able to load it as a complete config, and not have to do it piecemeal section by section.

dmbaturin triaged this task as Wishlist priority.Jan 9 2024, 5:03 PM
dmbaturin edited projects, added VyOS 1.5 Circinus; removed VyOS 1.3 Equuleus (1.3.6).