Define Views for Junos PyEZ Configuration Tables
Junos PyEZ configuration Tables can extract specific data from the selected configuration
database of a Junos device, or they can define structured resources that can be used to
programmatically configure a Junos device. A Table is associated with a View, which is used to
select and reference elements in the Table data. You associate a Table with a particular View
by including the view property in the Table definition, which takes the View
name as its argument.
A View maps your user-defined field names to XML elements in the selected Table items. A View enables you to access specific fields in the data as variables with properties that can be manipulated in Python. Junos PyEZ handles the extraction of the data into Python as well as any type conversion or data normalization.
When retrieving configuration data using Tables that have the get or the set property,
the View fields specify which configuration data the application should
retrieve for the object. For Tables that include the set property and define resources that you can configure
on a device, the fields defined in the View restrict which statements
that you can configure for that resource.
Junos PyEZ Views, like Tables, are formatted using YAML. View definitions that are associated with configuration Tables can include a number of parameters, which are summarized in Table 1.
View Parameter Name |
View Parameter |
Table Type |
Description |
|---|---|---|---|
View name |
– |
|
User-defined View identifier. |
Field items |
|
|
Associative array, or dictionary, of key-value pairs
that map user-defined field names to XPath expressions that select
elements in the configuration data. The field names must be valid
Python variable names. The XPath expressions are relative to the context
defined by the When the Table scope uses |
Field groups |
|
|
Associative array, or dictionary, of key-value pairs
that map user-defined field names to XPath expressions that select
elements in the configuration data. The field names must be valid
Python variable names. The XPath expressions are relative to the context
set by the corresponding When the Table scope uses |
Groups |
|
|
Associative array, or dictionary, of key-value pairs
that map a user-defined group name to an XPath expression that sets
the XPath context for fields in that group. The Xpath expression is
relative to the context defined by the |
Consider the following Junos PyEZ configuration Tables
and Views. UserTable, which includes the get property, extracts configuration data for user
accounts on the target device. UserConfigTable, which includes the set property, defines
a structured configuration resource that can be used to configure
user accounts on the target device as well as retrieve configuration
data for user accounts.
---
UserTable:
get: system/login/user
view: UserView
UserView:
groups:
auth: authentication
fields:
username: name
userclass: class
uid: uid
uidgroup: { uid: group }
fullgroup: { full-name: group }
fields_auth:
password: encrypted-password
---
UserConfigTable:
set: system/login/user
key-field:
username
view: UserConfigView
UserConfigView:
groups:
auth: authentication
fields:
username: name
userclass: class
uid: uid
fields_auth:
password: encrypted-password
The following sections discuss the different components of the View:
View Name
The View name is a user-defined identifier for the View.
You associate a Table with a particular View by including the view property in the Table definition and providing
the View name as its argument. For example:
--- UserTable: # Table definition view: UserView UserView: # View definition
Fields (fields)
You customize Views so that they only reference the necessary
elements in the selected configuration data. To do this you include
the fields property and an associative
array containing the mapping of user-defined field names to the XPath
expressions that select the desired elements from the configuration
Table items. The field names must be valid Python variable names.
The XPath expressions are relative to the configuration scope defined
by the get or set property in the Table definition.
When retrieving configuration data using Tables that include
either the get or the set property, the fields defined in the View identify the statements
to extract from the configuration. For Tables that include the set property and define resources that you can configure
on a device, the fields identify the statements that you can configure
for that resource. You must explicitly define fields for all identifier
elements for a configuration resource. These identifier fields are
then referenced in the key-field property
in the corresponding Table definition.
Consider the following sample configuration hierarchy:
user@router> show configuration system login | display xml
<rpc-reply>
<configuration>
<system>
<login>
...
<user>
<name>user1</name>
<uid>2001</uid>
<class>super-user</class>
<authentication>
<encrypted-password>...</encrypted-password>
</authentication>
</user>
<user>
<name>readonly</name>
<uid>3001</uid>
<class>read-only</class>
<authentication>
<encrypted-password>...</encrypted-password>
</authentication>
</user>
</login>
</system>
</configuration>
</rpc-reply>If the Table get or set parameter defines the scope as system/login/user, the XPath expression for each field in the View definition is relative
to that context. The following View definition maps the user-defined
field names username, userclass, and uid to child elements of the <user> element:
UserTable:
get: system/login/user
...
UserView:
fields:
username: name
userclass: class
uid: uid
If the Table definition includes the set property, you must explicitly define fields for any identifier elements
that uniquely identify the object, which in this case is <name>. The Table’s key-field property must reference all View fields that map to identifier elements
for an object. You must always define at least one identifier element
in the fields and key-field properties in set Tables.
In the Python script, you can then access a View item as a variable property.
from jnpr.junos import Device from myTables.ConfigTables import UserTable with Device(host='router.example.com') as dev: users = UserTable(dev) users.get() for account in users: print("Username is {}\nUser class is {}".format(account.name, account.userclass))
When retrieving configuration data, each object that
has a <name> element in the data has
a default name property that you can use
to access the value for that element.
View fields can include different options depending on the type
of the Table that references that View. Tables that define structured
configuration resources (set) can include
type and constraint checks for each field to ensure that the Junos
PyEZ application provides valid data when configuring the resource
on a device. Tables that retrieve configuration data (get) can include options that return attribute values
for specific elements or that specify the data type to use in the
application. Field Options ('get' Tables) and Field Options ('set' Tables) outline the options that can be included when using get and set Tables, respectively.
Field Options ('get' Tables)
Tables that include the get property
and solely retrieve configuration data from a device can define a
number of options or operators for fields in the associated View.
This section outlines the various options.
The field format determines the type for a field’s
value. By default, field values are stored as strings. You can specify
a different type for the value in the field mapping. The following
example defines the value of the uid element
to be an integer:
UserView:
fields:
username: name
userclass: class
uid: { uid : int }
You can also set the field item’s value to a Boolean by using the following syntax:
fieldname: { element-name: (True | False)=regex(expression) }The element’s value is evaluated against the regular
expression passed to regex(). If the element’s
value matches the expression, the field item’s value is set
to the Boolean defined in the format. In the following example, the superuser field is set to True if the value of the class element contains 'super-user':
superuser: { class : True=regex(super-user) }Junos PyEZ also provides the group operator for fields in configuration Views.
The group operator enables you to access the value of the
junos:group attribute for elements that are inherited from Junos
configuration groups. This value indicates the group from which that element was
inherited.
For example, in the following configuration, the remote user is
inherited from the global configuration group configured at the
[edit groups global] hierarchy level.
<user junos:group="global">
<name junos:group="global">remote</name>
<uid junos:group="global">2000</uid>
...
</user>You include the group operator in
the field mapping to reference the value of the junos:group attribute instead of the value of the element. The following example
defines the uidgroup and fullgroup fields with the group operator. When
you access these field names in a script, the field references the
value of the junos:group attribute associated
with the uid or full-name element.
UserView:
groups:
auth: authentication
fields:
username: name
userclass: class
uid: uid
uidgroup: { uid: group }
fullgroup: { full-name: group }
fields_auth:
password: encrypted-password
Field Options ('set' Tables)
Tables that define structured configuration resources (set) can include type and constraint checks for each
field in the associated View to ensure that the Junos PyEZ application
provides valid data when configuring the resource on a device. Type
checks ensure that the Junos PyEZ application supplies the correct
data type when it configures the statements for a specific resource.
Constraint checks enable you to define a default value for statements
and ensure that the application supplies values that are in the correct
range for those statements. The supported type and constraint checks,
which are included as options for the fields of the associated View,
are outlined in this section.
Table 2 and Table 3 summarize the type and constraint checks, respectively, that
you can define for fields in the View of a set configuration Table. Type checks are mutually exclusive, but multiple
constraint checks can be defined for each field.
|
Description |
Example |
|---|---|---|
|
Field only accepts Boolean values of |
|
|
Field only accepts one of the values defined in the |
|
|
Field only accepts floating point values |
|
|
Field only accepts integer values |
|
|
Field only accepts string values |
|
Constraint Check Name |
Description |
Example |
|---|---|---|
|
Default value for a field. A field uses its default value when the user does not explicitly
configure the field. If the user calls the |
|
|
Maximum value for a field, which is interpreted based
on the field |
|
|
Minimum value for a field, which is interpreted based
on the field |
|
You can only define a single type check for a field, but you
can define multiple constraint checks. Thus a field could include
a default value, a minimum value (minValue), and a maximum value (maxValue).
native_vlan : { 'native-vlan-id' : { 'type' : 'int', 'default' : 501, 'minValue' : 0, 'maxValue' : 4094 } }The minValue and maxValue options are interpreted based on the value for the type option. By default, field values are strings.
For strings, minValue and maxValue are the minimum and maximum lengths for the
string. For integers and floats, the values are the minimum and maximum
values for that type.
If you include type or constraint checks for a field, and the
user supplies configuration data that fails the checks, the Junos
PyEZ application raises the appropriate TypeError or ValueError exception with a message
that describes the error.
Groups (groups) and Field Groups (fields_)
Groups provide a shortcut method to select and reference elements within a specific node-set in a Table item.
In the following configuration data, the <authentication> element contains a child element
corresponding to the user’s authentication method:
<configuration>
<system>
<login>
...
<user>
<name>user1</name>
<uid>2001</uid>
<class>super-user</class>
<authentication>
<encrypted-password>...</encrypted-password>
</authentication>
</user>
<user>
<name>readonly</name>
<uid>3001</uid>
<class>read-only</class>
<authentication>
<encrypted-password>...</encrypted-password>
</authentication>
</user>
</login>
</system>
</configuration>
Within the View definition, you can use the fields property to access the child elements by providing
the full XPath expression to each element relative to the selected
configuration hierarchy. For example, if the Table get or set property selects <user> elements at the [edit system login] hierarchy level, the field item mapping would use the following
XPath expression:
UserConfigTable:
set: system/login/user
...
UserConfigView:
fields:
password: authentication/encrypted-password
Alternatively, you can create a group that sets the XPath context
to the <authentication> element and
then define field group items that just provide the XPath expression
relative to that context. You can define any number of groups within
a View definition.
To create a group, include the groups property and map a user-defined group name to the XPath expression
that defines the new context. Then define a field group whose name
is fields_ followed by the group name.
The field group is an associative array containing the mapping of
user-defined field names to XPath expressions that now are relative
to the context set within groups. The field
names must be valid Python variable names.
The following example defines the group auth and the corresponding field group fields_auth. The auth group
sets the context to the system/login/user/authentication hierarchy level, and the password field
references the value of the encrypted-password element.
UserConfigTable:
set: system/login/user
...
UserConfigView:
groups:
auth: authentication
fields_auth:
password: encrypted-password
...
Whether you use fields or field groups, you access the value in the same manner within the Junos PyEZ script by using the user-defined field names.