From a23a76a921f07e4420ad870ac1e4152f28cfecd9 Mon Sep 17 00:00:00 2001 From: loiclau Date: Tue, 5 Mar 2019 16:08:35 +0100 Subject: [PATCH] feat(API) Add showinstance CLAPI command to Host (#7199) --- doc/en/api/clapi/objects/hosts.rst | 8 ++ doc/fr/api/clapi/objects/hosts.rst | 8 ++ .../rest_api/rest_api.postman_collection.json | 82 ++++++++++++++++++- .../centreon-clapi/centreonHost.class.php | 35 ++++++++ 4 files changed, 131 insertions(+), 2 deletions(-) diff --git a/doc/en/api/clapi/objects/hosts.rst b/doc/en/api/clapi/objects/hosts.rst index 61c69d0b424..52bbcf02338 100644 --- a/doc/en/api/clapi/objects/hosts.rst +++ b/doc/en/api/clapi/objects/hosts.rst @@ -314,6 +314,14 @@ In order to set the instance from which a host will be monitored, use the **SETI [root@centreon ~]# ./centreon -u admin -p centreon -o HOST -a setinstance -v "Centreon-Server;Poller 1" +Showinstance +----------- + +To determine the instance from which a host will be monitored, use the **SHOWINSTANCE** action:: + + [root@centreon ~]# ./centreon -u admin -p centreon -o HOST -a showinstance -v "Centreon-Server" + id;name + 2;Poller 1 Getmacro -------- diff --git a/doc/fr/api/clapi/objects/hosts.rst b/doc/fr/api/clapi/objects/hosts.rst index 9ff271193cb..1c4085df397 100644 --- a/doc/fr/api/clapi/objects/hosts.rst +++ b/doc/fr/api/clapi/objects/hosts.rst @@ -313,6 +313,14 @@ In order to set the instance from which a host will be monitored, use the **SETI [root@centreon ~]# ./centreon -u admin -p centreon -o HOST -a setinstance -v "Centreon-Server;Poller 1" +Showinstance +----------- + +To determine the instance from which a host will be monitored, use the **SHOWINSTANCE** action:: + + [root@centreon ~]# ./centreon -u admin -p centreon -o HOST -a showinstance -v "Centreon-Server" + id;name + 2;Poller 1 Getmacro -------- diff --git a/tests/rest_api/rest_api.postman_collection.json b/tests/rest_api/rest_api.postman_collection.json index 272ed0c206c..5833e9c25d0 100644 --- a/tests/rest_api/rest_api.postman_collection.json +++ b/tests/rest_api/rest_api.postman_collection.json @@ -1,7 +1,7 @@ { "info": { - "_postman_id": "5882d474-6c79-41b4-8fd8-2b1e1e999702", - "name": "Centreon Web Rest API", + "_postman_id": "a0f59ed8-0da0-4aa8-b270-774fcaa83146", + "name": "Centreon Web Rest API copy", "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" }, "item": [ @@ -25006,6 +25006,84 @@ }, "response": [] }, + { + "name": "Showinstance", + "event": [ + { + "listen": "test", + "script": { + "id": "d98aa955-00be-475b-88cd-b6eb878571f3", + "exec": [ + "tests[\"Status code is 200\"] = responseCode.code === 200;", + "", + "var contentType = postman.getResponseHeader(\"Content-Type\");", + "tests[\"Content-Type is application/json\"] = contentType === \"application/json\";", + "", + "var jsonData = JSON.parse(responseBody);", + "var result= jsonData.result;", + "", + "var schema = {", + " \"$schema\": \"http://json-schema.org/draft-04/schema#\",", + " \"type\": \"array\",", + " \"items\": {", + " \"type\": \"object\",", + " \"properties\": {", + " \"id\": { \"type\": \"string\" },", + " \"name\": { \"type\": \"string\" }", + " },", + " \"additionalProperties\": false,", + " \"required \": [\"id\", \"name\"]", + " }", + "};", + "", + "tests[\"Validate schema\"] = tv4.validate(result, schema);", + "" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "POST", + "header": [ + { + "key": "Content-Type", + "value": "application/json" + }, + { + "key": "centreon-auth-token", + "value": "{{token}}" + } + ], + "body": { + "mode": "raw", + "raw": "{\n \"action\":\"showinstance\",\n \"object\":\"host\",\n \"values\": \"{{host_name}}\"\n}" + }, + "url": { + "raw": "http://{{url}}/centreon/api/index.php?action=action&object=centreon_clapi", + "protocol": "http", + "host": [ + "{{url}}" + ], + "path": [ + "centreon", + "api", + "index.php" + ], + "query": [ + { + "key": "action", + "value": "action" + }, + { + "key": "object", + "value": "centreon_clapi" + } + ] + } + }, + "response": [] + }, { "name": "Setmacro", "event": [ diff --git a/www/class/centreon-clapi/centreonHost.class.php b/www/class/centreon-clapi/centreonHost.class.php index a9a6d63afa5..ea6cfc5d26c 100644 --- a/www/class/centreon-clapi/centreonHost.class.php +++ b/www/class/centreon-clapi/centreonHost.class.php @@ -341,6 +341,41 @@ public function del($objectName) ); } + /** + * List instance (poller) for host + * + * @param string $parameters + * @throws CentreonClapiException + */ + public function showinstance($parameters) + { + $params = explode($this->delim, $parameters); + if ($parameters == '') { + throw new CentreonClapiException(self::MISSINGPARAMETER); + } + if (($hostId = $this->getObjectId($params[self::ORDER_UNIQUENAME])) != 0) { + $relObj = new \Centreon_Object_Relation_Instance_Host(); + $fields = array('id', 'name'); + $elements = $relObj->getMergedParameters( + $fields, + array(), + -1, + 0, + "host_name", + "ASC", + array('host_id' => $hostId), + 'AND' + ); + + echo 'id' . $this->delim . 'name' . "\n"; + foreach ($elements as $elem) { + echo $elem['id'] . $this->delim . $elem['name'] . "\n"; + } + } else { + throw new CentreonClapiException(self::OBJECT_NOT_FOUND . ":" . $params[self::ORDER_UNIQUENAME]); + } + } + /** * Tie host to instance (poller) *