KickstartForm: new form added

This commit is contained in:
Thomas Gelf 2015-12-18 10:51:38 +01:00
parent 1d8530f18b
commit c52d57dc22
3 changed files with 124 additions and 0 deletions

View File

@ -28,6 +28,11 @@ class IndexController extends ActionController
$this->view->qlink($this->translate('click here'), 'director/settings')
);
}
$this->view->stats = $this->db()->getObjectSummary();
if ((int) $this->view->stats['apiuser']->cnt_total === 0) {
$this->view->form = $this->loadForm('kickstart')->setDb($this->db)->handleRequest();
}
}
protected function addGlobalTypeTabs()

View File

@ -0,0 +1,113 @@
<?php
namespace Icinga\Module\Director\Forms;
use Icinga\Module\Director\Objects\IcingaApiUser;
use Icinga\Module\Director\Core\CoreApi;
use Icinga\Module\Director\Core\RestApiClient;
use Icinga\Module\Director\Web\Form\QuickForm;
class KickstartForm extends QuickForm
{
protected $db;
protected $apiUser;
public function setup()
{
$this->addHtmlHint(
$this->translate(
'Your installation of Icinga Director has not yet been prepared for deployments. This kickstart wizard will assist you with setting up the connection to your Icinga 2 server'
)
);
// TODO: distinct endpoint name / host
$this->addElement('text', 'host', array(
'label' => $this->translate('Icinga Host'),
'description' => $this->translate('IP address / hostname of remote node'),
'required' => true,
));
$this->addElement('text', 'port', array(
'label' => $this->translate('Port'),
'value' => '5665',
'description' => $this->translate('The port your '),
'required' => true,
));
$this->addElement('text', 'username', array(
'label' => $this->translate('API user'),
'required' => true,
));
$this->addElement('password', 'password', array(
'label' => $this->translate('Password'),
'required' => true,
));
}
public function onSuccess()
{
$this->importZones();
$this->importEndpoints();
$this->apiUser()->store();
parent::onSuccess();
}
protected function apiUser()
{
if ($this->apiUser === null) {
$this->apiUser = IcingaApiUser::create(array(
'object_name' => $this->getValue('username'),
'object_type' => 'external_object',
'password' => $this->getValue('password')
), $this->db);
}
return $this->apiUser;
}
protected function importZones()
{
$db = $this->db;
foreach ($this->api()->setDb($db)->getZoneObjects() as $object) {
if (! $object::exists($object->object_name, $db)) {
$object->store();
}
}
}
protected function importEndpoints()
{
$db = $this->db;
$master = $this->getValue('host');
foreach ($this->api()->setDb($db)->getEndpointObjects() as $object) {
if ($object->object_name === 'master') {
$this->apiUser()->store();
$object->apiuser = $this->apiUser()->object_name;
}
if (! $object::exists($object->object_name, $db)) {
$object->store();
}
}
}
public function setDb($db)
{
$this->db = $db;
if ($this->object !== null) {
$this->object->setConnection($db);
}
return $this;
}
protected function api()
{
$client = new RestApiClient($this->getValue('host'), $this->getValue('port'));
$client->setCredentials($this->apiUser()->object_name, $this->apiUser()->password);
$api = new CoreApi($client);
return $api;
}
}

View File

@ -8,6 +8,12 @@
<p class="error"><?= $this->errorMessage ?></p>
<?php endif ?>
<?php
if ((int) $stats['apiuser']->cnt_total === 0) {
echo $this->form . "</div>\n";
return;
}
$actions = array(
array('cloud', $this->translate('Monitoring Nodes'), 'director/commands'),
array('host', $this->translate('Host objecs'), 'director/hosts'),