From cd0b27525009496a84bc1da27251a6fb06e7a1ad Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Mon, 8 Oct 2018 06:25:38 +0200 Subject: [PATCH] DirectorJob: name is key, provide import/export --- library/Director/Objects/DirectorJob.php | 78 +++++++++++++++++++++++- 1 file changed, 76 insertions(+), 2 deletions(-) diff --git a/library/Director/Objects/DirectorJob.php b/library/Director/Objects/DirectorJob.php index 00ab70b7..5f25610f 100644 --- a/library/Director/Objects/DirectorJob.php +++ b/library/Director/Objects/DirectorJob.php @@ -2,19 +2,23 @@ namespace Icinga\Module\Director\Objects; +use Icinga\Exception\NotFoundError; 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 Exception; use InvalidArgumentException; -class DirectorJob extends DbObjectWithSettings +class DirectorJob extends DbObjectWithSettings implements ExportInterface { /** @var JobHook */ protected $job; protected $table = 'director_job'; - protected $keyName = 'id'; + protected $keyName = 'job_name'; protected $autoincKeyName = 'id'; @@ -42,6 +46,11 @@ class DirectorJob extends DbObjectWithSettings protected $settingsRemoteId = 'job_id'; + public function getUniqueIdentifier() + { + return $this->get('job_name'); + } + /** * @return JobHook */ @@ -188,6 +197,71 @@ class DirectorJob extends DbObjectWithSettings 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 * @throws \Icinga\Exception\NotFoundError