From 5419d4428c86c488a43124f85e5407d7cbae6541 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 14 Apr 2024 15:17:47 +0200 Subject: Adding upstream version 1.11.1. Signed-off-by: Daniel Baumann --- application/controllers/BasketController.php | 125 ++++++++++++++------------- 1 file changed, 64 insertions(+), 61 deletions(-) (limited to 'application/controllers/BasketController.php') diff --git a/application/controllers/BasketController.php b/application/controllers/BasketController.php index 8733d16..8d4db03 100644 --- a/application/controllers/BasketController.php +++ b/application/controllers/BasketController.php @@ -9,13 +9,10 @@ use gipfl\IcingaWeb2\Link; use gipfl\Web\Table\NameValueTable; use gipfl\Web\Widget\Hint; use Icinga\Date\DateFormatter; -use Icinga\Module\Director\Core\Json; -use Icinga\Module\Director\Data\Exporter; use Icinga\Module\Director\Db; use Icinga\Module\Director\DirectorObject\Automation\Basket; +use Icinga\Module\Director\DirectorObject\Automation\BasketDiff; use Icinga\Module\Director\DirectorObject\Automation\BasketSnapshot; -use Icinga\Module\Director\DirectorObject\Automation\BasketSnapshotFieldResolver; -use Icinga\Module\Director\DirectorObject\Automation\CompareBasketObject; use Icinga\Module\Director\Forms\AddToBasketForm; use Icinga\Module\Director\Forms\BasketCreateSnapshotForm; use Icinga\Module\Director\Forms\BasketForm; @@ -24,6 +21,7 @@ use Icinga\Module\Director\Forms\RestoreBasketForm; use Icinga\Module\Director\Web\Controller\ActionController; use ipl\Html\Html; use Icinga\Module\Director\Web\Table\BasketSnapshotTable; +use Ramsey\Uuid\Uuid; class BasketController extends ActionController { @@ -127,6 +125,26 @@ class BasketController extends ActionController $this->content()->add($form); } + public function uploadSnapshotAction() + { + $basket = Basket::load($this->params->get('name'), $this->db()); + $this->actions()->add( + Link::create( + $this->translate('back'), + 'director/basket/snapshots', + ['name' => $basket->get('basket_name')], + ['class' => 'icon-left-big'] + ) + ); + $this->basketTabs()->activate('snapshots'); + $this->addTitle($this->translate('Upload a Configuration Basket Snapshot')); + $form = (new BasketUploadForm()) + ->setObject($basket) + ->setDb($this->db()) + ->handleRequest(); + $this->content()->add($form); + } + /** * @throws \Icinga\Exception\NotFoundError */ @@ -147,6 +165,12 @@ class BasketController extends ActionController $basket->get('basket_name') )); $this->basketTabs()->activate('snapshots'); + $this->actions()->add(Link::create( + $this->translate('Upload'), + 'director/basket/upload-snapshot', + ['name' => $basket->get('basket_name')], + ['class' => 'icon-upload'] + )); } if ($basket !== null) { $this->content()->add( @@ -245,12 +269,9 @@ class BasketController extends ActionController $connection = $this->db(); } - $json = $snapshot->getJsonDump(); $this->addSingleTab($this->translate('Snapshot')); - $all = Json::decode($json); - $exporter = new Exporter($this->db()); - $fieldResolver = new BasketSnapshotFieldResolver($all, $connection); - foreach ($all as $type => $objects) { + $diff = new BasketDiff($snapshot, $connection); + foreach ($diff->getBasketObjects() as $type => $objects) { if ($type === 'Datafield') { // TODO: we should now be able to show all fields and link // to a "diff" for the ones that should be created @@ -274,39 +295,33 @@ class BasketController extends ActionController $linkParams['target_db'] = $targetDbName; } try { - $current = BasketSnapshot::instanceByIdentifier($type, $key, $connection); - if ($current === null) { - $table->addNameValueRow( - $key, - Link::create( - Html::tag('strong', ['style' => 'color: green'], $this->translate('new')), + if ($uuid = $object->uuid ?? null) { + $uuid = Uuid::fromString($uuid); + } + if ($diff->hasCurrentInstance($type, $key, $uuid)) { + if ($diff->hasChangedFor($type, $key, $uuid)) { + $link = Link::create( + $this->translate('modified'), 'director/basket/snapshotobject', - $linkParams - ) + $linkParams, + ['class' => 'basket-modified'] + ); + } else { + $link = Html::tag( + 'span', + ['class' => 'basket-unchanged'], + $this->translate('unchanged') + ); + } + } else { + $link = Link::create( + $this->translate('new'), + 'director/basket/snapshotobject', + $linkParams, + ['class' => 'basket-new'] ); - continue; - } - $currentExport = $exporter->export($current); - $fieldResolver->tweakTargetIds($currentExport); - - // Ignore originalId - if (isset($currentExport->originalId)) { - unset($currentExport->originalId); - } - if (isset($object->originalId)) { - unset($object->originalId); } - $hasChanged = ! CompareBasketObject::equals($currentExport, $object); - $table->addNameValueRow( - $key, - $hasChanged - ? Link::create( - Html::tag('strong', ['style' => 'color: orange'], $this->translate('modified')), - 'director/basket/snapshotobject', - $linkParams - ) - : Html::tag('span', ['style' => 'color: green'], $this->translate('unchanged')) - ); + $table->addNameValueRow($key, $link); } catch (Exception $e) { $table->addNameValueRow( $key, @@ -322,7 +337,6 @@ class BasketController extends ActionController $this->content()->add(Html::tag('h2', $type)); $this->content()->add($table); } - $this->content()->add(Html::tag('div', ['style' => 'height: 5em'])); } /** @@ -368,39 +382,28 @@ class BasketController extends ActionController ) */ ]); - $exporter = new Exporter($this->db()); - $json = $snapshot->getJsonDump(); + $this->addSingleTab($this->translate('Snapshot')); - $objects = Json::decode($json); $targetDbName = $this->params->get('target_db'); if ($targetDbName === null) { $connection = $this->db(); } else { $connection = Db::fromResourceName($targetDbName); } - $fieldResolver = new BasketSnapshotFieldResolver($objects, $connection); - $objectFromBasket = $objects->$type->$key; - unset($objectFromBasket->originalId); - CompareBasketObject::normalize($objectFromBasket); - $objectFromBasket = Json::encode($objectFromBasket, JSON_PRETTY_PRINT); - $current = BasketSnapshot::instanceByIdentifier($type, $key, $connection); - if ($current === null) { - $current = ''; - } else { - $exported = $exporter->export($current); - $fieldResolver->tweakTargetIds($exported); - unset($exported->originalId); - CompareBasketObject::normalize($exported); - $current = Json::encode($exported, JSON_PRETTY_PRINT); + $diff = new BasketDiff($snapshot, $connection); + $object = $diff->getBasketObject($type, $key); + if ($uuid = $object->uuid ?? null) { + $uuid = Uuid::fromString($uuid); } - - if ($current === $objectFromBasket) { + $basketJson = $diff->getBasketString($type, $key); + $currentJson = $diff->getCurrentString($type, $key, $uuid); + if ($currentJson === $basketJson) { $this->content()->add([ Hint::ok('Basket equals current object'), - Html::tag('pre', $current) + Html::tag('pre', $currentJson) ]); } else { - $this->content()->add(new InlineDiff(new PhpDiff($current, $objectFromBasket))); + $this->content()->add(new InlineDiff(new PhpDiff($currentJson, $basketJson))); } } -- cgit v1.2.3