Icinga / icingaweb2-module-director

The Director aims to be your new favourite Icinga config deployment tool. Director is designed for those who want to automate their configuration deployment and those who want to grant their “point & click” users easy access to the configuration.
https://icinga.com/docs/director/latest
GNU General Public License v2.0
413 stars 203 forks source link

Full support for apply rules on API and CLI #1783

Open julenl opened 5 years ago

julenl commented 5 years ago

Expected Behavior

To have full support for apply rules from API and CLI. Including checking if a rule with a given name exists and deleting it.

To the best of my knowledge, the CLI cannot even create rules, right?

In CLI:

icingacli director apply exists myrule
icingacli director apply delete myrule

On the API:

director-curl.sh GET director/apply?name=myrule
director-curl.sh DELETE director/apply?name=myrule

Current Behavior

The apply rules work perfect from the web interface, and it also work fine in case of creating them with the API. But there is no way to figure out how to check if a rule already exists in director or to delete it. I can query the icinga2 API and find a host!service object for each host the service is applied for, but I cannot delete them, because they were not created with the icinga2 API, but with the director one.

Possible Solution

Maybe the documentation is missing some hint about how to handle this. This was also requested in #1468 last year, but I haven't found any update.

Your Environment

cschneemann commented 5 years ago

As written in the documentation you have to fetch all applyrules and compare the names of the objects in the list you get with the name of the rule you are looking for, then you have to work with its id.

Here some parts from my python code:

url = "https://icinga2host/icingaweb2/director"
headers = {
       'Accept': 'application/json',
       'X-HTTP-Method-Override': 'POST'
      }
auth = ("admin", "admin")
r = request.post(url+"/service/applyrule", headers, auth)
for object in r.json()['objects']:
    if object['object_name'] == "apply rule Im looking for":
        id = object['id']

Then you can edit/delete the rule using url+"/service?id="+id

ejfv commented 4 years ago

Hi, It's true that we can use serviceapplyrules to see it but How can creeate it? It's seems that serviceapplyrules don't work for create

Best regards, Emilio J.

VGerris commented 7 months ago

You call the service api and use the JSON you get by doing a call to see it, e.g. (Ansible snippet ):

- name: Create service_rule for service_template_check_some_rule
  ansible.builtin.uri:
    url: https://{{ inventory_hostname }}/icingaweb2/director/service
    url_username: admin
    url_password: admin
    body_format: json
    body: {
      "assign_filter": "host.vars.cluster_role=%22master%22",
      "imports": [
        "service_template_check_some_service"
      ],
      "object_name": "service_template_check_some_service_rule",
      "object_type": "apply",
      "vars": {
        "var1": "testdb",
        "var2": "something",
      }
    }
    headers:
      Accept: 'application/json'
    validate_certs: false
    force_basic_auth: true
    method: POST