mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-07-28 16:24:05 +02:00
DirectorJob: name is key, provide import/export
This commit is contained in:
parent
8f3cf85954
commit
cd0b275250
@ -2,19 +2,23 @@
|
|||||||
|
|
||||||
namespace Icinga\Module\Director\Objects;
|
namespace Icinga\Module\Director\Objects;
|
||||||
|
|
||||||
|
use Icinga\Exception\NotFoundError;
|
||||||
use Icinga\Module\Director\Data\Db\DbObjectWithSettings;
|
use Icinga\Module\Director\Data\Db\DbObjectWithSettings;
|
||||||
|
use Icinga\Module\Director\Db;
|
||||||
|
use Icinga\Module\Director\DirectorObject\Automation\ExportInterface;
|
||||||
|
use Icinga\Module\Director\Exception\DuplicateKeyException;
|
||||||
use Icinga\Module\Director\Hook\JobHook;
|
use Icinga\Module\Director\Hook\JobHook;
|
||||||
use Exception;
|
use Exception;
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
|
|
||||||
class DirectorJob extends DbObjectWithSettings
|
class DirectorJob extends DbObjectWithSettings implements ExportInterface
|
||||||
{
|
{
|
||||||
/** @var JobHook */
|
/** @var JobHook */
|
||||||
protected $job;
|
protected $job;
|
||||||
|
|
||||||
protected $table = 'director_job';
|
protected $table = 'director_job';
|
||||||
|
|
||||||
protected $keyName = 'id';
|
protected $keyName = 'job_name';
|
||||||
|
|
||||||
protected $autoincKeyName = 'id';
|
protected $autoincKeyName = 'id';
|
||||||
|
|
||||||
@ -42,6 +46,11 @@ class DirectorJob extends DbObjectWithSettings
|
|||||||
|
|
||||||
protected $settingsRemoteId = 'job_id';
|
protected $settingsRemoteId = 'job_id';
|
||||||
|
|
||||||
|
public function getUniqueIdentifier()
|
||||||
|
{
|
||||||
|
return $this->get('job_name');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return JobHook
|
* @return JobHook
|
||||||
*/
|
*/
|
||||||
@ -188,6 +197,71 @@ class DirectorJob extends DbObjectWithSettings
|
|||||||
return $plain;
|
return $plain;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $plain
|
||||||
|
* @param Db $db
|
||||||
|
* @param bool $replace
|
||||||
|
* @return DirectorJob
|
||||||
|
* @throws DuplicateKeyException
|
||||||
|
* @throws NotFoundError
|
||||||
|
*/
|
||||||
|
public static function import($plain, Db $db, $replace = false)
|
||||||
|
{
|
||||||
|
$dummy = new static;
|
||||||
|
$idCol = $dummy->autoincKeyName;
|
||||||
|
$keyCol = $dummy->keyName;
|
||||||
|
$properties = (array) $plain;
|
||||||
|
if (isset($properties['originalId'])) {
|
||||||
|
$id = $properties['originalId'];
|
||||||
|
unset($properties['originalId']);
|
||||||
|
} else {
|
||||||
|
$id = null;
|
||||||
|
}
|
||||||
|
$name = $properties[$keyCol];
|
||||||
|
|
||||||
|
if ($replace && static::existsWithNameAndId($name, $id, $db)) {
|
||||||
|
$object = static::loadWithAutoIncId($id, $db);
|
||||||
|
} elseif ($replace && static::exists($name, $db)) {
|
||||||
|
$object = static::load($name, $db);
|
||||||
|
} elseif (static::exists($name, $db)) {
|
||||||
|
throw new DuplicateKeyException(
|
||||||
|
'Director Job "%s" already exists',
|
||||||
|
$name
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
$object = static::create([], $db);
|
||||||
|
}
|
||||||
|
|
||||||
|
$object->setProperties($properties);
|
||||||
|
if ($id !== null) {
|
||||||
|
$object->reallySet($idCol, $id);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $object;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @param int $id
|
||||||
|
* @param Db $connection
|
||||||
|
* @api internal
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected static function existsWithNameAndId($name, $id, Db $connection)
|
||||||
|
{
|
||||||
|
$db = $connection->getDbAdapter();
|
||||||
|
$dummy = new static;
|
||||||
|
$idCol = $dummy->autoincKeyName;
|
||||||
|
$keyCol = $dummy->keyName;
|
||||||
|
|
||||||
|
return (string) $id === (string) $db->fetchOne(
|
||||||
|
$db->select()
|
||||||
|
->from($dummy->table, $idCol)
|
||||||
|
->where("$idCol = ?", $id)
|
||||||
|
->where("$keyCol = ?", $name)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return IcingaTimePeriod
|
* @return IcingaTimePeriod
|
||||||
* @throws \Icinga\Exception\NotFoundError
|
* @throws \Icinga\Exception\NotFoundError
|
||||||
|
Loading…
x
Reference in New Issue
Block a user