Merge pull request #1597 from Icinga/bugfix/activitylog-restore

RestoreObjectForm: Fix restore of multi-key and apply
This commit is contained in:
Markus Frosch 2018-08-10 14:23:57 +02:00 committed by GitHub
commit 025a3f7390
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 4 deletions

View File

@ -2,6 +2,8 @@
namespace Icinga\Module\Director\Forms;
use Icinga\Exception\NotFoundError;
use Icinga\Exception\NotImplementedError;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Web\Form\DirectorForm;
@ -21,9 +23,50 @@ class RestoreObjectForm extends DirectorForm
$name = $object->getObjectName();
$db = $this->db;
// TODO: service -> multi-key
if ($object::exists($name, $db)) {
$existing = $object::load($name, $db)->replaceWith($object);
$keyParams = $object->getKeyParams();
if ($object->supportsApplyRules() && $object->get('object_type') === 'apply') {
// TODO: not all apply should be considered unique by name + object_type
$query = $db->getDbAdapter()
->select()
->from($object->getTableName())
->where('object_type = ?', 'apply')
->where('object_name = ?', $name);
$rules = $object::loadAll($db, $query);
if (empty($rules)) {
$existing = null;
} elseif (count($rules) === 1) {
$existing = current($rules);
} else {
// TODO: offer drop down?
throw new NotImplementedError(
"Found multiple apply rule matching name '%s', can not restore!",
$name
);
}
} else {
try {
$existing = $object::load($keyParams, $db);
} catch (NotFoundError $e) {
$existing = null;
}
}
if ($existing !== null) {
$typeExisting = $existing->get('object_type');
$typeObject = $object->get('object_type');
if ($typeExisting !== $typeObject) {
// Not sure when that may occur
throw new NotImplementedError(
'Found existing object has a mismatching object_type: %s != %s',
$typeExisting,
$typeObject
);
}
$existing->replaceWith($object);
if ($existing->hasBeenModified()) {
$msg = $this->translate('Object has been restored');

View File

@ -1208,7 +1208,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
try {
$imports = array_reverse($this->imports()->getObjects());
} catch (NotFoundError $e) {
throw new RuntimeException($e->getMessage(), 0, $e->getMessage());
throw new RuntimeException($e->getMessage(), 0, $e);
}
foreach ($imports as $object) {