Skip to content

Commit

Permalink
Merge pull request #953 from netenglabs/add-section-to-devconfig
Browse files Browse the repository at this point in the history
Add section to devconfig
  • Loading branch information
ddutt authored May 27, 2024
2 parents bbb35ca + 4aa1c81 commit 0c5e593
Show file tree
Hide file tree
Showing 10 changed files with 1,101 additions and 390 deletions.
9 changes: 7 additions & 2 deletions suzieq/cli/sqcmds/DevconfigCmd.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from nubia import command
from nubia import command, argument

from suzieq.cli.sqcmds.command import SqTableCommand
from suzieq.sqobjects.devconfig import DevconfigObj


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

Expand All @@ -19,6 +21,7 @@ def __init__(
format: str = "", # pylint: disable=redefined-builtin
query_str: str = " ",
columns: str = "default",
section: str = '',
) -> None:
super().__init__(
engine=engine,
Expand All @@ -33,10 +36,12 @@ def __init__(
sqobj=DevconfigObj,
)

self.lvars['section'] = section

@command("show", help="Show device information")
def show(self):
"""Show device config info
"""
if not self.format or (self.format == 'text'):
self.format = 'devconfig'
self.format = 'markdown'
return super().show()
4 changes: 2 additions & 2 deletions suzieq/config/schema/devconfig.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
{
"name": "config",
"type": "string",
"display": 1,
"display": 2,
"description": "The running config"
},
{
Expand Down Expand Up @@ -42,7 +42,7 @@
{
"name": "timestamp",
"type": "timestamp",
"display": 2,
"display": 3,
"description": "Unix epach When this record was created, in ms"
},
{
Expand Down
53 changes: 53 additions & 0 deletions suzieq/engines/pandas/devconfig.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from typing import List

from ciscoconfparse import CiscoConfParse

from suzieq.engines.pandas.engineobj import SqPandasEngine


Expand All @@ -8,3 +12,52 @@ class DevconfigObj(SqPandasEngine):
def table_name():
'''Table name'''
return 'devconfig'

def get(self, **kwargs):
'''Retrieve the devconfig table info'''

section = kwargs.pop('section', None)
columns = kwargs.pop('columns', ['default'])
query_str = kwargs.pop('query_str', '')

df = super().get(columns=columns, **kwargs)
if df.empty or 'error' in df.columns:
return df

if not section:
if query_str:
df = df.query(query_str).reset_index(drop=True)
return df

devdf = self._get_table_sqobj('device') \
.get(columns=['namespace', 'hostname', 'os'], **kwargs)

if devdf.empty or 'error' in devdf.columns:
return df

drop_indices: List[int] = []
for index, row in enumerate(df.itertuples()):
os = devdf.loc[(devdf['hostname'] == row.hostname) &
(devdf['namespace'] == row.namespace),
'os'].values[0]
if os.startswith('junos') or os == 'panos':
os = 'junos'
elif os == 'nxos':
os = 'nxos'
else:
os = 'ios'

parsed_conf = CiscoConfParse(row.config.split('\n'), syntax=os)
conf = '\n'.join(parsed_conf.find_all_children(section))
if not conf:
drop_indices.append(index)
else:
df.loc[index, 'config'] = conf

if drop_indices:
df = df.drop(index=drop_indices)

if query_str:
df = df.query(query_str).reset_index(drop=True)

return df
1 change: 1 addition & 0 deletions suzieq/restServer/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ def query_devconfig(verb: CommonVerbs, request: Request,
view: ViewValues = "latest",
namespace: List[str] = Query(None),
columns: List[str] = Query(default=["default"]),
section: str = Query(None),
query_str: str = None,
what: str = None,
count: str = None, reverse: str = None,
Expand Down
2 changes: 1 addition & 1 deletion suzieq/sqobjects/devconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class DevconfigObj(SqObject):
def __init__(self, **kwargs):
super().__init__(table='devconfig', **kwargs)
self._valid_get_args = ['namespace', 'hostname', 'columns',
'query_str']
'query_str', 'section']

def unique(self, **kwargs) -> pd.DataFrame:
return pd.DataFrame(
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/sqcmds/common-samples/describe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ tests:
marks: devconfig describe
output: '[{"name": "active", "type": "boolean", "key": "", "display": "", "description":
"If this entry is active or deleted"}, {"name": "config", "type": "string", "key":
"", "display": 1, "description": "The running config"}, {"name": "deviceSession",
"", "display": 2, "description": "The running config"}, {"name": "deviceSession",
"type": "timestamp", "key": "", "display": "", "description": "Device boot session
id"}, {"name": "hostname", "type": "string", "key": 1, "display": 1, "description":
"Hostname associated with this record"}, {"name": "namespace", "type": "string",
"key": 0, "display": 0, "description": "Namespace associated with this record"},
{"name": "sqvers", "type": "string", "key": "", "display": "", "description":
"Schema version, not selectable"}, {"name": "timestamp", "type": "timestamp",
"key": "", "display": 2, "description": "Unix epach When this record was created,
"key": "", "display": 3, "description": "Unix epach When this record was created,
in ms"}]'
- command: device describe --format=json
data-directory: tests/data/parquet
Expand Down
Loading

0 comments on commit 0c5e593

Please sign in to comment.