From 6ad88b04e2694d27017f67a45febb3d81fe4f1be Mon Sep 17 00:00:00 2001 From: Colin Gagnaire Date: Thu, 12 Mar 2020 19:14:40 +0100 Subject: [PATCH] fix(api): fix options retrieval issue (#8412) * Fixed a gorgonian issue due to a bad option class * Regeneration of the composer.lock Co-authored-by: Laurent Calvet --- .../Controller/GorgoneController.php | 6 +- src/Centreon/Domain/Gorgone/ActionLog.php | 1 + .../Domain/Gorgone/Command/BasicCommand.php | 10 ++- .../Domain/Gorgone/Command/CommandFactory.php | 3 +- .../Domain/Gorgone/Command/EmptyCommand.php | 1 + .../Command/Internal/ThumbprintCommand.php | 1 + .../Domain/Gorgone/ConfigurationLoader.php | 8 ++- .../Domain/Gorgone/GorgoneException.php | 2 + .../Domain/Gorgone/GorgoneService.php | 5 +- .../Gorgone/Interfaces/CommandInterface.php | 5 +- .../Interfaces/CommandRepositoryInterface.php | 1 + ...erface.php => GorgoneServiceInterface.php} | 3 +- .../Gorgone/Interfaces/ResponseInterface.php | 1 + .../ResponseRepositoryInterface.php | 1 + src/Centreon/Domain/Gorgone/Response.php | 31 ++++----- .../Interfaces/OptionRepositoryInterface.php | 1 + .../Interfaces/OptionServiceInterface.php | 1 + src/Centreon/Domain/Option/Option.php | 11 ++-- .../Domain/Option/OptionException.php | 27 ++++++++ src/Centreon/Domain/Option/OptionService.php | 1 + .../Gorgone/CommandRepositoryAPI.php | 1 + .../ConfigurationLoaderApiInterface.php | 11 +++- .../Gorgone/ResponseRepositoryAPI.php | 1 + .../Option/OptionRepositoryRDB.php | 1 + .../Domain/Gorgone/CommandServiceTest.php | 3 - .../configServers/popup/popup.php | 63 ++++++++++--------- 26 files changed, 135 insertions(+), 65 deletions(-) rename src/Centreon/Domain/Gorgone/Interfaces/{ServiceInterface.php => GorgoneServiceInterface.php} (97%) create mode 100644 src/Centreon/Domain/Option/OptionException.php diff --git a/src/Centreon/Application/Controller/GorgoneController.php b/src/Centreon/Application/Controller/GorgoneController.php index 18de9516173..8ab1ac182d3 100644 --- a/src/Centreon/Application/Controller/GorgoneController.php +++ b/src/Centreon/Application/Controller/GorgoneController.php @@ -23,7 +23,7 @@ use Centreon\Domain\Gorgone\Command\Internal\ThumbprintCommand; use Centreon\Domain\Gorgone\Interfaces\CommandInterface; -use Centreon\Domain\Gorgone\Interfaces\ServiceInterface; +use Centreon\Domain\Gorgone\Interfaces\GorgoneServiceInterface; use FOS\RestBundle\Controller\AbstractFOSRestController; use FOS\RestBundle\Controller\Annotations as Rest; use FOS\RestBundle\View\View; @@ -36,11 +36,11 @@ class GorgoneController extends AbstractFOSRestController { /** - * @var ServiceInterface + * @var GorgoneServiceInterface */ private $gorgoneService; - public function __construct(ServiceInterface $gorgoneService) + public function __construct(GorgoneServiceInterface $gorgoneService) { $this->gorgoneService = $gorgoneService; } diff --git a/src/Centreon/Domain/Gorgone/ActionLog.php b/src/Centreon/Domain/Gorgone/ActionLog.php index 01dd8be5ee6..6997968487d 100644 --- a/src/Centreon/Domain/Gorgone/ActionLog.php +++ b/src/Centreon/Domain/Gorgone/ActionLog.php @@ -1,4 +1,5 @@ monitoringInstanceId = $pollerId; + $this->bodyRequest = $bodyRequest; } /** diff --git a/src/Centreon/Domain/Gorgone/Command/CommandFactory.php b/src/Centreon/Domain/Gorgone/Command/CommandFactory.php index a9e154475e3..10519acc02c 100644 --- a/src/Centreon/Domain/Gorgone/Command/CommandFactory.php +++ b/src/Centreon/Domain/Gorgone/Command/CommandFactory.php @@ -1,4 +1,5 @@ GorgoneCommandInterface[] */ - static private $commands = []; + private static $commands = []; /** * @param CommandInterface $command Command to add diff --git a/src/Centreon/Domain/Gorgone/Command/EmptyCommand.php b/src/Centreon/Domain/Gorgone/Command/EmptyCommand.php index 7db83351ae9..8c0faacc45a 100644 --- a/src/Centreon/Domain/Gorgone/Command/EmptyCommand.php +++ b/src/Centreon/Domain/Gorgone/Command/EmptyCommand.php @@ -1,4 +1,5 @@ Parameters of the Gorgone server + * @var array Parameters of the Gorgone server */ private $gorgoneParameters; @@ -144,6 +145,8 @@ public function getCommandTimeout(): int /** * Loads configuration of the Gorgone server + * + * @throws \Exception */ private function loadConfiguration(): void { @@ -163,6 +166,7 @@ private function loadConfiguration(): void $this->isOptionsLoaded = true; } catch (\Exception $ex) { $this->isOptionsLoaded = false; + throw $ex; } } } diff --git a/src/Centreon/Domain/Gorgone/GorgoneException.php b/src/Centreon/Domain/Gorgone/GorgoneException.php index 6799c9e01c5..50d969589e2 100644 --- a/src/Centreon/Domain/Gorgone/GorgoneException.php +++ b/src/Centreon/Domain/Gorgone/GorgoneException.php @@ -1,4 +1,5 @@ actionLogs = []; + $rawResponse = $this->responseRepository->getResponse($this->command); + $jsonResponse = json_decode($rawResponse, true); + $this->error = $jsonResponse['error'] ?? null; + $this->token = (string) $jsonResponse['token']; + + if ($this->error === null) { + foreach ($jsonResponse['data'] as $key => $responseData) { + $this->actionLogs[$key] = ActionLog::create($responseData); + } + } + $this->message = ((string) $jsonResponse['message'] ?? null); return $this->actionLogs; } @@ -131,20 +145,7 @@ public function getActionLogs(): array */ public function getLastActionLog(): ?ActionLog { - if (empty($this->actionLogs)) { - $rawResponse = $this->responseRepository->getResponse($this->command); - $jsonResponse = json_decode($rawResponse, true); - $this->error = $jsonResponse['error'] ?? null; - $this->token = (string) $jsonResponse['token']; - - if ($this->error === null) { - $this->actionLogs = []; - foreach ($jsonResponse['data'] as $key => $responseData) { - $this->actionLogs[$key] = ActionLog::create($responseData); - } - } - $this->message = ((string) $jsonResponse['message'] ?? null); - } + $this->getActionLogs(); return $this->actionLogs[count($this->actionLogs) - 1] ?? null; } diff --git a/src/Centreon/Domain/Option/Interfaces/OptionRepositoryInterface.php b/src/Centreon/Domain/Option/Interfaces/OptionRepositoryInterface.php index 5d47def552a..624ed467fe1 100644 --- a/src/Centreon/Domain/Option/Interfaces/OptionRepositoryInterface.php +++ b/src/Centreon/Domain/Option/Interfaces/OptionRepositoryInterface.php @@ -1,4 +1,5 @@ value; } /** - * @param string $value + * @param string|null $value * @return Option * @see Option::$value */ - public function setValue(string $value): Option + public function setValue(?string $value): Option { $this->value = $value; return $this; diff --git a/src/Centreon/Domain/Option/OptionException.php b/src/Centreon/Domain/Option/OptionException.php new file mode 100644 index 00000000000..d8cb6fca20b --- /dev/null +++ b/src/Centreon/Domain/Option/OptionException.php @@ -0,0 +1,27 @@ +send($thumbprintCommand); - // check if we have log for 30 s every 2s - do { - $lastActionLog = $gorgoneResponse->getLastActionLog(); - sleep(2); - $timeout += 2; - } while ( - ($lastActionLog == null || $lastActionLog->getCode() === \Centreon\Domain\Gorgone\Response::STATUS_BEGIN) && - $timeout <= 30 - ); +try { + foreach ($parents as $serverId) { + $lastActionLog = null; + $thumbprintCommand = new \Centreon\Domain\Gorgone\Command\Internal\ThumbprintCommand($serverId); + $gorgoneResponse = $gorgoneService->send($thumbprintCommand); + // check if we have log for 30 s every 2s + do { + $lastActionLog = $gorgoneResponse->getLastActionLog(); + sleep(2); + $timeout += 2; + } while ( + ($lastActionLog == null || $lastActionLog->getCode() === \Centreon\Domain\Gorgone\Response::STATUS_BEGIN) && + $timeout <= 30 + ); - if ($timeout > 30) { - // add 10 s for the next server - $timeout -= 10; - $gorgoneError = true; - $dataError .= ' - - error : TimeOut error for poller ' . $serverId . ' We can\'t get log'; - continue; - } + if ($timeout > 30) { + // add 10 s for the next server + $timeout -= 10; + $gorgoneError = true; + $dataError .= ' + - error : TimeOut error for poller '.$serverId.' We can\'t get log'; + continue; + } - $thummprintReponse = json_decode($lastActionLog->getData(), true); - if ($lastActionLog->getCode() === \Centreon\Domain\Gorgone\Response::STATUS_OK) { - $thumbprints .= ' - - key: ' . $thummprintReponse['data']['thumbprint']; - } else { - $gorgoneError = true; - $dataError .= ' - - error : Poller ' . $serverId . ' : ' . $thummprintReponse['message']; + $thummprintReponse = json_decode($lastActionLog->getData(), true); + if ($lastActionLog->getCode() === \Centreon\Domain\Gorgone\Response::STATUS_OK) { + $thumbprints .= ' + - key: '.$thummprintReponse['data']['thumbprint']; + } else { + $gorgoneError = true; + $dataError .= ' + - error : Poller '.$serverId.' : '.$thummprintReponse['message']; + } } +} catch (\Exception $ex) { + $gorgoneError = true; + $dataError = $ex->getMessage(); } if (!empty($dataError)) {