Skip to content

Commit

Permalink
Added Context Manager documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
dbarrosop committed Feb 8, 2016
1 parent f8ac4fc commit 5aa0b0f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
18 changes: 18 additions & 0 deletions docs/tutorials/context_manager.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Context Manager
===============

In the previous tutorial we used the methods ``open()`` to connect to the device and ``close()`` to disconnect.
Using those methods are useful if you want to do complex or asynchronous code. However, for most situations you should
try to stick with the context manager. It handles opening and closing the session automatically and it's the
pythonic way:

.. code-block:: python
>>> from napalm import get_network_driver
>>> driver = get_network_driver('eos')
>>> with driver('localhost', 'vagrant', 'vagrant', optional_args={'port': 12443}) as device:
... print device.get_facts()
... print device.get_interfaces_counters()
...
{'os_version': u'4.15.2.1F-2759627.41521F', 'uptime': 2010, 'interface_list': [u'Ethernet1', u'Ethernet2', u'Management1'], 'vendor': u'Arista', 'serial_number': u'', 'model': u'vEOS', 'hostname': u'NEWHOSTNAME', 'fqdn': u'NEWHOSTNAME'}
{u'Ethernet2': {'tx_multicast_packets': 1028, 'tx_discards': 0, 'tx_octets': 130744, 'tx_errors': 0, 'rx_octets': 0, 'tx_unicast_packets': 0, 'rx_errors': 0, 'tx_broadcast_packets': 0, 'rx_multicast_packets': 0, 'rx_broadcast_packets': 0, 'rx_discards': 0, 'rx_unicast_packets': 0}, u'Management1': {'tx_multicast_packets': 0, 'tx_discards': 0, 'tx_octets': 99664, 'tx_errors': 0, 'rx_octets': 105000, 'tx_unicast_packets': 773, 'rx_errors': 0, 'tx_broadcast_packets': 0, 'rx_multicast_packets': 0, 'rx_broadcast_packets': 0, 'rx_discards': 0, 'rx_unicast_packets': 0}, u'Ethernet1': {'tx_multicast_packets': 1027, 'tx_discards': 0, 'tx_octets': 130077, 'tx_errors': 0, 'rx_octets': 0, 'tx_unicast_packets': 0, 'rx_errors': 0, 'tx_broadcast_packets': 0, 'rx_multicast_packets': 0, 'rx_broadcast_packets': 0, 'rx_discards': 0, 'rx_unicast_packets': 0}}
8 changes: 7 additions & 1 deletion docs/tutorials/first_steps_config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Use the appropriate network driver to connect to the device::
>>> device = driver('192.168.76.10', 'dbarroso', 'this_is_not_a_secure_password')
>>> device.open()

Configurations can be replaced entirely or merged into the existing device config.
Configurations can be replaced entirely or merged into the existing device config.
You can load configuration either from a string or from a file.

Replacing the Configuration
Expand Down Expand Up @@ -79,3 +79,9 @@ If for some reason you committed the changes and you want to rollback::

>>> device.rollback()

Disconnecting
-------------

To close the session with the device just do::

>>> device.close()
1 change: 1 addition & 0 deletions docs/tutorials/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ Tutorials
:maxdepth: 1

first_steps_config
context_manager

0 comments on commit 5aa0b0f

Please sign in to comment.