IcingaCloneObjectForm: support Branches

This commit is contained in:
Thomas Gelf 2021-10-05 22:55:08 +02:00
parent 57c4dda117
commit 8d3c901db7
1 changed files with 18 additions and 9 deletions

View File

@ -5,6 +5,7 @@ namespace Icinga\Module\Director\Forms;
use gipfl\Web\Widget\Hint; use gipfl\Web\Widget\Hint;
use Icinga\Exception\IcingaException; use Icinga\Exception\IcingaException;
use Icinga\Module\Director\Acl; use Icinga\Module\Director\Acl;
use Icinga\Module\Director\Data\Db\DbObjectStore;
use Icinga\Module\Director\Db\Branch\Branch; use Icinga\Module\Director\Db\Branch\Branch;
use Icinga\Module\Director\Objects\IcingaHost; use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaObject; use Icinga\Module\Director\Objects\IcingaObject;
@ -24,7 +25,9 @@ class IcingaCloneObjectForm extends DirectorForm
public function setup() public function setup()
{ {
if ($this->branch->isBranch() && $this->object instanceof IcingaObject && $this->object->isTemplate()) { $isBranch = $this->branch && $this->branch->isBranch();
$branchOnly = $this->object->get('id') === null;
if ($isBranch && $this->object instanceof IcingaObject && $this->object->isTemplate()) {
$this->addHtml(Hint::error($this->translate( $this->addHtml(Hint::error($this->translate(
'Templates cannot be cloned in Configuration Branches' 'Templates cannot be cloned in Configuration Branches'
))); )));
@ -38,7 +41,7 @@ class IcingaCloneObjectForm extends DirectorForm
'value' => $name, 'value' => $name,
)); ));
if (Acl::instance()->hasPermission('director/admin')) { if (!$branchOnly && Acl::instance()->hasPermission('director/admin')) {
$this->addElement('select', 'clone_type', array( $this->addElement('select', 'clone_type', array(
'label' => 'Clone type', 'label' => 'Clone type',
'required' => true, 'required' => true,
@ -49,8 +52,8 @@ class IcingaCloneObjectForm extends DirectorForm
)); ));
} }
if ($this->object instanceof IcingaHost if (!$branchOnly && ($this->object instanceof IcingaHost
|| $this->object instanceof IcingaServiceSet || $this->object instanceof IcingaServiceSet)
) { ) {
$this->addBoolean('clone_services', [ $this->addBoolean('clone_services', [
'label' => $this->translate('Clone Services'), 'label' => $this->translate('Clone Services'),
@ -60,7 +63,7 @@ class IcingaCloneObjectForm extends DirectorForm
], 'y'); ], 'y');
} }
if ($this->object instanceof IcingaHost) { if (!$branchOnly && $this->object instanceof IcingaHost) {
$this->addBoolean('clone_service_sets', [ $this->addBoolean('clone_service_sets', [
'label' => $this->translate('Clone Service Sets'), 'label' => $this->translate('Clone Service Sets'),
'description' => $this->translate( 'description' => $this->translate(
@ -139,6 +142,10 @@ class IcingaCloneObjectForm extends DirectorForm
$object->getObjectName() $object->getObjectName()
); );
if ($object->isTemplate() && $this->branch && $this->branch->isBranch()) {
throw new IcingaException('Cloning templates is not available for Branches');
}
if ($object->isTemplate() && $object->getObjectName() === $newName) { if ($object->isTemplate() && $object->getObjectName() === $newName) {
throw new IcingaException( throw new IcingaException(
$this->translate('Name needs to be changed when cloning a Template') $this->translate('Name needs to be changed when cloning a Template')
@ -188,7 +195,8 @@ class IcingaCloneObjectForm extends DirectorForm
$fields = []; $fields = [];
} }
if ($new->store()) { $store = new DbObjectStore($connection, $this->branch);
if ($store->store($new)) {
$newId = $new->get('id'); $newId = $new->get('id');
foreach ($services as $service) { foreach ($services as $service) {
$clone = IcingaService::fromPlainObject( $clone = IcingaService::fromPlainObject(
@ -201,14 +209,15 @@ class IcingaCloneObjectForm extends DirectorForm
} elseif ($new instanceof IcingaServiceSet) { } elseif ($new instanceof IcingaServiceSet) {
$clone->set('service_set_id', $newId); $clone->set('service_set_id', $newId);
} }
$clone->store(); $store->store($clone);
} }
foreach ($sets as $set) { foreach ($sets as $set) {
IcingaServiceSet::fromPlainObject( $newSet = IcingaServiceSet::fromPlainObject(
$set->toPlainObject(), $set->toPlainObject(),
$connection $connection
)->set('host_id', $newId)->store(); )->set('host_id', $newId);
$store->store($newSet);
} }
foreach ($fields as $row) { foreach ($fields as $row) {