cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
721
Views
10
Helpful
6
Replies

How to convert this restconf api to maapi python code?

himanss4
Cisco Employee
Cisco Employee

I have an restconf api i and not able to understand how to pass body in this.

This is my postman.

check-sync-output.png

 

I have code which do check-sync , but i don't know how to pass body ({"outformat":"cli"}) to that code 

 

import socket
import _ncs
from _ncs import maapi
import traceback
import json

def get_traceback(e):
    lines = traceback.format_exception(type(e), e, e.__traceback__)
    return '--- '.join(lines)


sock_maapi = socket.socket()
# self.log.info('action NCS_PORT: ', _ncs.NCS_PORT)
maapi.connect(sock_maapi,ip='127.0.0.1',port=_ncs.NCS_PORT)

# maapi.load_schemas(sock_maapi)

_ncs.maapi.start_user_session( sock_maapi,'admin','python',[],'127.0.0.1', _ncs.PROTO_TCP)

ns_hash = _ncs.str2hash("http://tail-f.com/ns/ncs")

results = maapi.request_action(sock_maapi, [], ns_hash, "/pycheck-sync-containtest:a/ntp:ntp{ntp2-default}/check-sync")

How to pass body? 

And can you provide some docs which tell how to convert restconf Api to maapi code?

6 Replies 6

@himanss4 on your Postman client, run the API call and then on the right side of Posstamn click the `</>` symbol, you can select Python request from the languages, this will display the API requests into Python.

 

Hope this helps.

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

@bigevilbeard I want to use maapi python code. What you are telling that is http calls from python code. But NSO has inbuild maapi package which can do same thing , I have already written that pls check above , I want how to pass body in that. I know http call in python by i want to leverage maapi package of NSO

I would normally use maagic, in which case it is pretty easy.

For the raw maapi you have to form the input list yourself, which is significantly less fun. You do that by a list of _ncs.TagValue elements containing _ncs.XmlTag elements.

Is there a reason you do not want to use the maagic api?

Hi @vleijon Maagic and Maapi is all new for me. I got more example in maapi then maagic api. I am good if maagic api does that work more easily. 

Any example you have for above thing? 

Do you have any good blog where maagic api implementation is provided for POST request ?

A good way of exploring the maagic api is ncs_pycli (https://github.com/NSO-developer/ncs_pycli) and the basics of magic are covered in the developer guide for NSO 5.7 (https://developer.cisco.com/docs/nso/guides/#!basic-automation-with-python/read-and-write-values)

Using ncs_pycli you can do a sync-from directly on a device like this:

Your maagic object 'root -> (root)' is now prepared... go have some fun!
trans.compare() to see your current transaction
trans.apply() to commit
trans.revert() to revert changes
Maapi object can be found at m
You can restart the transaction and create a fresh root object by invoking new_trans:
In [1]: new_trans
new transaction created

In [1]: dev = root.devices.device['r0']

In [2]: inp = dev.sync_from.get_input()

In [3]: inp.dry_run.outformat = 'cli'

In [4]: r = dev.sync_from(inp)

In [5]: r.result
Out[5]: False

In [6]: r.info
Out[6]: 'Failed to connect to device r0: connection refused'


work like charm