Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
feat(api): add details endpoint property to Resource list API and rem…
Browse files Browse the repository at this point in the history
…ove detailsUrl (#8451)

* feat(api): add details endpoint property to Resource list API and remove detailsUrl

Resolve MON-5070

* fix(api): move loading of endpoints of resource from service to the controller in Resource list API

Resolve MON-5070

* fix(api): update routes of details endpoints of Resource list API

Resolve MON-5070

* fix(test): improve unit tests

Resolve MON-5070
  • Loading branch information
vhr authored Apr 1, 2020
1 parent 9997cf6 commit f60cdc9
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 73 deletions.
10 changes: 5 additions & 5 deletions config/packages/serializer/Centreon/Monitoring.Resource.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ Centreon\Domain\Monitoring\Resource:
groups:
- 'resource_main'
- 'resource_parent'
detailsUrl:
type: string
groups:
- 'resource_main'
- 'resource_parent'
icon:
type: Centreon\Domain\Monitoring\Icon
groups:
Expand All @@ -64,6 +59,11 @@ Centreon\Domain\Monitoring\Resource:
type: string
groups:
- 'resource_main'
detailsEndpoint:
type: string
groups:
- 'resource_main'
- 'resource_parent'
acknowledgementEndpoint:
type: string
groups:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,28 @@ public function list(
->findResources($filter);

foreach ($resources as $resource) {
// set paths to endpoints
$routeNameAcknowledgement = 'centreon_application_acknowledgement_addhostacknowledgement';
$routeNameDowntime = 'monitoring.downtime.addHostDowntime';
$routeNameDetails = 'centreon_application_monitoring_resource_details_host';

$parameters = [
'hostId' => $resource->getId(),
];

if ($resource->getType() === Resource::TYPE_SERVICE && $resource->getParent()) {
$routeNameAcknowledgement = 'centreon_application_acknowledgement_addserviceacknowledgement';
$routeNameDowntime = 'monitoring.downtime.addServiceDowntime';
$routeNameDetails = 'centreon_application_monitoring_resource_details_service';

$parameters['hostId'] = $resource->getParent()->getId();
$parameters['serviceId'] = $resource->getId();
}

$resource->setAcknowledgementEndpoint($this->router->generate($routeNameAcknowledgement, $parameters));
$resource->setDowntimeEndpoint($this->router->generate($routeNameDowntime, $parameters));
$resource->setDetailsEndpoint($this->router->generate($routeNameDetails, $parameters));

if ($resource->getParent() != null) {
$parameters = [
'hostId' => $resource->getParent()->getId(),
Expand All @@ -183,6 +205,14 @@ public function list(
$parameters
)
);

$resource->getParent()
->setDetailsEndpoint($this->router->generate(
'centreon_application_monitoring_resource_details_host',
[
'hostId' => $resource->getParent()->getId(),
]
));
}
}

Expand Down
48 changes: 24 additions & 24 deletions src/Centreon/Domain/Monitoring/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ class Resource
*/
private $name;

/**
* @var string|null
*/
private $detailsUrl;

/**
* @var \Centreon\Domain\Monitoring\Icon|null
*/
Expand Down Expand Up @@ -102,6 +97,11 @@ class Resource
*/
private $acknowledgementEndpoint;

/**
* @var string|null
*/
private $detailsEndpoint;

/**
* @var string|null
*/
Expand Down Expand Up @@ -261,25 +261,6 @@ public function setName(?string $name): self
return $this;
}

/**
* @return string|null
*/
public function getDetailsUrl(): ?string
{
return $this->detailsUrl;
}

/**
* @param string|null $detailsUrl
* @return \Centreon\Domain\Monitoring\Resource
*/
public function setDetailsUrl(?string $detailsUrl): self
{
$this->detailsUrl = $detailsUrl ?: null;

return $this;
}

/**
* @return \Centreon\Domain\Monitoring\Icon|null
*/
Expand Down Expand Up @@ -413,6 +394,25 @@ public function setAcknowledgementEndpoint(string $acknowledgementEndpoint): sel
return $this;
}

/**
* @return string|null
*/
public function getDetailsEndpoint(): ?string
{
return $this->detailsEndpoint;
}

/**
* @param string|null $detailsEndpoint
* @return \Centreon\Domain\Monitoring\Resource
*/
public function setDetailsEndpoint(?string $detailsEndpoint): self
{
$this->detailsEndpoint = $detailsEndpoint ?: null;

return $this;
}

/**
* @return string|null
*/
Expand Down
30 changes: 1 addition & 29 deletions src/Centreon/Domain/Monitoring/ResourceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,16 @@ class ResourceService extends AbstractCentreonService implements ResourceService
*/
private $accessGroupRepository;

/**
* @var UrlGeneratorInterface
*/
private $router;

/**
* @param ResourceRepositoryInterface $resourceRepository
* @param AccessGroupRepositoryInterface $accessGroupRepository
* @param UrlGeneratorInterface $router
*/
public function __construct(
ResourceRepositoryInterface $resourceRepository,
AccessGroupRepositoryInterface $accessGroupRepository,
UrlGeneratorInterface $router
AccessGroupRepositoryInterface $accessGroupRepository
) {
$this->resourceRepository = $resourceRepository;
$this->accessGroupRepository = $accessGroupRepository;
$this->router = $router;
}

/**
Expand Down Expand Up @@ -102,26 +94,6 @@ public function findResources(ResourceFilter $filter): array
throw new ResourceException('Error while searching for resources', 0, $ex);
}

// set paths to endpoints
foreach ($list as $resource) {
$routeNameAcknowledgement = 'centreon_application_acknowledgement_addhostacknowledgement';
$routeNameDowntime = 'monitoring.downtime.addHostDowntime';
$parameters = [
'hostId' => $resource->getId(),
];

if ($resource->getType() === Resource::TYPE_SERVICE && $resource->getParent()) {
$routeNameAcknowledgement = 'centreon_application_acknowledgement_addserviceacknowledgement';
$routeNameDowntime = 'monitoring.downtime.addServiceDowntime';

$parameters['hostId'] = $resource->getParent()->getId();
$parameters['serviceId'] = $resource->getId();
}

$resource->setAcknowledgementEndpoint($this->router->generate($routeNameAcknowledgement, $parameters));
$resource->setDowntimeEndpoint($this->router->generate($routeNameDowntime, $parameters));
}

return $list;
}

Expand Down
10 changes: 2 additions & 8 deletions src/Centreon/Infrastructure/Monitoring/ResourceRepositoryRDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ final class ResourceRepositoryRDB extends AbstractRepositoryDRB implements Resou
'status_code' => 'resource.status_code',
'status' => 'resource.status_name',
'action_url' => 'resource.action_url',
'details_url' => 'resource.details_url',
'parent_name' => 'resource.parent_name',
'parent_status' => 'resource.parent_status_name',
'severity_level' => 'resource.severity_level',
Expand Down Expand Up @@ -255,11 +254,10 @@ public function findResources(ResourceFilter $filter): array

$collector = new StatementCollector();
$request = 'SELECT SQL_CALC_FOUND_ROWS '
. 'resource.id, resource.type, resource.name, '
. 'resource.details_url, resource.action_url, '
. 'resource.id, resource.type, resource.name, resource.action_url, '
. 'resource.status_code, resource.status_name, ' // status
. 'resource.icon_name, resource.icon_url, ' // icon
. 'resource.parent_id, resource.parent_name, resource.parent_details_url, ' // parent
. 'resource.parent_id, resource.parent_name, ' // parent
. 'resource.parent_icon_name, resource.parent_icon_url, ' // parent icon
. 'resource.parent_status_code, resource.parent_status_name, ' // parent status
. 'resource.severity_level, resource.severity_url, resource.severity_name, ' // severity
Expand Down Expand Up @@ -446,12 +444,10 @@ protected function prepareQueryForServiceResources(StatementCollector $collector
sh.host_id AS `host_id`,
s.description AS `name`,
s.action_url AS `action_url`,
s.notes_url AS `details_url`,
s.icon_image_alt AS `icon_name`,
s.icon_image AS `icon_url`,
sh.host_id AS `parent_id`,
sh.name AS `parent_name`,
sh.notes_url AS `parent_details_url`,
sh.icon_image_alt AS `parent_icon_name`,
sh.icon_image AS `parent_icon_url`,
sh.state AS `parent_status_code`,
Expand Down Expand Up @@ -599,12 +595,10 @@ protected function prepareQueryForHostResources(StatementCollector $collector, R
h.host_id AS `host_id`,
h.name AS `name`,
h.action_url AS `action_url`,
h.notes_url AS `details_url`,
h.icon_image_alt AS `icon_name`,
h.icon_image AS `icon_url`,
NULL AS `parent_id`,
NULL AS `parent_name`,
NULL AS `parent_details_url`,
NULL AS `parent_icon_name`,
NULL AS `parent_icon_url`,
NULL AS `parent_status_code`,
Expand Down
8 changes: 1 addition & 7 deletions tests/php/Centreon/Domain/Monitoring/ResourceServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
use Centreon\Domain\Monitoring\Resource;
use Centreon\Domain\Monitoring\ResourceFilter;
use Centreon\Domain\Security\Interfaces\AccessGroupRepositoryInterface;
use Centreon\Domain\Monitoring\Interfaces\ResourceServiceInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use PHPUnit\Framework\TestCase;

class ResourceServiceTest extends TestCase
Expand All @@ -47,12 +45,8 @@ public function testFindResources()
->willReturn([$resource]); // values returned for the all next tests

$accessGroup = $this->createMock(AccessGroupRepositoryInterface::class);
$router = $this->createMock(UrlGeneratorInterface::class);
$router->expects(self::any())
->method('generate')
->willReturn('/api-endpoint');

$resourceService = new ResourceService($repository, $accessGroup, $router);
$resourceService = new ResourceService($repository, $accessGroup);

$resourcesFound = $resourceService->findResources(new ResourceFilter());
$this->assertCount(
Expand Down

0 comments on commit f60cdc9

Please sign in to comment.