2015-07-24 10:51:55 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Import;
|
|
|
|
|
2015-08-04 19:52:02 +02:00
|
|
|
use Icinga\Module\Director\Objects\IcingaObject;
|
2015-07-24 10:51:55 +02:00
|
|
|
use Icinga\Module\Director\Objects\ImportSource;
|
|
|
|
use Icinga\Module\Director\Objects\SyncRule;
|
2015-10-20 22:21:48 +02:00
|
|
|
use Icinga\Exception\IcingaException;
|
2015-07-24 10:51:55 +02:00
|
|
|
|
|
|
|
class Sync
|
|
|
|
{
|
|
|
|
protected function __construct()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function run(SyncRule $rule)
|
|
|
|
{
|
|
|
|
$sync = new static;
|
|
|
|
return $sync->runWithRule($rule);
|
|
|
|
}
|
|
|
|
|
2015-10-30 23:34:27 +01:00
|
|
|
public static function hasModifications(SyncRule $rule)
|
|
|
|
{
|
|
|
|
return count(self::getExpectedModifications($rule)) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getExpectedModifications(SyncRule $rule)
|
|
|
|
{
|
|
|
|
$modified = array();
|
|
|
|
$sync = new static;
|
|
|
|
$objects = $sync->prepareSyncForRule($rule);
|
|
|
|
foreach ($objects as $object) {
|
|
|
|
if ($object->hasBeenModified()) {
|
|
|
|
$modified[] = $object;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $modified;
|
|
|
|
}
|
|
|
|
|
2015-07-24 10:51:55 +02:00
|
|
|
protected function extractVariableNames($string)
|
|
|
|
{
|
2015-11-25 09:21:15 +01:00
|
|
|
if (preg_match_all('/\${([A-Za-z0-9\._-]+)}/', $string, $m, PREG_PATTERN_ORDER)) {
|
2015-07-24 10:51:55 +02:00
|
|
|
return $m[1];
|
|
|
|
} else {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-28 23:56:54 +02:00
|
|
|
protected function wantArray($value)
|
|
|
|
{
|
|
|
|
if (is_array($value)) {
|
|
|
|
return $value;
|
|
|
|
} elseif ($value === null) {
|
|
|
|
return array();
|
|
|
|
} else {
|
|
|
|
return array($value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-25 09:21:15 +01:00
|
|
|
protected function getDeepValue($val, $keys)
|
|
|
|
{
|
|
|
|
$key = array_shift($keys);
|
|
|
|
if (! property_exists($val, $key)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($keys)) {
|
|
|
|
return $val->$key;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getDeepValue($val->$key, $keys);
|
|
|
|
}
|
|
|
|
|
2015-12-02 15:59:31 +01:00
|
|
|
protected function getSpecificValue($row, $var)
|
2015-07-24 10:51:55 +02:00
|
|
|
{
|
2015-12-02 15:59:31 +01:00
|
|
|
if (strpos($var, '.') === false) {
|
|
|
|
if (! property_exists($row, $var)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $row->$var;
|
|
|
|
} else {
|
|
|
|
$parts = explode('.', $var);
|
|
|
|
$main = array_shift($parts);
|
|
|
|
if (! is_object($row->$main)) {
|
|
|
|
die('Data is not nested, cannot access ...');
|
2015-08-28 23:52:02 +02:00
|
|
|
}
|
2015-11-25 09:21:15 +01:00
|
|
|
|
2015-12-02 15:59:31 +01:00
|
|
|
return $this->getDeepValue($row->$main, $parts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function fillVariables($string, $row)
|
|
|
|
{
|
|
|
|
if (preg_match('/^\${([A-Za-z0-9\._-]+)}$/', $string, $m)) {
|
|
|
|
return $this->getSpecificValue($row, $m[1]);
|
2015-08-28 23:52:02 +02:00
|
|
|
}
|
|
|
|
|
2015-07-24 10:51:55 +02:00
|
|
|
$func = function ($match) use ($row) {
|
2015-12-02 15:59:31 +01:00
|
|
|
return $this->getSpecificValue($row, $match[1]);
|
2015-07-24 10:51:55 +02:00
|
|
|
};
|
|
|
|
|
2015-11-25 09:21:15 +01:00
|
|
|
return preg_replace_callback('/\${([A-Za-z0-9\._-]+)}/', $func, $string);
|
2015-07-24 10:51:55 +02:00
|
|
|
}
|
|
|
|
|
2015-10-20 23:29:29 +02:00
|
|
|
protected function prepareSyncForRule(SyncRule $rule)
|
2015-07-24 10:51:55 +02:00
|
|
|
{
|
|
|
|
$db = $rule->getConnection();
|
|
|
|
$properties = $rule->fetchSyncProperties();
|
|
|
|
$sourceColumns = array();
|
|
|
|
$sources = array();
|
|
|
|
// $fieldMap = array();
|
|
|
|
|
|
|
|
foreach ($properties as $p) {
|
|
|
|
$sourceId = $p->source_id;
|
|
|
|
if (! array_key_exists($sourceId, $sources)) {
|
|
|
|
$sources[$sourceId] = ImportSource::load($sourceId, $db);
|
|
|
|
$sourceColumns[$sourceId] = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->extractVariableNames($p->source_expression) as $varname) {
|
|
|
|
$sourceColumns[$sourceId][$varname] = $varname;
|
|
|
|
// -> ? $fieldMap[
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$imported = array();
|
|
|
|
foreach ($sources as $source) {
|
|
|
|
$sourceId = $source->id;
|
|
|
|
$key = $source->key_column;
|
2015-07-26 15:46:25 +02:00
|
|
|
$sourceColumns[$sourceId][$key] = $key;
|
|
|
|
$rows = $db->fetchLatestImportedRows($sourceId, $sourceColumns[$sourceId]);
|
|
|
|
|
2015-07-24 10:51:55 +02:00
|
|
|
$imported[$sourceId] = array();
|
|
|
|
foreach ($rows as $row) {
|
2015-07-24 15:27:37 +02:00
|
|
|
if (! property_exists($row, $key)) {
|
2015-10-20 22:21:48 +02:00
|
|
|
throw new IcingaException(
|
|
|
|
'There is no key column "%s" in this row from "%s": %s',
|
|
|
|
$key,
|
|
|
|
$source->source_name,
|
|
|
|
json_encode($row)
|
2015-07-24 15:27:37 +02:00
|
|
|
);
|
|
|
|
}
|
2015-07-24 10:51:55 +02:00
|
|
|
$imported[$sourceId][$row->$key] = $row;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Filter auf object, nicht template
|
2015-08-04 19:52:02 +02:00
|
|
|
$objects = IcingaObject::loadAllByType($rule->object_type, $db);
|
2015-11-02 09:29:03 +01:00
|
|
|
|
2015-10-30 23:34:27 +01:00
|
|
|
if ($rule->object_type === 'datalistEntry') {
|
|
|
|
$no = array();
|
|
|
|
foreach ($objects as $o) {
|
|
|
|
// if ($o->list_id !== $source->
|
|
|
|
}
|
|
|
|
}
|
2015-08-04 19:52:02 +02:00
|
|
|
$dummy = IcingaObject::createByType($rule->object_type, array());
|
|
|
|
$objectKey = $rule->object_type === 'datalistEntry' ? 'entry_name' : 'object_name';
|
2015-07-24 10:51:55 +02:00
|
|
|
|
2015-07-26 15:46:25 +02:00
|
|
|
foreach ($sources as $source) {
|
|
|
|
$sourceId = $source->id;
|
2015-07-24 10:51:55 +02:00
|
|
|
|
2015-07-26 15:46:25 +02:00
|
|
|
foreach ($imported[$sourceId] as $key => $row) {
|
2015-10-30 23:34:27 +01:00
|
|
|
$newProps = array();
|
2015-08-04 19:52:02 +02:00
|
|
|
|
2015-07-26 15:46:25 +02:00
|
|
|
$newVars = array();
|
2015-08-03 13:39:55 +02:00
|
|
|
$imports = array();
|
2015-07-24 10:51:55 +02:00
|
|
|
|
2015-07-26 15:46:25 +02:00
|
|
|
foreach ($properties as $p) {
|
|
|
|
if ($p->source_id !== $sourceId) continue;
|
2015-07-24 10:51:55 +02:00
|
|
|
|
2015-07-26 15:46:25 +02:00
|
|
|
$prop = $p->destination_field;
|
|
|
|
$val = $this->fillVariables($p->source_expression, $row);
|
|
|
|
|
|
|
|
if (substr($prop, 0, 5) === 'vars.') {
|
2015-08-28 23:56:54 +02:00
|
|
|
$varName = substr($prop, 5);
|
|
|
|
if (substr($varName, -2) === '[]') {
|
|
|
|
$varName = substr($varName, 0, -2);
|
|
|
|
$val = $this->wantArray($val);
|
|
|
|
}
|
|
|
|
$newVars[$varName] = $val;
|
2015-07-26 15:46:25 +02:00
|
|
|
} else {
|
2015-08-03 13:39:55 +02:00
|
|
|
if ($prop === 'import') {
|
|
|
|
$imports[] = $val;
|
|
|
|
} else {
|
|
|
|
$newProps[$prop] = $val;
|
|
|
|
}
|
2015-07-26 15:46:25 +02:00
|
|
|
}
|
2015-07-24 10:51:55 +02:00
|
|
|
}
|
2015-11-17 18:28:24 +01:00
|
|
|
|
2015-07-26 15:46:25 +02:00
|
|
|
if (array_key_exists($key, $objects)) {
|
2015-11-02 09:29:03 +01:00
|
|
|
|
2015-07-26 15:46:25 +02:00
|
|
|
switch ($rule->update_policy) {
|
|
|
|
case 'override':
|
2015-11-02 09:29:03 +01:00
|
|
|
// TODO: Only override if it doesn't equal
|
2015-08-04 19:52:02 +02:00
|
|
|
$objects[$key] = IcingaObject::createByType(
|
|
|
|
$rule->object_type,
|
|
|
|
$newProps,
|
|
|
|
$db
|
|
|
|
);
|
|
|
|
|
2015-07-26 15:46:25 +02:00
|
|
|
foreach ($newVars as $prop => $var) {
|
|
|
|
$objects[$key]->vars()->$prop = $var;
|
|
|
|
}
|
2015-08-03 13:39:55 +02:00
|
|
|
if (! empty($imports)) {
|
|
|
|
$objects[$key]->imports()->set($imports);
|
|
|
|
}
|
2015-07-26 15:46:25 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'merge':
|
|
|
|
$object = $objects[$key];
|
|
|
|
foreach ($newProps as $prop => $value) {
|
|
|
|
// TODO: data type?
|
|
|
|
$object->set($prop, $value);
|
2015-11-02 09:29:03 +01:00
|
|
|
|
2015-07-26 15:46:25 +02:00
|
|
|
}
|
2015-11-02 09:29:03 +01:00
|
|
|
|
2015-07-26 15:46:25 +02:00
|
|
|
foreach ($newVars as $prop => $var) {
|
|
|
|
// TODO: property merge policy
|
|
|
|
$object->vars()->$prop = $var;
|
|
|
|
}
|
2015-11-02 09:29:03 +01:00
|
|
|
|
2015-08-03 13:39:55 +02:00
|
|
|
if (! empty($imports)) {
|
|
|
|
// TODO: merge imports ?!
|
|
|
|
$objects[$key]->imports()->set($imports);
|
|
|
|
}
|
2015-07-26 15:46:25 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// policy 'ignore', no action
|
|
|
|
}
|
|
|
|
} else {
|
2015-10-30 23:34:27 +01:00
|
|
|
// New object
|
|
|
|
if ($rule->object_type !== 'datalistEntry') {
|
|
|
|
$newProps['object_type'] = 'object';
|
|
|
|
$newProps['object_name'] = $key;
|
|
|
|
}
|
|
|
|
|
2015-08-04 19:52:02 +02:00
|
|
|
$objects[$key] = IcingaObject::createByType($rule->object_type, $newProps, $db);
|
2015-07-26 15:46:25 +02:00
|
|
|
foreach ($newVars as $prop => $var) {
|
|
|
|
$objects[$key]->vars()->$prop = $var;
|
|
|
|
}
|
2015-08-03 15:12:46 +02:00
|
|
|
|
|
|
|
if (! empty($imports)) {
|
|
|
|
$objects[$key]->imports()->set($imports);
|
|
|
|
}
|
2015-07-24 10:51:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-26 15:46:25 +02:00
|
|
|
}
|
|
|
|
|
2015-10-20 23:29:29 +02:00
|
|
|
$ignore = array();
|
2015-07-24 15:30:09 +02:00
|
|
|
|
2015-10-20 23:29:29 +02:00
|
|
|
foreach ($objects as $key => $object) {
|
2015-07-24 15:30:57 +02:00
|
|
|
|
2015-10-20 22:22:58 +02:00
|
|
|
if ($object->hasBeenLoadedFromDb() && $rule->purge_existing === 'y') {
|
|
|
|
$found = false;
|
|
|
|
foreach ($sources as $source) {
|
|
|
|
if (array_key_exists($object->$objectKey, $imported[$source->id])) {
|
|
|
|
$found = true;
|
|
|
|
break;
|
2015-07-24 10:51:55 +02:00
|
|
|
}
|
|
|
|
}
|
2015-10-20 22:22:58 +02:00
|
|
|
|
|
|
|
if (! $found) {
|
2015-10-20 23:29:29 +02:00
|
|
|
// TODO: temporarily disabled, "mark" them: $object->delete();
|
2015-10-20 22:22:58 +02:00
|
|
|
}
|
2015-07-24 10:51:55 +02:00
|
|
|
}
|
2015-07-24 15:29:17 +02:00
|
|
|
|
2015-10-20 22:22:58 +02:00
|
|
|
// TODO: This should be noticed or removed:
|
|
|
|
if (! $object->$objectKey) {
|
2015-10-20 23:29:29 +02:00
|
|
|
$ignore[] = $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($ignore as $key) {
|
|
|
|
unset($objects[$key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $objects;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function runWithRule(SyncRule $rule)
|
|
|
|
{
|
|
|
|
$db = $rule->getConnection();
|
|
|
|
// TODO: Evaluate whether fetching data should happen within the same transaction
|
|
|
|
$objects = $this->prepareSyncForRule($rule);
|
|
|
|
$dba = $db->getDbAdapter();
|
|
|
|
$dba->beginTransaction();
|
|
|
|
foreach ($objects as $object) {
|
|
|
|
if ($object instanceof IcingaObject && $object->isTemplate()) {
|
|
|
|
if ($object->hasBeenModified()) {
|
|
|
|
throw new IcingaException(
|
|
|
|
'Sync is not allowed to modify template "%s"',
|
|
|
|
$object->$objectKey
|
|
|
|
);
|
|
|
|
}
|
2015-10-20 22:22:58 +02:00
|
|
|
continue;
|
|
|
|
}
|
2015-11-02 09:29:03 +01:00
|
|
|
if ($object->hasBeenModified()) {
|
|
|
|
$object->store($db);
|
|
|
|
}
|
2015-10-20 22:22:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$dba->commit();
|
2015-07-24 15:31:16 +02:00
|
|
|
return 42; // We have no sync_run history table yet
|
2015-07-24 10:51:55 +02:00
|
|
|
}
|
|
|
|
}
|