From ca4cdafc0191636b5298add6040122a1bee79542 Mon Sep 17 00:00:00 2001 From: Manuel Aranda Date: Mon, 9 Jun 2025 11:43:14 +0200 Subject: [PATCH] refs #2181. Changed Output messages --- CHANGELOG.md | 5 ++ config/api_platform/Client.yaml | 14 +++++ config/api_platform/Software.yaml | 1 + config/services/api_platform.yaml | 6 +- env.json | 5 +- migrations/Version20250604084222.php | 33 ++++++++++ .../OgAgent/AbstractOgAgentController.php | 2 +- src/Controller/OgAgent/CreateImageAction.php | 5 +- src/Controller/OgAgent/DeployImageAction.php | 9 +-- .../OgAgent/HardwareInventoryAction.php | 57 +++++++++++++++++ src/Controller/OgAgent/LoginAction.php | 7 ++- .../OgAgent/PartitionAssistantAction.php | 5 +- src/Controller/OgAgent/PowerOffAction.php | 5 +- src/Controller/OgAgent/RebootAction.php | 5 +- .../OgAgent/RemoveCacheImageAction.php | 9 +-- src/Controller/OgAgent/RunScriptAction.php | 7 ++- .../OgAgent/SoftwareInventoryAction.php | 62 +++++++++++++++++++ src/Controller/OgAgent/StatusAction.php | 6 +- .../OgAgent/Webhook/StatusController.php | 54 ++++++++++++++-- src/Controller/OgBoot/OgBootController.php | 5 ++ src/Controller/OgBoot/OgLive/GetAction.php | 7 ++- .../OgBoot/OgLive/GetCollectionAction.php | 5 +- .../OgBoot/OgLive/GetDefaultAction.php | 5 +- .../OgBoot/OgLive/GetIsosAction.php | 5 +- .../OgBoot/OgLive/InstallAction.php | 7 ++- .../OgBoot/OgLive/SetDefaultAction.php | 7 ++- src/Controller/OgBoot/OgLive/SyncAction.php | 6 +- .../OgBoot/OgLive/UninstallAction.php | 7 ++- .../OgBoot/PxeBootFile/PostAction.php | 4 +- .../OgBoot/PxeTemplate/DeleteAction.php | 3 +- .../PxeTemplate/GetCollectionAction.php | 3 +- .../OgBoot/PxeTemplate/PostAction.php | 3 +- .../OgBoot/PxeTemplate/SyncAction.php | 3 +- src/Controller/OgDhcp/Subnet/DeleteAction.php | 3 +- .../OgDhcp/Subnet/DeleteHostAction.php | 5 +- src/Controller/OgDhcp/Subnet/GetAction.php | 3 +- .../OgDhcp/Subnet/GetHostsAction.php | 3 +- src/Controller/OgDhcp/Subnet/PutAction.php | 3 +- .../Git/CreateRepositoryAction.php | 3 +- .../OgRepository/Git/CreateTagAction.php | 3 +- .../OgRepository/Git/GetTagsAction.php | 3 +- .../OgRepository/Image/BackupImageAction.php | 8 ++- .../Image/CancelTransmissionAction.php | 12 ++-- .../OgRepository/Image/ConvertAction.php | 7 ++- .../Image/ConvertImageToVirtualAction.php | 8 +-- .../Image/CreateAuxFilesAction.php | 8 ++- .../Image/DeletePermanentAction.php | 4 +- .../OgRepository/Image/DeleteTrashAction.php | 4 +- .../OgRepository/Image/GetAction.php | 8 ++- .../Image/GetCollectionAction.php | 5 ++ .../OgRepository/Image/GetStatusAction.php | 8 +-- .../OgRepository/Image/ImportAction.php | 13 +++- .../OgRepository/Image/RecoverAction.php | 8 ++- .../OgRepository/Image/RenameAction.php | 5 +- .../OgRepository/Image/TransferAction.php | 8 +-- .../Image/TransferGlobalAction.php | 4 +- src/Controller/OgRepository/StatusAction.php | 5 ++ src/Controller/OgRepository/WoLAction.php | 5 +- .../Input/SoftwareInventoryPartitionInput.php | 13 ++++ src/Entity/Partition.php | 2 +- .../ApiExceptionSubscriber.php | 49 +++++++++++++++ src/EventSubscriber/ClientSubscriber.php | 4 ++ .../OrganizationalUnitSubscriber.php | 29 ++++++++- src/Model/CommandTypes.php | 6 ++ src/Service/OgBoot/StatusService.php | 26 +++++--- src/State/Processor/ClientProcessor.php | 11 ++-- src/State/Processor/OgLiveProcessor.php | 26 +++++++- .../Processor/OrganizationalUnitProcessor.php | 2 +- 68 files changed, 553 insertions(+), 128 deletions(-) create mode 100644 migrations/Version20250604084222.php create mode 100644 src/Controller/OgAgent/HardwareInventoryAction.php create mode 100644 src/Controller/OgAgent/SoftwareInventoryAction.php create mode 100644 src/Dto/Input/SoftwareInventoryPartitionInput.php create mode 100644 src/EventSubscriber/ApiExceptionSubscriber.php diff --git a/CHANGELOG.md b/CHANGELOG.md index edbd977..8679d3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,9 @@ # Changelog +## [0.14.1] - 2025-06-09 +### Fixed +- Se han corregido los mensajes de error que se envian cuando las APIs de los modulos no estan disponibles. +- Se ha corregido un bug en la herencia de datos entre aulas y grupos, que hacia que no se almacenaran los datos de manera correcta. + ## [0.14.0] - 2025-06-02 ### Added - Se ha aƱadido la funcionalidad de mover equipos entre aulas y grupos. diff --git a/config/api_platform/Client.yaml b/config/api_platform/Client.yaml index 3882d9c..80b0ebd 100644 --- a/config/api_platform/Client.yaml +++ b/config/api_platform/Client.yaml @@ -64,6 +64,20 @@ resources: uriTemplate: /clients/server/remove-cache-image controller: App\Controller\OgAgent\RemoveCacheImageAction + hardware_inventory: + class: ApiPlatform\Metadata\Post + method: POST + input: false + uriTemplate: /clients/server/{uuid}/hardware-inventory + controller: App\Controller\OgAgent\HardwareInventoryAction + + software_inventory: + class: ApiPlatform\Metadata\Post + method: POST + input: App\Dto\Input\SoftwareInventoryPartitionInput + uriTemplate: /clients/server/{uuid}/software-inventory + controller: App\Controller\OgAgent\SoftwareInventoryAction + reboot_client: class: ApiPlatform\Metadata\Post method: POST diff --git a/config/api_platform/Software.yaml b/config/api_platform/Software.yaml index 4e34a66..949e545 100644 --- a/config/api_platform/Software.yaml +++ b/config/api_platform/Software.yaml @@ -13,6 +13,7 @@ resources: filters: - 'api_platform.filter.software.order' - 'api_platform.filter.software.search' + - 'software.software_profile_filter' ApiPlatform\Metadata\Get: provider: App\State\Provider\SoftwareProvider diff --git a/config/services/api_platform.yaml b/config/services/api_platform.yaml index 0f70054..fd70e1d 100644 --- a/config/services/api_platform.yaml +++ b/config/services/api_platform.yaml @@ -119,6 +119,10 @@ services: parent: 'App\Filter\ImageSearchRepositoryFilter' tags: [ 'api_platform.filter' ] + software.software_profile_filter: + parent: 'App\Filter\SoftwareProfileSearchSoftwareFilter' + tags: [ 'api_platform.filter' ] + api_platform.filter.og_live.order: parent: 'api_platform.doctrine.orm.order_filter' arguments: @@ -277,7 +281,7 @@ services: api_platform.filter.software_profile.search: parent: 'api_platform.doctrine.orm.search_filter' - arguments: [ { 'id': 'exact', 'description': 'partial' } ] + arguments: [ { 'id': 'exact', 'description': 'partial'} ] tags: [ 'api_platform.filter' ] api_platform.filter.subnet.order: diff --git a/env.json b/env.json index 9020b33..c40cd36 100644 --- a/env.json +++ b/env.json @@ -1,6 +1,5 @@ { "vars": { - "OG_BOOT_API_URL": "127.0.0.1:8082", "OG_DHCP_API_URL": "127.0.0.1:8081", "OG_CORE_IP": "127.0.0.1", "OG_LOG_IP": "127.0.0.1", @@ -8,9 +7,9 @@ "UDS_AUTH_USERNAME": "test", "UDS_AUTH_PASSWORD": "test", "UDS_URL": "https:\/\/localhost:8087\/uds\/rest\/", - "SSL_ENABLED": "true", + "SSL_ENABLED": "false", "OG_BOOT_IP": "127.0.0.1", "OG_BOOT_API_PORT": "8082", - "OG_BOOT_PXE_PORT": "8085" + "OG_BOOT_PXE_PORT": "8082" } } \ No newline at end of file diff --git a/migrations/Version20250604084222.php b/migrations/Version20250604084222.php new file mode 100644 index 0000000..140b355 --- /dev/null +++ b/migrations/Version20250604084222.php @@ -0,0 +1,33 @@ +addSql('ALTER TABLE `partition` DROP FOREIGN KEY FK_9EB910E43DA5256D'); + $this->addSql('ALTER TABLE `partition` ADD CONSTRAINT FK_9EB910E43DA5256D FOREIGN KEY (image_id) REFERENCES image_image_repository (id) ON DELETE SET NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE `partition` DROP FOREIGN KEY FK_9EB910E43DA5256D'); + $this->addSql('ALTER TABLE `partition` ADD CONSTRAINT FK_9EB910E43DA5256D FOREIGN KEY (image_id) REFERENCES image_image_repository (id)'); + } +} diff --git a/src/Controller/OgAgent/AbstractOgAgentController.php b/src/Controller/OgAgent/AbstractOgAgentController.php index 97ed83d..5739ef2 100644 --- a/src/Controller/OgAgent/AbstractOgAgentController.php +++ b/src/Controller/OgAgent/AbstractOgAgentController.php @@ -79,7 +79,7 @@ abstract class AbstractOgAgentController extends AbstractController return [ 'code' => Response::HTTP_INTERNAL_SERVER_ERROR, - 'error' => 'Client/Server error', + 'error' => 'Transport error', 'details' => $e->getMessage(), ]; } diff --git a/src/Controller/OgAgent/CreateImageAction.php b/src/Controller/OgAgent/CreateImageAction.php index 907a0f9..6a3f032 100644 --- a/src/Controller/OgAgent/CreateImageAction.php +++ b/src/Controller/OgAgent/CreateImageAction.php @@ -26,6 +26,7 @@ use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; @@ -45,7 +46,7 @@ class CreateImageAction extends AbstractOgAgentController public function __invoke(Image $image, ?Partition $partition = null, ?Client $client = null): JsonResponse { if (!$image->getClient()->getIp()) { - throw new ValidatorException('IP is required'); + throw new BadRequestHttpException('IP is required'); } $partitionInfo = []; @@ -151,7 +152,7 @@ class CreateImageAction extends AbstractOgAgentController $this->logger->info('Creating image', ['image' => $imageImageRepository->getName(), 'repository' => $repository->getIp()]); if (isset($response['error']) && $response['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) { - throw new ValidatorException('Error creating image'); + throw new BadRequestHttpException('Error creating image'); } $jobId = $response['job_id']; diff --git a/src/Controller/OgAgent/DeployImageAction.php b/src/Controller/OgAgent/DeployImageAction.php index 2ab9d01..6bebf9e 100644 --- a/src/Controller/OgAgent/DeployImageAction.php +++ b/src/Controller/OgAgent/DeployImageAction.php @@ -21,6 +21,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpClient\Exception\TransportException; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; @@ -42,7 +43,7 @@ class DeployImageAction extends AbstractOgAgentController $image = $imageImageRepository->getImage(); if (!$client->getIp()) { - throw new ValidatorException('IP is required'); + throw new BadRequestHttpException('IP is required'); } $method = match ($input->method) { @@ -51,7 +52,7 @@ class DeployImageAction extends AbstractOgAgentController DeployMethodTypes::UNICAST_DIRECT => 'unicast-direct', DeployMethodTypes::UNICAST => 'unicast', DeployMethodTypes::TORRENT => 'torrent', - default => throw new ValidatorException('Invalid method'), + default => throw new BadRequestHttpException('Invalid method'), }; $mcastMode = $input->mcastMode.'-duplex'; @@ -65,7 +66,7 @@ class DeployImageAction extends AbstractOgAgentController DeployMethodTypes::MULTICAST, DeployMethodTypes::MULTICAST_UFTP, DeployMethodTypes::MULTICAST_UFTP_DIRECT, DeployMethodTypes::MULTICAST_UDPCAST, DeployMethodTypes::MULTICAST_UDPCAST_DIRECT => $ptcMulticastValue, DeployMethodTypes::UNICAST, DeployMethodTypes::UNICAST_DIRECT => $ptcUnicastValue, DeployMethodTypes::TORRENT => $ptcTorrentValue, - default => throw new ValidatorException('Invalid method'), + default => throw new BadRequestHttpException('Invalid method'), }; $repository = $imageImageRepository->getRepository(); @@ -94,7 +95,7 @@ class DeployImageAction extends AbstractOgAgentController $this->logger->info('Deploying image', [ 'image' => $imageImageRepository->getName(), 'repository' => $repository->getIp()]); if (isset($response['error']) && $response['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) { - throw new ValidatorException('Error deploying image'); + throw new BadRequestHttpException('Error deploying image'); } $jobId = $response['job_id']; diff --git a/src/Controller/OgAgent/HardwareInventoryAction.php b/src/Controller/OgAgent/HardwareInventoryAction.php new file mode 100644 index 0000000..3c6792d --- /dev/null +++ b/src/Controller/OgAgent/HardwareInventoryAction.php @@ -0,0 +1,57 @@ + 'InventarioHardware', + 'ids' => '0' + ]; + + $response = $this->createRequest( + method: 'POST', + url: 'https://'.$client->getIp().':8000/opengnsys/InventarioHardware', + params: [ + 'json' => $data, + ], + token: $client->getToken(), + ); + + if (isset($response['error']) && $response['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) { + throw new BadRequestHttpException('Error performing hardware inventory: '.$response['error']); + } + + $this->logger->info('Login client', ['client' => $client->getId()]); + + $jobId = $response['job_id']; + + $inputData = [ + 'client' => $client->getIp(), + ]; + + $this->createService->__invoke($client, CommandTypes::HARDWARE_INVENTORY, TraceStatus::IN_PROGRESS, $jobId, $inputData); + + return new JsonResponse(data: [], status: Response::HTTP_OK); + } +} \ No newline at end of file diff --git a/src/Controller/OgAgent/LoginAction.php b/src/Controller/OgAgent/LoginAction.php index 260f02f..67d6058 100644 --- a/src/Controller/OgAgent/LoginAction.php +++ b/src/Controller/OgAgent/LoginAction.php @@ -21,6 +21,7 @@ use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; @@ -48,11 +49,11 @@ class LoginAction extends AbstractOgAgentController if (!$client->getIp()) { - throw new ValidatorException('IP is required'); + throw new BadRequestHttpException('IP is required'); } if ($client->getStatus() !== ClientStatus::OG_LIVE) { - throw new ValidatorException('Client is not in OG_LIVE status'); + throw new BadRequestHttpException('Client is not in OG_LIVE status'); } $data = [ @@ -72,7 +73,7 @@ class LoginAction extends AbstractOgAgentController ); if (isset($response['error']) && $response['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) { - throw new ValidatorException('Error logging in: '.$response['error']); + throw new BadRequestHttpException('Error logging in: '.$response['error']); } $this->logger->info('Login client', ['client' => $client->getId()]); diff --git a/src/Controller/OgAgent/PartitionAssistantAction.php b/src/Controller/OgAgent/PartitionAssistantAction.php index d69dba9..a443de1 100644 --- a/src/Controller/OgAgent/PartitionAssistantAction.php +++ b/src/Controller/OgAgent/PartitionAssistantAction.php @@ -20,6 +20,7 @@ use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; @@ -41,7 +42,7 @@ class PartitionAssistantAction extends AbstractOgAgentController $partitions = $input->partitions; if (empty($partitions)) { - throw new ValidatorException('Partitions is required'); + throw new BadRequestHttpException('Partitions is required'); } foreach ($input->clients as $clientInput) { @@ -110,7 +111,7 @@ class PartitionAssistantAction extends AbstractOgAgentController $this->logger->info('Partition assistant', ['client' => $client->getId()]); if (isset($response['error']) && $response['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) { - throw new ValidatorException('Error occurred while partitioning'); + throw new BadRequestHttpException('Error occurred while partitioning'); } $jobId = $response['job_id']; diff --git a/src/Controller/OgAgent/PowerOffAction.php b/src/Controller/OgAgent/PowerOffAction.php index ed8c913..19ac568 100644 --- a/src/Controller/OgAgent/PowerOffAction.php +++ b/src/Controller/OgAgent/PowerOffAction.php @@ -20,6 +20,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpClient\Exception\TransportException; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; @@ -43,7 +44,7 @@ class PowerOffAction extends AbstractOgAgentController $client = $clientEntity->getEntity(); if (!$client->getIp()) { - throw new ValidatorException('IP is required'); + throw new BadRequestHttpException('IP is required'); } if ($client->getStatus() === ClientStatus::OFF) { @@ -67,7 +68,7 @@ class PowerOffAction extends AbstractOgAgentController ); if (isset($response['error']) && $response['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) { - throw new ValidatorException('Error deploying image'); + throw new BadRequestHttpException('Error deploying image'); } $this->logger->info('Powering off client', ['client' => $client->getId()]); diff --git a/src/Controller/OgAgent/RebootAction.php b/src/Controller/OgAgent/RebootAction.php index 410bba6..411ff7e 100644 --- a/src/Controller/OgAgent/RebootAction.php +++ b/src/Controller/OgAgent/RebootAction.php @@ -19,6 +19,7 @@ use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; @@ -43,7 +44,7 @@ class RebootAction extends AbstractOgAgentController if (!$client->getIp()) { - throw new ValidatorException('IP is required'); + throw new BadRequestHttpException('IP is required'); } $endpoint = $client->getStatus() === ClientStatus::OG_LIVE ? 'opengnsys/Reiniciar' : '/opengnsys/reboot'; @@ -63,7 +64,7 @@ class RebootAction extends AbstractOgAgentController ); if (isset($response['error']) && $response['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) { - throw new ValidatorException('Error deploying image'); + throw new BadRequestHttpException('Error deploying image'); } $this->logger->info('Rebooting client', ['client' => $client->getId()]); diff --git a/src/Controller/OgAgent/RemoveCacheImageAction.php b/src/Controller/OgAgent/RemoveCacheImageAction.php index 769453f..8de9ff7 100644 --- a/src/Controller/OgAgent/RemoveCacheImageAction.php +++ b/src/Controller/OgAgent/RemoveCacheImageAction.php @@ -10,6 +10,7 @@ use App\Model\CommandTypes; use App\Model\TraceStatus; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; @@ -34,15 +35,15 @@ class RemoveCacheImageAction extends AbstractOgAgentController $client = $clientEntity->getEntity(); if (!$partition->getImage()) { - throw new ValidatorException('Image is required'); + throw new BadRequestHttpException('Image is required'); } if (!$client->getIp()) { - throw new ValidatorException('IP is required'); + throw new BadRequestHttpException('IP is required'); } if ($client->getStatus() !== ClientStatus::OG_LIVE) { - throw new ValidatorException('Client is not in OG_LIVE status'); + throw new BadRequestHttpException('Client is not in OG_LIVE status'); } $script = `rm%20-r%20/opt/opengnsys/cache/opt/opengnsys/images/{$partition->getImage()->getName()}.*@'`; @@ -63,7 +64,7 @@ class RemoveCacheImageAction extends AbstractOgAgentController ); if (isset($response['error']) && $response['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) { - throw new ValidatorException('Error logging in: '.$response['error']); + throw new BadRequestHttpException('Error logging in: '.$response['error']); } $this->logger->info('Login client', ['client' => $client->getId()]); diff --git a/src/Controller/OgAgent/RunScriptAction.php b/src/Controller/OgAgent/RunScriptAction.php index 840e6d2..eb9112a 100644 --- a/src/Controller/OgAgent/RunScriptAction.php +++ b/src/Controller/OgAgent/RunScriptAction.php @@ -14,6 +14,7 @@ use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; @@ -37,7 +38,7 @@ class RunScriptAction extends AbstractOgAgentController $client = $clientEntity->getEntity(); if (!$client->getIp()) { - throw new ValidatorException('IP is required'); + throw new BadRequestHttpException('IP is required'); } $data = [ @@ -56,7 +57,7 @@ class RunScriptAction extends AbstractOgAgentController ); if (isset($response['error']) && $response['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) { - throw new ValidatorException('Error deploying image'); + throw new BadRequestHttpException('Error deploying image'); } $this->logger->info('Powering off client', ['client' => $client->getId()]); @@ -70,7 +71,7 @@ class RunScriptAction extends AbstractOgAgentController 'script' => $input->script, ]; - $this->createService->__invoke($client, CommandTypes::RUN_SCRIPT, TraceStatus::SUCCESS, $jobId, $inputData); + $this->createService->__invoke($client, CommandTypes::RUN_SCRIPT, TraceStatus::IN_PROGRESS, $jobId, $inputData); } return new JsonResponse(data: [], status: Response::HTTP_OK); diff --git a/src/Controller/OgAgent/SoftwareInventoryAction.php b/src/Controller/OgAgent/SoftwareInventoryAction.php new file mode 100644 index 0000000..f94d518 --- /dev/null +++ b/src/Controller/OgAgent/SoftwareInventoryAction.php @@ -0,0 +1,62 @@ + 'InventarioSoftware', + 'ids' => '0', + 'dsk' => (string) $input->partition->getEntity()->getDiskNumber(), + 'par' => (string) $input->partition->getEntity()->getPartitionNumber(), + ]; + + $response = $this->createRequest( + method: 'POST', + url: 'https://'.$client->getIp().':8000/opengnsys/InventarioSoftware', + params: [ + 'json' => $data, + ], + token: $client->getToken(), + ); + + if (isset($response['error']) && $response['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) { + throw new BadRequestHttpException('Error performing software inventory: '.$response['error']); + } + + $this->logger->info('Login client', ['client' => $client->getId()]); + + $jobId = $response['job_id']; + + $inputData = [ + 'partition' => $input->partition->getEntity()->getUuid(), + 'image' => $input->partition->getEntity()->getImage()?->getUuid(), + 'client' => $client->getIp(), + ]; + + $this->createService->__invoke($client, CommandTypes::SOFTWARE_INVENTORY, TraceStatus::IN_PROGRESS, $jobId, $inputData); + + return new JsonResponse(data: [], status: Response::HTTP_OK); + } +} \ No newline at end of file diff --git a/src/Controller/OgAgent/StatusAction.php b/src/Controller/OgAgent/StatusAction.php index 01dcf28..f1efecc 100644 --- a/src/Controller/OgAgent/StatusAction.php +++ b/src/Controller/OgAgent/StatusAction.php @@ -16,6 +16,7 @@ use Symfony\Component\HttpClient\Internal\ClientState; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; @@ -37,7 +38,7 @@ class StatusAction extends AbstractOgAgentController $response = null; if (!$client->getIp()) { - throw new ValidatorException('IP is required'); + throw new BadRequestHttpException('IP is required'); } if ($client->getStatus() === ClientStatus::OG_LIVE @@ -87,7 +88,8 @@ class StatusAction extends AbstractOgAgentController $client->setStatus(ClientStatus::OFF); $this->entityManager->persist($client); $this->entityManager->flush(); - throw new ValidatorException('Error checking client status: ' . $data['error']); + + throw new BadRequestHttpException('Error checking client status: ' . $data['error'] . ' (' . $data['details'] . ')'); } if (isset($data['cfg'])) { diff --git a/src/Controller/OgAgent/Webhook/StatusController.php b/src/Controller/OgAgent/Webhook/StatusController.php index eeb2d27..b4df0f1 100644 --- a/src/Controller/OgAgent/Webhook/StatusController.php +++ b/src/Controller/OgAgent/Webhook/StatusController.php @@ -38,7 +38,9 @@ class StatusController extends AbstractController const string CREATE_IMAGE = 'RESPUESTA_CrearImagen'; const string RESTORE_IMAGE = 'RESPUESTA_RestaurarImagen'; const string CONFIGURE_IMAGE = 'RESPUESTA_Configurar'; - + const string HARDWARE_INVENTORY = 'RESPUESTA_InventarioHardware'; + const string SOFTWARE_INVENTORY = 'RESPUESTA_InventarioSoftware'; + const string RUN_SCRIPT = 'RESPUESTA_EjecutarScript'; public function __construct( protected readonly EntityManagerInterface $entityManager, @@ -180,10 +182,54 @@ class StatusController extends AbstractController $this->entityManager->flush(); } + if (isset($data['nfn']) && $data['nfn'] === self::RUN_SCRIPT) { + $trace = $this->entityManager->getRepository(Trace::class)->findOneBy(['jobId' => $data['job_id']]); + if ($trace) { + $trace->setStatus(TraceStatus::SUCCESS); + $trace->setFinishedAt(new \DateTime()); + $this->entityManager->persist($trace); + $this->entityManager->flush(); + } + } + + if (isset($data['nfn']) && $data['nfn'] === self::HARDWARE_INVENTORY) { + $trace = $this->entityManager->getRepository(Trace::class)->findOneBy(['jobId' => $data['job_id']]); + if ($trace) { + $client = $trace->getClient(); + + $trace->setStatus(TraceStatus::SUCCESS); + $trace->setFinishedAt(new \DateTime()); + $this->entityManager->persist($trace); + + //$client->setHardwareProfile(); + + $this->entityManager->flush(); + } + } + + if (isset($data['nfn']) && $data['nfn'] === self::SOFTWARE_INVENTORY) { + $trace = $this->entityManager->getRepository(Trace::class)->findOneBy(['jobId' => $data['job_id']]); + + if ($trace) { + $client = $trace->getClient(); + $dataInput = json_decode(json_encode($trace->getInput()), true); + $imageUuid = $dataInput['image'] ?? null; + + $trace->setStatus(TraceStatus::SUCCESS); + $trace->setFinishedAt(new \DateTime()); + $this->entityManager->persist($trace); + + $image = $this->entityManager->getRepository(ImageImageRepository::class)->findOneBy(['uuid' => $imageUuid]); + $this->createSoftwareProfile($data['contents'], $image); + + $this->entityManager->flush(); + } + } + return new JsonResponse(data: 'Webhook finished', status: Response::HTTP_OK); } - public function createSoftwareProfile(string $base64Data, ImageImageRepository $imageImageRepository): void + public function createSoftwareProfile(string $base64Data, ?ImageImageRepository $imageImageRepository = null): void { $decodedData = base64_decode($base64Data); $this->logger->info('Software profile decoded', ['data' => '']); @@ -200,10 +246,10 @@ class StatusController extends AbstractController $this->entityManager->persist($softwareEntity); } - $image = $imageImageRepository->getImage(); + $image = $imageImageRepository?->getImage(); $softwareProfile = new SoftwareProfile(); - $softwareProfile->setDescription('Perfil software: ' . $image->getClient()->getName()); + $softwareProfile->setDescription('SW Profile - ' . $image->getClient()->getName()); $softwareProfile->setOrganizationalUnit($image->getClient()->getOrganizationalUnit()); foreach ($existingSoftware as $softwareEntity) { diff --git a/src/Controller/OgBoot/OgBootController.php b/src/Controller/OgBoot/OgBootController.php index eaa71be..e328d2f 100644 --- a/src/Controller/OgBoot/OgBootController.php +++ b/src/Controller/OgBoot/OgBootController.php @@ -9,6 +9,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Routing\Attribute\Route; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; @@ -37,6 +38,10 @@ class OgBootController extends AbstractController { $data = $this->ogbootStatusService->__invoke(); + if (isset($data['error']) && $data['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) { + throw new BadRequestHttpException('An error occurred while fetching the status: ' . $data['details']); + } + return new JsonResponse( data: $data, status: Response::HTTP_OK); } } diff --git a/src/Controller/OgBoot/OgLive/GetAction.php b/src/Controller/OgBoot/OgLive/GetAction.php index 296d4bd..cceeddf 100644 --- a/src/Controller/OgBoot/OgLive/GetAction.php +++ b/src/Controller/OgBoot/OgLive/GetAction.php @@ -7,6 +7,7 @@ use App\Entity\OgLive; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; @@ -26,13 +27,13 @@ class GetAction extends AbstractOgBootController public function __invoke(OgLive $data): JsonResponse { if (!$data->getChecksum()) { - throw new ValidatorException('Checksum is required'); + throw new BadRequestHttpException('Checksum is required'); } $content = $this->createRequest('GET', '/ogboot/v1/oglives/'.$data->getChecksum()); - if (isset($content['error']) && $content['error'] === Response::HTTP_INTERNAL_SERVER_ERROR ) { - throw new ValidatorException('An error occurred: ' . $content['error']); + if (isset($content['error']) && $content['code'] === Response::HTTP_INTERNAL_SERVER_ERROR ) { + throw new BadRequestHttpException('An error occurred while fetching the OgLive: ' . $content['error']. ' (' . $content['details'] . ')'); } return new JsonResponse(data: $content, status: Response::HTTP_OK); diff --git a/src/Controller/OgBoot/OgLive/GetCollectionAction.php b/src/Controller/OgBoot/OgLive/GetCollectionAction.php index 2427f49..f328530 100644 --- a/src/Controller/OgBoot/OgLive/GetCollectionAction.php +++ b/src/Controller/OgBoot/OgLive/GetCollectionAction.php @@ -6,6 +6,7 @@ use App\Controller\OgBoot\AbstractOgBootController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; @@ -25,8 +26,8 @@ class GetCollectionAction extends AbstractOgBootController { $content = $this->createRequest('GET', '/ogboot/v1/oglives'); - if (isset($content['error']) && $content['error'] === Response::HTTP_INTERNAL_SERVER_ERROR ) { - throw new ValidatorException('An error occurred: ' . $content['error']); + if (isset($content['error']) && $content['code'] === Response::HTTP_INTERNAL_SERVER_ERROR ) { + throw new BadRequestHttpException('An error occurred while fetching the collection: ' . $content['error']. ' (' . $content['details'] . ')'); } return new JsonResponse(data: $content, status: Response::HTTP_OK); diff --git a/src/Controller/OgBoot/OgLive/GetDefaultAction.php b/src/Controller/OgBoot/OgLive/GetDefaultAction.php index 7b6ef5a..5feea2e 100644 --- a/src/Controller/OgBoot/OgLive/GetDefaultAction.php +++ b/src/Controller/OgBoot/OgLive/GetDefaultAction.php @@ -6,6 +6,7 @@ use App\Controller\OgBoot\AbstractOgBootController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; @@ -25,8 +26,8 @@ class GetDefaultAction extends AbstractOgBootController { $content = $this->createRequest('GET', '/ogboot/v1/oglives/default'); - if (isset($content['error']) && $content['error'] === Response::HTTP_INTERNAL_SERVER_ERROR ) { - throw new ValidatorException('An error occurred: ' . $content['error']); + if (isset($content['error']) && $content['code'] === Response::HTTP_INTERNAL_SERVER_ERROR ) { + throw new BadRequestHttpException('An error occurred: ' . $content['error']. ' (' . $content['details'] . ')'); } return new JsonResponse(status: Response::HTTP_OK); diff --git a/src/Controller/OgBoot/OgLive/GetIsosAction.php b/src/Controller/OgBoot/OgLive/GetIsosAction.php index 52e08c6..ff97ab5 100644 --- a/src/Controller/OgBoot/OgLive/GetIsosAction.php +++ b/src/Controller/OgBoot/OgLive/GetIsosAction.php @@ -7,6 +7,7 @@ use App\Entity\OgLive; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; @@ -25,8 +26,8 @@ class GetIsosAction extends AbstractOgBootController { $content = $this->createRequest('GET', '/ogboot/v1/oglives/isos'); - if (!isset($content['message']) || !is_array($content['message'])) { - return new JsonResponse(data: ['error' => 'Invalid response'], status: Response::HTTP_BAD_REQUEST); + if (isset($content['error']) && $content['code'] === Response::HTTP_INTERNAL_SERVER_ERROR ) { + throw new BadRequestHttpException('An error occurred while fetching ISOs: ' . $content['error']. ' (' . $content['details'] . ')'); } $isos = array_map(function ($iso) { diff --git a/src/Controller/OgBoot/OgLive/InstallAction.php b/src/Controller/OgBoot/OgLive/InstallAction.php index 95c4e91..5538bf9 100644 --- a/src/Controller/OgBoot/OgLive/InstallAction.php +++ b/src/Controller/OgBoot/OgLive/InstallAction.php @@ -11,6 +11,7 @@ use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; @@ -30,7 +31,7 @@ class InstallAction extends AbstractOgBootController public function __invoke(OgLive $data): JsonResponse { if (!$data->getDownloadUrl()) { - throw new ValidatorException('Download URL is required'); + throw new BadRequestHttpException('Download URL is required'); } $params = [ @@ -47,8 +48,8 @@ class InstallAction extends AbstractOgBootController $content = $this->createRequest('POST', '/ogboot/v1/oglives/install', $params); - if (isset($content['error']) && $content['error'] === Response::HTTP_INTERNAL_SERVER_ERROR ) { - throw new ValidatorException('An error occurred: ' . $content['error']); + if (isset($content['error']) && $content['code'] === Response::HTTP_INTERNAL_SERVER_ERROR ) { + throw new BadRequestHttpException('An error occurred while installing OgLive: ' . $content['error']. ' (' . $content['details'] . ')'); } $this->createService->__invoke(null, CommandTypes::INSTALL_OGLIVE, TraceStatus::IN_PROGRESS, 'InstallOgLive_'.$data->getUuid(), $inputData); diff --git a/src/Controller/OgBoot/OgLive/SetDefaultAction.php b/src/Controller/OgBoot/OgLive/SetDefaultAction.php index d148b1b..c9e4ca7 100644 --- a/src/Controller/OgBoot/OgLive/SetDefaultAction.php +++ b/src/Controller/OgBoot/OgLive/SetDefaultAction.php @@ -8,6 +8,7 @@ use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; @@ -27,7 +28,7 @@ class SetDefaultAction extends AbstractOgBootController public function __invoke(OgLive $data): JsonResponse { if (!$data->getChecksum()) { - throw new ValidatorException('Checksum URL is required'); + throw new BadRequestHttpException('Checksum URL is required'); } $params = [ @@ -38,8 +39,8 @@ class SetDefaultAction extends AbstractOgBootController $content = $this->createRequest('PUT', '/ogboot/v1/oglives/default', $params); - if (isset($content['error']) && $content['error'] === Response::HTTP_INTERNAL_SERVER_ERROR ) { - throw new ValidatorException('An error occurred: ' . $content['error']); + if (isset($content['error']) && $content['code'] === Response::HTTP_INTERNAL_SERVER_ERROR ) { + throw new BadRequestHttpException('An error occurred: ' . $content['error'] . ' (' . $content['details'] . ')'); } $oldDefaultOgLive = $this->entityManager->getRepository(OgLive::class)->findBy(['isDefault' => true]); diff --git a/src/Controller/OgBoot/OgLive/SyncAction.php b/src/Controller/OgBoot/OgLive/SyncAction.php index 13d790c..6bd57ae 100644 --- a/src/Controller/OgBoot/OgLive/SyncAction.php +++ b/src/Controller/OgBoot/OgLive/SyncAction.php @@ -9,7 +9,7 @@ use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; -use Symfony\Component\Validator\Exception\ValidatorException; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; @@ -32,8 +32,8 @@ class SyncAction extends AbstractOgBootController { $content = $this->createRequest('GET', '/ogboot/v1/oglives'); - if (isset($content['error']) && $content['error'] === Response::HTTP_INTERNAL_SERVER_ERROR ) { - throw new ValidatorException('An error occurred: ' . $content['error']); + if (isset($content['error']) && $content['code'] === Response::HTTP_INTERNAL_SERVER_ERROR ) { + throw new BadRequestHttpException('An error occurred while fetching ogLives: ' . $content['message'] . ' (' . $content['details'] . ')'); } $allOgLives = $this->entityManager->getRepository(OgLive::class)->findAll(); diff --git a/src/Controller/OgBoot/OgLive/UninstallAction.php b/src/Controller/OgBoot/OgLive/UninstallAction.php index 4d72aee..47948a0 100644 --- a/src/Controller/OgBoot/OgLive/UninstallAction.php +++ b/src/Controller/OgBoot/OgLive/UninstallAction.php @@ -8,6 +8,7 @@ use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; @@ -28,13 +29,13 @@ class UninstallAction extends AbstractOgBootController public function __invoke(OgLive $data): JsonResponse { if (!$data->getChecksum()) { - throw new ValidatorException('Checksum is required'); + throw new BadRequestHttpException('Checksum is required'); } $content = $this->createRequest( 'DELETE', '/ogboot/v1/oglives/'.$data->getChecksum()); - if (isset($content['error']) && $content['error'] === Response::HTTP_INTERNAL_SERVER_ERROR ) { - throw new ValidatorException('An error occurred: ' . $content['error']); + if (isset($content['error']) && $content['code'] === Response::HTTP_INTERNAL_SERVER_ERROR ) { + throw new BadRequestHttpException('An error occurred while uninstalling the OgLive: ' . $content['error'] . ' (' . $content['details'] . ')'); } $this->entityManager->remove($data); diff --git a/src/Controller/OgBoot/PxeBootFile/PostAction.php b/src/Controller/OgBoot/PxeBootFile/PostAction.php index 28e4b8a..97c5575 100644 --- a/src/Controller/OgBoot/PxeBootFile/PostAction.php +++ b/src/Controller/OgBoot/PxeBootFile/PostAction.php @@ -45,10 +45,10 @@ class PostAction extends AbstractOgBootController 'server_ip' => $this->ogBootIp, 'server_api_port' => $this->ogBootApiPort, 'server_pxe_port' => $this->ogBootPxePort, - 'router' => $client->getOrganizationalUnit()->getNetworkSettings()->getRouter(), + 'router' => $client->getOrganizationalUnit()->getNetworkSettings()?->getRouter(), 'netmask' => $client->getOrganizationalUnit()->getNetworkSettings() ? $client->getOrganizationalUnit()->getNetworkSettings()->getNetmask() : '255.255.255.0', 'computer_name' => $client->getName(), - 'netiface' => $client->getNetiface() ? $client->getNetiface() : $client->getOrganizationalUnit()->getNetworkSettings()->getNetiface(), + 'netiface' => $client->getNetiface() ? $client->getNetiface() : $client->getOrganizationalUnit()->getNetworkSettings()?->getNetiface(), 'group' => $client->getOrganizationalUnit()->getName(), 'ogrepo' => $ogRepoIp, 'ogcore' => $this->ogCoreIP, diff --git a/src/Controller/OgBoot/PxeTemplate/DeleteAction.php b/src/Controller/OgBoot/PxeTemplate/DeleteAction.php index f319303..51d97ef 100644 --- a/src/Controller/OgBoot/PxeTemplate/DeleteAction.php +++ b/src/Controller/OgBoot/PxeTemplate/DeleteAction.php @@ -10,6 +10,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; @@ -34,7 +35,7 @@ class DeleteAction extends AbstractOgBootController $content = $this->createRequest('DELETE', '/ogboot/v1/pxe-templates/'.$data->getName()); if (isset($content['error']) && $content['error'] === Response::HTTP_INTERNAL_SERVER_ERROR ) { - throw new ValidatorException('An error occurred: ' . $content['error']); + throw new BadRequestHttpException('An error occurred: ' . $content['error']); } $defaultTemplateEntity = $this->entityManager->getRepository(PxeTemplate::class)->findOneBy(['isDefault' => true]); diff --git a/src/Controller/OgBoot/PxeTemplate/GetCollectionAction.php b/src/Controller/OgBoot/PxeTemplate/GetCollectionAction.php index 936f2b4..7c63ae0 100644 --- a/src/Controller/OgBoot/PxeTemplate/GetCollectionAction.php +++ b/src/Controller/OgBoot/PxeTemplate/GetCollectionAction.php @@ -7,6 +7,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; @@ -28,7 +29,7 @@ class GetCollectionAction extends AbstractOgBootController $content = $this->createRequest('GET', '/ogboot/v1/pxe-templates'); if (isset($content['error']) && $content['error'] === Response::HTTP_INTERNAL_SERVER_ERROR ) { - throw new ValidatorException('An error occurred: ' . $content['error']); + throw new BadRequestHttpException('An error occurred: ' . $content['error']); } return new JsonResponse( data: $content, status: Response::HTTP_OK); diff --git a/src/Controller/OgBoot/PxeTemplate/PostAction.php b/src/Controller/OgBoot/PxeTemplate/PostAction.php index 42ff754..f22aece 100644 --- a/src/Controller/OgBoot/PxeTemplate/PostAction.php +++ b/src/Controller/OgBoot/PxeTemplate/PostAction.php @@ -9,6 +9,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; @@ -37,7 +38,7 @@ class PostAction extends AbstractOgBootController $content = $this->createRequest('POST', '/ogboot/v1/pxe-templates' , $params); if (isset($content['error']) && $content['error'] === Response::HTTP_INTERNAL_SERVER_ERROR ) { - throw new ValidatorException('An error occurred: ' . $content['error']); + throw new BadRequestHttpException('An error occurred: ' . $content['error']); } $data->setSynchronized(true); diff --git a/src/Controller/OgBoot/PxeTemplate/SyncAction.php b/src/Controller/OgBoot/PxeTemplate/SyncAction.php index 1d9e2b6..489aaca 100644 --- a/src/Controller/OgBoot/PxeTemplate/SyncAction.php +++ b/src/Controller/OgBoot/PxeTemplate/SyncAction.php @@ -10,6 +10,7 @@ use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; @@ -52,7 +53,7 @@ class SyncAction extends AbstractOgBootController $templateContent = $this->createRequest('GET', '/ogboot/v1/pxe-templates/' . $templateEntity->getName()); if (isset($templateContent['error']) && $templateContent['error'] === Response::HTTP_INTERNAL_SERVER_ERROR ) { - throw new ValidatorException('An error occurred: ' . $templateContent['error']); + throw new BadRequestHttpException('An error occurred: ' . $templateContent['error']); } $templateEntity->setTemplateContent($templateContent['template_content']); diff --git a/src/Controller/OgDhcp/Subnet/DeleteAction.php b/src/Controller/OgDhcp/Subnet/DeleteAction.php index 4d1b5db..383e587 100644 --- a/src/Controller/OgDhcp/Subnet/DeleteAction.php +++ b/src/Controller/OgDhcp/Subnet/DeleteAction.php @@ -7,6 +7,7 @@ use App\Entity\Subnet; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; @@ -27,7 +28,7 @@ class DeleteAction extends AbstractOgDhcpController public function __invoke(Subnet $data, HttpClientInterface $httpClient): JsonResponse { if (!$data->getId()) { - throw new ValidatorException('Data Id is required'); + throw new BadRequestHttpException('Data Id is required'); } try { diff --git a/src/Controller/OgDhcp/Subnet/DeleteHostAction.php b/src/Controller/OgDhcp/Subnet/DeleteHostAction.php index 1dc1a1e..0346ff7 100644 --- a/src/Controller/OgDhcp/Subnet/DeleteHostAction.php +++ b/src/Controller/OgDhcp/Subnet/DeleteHostAction.php @@ -8,6 +8,7 @@ use App\Entity\Subnet; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; @@ -29,11 +30,11 @@ class DeleteHostAction extends AbstractOgDhcpController $client = $this->entityManager->getRepository(Client::class)->findOneBy(['uuid' => $clientUuid]); if (!$client || $client->getSubnet() !== $data) { - throw new ValidatorException('Client not found'); + throw new BadRequestHttpException('Client not found'); } if (!$data->getId()) { - throw new ValidatorException('Data URL is required'); + throw new BadRequestHttpException('Data URL is required'); } $params = [ diff --git a/src/Controller/OgDhcp/Subnet/GetAction.php b/src/Controller/OgDhcp/Subnet/GetAction.php index fcb1cf7..b9e7ee2 100644 --- a/src/Controller/OgDhcp/Subnet/GetAction.php +++ b/src/Controller/OgDhcp/Subnet/GetAction.php @@ -7,6 +7,7 @@ use App\Entity\Subnet; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; @@ -26,7 +27,7 @@ class GetAction extends AbstractOgDhcpController public function __invoke(Subnet $data): JsonResponse { if (!$data->getId()) { - throw new ValidatorException('Checksum is required'); + throw new BadRequestHttpException('Checksum is required'); } $content = $this->createRequest('GET', 'http://'.$this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$data->getServerId()); diff --git a/src/Controller/OgDhcp/Subnet/GetHostsAction.php b/src/Controller/OgDhcp/Subnet/GetHostsAction.php index 2d553bb..d0334dd 100644 --- a/src/Controller/OgDhcp/Subnet/GetHostsAction.php +++ b/src/Controller/OgDhcp/Subnet/GetHostsAction.php @@ -7,6 +7,7 @@ use App\Entity\Subnet; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; @@ -26,7 +27,7 @@ class GetHostsAction extends AbstractOgDhcpController public function __invoke(Subnet $data): JsonResponse { if (!$data->getId()) { - throw new ValidatorException('Checksum is required'); + throw new BadRequestHttpException('Checksum is required'); } $content = $this->createRequest('GET', 'http://'.$this->ogDhcpApiUrl.'/ogdhcp/v1/subnets/'.$data->getServerId().'/hosts'); diff --git a/src/Controller/OgDhcp/Subnet/PutAction.php b/src/Controller/OgDhcp/Subnet/PutAction.php index fbaad21..2d2c91d 100644 --- a/src/Controller/OgDhcp/Subnet/PutAction.php +++ b/src/Controller/OgDhcp/Subnet/PutAction.php @@ -7,6 +7,7 @@ use App\Entity\Subnet; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; @@ -26,7 +27,7 @@ class PutAction extends AbstractOgDhcpController public function __invoke(Subnet $data): JsonResponse { if (!$data->getId()) { - throw new ValidatorException('Id is required'); + throw new BadRequestHttpException('Id is required'); } $params = [ diff --git a/src/Controller/OgRepository/Git/CreateRepositoryAction.php b/src/Controller/OgRepository/Git/CreateRepositoryAction.php index 4abca75..f90aac8 100644 --- a/src/Controller/OgRepository/Git/CreateRepositoryAction.php +++ b/src/Controller/OgRepository/Git/CreateRepositoryAction.php @@ -8,6 +8,7 @@ use App\Entity\ImageRepository; use Symfony\Component\HttpKernel\Attribute\AsController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; @@ -34,7 +35,7 @@ class CreateRepositoryAction extends AbstractOgRepositoryController $content = $this->createRequest('POST', 'http://'.$repository->getIp().':8006/ogrepository/v1/git/repositories', $params); if (isset($content['error']) && $content['code'] === Response::HTTP_INTERNAL_SERVER_ERROR ) { - throw new ValidatorException('Error creating repository'); + throw new BadRequestHttpException('Error creating repository'); } return new JsonResponse(data: [], status: Response::HTTP_OK); diff --git a/src/Controller/OgRepository/Git/CreateTagAction.php b/src/Controller/OgRepository/Git/CreateTagAction.php index d904418..43c491e 100644 --- a/src/Controller/OgRepository/Git/CreateTagAction.php +++ b/src/Controller/OgRepository/Git/CreateTagAction.php @@ -9,6 +9,7 @@ use App\Entity\ImageRepository; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; @@ -38,7 +39,7 @@ class CreateTagAction extends AbstractOgRepositoryController $image->getName().'/tags', $params); if (isset($content['error']) && $content['code'] === Response::HTTP_INTERNAL_SERVER_ERROR ) { - throw new ValidatorException('Error creating repository'); + throw new BadRequestHttpException('Error creating repository'); } return new JsonResponse(data: [], status: Response::HTTP_OK); diff --git a/src/Controller/OgRepository/Git/GetTagsAction.php b/src/Controller/OgRepository/Git/GetTagsAction.php index 213dd01..55a94d3 100644 --- a/src/Controller/OgRepository/Git/GetTagsAction.php +++ b/src/Controller/OgRepository/Git/GetTagsAction.php @@ -7,6 +7,7 @@ use App\Entity\GitImageRepository; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; @@ -30,7 +31,7 @@ class GetTagsAction extends AbstractOgRepositoryController $content = $this->createRequest('GET', 'http://'.$repository->getIp().':8006/ogrepository/v1/git/repositories/'.$image->getName().'/tags'); if (isset($content['error']) && $content['code'] === Response::HTTP_INTERNAL_SERVER_ERROR ) { - throw new ValidatorException('Error getting tags'); + throw new BadRequestHttpException('Error getting tags'); } return new JsonResponse(data: $content, status: Response::HTTP_OK); diff --git a/src/Controller/OgRepository/Image/BackupImageAction.php b/src/Controller/OgRepository/Image/BackupImageAction.php index 51e76b4..aaf3d1f 100644 --- a/src/Controller/OgRepository/Image/BackupImageAction.php +++ b/src/Controller/OgRepository/Image/BackupImageAction.php @@ -14,7 +14,7 @@ use App\Model\TraceStatus; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; -use Symfony\Component\Validator\Exception\ValidatorException; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; @@ -35,7 +35,7 @@ class BackupImageAction extends AbstractOgRepositoryController $repository = $imageImageRepository->getRepository(); if (!$image->getName()) { - throw new ValidatorException('Name is required'); + throw new BadRequestHttpException('Name is required'); } $params = [ @@ -54,6 +54,10 @@ class BackupImageAction extends AbstractOgRepositoryController $content = $this->createRequest('PUT', 'http://'.$repository->getIp().':8006/ogrepository/v1/repo/images', $params); + if (isset($content['error']) && $content['code'] === Response::HTTP_INTERNAL_SERVER_ERROR ) { + throw new BadRequestHttpException('Error backing up image: ' . $content['error'] . ' - ' . $content['details']); + } + $inputData = [ 'imageName' => $image->getName(), 'repositoryUuid' => $repository->getUuid(), diff --git a/src/Controller/OgRepository/Image/CancelTransmissionAction.php b/src/Controller/OgRepository/Image/CancelTransmissionAction.php index c7b84a8..5ccfca0 100644 --- a/src/Controller/OgRepository/Image/CancelTransmissionAction.php +++ b/src/Controller/OgRepository/Image/CancelTransmissionAction.php @@ -13,7 +13,7 @@ use App\Model\TraceStatus; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; -use Symfony\Component\Validator\Exception\ValidatorException; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; @@ -32,25 +32,25 @@ class CancelTransmissionAction extends AbstractOgRepositoryController public function __invoke(Trace $data): JsonResponse { if ($data->getCommand() !== CommandTypes::DEPLOY_IMAGE) { - throw new ValidatorException('Command is not DEPLOY_IMAGE'); + throw new BadRequestHttpException('Command is not DEPLOY_IMAGE'); } $input = $data->getInput(); if (!isset($input['client']) || !isset($input['image']) || !isset($input['method'])) { - throw new ValidatorException('Client, image and method are required'); + throw new BadRequestHttpException('Client, image and method are required'); } $client = $this->entityManager->getRepository(Client::class)->findOneBy(['uuid' => $input['client']]); $image = $this->entityManager->getRepository(ImageImageRepository::class)->findOneBy(['uuid' => $input['image']]); if (!$client || !$image) { - throw new ValidatorException('Client or image not found'); + throw new BadRequestHttpException('Client or image not found'); } $method = $input['method']; if (!$image->getImageFullsum()) { - throw new ValidatorException('Fullsum is required'); + throw new BadRequestHttpException('Fullsum is required'); } if ($method === DeployMethodTypes::TORRENT) { @@ -60,7 +60,7 @@ class CancelTransmissionAction extends AbstractOgRepositoryController } if (isset($content['error']) && $content['code'] === Response::HTTP_INTERNAL_SERVER_ERROR ) { - throw new ValidatorException('Error cancelling transmission'); + throw new BadRequestHttpException('Error cancelling transmission: ' . $content['details']); } $data->setStatus(TraceStatus::CANCELLED); diff --git a/src/Controller/OgRepository/Image/ConvertAction.php b/src/Controller/OgRepository/Image/ConvertAction.php index ff3a4bd..820d75c 100644 --- a/src/Controller/OgRepository/Image/ConvertAction.php +++ b/src/Controller/OgRepository/Image/ConvertAction.php @@ -14,8 +14,8 @@ use App\Model\TraceStatus; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; -use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; @@ -40,6 +40,7 @@ class ConvertAction extends AbstractOgRepositoryController if (!$imageEntity){ $imageEntity = new Image(); $imageEntity->setName($image); + $imageEntity->setType('monolithic'); $imageEntity->setRemotePc(false); $imageEntity->setIsGlobal(false); @@ -49,7 +50,7 @@ class ConvertAction extends AbstractOgRepositoryController $imageImageRepositoryEntity = $this->entityManager->getRepository(ImageImageRepository::class)->findOneBy(['image' => $imageEntity, 'repository' => $repository]); if ($imageImageRepositoryEntity){ - throw new ValidatorException('This image already exists in this repository'); + throw new BadRequestHttpException('This image already exists in this repository'); } $imageImageRepositoryEntity = new ImageImageRepository(); @@ -73,7 +74,7 @@ class ConvertAction extends AbstractOgRepositoryController $content = $this->createRequest('POST', 'http://'.$repository->getIp().':8006/ogrepository/v1/images/virtual', $params); if (isset($content['error']) && $content['code'] === Response::HTTP_INTERNAL_SERVER_ERROR ) { - throw new ValidatorException('Error converting image'); + throw new BadRequestHttpException('An error occurred while converting the image: ' . $content['error'] . ' - ' . $content['details']); } $this->entityManager->flush(); diff --git a/src/Controller/OgRepository/Image/ConvertImageToVirtualAction.php b/src/Controller/OgRepository/Image/ConvertImageToVirtualAction.php index 7591e08..1e35c1a 100644 --- a/src/Controller/OgRepository/Image/ConvertImageToVirtualAction.php +++ b/src/Controller/OgRepository/Image/ConvertImageToVirtualAction.php @@ -12,7 +12,7 @@ use App\Model\TraceStatus; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; -use Symfony\Component\Validator\Exception\ValidatorException; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; @@ -32,7 +32,7 @@ class ConvertImageToVirtualAction extends AbstractOgRepositoryController $image = $imageImageRepository->getImage(); if (!$image->getName()) { - throw new ValidatorException('Name is required'); + throw new BadRequestHttpException('Name is required'); } $params = [ @@ -48,8 +48,8 @@ class ConvertImageToVirtualAction extends AbstractOgRepositoryController $content = $this->createRequest('PUT', 'http://'.$repository->getIp().':8006/ogrepository/v1/images/virtual', $params); - if (isset($content['error']) && $content['code'] === Response::HTTP_INTERNAL_SERVER_ERROR ) { - throw new ValidatorException('Error converting image'); + if (isset($content['error']) && $content['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) { + throw new BadRequestHttpException('An error occurred while converting the image to virtual: ' . $content['error']); } $inputData = [ diff --git a/src/Controller/OgRepository/Image/CreateAuxFilesAction.php b/src/Controller/OgRepository/Image/CreateAuxFilesAction.php index aaf0bdb..8e854d0 100644 --- a/src/Controller/OgRepository/Image/CreateAuxFilesAction.php +++ b/src/Controller/OgRepository/Image/CreateAuxFilesAction.php @@ -13,7 +13,7 @@ use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; -use Symfony\Component\Validator\Exception\ValidatorException; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; @@ -34,7 +34,7 @@ class CreateAuxFilesAction extends AbstractOgRepositoryController $image = $data->getImage(); if (!$image->getName()) { - throw new ValidatorException('Name is required'); + throw new BadRequestHttpException('Name is required'); } $params = [ @@ -49,6 +49,10 @@ class CreateAuxFilesAction extends AbstractOgRepositoryController $content = $this->createRequest('POST', 'http://'.$repository->getIp().':8006/ogrepository/v1/images/torrentsum', $params); + if (isset($content['error']) && $content['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) { + throw new BadRequestHttpException('An error occurred while creating aux files: ' . $content['error'] . ' - ' . $content['details']); + } + $inputData = [ 'imageName' => $data->getName(), 'imageImageRepositoryUuid' => $data->getUuid(), diff --git a/src/Controller/OgRepository/Image/DeletePermanentAction.php b/src/Controller/OgRepository/Image/DeletePermanentAction.php index 892936a..ecc0561 100644 --- a/src/Controller/OgRepository/Image/DeletePermanentAction.php +++ b/src/Controller/OgRepository/Image/DeletePermanentAction.php @@ -9,7 +9,7 @@ use App\Entity\ImageRepository; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; -use Symfony\Component\Validator\Exception\ValidatorException; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; @@ -28,7 +28,7 @@ class DeletePermanentAction extends AbstractOgRepositoryController public function __invoke(ImageImageRepository $data, HttpClientInterface $httpClient): JsonResponse { if (!$data->getImageFullsum()) { - throw new ValidatorException('Fullsum is required'); + throw new BadRequestHttpException('Fullsum is required'); } /** @var ImageRepository $image */ diff --git a/src/Controller/OgRepository/Image/DeleteTrashAction.php b/src/Controller/OgRepository/Image/DeleteTrashAction.php index 31c245e..170ced5 100644 --- a/src/Controller/OgRepository/Image/DeleteTrashAction.php +++ b/src/Controller/OgRepository/Image/DeleteTrashAction.php @@ -11,7 +11,7 @@ use App\Model\ImageStatus; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; -use Symfony\Component\Validator\Exception\ValidatorException; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; @@ -32,7 +32,7 @@ class DeleteTrashAction extends AbstractOgRepositoryController $image = $imageImageRepository->getImage(); if (!$imageImageRepository->getImageFullsum()) { - throw new ValidatorException('Fullsum is required'); + throw new BadRequestHttpException('Fullsum is required'); } $this->logger->info('Deleting image', ['image' => $image->getName()]); diff --git a/src/Controller/OgRepository/Image/GetAction.php b/src/Controller/OgRepository/Image/GetAction.php index fbd3626..da01c99 100644 --- a/src/Controller/OgRepository/Image/GetAction.php +++ b/src/Controller/OgRepository/Image/GetAction.php @@ -8,7 +8,7 @@ use App\Entity\ImageImageRepository; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; -use Symfony\Component\Validator\Exception\ValidatorException; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; @@ -27,11 +27,15 @@ class GetAction extends AbstractOgRepositoryController public function __invoke(ImageImageRepository $data): JsonResponse { if (!$data->getImageFullsum()) { - throw new ValidatorException('Fullsum is required'); + throw new BadRequestHttpException('Fullsum is required'); } $content = $this->createRequest('GET', 'http://'.$data->getRepository()->getIp().':8006/ogrepository/v1/images/'.$data->getImageFullsum()); + if (isset($content['error']) && $content['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) { + throw new BadRequestHttpException('An error occurred while fetching the image: ' . $content['details']); + } + return new JsonResponse(data: $content, status: Response::HTTP_OK); } } \ No newline at end of file diff --git a/src/Controller/OgRepository/Image/GetCollectionAction.php b/src/Controller/OgRepository/Image/GetCollectionAction.php index 26a01c7..f2ce6c6 100644 --- a/src/Controller/OgRepository/Image/GetCollectionAction.php +++ b/src/Controller/OgRepository/Image/GetCollectionAction.php @@ -7,6 +7,7 @@ use App\Entity\ImageRepository; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; @@ -25,6 +26,10 @@ class GetCollectionAction extends AbstractOgRepositoryController { $content = $this->createRequest('GET', 'http://'.$data->getIp(). ':8006/ogrepository/v1/images'); + if (isset($content['error']) && $content['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) { + throw new BadRequestHttpException('An error occurred while fetching the image collection: ' . $content['details']); + } + return new JsonResponse( data: $content, status: Response::HTTP_OK); } } \ No newline at end of file diff --git a/src/Controller/OgRepository/Image/GetStatusAction.php b/src/Controller/OgRepository/Image/GetStatusAction.php index 37d5085..58ff966 100644 --- a/src/Controller/OgRepository/Image/GetStatusAction.php +++ b/src/Controller/OgRepository/Image/GetStatusAction.php @@ -8,7 +8,7 @@ use App\Entity\ImageImageRepository; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; -use Symfony\Component\Validator\Exception\ValidatorException; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; @@ -27,13 +27,13 @@ class GetStatusAction extends AbstractOgRepositoryController public function __invoke(ImageImageRepository $data): JsonResponse { if (!$data->getImageFullsum()) { - throw new ValidatorException('Fullsum is required'); + throw new BadRequestHttpException('Fullsum is required'); } $content = $this->createRequest('GET', 'http://'.$data->getRepository()->getIp().':8006/ogrepository/v1/status/images/'.$data->getImageFullsum()); - if (isset($content['error']) && $content['error'] === Response::HTTP_INTERNAL_SERVER_ERROR ) { - throw new ValidatorException('Error getting status'); + if (isset($content['error']) && $content['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) { + throw new BadRequestHttpException('An error occurred while fetching the image status: ' . $content['details']); } return new JsonResponse(data: $content, status: Response::HTTP_OK); diff --git a/src/Controller/OgRepository/Image/ImportAction.php b/src/Controller/OgRepository/Image/ImportAction.php index 4e1af2f..6adc2dd 100644 --- a/src/Controller/OgRepository/Image/ImportAction.php +++ b/src/Controller/OgRepository/Image/ImportAction.php @@ -13,8 +13,8 @@ use App\Model\TraceStatus; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; -use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; @@ -31,6 +31,12 @@ class ImportAction extends AbstractOgRepositoryController */ public function __invoke(ImportImageRepositoryInput $input, ImageRepository $repository): JsonResponse { + $content = $this->createRequest('GET', 'http://'.$repository->getIp(). ':8006/ogrepository/v1/status'); + + if (isset($content['error']) && $content['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) { + throw new BadRequestHttpException('An error occurred while fetching the status: ' . $content['details']); + } + $image = $input->name; $imageEntity = $this->entityManager->getRepository(Image::class)->findOneBy(['name' => $image]); @@ -38,6 +44,7 @@ class ImportAction extends AbstractOgRepositoryController if (!$imageEntity){ $imageEntity = new Image(); $imageEntity->setName($image); + $imageEntity->setType('monolithic'); $imageEntity->setRemotePc(false); $imageEntity->setIsGlobal(false); @@ -47,7 +54,7 @@ class ImportAction extends AbstractOgRepositoryController $imageImageRepositoryEntity = $this->entityManager->getRepository(ImageImageRepository::class)->findOneBy(['image' => $imageEntity, 'repository' => $repository]); if ($imageImageRepositoryEntity){ - throw new ValidatorException('This image already exists in this repository'); + throw new BadRequestHttpException('This image already exists in this repository'); } $imageImageRepositoryEntity = new ImageImageRepository(); @@ -71,7 +78,7 @@ class ImportAction extends AbstractOgRepositoryController $content = $this->createRequest('POST', 'http://'.$repository->getIp().':8006/ogrepository/v1/images/torrentsum', $params); if (isset($content['error']) && $content['code'] === Response::HTTP_INTERNAL_SERVER_ERROR ) { - throw new ValidatorException('Error importing image'); + throw new BadRequestHttpException('Error importing image' . ' - ' . $content['error'] . ' - ' . $content['details']); } $inputData = [ diff --git a/src/Controller/OgRepository/Image/RecoverAction.php b/src/Controller/OgRepository/Image/RecoverAction.php index 1b9e4bf..3f25ec9 100644 --- a/src/Controller/OgRepository/Image/RecoverAction.php +++ b/src/Controller/OgRepository/Image/RecoverAction.php @@ -12,7 +12,7 @@ use App\Model\TraceStatus; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; -use Symfony\Component\Validator\Exception\ValidatorException; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; @@ -31,7 +31,7 @@ class RecoverAction extends AbstractOgRepositoryController public function __invoke(ImageImageRepository $data, HttpClientInterface $httpClient): JsonResponse { if (!$data->getImageFullsum()) { - throw new ValidatorException('Fullsum is required'); + throw new BadRequestHttpException('Fullsum is required'); } $image = $data->getImage(); @@ -48,6 +48,10 @@ class RecoverAction extends AbstractOgRepositoryController $content = $this->createRequest('POST', 'http://'.$repository->getIp().':8006/ogrepository/v1/trash/images', $params); + if (isset($content['error']) && $content['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) { + throw new BadRequestHttpException('An error occurred while recovering the image: ' . $content['error'] . ' - ' . $content['details']); + } + $this->logger->info('Image recovered successfully', ['image' => $image->getName()]); $data->setStatus(ImageStatus::SUCCESS); diff --git a/src/Controller/OgRepository/Image/RenameAction.php b/src/Controller/OgRepository/Image/RenameAction.php index 6e41e16..5c481b2 100644 --- a/src/Controller/OgRepository/Image/RenameAction.php +++ b/src/Controller/OgRepository/Image/RenameAction.php @@ -14,7 +14,7 @@ use App\Model\TraceStatus; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; -use Symfony\Component\Validator\Exception\ValidatorException; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; @@ -69,7 +69,7 @@ class RenameAction extends AbstractOgRepositoryController $this->entityManager->flush(); if ($hasError) { - return new JsonResponse(['error' => 'Error renaming image'], Response::HTTP_INTERNAL_SERVER_ERROR); + throw new BadRequestHttpException('An error occurred while renaming the image: ' . $content['error'] . ' - ' . $content['details']); } return new JsonResponse([], Response::HTTP_OK); @@ -112,6 +112,7 @@ class RenameAction extends AbstractOgRepositoryController 'http://' . $repository->getRepository()->getIp() . ':8006/ogrepository/v1/images/rename', $params ); + } private function jsonError(string $message): JsonResponse diff --git a/src/Controller/OgRepository/Image/TransferAction.php b/src/Controller/OgRepository/Image/TransferAction.php index 1e3bb34..61e3088 100644 --- a/src/Controller/OgRepository/Image/TransferAction.php +++ b/src/Controller/OgRepository/Image/TransferAction.php @@ -14,7 +14,7 @@ use App\Model\TraceStatus; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; -use Symfony\Component\Validator\Exception\ValidatorException; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; @@ -40,7 +40,7 @@ class TransferAction extends AbstractOgRepositoryController $image = $imageImageRepository->getImage(); if (!$imageImageRepository->getImageFullsum()) { - throw new ValidatorException('Fullsum is required'); + throw new BadRequestHttpException('Fullsum is required'); } $params = [ @@ -56,8 +56,8 @@ class TransferAction extends AbstractOgRepositoryController $content = $this->createRequest('POST', 'http://'.$repository->getIp().':8006/ogrepository/v1/repo/images', $params); - if (!isset($content['job_id'])) { - throw new ValidatorException('Job ID not found'); + if (isset($content['error']) && $content['code'] === Response::HTTP_INTERNAL_SERVER_ERROR ) { + throw new BadRequestHttpException('Error transferring image: ' . $content['error']); } $inputData = [ diff --git a/src/Controller/OgRepository/Image/TransferGlobalAction.php b/src/Controller/OgRepository/Image/TransferGlobalAction.php index cf2c57b..e0d8d12 100644 --- a/src/Controller/OgRepository/Image/TransferGlobalAction.php +++ b/src/Controller/OgRepository/Image/TransferGlobalAction.php @@ -14,7 +14,7 @@ use Doctrine\Common\Collections\ArrayCollection; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; -use Symfony\Component\Validator\Exception\ValidatorException; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; @@ -58,7 +58,7 @@ class TransferGlobalAction extends AbstractOgRepositoryController $content = $this->createRequest('POST', 'http://'.$repository->getIp().':8006/ogrepository/v1/repo/images', $params); if (!isset($content['job_id'])) { - throw new ValidatorException('Job ID not found'); + throw new BadRequestHttpException('Job ID not found'); } $inputData = [ diff --git a/src/Controller/OgRepository/StatusAction.php b/src/Controller/OgRepository/StatusAction.php index 18945b4..39da0b9 100644 --- a/src/Controller/OgRepository/StatusAction.php +++ b/src/Controller/OgRepository/StatusAction.php @@ -6,6 +6,7 @@ use App\Entity\ImageRepository; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; @@ -25,6 +26,10 @@ class StatusAction extends AbstractOgRepositoryController { $content = $this->createRequest('GET', 'http://'.$data->getIp(). ':8006/ogrepository/v1/status'); + if (isset($content['error']) && $content['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) { + throw new BadRequestHttpException('An error occurred while fetching the status: ' . $content['details']); + } + return new JsonResponse( data: $content, status: Response::HTTP_OK); } } \ No newline at end of file diff --git a/src/Controller/OgRepository/WoLAction.php b/src/Controller/OgRepository/WoLAction.php index 814d7b9..c712ac0 100644 --- a/src/Controller/OgRepository/WoLAction.php +++ b/src/Controller/OgRepository/WoLAction.php @@ -20,6 +20,7 @@ use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; @@ -44,7 +45,7 @@ class WoLAction extends AbstractOgRepositoryController $repository = $client->getRepository(); if (!$repository->getIp()) { - throw new ValidatorException('IP is required'); + throw new BadRequestHttpException('IP is required'); } $params = [ @@ -58,7 +59,7 @@ class WoLAction extends AbstractOgRepositoryController $content = $this->createRequest('POST', 'http://'.$repository->getIp(). ':8006/ogrepository/v1/wol', $params); - if (isset($content['error']) && $content['error'] === Response::HTTP_INTERNAL_SERVER_ERROR ) { + if (isset($content['error']) && $content['code'] === Response::HTTP_INTERNAL_SERVER_ERROR ) { $this->logger->error('Error sending WoL to client', ['mac' => $client->getMac()]); continue; } diff --git a/src/Dto/Input/SoftwareInventoryPartitionInput.php b/src/Dto/Input/SoftwareInventoryPartitionInput.php new file mode 100644 index 0000000..161fdbf --- /dev/null +++ b/src/Dto/Input/SoftwareInventoryPartitionInput.php @@ -0,0 +1,13 @@ + 'onKernelException', + ]; + } + + public function onKernelException(ExceptionEvent $event): void + { + if ($this->kernel->getEnvironment() === 'dev') { + return; + } + + $exception = $event->getThrowable(); + $statusCode = $exception instanceof HttpExceptionInterface ? $exception->getStatusCode() : 500; + $message = $exception->getMessage(); + + $data = [ + '@context' => '/contexts/Error', + '@type' => 'hydra:Error', + 'hydra:title' => 'An error occurred', + 'hydra:description' => $message, + ]; + + $response = new JsonResponse($data, $statusCode); + $event->setResponse($response); + } + +} diff --git a/src/EventSubscriber/ClientSubscriber.php b/src/EventSubscriber/ClientSubscriber.php index ed08088..e7ce4ae 100644 --- a/src/EventSubscriber/ClientSubscriber.php +++ b/src/EventSubscriber/ClientSubscriber.php @@ -67,6 +67,10 @@ final readonly class ClientSubscriber implements EventSubscriberInterface } $this->postAction->__invoke($client, $template); + + if (!$client->getSubnet()) { + return; + } $this->putHostAction->__invoke($client->getMac(), $client); } } \ No newline at end of file diff --git a/src/EventSubscriber/OrganizationalUnitSubscriber.php b/src/EventSubscriber/OrganizationalUnitSubscriber.php index 4e0c93f..30591b1 100644 --- a/src/EventSubscriber/OrganizationalUnitSubscriber.php +++ b/src/EventSubscriber/OrganizationalUnitSubscriber.php @@ -59,7 +59,7 @@ final readonly class OrganizationalUnitSubscriber implements EventSubscriberInte return; } - $newNetworkSettings = $this->buildNetworkSettings($organizationalUnitEntity); + $newNetworkSettings = $this->cloneNetworkSettings($organizationalUnitEntity->getNetworkSettings()); $this->updateChildrenNetworkSettings($organizationalUnitEntity, $newNetworkSettings); @@ -76,6 +76,7 @@ final readonly class OrganizationalUnitSubscriber implements EventSubscriberInte if ($childUnit->isExcludeParentChanges()) { $childUnit->setNetworkSettings(null); } else{ + $childUnit->setNetworkSettings($networkSettings); foreach ($childUnit->getClients() as $client) { @@ -85,7 +86,6 @@ final readonly class OrganizationalUnitSubscriber implements EventSubscriberInte $client->setTemplate($networkSettings->getPxeTemplate()); $this->entityManager->persist($client); } - } $this->entityManager->persist($childUnit); $this->entityManager->flush(); @@ -94,6 +94,31 @@ final readonly class OrganizationalUnitSubscriber implements EventSubscriberInte } } + private function cloneNetworkSettings(NetworkSettings $original): NetworkSettings + { + $cloned = new NetworkSettings(); + + $cloned->setNextServer($original->getNextServer()); + $cloned->setBootFileName($original->getBootFileName()); + $cloned->setProxy($original->getProxy()); + $cloned->setDns($original->getDns()); + $cloned->setNetmask($original->getNetmask()); + $cloned->setRouter($original->getRouter()); + $cloned->setP2pTime($original->getP2pTime()); + $cloned->setP2pMode($original->getP2pMode()); + $cloned->setMcastMode($original->getMcastMode()); + $cloned->setMcastIp($original->getMcastIp()); + $cloned->setMcastPort($original->getMcastPort()); + $cloned->setMcastSpeed($original->getMcastSpeed()); + $cloned->setMenu($original->getMenu()); + $cloned->setRepository($original->getRepository()); + $cloned->setOgLive($original->getOgLive()); + $cloned->setPxeTemplate($original->getPxeTemplate()); + $cloned->setNetiface($original->getNetiface()); + + return $cloned; + } + private function buildNetworkSettings($organizationalUnitEntity): NetworkSettings { if ($organizationalUnitEntity->getNetworkSettings() === null) { diff --git a/src/Model/CommandTypes.php b/src/Model/CommandTypes.php index 4f31c21..63c48eb 100644 --- a/src/Model/CommandTypes.php +++ b/src/Model/CommandTypes.php @@ -23,6 +23,9 @@ final class CommandTypes public const string PARTITION_AND_FORMAT = 'partition-and-format'; public const string INSTALL_OGLIVE = 'install-oglive'; public const string RUN_SCRIPT = 'run-script'; + public const string REMOVE_CACHE_IMAGE = 'remove-cache-image'; + public const string HARDWARE_INVENTORY = 'hardware-inventory'; + public const string SOFTWARE_INVENTORY = 'software-inventory'; private const array COMMAND_TYPES = [ self::DEPLOY_IMAGE => 'Deploy Image', @@ -44,6 +47,9 @@ final class CommandTypes self::TRANSFER_IMAGE => 'Transfer Image', self::INSTALL_OGLIVE => 'Instalar OgLive', self::RUN_SCRIPT => 'Run Script', + self::REMOVE_CACHE_IMAGE => 'Remove Cache Image', + self::HARDWARE_INVENTORY => 'Hardware Inventory', + self::SOFTWARE_INVENTORY => 'Software Inventory', ]; public static function getCommandTypes(): array diff --git a/src/Service/OgBoot/StatusService.php b/src/Service/OgBoot/StatusService.php index f11fb10..3007aee 100644 --- a/src/Service/OgBoot/StatusService.php +++ b/src/Service/OgBoot/StatusService.php @@ -40,15 +40,25 @@ readonly class StatusService ]); try { - $response = $httpClient->request('GET', 'http://'.$this->ogBootIp.':'.$this->ogBootApiPort.'/ogboot/v1/status', [ - 'headers' => [ - 'accept' => 'application/json', - ], + $response = $httpClient->request('GET', 'http://' . $this->ogBootIp . ':' . $this->ogBootApiPort . '/ogboot/v1/status', [ + 'headers' => ['accept' => 'application/json'], ]); - } catch (TransportExceptionInterface $e) { - return new JsonResponse( data: 'An error occurred', status: Response::HTTP_INTERNAL_SERVER_ERROR); - } - return json_decode($response->getContent(), true); + return json_decode($response->getContent(), true); + + } catch (ClientExceptionInterface | ServerExceptionInterface $e) { + return [ + 'code' => Response::HTTP_INTERNAL_SERVER_ERROR, + 'error' => 'Client/Server error', + 'details' => $e->getMessage(), + ]; + } catch (TransportExceptionInterface $e) { + return [ + 'code' => Response::HTTP_INTERNAL_SERVER_ERROR, + 'error' => 'Transport error', + 'details' => $e->getMessage(), + ]; + } } + } \ No newline at end of file diff --git a/src/State/Processor/ClientProcessor.php b/src/State/Processor/ClientProcessor.php index 69c07e4..528ce64 100644 --- a/src/State/Processor/ClientProcessor.php +++ b/src/State/Processor/ClientProcessor.php @@ -76,15 +76,15 @@ readonly class ClientProcessor implements ProcessorInterface $client = $data->createOrUpdateEntity($entity); - if ($defaultMenu) { + if ($defaultMenu && !$client->getMenu()) { $client->setMenu($defaultMenu); } - if ($defaultPxe) { + if ($defaultPxe && !$client->getTemplate()) { $client->setTemplate($defaultPxe); } - if ($defaultPxeOgLive) { + if ($defaultPxeOgLive && !$client->getOgLive()) { $client->setOgLive($defaultPxeOgLive); } @@ -106,7 +106,10 @@ readonly class ClientProcessor implements ProcessorInterface $this->clientRepository->delete($client); if ($this->kernel->getEnvironment() !== 'test') { - $this->deleteHostAction->__invoke($client->getSubnet(), $client->getUuid()); + + if ($client->getSubnet()) { + $this->deleteHostAction->__invoke($client->getSubnet(), $client->getUuid()); + } $this->deletePxeAction->__invoke($client->getUuid()); } diff --git a/src/State/Processor/OgLiveProcessor.php b/src/State/Processor/OgLiveProcessor.php index 0cc9bc3..edef074 100644 --- a/src/State/Processor/OgLiveProcessor.php +++ b/src/State/Processor/OgLiveProcessor.php @@ -9,15 +9,24 @@ use ApiPlatform\Metadata\Post; use ApiPlatform\Metadata\Put; use ApiPlatform\State\ProcessorInterface; use ApiPlatform\Validator\ValidatorInterface; +use App\Controller\OgBoot\OgLive\InstallAction; +use App\Controller\OgBoot\OgLive\UninstallAction; use App\Dto\Input\OgLiveInput; use App\Dto\Output\OgLiveOutput; +use App\Model\OgLiveStatus; use App\Repository\OgLiveRepository; +use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; +use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; +use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; +use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; readonly class OgLiveProcessor implements ProcessorInterface { public function __construct( private OgLiveRepository $ogLiveRepository, - private ValidatorInterface $validator + private ValidatorInterface $validator, + private InstallAction $installAction, + private UninstallAction $uninstallAction ) { } @@ -39,6 +48,7 @@ readonly class OgLiveProcessor implements ProcessorInterface /** * @throws \Exception + * @throws TransportExceptionInterface */ private function processCreateOrUpdate($data, Operation $operation, array $uriVariables = [], array $context = []): OgLiveOutput { @@ -55,12 +65,26 @@ readonly class OgLiveProcessor implements ProcessorInterface $this->validator->validate($ogLive); $this->ogLiveRepository->save($ogLive); + if ($operation instanceof Post) { + $this->installAction->__invoke($ogLive); + } + return new OgLiveOutput($ogLive); } + /** + * @throws TransportExceptionInterface + * @throws ServerExceptionInterface + * @throws RedirectionExceptionInterface + * @throws ClientExceptionInterface + */ private function processDelete($data, Operation $operation, array $uriVariables = [], array $context = []): null { $ogLive = $this->ogLiveRepository->findOneByUuid($uriVariables['uuid']); + + if ($ogLive->getStatus() === OgLiveStatus::ACTIVE) { + $this->uninstallAction->__invoke($ogLive); + } $this->ogLiveRepository->delete($ogLive); return null; diff --git a/src/State/Processor/OrganizationalUnitProcessor.php b/src/State/Processor/OrganizationalUnitProcessor.php index e4d2244..a83d14c 100644 --- a/src/State/Processor/OrganizationalUnitProcessor.php +++ b/src/State/Processor/OrganizationalUnitProcessor.php @@ -60,7 +60,7 @@ readonly class OrganizationalUnitProcessor implements ProcessorInterface $this->validator->validate($organizationalUnit, ['groups' => ['organizational-unit:write']]); $this->organizationalUnitRepository->save($organizationalUnit); - $this->changeClientNetworkSettingsService->__invoke($organizationalUnit); + //$this->changeClientNetworkSettingsService->__invoke($organizationalUnit); return new OrganizationalUnitOutput($organizationalUnit); }