Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

header-navigation
keyboard_arrow_up
close
keyboard_arrow_left
Junos PyEZ Developer Guide
Table of Contents Expand all
list Table of Contents
file_download PDF
{ "lLangCode": "en", "lName": "English", "lCountryCode": "us", "transcode": "en_US" }
English
keyboard_arrow_right

Load Inline or External Tables and Views in Junos PyEZ Applications

date_range 09-May-24

Import predefined Tables or inline or external custom Tables into your Junos PyEZ application.

Junos PyEZ Tables and Views provide a simple and efficient way to configure Junos devices or extract specific information from operational command output or configuration data. 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.

Import Junos PyEZ’s Predefined Tables and Views

The Junos PyEZ jnpr.junos.op module and jnpr.junos.command module provide predefined Table and View definitions for some common operational RPCs and commands. To use Junos PyEZ’s predefined Tables and Views in your Python 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:

content_copy zoom_out_map
from jnpr.junos import Device
from jnpr.junos.op.ethport import EthPortTable

After you import the Table and View definitions, you can use them as described in Use Tables and Views. The following example retrieves the data for the RPC defined in the Table and then prints the interface name and operational status.

content_copy zoom_out_map
from jnpr.junos import Device
from jnpr.junos.op.ethport import EthPortTable

with Device(host='router1.example.net') as dev:
    eth = EthPortTable(dev)
    eth.get()
        
    for item in eth:
        print ("{}: {}".format(item.name, item.oper))

For more information about Junos PyEZ’s predefined Tables and Views, see Predefined Junos PyEZ Operational Tables (Structured Output).

Load Inline Tables and Views

To create, load, and use custom inline Tables and Views in your Junos PyEZ application:

  1. Import the following classes and libraries in your module:
    content_copy zoom_out_map
    from jnpr.junos import Device
    from jnpr.junos.factory.factory_loader import FactoryLoader
    import yaml
    
  2. Define one or more Tables and Views in YAML as a multiline string.
    content_copy zoom_out_map
    myYAML = """
    ---
    UserTable:
      get: system/login/user
      view: UserView
    UserView:
      fields:
        username: name
        userclass: class
    """
    
  3. Load the Table and View definitions by including the following statement, where string-name is the identifier for the multiline string that contains the Table/View definition:
    content_copy zoom_out_map
    globals().update(FactoryLoader().load(yaml.load(string-name, Loader=yaml.FullLoader)))
    
  4. Connect to the device and use the Table to retrieve information, configure the device, or both, depending on the type of Table, for example:
    content_copy zoom_out_map
    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.username, account.userclass))
    

After the Table and View definitions are loaded, there is no difference in how you use inline or external Tables in your module. For additional information, see Use Tables and Views.

Import External Tables and Views

External Table and View definitions are placed in files that are external to your Junos PyEZ application. To create external custom Tables and Views and import them into your Junos PyEZ application:

  1. Define one or more Tables and Views in YAML, and save them to a file that has a .yml extension.
    content_copy zoom_out_map
    [user@server]$ cat myTables/ConfigTables.yml
    ---
    UserTable:
      get: system/login/user
      view: UserView
      
    UserView:
      fields:
        username: name
        userclass: class
    
    ExtendedUserTable:
      get: system/login/user
      view: ExtendedUserView
    
    ExtendedUserView:
      fields:
        username: name
        userclass: class
        userid: uid
  2. Create a file that has the same base name as your Table file but uses a .py extension, and include the following four lines of code in the file.
    content_copy zoom_out_map
    [user@server]$ cat myTables/ConfigTables.py
    from jnpr.junos.factory import loadyaml
    from os.path import splitext
    _YAML_ = splitext(__file__)[0] + '.yml'
    globals().update(loadyaml(_YAML_))
    
  3. If the .yml and .py files are located in a subdirectory, include an __init__.py file in that subdirectory, so that Python checks this directory when it processes the Table import statements in your application.
    content_copy zoom_out_map
    [user@server]$  ls myTables 
    __init__.py  ConfigTables.py  ConfigTables.yml
  4. In the Junos PyEZ application, import the Device class and any required Tables.
    content_copy zoom_out_map
    from jnpr.junos import Device
    from myTables.ConfigTables import UserTable
    
  5. Connect to the device and use the Table to retrieve information, configure the device, or both, depending on the type of Table, for example:
    content_copy zoom_out_map
    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.username, account.userclass))
    

After the Table and View definitions are loaded, there is no difference in how you use inline or external Tables in your module. For additional information, see Use Tables and Views.

Use Tables and Views

After you load or import the Table and View definitions, you can use the predefined, custom inline, or custom external Tables in the same manner.

To use a Table:

  1. Create the Device instance and open a connection to the target device:
    content_copy zoom_out_map
    with Device(host='router.example.com') as dev:
    
  2. Create the Table instance and associate it with the Device instance.
    content_copy zoom_out_map
        users = UserTable(dev)
    
  3. Use the Table to retrieve information, configure the device, or both, depending on the type of Table.
    content_copy zoom_out_map
        users.get()
    
  4. Iterate over and manipulate the resulting object to extract the required information.
    content_copy zoom_out_map
        for account in users:
            print("Username is {}\nUser class is {}".format(account.username, account.userclass))
    

The following example imports a custom external Table, UserTable. The application connects to the device and calls the Tables’s get() method to retrieve user objects from the [edit system login] hierarchy level. The application then prints each username and its corresponding login class.

content_copy zoom_out_map
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.username, account.userclass))

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

footer-navigation