| 1 |
from jnpr.junos.exception import RpcError |
| 2 |
|
| 3 |
|
| 4 |
def provides_facts(): |
| 5 |
""" |
| 6 |
Returns a dictionary keyed on the facts provided by this module. The value |
| 7 |
of each key is the doc string describing the fact. |
| 8 |
""" |
| 9 |
return { |
| 10 |
"vc_capable": "A boolean indicating if the device is currently " |
| 11 |
"configured in a virtual chassis. In spite of the " |
| 12 |
"name, this fact does NOT indicate whether or not " |
| 13 |
"the device is CAPABLE of joining a VC.", |
| 14 |
"vc_mode": "A string indicating the current virtual chassis " |
| 15 |
"mode of the device.", |
| 16 |
"vc_fabric": "A boolean indicating if the device is currently in " |
| 17 |
"fabric mode.", |
| 18 |
"vc_master": "A string indicating the chassis/node which is " |
| 19 |
"currently the master of the VC.", |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
def get_facts(device): |
| 24 |
""" |
| 25 |
Gathers facts from the <get-virtual-chassis-information/> RPC. |
| 26 |
""" |
| 27 |
vc_capable = None |
| 28 |
vc_mode = None |
| 29 |
vc_fabric = None |
| 30 |
vc_master = None |
| 31 |
|
| 32 |
try: |
| 33 |
rsp = device.rpc.get_virtual_chassis_information(normalize=True) |
| 34 |
# MX issue where command returns, but without content. In this case, |
| 35 |
# rsp is set to True. |
| 36 |
if rsp is not True: |
| 37 |
vc_capable = True |
| 38 |
if rsp is not None: |
| 39 |
vc_mode = rsp.findtext(".//virtual-chassis-mode") |
| 40 |
vc_id_info = rsp.find(".//virtual-chassis-id-information") |
| 41 |
if vc_id_info is not None: |
| 42 |
if vc_id_info.get("style") == "fabric": |
| 43 |
vc_fabric = True |
| 44 |
else: |
| 45 |
vc_fabric = False |
| 46 |
for member_id in rsp.xpath( |
| 47 |
".//member-role[starts-with(.,'Master')]" |
| 48 |
"/preceding-sibling::member-id" |
| 49 |
): |
| 50 |
if vc_master is None: |
| 51 |
vc_master = member_id.text |
| 52 |
else: |
| 53 |
old_vc_master = vc_master |
| 54 |
vc_master = None |
| 55 |
raise ValueError( |
| 56 |
"Member %s and member %s both claim " |
| 57 |
"to be master of the VC." % (old_vc_master, member_id.text) |
| 58 |
) |
| 59 |
else: |
| 60 |
vc_capable = False |
| 61 |
except RpcError: |
| 62 |
# Likely a device that doesn't implement the |
| 63 |
# <get-virtual-chassis-information/> RPC. |
| 64 |
# That's OK. Set vc_capable = False. |
| 65 |
vc_capable = False |
| 66 |
|
| 67 |
return { |
| 68 |
"vc_capable": vc_capable, |
| 69 |
"vc_mode": vc_mode, |
| 70 |
"vc_fabric": vc_fabric, |
| 71 |
"vc_master": vc_master, |
| 72 |
} |