diff --git a/src/Controller/OgAgent/OgAdmClientController.php b/src/Controller/OgAgent/OgAdmClientController.php index 3050e63..f8fe811 100644 --- a/src/Controller/OgAgent/OgAdmClientController.php +++ b/src/Controller/OgAgent/OgAdmClientController.php @@ -25,7 +25,6 @@ class OgAdmClientController extends AbstractController { } - #[Route('/opengnsys/rest/ogAdmClient/InclusionCliente', methods: ['POST'])] public function processClient(Request $request): JsonResponse { @@ -54,11 +53,9 @@ class OgAdmClientController extends AbstractController $partitionEntity->setClient($clientEntity); $partitionEntity->setDiskNumber($cfg['disk']); - //$partitionEntity->setPartitionCode($cfg['codpar']); $partitionEntity->setPartitionNumber($cfg['par']); $partitionEntity->setSize($cfg['tam']); $partitionEntity->setMemoryUsage($cfg['uso']); - //$partitionEntity->setFilesystem($cfg['idsistemafichero']); $this->entityManager->persist($partitionEntity); $this->entityManager->flush(); } diff --git a/src/Controller/OgAgent/StatusAction.php b/src/Controller/OgAgent/StatusAction.php index 12f8b92..8de1498 100644 --- a/src/Controller/OgAgent/StatusAction.php +++ b/src/Controller/OgAgent/StatusAction.php @@ -2,7 +2,60 @@ namespace App\Controller\OgAgent; -class StatusAction -{ +use App\Entity\Client; +use App\Model\OgLiveStatus; +use Doctrine\ORM\EntityManagerInterface; +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\HttpClient\HttpClient; +use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Attribute\AsController; +use Symfony\Component\Validator\Exception\ValidatorException; +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; +#[AsController] +class StatusAction extends AbstractController +{ + public function __construct( + protected readonly EntityManagerInterface $entityManager, + protected readonly HttpClientInterface $httpClient + ) + { + $httpClient = HttpClient::create([ + 'verify_peer' => false, + 'verify_host' => false, + ]); + } + + /** + * @throws TransportExceptionInterface + * @throws ServerExceptionInterface + * @throws RedirectionExceptionInterface + * @throws ClientExceptionInterface + */ + public function __invoke(Client $client): JsonResponse + { + if (!$client->getIp()) { + throw new ValidatorException('IP is required'); + } + + + try { + $response = $this->httpClient->request('POST', 'https://'.$client->getIp().':8000/ogAdmClient/status'); + } catch (TransportExceptionInterface $e) { + return new JsonResponse( data: 'An error occurred', status: Response::HTTP_INTERNAL_SERVER_ERROR); + } + + $data = json_decode($response->getContent(), true); + + $client->setAgentJobId($data['job_id']); + $this->entityManager->persist($data); + $this->entityManager->flush(); + + return new JsonResponse(data: $data, status: Response::HTTP_OK); + } } \ No newline at end of file diff --git a/src/Controller/OgAgent/Webhook/GetStatusAction.php b/src/Controller/OgAgent/Webhook/GetStatusAction.php index 7c63b23..fc256f8 100644 --- a/src/Controller/OgAgent/Webhook/GetStatusAction.php +++ b/src/Controller/OgAgent/Webhook/GetStatusAction.php @@ -2,7 +2,43 @@ namespace App\Controller\OgAgent\Webhook; -class GetStatusAction -{ +use App\Controller\OgBoot\AbstractOgBootController; +use App\Entity\OgLive; +use App\Model\OgLiveStatus; +use Doctrine\ORM\EntityManagerInterface; +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Attribute\AsController; +use Symfony\Component\Routing\Annotation\Route; +use Symfony\Component\Validator\Exception\ValidatorException; +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; +#[AsController] +class GetStatusAction extends AbstractController +{ + public function __construct( + protected readonly EntityManagerInterface $entityManager + ) + { + } + + #[Route('/clients/status/webhook', name: 'status', methods: ['POST'])] + public function installWebhook(Request $request): JsonResponse + { + $data = json_decode($request->getContent(), true); + + if (!is_array($data)) { + return new JsonResponse(['error' => 'Invalid JSON data'], Response::HTTP_BAD_REQUEST); + } + + + + return new JsonResponse(data: $data, status: Response::HTTP_OK); + } } \ No newline at end of file