Skip to content

Commit

Permalink
enh(host_categories): add search capabilities to host categories endp…
Browse files Browse the repository at this point in the history
…oint
  • Loading branch information
adr-mo committed May 24, 2024
1 parent 70ab22b commit 2d4fb12
Show file tree
Hide file tree
Showing 16 changed files with 626 additions and 269 deletions.
6 changes: 0 additions & 6 deletions centreon/config/routes/Centreon/monitoring/category.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,3 @@ centreon_application_monitoring_services_find_categories:
controller: >-
Core\Category\RealTime\Infrastructure\Api\FindServiceCategory\FindServiceCategoryController
condition: "request.attributes.get('version') >= 22.10"
centreon_application_monitoring_host_find_categories:
methods: "GET"
path: "/monitoring/hosts/categories"
controller: >-
Core\Category\RealTime\Infrastructure\Api\FindHostCategory\FindHostCategoryController
condition: "request.attributes.get('version') >= 22.10"
8 changes: 8 additions & 0 deletions centreon/doc/API/centreon-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ info:
+
# Updated features
+ Added host_category filter to /monitoring/hostgroups endpoint
+ Added new filters for the /monitoring/hosts/categories endpoint
+
# Information
All dates are in **ISO 8601** format
Expand Down Expand Up @@ -2159,6 +2160,13 @@ paths:
summary: "List all real-time host categories"
description: |
List of all host categories in real-time context
The available parameters to **search** / **sort_by** are:
* id
* name
* host_group.id
* host_group.name
responses:
"200":
description: "OK"
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,9 @@ public static function errorWhileRetrievingObject(): self
{
return new self(_('Error while retrieving a host category'));
}

public static function errorWhileRetrievingRealTimeHostCategories(\Throwable $exception): self
{
return new self(_('Error while searching host categories in real time context'), 0, $exception);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* Copyright 2005 - 2023 Centreon (https://www.centreon.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : contact@centreon.com
*
*/

declare(strict_types=1);

namespace Core\HostCategory\Application\Repository;

use Centreon\Domain\RequestParameters\Interfaces\RequestParametersInterface;
use Core\Tag\RealTime\Domain\Model\Tag;

interface ReadRealTimeHostCategoryRepositoryInterface
{
/**
* @param null|RequestParametersInterface $requestParameters
*
* @return Tag[]
*/
public function findAll(?RequestParametersInterface $requestParameters): array;

/**
* @param null|RequestParametersInterface $requestParameters
* @param int[] $accessGroupIds
*
* @return Tag[]
*/
public function findAllByAccessGroupIds(?RequestParametersInterface $requestParameters, array $accessGroupIds): array;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

/*
* Copyright 2005 - 2023 Centreon (https://www.centreon.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : contact@centreon.com
*
*/

declare(strict_types=1);

namespace Core\HostCategory\Application\UseCase\FindRealTimeHostCategories;

use Centreon\Domain\Contact\Interfaces\ContactInterface;
use Centreon\Domain\Log\LoggerTrait;
use Centreon\Domain\RequestParameters\Interfaces\RequestParametersInterface;
use Core\Application\Common\UseCase\ErrorResponse;
use Core\HostCategory\Application\Exception\HostCategoryException;
use Core\HostCategory\Application\Repository\ReadHostCategoryRepositoryInterface;
use Core\HostCategory\Application\Repository\ReadRealTimeHostCategoryRepositoryInterface;
use Core\Security\AccessGroup\Application\Repository\ReadAccessGroupRepositoryInterface;
use Core\Security\AccessGroup\Domain\Model\AccessGroup;
use Core\Tag\RealTime\Domain\Model\Tag;

final class FindRealTimeHostCategories
{
use LoggerTrait;

/**
* @param ContactInterface $user
* @param ReadRealTimeHostCategoryRepositoryInterface $repository
* @param ReadHostCategoryRepositoryInterface $configurationRepository
* @param RequestParametersInterface $requestParameters
* @param ReadAccessGroupRepositoryInterface $accessGroupRepository
*/
public function __construct(
private readonly ContactInterface $user,
private readonly ReadRealTimeHostCategoryRepositoryInterface $repository,
private readonly ReadHostCategoryRepositoryInterface $configurationRepository,
private readonly RequestParametersInterface $requestParameters,
private readonly ReadAccessGroupRepositoryInterface $accessGroupRepository
) {
}

/**
* @param FindRealTimeHostCategoriesPresenterInterface $presenter
*/
public function __invoke(FindRealTimeHostCategoriesPresenterInterface $presenter): void
{
$this->info('Find service categories', ['user_id' => $this->user->getId()]);

try {
$serviceCategories = $this->user->isAdmin()
? $this->findHostCategoriesAsAdmin()
: $this->findHostCategoriesAsUser();

$presenter->presentResponse($this->createResponse($serviceCategories));
} catch (\Throwable $exception) {
$presenter->presentResponse(
new ErrorResponse(HostCategoryException::errorWhileRetrievingRealTimeHostCategories($exception))
);
$this->error($exception->getMessage(), ['trace' => $exception->getTraceAsString()]);
}
}

/**
* @param Tag[] $serviceCategories
*
* @return FindRealTimeHostCategoriesResponse
*/
private function createResponse(array $serviceCategories): FindRealTimeHostCategoriesResponse
{
return new FindRealTimeHostCategoriesResponse($serviceCategories);
}

/**
* @return Tag[]
*/
private function findHostCategoriesAsUser(): array
{
$categories = [];

$this->debug(
'User is not admin, use ACLs to retrieve service categories',
['user' => $this->user->getName()]
);

$accessGroupIds = array_map(
static fn (AccessGroup $accessGroup): int => $accessGroup->getId(),
$this->accessGroupRepository->findByContact($this->user)
);

if ($accessGroupIds === []) {
return $categories;
}

// If the current user has ACL filter on Host Categories it means that not all categories are visible so
// we need to apply the ACL
if ($this->configurationRepository->hasAclFilterOnHostCategories($accessGroupIds)) {
return $this->repository->findAllByAccessGroupIds(
$this->requestParameters,
$accessGroupIds,
);
}

$this->debug(
'No ACL filter found on service categories for user. Retrieving all service categories',
['user' => $this->user->getName()]
);

return $this->repository->findAll($this->requestParameters);
}

/**
* @return Tag[]
*/
private function findHostCategoriesAsAdmin(): array
{
return $this->repository->findAll($this->requestParameters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@

declare(strict_types=1);

namespace Core\Category\RealTime\Application\UseCase\FindHostCategory;
namespace Core\HostCategory\Application\UseCase\FindRealTimeHostCategories;

use Core\Application\Common\UseCase\PresenterInterface;
use Core\Application\Common\UseCase\ResponseStatusInterface;

interface FindHostCategoryPresenterInterface extends PresenterInterface
interface FindRealTimeHostCategoriesPresenterInterface extends PresenterInterface
{
/**
* @param FindRealTimeHostCategoriesResponse|ResponseStatusInterface $response
*/
public function presentResponse(FindRealTimeHostCategoriesResponse|ResponseStatusInterface $response): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

declare(strict_types=1);

namespace Core\Category\RealTime\Application\UseCase\FindHostCategory;
namespace Core\HostCategory\Application\UseCase\FindRealTimeHostCategories;

use Core\Tag\RealTime\Application\UseCase\FindTag\FindTagResponse;

final class FindHostCategoryResponse extends FindTagResponse
final class FindRealTimeHostCategoriesResponse extends FindTagResponse
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,27 @@

declare(strict_types=1);

namespace Core\Category\RealTime\Infrastructure\Api\FindHostCategory;
namespace Core\HostCategory\Infrastructure\API\FindRealTimeHostCategories;

use Centreon\Application\Controller\AbstractController;
use Core\Category\RealTime\Application\UseCase\FindHostCategory\FindHostCategory;
use Core\HostCategory\Application\UseCase\FindRealTimeHostCategories\FindRealTimeHostCategories;
use Core\HostCategory\Application\UseCase\FindRealTimeHostCategories\FindRealTimeHostCategoriesPresenterInterface;

final class FindHostCategoryController extends AbstractController
final class FindRealTimeHostCategoriesController extends AbstractController
{
/**
* @param FindHostCategory $useCase
* @param FindHostCategoryPresenter $presenter
* @param FindRealTimeHostCategories $useCase
* @param FindRealTimeHostCategoriesPresenterInterface $presenter
*
* @return object
*/
public function __invoke(FindHostCategory $useCase, FindHostCategoryPresenter $presenter): object
{
public function __invoke(
FindRealTimeHostCategories $useCase,
FindRealTimeHostCategoriesPresenterInterface $presenter
): object {
/**
* Access denied if no rights given to the realtime for the current user.
*/
$this->denyAccessUnlessGrantedForApiRealtime();

$useCase($presenter);
Expand Down
Loading

0 comments on commit 2d4fb12

Please sign in to comment.