Using the director api in Scripts


(Stevie Sy) #1

Hi,
I work on a perl script to access the director api for updating hosts and Services (e.g. custom vars).
The access works fine for me. But I noticed if a host has more custom vars and I only update one of them, the others are deleted.
Is this a normal behavior - work as design - or is it a bug. If the last Thing I will open an Issue on github. If not I have to save the other vars before. I didn’t noticed this with the icinga api.

cheers.


(Michael Klug) #2

Maybe look at post vs put.


(Stevie Sy) #3

thanks for the Input.

from the documentation: https://icinga.com/docs/director/latest/doc/70-REST-API/
POST - Trigger actions, create or modify objects. Can also be used to partially modify objects
PUT - Creates or replaces objects, cannot be used to modify single object properties|

I tried with POST like the documentation.


(Matthias) #4

POST should merge the properties.
Can you post a sample request body?


(Stevie Sy) #5

Example Outputs with curl:

  1. GET ( curl -H ‘Accept: application/json’ -u ‘user:password’ ‘https://icinga/icingaweb2/director/host?name=Testhost

Output:
{
“address”: “127.0.0.1”,
“display_name”: “Testhost”,
“imports”: [
“template_host_others”
],
“object_name”: “Testhost”,
“object_type”: “object”,
“vars”: {
“Kontakt1”: “OP”,
“Quelle_Grunddaten”: “MANUELL”,
“Verwalter”: “OP”,
“nameelastic”: “TESTHOST”
}
}

  1. POST ( curl -H ‘Accept: application/json’ -u -u ‘user:password’ -X POST ‘https://icinga/icingaweb2/director/host?name=Testhost’ -d ‘{ “vars”: { “Kontakt1”: “Test” } }’)

Output:
{
“address”: “127.0.0.1”,
“display_name”: “Testhost”,
“imports”: [
“template_host_others”
],
“object_name”: “Testhost”,
“object_type”: “object”,
“vars”: {
“Kontakt1”: “Test”
}
}

And the Output in the director:


(Dmitry) #6

Hello
When I’m attempting to do it in the way you are, I’m getting the same result. Looks like if your modification request has an array of vars - the entire array will be overwritten.

To achieve the goal you need, try to make this request:

POST ( curl -H ‘Accept: application/json’ -u -u ‘user:password’ -X POST ‘https://icinga/icingaweb2/director/host?name=Testhost’ -d ‘{ “vars.Kontakt1”: “Test” }’)

That must change only one variable in the array.

Best,
-Dmitry


(Stevie Sy) #7

thanks Dimitry for the hint. That was it.
I read the documentary of the REST API again and I found the part. I’ve read it over all the time. Now I can finish my script.