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
414 stars 202 forks source link

Error: Trying to recreate icinga_host when trying to update existing objects using API #2001

Closed sol1-matt closed 10 months ago

sol1-matt commented 4 years ago

Expected Behavior

POST or PUT on and existing object will update the object as per the documentation

https://github.com/Icinga/icingaweb2-module-director/blob/master/doc/70-REST-API.md

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

Current Behavior

POST or PUT on and existing object give the error "trying to recreate icinga_host"

Possible Solution

Steps to Reproduce (for bugs)

setup a director user and password and configure the sample curl shell script

-bash-4.2# cat ./icinga_director_api.sh
METHOD=$1
URL=$2
BODY="$3"
USERNAME="<director_user>"
PASSWORD="<director_password>"
test -z "$PASSWORD" || USERNAME="$USERNAME:$PASSWORD"

test -z "$BODY" && curl -u "$USERNAME" \
  -i https://<domain_here>/icingaweb2/$URL \
  -H 'Accept: application/json' \
  -X $METHOD

test -z "$BODY" || curl -u "$USERNAME" \
  -i https://<domain_here>/icingaweb2/$URL \
  -H 'Accept: application/json' \
  -X $METHOD \
  -d "$BODY"

echo

create a host

-bash-4.2# ./icinga_director_api.sh POST director/host '{ "object_name":"test1","object_type": "object"}'
HTTP/1.1 201 Created
Date: Wed, 30 Oct 2019 05:10:45 GMT
Server: Apache/2.4.29 (Ubuntu)
Content-Length: 60
Content-Type: application/json

{
    "object_name": "test1",
    "object_type": "object"
}

try and update the host (should update, it fails instead)

-bash-4.2# ./icinga_director_api.sh POST director/host '{ "object_name":"test1","object_type": "object", "address": "127.0.0.1"}'
HTTP/1.1 422 Unprocessable Entity
Date: Wed, 30 Oct 2019 05:10:47 GMT
Server: Apache/2.4.29 (Ubuntu)
Content-Length: 62
Content-Type: application/json

{
    "error": "Trying to recreate icinga_host (\"test1\")"
}

try and recreate the host (should create a clean host)

-bash-4.2# ./icinga_director_api.sh PUT director/host '{ "object_name":"test1","object_type": "object", "address": "127.0.0.1"}'
HTTP/1.1 422 Unprocessable Entity
Date: Wed, 30 Oct 2019 05:11:03 GMT
Server: Apache/2.4.29 (Ubuntu)
Content-Length: 62
Content-Type: application/json

{
    "error": "Trying to recreate icinga_host (\"test1\")"
}

Your Environment

Current workarounds

We have seens this happen in bash scripts, ansible and director automation from netbox (https://github.com/digitalocean/icingaweb2-module-netboximport). The the director automation from netbox can update host objects but other objects and templates get the same error message.

The work around for bash was to delete the object and recreate it each time. The work around for ansible has been to ignore the error and assume the object is correct. The work around for netbox automation has been to delete all objects that have already been created so they can be recreated and new ones added.

NeverUsedID commented 4 years ago

Same problem here: Iam trying to add Hosts from vspheredb and the sync rule wants to create a new host, even its already existing.

This Sync Rule failed when last checked at 2019-10-31 10:46:49: Exception while syncing Icinga\Module\Director\Objects\IcingaHost some_host_name: Trying to recreate icinga_host ("some_host_name")

The host exists in the director and is not coming from some local configfiles. If i click preview, i can clearly see that he want to create the host instead of ignoring/modifying it. Only one host is merged/replaced in my example, all others are created instead of modified.

My Environment

Director version (System - About): 1.7.0 Icinga Web 2 version and modules (System - About): 2.7.2 Icinga 2 version (icinga2 --version): r2.11.0-1 Operating System and version: Ubuntu 16.04.6 LTS Webserver, PHP versions: Apache/2.4.41 (Ubuntu), PHP 7.3.10-1+ubuntu16.04.1+deb.sury.org+1

Thomas-Gelf commented 4 years ago

@sol1-matt: please append ?name=<hostname> to your URL for now, it should then work as expected. I see that the documentation optionally suggests that skipping this should also work, so I need to figure out whether the documentation is wrong or when/how this behaviour has been changed.

@NeverUsedID: your problem is not related to this one. Please create a dedicated issue with a snaphot of a Configuration Basket containing this Import Source and Sync Rule definition.

sol1-matt commented 4 years ago

thanks @Thomas-Gelf this works for the command line

-bash-4.2# ./icinga_director_api.sh POST director/host?name=test1 '{ "object_type": "object", "address": "123.123.123.123" }'
HTTP/1.1 200 OK
Date: Tue, 26 Nov 2019 04:02:40 GMT
Server: Apache/2.4.29 (Ubuntu)
Content-Length: 94
Content-Type: application/json

{
    "address": "123.123.123.123",
    "object_name": "test1",
    "object_type": "object"
}