Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add section to devconfig #953

Merged
merged 10 commits into from
May 27, 2024
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
48 changes: 48 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,47 @@ 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 or not section:
return df

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

if devdf.empty or 'errror' 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
568 changes: 416 additions & 152 deletions tests/integration/sqcmds/cumulus-samples/devconfig.yml

Large diffs are not rendered by default.

315 changes: 268 additions & 47 deletions tests/integration/sqcmds/eos-samples/devconfig.yml

Large diffs are not rendered by default.

347 changes: 258 additions & 89 deletions tests/integration/sqcmds/nxos-samples/devconfig.yml

Large diffs are not rendered by default.

Loading