Skip to content
Snippets Groups Projects
Commit e8679673 authored by L N's avatar L N Committed by Yas Naoi
Browse files

Issue #3391015 by nakamurarts, yas, sekinet: Add health check APIs to monitor Cloud Orchestrator

parent 4bf9a0b3
No related branches found
No related tags found
1 merge request!2123Issue #3386224 by asai.noriaki, yas: Fix an issue where clicking an OpenStack...
......@@ -341,6 +341,16 @@ cloud.settings:
requirements:
_permission: 'administer cloud'
# Health Check API.
cloud.health_check:
path: '/health_check'
defaults:
_controller: '\Drupal\cloud\Controller\CloudHealthCheckController::healthCheck'
methods: [POST]
requirements:
# Public access.
_access: 'TRUE'
# List of visible manage menu links API.
cloud_dashboard.manage_menu.visible:
path: '/cloud_dashboard/manage_menu/visible'
......
<?php
namespace Drupal\cloud\Controller;
use Drupal\cloud\Traits\CloudContentEntityTrait;
use Drupal\Core\Controller\ControllerBase;
use Drupal\user\UserAuthInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Controller responsible for "health check" URLs.
*/
class CloudHealthCheckController extends ControllerBase implements CloudHealthCheckControllerInterface {
use CloudContentEntityTrait;
/**
* UserAuth object.
*
* @var \Drupal\user\UserAuthInterface
*/
protected $userAuth;
/**
* Request object.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* CloudHealthCheckController constructor.
*
* @param \Drupal\user\UserAuthInterface $user_auth
* The user authentication service.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
*/
public function __construct(
UserAuthInterface $user_auth,
RequestStack $request_stack
) {
$this->userAuth = $user_auth;
$this->request = $request_stack->getCurrentRequest();
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) : CloudHealthCheckController {
return new static(
$container->get('user.auth'),
$container->get('request_stack')
);
}
/**
* Returns the current date and time formatted according to ISO-8601.
*
* This function retrieves the current date and time and formats it as per the
* ISO-8601 standard (e.g., "Y-m-d\TH:i:sP").
*
* @return string
* The formatted date and time string in ISO-8601 format.
*/
private static function getCurrentDateTime(): string {
// Use system's timezone.
return (new \DateTimeImmutable('now', new \DateTimeZone(date_default_timezone_get())))->format(\DateTimeInterface::ATOM);
}
/**
* Creates JsonResponse from an associative array.
*
* @param array $json
* An associative array to be converted to JSON response.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
* A JSON response object.
*/
private function createJsonResponse(array $json): JsonResponse {
$json['status'] ??= self::STATUS_FAIL;
$json['time'] = self::getCurrentDateTime();
$status_code = $json['status'] === self::STATUS_SUCCESS ? 200 : 503;
$response = new JsonResponse($json, $status_code);
$response->setCache(['no_cache' => TRUE]);
$this->logger('health_check')->info('Health check status is @status: @message', [
'@status' => $json['status'],
'@message' => $json['message'] ?? '',
]);
return $response;
}
/**
* {@inheritdoc}
*/
public function healthCheck(): JsonResponse {
$username = $this->request->get('username');
$password = $this->request->get('password');
if (empty($username) || empty($password)) {
return $this->createJsonResponse([
'status' => self::STATUS_FAIL,
'message' => 'Username and password are required',
]);
}
$uid = $this->userAuth->authenticate($username, $password);
$auth_result = empty($uid) ? [
'status' => self::STATUS_FAIL,
'message' => 'User authentication failed',
] : [
'status' => self::STATUS_SUCCESS,
];
return $this->createJsonResponse($auth_result);
}
}
<?php
namespace Drupal\cloud\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* Interface for the health check controller.
*/
interface CloudHealthCheckControllerInterface {
/**
* The value of the JSON property 'status'.
*
* This value means that the health check was successful.
*
* @var int
*/
public const STATUS_SUCCESS = 1;
/**
* The value of the JSON property 'status'.
*
* This value means that the health check was failed.
*
* @var int
*/
public const STATUS_FAIL = 0;
/**
* Perform a health check.
*
* This method checks the health status by authenticating
* the user with a provided username and password.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
* The JSON response containing the health status and time.
*/
public function healthCheck(): JsonResponse;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment