# Probleme Dependency with API

**URL:** <https://community.icinga.com/t/probleme-dependency-with-api/3684>\
**Category:** Icinga 2\
**Created:** [March 4, 2020, 7:57am UTC](https://community.icinga.com/t/probleme-dependency-with-api/3684 "2020-03-04T07:57:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tealwen](https://community.icinga.com/user_avatar/community.icinga.com/tealwen/32/1949_2.png) [@Tealwen](https://community.icinga.com/u/Tealwen)\
**Post date:** [March 4, 2020, 7:57am UTC](https://community.icinga.com/t/probleme-dependency-with-api/3684/1 "2020-03-04T07:57:54Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![anon66228339](https://community.icinga.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@anon66228339](https://community.icinga.com/u/anon66228339)\
**Post date:** [March 4, 2020, 8:04am UTC](https://community.icinga.com/t/probleme-dependency-with-api/3684/2 "2020-03-04T08:04:43Z")

</div>

Hello,

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

Regards,  
Carsten

---

<div class="post-metadata">

**Author:** ![Tealwen](https://community.icinga.com/user_avatar/community.icinga.com/tealwen/32/1949_2.png) [@Tealwen](https://community.icinga.com/u/Tealwen)\
**Post date:** [March 4, 2020, 8:10am UTC](https://community.icinga.com/t/probleme-dependency-with-api/3684/3 "2020-03-04T08:10:00Z")

</div>

> [@anon66228339](#):
>
> v1/objects/dependencies

Thanks You it work now !

---

<div class="post-metadata">

**Author:** ![Tealwen](https://community.icinga.com/user_avatar/community.icinga.com/tealwen/32/1949_2.png) [@Tealwen](https://community.icinga.com/u/Tealwen)\
**Post date:** [March 4, 2020, 9:22am UTC](https://community.icinga.com/t/probleme-dependency-with-api/3684/4 "2020-03-04T09:22:04Z")

</div>

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](https://172.20.20.73:5665/v1/objects/dependencies/%22+parent+%22To)”+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()
```

---

<div class="post-metadata">

**Author:** ![log1c](https://community.icinga.com/user_avatar/community.icinga.com/log1c/32/2071_2.png) [@log1c](https://community.icinga.com/u/log1c)\
**Post date:** [March 4, 2020, 9:53am UTC](https://community.icinga.com/t/probleme-dependency-with-api/3684/5 "2020-03-04T09:53:15Z")

</div>

I’m not very experienced with the API, but [the docs about creating new config objects](https://icinga.com/docs/icinga2/latest/doc/12-icinga2-api/#creating-config-objects) (which I assume dependencies are) state:

> New objects must be created by sending a PUT request.

---

<div class="post-metadata">

**Author:** ![Tealwen](https://community.icinga.com/user_avatar/community.icinga.com/tealwen/32/1949_2.png) [@Tealwen](https://community.icinga.com/u/Tealwen)\
**Post date:** [March 4, 2020, 10:07am UTC](https://community.icinga.com/t/probleme-dependency-with-api/3684/6 "2020-03-04T10:07:18Z")

</div>

> [@Tealwen](#):
>
> POST

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.

---

<div class="post-metadata">

**Author:** ![log1c](https://community.icinga.com/user_avatar/community.icinga.com/log1c/32/2071_2.png) [@log1c](https://community.icinga.com/u/log1c)\
**Post date:** [March 4, 2020, 10:17am UTC](https://community.icinga.com/t/probleme-dependency-with-api/3684/7 "2020-03-04T10:17:33Z")

</div>

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

```auto
{
	"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:

 ![image](https://community.icinga.com/uploads/default/original/2X/6/6c2234c16406349ccf47d4de1393d7005d25b5b6.png)

---

<div class="post-metadata">

**Author:** ![Tealwen](https://community.icinga.com/user_avatar/community.icinga.com/tealwen/32/1949_2.png) [@Tealwen](https://community.icinga.com/u/Tealwen)\
**Post date:** [March 4, 2020, 12:27pm UTC](https://community.icinga.com/t/probleme-dependency-with-api/3684/8 "2020-03-04T12:27:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tealwen](https://community.icinga.com/user_avatar/community.icinga.com/tealwen/32/1949_2.png) [@Tealwen](https://community.icinga.com/u/Tealwen)\
**Post date:** [March 4, 2020, 2:22pm UTC](https://community.icinga.com/t/probleme-dependency-with-api/3684/9 "2020-03-04T14:22:10Z")

</div>

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()
```
