Recently, new classes were added to NSO Python API that makes it easier to play with commit/re-deploy parameters. Let me show you some examples on how you can make your life easier:
Setting CommitParams:
General Steps:
1) Create RW Maapi session and transaction handler:
m = ncs.maapi.Maapi()
m.start_user_session('admin', 'system', [])
trans = m.start_trans(ncs.RUNNING, ncs.READ_WRITE)
2) Create params instance of CommitParams class:
params = trans.get_params()
3) Depending on the objective you format your params object. See examples bellow
4) Optional: validate params:
print(params)
5) Apply and capture result:
result = trans.apply_params(True, params)
Example of possible parameters:
a) Commit dry-run outformat native use-lsa:
params.dry_run_native()
params.use_lsa()
print(params)
Output: CommitParams(dry-run/outformat=2 use-lsa=(1145948134, 526283796))
Note: you can add as many as needed but some combinations would not make sense.
b) Commit reconcile:
params.reconcile_discard_non_service_config()
or
params.reconcile_keep_non_service_config()
In [19]: print(params)
CommitParams(reconcile/discard-non-service-config)
or
In [22]: print(params)
CommitParams(reconcile/keep-non-service-config)
c) Commit no-networking:
params.no_networking()
In [27]: print(params)
Outout: CommitParams(no-networking)
d) Commit no-deploy:
params.no_deploy()
In [28]: print(params)
Output: CommitParams(no-deploy)
e) Set the trace-id:
params.trace_id('12345')
In [38]: print(params)
Output: CommitParams(trace-id=12345)
f) Commit no-out-of-sync-check
params.no_out_of_sync_check()
In [41]: print(params)
Output: CommitParams(no-out-of-sync-check)
g) Commit no-overwrite
params.no_overwrite()
In [14]: print(params)
Output: CommitParams(no-overwrite)
How to access to CommitParams from service Code in Python:
Check if it’s a dry-run and other commit parameters:
Note: Please remember to avoid side-effects as a well-known best practice.
Some examples based on the python-and service skeleton service:
class ServiceCallbacks(Service):
# The create() callback is invoked inside NCS FASTMAP and
# must always exist.
@service.create
def cb_create(self, tctx, root, service, proplist):
self.log.info('Service create(service=', service._path, ')')
#Get a Transaction object
ma = ncs.maagic.get_maapi(root)
trans = ma.attach(tctx)
#Get transaction paramters option 1: get_trans_params()
trans_param = trans.get_trans_params()
self.log.info('Trans params:', str(trans_param))
for i in trans_param:
self.log.info('Trans param: ', _ncs.hash2str(i.tag))
#Get transaction parameters option 2: using CommitParams class
params = trans.get_params()
self.log.info('Pameters: ', str(params))
#More methods: See here https://developer.cisco.com/docs/nso/api/#ncs-maapi/ncs.maapi.CommitParams
if params.is_dry_run():
self.log.info('Outformat: ', params.get_dry_run_outformat())
# Outformat: DryRunOutformat.CLI
#Get TraceID if set northbound
self.log.info('Trace ID: ', params.get_trace_id())
# Trace ID: 1234
# Check if it is reconcile:
if parms.is_reconcile_discard_non_service_config() or \
parms.is_reconcile_keep_non_service_config():
self.log.info('Reconciliation is On')
vars = ncs.template.Variables()
vars.add('DUMMY', '127.0.0.1')
template = ncs.template.Template(service)
template.apply('check_commit_params-template', vars)