Skip to content

Commit 0c5e593

Browse files
authored
Merge pull request #953 from netenglabs/add-section-to-devconfig
Add section to devconfig
2 parents bbb35ca + 4aa1c81 commit 0c5e593

File tree

10 files changed

+1101
-390
lines changed

10 files changed

+1101
-390
lines changed

suzieq/cli/sqcmds/DevconfigCmd.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
from nubia import command
1+
from nubia import command, argument
22

33
from suzieq.cli.sqcmds.command import SqTableCommand
44
from suzieq.sqobjects.devconfig import DevconfigObj
55

66

77
@command("devconfig", help="Act on device data")
8+
@argument("section",
9+
description="show device config only for this regex match")
810
class DevconfigCmd(SqTableCommand):
911
"""Device configurations"""
1012

@@ -19,6 +21,7 @@ def __init__(
1921
format: str = "", # pylint: disable=redefined-builtin
2022
query_str: str = " ",
2123
columns: str = "default",
24+
section: str = '',
2225
) -> None:
2326
super().__init__(
2427
engine=engine,
@@ -33,10 +36,12 @@ def __init__(
3336
sqobj=DevconfigObj,
3437
)
3538

39+
self.lvars['section'] = section
40+
3641
@command("show", help="Show device information")
3742
def show(self):
3843
"""Show device config info
3944
"""
4045
if not self.format or (self.format == 'text'):
41-
self.format = 'devconfig'
46+
self.format = 'markdown'
4247
return super().show()

suzieq/config/schema/devconfig.avsc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
{
1515
"name": "config",
1616
"type": "string",
17-
"display": 1,
17+
"display": 2,
1818
"description": "The running config"
1919
},
2020
{
@@ -42,7 +42,7 @@
4242
{
4343
"name": "timestamp",
4444
"type": "timestamp",
45-
"display": 2,
45+
"display": 3,
4646
"description": "Unix epach When this record was created, in ms"
4747
},
4848
{

suzieq/engines/pandas/devconfig.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
from typing import List
2+
3+
from ciscoconfparse import CiscoConfParse
4+
15
from suzieq.engines.pandas.engineobj import SqPandasEngine
26

37

@@ -8,3 +12,52 @@ class DevconfigObj(SqPandasEngine):
812
def table_name():
913
'''Table name'''
1014
return 'devconfig'
15+
16+
def get(self, **kwargs):
17+
'''Retrieve the devconfig table info'''
18+
19+
section = kwargs.pop('section', None)
20+
columns = kwargs.pop('columns', ['default'])
21+
query_str = kwargs.pop('query_str', '')
22+
23+
df = super().get(columns=columns, **kwargs)
24+
if df.empty or 'error' in df.columns:
25+
return df
26+
27+
if not section:
28+
if query_str:
29+
df = df.query(query_str).reset_index(drop=True)
30+
return df
31+
32+
devdf = self._get_table_sqobj('device') \
33+
.get(columns=['namespace', 'hostname', 'os'], **kwargs)
34+
35+
if devdf.empty or 'error' in devdf.columns:
36+
return df
37+
38+
drop_indices: List[int] = []
39+
for index, row in enumerate(df.itertuples()):
40+
os = devdf.loc[(devdf['hostname'] == row.hostname) &
41+
(devdf['namespace'] == row.namespace),
42+
'os'].values[0]
43+
if os.startswith('junos') or os == 'panos':
44+
os = 'junos'
45+
elif os == 'nxos':
46+
os = 'nxos'
47+
else:
48+
os = 'ios'
49+
50+
parsed_conf = CiscoConfParse(row.config.split('\n'), syntax=os)
51+
conf = '\n'.join(parsed_conf.find_all_children(section))
52+
if not conf:
53+
drop_indices.append(index)
54+
else:
55+
df.loc[index, 'config'] = conf
56+
57+
if drop_indices:
58+
df = df.drop(index=drop_indices)
59+
60+
if query_str:
61+
df = df.query(query_str).reset_index(drop=True)
62+
63+
return df

suzieq/restServer/query.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ def query_devconfig(verb: CommonVerbs, request: Request,
381381
view: ViewValues = "latest",
382382
namespace: List[str] = Query(None),
383383
columns: List[str] = Query(default=["default"]),
384+
section: str = Query(None),
384385
query_str: str = None,
385386
what: str = None,
386387
count: str = None, reverse: str = None,

suzieq/sqobjects/devconfig.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class DevconfigObj(SqObject):
88
def __init__(self, **kwargs):
99
super().__init__(table='devconfig', **kwargs)
1010
self._valid_get_args = ['namespace', 'hostname', 'columns',
11-
'query_str']
11+
'query_str', 'section']
1212

1313
def unique(self, **kwargs) -> pd.DataFrame:
1414
return pd.DataFrame(

tests/integration/sqcmds/common-samples/describe.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,14 @@ tests:
138138
marks: devconfig describe
139139
output: '[{"name": "active", "type": "boolean", "key": "", "display": "", "description":
140140
"If this entry is active or deleted"}, {"name": "config", "type": "string", "key":
141-
"", "display": 1, "description": "The running config"}, {"name": "deviceSession",
141+
"", "display": 2, "description": "The running config"}, {"name": "deviceSession",
142142
"type": "timestamp", "key": "", "display": "", "description": "Device boot session
143143
id"}, {"name": "hostname", "type": "string", "key": 1, "display": 1, "description":
144144
"Hostname associated with this record"}, {"name": "namespace", "type": "string",
145145
"key": 0, "display": 0, "description": "Namespace associated with this record"},
146146
{"name": "sqvers", "type": "string", "key": "", "display": "", "description":
147147
"Schema version, not selectable"}, {"name": "timestamp", "type": "timestamp",
148-
"key": "", "display": 2, "description": "Unix epach When this record was created,
148+
"key": "", "display": 3, "description": "Unix epach When this record was created,
149149
in ms"}]'
150150
- command: device describe --format=json
151151
data-directory: tests/data/parquet

0 commit comments

Comments
 (0)