TNSR API payload requirements
-
This post is deleted! -
I'm dumb. (don't have it working yet)
Should have paid more attention to the docs. -
ok, with better formatted json, I'm now getting a different error.
Failed to find YANG spec of XML node: static with parent: static in namespace: urn:netgate:xml:yang:netgate-nat
the sample code I've put together to do it. I'm getting the feeling it's probably something simple that I'm missing.
payload=dict() payload['netgate-nat:static']=dict() payload['netgate-nat:static']['mapping-table']=dict() payload['netgate-nat:static']['mapping-table']['mapping-entry']=[] baseport=8732 ipfirst="192.168.49." externaladdress="myexternalip" for octet in range(2,3): entry=dict() entry["local-port"]="3425" entry["route-table-name"]="ipv4-VRF:0" entry["transport-protocol"]="tcp" entry["local-address"]="{}{}".format(ipfirst,octet) entry["external-port"]="{}".format(octet+baseport) entry["external-address"]=externaladdress payload['netgate-nat:static']['mapping-table']['mapping-entry'].append(entry) import json import requests from requests.auth import HTTPBasicAuth basic = HTTPBasicAuth('username', 'password') headers = {'Content-type': 'application/yang-data+json'} url = "http://10.79.79.49/restconf/data/netgate-nat:nat-config/netgate-nat:static" response = requests.post(url,json=payload,auth=basic,headers=headers) print(json.dumps(payload)) print(response.text)
payload is
{"netgate-nat:static": {"mapping-table": {"mapping-entry": [{"local-port": "3425", "route-table-name": "ipv4-VRF:0", "transport-protocol": "tcp", "local-address": "192.168.49.2", "external-port": "8734", "external-address": "myexternalip"}]}}}
-
The trick is making sure the payload and URL agree in that the payload should be contained within the URL's namespace.
This works, for example:
$ curl -k --fail-with-body \ --cert ~/tnsr/tnsr-myuser.crt \ --key ~/tnsr/tnsr-myuser.key \ --cacert ~/tnsr/tnsr-selfca.crt \ -H "Content-Type: application/yang-data+json" \ -d '{"netgate-nat:mapping-entry": [{"transport-protocol": "tcp", "local-address": "10.2.0.5", "local-port": "22", "external-address": "203.0.113.2", "external-port": "222", "route-table-name": "ipv4-VRF:0" }]}' \ -X POST \ https://198.51.100.2/restconf/data/netgate-nat:nat-config/netgate-nat:static/netgate-nat:mapping-table/
You also have to be careful to scope it correctly. Doing a POST at a higher level than that would create the entire static mapping table (and likewise a PUT would replace it), not individual entries.
You want to be looking at this bit in the docs:
Whereas your example was a couple levels too high.
-
@jimp Thanks!