Creating objects using the API
-
Hello.
I am trying to create objects using the API (JSON-RPC).
I am using the method cmdb.object.create for that.
However, I cannot send a lot of information about the objects.For example, the template to create a server is:
{
"version": "2.0",
"method": "cmdb.object.create",
"params": {
"type": "C__OBJTYPE__SERVER",
"title": "My little server",
"apikey": "xxx",
"language": "en"
},
"id": 1
}Although, I would like to send information about its IP address for example. I can do that by creating the Server using the UI. Can I also do that using the API?
Can you help me?
-
Hey @joana_115
yes that is possible You can pass category data in your "object create" request like this:
{ "version": "2.0", "method": "cmdb.object.create", "params": { "type": "C__OBJTYPE__SERVER", "title": "My little server", "apikey": "xxx", "language": "en", "categories": { "<category constant>": [ {"<prop-key>": "<value>"} ], "C__CATG__CPU": [ {"title": "CPU 1"}, {"title": "CPU 2"} ], "C__CATG__MEMORY": [ {"title": "Memory A"} ] } }, "id": 1 }
-
Hey @lfischer,
Thank you for your response!
I tried to create the server and associate the IPv4 address, but I can't.
This is my request:{ "version": "2.0", "method": "cmdb.object.create", "params": { "type": "C__OBJTYPE__SERVER", "title": "my little server", "description": "my little server description", "apikey": "---", "language": "en", "categories": { "C__CATG__IP": [ {"net_type": 1}, {"ipv4_assignment": 1}, {"ipv4_address": "192.168.1.13"}, {"primary_hostaddress": "192.168.1.13"} ] } }, "id": 1 }
The 1 for the "net_type" refers to the IPv4 (Internet Protocol v4) in the table isys_net_type, and the 1 for the "ipv4_assignment" refers to DHCP in the table isys_ip_assignment.
I am sorry for not being able to use your suggestion.
What am I doing wrong? -
Hey @joana_115
I'm afraid I used a bad example or should have explained it a bit more You'll need to pass all data of one entry in one package, like this:
{ "version": "2.0", "method": "cmdb.object.create", "params": { "type": "C__OBJTYPE__SERVER", "title": "my little server", "description": "my little server description", "apikey": "---", "language": "en", "categories": { "C__CATG__IP": [ { "net_type": 1, "ipv4_assignment": 1, "ipv4_address": "192.168.1.13", "primary_hostaddress": "192.168.1.13" } ] } }, "id": 1 }
The reason for this syntax is that you can create multiple entries at once (for multivalue categories). That would look like this:
"C__CATG__IP": [ { "net_type": 1, "ipv4_assignment": 1, "ipv4_address": "192.168.1.13", "primary_hostaddress": "192.168.1.13" }, { "net_type": 1, "ipv4_assignment": 1, "ipv4_address": "192.168.1.123", "primary_hostaddress": "192.168.1.123" }, { ... } ]
I believe this might be your issue The single data block should look the same as if you create a category entry on its own via
cmdb.category.create
, like this:{ "version": "2.0", "method": "cmdb.category.save", "params": { "category": "C__CATG__WAN", "object": 10582, "data": { "capacity_up": 6, "capacity_up_unit": "MBit" }, "apikey": "<api key>" }, "id": 1 }
I hope this helps
-
Thank you @lfischer !