cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
418
Views
10
Helpful
7
Replies
Highlighted
Beginner

Is there a way to get "commit dry-run" from python?

Hi, it would be very useful for a code I'm writing in Python, to be able to get "commit dry-run outfomat xml" like output in python.

The code is like

...

with ncs.maapi.single_write_trans('admin', 'python') as t:
....
several maagic writes to CDB
...

At this point, before applying the transaction, I would lke to get CLI o XML output similar to "commit dry-run..." on the NSO CLI..

I tried t.apply with COMMIT_NCS_DRY_RUN_XML flag, but didn't get any result..

Anybody knows if that is possible?

Regards

7 REPLIES 7
Highlighted
Cisco Employee

If you use apply_params it ought to work.
Highlighted

Thanks a lot...I don't find documentation on that function...would be nice if you have an example

Highlighted

It is documented in the main python documentation, so you can do pydoc ncs.maapi.Transaction.apply_params. But, I think essentially it works the way you wanted apply to work, set flags to dry run and it will return a dictionary with dry-run results in it.
Highlighted

You can find apply_params() pydocs here: nso-5.2.0.1/doc/api/python/ncs.maapi.html

CommitParams class here: nso-5.2.0.1/src/ncs/pyapi/pysrc/maapi.py

 

Using CommitParams() along with apply_params() you can do dry-run like this:

...: cp.dry_run_native()
#...: cp.dry_run_cli()
#...: cp.dry_run_native()
#...: cp.dry_run_xml()
...: r = t.apply_params(True, cp)
...: print r
...:
{'device': {'ios-1': '\n\nhostname Test_75\n'}, 'outformat': 'native', 'dry-run': True}
 
 
Highlighted

OK! thanks a lot for your help!
Highlighted

Is there a way to do this in a service? I attempted to get the transaction of the service and follow this approach but I am getting:
r = t.apply_params(True, cp)
File "/root/nso/nso-versions/5.2.1/src/ncs/pyapi/ncs/tm.py", line 12, in wrapper
return fn(*args, **kwargs)
File "/root/nso/nso-versions/5.2.1/src/ncs/pyapi/ncs/maapi.py", line 1325, in apply_params
tvresult = self.maapi.apply_trans_params(self.th, keep_open, tvparams)
File "/root/nso/nso-versions/5.2.1/src/ncs/pyapi/ncs/maapi.py", line 273, in proxy
return real(self2.msock, *args, **kwargs)
Error: Bad protocol usage or unexpected retval (21): not allowed for attached transaction

Thanks!
Highlighted

HI Johan,

I don't recognize the this exact error, but usually it is not prudent to apply a transaction 'inside' of a transaction - tends to get into a transaction lock deadlock situation.

See this thread previous thread on a similar:

https://community.cisco.com/t5/nso-developer-hub-discussions/call-partial-sync-from-function-in-cb-create/td-p/3895272

 

Presumably, you are attempting to save off the dry-run config that is getting written to the network. I've seen this done through a 2-step action, does a transaction for the dry-run followed by a transaction for the actual service instance commit. Also seen it done via a custom cli command that issues subsequent commit-dry-run and commit commands. I'm sure a reactive fastmap service could be crafted to do the dry-run/commit sequence as well.