COSC Learning Lab

03_interface_configuration_update.py

Table of Contents

Documentation


In [1]:
help('learning_lab.03_interface_configuration_update')


Help on module learning_lab.03_interface_configuration_update in learning_lab:

NAME
    learning_lab.03_interface_configuration_update - Sample usage of function 'interface_configuration_update'.

DESCRIPTION
    Print the function's documentation then invoke the function.
    Use any one interface on any one device that is connected.
    Verify that the configuration has changed.
    Restore the configuration.

FUNCTIONS
    demonstrate(device_name, interface_config)
    
    main()

DATA
    config = {'network_device': {'xrvr-1': {'address': '172.16.1.76', 'pas...
    temp_address = '101.101.101.101'
    temp_description = 'Mutable'
    temp_netmask = '255.255.0.0'

FILE
    /home/virl/git/cosc-learning-labs/src/learning_lab/03_interface_configuration_update.py


Implementation


In [2]:
from importlib import import_module
script = import_module('learning_lab.03_interface_configuration_update')
from inspect import getsource
print(getsource(script.main))


def main():
    print(plain(doc(interface_configuration_update)))
    for device_name in inventory_connected():
        mgmt_name = management_interface(device_name)
        for interface_name in interface_names(device_name):
            # Avoid modifying the management interface.
            if interface_name == mgmt_name:
                continue
            interface_config = interface_configuration(device_name, interface_name)
            if not interface_config.address:
                continue
            demonstrate(device_name, interface_config)
            return
    print("There are no suitable network devices. Demonstration cancelled.")


In [ ]:
print(getsource(script.demonstrate))


def demonstrate(device_name, interface_config):
    print("Initial configuration:")
    initial = interface_config
    print(initial)
    
    try:
        print()
        print("Modify configuration:")
        print('interface_configuration_update(' + device_name, initial.name, temp_description, temp_address, temp_netmask, not initial.shutdown, sep=', ', end=')\n')
        interface_configuration_update(device_name, initial.name, description=temp_description,
                               address=temp_address, netmask=temp_netmask, shutdown=not initial.shutdown)
        print()
        print("Modified configuration:")
        modified = interface_configuration(device_name, initial.name)
        print(modified)
        assert modified.name == initial.name
        assert modified.description != initial.description
        assert modified.address != initial.address
        assert modified.netmask != initial.netmask
        assert modified.shutdown != initial.shutdown
        
    finally:
        print()
        print("Restore configuration:")
        print('interface_configuration_update(' + device_name, initial.name, initial.description, initial.address, initial.netmask, initial.shutdown, sep=', ', end=')\n')
        interface_configuration_update(device_name, initial.name, description=initial.description, address=initial.address, netmask=initial.netmask, shutdown=initial.shutdown)
    
        print()
        print("Restored configuration:")
        restored = interface_configuration(device_name, initial.name)
        print(restored)
        assert restored.name == initial.name
        assert restored.description == initial.description, ("got " % initial.description)
        assert restored.address == initial.address
        assert restored.netmask == initial.netmask
        assert restored.shutdown == initial.shutdown

Execution


In [ ]:
run ../learning_lab/03_interface_configuration_update.py

HTTP


In [ ]:
from basics.odl_http import http_history
from basics.http import http_history_to_html
from IPython.core.display import HTML
HTML(http_history_to_html(http_history()))