cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1052
Views
0
Helpful
4
Replies

Change the Hostname using REST

Ivan Sanchez
Level 1
Level 1

Hi,

I am working with my first programs in order to integrate NSO with REST, I already succeed making a REST Call using GET and DELETE but now I am trying to change the hostname of one of my devices through the POST method and I am getting some errors. These are my tests.

GET

http://IPADDRESS_NSO:8080/api/running/devices/device/ce1

Headers:

Authorization: Basic

Accept: application/vnd.yang.data+json


it works fine


DELETE

http://IPADDRESS_NSO/api/running/devices/device/ce0

Headers:

Authorization: Basic


it works fine


POST

I have tried with several URLS

http://IPADDRESS_NSO/api/running/devices/device/ce1/config

http://IPADDRESS_NSO/api/running/devices/device/ce1

Headers:

Authorization: Basic

Accept: application/vnd.yang.data+json

Body


{

    "tailf-ned-cisco-ios:hostname": "newhostname"

}


But I got this error in postman


{

    "errors": {

        "error": [

            {

                "error-message": "unknown element: hostname in /ncs:devices/ncs:device[ncs:name='ce1']/ncs:config/ncs:hostname",

                "error-urlpath": "/api/running/devices/device/ce1/config",

                "error-tag": "malformed-message"

            }

        ]

    }

}


Someone can help me or point me out to some documentation. Thanks.


1 Accepted Solution

Accepted Solutions

yfherzog
Cisco Employee
Cisco Employee

Hi,

As the hostname might already be defined, you might need to use a PATCH rather than a POST method on your call.

I assume you refer here to the hostname command configured on the device (rather than the name of the device on the list of devices in NSO), so in this case, this is going to be located under the config container, and the exact command might depend on the specific device type.

If we use Cisco IOS-XR for example, then running the GET method you can see the structure of the running config which should point you towards the right structure on your PATCH/POST command.

For example:

YFHERZOG-M-W1ZZ:stacked-example yfherzog$ curl -u admin:admin -X GET http://localhost:8080/api/running/devices/device/xr-0/config

<config xmlns="http://tail-f.com/ns/ncs" xmlns:y="http://tail-f.com/ns/rest"  xmlns:ncs="http://tail-f.com/ns/ncs">

  <hostname xmlns="http://tail-f.com/ned/cisco-ios-xr">old-name</hostname>

...

...

...

In the case of the XR NED, we can see the hostname directly under the config container (/devices/device/config/hostname), but in your case it might be different, depending on the NED.

I will construct my PATCH command accordingly (I'll use XML for the payload, but you can use JSON as well):

YFHERZOG-M-W1ZZ:stacked-example yfherzog$ curl -i -u admin:admin http://localhost:8080/api/running/devices/device/xr-0/config -X PATCH -H "Content-Type: application/vnd.yang.data+xml" -d "<config><hostname>new-name</hostname></config>"

HTTP/1.1 204 No Content

Server:

Date: Sun, 13 Aug 2017 12:42:29 GMT

Last-Modified: Sun, 13 Aug 2017 12:42:29 GMT

Cache-Control: private, no-cache, must-revalidate, proxy-revalidate

Etag: 1502-628149-698901

Content-Length: 0

Content-Type: text/html

Pragma: no-cache

Subsequent GET, shows the new value:

YFHERZOG-M-W1ZZ:stacked-example yfherzog$ curl -u admin:admin -X GET http://localhost:8080/api/running/devices/device/xr-0/config/hostname

<hostname xmlns="http://tail-f.com/ned/cisco-ios-xr" xmlns:y="http://tail-f.com/ns/rest"  xmlns:cisco-ios-xr="http://tail-f.com/ned/cisco-ios-xr"  xmlns:ncs="http://tail-f.com/ns/ncs">new-name</hostname>

Hope that helps.

Regards,

Yftach

View solution in original post

4 Replies 4

yfherzog
Cisco Employee
Cisco Employee

Hi,

As the hostname might already be defined, you might need to use a PATCH rather than a POST method on your call.

I assume you refer here to the hostname command configured on the device (rather than the name of the device on the list of devices in NSO), so in this case, this is going to be located under the config container, and the exact command might depend on the specific device type.

If we use Cisco IOS-XR for example, then running the GET method you can see the structure of the running config which should point you towards the right structure on your PATCH/POST command.

For example:

YFHERZOG-M-W1ZZ:stacked-example yfherzog$ curl -u admin:admin -X GET http://localhost:8080/api/running/devices/device/xr-0/config

<config xmlns="http://tail-f.com/ns/ncs" xmlns:y="http://tail-f.com/ns/rest"  xmlns:ncs="http://tail-f.com/ns/ncs">

  <hostname xmlns="http://tail-f.com/ned/cisco-ios-xr">old-name</hostname>

...

...

...

In the case of the XR NED, we can see the hostname directly under the config container (/devices/device/config/hostname), but in your case it might be different, depending on the NED.

I will construct my PATCH command accordingly (I'll use XML for the payload, but you can use JSON as well):

YFHERZOG-M-W1ZZ:stacked-example yfherzog$ curl -i -u admin:admin http://localhost:8080/api/running/devices/device/xr-0/config -X PATCH -H "Content-Type: application/vnd.yang.data+xml" -d "<config><hostname>new-name</hostname></config>"

HTTP/1.1 204 No Content

Server:

Date: Sun, 13 Aug 2017 12:42:29 GMT

Last-Modified: Sun, 13 Aug 2017 12:42:29 GMT

Cache-Control: private, no-cache, must-revalidate, proxy-revalidate

Etag: 1502-628149-698901

Content-Length: 0

Content-Type: text/html

Pragma: no-cache

Subsequent GET, shows the new value:

YFHERZOG-M-W1ZZ:stacked-example yfherzog$ curl -u admin:admin -X GET http://localhost:8080/api/running/devices/device/xr-0/config/hostname

<hostname xmlns="http://tail-f.com/ned/cisco-ios-xr" xmlns:y="http://tail-f.com/ns/rest"  xmlns:cisco-ios-xr="http://tail-f.com/ned/cisco-ios-xr"  xmlns:ncs="http://tail-f.com/ns/ncs">new-name</hostname>

Hope that helps.

Regards,

Yftach

For documentation, you can use the NSO Northbound APIs guide, which can be found under your installation directory.

Thanks a lot,, I changed to PATCH and change the body like this and it works!!!!

<config xmlns="http://tail-f.com/ns/ncs" xmlns:y="http://tail-f.com/ns/rest"  xmlns:ncs="http://tail-f.com/ns/ncs">

    <hostname xmlns="urn:ios">new hostname</hostname>

</config>

I have not seen the guide that you mentioned. Thanks.

I'm Glad it works!

Regarding the guide, you can find it together with the rest of the PDF guides, under the installation directory:

<nso-install-dir>/doc/pdf/nso_northbound-<nso-version>.pdf