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\Post: ~
ApiPlatform\Metadata\Delete: ~ ApiPlatform\Metadata\Delete: ~
sync_uds:
class: ApiPlatform\Metadata\Post
method: POST
uriTemplate: /remote-calendars/{uuid}/sync-uds
controller: App\Controller\UDS\RemoteCalendarSyncUdsAction
properties: properties:
App\Entity\RemoteCalendar: App\Entity\RemoteCalendar:
id: 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; 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; namespace App\Service\UDS;
use AllowDynamicProperties; use AllowDynamicProperties;
use App\Entity\OrganizationalUnit;
use Doctrine\ORM\EntityManagerInterface; 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\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\HttpClientInterface;
#[AllowDynamicProperties] #[AllowDynamicProperties]
class UDSClient class UDSClient
{ {
private string $token;
private string $scrambler;
public function __construct( public function __construct(
private HttpClientInterface $httpClient, private HttpClientInterface $httpClient,
EntityManagerInterface $entityManager, private readonly EntityManagerInterface $entityManager,
private string $udsAPIurl, private string $udsAPIurl,
private string $udsAuthLogin, private string $udsAuthLogin,
private string $udsAuthUsername, private string $udsAuthUsername,
private string $udsAuthPassword, private string $udsAuthPassword,
string $token,
string $scrambler
) )
{ {
} }
@ -26,9 +35,35 @@ class UDSClient
/** /**
* @throws TransportExceptionInterface * @throws TransportExceptionInterface
*/ */
public function __invoke(): void public function __invoke(): array
{ {
$this->login(); $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 public function login(): void
{ {
$response = $this->httpClient->request('POST', $this->udsAPIurl . '/auth/login', [ try {
'json' => [ $response = $this->httpClient->request('POST', $this->udsAPIurl . 'auth/login', [
'login' => $this->udsAuthLogin, 'verify_host' => false,
'username' => $this->udsAuthUsername, 'headers' => [
'password' => $this->udsAuthPassword 'Content-Type' => 'application/json'
] ],
]); 'json' => [
'auth' => $this->udsAuthLogin,
'username' => $this->udsAuthUsername,
'password' => $this->udsAuthPassword
]
]);
$data = json_decode($response->getContent(), true); $data = json_decode($response->getContent(false), true);
$this->token = $data['token'];
$this->scrambler = $data['scrambler']; $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 public function getServicePools(): array
{ {
$response = $this->httpClient->request('GET', $this->udsAPIurl . '/servicespools/overview', [ try {
'headers' => [ $response = $this->httpClient->request('GET', $this->udsAPIurl . '/servicespools/overview', [
'X-Auth-Token' => $this->token, 'verify_host' => false,
'Content-Type' => 'application/json', 'headers' => [
'Scrambler' => $this->scrambler '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, [ try {
'headers' => [ $response = $this->httpClient->request('GET', $this->udsAPIurl . 'providers/' . $providerId .'/services/'. $serviceId, [
'X-Auth-Token' => $this->token, 'verify_host' => false,
'Content-Type' => 'application/json', 'headers' => [
'Scrambler' => $this->scrambler '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, [ $organizationalUnit = $this->entityManager->getRepository(OrganizationalUnit::class)->find($organizationalUnitId);
'headers' => [
'X-Auth-Token' => $this->token, if (!$organizationalUnit) {
'Content-Type' => 'application/json', return 0;
'Scrambler' => $this->scrambler }
],
'json' => $payload $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;
} }
} }