From e93f5ab694ce8d7e6d98f66bab97ba560ee54a2f Mon Sep 17 00:00:00 2001
From: loiclau <loic.lau@gmail.com>
Date: Mon, 5 Mar 2018 11:25:02 +0100
Subject: [PATCH] fix(clapi) link host with default poller if unknown poller
 (#6099)

---
 lib/Centreon/Object/Instance/Instance.php       | 12 ++++++++++++
 www/class/centreon-clapi/centreonHost.class.php |  9 ++++++++-
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/lib/Centreon/Object/Instance/Instance.php b/lib/Centreon/Object/Instance/Instance.php
index 0e45909149..bacfa8c901 100644
--- a/lib/Centreon/Object/Instance/Instance.php
+++ b/lib/Centreon/Object/Instance/Instance.php
@@ -45,4 +45,16 @@ class Centreon_Object_Instance extends Centreon_Object
     protected $table = "nagios_server";
     protected $primaryKey = "id";
     protected $uniqueLabelField = "name";
+
+    public function getDefaultInstance()
+    {
+        $res = $this->db->query("SELECT `name` FROM `nagios_server` WHERE `is_default` = 1");
+        if( $res->rowCount() == 0) {
+            $res = $this->db->query("SELECT `name` FROM `nagios_server` WHERE `localhost` = '1'");
+        }
+
+        $row = $res->fetch();
+        return $row['name'];
+    }
+
 }
diff --git a/www/class/centreon-clapi/centreonHost.class.php b/www/class/centreon-clapi/centreonHost.class.php
index e8a62ad376..e1f90c89da 100644
--- a/www/class/centreon-clapi/centreonHost.class.php
+++ b/www/class/centreon-clapi/centreonHost.class.php
@@ -257,7 +257,14 @@ class CentreonHost extends CentreonObject
             if ($instanceName) {
                 $tmp = $instanceObject->getIdByParameter($instanceObject->getUniqueLabelField(), $instanceName);
                 if (!count($tmp)) {
-                    throw new CentreonClapiException(self::OBJECT_NOT_FOUND . ":" . $instanceName);
+                    $defaultInstanceName = $instanceObject->getDefaultInstance();
+                    $tmp = $instanceObject->getIdByParameter(
+                        $instanceObject->getUniqueLabelField(),
+                        $defaultInstanceName
+                    );
+                    if (!count($tmp)) {
+                        throw new CentreonClapiException(self::OBJECT_NOT_FOUND . " :" . $instanceName);
+                    }
                 }
                 $instanceId = $tmp[0];
             } else {
-- 
GitLab