cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
674
Views
3
Helpful
5
Replies
Highlighted
Beginner

SERVICE TEMPLATE REST API

I am able to successfully create device templates and deploy the variables in those templates via REST API calls.  I have not been able to successfully deploy SERVICE templates via REST API.  I am able to do so via the CLI, however it would be beneficial for integration purposes if I could do so via REST API.  Does anyone have a good working example of deploying a SERVICE template via REST API?  Any assistance would be greatly appreciated!

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted

unclear what's happening in your python code (or curl) -- could be in the header(?) or auth(?)

i just tried another one on my box - this time in XML (since you're using that)...

here is the service info, and then the CURL call... (xml in-line -- should be same if calling a file)

admin@ncs% show services secTemplate1

secTemplate1 FOOFOO {

    device c0;

    access ro;

}

[ok][2017-08-09 11:06:03]

[edit]

admin@ncs% show services secTemplate1 | display xml

<config xmlns="http://tail-f.com/ns/config/1.0">

  <services xmlns="http://tail-f.com/ns/ncs">

  <secTemplate1 xmlns="http://com/example/secTemplate1">

    <comm-str>FOOFOO</comm-str>

    <device>c0</device>

    <access>ro</access>

  </secTemplate1>

  </services>

</config>

[ok][2017-08-09 11:06:08]

[edit]

admin@ncs%

And then...

in linux...

curl -X POST -u admin:admin --header "Content-Type:application/vnd.yang.data+xml" http://127.0.0.1:8080/api/running/services \

-d '<secTemplate1 xmlns="http://com/example/secTemplate1"><comm-str>BLAHBLAH</comm-str><device>c0</device><access>ro</access></secTemplate1>'

GSCHUDEL-M-61C2:Training442 gschudel$ curl -X POST -u admin:admin --header "Content-Type:application/vnd.yang.data+xml" http://127.0.0.1:8080/api/running/services -d '<secTemplate1 xmlns="http://com/example/secTemplate1"><comm-str>BLAHBLAH</comm-str><device>c0</device><access>ro</access></secTemplate1>'

GSCHUDEL-M-61C2:Training442 gschudel$



and now on NSO...

admin@ncs%

System message at 2017-08-09 11:33:54...

Commit performed by admin via http using rest.

admin@ncs% show services secTemplate1 | display xml

<config xmlns="http://tail-f.com/ns/config/1.0">

  <services xmlns="http://tail-f.com/ns/ncs">

  <secTemplate1 xmlns="http://com/example/secTemplate1">

    <comm-str>BLAHBLAH</comm-str>

    <device>c0</device>

    <access>ro</access>

  </secTemplate1>

  <secTemplate1 xmlns="http://com/example/secTemplate1">

    <comm-str>FOOFOO</comm-str>

    <device>c0</device>

    <access>ro</access>

  </secTemplate1>

  </services>

</config>

[ok][2017-08-09 11:33:57]

View solution in original post

5 REPLIES 5
Highlighted
Cisco Employee

Hi...

are you just trying to create a service instance via REST?

do you want to use XML or JSON payload(s)?

** if you create a service instance via CLI, and then "show services | display XML" (or json) -- it will give you the output required (and that you can then modify by changing variables) for repeating this via REST

here's an example -- starting from a display of a service, and then how this is rendered in the curl call...

admin@ncs% show services stackedIfMgr tix01 | display json

{

  "data": {

    "tailf-ncs:services": {

      "stackedIfMgr:stackedIfMgr": [

        {

          "name": "tix01",

          "cat-device": "c0",

          "asa-device": "asa0",

          "cat-wan-ip": "10.1.1.1/30",

          "cat-lan-ip": "172.16.16.1/24",

          "asa-port": "80",

          "asa-host": "172.16.16.16"

        }

      ]

    }

  }

}

[ok][2017-08-07 11:02:19]

[edit]

admin@ncs%

and then in CURL (with in-line data...)

curl -X POST -u admin:admin --header "Content-Type:application/vnd.yang.data+json" http://127.0.0.1:8080/api/running/services

-d '{"stackedIfMgr:stackedIfMgr": [{"name": "tix01", "cat-device": "c0", "asa-device": "asa0", "cat-wan-ip": "10.1.1.1/30", "cat-lan-ip": "172.16.16.1/24", "asa-port": "80", "asa-host": "172.16.16.16"}]}'

You can do this in XML as well, and you can point to files to load rather than in-line data (all your choice).

For "modifying" an existing service instance, use "-X PATCH"

For "deleting" an existing service instance, use "-X DELETE"

Highlighted

gschudel,

  My service template looks as follows:

[edit services ospf]

devops@ncs% show

ospf WASHINGTON {

    interface ge-0/0/5;

    area 0.0.0.0;

devops@ncs> show services ospf | display xml

<config xmlns='http://tail-f.com/ns/config1.0">

    <services xmlns="http://tail-f.com/ns/ncs">

    <ospf xmlns="http://com/example/ospf">

        <device-name>WASHINGTON</device-name>

        <interface>ge-0/0/5</interface>

        <area>0.0.0.0</area>

    </ospf>

    </services>

</config>

  I took the xml output from the template (only 2 variables required, which are device-name, interface, and area) and substituted the name for another device called NEWYORK.  The interface and area variables remained constant for purposes of this test.  I added those contents to a text file called OSPF.CFG and stored it in the directory from which my REST calls are to be executed.  The REST API is written in Python Requests and looks as follows:

import requests

from requests.auth import HTTPBasicAuth

url = ('http://xx.xx.xx.xx:8080/api/running/services')

headers = {'Content-Type': 'application/vnd.yang.data+xml}

content = open('ospf.cfg', 'rb').read

config = requests.post(url, auth=HTTPBasicAuth('admin', 'admin'), data=content)

print config.text

  Note that I also executed a "curl-based" script that mirrored the Python script.  The result remained the same, which was:

<errors xmlns="http://tail-f.com/ns/tailf-rest-error">

    <error>

        <error-tag>malformed-message</error-tag>

        <error-urlpath>/api/running/services</error-urlpath>

        <error-message>unknown element: services in /ncs:services/ncs:services</error-message>  

    </error>

</errors>

  Also note that I am able to successfully deploy this service as long as I commit it via the CLI, but I am unable to do so via REST API.  I'm not sure what I'm doing incorrectly here.

Highlighted

unclear what's happening in your python code (or curl) -- could be in the header(?) or auth(?)

i just tried another one on my box - this time in XML (since you're using that)...

here is the service info, and then the CURL call... (xml in-line -- should be same if calling a file)

admin@ncs% show services secTemplate1

secTemplate1 FOOFOO {

    device c0;

    access ro;

}

[ok][2017-08-09 11:06:03]

[edit]

admin@ncs% show services secTemplate1 | display xml

<config xmlns="http://tail-f.com/ns/config/1.0">

  <services xmlns="http://tail-f.com/ns/ncs">

  <secTemplate1 xmlns="http://com/example/secTemplate1">

    <comm-str>FOOFOO</comm-str>

    <device>c0</device>

    <access>ro</access>

  </secTemplate1>

  </services>

</config>

[ok][2017-08-09 11:06:08]

[edit]

admin@ncs%

And then...

in linux...

curl -X POST -u admin:admin --header "Content-Type:application/vnd.yang.data+xml" http://127.0.0.1:8080/api/running/services \

-d '<secTemplate1 xmlns="http://com/example/secTemplate1"><comm-str>BLAHBLAH</comm-str><device>c0</device><access>ro</access></secTemplate1>'

GSCHUDEL-M-61C2:Training442 gschudel$ curl -X POST -u admin:admin --header "Content-Type:application/vnd.yang.data+xml" http://127.0.0.1:8080/api/running/services -d '<secTemplate1 xmlns="http://com/example/secTemplate1"><comm-str>BLAHBLAH</comm-str><device>c0</device><access>ro</access></secTemplate1>'

GSCHUDEL-M-61C2:Training442 gschudel$



and now on NSO...

admin@ncs%

System message at 2017-08-09 11:33:54...

Commit performed by admin via http using rest.

admin@ncs% show services secTemplate1 | display xml

<config xmlns="http://tail-f.com/ns/config/1.0">

  <services xmlns="http://tail-f.com/ns/ncs">

  <secTemplate1 xmlns="http://com/example/secTemplate1">

    <comm-str>BLAHBLAH</comm-str>

    <device>c0</device>

    <access>ro</access>

  </secTemplate1>

  <secTemplate1 xmlns="http://com/example/secTemplate1">

    <comm-str>FOOFOO</comm-str>

    <device>c0</device>

    <access>ro</access>

  </secTemplate1>

  </services>

</config>

[ok][2017-08-09 11:33:57]

View solution in original post

Highlighted

THANK YOU SOOOOOOOOOO VERY MUCH!!!!

The problem was where my XML start and end was being copied from!

I needed to start the copy of the XML, not from the config, or services tag, but from the template-named tag itself.  And from there, obviously end it with the ending template tag.  That resolved my problem!!

I still had problems doing the same thing in Python, but CURL works flawlessly.  I'll figure out the Python Requests issue on an aside, but for now, you've helped me out TREMENDOUSLY!  Many thanks!

Highlighted

COOL!

i find it always helps and makes sense when i see "real examples"

glad it works !

you can PATCH and DELETE too this way

best regards,

Gregg