| 1 |
# Import any exceptions raised in this module. |
| 2 |
from jnpr.junos.exception import RpcError |
| 3 |
|
| 4 |
|
| 5 |
# In general, a "fact" should be a piece of information that does not change |
| 6 |
# over the life of a PyEZ connection. While things like mastership state can |
| 7 |
# change, they also result in the PyEZ connection being closed, so it can |
| 8 |
# be considered a fact. Things like number of routes in the route table or |
| 9 |
# system uptime do not maintain the same value over the life of a PyEZ |
| 10 |
# connection and should therefore not be considered facts. |
| 11 |
|
| 12 |
# The name of this file should be based on the RPCs which are invoked by this |
| 13 |
# file. This avoids accidentally invoking the same RPC multiple times when we |
| 14 |
# could invoke it just once and gather multiple facts from the output. |
| 15 |
|
| 16 |
# An import for each fact file must be present in |
| 17 |
# lib/jnpr/junos/facts/__init__.py |
| 18 |
|
| 19 |
|
| 20 |
# The file must include a provide_facts() function |
| 21 |
# The provide_facts() function must return a dictionary. The keys of the |
| 22 |
# dictionary are each fact that is handled/returned by this module. The value |
| 23 |
# of each item in the dictionary is the documentation string describing the |
| 24 |
# particular fact. |
| 25 |
def provides_facts(): |
| 26 |
""" |
| 27 |
Returns a dictionary keyed on the facts provided by this module. The value |
| 28 |
of each key is the doc string describing the fact. |
| 29 |
""" |
| 30 |
return { |
| 31 |
"foo": "The foo information.", |
| 32 |
"bar": "The bar information.", |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
# The file must include a get_facts(device) function. The get_facts(device) |
| 37 |
# function takes a single mandatory device argument which is the Device object |
| 38 |
# on which the fact is discovered. |
| 39 |
# The get_facts(device) function must return a dictionary with a key for each |
| 40 |
# fact that is handled/returned by this module. |
| 41 |
# The get_facts(device) function should raise an appropriate exception if the |
| 42 |
# response from the device prevents getting value(s) for ALL of the facts |
| 43 |
# provided by the module. The example gets the 'foo' and 'bar' facts. |
| 44 |
# An exception should only be raised if the error prevents getting the value |
| 45 |
# for the 'foo' fact AND prevents getting the value for the 'bar' fact. If |
| 46 |
# unable to determine the value for a given fact, then set the value for that |
| 47 |
# fact to None. |
| 48 |
def get_facts(device): |
| 49 |
""" |
| 50 |
Gathers facts from the <get-foo-bar-information/> RPC. |
| 51 |
""" |
| 52 |
# Invoke an RPC on device using device.rpc.rpc_name() methods. |
| 53 |
# Avoid using the cli() or shell() methods. |
| 54 |
# Avoid any RPC which requires more than view privileges. |
| 55 |
# Avoid any RPC which doesn't run on non-master REs. |
| 56 |
# Avoid any RPC which takes more than a couple of seconds to execute. |
| 57 |
# Avoid any RPC which MIGHT take longer than the default PyEZ RPC timeout |
| 58 |
# (currently 30 seconds). |
| 59 |
# If there are different ways of gathering the info depending on |
| 60 |
# Junos version, platform, model, or other things, then try the most common |
| 61 |
# way first, catch exceptions, try the second most common, etc. |
| 62 |
# Note that executing an RPC might itself raise an exception. There is no |
| 63 |
# need to catch that exception. It is handled within the FactCache() class. |
| 64 |
# Always pass the normalize=True argument when invoking an RPC to avoid any |
| 65 |
# potential white-space problems in the RPC response. |
| 66 |
rsp = device.rpc.get_foo_bar_information(normalize=True) |
| 67 |
|
| 68 |
# Handle any exceptional situations unique to this RPC which prevent |
| 69 |
# getting values for ALL of the facts provided by this module. |
| 70 |
# These are examples which MAY or MAY NOT apply to your particular RPC. |
| 71 |
# Don't just blindly leave these in. |
| 72 |
if rsp.tag == "error": |
| 73 |
raise RpcError() |
| 74 |
|
| 75 |
# An example of a boolean fact. False if the top-level tag is not 'foo'. |
| 76 |
foo = False |
| 77 |
if rsp.tag == "foo": |
| 78 |
foo = True |
| 79 |
|
| 80 |
# An example of a string value which might be found at various levels or |
| 81 |
# locations within the response hierarchy. |
| 82 |
bar = ( |
| 83 |
rsp.findtext(".//chassis[1]/bar") |
| 84 |
or rsp.findtext('.//chassis-module[name="Backplane"]/bar') |
| 85 |
or rsp.findtext('.//chassis-module[name="Midplane"]/bar') |
| 86 |
) |
| 87 |
|
| 88 |
return { |
| 89 |
"foo": foo, |
| 90 |
"bar": bar, |
| 91 |
} |