Loading Inline or External Tables and Views in Junos PyEZ Applications

 

Junos PyEZ Tables and Views provide a simple and efficient way to configure devices running Junos OS or extract specific information from operational command output or configuration data on the device. Junos PyEZ provides a set of predefined operational Tables and Views that you can use in applications, or you can create your own custom operational or configuration Tables and Views.

You can create quick inline Tables and Views as a multiline string directly in the Python application, or you can create one or more Table and View definitions in external files and import the Tables into your Python application. Inline Tables and Views are simpler to use, but using external files enables you to create a central, reusable library.

To use Junos PyEZ’s predefined Tables and Views in your Python application, you must import the Table into your application. To use custom Tables and Views, you must create the Table and View definitions, and then either load or import the definitions into the application, depending on whether they are internal or external to the module. The following sections outline this process for Tables and Views that are both internal and external to the module.

Loading Inline Tables and Views

In Junos PyEZ, you define inline Tables and Views in YAML as a multiline string in your module. For example:

To use inline Tables and Views, you must include several import statements in your module. Along with importing the Junos PyEZ Device class, you must also import the yaml package, which enables you to work with YAML, and the Junos PyEZ jnpr.junos.factory.factory_loader.FactoryLoader class, which is used to load the Table and View definitions.

To load the Table and View definitions, you must include the following statement in your Python application, where string-name is the identifier for the multiline string that contains the Table/View definition:

In this statement, the yaml.load() method maps the YAML-formatted Table definition to Python. The FactoryLoader().load() method then loads the Table and View definitions as if you had imported them from a module so that you can manipulate them as a class. Finally, the globals().update() call imports the definitions directly into your namespace.

After the Table and View definitions are loaded, there is no difference in how you use inline or external Tables in your module, which is briefly outlined in Using Tables and Views. The following example loads the inline Table UserTable and retrieves and prints each username and corresponding login class:

Importing Predefined or External Tables and Views

Custom external Table and View definitions are placed in files that are external to the Python module. You must create two files with the same base name, one of which has a .yml extension, and the other of which has a .py extension. The .yml file contains one or more Table/View definitions. For example:

[user@server]$ cat myTables/ConfigTables.yml

The .py file must always contain the following four lines of code, which cause the main script to load the Table and View definitions as if you had imported them from a module so that you can manipulate them as a class within the script:

[user@server]$ cat myTables/ConfigTables.py

If the .yml and .py files are located in a subdirectory, you must also add an __init__.py file to that subdirectory, so that Python checks this directory when it processes the Table import statements in your application.

[user@server]$ ls myTables

To use Junos PyEZ’s predefined Tables or your own custom external Tables in a Junos PyEZ application, you must include the appropriate import statements in your application. Along with importing the Junos PyEZ Device class, you must also import any required Tables.

The following example imports a predefined operational Table, EthPortTable, from the jnpr.junos.op.ethport module:

The following example imports a custom configuration Table. The myTables.ConfigTables construct references the ConfigTables.py file within the myTables subdirectory and imports the UserTable Table, which is defined in the ConfigTables.yml file:

After the Table and View definitions are imported, there is no difference in how you use inline or external Tables in your module, which is briefly outlined in Using Tables and Views. The following example loads the external Table UserTable and retrieves and prints each username and corresponding login class:

Using Tables and Views

After you load or import the Table and View definitions, you can use the predefined, inline, or custom external Tables in the same manner. To use a Table, first create the Device instance and open a connection to the target device. Then create the Table instance and associate it with the Device instance. You can then use the Table to retrieve information, configure the device, or both, depending on the type of Table.

In this example, UserTable retrieves user objects from the [edit system login] hierarchy level. To retrieve the Table information, call the get() method with any desired arguments.

You can now iterate over and manipulate the resulting object to extract the required information. For example:

The following example uses the UserTable Table and retrieves and prints each username and corresponding login class:

For more information about using Junos PyEZ Tables, see the following topics: