convert nxos_facts map from dict to frozenset

* key maps are now frozenset instead of dict objects
* FactsBase now includes utility functions for transforming json data structures

Tested on NXOS 7.3(0)D1(1)
This commit is contained in:
Peter Sprygada 2016-08-27 08:47:42 -04:00 committed by Matt Clay
parent 964b7ecffa
commit 66b0a1ef2d

View file

@ -193,25 +193,34 @@ class FactsBase(object):
self.facts = dict() self.facts = dict()
self.commands() self.commands()
def transform_dict(self, data, keymap):
transform = dict()
for key, fact in keymap:
if key in data:
transform[fact] = data[key]
return transform
def transform_iterable(self, iterable, keymap):
for item in iterable:
yield self.transform_dict(item, keymap)
class Default(FactsBase): class Default(FactsBase):
SYSTEM_MAP = { VERSION_MAP = frozenset([
'sys_ver_str': 'version', ('sys_ver_str', 'version'),
'proc_board_id': 'serialnum', ('proc_board_id', 'serialnum'),
'chassis_id': 'model', ('chassis_id', 'model'),
'isan_file_name': 'image', ('isan_file_name', 'image'),
'host_name': 'hostname' ('host_name', 'hostname')
} ])
def commands(self): def commands(self):
add_command(self.runner, 'show version', output='json') add_command(self.runner, 'show version', output='json')
def populate(self): def populate(self):
data = self.runner.get_command('show version', output='json') data = self.runner.get_command('show version', output='json')
for key, value in self.SYSTEM_MAP.iteritems(): self.facts.update(self.transform_dict(data, self.VERSION_MAP))
if key in data:
self.facts[value] = data[key]
class Config(FactsBase): class Config(FactsBase):
@ -242,29 +251,27 @@ class Hardware(FactsBase):
class Interfaces(FactsBase): class Interfaces(FactsBase):
INTERFACE_MAP = { INTERFACE_MAP = frozenset([
'state': 'state', ('state', 'state'),
'desc': 'description', ('desc', 'description'),
'eth_bw': 'bandwidth', ('eth_bw', 'bandwidth'),
'eth_duplex': 'duplex', ('eth_duplex','duplex'),
'eth_speed': 'speed', ('eth_speed', 'speed'),
'eth_mode': 'mode', ('eth_mode', 'mode'),
'eth_ip_addr': 'address', ('eth_hw_addr', 'macaddress'),
'eth_ip_mask': 'masklen', ('eth_mtu', 'mtu'),
'eth_hw_addr': 'macaddress', ('eth_hw_desc', 'type')
'eth_mtu': 'mtu', ])
'eth_hw_desc': 'type'
}
INTERFACE_IPV4_MAP = { INTERFACE_IPV4_MAP = frozenset([
'eth_ip_addr': 'address', ('eth_ip_addr', 'address'),
'eth_ip_mask': 'masklen' ('eth_ip_mask', 'masklen')
} ])
INTERFACE_IPV6_MAP = { INTERFACE_IPV6_MAP = frozenset([
'addr': 'address', ('addr', 'address'),
'prefix': 'subnet' ('prefix', 'subnet')
} ])
def commands(self): def commands(self):
add_command(self.runner, 'show interface', output='json') add_command(self.runner, 'show interface', output='json')
@ -302,18 +309,13 @@ class Interfaces(FactsBase):
def populate_interfaces(self, data): def populate_interfaces(self, data):
interfaces = dict() interfaces = dict()
for item in data['TABLE_interface']['ROW_interface']: for item in data['TABLE_interface']['ROW_interface']:
intf = dict()
name = item['interface'] name = item['interface']
for key, value in self.INTERFACE_MAP.iteritems(): intf = dict()
if key in item: intf.update(self.transform_dict(item, self.INTERFACE_MAP))
intf[value] = item[key]
if 'eth_ip_addr' in item: if 'eth_ip_addr' in item:
intf['ipv4'] = dict() intf['ipv4'] = self.transform_dict(item, self.INTERFACE_IPV4_MAP)
for key, value in self.INTERFACE_IPV4_MAP.iteritems():
if key in item:
intf['ipv4'][value] = item[key]
self.facts['all_ipv4_addresses'].append(item['eth_ip_addr']) self.facts['all_ipv4_addresses'].append(item['eth_ip_addr'])
interfaces[name] = intf interfaces[name] = intf
@ -338,23 +340,47 @@ class Interfaces(FactsBase):
def parse_ipv6_interfaces(self, data): def parse_ipv6_interfaces(self, data):
data = data['TABLE_intf'] data = data['TABLE_intf']
if isinstance(data, dict): if isinstance(data, dict):
data = [data] data = [data]
for item in data: for item in data:
name = item['ROW_intf']['intf-name'] name = item['ROW_intf']['intf-name']
intf = self.facts['interfaces'][name] intf = self.facts['interfaces'][name]
intf['ipv6'] = self.transform_dict(item, self.INTERFACE_IPV6_MAP)
intf['ipv6'] = dict()
for key, value in self.INTERFACE_IPV6_MAP.iteritems():
intf['ipv6'][value] = item['ROW_intf'][key]
self.facts['all_ipv6_addresses'].append(item['ROW_intf']['addr']) self.facts['all_ipv6_addresses'].append(item['ROW_intf']['addr'])
class Legacy(FactsBase): class Legacy(FactsBase):
# facts from nxos_facts 2.1 # facts from nxos_facts 2.1
VERSION_MAP = frozenset([
('host_name', '_hostname'),
('kickstart_ver_str', '_os'),
('chassis_id', '_platform')
])
MODULE_MAP = frozenset([
('model', 'model'),
('modtype', 'type'),
('ports', 'ports'),
('status', 'status')
])
FAN_MAP = frozenset([
('fanname', 'name'),
('fanmodel', 'model'),
('fanhwver', 'hw_ver'),
('fandir', 'direction'),
('fanstatus', 'status')
])
POWERSUP_MAP = frozenset([
('psmodel', 'model'),
('psnum', 'number'),
('ps_status', 'status'),
('actual_out', 'actual_output'),
('actual_in', 'actual_in'),
('total_capa', 'total_capacity')
])
def commands(self): def commands(self):
add_command(self.runner, 'show version', output='json') add_command(self.runner, 'show version', output='json')
add_command(self.runner, 'show module', output='json') add_command(self.runner, 'show module', output='json')
@ -364,15 +390,7 @@ class Legacy(FactsBase):
def populate(self): def populate(self):
data = self.runner.get_command('show version', 'json') data = self.runner.get_command('show version', 'json')
self.facts['_hostname'] = data['host_name'] self.facts.update(self.transform_dict(data, self.VERSION_MAP))
self.facts['_kickstart'] = data['kickstart_ver_str']
self.facts['_platform'] = data['chassis_id']
if 'rr_sys_ver' in data:
self.facts['_os'] = data['rr_sys_ver']
if 'rr_reason' in data:
self.facts['_rr'] = data['rr_reason']
data = self.runner.get_command('show interface', 'json') data = self.runner.get_command('show interface', 'json')
self.facts['_interfaces_list'] = self.parse_interfaces(data) self.facts['_interfaces_list'] = self.parse_interfaces(data)
@ -404,49 +422,21 @@ class Legacy(FactsBase):
return objects return objects
def parse_module(self, data): def parse_module(self, data):
modules = list() data = data['TABLE_modinfo']['ROW_modinfo']
for item in data['TABLE_modinfo']['ROW_modinfo']: objects = list(self.transform_iterable(data, self.MODULE_MAP))
mod = dict() return objects
mod['model'] = item['model']
mod['type'] = item['modtype']
mod['ports'] = item['ports']
mod['status'] = item['status']
modules.append(mod)
return modules
def parse_fan_info(self, data): def parse_fan_info(self, data):
objects = list() data = data['fandetails']['TABLE_faninfo']['ROW_faninfo']
for item in data['fandetails']['TABLE_faninfo']['ROW_faninfo']: objects = list(self.transform_iterable(data, self.FAN_MAP))
obj = dict()
obj['name'] = item['fanname']
obj['model'] = item['fanmodel']
if 'fanhwver' in item:
obj['hw_ver'] = item['fanhwver']
if 'fandir' in item:
obj['direction'] = item['fandir']
if 'fanstatus' in item:
obj['status'] = item['fanstatus']
objects.append(obj)
return objects return objects
def parse_power_supply_info(self, data): def parse_power_supply_info(self, data):
objects = list() data = data['powersup']['TABLE_psinfo']['ROW_psinfo']
for item in data['powersup']['TABLE_psinfo']['ROW_psinfo']: objects = list(self.transform_iterable(data, self.POWERSUP_MAP))
obj = dict()
obj['model'] = item['psmodel']
obj['number'] = item['psnum']
obj['status'] = item['ps_status']
if 'actual_out' in item:
obj['actual_output'] = item['actual_out']
if 'actual_in' in item:
obj['actual_in'] = item['actual_in']
if 'total_capa' in item:
obj['total_capacity'] = item['total_capa']
objects.append(obj)
return objects return objects
FACT_SUBSETS = dict( FACT_SUBSETS = dict(
default=Default, default=Default,
legacy=Legacy, legacy=Legacy,