refs #1967. Sync ogBoot API when put template
parent
d347896158
commit
927e38102e
|
@ -3,6 +3,7 @@
|
||||||
namespace App\Controller\OgBoot\PxeTemplate;
|
namespace App\Controller\OgBoot\PxeTemplate;
|
||||||
|
|
||||||
use App\Controller\OgBoot\AbstractOgBootController;
|
use App\Controller\OgBoot\AbstractOgBootController;
|
||||||
|
use App\Entity\Client;
|
||||||
use App\Entity\PxeTemplate;
|
use App\Entity\PxeTemplate;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
|
@ -26,11 +27,20 @@ class DeleteAction extends AbstractOgBootController
|
||||||
*/
|
*/
|
||||||
public function __invoke(PxeTemplate $data): JsonResponse
|
public function __invoke(PxeTemplate $data): JsonResponse
|
||||||
{
|
{
|
||||||
$content = $this->createRequest('DELETE', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/pxe-templates/'.$data->getName());
|
|
||||||
|
|
||||||
$this->entityManager->remove($data);
|
$this->entityManager->remove($data);
|
||||||
$this->entityManager->flush();
|
$this->entityManager->flush();
|
||||||
|
|
||||||
|
$content = $this->createRequest('DELETE', 'http://'.$this->ogBootApiUrl.'/ogboot/v1/pxe-templates/'.$data->getName());
|
||||||
|
|
||||||
|
$defaultTemplateEntity = $this->entityManager->getRepository(PxeTemplate::class)->findOneBy(['isDefault' => true]);
|
||||||
|
|
||||||
|
if ($defaultTemplateEntity === null) {
|
||||||
|
return new JsonResponse(
|
||||||
|
'Default template not found',
|
||||||
|
Response::HTTP_NOT_FOUND
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return new JsonResponse(status: Response::HTTP_OK);
|
return new JsonResponse(status: Response::HTTP_OK);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -28,25 +28,35 @@ class SyncAction extends AbstractOgBootController
|
||||||
*/
|
*/
|
||||||
public function __invoke(): JsonResponse
|
public function __invoke(): JsonResponse
|
||||||
{
|
{
|
||||||
$content = $this->createRequest('GET', 'http://'.$this->ogBootApiUrl . '/ogboot/v1/pxe-templates');
|
$content = $this->createRequest('GET', 'http://' . $this->ogBootApiUrl . '/ogboot/v1/pxe-templates');
|
||||||
|
$templateNamesFromApi = $content['message'];
|
||||||
|
|
||||||
foreach ($content['message'] as $template) {
|
$existingTemplates = $this->entityManager->getRepository(PxeTemplate::class)->findAll();
|
||||||
$templateEntity = $this->entityManager->getRepository(PxeTemplate::class)->findOneBy(['name' => $template]);
|
|
||||||
|
|
||||||
if ($templateEntity) {
|
$existingTemplateNames = array_map(fn($t) => $t->getName(), $existingTemplates);
|
||||||
$this->entityManager->persist($templateEntity);
|
|
||||||
} else {
|
foreach ($existingTemplates as $existingTemplate) {
|
||||||
$templateEntity = new PxeTemplate();
|
if (!in_array($existingTemplate->getName(), $templateNamesFromApi, true)) {
|
||||||
$templateEntity->setName($template);
|
$this->entityManager->remove($existingTemplate);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$templateContent = $this->createRequest('GET', 'http://'.$this->ogBootApiUrl . '/ogboot/v1/pxe-templates/'.$templateEntity->getName());
|
foreach ($templateNamesFromApi as $templateName) {
|
||||||
|
$templateEntity = $this->entityManager->getRepository(PxeTemplate::class)->findOneBy(['name' => $templateName]);
|
||||||
|
|
||||||
|
if (!$templateEntity) {
|
||||||
|
$templateEntity = new PxeTemplate();
|
||||||
|
$templateEntity->setName($templateName);
|
||||||
|
}
|
||||||
|
|
||||||
|
$templateContent = $this->createRequest('GET', 'http://' . $this->ogBootApiUrl . '/ogboot/v1/pxe-templates/' . $templateEntity->getName());
|
||||||
|
|
||||||
$templateEntity->setTemplateContent($templateContent['template_content']);
|
$templateEntity->setTemplateContent($templateContent['template_content']);
|
||||||
$templateEntity->setSynchronized(true);
|
$templateEntity->setSynchronized(true);
|
||||||
|
|
||||||
$this->entityManager->persist($templateEntity);
|
$this->entityManager->persist($templateEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->entityManager->flush();
|
$this->entityManager->flush();
|
||||||
|
|
||||||
return new JsonResponse(data: $content, status: Response::HTTP_OK);
|
return new JsonResponse(data: $content, status: Response::HTTP_OK);
|
||||||
|
|
Loading…
Reference in New Issue