diff --git a/src/Centreon/Domain/Repository/CfgCentreonBrokerRepository.php b/src/Centreon/Domain/Repository/CfgCentreonBrokerRepository.php index 26527129984..261a8f3b22f 100644 --- a/src/Centreon/Domain/Repository/CfgCentreonBrokerRepository.php +++ b/src/Centreon/Domain/Repository/CfgCentreonBrokerRepository.php @@ -35,7 +35,7 @@ public function findCentralBrokerConfigId(): int if ($row = $stmt->fetch()) { $configId = $row['config_id']; } else { - throw new NotFoundException('Central broker config id not found'); + throw new NotFoundException(_('Central broker config id not found')); } return $configId; @@ -68,7 +68,7 @@ public function findBrokerConfigIdByPollerId(int $pollerId): int if ($row = $stmt->fetch()) { $configId = $row['config_id']; } else { - throw new NotFoundException('Poller broker config id not found'); + throw new NotFoundException(_('Poller broker config id not found')); } return $configId; diff --git a/src/CentreonRemote/Application/Webservice/CentreonConfigurationRemote.php b/src/CentreonRemote/Application/Webservice/CentreonConfigurationRemote.php index 9b1f53ea9f5..c200abe55d8 100755 --- a/src/CentreonRemote/Application/Webservice/CentreonConfigurationRemote.php +++ b/src/CentreonRemote/Application/Webservice/CentreonConfigurationRemote.php @@ -239,6 +239,8 @@ public function postLinkCentreonRemoteServer(): array $serverConfigurationService->setName($serverName); $serverConfigurationService->setOpenBrokerFlow($openBrokerFlow); + $pollerConfigurationService->setOpenBrokerFlow($openBrokerFlow); + // set linked pollers $pollerConfigurationBridge->collectDataFromRequest(); @@ -258,8 +260,7 @@ public function postLinkCentreonRemoteServer(): array $serverConfigurationService->isLinkedToCentralServer(); } - $serverID = $serverConfigurationService->insert(); - $pollerConfigurationBridge->setServerID($serverID); + $serverId = $serverConfigurationService->insert(); } catch (\Exception $e) { return ['error' => true, 'message' => $e->getMessage()]; } @@ -268,7 +269,7 @@ public function postLinkCentreonRemoteServer(): array if ($isRemoteConnection) { $taskId = null; - $remoteServer = $pollerConfigurationBridge->getRemoteServerForConfiguration(); + $remoteServer = $pollerConfigurationBridge->getPollerFromId($serverId); // set basic parameters to export task $params = [ @@ -280,11 +281,10 @@ public function postLinkCentreonRemoteServer(): array // If you want to link pollers to a remote if ($pollerConfigurationBridge->hasPollersForUpdating()) { - $pollerServers = $pollerConfigurationBridge->getLinkedPollersSelectedForUpdate(); - $pollerConfigurationService->setOpenBrokerFlow($openBrokerFlow); - $pollerConfigurationService->setPollersConfigurationWithServer($pollerServers, $remoteServer); + $pollers = $pollerConfigurationBridge->getLinkedPollersSelectedForUpdate(); + $pollerConfigurationService->linkPollersToParentPoller($pollers, $remoteServer); - foreach ($pollerServers as $poller){ + foreach ($pollers as $poller) { $params['pollers'][] = $poller->getId(); } } @@ -296,12 +296,11 @@ public function postLinkCentreonRemoteServer(): array $this->addServerToListOfRemotes($serverIP, $centreonPath); $this->setCentreonInstanceAsCentral(); - // if it is poller wizard and poller is linked to a another poller/remote server (instead of central) + // if it is poller wizard and poller is linked to another poller/remote server (instead of central) } elseif ($pollerConfigurationBridge->hasPollersForUpdating()) { - $remoteServer = $pollerConfigurationBridge->getRemoteServerForConfiguration(); - $pollerServers = $pollerConfigurationBridge->getLinkedPollersSelectedForUpdate(); - $pollerConfigurationService->setOpenBrokerFlow($openBrokerFlow); - $pollerConfigurationService->setPollersConfigurationWithServer($pollerServers, $remoteServer); + $pollers = [$pollerConfigurationBridge->getPollerFromId($serverId)]; + $parentPoller = $pollerConfigurationBridge->getLinkedPollersSelectedForUpdate()[0]; + $pollerConfigurationService->linkPollersToParentPoller($pollers, $parentPoller); } return ['success' => true, 'task_id' => $taskId]; diff --git a/src/CentreonRemote/Domain/Service/ConfigurationWizard/LinkedPollerConfigurationService.php b/src/CentreonRemote/Domain/Service/ConfigurationWizard/LinkedPollerConfigurationService.php index b95913d302d..dc6425e95fa 100644 --- a/src/CentreonRemote/Domain/Service/ConfigurationWizard/LinkedPollerConfigurationService.php +++ b/src/CentreonRemote/Domain/Service/ConfigurationWizard/LinkedPollerConfigurationService.php @@ -96,10 +96,12 @@ public function setOpenBrokerFlow($openBrokerFlow) } /** + * Link a set of pollers to a parent poller by creating broker input/output + * * @param PollerServer[] $pollers * @param PollerServer $remote */ - public function setPollersConfigurationWithServer(array $pollers, PollerServer $remote) + public function linkPollersToParentPoller(array $pollers, PollerServer $remote) { $pollerIds = array_map(function ($poller) { return $poller->getId(); diff --git a/src/CentreonRemote/Domain/Service/ConfigurationWizard/PollerConfigurationRequestBridge.php b/src/CentreonRemote/Domain/Service/ConfigurationWizard/PollerConfigurationRequestBridge.php index 4bfad192d4b..dc3295583b6 100644 --- a/src/CentreonRemote/Domain/Service/ConfigurationWizard/PollerConfigurationRequestBridge.php +++ b/src/CentreonRemote/Domain/Service/ConfigurationWizard/PollerConfigurationRequestBridge.php @@ -16,19 +16,12 @@ class PollerConfigurationRequestBridge /** @var PollerServer[] */ private $pollers = []; - private $serverID = null; - public function __construct(Container $di) { $this->dbAdapter = $di['centreon.db-manager']->getAdapter('configuration_db'); } - public function setServerID($serverID) - { - $this->serverID = $serverID; - } - public function hasPollersForUpdating(): bool { return !empty($this->pollers); @@ -42,16 +35,19 @@ public function getLinkedPollersSelectedForUpdate(): array return $this->pollers; } - public function collectDataFromRequest() + /** + * Set linked pollers regarding wizard type (poller/remote server) + */ + public function collectDataFromRequest(): void { - $isRemoteConnection = (new ServerWizardIdentity)->requestConfigurationIsRemote(); + $isRemoteServerWizard = (new ServerWizardIdentity)->requestConfigurationIsRemote(); - if ($isRemoteConnection) { // configure remote server - $linkedPollers = $_POST['linked_pollers'] ?? ''; // pollers linked to the remote server - $linkedPollers = (array) $linkedPollers; + if ($isRemoteServerWizard) { // configure remote server + // pollers linked to the remote server + $linkedPollers = isset($_POST['linked_pollers']) ? (array) $_POST['linked_pollers'] : []; } else { // configure poller - $remoteId = $_POST['linked_remote'] ?? ''; // if the poller is linked to a remote server - $linkedPollers = [$this->serverID]; + // if the poller is linked to a remote server + $linkedPollers = isset($_POST['linked_remote']) ? [$_POST['linked_remote']] : []; } $this->pollers = $this->getPollersToLink($linkedPollers); // set and instantiate linked pollers @@ -77,39 +73,34 @@ private function getPollersToLink(array $pollers) $idBindString = str_repeat('?,', count($pollerIDs)); $idBindString = rtrim($idBindString, ','); - $queryPollers = "SELECT id, name, ns_ip_address as ip FROM nagios_server WHERE id IN({$idBindString})"; - - try { - $this->dbAdapter->query($queryPollers, $pollerIDs); - $results = $this->dbAdapter->results(); - $data = []; + $queryPollers = "SELECT id, name, ns_ip_address as ip FROM nagios_server WHERE id IN ({$idBindString})"; - foreach ($results as $result) { - $poller = new PollerServer; - $poller->setId($result->id); - $poller->setName($result->name); - $poller->setIp($result->ip); + $this->dbAdapter->query($queryPollers, $pollerIDs); + $results = $this->dbAdapter->results(); + $data = []; - $data[] = $poller; - } + foreach ($results as $result) { + $poller = new PollerServer; + $poller->setId($result->id); + $poller->setName($result->name); + $poller->setIp($result->ip); - return $data; - } catch (\Exception $e) { - error_log($e->getMessage()); + $data[] = $poller; } + return $data; + return []; } - public function getRemoteServerForConfiguration(): ?PollerServer + /** + * Get poller information from poller id + */ + public function getPollerFromId(int $pollerId): ?PollerServer { - if (is_null($this->serverID)) { - return null; - } - $query = 'SELECT id, name, ns_ip_address as ip FROM nagios_server WHERE id = ?'; - $this->dbAdapter->query($query, [$this->serverID]); + $this->dbAdapter->query($query, [$pollerId]); $results = $this->dbAdapter->results(); if (count($results)) { diff --git a/src/CentreonRemote/Domain/Service/ConfigurationWizard/PollerConnectionConfigurationService.php b/src/CentreonRemote/Domain/Service/ConfigurationWizard/PollerConnectionConfigurationService.php index a1e524bd939..080e32d93f7 100644 --- a/src/CentreonRemote/Domain/Service/ConfigurationWizard/PollerConnectionConfigurationService.php +++ b/src/CentreonRemote/Domain/Service/ConfigurationWizard/PollerConnectionConfigurationService.php @@ -43,7 +43,13 @@ public function setBrokerConfigurationService(BrokerConfigurationService $broker $this->brokerConfigurationService = $brokerConfigurationService; } - protected function insertConfigCentreonBroker($serverID) + /** + * Insert centreon broker configuration to a given poller + * this configuration i only for broker module (not cbd) + * + * @param int $serverID the poller id + */ + protected function insertConfigCentreonBroker(int $serverID): void { $configCentreonBrokerData = CfgCentreonBroker::getConfiguration($serverID, $this->name); $configCentreonBrokerInfoData = CfgCentreonBrokerInfo::getConfiguration($this->name, null, null); diff --git a/src/CentreonRemote/Domain/Service/ConfigurationWizard/RemoteConnectionConfigurationService.php b/src/CentreonRemote/Domain/Service/ConfigurationWizard/RemoteConnectionConfigurationService.php index f681dfe31bd..5a657dcf050 100644 --- a/src/CentreonRemote/Domain/Service/ConfigurationWizard/RemoteConnectionConfigurationService.php +++ b/src/CentreonRemote/Domain/Service/ConfigurationWizard/RemoteConnectionConfigurationService.php @@ -10,7 +10,7 @@ class RemoteConnectionConfigurationService extends ServerConnectionConfigurationService { - protected function insertConfigCentreonBroker($serverID) + protected function insertConfigCentreonBroker(int $serverID): void { $configCentreonBrokerData = CfgCentreonBroker::getConfiguration($serverID, $this->name); $configCentreonBrokerInfoData = CfgCentreonBrokerInfo::getConfiguration( diff --git a/src/CentreonRemote/Domain/Service/ConfigurationWizard/ServerConnectionConfigurationService.php b/src/CentreonRemote/Domain/Service/ConfigurationWizard/ServerConnectionConfigurationService.php index 73b2cc45811..2b3aa3e2d9d 100644 --- a/src/CentreonRemote/Domain/Service/ConfigurationWizard/ServerConnectionConfigurationService.php +++ b/src/CentreonRemote/Domain/Service/ConfigurationWizard/ServerConnectionConfigurationService.php @@ -40,7 +40,7 @@ public function __construct(CentreonDBAdapter $dbAdapter) $this->dbAdapter = $dbAdapter; } - abstract protected function insertConfigCentreonBroker($serverID); + abstract protected function insertConfigCentreonBroker(int $serverID): void; public function setServerIp($ip) { diff --git a/src/CentreonRemote/Domain/Service/NotifyMasterService.php b/src/CentreonRemote/Domain/Service/NotifyMasterService.php index 5d8a64768d2..c286ed9bfa1 100644 --- a/src/CentreonRemote/Domain/Service/NotifyMasterService.php +++ b/src/CentreonRemote/Domain/Service/NotifyMasterService.php @@ -3,8 +3,8 @@ namespace CentreonRemote\Domain\Service; use Centreon\Domain\Repository\InformationsRepository; +use Centreon\Infrastructure\Service\CentreonDBManagerService; use Curl\Curl; -use Pimple\Container; class NotifyMasterService { @@ -26,16 +26,31 @@ class NotifyMasterService const FAIL = 'fail'; /** - * @var Container + * @var CentreonDBManagerService */ - private $di; + private $dbManager; + + /** + * @var Curl + */ + private $curl; + + /** + * @return Curl + */ + public function setCurl(Curl $curl): void + { + $this->curl = $curl; + } /** * NotifyMasterService constructor. + * + * @param CentreonDBManagerService $dbManager */ - public function __construct(Container $di) + public function __construct(CentreonDBManagerService $dbManager) { - $this->di = $di; + $this->dbManager = $dbManager; } /** @@ -54,7 +69,7 @@ public function pingMaster($ip) } $url = "{$ip}/centreon/api/external.php?object=centreon_remote_server&action=addToWaitList"; - $repository = $this->getDi()['centreon.db-manager']->getRepository(InformationsRepository::class); + $repository = $this->dbManager->getRepository(InformationsRepository::class); $applicationKey = $repository->getOneByKey('appKey'); $version = $repository->getOneByKey('version'); @@ -70,8 +85,7 @@ public function pingMaster($ip) 'app_key' => $applicationKey->getValue(), 'version' => $version->getValue(), ]; - $curl = new Curl(); - $curl->post($url, $curlData); + $this->curl->post($url, $curlData); if ($curl->error) { switch ($curl->error_code) { @@ -103,9 +117,4 @@ public function pingMaster($ip) return ['status' => self::SUCCESS]; } - - private function getDi(): Container - { - return $this->di; - } } \ No newline at end of file diff --git a/src/CentreonRemote/ServiceProvider.php b/src/CentreonRemote/ServiceProvider.php index 471bf5d62f1..76f60d1a997 100644 --- a/src/CentreonRemote/ServiceProvider.php +++ b/src/CentreonRemote/ServiceProvider.php @@ -17,6 +17,7 @@ use CentreonRemote\Domain\Service\NotifyMasterService; use CentreonRemote\Domain\Service\TaskService; use CentreonRemote\Infrastructure\Service\PollerInteractionService; +use Curl\Curl; class ServiceProvider implements AutoloadServiceProviderInterface { @@ -39,7 +40,8 @@ public function register(Container $pimple): void $pimple['centreon.clapi']->add(Clapi\CentreonWorker::class); $pimple['centreon.notifymaster'] = function (Container $pimple): NotifyMasterService { - $service = new NotifyMasterService($pimple); + $service = new NotifyMasterService($pimple['centreon.db-manager']); + $service->setCurl(new Curl); return $service; };