Probleme Dependency with API

Hi,

I’ve tried to create a dependency with api but I don’t see how. So far I’ve been trying to do it like this.

        requests_url = "https://172.20.20.73:5665/v1/objects/Dependencys"  
    
    headers = {
        'Accept': 'application/json',
        'X-HTTP-Method-Override': 'GET'
    }
    
    data = {
        "attrs": []
    }
    
    r = requests.post(requests_url,
                      headers=headers,
                      auth=('felix', 'felix22'),
                      data=json.dumps(data),
                      verify=False)
    print("Request URL: " + str(r.url))
    print("Status code: " + str(r.status_code))

    if (r.status_code == 200):
        print("Result: " + json.dumps(r.json()))
    else:
        print(r.text)
        r.raise_for_status()

because the url doesn’t exist can you help me please ?

Hello,

the url should be ...v1/objects/dependencies

Regards,
Carsten

Thanks You it work now !

1 Like

re,

I have just done some tests and I can’t create a dependency, could you show me an example of use with the following code?
requests_url = “https://172.20.20.73:5665/v1/objects/dependencies/"+parent+"To”+enfant

    headers = {
        'Accept': 'application/json',
        'X-HTTP-Method-Override': 'POST'
    }
    
    data = {
        "attrs": [parent , enfant]
    }
    
    r = requests.post(requests_url,
                      headers=headers,
                      auth=('felix', 'felix22'),
                      data=json.dumps(data),
                      verify=False)
    print("Request URL: " + str(r.url))
    print("Status code: " + str(r.status_code))

    if (r.status_code == 200):
        print("Result: " + json.dumps(r.json()))
    else:
        print(r.text)
        r.raise_for_status()

I’m not very experienced with the API, but the docs about creating new config objects (which I assume dependencies are) state:

New objects must be created by sending a PUT request.

Thank you for your answer. I know that but I don’t know what to put in the data or the name of the dependency.

Here is the JSON-output of querying a dependency, hope this helps :slight_smile:

{
	"results": [
		{
			"attrs": {
				"__name": "CHILD!test2",
				"active": true,
				"child_host_name": "CHILD",
				"child_service_name": "",
				"disable_checks": false,
				"disable_notifications": true,
				"ha_mode": 0.0,
				"ignore_soft_states": true,
				"name": "test2",
				"original_attributes": null,
				"package": "director",
				"parent_host_name": "PARENT",
				"parent_service_name": "",
				"paused": false,
				"period": "24x7",
				"source_location": {
					"first_column": 0.0,
					"first_line": 1.0,
					"last_column": 31.0,
					"last_line": 1.0,
					"path": "/var/lib/icinga2/api/packages/director/09e6b9e1-6d3a-4e45-91de-ef1a55aa6ba1/zones.d/director-global/dependency_apply.conf"
				},
				"states":["Up"],
				"templates": [
					"test2",
					"generic-dependency"
				],
				"type": "Dependency",
				"vars": null,
				"version": 0.0,
				"zone": "master"
			},
			"joins": {},
			"meta": {},
			"name": "CHILD!test2",
			"type": "Dependency"
		}
	]
}

Here is how it looks inside Icinga Director:

Unfortunately, that doesn’t help. anyone have an example, on how ot use depency with api with python ?

re,

After several research and testing, I finally found the solution for those who would have the same problem as me here is the code:

requests_url = "https://172.20.20.73:5665/v1/objects/dependencies/"+enfant+"!"+parent
    
    headers = {
        'Accept': 'application/json',
        'X-HTTP-Method-Override': 'PUT'
    }
    
    data = {
        "attrs": {"parent_host_name": parent,"child_host_name": enfant}
    }
    
    r = requests.post(requests_url,
                      headers=headers,
                      auth=('felix', 'felix22'),
                      data=json.dumps(data),
                      verify=False)
    print("Request URL: " + str(r.url))
    print("Status code: " + str(r.status_code))

    if (r.status_code == 200):
        print("Result: " + json.dumps(r.json()))
    else:
        print(r.text)
        r.raise_for_status()
3 Likes