Vyatta::Config (/opt/vyatta/share/perl5/Vyatta/Config.pm) is a Perl API library for working with VyOS configuration (and Vyatta and EdgeOS too).
Depecation warning
===============
Since VyOS 1.2.0, no new Perl code is accepted to the mainline VyOS. This documentation is kept for the reference since there's still old Perl code that needs bug fixes until it's replaced with new Python code.
Using the Library
=================
use lib '/opt/vyatta/share/perl5';
use Vyatta::Config;
$config = new Vyatta::Config;
Definitions
===========
**Active config** is config, currently used by system.
**Working config** is config we are making during configuration session.
Effective config
----------------
The definition of "effective" is different under these two scenarios.
1. When used outside a config session, "effective" == "active". In
other words, in such cases the effective config is the same as the
running config.
2. When used during a config session, a config path (leading to either
a "node" or a "value") is "effective" if ANY of the following
is true.
- active && working
Path is in both active and working configs, i.e., unchanged.
- !active && working && committed
Path is not in active, has been set in working, AND has already been
committed, i.e., "commit" has successfully processed the
addition/update of the path.
- active && !working && !committed
Path is in active, has been deleted from working, AND has not been
committed yet, i.e., "commit" (per priority) has not processed the
deletion of the path yet, or it has been processed but failed.
**Note:** during commit, deactivate has the same effect as delete. So in
such cases, as far as these functions are concerned, deactivated nodes
don't exist.
Method reference
================
Currently API has the following methods:
- **Low-level API functions that use the cstore library directly: **
| Method | Arguments | Returns | Purpose |
|----------------------------------------------------------------------------|----------------------------------------|----------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
| exists | Configuration node | True or false | Returns true if specified node exists in current working config. |
| existsOrig | Configuration node | True or false | Returns true if specified node exists in current active (running) config. |
| isDefault | Configuration node | True or false | Returns true if specified node has default value in current working config. |
| isDefaultOrig | Configuration node | True or false | Returns true if specified node had default value in current active (running) config. |
| listNodes | Configuration path | Array | Returns array of all child nodes under specified path in working config. |
| listOrigNodes | Configuration path | Array | Returns array of all child nodes under specified path in active config. |
| returnValue | Configuration node | Scalar | Returns value of a node under specified path in working config. |
| returnOrigValue | Configuration node | Scalar | Returns value of a node under specified path in active config. |
| returnValues | Configuration multi-value node | Array | Returns array of all values of a multi-value node under specified path in working config. |
| returnOrigValues | Configuration multi-value node | Array | Returns array of all values of a multi-value node under specified path in active config. |
|
- **Observers of the "effective" config:**
| isEffective | Configuration node | True or false | Returns true if node under specified path is "effective". |
| isActive | Configuration node | True or false | Same to isEffective. |
| listEffectiveNodes | Configuration path | Aray | Returns array of "effective" child nodes during current commit under specified path. |
| listOrigPlusComNodes | Configuration path | Array | Same to listEffectiveNodes. |
| returnEffectiveValue | Configuration node | Scalar | Returns "effective" value of a node during current commit. |
| returnOrigPlusComValue | Configuration node | Scalar | Returns "effective" value of a node during current commit. |
| returnEffectiveValues | Configuraion multi-value node | Array | Returns array of "effective" values of a multi-value node during current commit. |
| returnOrigPlusComValues | Configuraion multi-value node | Array | Returns array of "effective" values of a multi-value node during current commit. |
| isDeleted | Configuration node | True or false | Returns true if node has been deleted in working config. |
| listDeleted | Configuration path | Array | Returns array of nodes that have been deleted under specified level in working config. |
| returnDeletedValues | Configuration multi-value node | Array | Returns array of values of a multi-value node that have been deleted in working config. |
| isAdded | Configuration node | True or false | Returns true if specified node has been added in working config. |
| isChanged | Configuration node | True or false | Returns true if specified node has been changed in working config. |
| listNodeStatus | Configuration path | Hash | Return a hash of status of child nodes at specified level. Node name is the hash key. node status is the hash value. Node status can be one of "deleted", "added", "changed", or "static". |
| getTmplChildren | Configuration path | Array | Return list of child nodes in the template hierarchy at specified level. |
| validateTmplPath | Configuration path | True or false | Return whether specified path is a valid template path. |
| parseTmplAll | Configuration path | Hash | Return hash ref of parsed template of specified path, undef if path is invalid. note: if !allow_val, path must terminate at a "node", not "value". |
| hasTmplChildren | Configuration path | True or false | Returns true if specified configuration path has child nodes. |
- **"Deactivate-aware" observers of current working config or active config;**
**Note: ** deactivate/activate commands are disabled in the CLI due to unsolvable problem in the configuration backends. These functions shouldn't be used.
| deactivated | Configuration node | True or false | Return whether specified node is deactivated in working config. Note that this is different from "marked deactivated". If a node is "marked deactivated", then the node itself and any descendants are "deactivated". |
| deactivatedOrig | Configuration node | True or false | Return whether specified node is deactivated in active config. |
| returnValuesDA | Configuration multi-value node | Array | Deactivate aware version of returnValues(). |
| returnOrigValuesDA | Configuration multi-value node | Array | DA version of returnOrigValues(). |
| returnValueDA | Configuration node | Scalar | DA version of returnOrigValue(). |
| listOrigNodesDA | Configuration path | Array | DA version of listOrigNodes() |
| listNodeStatusDA | Configuration path | Hash | DA version of listNodeStatus(). |
| returnComment | Configuration node | Scalar | Returns comment of "node" in working config or undef if comment doesn't exist. |
| returnOrigComment | Configuration node | Scalar | Returns comment of "node" in active config or undef if comment doesn't exist |
- **High-level API functions (not using the cstore library directly):**
| setLevel | Configuration path | Current level | Set the current level of config hierarchy to specified level (if defined). |
| returnParent | Configuration node | Scalar | Returns the name of ancestor node relative to the current level. Each level up is represented by a ".." in the argument. |
| parseTmpl | Configuration path | Array | Parse template of specified path and return ($is_multi, $is_text, $default) or undef if specified path is not valid. |
| isTagNode | Configuration path | True or false | Returns true if specified path is a tag node. |
| isLeafNode | Configuration node | True or false | Returns true if specified path is a "leaf node", i.e., single-/multi-value node. |
| isLeafValue | Configuration node | True or false | Returns true if specified path is a "leaf value", i.e., value of a leaf node. |
| compareValueLists | Original value list and new value list | Lists of deleted and added nodes | Compare two value lists and return "deleted" and "added" lists. Since this is for multi-value nodes, there is no "changed" (if a value's ordering changed, it is deleted then added). |
Usage example
-------------
Background: Your helpdesk guys ask you to make a spreadsheet with
remote-access PPTP VPN user information and keep it up to date. Of
course you do not want to copy and paste this information by hand.
All spreadsheet software supports import from CSV, so the only thing we
need to do is to extract values from config and print then as comma
separated lines, passwords enclosed into double quotes for the case they
contain a comma and add some header. Like this:
```
user,password,address,is_disabled
johnsmith,"this,isapass",192.0.2.41,no
```
Here we go:
#!/usr/bin/perl
use strict;
# using the Vyatta perl API
use lib "/opt/vyatta/share/perl5/";
use Vyatta::Config;
# Settings
my $proto = "pptp";
my $delimiter = ",";
# CSV RFC4180 requires to use CRLF (\r\n) as line break
my $header = "user,password,address,disabled\r\n";
# Create a config instance
my $config = new Vyatta::Config;
# Set default config level, this allows to use relative paths
$config->setLevel("vpn pptp remote-access authentication local-users username");
# Obtain array of user names
my @users = $config->listNodes();
# Print file header
print $header;
# Now walk through the user names, obtain values and print them
foreach my $user (@users) {
# Since we set config level to "vpn pptp remote-access authentication local-users username"
# we now can specify only the remaining part as methods argument,
# it will be appended to the path we used in setLevel()
my $password = $config->returnValue("$user password");
my $address = $config->returnValue("$user static-ip");
#"disable" is a leaf node with no value, so we should check
# if it exists instead of obtaining its value
my $disabled = $config->exists("$user disable") ? "yes" : "no";
# It's not prohibited to use "," in passwords,
# so we should enclose them into quotes to prevent
# the parser from treating them as delimiters
$password = "\"$password\"";
my $line = join($delimiter, $user, $password, $address, $disabled);
print "$line\r\n";
}
Check it works:
vyatta@R1# show vpn pptp remote-access authentication local-users
username joerandomuser {
disable
password qwerrty
static-ip 10.91.17.105
}
username johnsmith {
password this,isapass
}
vyatta@R1# perl ./pptpusers.pl
user,password,address,disabled
joerandomuser,"qwerrty",10.91.17.105,yes
johnsmith,"this,isapass",,no