10-23-2018 11:44 AM - edited 03-01-2019 04:13 AM
I have seen some posts about how to perform a commit-dry-run in a python action such as the below:
However when I try this, I get an empty list rather than the changes I am staging. Note, I am not setting values in a template with python, I am setting values through python. I would like to be able to see what the changes the action would make with a commit-dry-run, then let the user run the action again with a commit parameter to perform the t.apply().
Most of the use cases I have found an answer to use the deprecated commit-dry-run action. Is this what I should be using?
Example:
dryRun = root.services.commit_dry_run
dryInput = dryRun.get_input()
dryInput.outformat = 'native'
dryOutput = dryRun(dryInput)
if dryOutput.native != None:
devlist = dryOutput.native.device
I see in the pydocs for ncs.maagic that I can pass the flag ncs.maapi.COMMIT_NCS_DRY_RUN_CLI to t.apply() or t.prepare() like so: t.apply(flags=ncs.maapi.COMMIT_NCS_DRY_RUN_CLI), however when I try this I get the error: 'Error: Python cb_action error. internal error (18): operation aborted by user'. When I pass other flags (e.g. COMMIT_NCS_COMMIT_QUEUE_SYNC), it works fine, only on COMMIT_NCS_DRY_RUN_CLI and other COMMIT_NCS_DRY_RUN does it fail.
I would like to know if I should use the deprecated version of commit-dry-run or the flag in the pydocs for ncs.maagic.
I haven't been able to get either to work, so an example would be greatly appreciated.
Thanks in advance,
Sam
Solved! Go to Solution.
10-24-2018 12:08 PM - edited 10-24-2018 01:40 PM
I have found the answer to my question, using the deprecated commit-dry-run action works fine. I was not handling the 'dryOutput' properly after getting the data.
# Here are some ways I found the information, making use of the dir() function
dryRun = root.services.commit_dry_run
dryInput = dryRun.get_input()
dryInput.outformat = 'xml'
dryOutput = dryRun(dryInput)
# Used to find what I can access
# Such as I see 'cli', 'native', and 'result_xml'
self.log.info(dir(dryOutput))
self.log.info(dir(dryOutput.result_xml))
if dryOutput.result_xml != None:
# result_xml and cli use .local_node
# native uses .device
devlist = dryOutput.result_xml.local_node
# devlist.data is what I want
self.log.info(devlist.data)
# Note that native acts differently
# The below if what I ended up implementing. You can take outformat in as a YANG variable, I just hard coded it for this example
outformat = 'cli'
dryRun = root.services.commit_dry_run
dryInput = dryRun.get_input()
dryInput.outformat = outformat
dryOutput = dryRun(dryInput)
if dryOutput.result_xml.local_node.data != None:
devlist = dryOutput.result_xml.local_node
self.log.info(devlist.data)
ret = devlist.data
if dryOutput.cli.local_node.data != None:
devlist = dryOutput.cli.local_node
self.log.info(devlist.data)
ret = devlist.data
if len(dryOutput.native.device) != 0:
data = ""
for dev in dryOutput.native.device:
data = data + "\n" + dev.name + dev.data
self.log.info(data)
ret.append(data)
# you can set your YANG output value to ret to print it to the NSO user
# output.dry_run = ret
return ret
10-24-2018 12:08 PM - edited 10-24-2018 01:40 PM
I have found the answer to my question, using the deprecated commit-dry-run action works fine. I was not handling the 'dryOutput' properly after getting the data.
# Here are some ways I found the information, making use of the dir() function
dryRun = root.services.commit_dry_run
dryInput = dryRun.get_input()
dryInput.outformat = 'xml'
dryOutput = dryRun(dryInput)
# Used to find what I can access
# Such as I see 'cli', 'native', and 'result_xml'
self.log.info(dir(dryOutput))
self.log.info(dir(dryOutput.result_xml))
if dryOutput.result_xml != None:
# result_xml and cli use .local_node
# native uses .device
devlist = dryOutput.result_xml.local_node
# devlist.data is what I want
self.log.info(devlist.data)
# Note that native acts differently
# The below if what I ended up implementing. You can take outformat in as a YANG variable, I just hard coded it for this example
outformat = 'cli'
dryRun = root.services.commit_dry_run
dryInput = dryRun.get_input()
dryInput.outformat = outformat
dryOutput = dryRun(dryInput)
if dryOutput.result_xml.local_node.data != None:
devlist = dryOutput.result_xml.local_node
self.log.info(devlist.data)
ret = devlist.data
if dryOutput.cli.local_node.data != None:
devlist = dryOutput.cli.local_node
self.log.info(devlist.data)
ret = devlist.data
if len(dryOutput.native.device) != 0:
data = ""
for dev in dryOutput.native.device:
data = data + "\n" + dev.name + dev.data
self.log.info(data)
ret.append(data)
# you can set your YANG output value to ret to print it to the NSO user
# output.dry_run = ret
return ret
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide