cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1921
Views
5
Helpful
1
Replies

Get python action to display dry-run of values set by python (not template)

I have seen some posts about how to perform a commit-dry-run in a python action such as the below:

https://community.cisco.com/t5/nso-developer-hub-discussions/how-to-write-nso-action-python-to-get-its-dry-run-output/td-p/3499887

 

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

 

1 Accepted Solution

Accepted Solutions

samuelcoome
Level 1
Level 1

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

 

View solution in original post

1 Reply 1

samuelcoome
Level 1
Level 1

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