2015-07-21 15:16:18 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Forms;
|
|
|
|
|
|
|
|
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
|
|
|
|
use Icinga\Web\Hook;
|
|
|
|
|
|
|
|
class ImportSourceForm extends DirectorObjectForm
|
|
|
|
{
|
|
|
|
public function setup()
|
|
|
|
{
|
|
|
|
$this->addElement('text', 'source_name', array(
|
2016-03-14 12:39:41 +01:00
|
|
|
'label' => $this->translate('Import source name'),
|
|
|
|
'description' => $this->translate(
|
|
|
|
'A short name identifying this import source. Use something meaningful,'
|
|
|
|
. ' like "Hosts from Puppet", "Users from Active Directory" or similar'
|
|
|
|
),
|
2015-07-21 15:16:18 +02:00
|
|
|
'required' => true,
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->addElement('select', 'provider_class', array(
|
2016-03-14 12:39:41 +01:00
|
|
|
'label' => $this->translate('Source Type'),
|
|
|
|
'required' => true,
|
2015-08-03 13:37:43 +02:00
|
|
|
'multiOptions' => $this->optionalEnum($this->enumSourceTypes()),
|
2016-03-14 12:39:41 +01:00
|
|
|
'description' => $this->translate(
|
|
|
|
'These are different data providers fetching data from various sources.'
|
2016-03-14 12:55:26 +01:00
|
|
|
. ' You didn\'t find what you\'re looking for? Import sources are implemented'
|
2016-03-14 12:39:41 +01:00
|
|
|
. ' as a hook in Director, so you might find (or write your own) Icinga Web 2'
|
|
|
|
. ' module fetching data from wherever you want'
|
|
|
|
),
|
|
|
|
'class' => 'autosubmit'
|
2015-07-21 15:16:18 +02:00
|
|
|
));
|
|
|
|
|
2015-08-03 13:37:43 +02:00
|
|
|
$this->addSettings();
|
2015-11-03 09:46:23 +01:00
|
|
|
$this->setButtons();
|
2015-07-21 15:16:18 +02:00
|
|
|
}
|
|
|
|
|
2015-12-03 01:37:36 +01:00
|
|
|
public function getSentOrObjectSetting($name, $default = null)
|
|
|
|
{
|
|
|
|
if ($this->hasObject()) {
|
|
|
|
$value = $this->getSentValue($name);
|
|
|
|
if ($value === null) {
|
|
|
|
$object = $this->getObject();
|
|
|
|
|
|
|
|
return $object->getSetting($name, $default);
|
|
|
|
} else {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return $this->getSentValue($name, $default);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-21 15:16:18 +02:00
|
|
|
protected function addSettings($class = null)
|
|
|
|
{
|
2015-11-30 13:06:34 +01:00
|
|
|
if (! ($class = $this->getProviderClass())) {
|
|
|
|
return;
|
2015-07-21 15:16:18 +02:00
|
|
|
}
|
|
|
|
|
2015-11-30 13:06:34 +01:00
|
|
|
$defaultKeyCol = $this->getDefaultKeyColumnName();
|
|
|
|
|
|
|
|
$this->addElement('text', 'key_column', array(
|
|
|
|
'label' => $this->translate('Key column name'),
|
2016-03-14 12:39:41 +01:00
|
|
|
'description' => $this->translate(
|
|
|
|
'This must be a column containing unique values like hostnames. Unless otherwise'
|
|
|
|
. ' specified this will then be used as the object_name for the syncronized'
|
|
|
|
. ' Icinga object. Especially when getting started with director please make'
|
|
|
|
. ' sure to strictly follow this rule. Duplicate values for this column on different'
|
|
|
|
. ' rows will trigger a failure, your import run will not succeed'
|
|
|
|
),
|
2015-11-30 13:06:34 +01:00
|
|
|
'placeholder' => $defaultKeyCol,
|
|
|
|
'required' => $defaultKeyCol === null,
|
|
|
|
));
|
|
|
|
|
2015-08-03 13:37:43 +02:00
|
|
|
if (array_key_exists($class, $this->enumSourceTypes())) {
|
|
|
|
$class::addSettingsFormFields($this);
|
|
|
|
foreach ($this->object()->getSettings() as $key => $val) {
|
|
|
|
if ($el = $this->getElement($key)) {
|
|
|
|
$el->setValue($val);
|
|
|
|
}
|
2015-07-21 15:16:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 13:06:34 +01:00
|
|
|
protected function getDefaultKeyColumnName()
|
|
|
|
{
|
|
|
|
if (! ($class = $this->getProviderClass())) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-11-30 13:31:28 +01:00
|
|
|
if (! class_exists($class)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-11-30 13:06:34 +01:00
|
|
|
return $class::getDefaultKeyColumnName();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getProviderClass()
|
|
|
|
{
|
|
|
|
if ($this->hasBeenSent()) {
|
|
|
|
$class = $this->getRequest()->getPost('provider_class');
|
|
|
|
} else {
|
|
|
|
if (! ($class = $this->object()->provider_class)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $class;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onSuccess()
|
|
|
|
{
|
|
|
|
if (! $this->getValue('key_column')) {
|
|
|
|
if ($default = $this->getDefaultKeyColumnName()) {
|
|
|
|
$this->setElementValue('key_column', $default);
|
|
|
|
$this->object()->key_column = $default;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::onSuccess();
|
|
|
|
}
|
|
|
|
|
2015-07-21 15:16:18 +02:00
|
|
|
protected function enumSourceTypes()
|
|
|
|
{
|
|
|
|
$hooks = Hook::all('Director\\ImportSource');
|
2015-12-15 14:42:54 +01:00
|
|
|
|
2015-08-03 13:37:43 +02:00
|
|
|
$enum = array();
|
2015-07-21 15:16:18 +02:00
|
|
|
foreach ($hooks as $hook) {
|
|
|
|
$enum[get_class($hook)] = $hook->getName();
|
|
|
|
}
|
2015-12-15 14:42:54 +01:00
|
|
|
asort($enum);
|
2015-07-21 15:16:18 +02:00
|
|
|
|
|
|
|
return $enum;
|
|
|
|
}
|
|
|
|
}
|