MadaMzandu / uisp-ros-plugin

This is a follow up replacement for the previous Uisp api for mikrotik routers.
32 stars 9 forks source link

Add "custom device" to NMS section #46

Open fpaquin92 opened 1 year ago

fpaquin92 commented 1 year ago

Hi !

Would be great if the plugin can create or update a custom device to client site in NMS section when the customer picks an IP address from server. This will help to track user data and some statistics.

Thanks !

MadaMzandu commented 1 year ago

Thanks for the feedback. That would be a great feature. Last time I checked I could not find an nms api method for creating third party devices. If I find the call I will add the feature. Also unlike crm, nms requires an app token, so this has to be done carefully without breaking the crm side of the plugin.

fpaquin92 commented 1 year ago

I found a way to add custom device to UNMS using API Calls

First, use /devices/connect/other

curl -X 'POST' \
  'https://unms.mywisp.com/nms/api/v2.1/devices/connect/other' \
  -H 'accept: application/json' \
  -H 'x-auth-token: ********-****-****-****-************' \
  -H 'Content-Type: application/json' \
  -d '{
  "role": "router",
  "ip": "10.95.41.154",
  "hostname": "Client Name",
  "snmpCommunity": "public",
  "enablePing": false

And in the response, you will get the UNMS device ID

"identification": {
    "id": "adfcf475-27ed-42fd-8662-d15fffaf812f",

Then there is a call to search for a nms client site ID with his name using /sites/search

curl -X 'GET' \
  'https://unms.mywisp.com/nms/api/v2.1/sites/search?query=ClientFirstName%20ClientLastName&count=1&page=1' \
  -H 'accept: application/json' \
  -H 'x-auth-token: ********-****-****-****-************' 

and in the response you will get the NMS client site ID

"id": "c7658d8b-0844-46d1-9d72-0db5235c4c43",
Then, the final step is to use /devices/authorize

curl -X 'POST' \
  'https://unms.mywisp.com/nms/api/v2.1/devices/authorize' \
  -H 'accept: application/json' \
  -H 'x-auth-token: ********-****-****-****-************'  \
  -H 'Content-Type: application/json' \
  -d '{
  "siteId": "c7658d8b-0844-46d1-9d72-0db5235c4c43",
  "deviceIds": [
    "adfcf475-27ed-42fd-8662-d15fffaf812f"

And in the response you will get

  "result": true,
  "message": "Authorized"

And here it is ! The third party device is added and assigned to client site in NMS section !

If there is anything else i could help you with, i'll be happy to help you.

MadaMzandu commented 1 year ago

Great work! I will start on working on this, hopefully should be ready for the next version release.

fpaquin92 commented 1 year ago

Alright thanks ! I was looking to other api calls, and it would be nice if a data link is created between Client CPE and Client Router. Here is the way it could be done

Get client site uplink-devices to have the "uplink device id" (/nms/api/v2.1/sites/{Client Site ID}/uplink-devices)

Api call

curl` -X 'GET' \

  'https://unms.mywisp.com/nms/api/v2.1/sites/c7658d8b-0844-46d1-9d72-0db5235c4c43/uplink-devices?interval=hour' \
  -H 'accept: application/json' \
  -H 'x-auth-token: ****'

Response

"identification": {
  "id": "5128e41a-a99c-4d20-afeb-c77708fad602",
  "type": "airMax",

Query the device to get interface id (/nms/api/v2.1/devices/{ID}/detail)

Api Call

curl -X 'GET' \
  'https://unms.mywisp.com/nms/api/v2.1/devices/5128e41a-a99c-4d20-afeb-c77708fad602/detail?withStations=false' \
  -H 'accept: application/json' \
  -H 'x-auth-token: ****'

Response (Get the name of interface type eth from the response body)

   "identification": {
    "position": 0,
    "type": "eth",
    "name": "eth0",

Then, create data link between the new device and the client CPE (/data/links)

Api Call

curl -X 'POST' \
  'https://unms.mywisp.com/nms/api/v2.1/data-links' \
  -H 'accept: application/json' \
  -H 'x-auth-token: ****' \
  -H 'Content-Type: application/json' \
  -d '{
  "deviceIdFrom": "5128e41a-a99c-4d20-afeb-c77708fad602", <- Client CPE get from uplink device id
  "interfaceIdFrom": "eth0", <- Interface ID get from client device ID detail
  "deviceIdTo": "1e161673-5f6d-4fdf-be14-88c2e6f325f2", <- New device created earlier (We can get his device ID from the response body when we create the device)
  "interfaceIdTo": "eth1" <- New device interface ID (it's always eth1, but can also be queried)

And that's it ! A data link is created from CPE to client device

If there is already a data link created on CPE, the server will return a error 500, but the existing data link can be updated using "PUT" instead of "POST" but we will need the data link ID. It can be found using site id with this api call

curl -X 'GET' \
  'https://unms.mywisp.com/nms/api/v2.1/data-links/sites/c7658d8b-0844-46d1-9d72-0db5235c4c43' \
  -H 'accept: application/json' \
  -H 'x-auth-token: ****'

In the response, we will find each Data-link id of the client site, but the first one is always the "inside the site" data link, so normally client cpe to client router device

"id": "21d3ce69-d8bc-4e5a-9efa-1326add1814a", and, with this api call, we will update the existing data link to the new one router

curl -X 'PUT' \
  'https://unms.mywisp.com/nms/api/v2.1/data-links/21d3ce69-d8bc-4e5a-9efa-1326add1814a' \
  -H 'accept: application/json' \
  -H 'x-auth-token: ****' \
  -H 'Content-Type: application/json' \
  -d '{
  "id": "21d3ce69-d8bc-4e5a-9efa-1326add1814a",
  "deviceIdFrom": "5128e41a-a99c-4d20-afeb-c77708fad602", <- Client CPE
  "interfaceIdFrom": "eth0", <- Client CPE interface ID
  "deviceIdTo": "3edc3570-41bf-459c-825f-42b1bedd1624", <- New client router 
  "interfaceIdTo": "eth1" <- New client router interface ID

Voilà ! If there is anything else i could help you with, i'll be happy to help you.

Thanks !

MadaMzandu commented 1 year ago

You've been busy, thanks this should make for some interesting features. I will play round with the api calls and see how we can fit the functionality into the plugin. Will keep you updated