Include compliance reports

For the netyang state, the compliance report will be
included by default in the pchanges dictionary.
It can be also returned in the comment field,
by enabling using the `compliance_report` option.
This commit is contained in:
Mircea Ulinic 2017-08-24 12:26:46 +00:00
parent 3634055e34
commit 3a906109bd
2 changed files with 23 additions and 6 deletions

View file

@ -38,6 +38,7 @@ except ImportError:
HAS_NAPALM_YANG = False
# Import salt modules
import salt.output
from salt.utils import fopen
import salt.utils.napalm
@ -140,6 +141,7 @@ def managed(name,
debug = kwargs.get('debug', False) or __opts__.get('debug', False)
commit = kwargs.get('commit', True) or __opts__.get('commit', True)
replace = kwargs.get('replace', False) or __opts__.get('replace', False)
return_compliance_report = kwargs.get('compliance_report', False) or __opts__.get('compliance_report', False)
profiles = kwargs.get('profiles', [])
temp_file = __salt__['temp.file']()
log.debug('Creating temp file: {0}'.format(temp_file))
@ -180,7 +182,13 @@ def managed(name,
log.debug('Loaded config result:')
log.debug(loaded_changes)
__salt__['file.remove'](temp_file)
return salt.utils.napalm.loaded_ret(ret, loaded_changes, test, debug)
loaded_changes['compliance_report'] = compliance_report
return salt.utils.napalm.loaded_ret(ret,
loaded_changes,
test,
debug,
opts=__opts__,
compliance_report=return_compliance_report)
def configured(name,

View file

@ -432,7 +432,7 @@ def default_ret(name):
return ret
def loaded_ret(ret, loaded, test, debug):
def loaded_ret(ret, loaded, test, debug, compliance_report=False, opts=None):
'''
Return the final state output.
ret
@ -445,6 +445,8 @@ def loaded_ret(ret, loaded, test, debug):
'comment': loaded.get('comment', '')
})
pchanges = {}
if 'compliance_report' in loaded:
pchanges['compliance_report'] = loaded['compliance_report']
if debug:
# Always check for debug
pchanges.update({
@ -471,10 +473,17 @@ def loaded_ret(ret, loaded, test, debug):
'pchanges': pchanges
})
if test:
for k, v in pchanges.items():
ret.update({
"comment": "{}:\n{}\n\n{}".format(k, v, ret.get("comment", ''))
})
if pchanges.get('diff'):
ret['comment'] = '{comment_base}\n\nConfiguration diff:\n\n{diff}'.format(comment_base=ret['comment'],
diff=pchanges['diff'])
if pchanges.get('loaded_config'):
ret['comment'] = '{comment_base}\n\nLoaded config:\n\n{loaded_cfg}'.format(
comment_base=ret['comment'],
loaded_cfg=pchanges['loaded_config'])
if compliance_report and pchanges.get('compliance_report'):
ret['comment'] = '{comment_base}\n\nCompliance report:\n\n{compliance}'.format(
comment_base=ret['comment'],
compliance=salt.output.string_format(pchanges['compliance_report'], 'nested', opts=opts))
ret.update({
'result': None,
})