Create a Post
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted

Creating Network Objects via API

Hello All,

I have a requirement to create network objects via API (mgmt_cli or smartconsole CLI) but the catch is:

API needs to create the object with the provided name and IP address, etc only if no object exists with that IP address.

API needs to edit the name, comments fields if an object exists for a given IP address.

Both needs to be part of the same script (may be like a if,else script).

Can someone shed some light on this?

Thanks in Advance!

0 Kudos
1 Reply
Highlighted
Admin
Admin

Re: Creating Network Objects via API

First off, unless you include set-if-exists true as part of your API call, the API will not allow you to create an object if something with the same IP exists--you'll get an error like:

[Expert@mgmt:0]# mgmt_cli -r true add host name foobar ipv4-address 192.168.1.1
code: "err_validation_failed"
message: "Validation failed with 1 warning"
warnings:
- message: "Multiple objects have the same IP address 192.168.1.1"

The trick is figuring out what object has that IP.
Fortunately, there's an API call for that:

[Expert@mgmt:0]# mgmt_cli -r true show objects filter 192.168.1.1 ip-only true type host
from: 1
to: 1
total: 1
objects:
- uid: "846051cb-a16a-4c4b-acfc-664217fc8ee6"
name: "barfoo"
type: "host"
domain:
uid: "41e821a0-3720-11e3-aa6e-0800200c9fde"
name: "SMC User"
domain-type: "domain"
ipv4-address: "192.168.1.1"

Now, you can reference the object by UID and change the name/comment:

[Expert@mgmt:0]# mgmt_cli -r true set host uid 846051cb-a16a-4c4b-acfc-664217fc8ee6 new-name foobar \
comments "This is my comment"

---------------------------------------------
Time: [13:56:11] 3/6/2019
---------------------------------------------
"Publish operation" succeeded (100%)

 How you script the above is up to you, but that's the gist of it.