Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

Commit

Permalink
fix: device configs
Browse files Browse the repository at this point in the history
  • Loading branch information
jjeff07 committed Feb 18, 2022
1 parent d35ad5f commit a3b2d66
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
17 changes: 16 additions & 1 deletion examples/tools/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# ipf = IPFClient('https://demo3.ipfabric.io/', token='token', verify=False, timeout=15)
cfg = DeviceConfigs(ipf)

# cfgs = cfg.get_all_configurations() # Get all Configurations
cfgs = cfg.get_all_configurations() # Get all Configurations
# Returns dictionary like {SN: [Config, Config]} where SN is the IPF Unique Serial Number
L35AC92_cfgs = cfg.get_all_configurations(device='L35AC92') # Get all Configurations for device
# Returns dictionary like {'a23ffbf': [Config, Config], '91624130': [Config, Config]}
Expand All @@ -26,6 +26,21 @@
"""
print()

text = cfg.get_text_config(L35AC92_cfgs['a23ffbf'][0], False).text
print(text)
"""
version 15.2
service timestamps debug datetime msec
service timestamps log datetime msec
service password-encryption
service compress-config
!
hostname L35AC92
!
boot-start-marker
"""
print()

L35AC92_sn_cfgs = cfg.get_all_configurations(sn='a23ffbf')
# Returns dictionary like {'a23ffbf': [Config, Config]}
print(L35AC92_sn_cfgs)
Expand Down
39 changes: 22 additions & 17 deletions ipfabric/tools/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,12 @@ def _search_ip(self, ip: str, snapshot_id: str = None, log: bool = False) -> dic
logger.warning(f"Could not find a matching IP for '{ip}'.")
return {"hostname": None, "sn": None}

def get_configuration(self, device: str, sanitized: bool = True, date: Union[str, tuple] = "$last"):
def get_configuration(self, device: str = None, sn: str = None, sanitized: bool = True,
date: Union[str, tuple] = "$last"):
"""
Gets last configuration of a device based on hostname or IP
Gets last configuration of a device based on hostname or IP or IPF Unique Serial Number
:param device: str: Hostname or IP
:param sn: str: Serial Number
:param sanitized: bool: Default True to mask passwords
:param date: Union[str, tuple]: Defaults to latest config. Values in [$last, $prev, $first] or can be a
tuple of a date range to get the latest snapshot in that range.
Expand All @@ -106,25 +108,28 @@ def get_configuration(self, device: str, sanitized: bool = True, date: Union[str
"""
if not isinstance(date, tuple) and date not in ["$last", "$prev", "$first"]:
raise SyntaxError("Date must be in [$last, $prev, $first] or tuple ('startDate', 'endDate')")
sn = self._validate_device(device)["sn"]
if not sn:
return None
sn = self._validate_device(device)["sn"]
if not sn:
return None
cfgs = self.get_all_configurations(sn=sn)
if not cfgs:
return None
if device:
cfg = self._get_hash(cfgs[sn], date)
if cfg:
res = self.ipf.get(
"/tables/management/configuration/download",
params=dict(hash=cfg.config_hash, sanitized=sanitized),
)
res.raise_for_status()
cfg.text = res.text
return cfg
else:
logger.error(f"Could not find a configuration with date {date}")
return None
cfg = self._get_hash(cfgs[sn], date)
if cfg:
return self.get_text_config(cfg, sanitized)
else:
logger.error(f"Could not find a configuration with date {date}")
return None

def get_text_config(self, cfg: Config, sanitized: bool = True):
res = self.ipf.get(
"/tables/management/configuration/download",
params=dict(hash=cfg.config_hash, sanitized=sanitized),
)
res.raise_for_status()
cfg.text = res.text
return cfg

@staticmethod
def _get_hash(configs, date):
Expand Down

0 comments on commit a3b2d66

Please sign in to comment.