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

Commit

Permalink
refacto #2
Browse files Browse the repository at this point in the history
  • Loading branch information
adr-mo committed Jul 21, 2021
1 parent e01cdb0 commit 76cf542
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,6 @@ public function massAcknowledgeResources(
Request $request,
SerializerInterface $serializer,
MassiveAcknowledgementValidatorInterface $massiveAcknowledgementValidator
// add interface
): View {
$this->denyAccessUnlessGrantedForApiRealtime();

Expand All @@ -719,29 +718,30 @@ public function massAcknowledgeResources(
throw new AcknowledgementException('Error when decoding your sent data');
}

/**
* Validate the payload
*/
// validate the payload sent
$massiveAcknowledgementValidator->validateOrFail($acknowledgementPayload);

/**
* @var AckRequest $ackRequest
*/
$ackRequest = $serializer->deserialize(
(string) $request->getContent(),
$payload,
AckRequest::class,
'json'
);

//validate acknowledgement
// Get acknowledgement entity
$acknowledgement = $ackRequest->getAcknowledgement();

// set default values [sticky, persistent_comment] to true
$acknowledgement->setSticky(true);
$acknowledgement->setPersistentComment(true);

foreach ($ackRequest->getMonitoringResources() as $monitoringResource) {
// start acknowledgement process
/**
* Start the acknowledgement process.
* Failed acknowledgements are passed to avoid others to fail.
*/
try {
if ($this->hasAckRightsForResource($contact, $monitoringResource)) {
if (!$contact->isAdmin() && !$contact->hasRole(Contact::ROLE_SERVICE_ACKNOWLEDGEMENT)) {
Expand Down
40 changes: 20 additions & 20 deletions src/Centreon/Domain/Acknowledgement/AcknowledgementService.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,51 +417,51 @@ public function disacknowledgeResource(MonitoringResource $resource, Acknowledge
* @inheritDoc
*/
public function acknowledgeResource(MonitoringResource $monitoringResource, Acknowledgement $acknowledgement): void
{/*
{
switch ($monitoringResource->getType()) {
case ResourceEntity::TYPE_HOST:
$host = $this->monitoringRepository->findOneHost(ResourceService::generateHostIdByResource($monitoringResource));
case MonitoringResource::TYPE_HOST:
$host = $this->monitoringRepository->findOneHost($monitoringResource->getId());
if (is_null($host)) {
throw new EntityNotFoundException(_('Host not found'));
}
$this->engineService->addHostAcknowledgement($ack, $host);
if ($ack->isWithServices()) {
$this->engineService->addHostAcknowledgement($acknowledgement, $host);
if ($acknowledgement->isWithServices()) {
$services = $this->monitoringRepository->findServicesByHostWithoutRequestParameters($host->getId());
foreach ($services as $service) {
$service->setHost($host);
$this->engineService->addServiceAcknowledgement($ack, $service);
$this->engineService->addServiceAcknowledgement($acknowledgement, $service);
}
}
break;
case ResourceEntity::TYPE_SERVICE:
$host = $this->monitoringRepository->findOneHost(ResourceService::generateHostIdByResource($resource));
case MonitoringResource::TYPE_SERVICE:
$host = $this->monitoringRepository->findOneHost($monitoringResource->getParent()->getId());
if (is_null($host)) {
throw new EntityNotFoundException(_('Host not found'));
}
$service = $this->monitoringRepository->findOneService(
(int)$resource->getParent()->getId(),
(int)$resource->getId()
(int) $monitoringResource->getParent()->getId(),
(int) $monitoringResource->getId()
);
if (is_null($service)) {
throw new EntityNotFoundException(
sprintf(
_('Service %d (parent: %d) not found'),
$resource->getId(),
$resource->getParent()->getId()
$monitoringResource->getId(),
$monitoringResource->getParent()->getId()
)
);
}
$service->setHost($host);
$this->engineService->addServiceAcknowledgement($ack, $service);
$this->engineService->addServiceAcknowledgement($acknowledgement, $service);
break;
case ResourceEntity::TYPE_META:
$service = $this->monitoringRepository->findOneServiceByDescription('meta_' . $resource->getId());
case MonitoringResource::TYPE_META:
$service = $this->monitoringRepository->findOneServiceByDescription('meta_' . $monitoringResource->getId());
if (is_null($service)) {
throw new EntityNotFoundException(
sprintf(
_('Meta Service %d not found'),
$resource->getId(),
$resource->getParent()->getId()
$monitoringResource->getId(),
$monitoringResource->getParent()->getId()
)
);
}
Expand All @@ -470,10 +470,10 @@ public function acknowledgeResource(MonitoringResource $monitoringResource, Ackn
throw new EntityNotFoundException(_('Host not found'));
}
$service->setHost($host);
$this->engineService->addServiceAcknowledgement($ack, $service);
$this->engineService->addServiceAcknowledgement($acknowledgement, $service);
break;
default:
throw new ResourceException(sprintf(_('Incorrect Resource type: %s'), $resource->getType()));
} */
throw new ResourceException(sprintf(_('Incorrect Resource type: %s'), $monitoringResource->getType()));
}
}
}
38 changes: 38 additions & 0 deletions src/Centreon/Domain/Common/Assertion/Assertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,42 @@ function (array $parameters) {
$propertyPath
);
}

/**
* Assert that value is a string.
*
* @param mixed $value Value to test
* @param string|null $propertyPath Property's path (ex: Host::name)
* @throws \Assert\AssertionFailedException
*/
public static function string($value, string $propertyPath = null): void
{
Assert::string(
$value,
function (array $parameters) {
return AssertionException::integer($parameters['propertyPath']);
},
$propertyPath
);
}

/**
* Assert that value is equal to an other one.
*
* @param mixed $value value to compare
* @param mixed $value2 value expected
* @param string|null $propertyPath Property's path (ex: Host::name)
* @throws \Assert\AssertionFailedException
*/
public static function eq($value, $value2, string $propertyPath = null): void
{
Assert::eq(
$value,
$value2,
function (array $parameters) {
return AssertionException::eq($parameters['propertyPath']);
},
$propertyPath
);
}
}
16 changes: 16 additions & 0 deletions src/Centreon/Domain/Common/Assertion/AssertionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,20 @@ public static function integer(string $propertyPath = null): self
)
);
}

/**
* Exception when the two values are not equal.
*
* @param string|null $propertyPath Property's path (ex: Host::name)
* @return self
*/
public static function eq(string $propertyPath = null): self
{
return new self(
sprintf(
_('[%s] Unexpected value found'),
$propertyPath
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ public function validateOrFail(array $monitoringResource): void
{
Assertion::keyExists($monitoringResource, 'id', 'id');
Assertion::integer($monitoringResource['id'], 'id');

Assertion::keyExists($monitoringResource, 'id', 'id');
Assertion::string($monitoringResource['name'], 'name');

Assertion::keyExists($monitoringResource, 'type', 'resource::type');
Assertion::eq($monitoringResource['type'], 'host', 'resource::type');

Assertion::keyExists($monitoringResource, 'parent', 'parent');
Assertion::null($monitoringResource['parent'], 'parent');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,24 @@ public function validateOrFail(array $monitoringResource): void
{
Assertion::keyExists($monitoringResource, 'id', 'resource::id');
Assertion::integer($monitoringResource['id'], 'resource::id');

Assertion::keyExists($monitoringResource, 'name', 'resource::name');
Assertion::string($monitoringResource['name'], 'resource::name');

Assertion::keyExists($monitoringResource, 'type', 'resource::type');
Assertion::eq($monitoringResource['type'], 'service', 'resource::type');

Assertion::keyExists($monitoringResource, 'parent', 'resource::parent');
Assertion::notNull($monitoringResource['parent'], 'resource::parent');

Assertion::keyExists($monitoringResource['parent'], 'id', 'resource::parent::id');
Assertion::integer($monitoringResource['parent']['id'], 'resource::parent::id');

Assertion::keyExists($monitoringResource['parent'], 'name', 'resource::parent::name');
Assertion::string($monitoringResource['parent']['name'], 'resource::parent::name');

Assertion::keyExists($monitoringResource['parent'], 'type', 'resource::parent::type');
Assertion::eq($monitoringResource['parent']['type'], 'host', 'resource::parent::type');
}

public function isValidFor(string $type): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ public static function create(array $data): MonitoringResource
$monitoringResource->setCommandLine($data['command_line']);
$monitoringResource->setTimezone($data['timezone']);
$monitoringResource->setMonitoringServerName($data['monitoring_server_name']);
$monitoringResource->setFlapping($data['flapping'] === 1);
$monitoringResource->setFlapping((int) $data['flapping'] === 1);
$monitoringResource->setPercentStateChange((float) $data['percent_state_change']);
$monitoringResource->setSeverityLevel(self::getIntOrNull($data['severity_level']));
$monitoringResource->setInDowntime($data['in_downtime'] === 1);
$monitoringResource->setAcknowledged($data['acknowledged'] === 1);
$monitoringResource->setActiveChecks($data['active_checks'] === 1);
$monitoringResource->setPassiveChecks($data['passive_checks'] === 1);
$monitoringResource->setInDowntime((int) $data['in_downtime'] === 1);
$monitoringResource->setAcknowledged((int) $data['acknowledged'] === 1);
$monitoringResource->setActiveChecks((int) $data['active_checks'] === 1);
$monitoringResource->setPassiveChecks((int) $data['passive_checks'] === 1);

$lastStatusChange = (new \DateTime())
->setTimestamp((int) $data['last_status_change']);
Expand Down Expand Up @@ -91,7 +91,7 @@ public static function create(array $data): MonitoringResource
$monitoringResource->setPerformanceData($data['performance_data']);
$monitoringResource->setExecutionTime((float) $data['execution_time']);
$monitoringResource->setLatency((float) $data['latency']);
$monitoringResource->setNotificationEnabled($data['notification_enabled'] === 1);
$monitoringResource->setNotificationEnabled((int) $data['notification_enabled'] === 1);


$status = (new ResourceStatus())
Expand Down

0 comments on commit 76cf542

Please sign in to comment.