mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-07-31 01:34:12 +02:00
SyncRule: add import-related methods
This commit is contained in:
parent
0735ed0aa1
commit
5e0b237d2d
@ -5,6 +5,8 @@ namespace Icinga\Module\Director\Objects;
|
|||||||
use Icinga\Application\Benchmark;
|
use Icinga\Application\Benchmark;
|
||||||
use Icinga\Data\Filter\Filter;
|
use Icinga\Data\Filter\Filter;
|
||||||
use Icinga\Module\Director\Data\Db\DbObject;
|
use Icinga\Module\Director\Data\Db\DbObject;
|
||||||
|
use Icinga\Module\Director\Db;
|
||||||
|
use Icinga\Module\Director\Exception\DuplicateKeyException;
|
||||||
use Icinga\Module\Director\Import\PurgeStrategy\PurgeStrategy;
|
use Icinga\Module\Director\Import\PurgeStrategy\PurgeStrategy;
|
||||||
use Icinga\Module\Director\Import\Sync;
|
use Icinga\Module\Director\Import\Sync;
|
||||||
use Exception;
|
use Exception;
|
||||||
@ -31,7 +33,7 @@ class SyncRule extends DbObject
|
|||||||
];
|
];
|
||||||
|
|
||||||
protected $stateProperties = [
|
protected $stateProperties = [
|
||||||
'import_state',
|
'sync_state',
|
||||||
'last_error_message',
|
'last_error_message',
|
||||||
'last_attempt',
|
'last_attempt',
|
||||||
];
|
];
|
||||||
@ -54,6 +56,8 @@ class SyncRule extends DbObject
|
|||||||
|
|
||||||
private $destinationKeyPattern;
|
private $destinationKeyPattern;
|
||||||
|
|
||||||
|
private $newSyncProperties;
|
||||||
|
|
||||||
public function listInvolvedSourceIds()
|
public function listInvolvedSourceIds()
|
||||||
{
|
{
|
||||||
if (! $this->hasBeenLoadedFromDb()) {
|
if (! $this->hasBeenLoadedFromDb()) {
|
||||||
@ -254,6 +258,65 @@ class SyncRule extends DbObject
|
|||||||
return $plain;
|
return $plain;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param object $plain
|
||||||
|
* @param Db $db
|
||||||
|
* @param bool $replace
|
||||||
|
* @return static
|
||||||
|
* @throws DuplicateKeyException
|
||||||
|
* @throws \Icinga\Exception\NotFoundError
|
||||||
|
*/
|
||||||
|
public static function import($plain, Db $db, $replace = false)
|
||||||
|
{
|
||||||
|
$properties = (array) $plain;
|
||||||
|
$id = $properties['originalId'];
|
||||||
|
unset($properties['originalId']);
|
||||||
|
$name = $properties['rule_name'];
|
||||||
|
|
||||||
|
if ($replace && static::existsWithNameAndId($name, $id, $db)) {
|
||||||
|
$object = static::loadWithAutoIncId($id, $db);
|
||||||
|
} elseif (static::existsWithName($name, $db)) {
|
||||||
|
throw new DuplicateKeyException(
|
||||||
|
'Import Source %s already exists',
|
||||||
|
$name
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
$object = static::create([], $db);
|
||||||
|
}
|
||||||
|
|
||||||
|
$object->newSyncProperties = $properties['properties'];
|
||||||
|
unset($properties['properties']);
|
||||||
|
$object->setProperties($properties);
|
||||||
|
|
||||||
|
return $object;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws DuplicateKeyException
|
||||||
|
*/
|
||||||
|
protected function onStore()
|
||||||
|
{
|
||||||
|
parent::onStore();
|
||||||
|
if ($this->newSyncProperties !== null) {
|
||||||
|
$connection = $this->getConnection();
|
||||||
|
$db = $connection->getDbAdapter();
|
||||||
|
$myId = $this->get('id');
|
||||||
|
if ($this->hasBeenLoadedFromDb()) {
|
||||||
|
$db->delete(
|
||||||
|
'sync_rule_property',
|
||||||
|
$db->quoteInto('rule_id = ?', $myId)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($this->newSyncProperties as $property) {
|
||||||
|
unset($property->rule_name);
|
||||||
|
$property = SyncProperty::create((array) $property, $connection);
|
||||||
|
$property->set('rule_id', $myId);
|
||||||
|
$property->store();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function exportSyncProperties()
|
public function exportSyncProperties()
|
||||||
{
|
{
|
||||||
$all = [];
|
$all = [];
|
||||||
@ -407,4 +470,44 @@ class SyncRule extends DbObject
|
|||||||
->order('priority ASC')
|
->order('priority ASC')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO: implement in a generic way, this is duplicated code
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @param Db $connection
|
||||||
|
* @api internal
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function existsWithName($name, Db $connection)
|
||||||
|
{
|
||||||
|
$db = $connection->getDbAdapter();
|
||||||
|
|
||||||
|
return (string) $name === (string) $db->fetchOne(
|
||||||
|
$db->select()
|
||||||
|
->from('sync_rule', 'rule_name')
|
||||||
|
->where('rule_name = ?', $name)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO: idem
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @param int $id
|
||||||
|
* @param Db $connection
|
||||||
|
* @api internal
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected static function existsWithNameAndId($name, $id, Db $connection)
|
||||||
|
{
|
||||||
|
$db = $connection->getDbAdapter();
|
||||||
|
|
||||||
|
return (string) $id === (string) $db->fetchOne(
|
||||||
|
$db->select()
|
||||||
|
->from('sync_rule', 'id')
|
||||||
|
->where('id = ?', $id)
|
||||||
|
->where('rule_name = ?', $name)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user