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

Commit

Permalink
feat: Added a comparison for snapshot intents
Browse files Browse the repository at this point in the history
  • Loading branch information
jjeff07 committed Dec 3, 2021
1 parent c606802 commit b8028d5
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 6 deletions.
19 changes: 18 additions & 1 deletion examples/intent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"""
from pprint import pprint

from tabulate import tabulate

from ipfabric import IPFClient

if __name__ == '__main__':
Expand Down Expand Up @@ -71,4 +73,19 @@
'routeBased': True, 'selectorLocalAddress': '10.71.200.1/32', 'selectorLocalPort': None, 'selectorProtocol': None,
'selectorRemoteAddress': '10.64.200.1/32', 'selectorRemotePort': None, 'siteKey': '885963247', 'siteName': 'L71',
'sn': 'FOSVM1QWZRUM4EB7', 'status': {'data': 'up', 'severity': 0}, 'tunnelIntName': 'ipsec_L64_mtik'}
"""
"""
print()

compare = ipf.intent.compare_snapshot('$lastLocked')
print(tabulate(compare, headers=['Intent Name', 'Check', 'Current', 'Other', 'Difference']))
"""
Intent Name Check Current Other Difference
-------------------------------------------- ------- --------- ------- ------------
CDP/LLDP unidirectional total 21 25 4
CDP/LLDP unidirectional blue 21 25 4
BGP Session Age total 344 367 23
BGP Session Age green 254 309 55
BGP Session Age blue 56 22 -34
BGP Session Age amber 0 3 3
BGP Session Age red 34 33 -1
"""
18 changes: 15 additions & 3 deletions ipfabric/intent.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from typing import Any
import logging
from typing import Any
from typing import Union
Expand All @@ -15,14 +14,16 @@ def __init__(self, client):
self.intent_checks: list = self.get_intent_checks()
self.groups: list = self.get_groups()

def get_intent_checks(self):
def get_intent_checks(self, snapshot_id: str = None):
"""
Gets all intent checks and returns a list of them. You can also:
ipf.intent() # Loads the intents to intent_checks
print(len(ipf.intent.intent_checks))
:param snapshot_id: str: Optional snapshot ID to get different data
:return: list: List of intent checks
"""
res = self.client.get('reports', params=dict(snapshot=self.client.snapshot_id))
snapshot_id = self.client.snapshots[snapshot_id].snapshot_id if snapshot_id else self.client.snapshot_id
res = self.client.get('reports', params=dict(snapshot=snapshot_id))
res.raise_for_status()
return [IntentCheck(**check) for check in res.json()]

Expand Down Expand Up @@ -60,3 +61,14 @@ def get_results(self, intent: IntentCheck, color: Union[str, int], snapshot_id:
color = dict(green=0, blue=10, amber=20, red=30)[color]
return self.client.fetch_all(intent.api_endpoint, snapshot_id=snapshot_id, reports=intent.web_endpoint,
filters={intent.column: ['color', 'eq', color]})

def compare_snapshot(self, snapshot_id: str = None):
new_intents = {i.name: i for i in self.get_intent_checks(snapshot_id)}
comparison = list()
for name, intent in new_intents.items():
old = self.intent_by_name[name].result
compare = old.compare(intent.result)
for desc, value in compare.items():
n = desc if desc != 'count' else 'total'
comparison.append((name, n, *value))
return comparison
21 changes: 20 additions & 1 deletion ipfabric/intent_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,26 @@ class Description(BaseModel):

class Result(BaseModel):
count: Union[int, None]
checks: Union[Checks, None]
checks: Checks = Field(default_factory=Checks)

def compare(self, other):

old = self.checks
new = other.checks
data = dict()
if self.count is not None or other.count is not None:
data['count'] = (self.count or 0, other.count or 0, (other.count or 0) - (self.count or 0))

for value in ['green', 'blue', 'amber', 'red']:
if getattr(old, value) is not None and getattr(new, value) is not None:
o = self.get_value(old, value)
n = self.get_value(new, value)
data[value] = (o, n, (n - o))
return data

@staticmethod
def get_value(data: Checks, value: str):
return int(getattr(data, value) if data else 0)


class Child(BaseModel):
Expand Down
17 changes: 16 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pytest = "^6.2.5"
pytest-cov = "^3.0.0"
flake8 = "^4.0.1"
python-semantic-release = "^7.23.0"
tabulate = "^0.8.9"


[build-system]
Expand Down

0 comments on commit b8028d5

Please sign in to comment.