refs #726. Integration UDS. Success Put info
testing/ogcore-api/pipeline/head This commit looks good Details

develop-jenkins
Manuel Aranda Rosales 2024-10-01 15:05:25 +02:00
parent 4b3f55f631
commit d135c74f8a
4 changed files with 179 additions and 43 deletions

View File

@ -23,6 +23,12 @@ resources:
ApiPlatform\Metadata\Post: ~
ApiPlatform\Metadata\Delete: ~
sync_uds:
class: ApiPlatform\Metadata\Post
method: POST
uriTemplate: /remote-calendars/{uuid}/sync-uds
controller: App\Controller\UDS\RemoteCalendarSyncUdsAction
properties:
App\Entity\RemoteCalendar:
id:

View File

@ -0,0 +1,34 @@
<?php
namespace App\Controller\UDS;
use App\Entity\OrganizationalUnit;
use App\Entity\RemoteCalendar;
use App\Model\OrganizationalUnitTypes;
use App\Service\UDS\UDSClient;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
class RemoteCalendarSyncUdsAction extends AbstractController
{
public function __construct(
private readonly UDSClient $udsClient
)
{
}
/**
* @throws TransportExceptionInterface
*/
public function __invoke(RemoteCalendar $remoteCalendar): JsonResponse
{
$data = $this->udsClient->__invoke();
return new JsonResponse($data, Response::HTTP_OK);
}
}

View File

@ -83,4 +83,21 @@ class RemoteCalendar extends AbstractEntity
return $this;
}
public function isRemoteAvailable(): bool
{
$now = new \DateTime();
foreach ($this->getRules() as $rule) {
$dayOfWeek = (int) $now->format('w');
$adjustedDayOfWeek = ($dayOfWeek + 6) % 7;
if (!in_array($adjustedDayOfWeek, $rule->getBusyWeekdays())) {
return false;
}
$currentTime = $now->format('H:i');
return ($currentTime >= $rule->getBusyFromHour() && $currentTime <= $rule->getBusyToHour());
}
}
}

View File

@ -3,22 +3,31 @@
namespace App\Service\UDS;
use AllowDynamicProperties;
use App\Entity\OrganizationalUnit;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
#[AllowDynamicProperties]
class UDSClient
{
private string $token;
private string $scrambler;
public function __construct(
private HttpClientInterface $httpClient,
EntityManagerInterface $entityManager,
private string $udsAPIurl,
private string $udsAuthLogin,
private string $udsAuthUsername,
private string $udsAuthPassword,
string $token,
string $scrambler
private HttpClientInterface $httpClient,
private readonly EntityManagerInterface $entityManager,
private string $udsAPIurl,
private string $udsAuthLogin,
private string $udsAuthUsername,
private string $udsAuthPassword,
)
{
}
@ -26,9 +35,35 @@ class UDSClient
/**
* @throws TransportExceptionInterface
*/
public function __invoke(): void
public function __invoke(): array
{
$this->login();
$servicePools = $this->getServicePools();
foreach ($servicePools as $servicePool) {
$servicePoolInfo = $this->getServicePool($servicePool['provider_id'], $servicePool['service_id']);
if (!$servicePoolInfo) {
continue;
}
$classroom = $servicePoolInfo['ou'];
$maxAvailableSeats = $this->getMaxAvailableSeats($classroom);
$servicePool['max_srvs'] = 10;
$servicePool['osmanager_id'] = null;
$response = $this->httpClient->request('PUT', $this->udsAPIurl . 'servicespools/' . $servicePool['id'], [
'verify_host' => false,
'headers' => [
'X-Auth-Token' => $this->token,
'Content-Type' => 'application/json',
'Scrambler' => $this->scrambler
],
'body' => json_encode($servicePool)
]);
return json_decode($response->getContent(false), true);
}
}
/**
@ -36,17 +71,28 @@ class UDSClient
*/
public function login(): void
{
$response = $this->httpClient->request('POST', $this->udsAPIurl . '/auth/login', [
'json' => [
'login' => $this->udsAuthLogin,
'username' => $this->udsAuthUsername,
'password' => $this->udsAuthPassword
]
]);
try {
$response = $this->httpClient->request('POST', $this->udsAPIurl . 'auth/login', [
'verify_host' => false,
'headers' => [
'Content-Type' => 'application/json'
],
'json' => [
'auth' => $this->udsAuthLogin,
'username' => $this->udsAuthUsername,
'password' => $this->udsAuthPassword
]
]);
$data = json_decode($response->getContent(), true);
$this->token = $data['token'];
$this->scrambler = $data['scrambler'];
$data = json_decode($response->getContent(false), true);
$this->token = $data['token'];
$this->scrambler = $data['scrambler'];
} catch (TransportExceptionInterface $e) {
throw new TransportException('Error while logging in');
} catch (ClientExceptionInterface|ServerExceptionInterface|RedirectionExceptionInterface $e) {
}
}
/**
@ -54,35 +100,68 @@ class UDSClient
*/
public function getServicePools(): array
{
$response = $this->httpClient->request('GET', $this->udsAPIurl . '/servicespools/overview', [
'headers' => [
'X-Auth-Token' => $this->token,
'Content-Type' => 'application/json',
'Scrambler' => $this->scrambler
]
]);
try {
$response = $this->httpClient->request('GET', $this->udsAPIurl . '/servicespools/overview', [
'verify_host' => false,
'headers' => [
'X-Auth-Token' => $this->token,
'Content-Type' => 'application/json',
'Scrambler' => $this->scrambler
]
]);
return json_decode($response->getContent(), true);
} catch (TransportExceptionInterface $e) {
throw new TransportException('Error while fetching service pools');
} catch (ClientExceptionInterface|ServerExceptionInterface|RedirectionExceptionInterface $e) {
}
}
public function getServicePool(string $providerId, string $serviceId): array
/**
* @throws TransportExceptionInterface
*/
public function getServicePool(string $providerId, string $serviceId): ?array
{
$response = $this->httpClient->request('GET', $this->udsAPIurl . '/providers/' . $providerId .'/services/'. $serviceId, [
'headers' => [
'X-Auth-Token' => $this->token,
'Content-Type' => 'application/json',
'Scrambler' => $this->scrambler
]
]);
try {
$response = $this->httpClient->request('GET', $this->udsAPIurl . 'providers/' . $providerId .'/services/'. $serviceId, [
'verify_host' => false,
'headers' => [
'X-Auth-Token' => $this->token,
'Content-Type' => 'application/json',
'Scrambler' => $this->scrambler
]
]);
return json_decode($response->getContent(), true);
} catch (TransportExceptionInterface $e) {
throw new TransportException('Error while fetching service pool');
} catch (ClientExceptionInterface|ServerExceptionInterface|RedirectionExceptionInterface $e) {
}
return null;
}
public function setServicePool(string $serviceId, array $payload): void
public function getMaxAvailableSeats(int $organizationalUnitId): int
{
$response = $this->httpClient->request('PUT', $this->udsAPIurl . '/servicespools/' . $serviceId, [
'headers' => [
'X-Auth-Token' => $this->token,
'Content-Type' => 'application/json',
'Scrambler' => $this->scrambler
],
'json' => $payload
]);
$organizationalUnit = $this->entityManager->getRepository(OrganizationalUnit::class)->find($organizationalUnitId);
if (!$organizationalUnit) {
return 0;
}
$remoteCalendar = $organizationalUnit->getRemoteCalendar();
if (!$remoteCalendar || !$remoteCalendar->isRemoteAvailable()) {
return 0;
}
$totalAvailableClients = 0;
foreach ($organizationalUnit->getClients() as $client) {
$status = $client->getStatus();
$clientAvailable = in_array($status, ['active', 'windows', 'linux']);
$totalAvailableClients += $clientAvailable ? 1 : 0;
}
return $totalAvailableClients;
}
}