Page MenuHomeVyOS Platform

Perl API
Updated 1,475 Days AgoPublic

Version 1 of 7: You are viewing an older version of this document, as it appeared on Apr 14 2020, 10:03 PM.

<p>Vyatta::Config (/opt/vyatta/share/perl5/Vyatta/Config.pm) is a Perl API library for working with VyOS configuration (and Vyatta and EdgeOS too).</p>
<h1 id="using_the_library">Using the Library</h1>
<pre><code>use lib &#39;/opt/vyatta/share/perl5&#39;;
use Vyatta::Config;
$config = new Vyatta::Config;</code></pre>
<h1 id="definitions">Definitions</h1>
<p><strong>Active config</strong> is config, currently used by system.</p>
<p><strong>Working config</strong> is config we are making during configuration session.</p>
<h2 id="effective_config">Effective config</h2>
<p>The definition of "effective" is different under these two scenarios.</p>
<ol>
<li>When used outside a config session, "effective" == "active". In other words, in such cases the effective config is the same as the running config.</li>
<li>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.</li>
</ol>
<ul>
<li>active &amp;&amp; working<br />
Path is in both active and working configs, i.e., unchanged.</li>
<li>!active &amp;&amp; working &amp;&amp; committed<br />
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.</li>
<li>active &amp;&amp; !working &amp;&amp; !committed<br />
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.</li>
</ul>
<p><strong>Note:</strong> 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.</p>
<h1 id="method_reference">Method reference</h1>
<p>Currently API has the following methods:</p>

<table> <thead> <tr class="header"> <th><p>Method</p></th> <th><p>Arguments</p></th> <th><p>Returns</p></th> <th><p>Purpose</p></th> </tr> </thead> <tbody> <tr class="odd"> <td><p>Low-level API functions that use the cstore library directly.</p></td> <td></td> <td></td> <td></td> </tr> <tr class="even"> <td><p>exists</p></td> <td><p>Configuration node</p></td> <td><p>True or false</p></td> <td><p>Returns true if specified node exists in current working config.</p></td> </tr> <tr class="odd"> <td><p>existsOrig</p></td> <td><p>Configuration node</p></td> <td><p>True or false</p></td> <td><p>Returns true if specified node exists in current active (running) config.</p></td> </tr> <tr class="even"> <td><p>isDefault</p></td> <td><p>Configuration node</p></td> <td><p>True or false</p></td> <td><p>Returns true if specified node has default value in current working config.</p></td> </tr> <tr class="odd"> <td><p>isDefaultOrig</p></td> <td><p>Configuration node</p></td> <td><p>True or false</p></td> <td><p>Returns true if specified node had default value in current active (running) config.</p></td> </tr> <tr class="even"> <td><p>listNodes</p></td> <td><p>Configuration path</p></td> <td><p>Array</p></td> <td><p>Returns array of all child nodes under specified path in working config.</p></td> </tr> <tr class="odd"> <td><p>listOrigNodes</p></td> <td><p>Configuration path</p></td> <td><p>Array</p></td> <td><p>Returns array of all child nodes under specified path in active config.</p></td> </tr> <tr class="even"> <td><p>returnValue</p></td> <td><p>Configuration node</p></td> <td><p>Scalar</p></td> <td><p>Returns value of a node under specified path in working config.</p></td> </tr> <tr class="odd"> <td><p>returnOrigValue</p></td> <td><p>Configuration node</p></td> <td><p>Scalar</p></td> <td><p>Returns value of a node under specified path in active config.</p></td> </tr> <tr class="even"> <td><p>returnValues</p></td> <td><p>Configuration multi-value node</p></td> <td><p>Array</p></td> <td><p>Returns array of all values of a multi-value node under specified path in working config.</p></td> </tr> <tr class="odd"> <td><p>returnOrigValues</p></td> <td><p>Configuration multi-value node</p></td> <td><p>Array</p></td> <td><p>Returns array of all values of a multi-value node under specified path in active config.</p></td> </tr> <tr class="even"> <td><p>Observers of the "effective" config</p></td> <td></td> <td></td> <td></td> </tr> <tr class="odd"> <td><p>isEffective</p></td> <td><p>Configuration node</p></td> <td><p>True or false</p></td> <td><p>Returns true if node under specified path is "effective".</p></td> </tr> <tr class="even"> <td><p>isActive</p></td> <td><p>Configuration node</p></td> <td><p>True or false</p></td> <td><p>Same to isEffective.</p></td> </tr> <tr class="odd"> <td><p>listEffectiveNodes</p></td> <td><p>Configuration path</p></td> <td><p>Aray</p></td> <td><p>Returns array of "effective" child nodes during current commit under specified path.</p></td> </tr> <tr class="even"> <td><p>listOrigPlusComNodes</p></td> <td><p>Configuration path</p></td> <td><p>Array</p></td> <td><p>Same to listEffectiveNodes.</p></td> </tr> <tr class="odd"> <td><p>returnEffectiveValue</p></td> <td><p>Configuration node</p></td> <td><p>Scalar</p></td> <td><p>Returns "effective" value of a node during current commit.</p></td> </tr> <tr class="even"> <td><p>returnOrigPlusComValue</p></td> <td><p>Configuration node</p></td> <td><p>Scalar</p></td> <td><p>Returns "effective" value of a node during current commit.</p></td> </tr> <tr class="odd"> <td><p>returnEffectiveValues</p></td> <td><p>Configuraion multi-value node</p></td> <td><p>Array</p></td> <td><p>Returns array of "effective" values of a multi-value node during current commit.</p></td> </tr> <tr class="even"> <td><p>returnOrigPlusComValues</p></td> <td><p>Configuraion multi-value node</p></td> <td><p>Array</p></td> <td><p>Returns array of "effective" values of a multi-value node during current commit.</p></td> </tr> <tr class="odd"> <td><p>isDeleted</p></td> <td><p>Configuration node</p></td> <td><p>True or false</p></td> <td><p>Returns true if node has been deleted in working config.</p></td> </tr> <tr class="even"> <td><p>listDeleted</p></td> <td><p>Configuration path</p></td> <td><p>Array</p></td> <td><p>Returns array of nodes that have been deleted under specified level in working config.</p></td> </tr> <tr class="odd"> <td><p>returnDeletedValues</p></td> <td><p>Configuration multi-value node</p></td> <td><p>Array</p></td> <td><p>Returns array of values of a multi-value node that have been deleted in working config.</p></td> </tr> <tr class="even"> <td><p>isAdded</p></td> <td><p>Configuration node</p></td> <td><p>True or false</p></td> <td><p>Returns true if specified node has been added in working config.</p></td> </tr> <tr class="odd"> <td><p>isChanged</p></td> <td><p>Configuration node</p></td> <td><p>True or false</p></td> <td><p>Returns true if specified node has been changed in working config.</p></td> </tr> <tr class="even"> <td><p>listNodeStatus</p></td> <td><p>Configuration path</p></td> <td><p>Hash</p></td> <td><p>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".</p></td> </tr> <tr class="odd"> <td><p>getTmplChildren</p></td> <td><p>Configuration path</p></td> <td><p>Array</p></td> <td><p>Return list of child nodes in the template hierarchy at specified level.</p></td> </tr> <tr class="even"> <td><p>validateTmplPath</p></td> <td><p>Configuration path</p></td> <td><p>True or false</p></td> <td><p>Return whether specified path is a valid template path.</p></td> </tr> <tr class="odd"> <td><p>parseTmplAll</p></td> <td><p>Configuration path</p></td> <td><p>Hash</p></td> <td><p>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".</p></td> </tr> <tr class="even"> <td><p>hasTmplChildren</p></td> <td><p>Configuration path</p></td> <td><p>True or false</p></td> <td><p>Returns true if specified configuration path has child nodes.</p></td> </tr> <tr class="odd"> <td><p>"Deactivate-aware" observers of current working config or active config.</p></td> <td></td> <td></td> <td></td> </tr> <tr class="even"> <td><p>deactivated</p></td> <td><p>Configuration node</p></td> <td><p>True or false</p></td> <td><p>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".</p></td> </tr> <tr class="odd"> <td><p>deactivatedOrig</p></td> <td><p>Configuration node</p></td> <td><p>True or false</p></td> <td><p>Return whether specified node is deactivated in active config.</p></td> </tr> <tr class="even"> <td><p>returnValuesDA</p></td> <td><p>Configuration multi-value node</p></td> <td><p>Array</p></td> <td><p>Deactivate aware version of returnValues().</p></td> </tr> <tr class="odd"> <td><p>returnOrigValuesDA</p></td> <td><p>Configuration multi-value node</p></td> <td><p>Array</p></td> <td><p>DA version of returnOrigValues().</p></td> </tr> <tr class="even"> <td><p>returnValueDA</p></td> <td><p>Configuration node</p></td> <td><p>Scalar</p></td> <td><p>DA version of returnOrigValue().</p></td> </tr> <tr class="odd"> <td><p>listOrigNodesDA</p></td> <td><p>Configuration path</p></td> <td><p>Array</p></td> <td><p>DA version of listOrigNodes()</p></td> </tr> <tr class="even"> <td><p>listNodeStatusDA</p></td> <td><p>Configuration path</p></td> <td><p>Hash</p></td> <td><p>DA version of listNodeStatus().</p></td> </tr> <tr class="odd"> <td><p>returnComment</p></td> <td><p>Configuration node</p></td> <td><p>Scalar</p></td> <td><p>Returns comment of "node" in working config or undef if comment doesn't exist.</p></td> </tr> <tr class="even"> <td><p>returnOrigComment</p></td> <td><p>Configuration node</p></td> <td><p>Scalar</p></td> <td><p>Returns comment of "node" in active config or undef if comment doesn't exist</p></td> </tr> <tr class="odd"> <td><p>High-level API functions (not using the cstore library directly)</p></td> <td></td> <td></td> <td></td> </tr> <tr class="even"> <td><p>setLevel</p></td> <td><p>Configuration path</p></td> <td><p>Current level</p></td> <td><p>Set the current level of config hierarchy to specified level (if defined).</p></td> </tr> <tr class="odd"> <td><p>returnParent</p></td> <td><p>Configuration node</p></td> <td><p>Scalar</p></td> <td><p>Returns the name of ancestor node relative to the current level. Each level up is represented by a ".." in the argument.</p></td> </tr> <tr class="even"> <td><p>parseTmpl</p></td> <td><p>Configuration path</p></td> <td><p>Array</p></td> <td><p>Parse template of specified path and return ($is_multi, $is_text, $default) or undef if specified path is not valid.</p></td> </tr> <tr class="odd"> <td><p>isTagNode</p></td> <td><p>Configuration path</p></td> <td><p>True or false</p></td> <td><p>Returns true if specified path is a tag node.</p></td> </tr> <tr class="even"> <td><p>isLeafNode</p></td> <td><p>Configuration node</p></td> <td><p>True or false</p></td> <td><p>Returns true if specified path is a "leaf node", i.e., single-/multi-value node.</p></td> </tr> <tr class="odd"> <td><p>isLeafValue</p></td> <td><p>Configuration node</p></td> <td><p>True or false</p></td> <td><p>Returns true if specified path is a "leaf value", i.e., value of a leaf node.</p></td> </tr> <tr class="even"> <td><p>compareValueLists</p></td> <td><p>Original value list and new value list</p></td> <td><p>Lists of deleted and added nodes</p></td> <td><p>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).</p></td> </tr> </tbody> </table>

<h2 id="usage_example">Usage example</h2>
<p>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.</p>
<p>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:</p>
<p><code>user,password,address,is_disabled</code><br />
<code>johnsmith,"this,isapass",192.0.2.41,no</code></p>
<p>Here we go:</p>
<pre><code>#!/usr/bin/perl

use strict;

using the Vyatta perl API

use lib &quot;/opt/vyatta/share/perl5/&quot;;
use Vyatta::Config;

Settings

my $proto = &quot;pptp&quot;;
my $delimiter = &quot;,&quot;;

CSV RFC4180 requires to use CRLF (\r\n) as line break

my $header = &quot;user,password,address,disabled\r\n&quot;;

Create a config instance

my $config = new Vyatta::Config;

Set default config level, this allows to use relative paths

$config-&gt;setLevel(&quot;vpn pptp remote-access authentication local-users username&quot;);

Obtain array of user names

my @users = $config-&gt;listNodes();

Print file header

print $header;

Now walk through the user names, obtain values and print them

foreach my $user (@users) {

  1. Since we set config level to &quot;vpn pptp remote-access authentication local-users username&quot;
  2. we now can specify only the remaining part as methods argument,
  3. it will be appended to the path we used in setLevel() my $password = $config-&gt;returnValue(&quot;$user password&quot;); my $address = $config-&gt;returnValue(&quot;$user static-ip&quot;); #&quot;disable&quot; is a leaf node with no value, so we should check
  4. if it exists instead of obtaining its value my $disabled = $config-&gt;exists(&quot;$user disable&quot;) ? &quot;yes&quot; : &quot;no&quot;;
  5. It&#39;s not prohibited to use &quot;,&quot; in passwords,
  6. so we should enclose them into quotes to prevent
  7. the parser from treating them as delimiters $password = &quot;\&quot;$password\&quot;&quot;;

    my $line = join($delimiter, $user, $password, $address, $disabled); print &quot;$line\r\n&quot;;

}</code></pre>
<p>Check it works:</p>
<pre><code>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,&quot;qwerrty&quot;,10.91.17.105,yes
johnsmith,&quot;this,isapass&quot;,,no</code></pre>
<p><a href="Category:_Development" title="wikilink">Category: Development</a></p>

Last Author
Unknown Object (User)
Last Edited
Apr 14 2020, 10:03 PM

Event Timeline

Unknown Object (User) deleted this document.Apr 14 2020, 10:03 PM
Unknown Object (User) created this object.
Unknown Object (User) edited the content of this document. (Show Details)Apr 15 2020, 7:59 PM
Unknown Object (User) edited the content of this document. (Show Details)Apr 15 2020, 8:03 PM
Unknown Object (User) edited the content of this document. (Show Details)Apr 16 2020, 11:47 AM
Unknown Object (User) edited the content of this document. (Show Details)Apr 19 2020, 11:21 PM
Unknown Object (User) published a new version of this document.