ConfD User Community

How to get Container as an Object using Python API


#1

Hi,

Please help me what python api to be used to get container as an object in python?

Regards,
Harish Altiostar


#2

The maagic API does exactly that. Sadly, there is no example showing how to use that, so here’s a short one, albeit a bit concocted (using the dhcp introductory data model):

from confd import maapi, maagic

# create "local subnet" for all networks
loc_subnet = ['127.0.0.1', '255.255.255.0']
with maapi.single_write_trans('admin', 'system') as trans:
    # start with the root node
    root = maagic.get_root(trans)
    # iterate over "shared-network" instances:
    for network in root.dhcp.shared_networks.shared_network:
        subnet_list = network.subnets.subnet
        # if the local subnet is not there, create it
        if loc_subnet not in subnet_list:
            subnet_list.create(loc_subnet)
    trans.apply()