2015-06-30 11:19:31 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Web\Controller;
|
|
|
|
|
2016-02-03 00:54:00 +01:00
|
|
|
use Exception;
|
2016-03-01 11:24:24 +01:00
|
|
|
use Icinga\Exception\IcingaException;
|
2016-03-06 14:20:49 +01:00
|
|
|
use Icinga\Exception\InvalidPropertyException;
|
2016-02-17 19:22:36 +01:00
|
|
|
use Icinga\Exception\NotFoundError;
|
2017-06-26 14:39:35 +02:00
|
|
|
use Icinga\Module\Director\Exception\DuplicateKeyException;
|
2016-10-12 15:52:00 +02:00
|
|
|
use Icinga\Module\Director\Exception\NestingError;
|
2017-07-06 09:04:32 +02:00
|
|
|
use Icinga\Module\Director\Forms\IcingaObjectFieldForm;
|
2015-08-02 15:01:07 +02:00
|
|
|
use Icinga\Module\Director\Objects\IcingaObject;
|
2017-06-22 00:44:31 +02:00
|
|
|
use Icinga\Module\Director\Web\Controller\Extension\ObjectRestrictions;
|
2016-11-16 16:02:28 +01:00
|
|
|
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
|
2017-07-03 07:04:31 +02:00
|
|
|
use Icinga\Module\Director\Web\Tabs\ObjectTabs;
|
|
|
|
use ipl\Html\Html;
|
|
|
|
use ipl\Html\Link;
|
2015-06-30 11:19:31 +02:00
|
|
|
|
|
|
|
abstract class ObjectController extends ActionController
|
|
|
|
{
|
2017-06-22 00:44:31 +02:00
|
|
|
use ObjectRestrictions;
|
|
|
|
|
2017-02-08 12:38:14 +01:00
|
|
|
/** @var IcingaObject */
|
2015-06-30 11:19:31 +02:00
|
|
|
protected $object;
|
|
|
|
|
2015-12-10 13:11:21 +01:00
|
|
|
protected $isApified = true;
|
|
|
|
|
2016-03-22 18:19:13 +01:00
|
|
|
protected $allowedExternals = array(
|
|
|
|
'apiuser',
|
|
|
|
'endpoint'
|
|
|
|
);
|
|
|
|
|
2015-06-30 11:19:31 +02:00
|
|
|
public function init()
|
|
|
|
{
|
2015-12-10 13:11:21 +01:00
|
|
|
parent::init();
|
|
|
|
|
2016-12-16 12:07:01 +01:00
|
|
|
if ($this->getRequest()->isApiRequest()) {
|
|
|
|
$response = $this->getResponse();
|
|
|
|
try {
|
|
|
|
$this->loadObject();
|
2017-06-22 00:44:31 +02:00
|
|
|
$this->handleApiRequest();
|
2016-12-16 12:07:01 +01:00
|
|
|
} catch (NotFoundError $e) {
|
|
|
|
$response->setHttpResponseCode(404);
|
2017-06-22 00:44:31 +02:00
|
|
|
$this->sendJson($response, (object) array('error' => $e->getMessage()));
|
2017-06-26 14:39:35 +02:00
|
|
|
} catch (DuplicateKeyException $e) {
|
|
|
|
$response->setHttpResponseCode(422);
|
|
|
|
$this->sendJson($response, (object) array('error' => $e->getMessage()));
|
2016-12-16 12:07:01 +01:00
|
|
|
} catch (Exception $e) {
|
|
|
|
if ($response->getHttpResponseCode() === 200) {
|
|
|
|
$response->setHttpResponseCode(500);
|
|
|
|
}
|
|
|
|
|
2017-06-22 00:44:31 +02:00
|
|
|
$this->sendJson($response, (object) array('error' => $e->getMessage()));
|
2016-12-16 12:07:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-13 11:13:21 +02:00
|
|
|
$type = strtolower($this->getType());
|
2017-07-10 20:05:38 +02:00
|
|
|
if ($this->params->get('name') || $this->params->get('id')) {
|
2017-07-03 07:04:31 +02:00
|
|
|
$this->loadObject();
|
2015-06-30 11:19:31 +02:00
|
|
|
}
|
2017-07-03 07:04:31 +02:00
|
|
|
$this->tabs(new ObjectTabs($type, $this->getAuth(), $this->object));
|
2015-06-30 11:19:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function indexAction()
|
2015-07-30 10:04:12 +02:00
|
|
|
{
|
2015-12-10 13:11:21 +01:00
|
|
|
if ($this->getRequest()->isApiRequest()) {
|
2016-12-16 12:07:01 +01:00
|
|
|
return;
|
2015-12-02 03:32:42 +01:00
|
|
|
}
|
|
|
|
|
2016-03-18 14:58:46 +01:00
|
|
|
if ($this->object
|
|
|
|
&& $this->object->isExternal()
|
2016-03-22 18:19:13 +01:00
|
|
|
&& ! in_array($this->object->getShortTableName(), $this->allowedExternals)
|
2016-03-18 14:58:46 +01:00
|
|
|
) {
|
2016-03-21 21:24:22 +01:00
|
|
|
$this->redirectNow(
|
|
|
|
$this->getRequest()->getUrl()->setPath(sprintf('director/%s/render', $this->getType()))
|
|
|
|
);
|
2016-03-18 14:58:46 +01:00
|
|
|
}
|
|
|
|
|
2017-06-22 00:44:31 +02:00
|
|
|
$this->editAction();
|
2015-07-30 10:04:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function renderAction()
|
2015-06-30 11:19:31 +02:00
|
|
|
{
|
2016-11-03 16:06:18 +01:00
|
|
|
$this->assertPermission('director/showconfig');
|
2017-07-03 07:04:31 +02:00
|
|
|
$this->tabs()->activate('render');
|
2016-02-28 14:15:56 +01:00
|
|
|
$object = $this->object;
|
2017-07-03 07:04:31 +02:00
|
|
|
$this->addTitle(
|
|
|
|
$this->translate('Config preview: %s'),
|
|
|
|
$object->object_name
|
|
|
|
);
|
2016-02-28 14:15:56 +01:00
|
|
|
|
|
|
|
if ($this->params->shift('resolved')) {
|
2016-05-03 09:07:28 +02:00
|
|
|
$object = $object::fromPlainObject(
|
2016-02-28 14:15:56 +01:00
|
|
|
$object->toPlainObject(true),
|
|
|
|
$object->getConnection()
|
|
|
|
);
|
|
|
|
|
2017-07-03 07:04:31 +02:00
|
|
|
$this->actions()->add(Link::create(
|
2016-05-03 09:07:28 +02:00
|
|
|
$this->translate('Show normal'),
|
|
|
|
$this->getRequest()->getUrl()->without('resolved'),
|
|
|
|
null,
|
2017-07-03 07:04:31 +02:00
|
|
|
['class' => 'icon-resize-small state-warning']
|
|
|
|
));
|
2016-02-28 14:15:56 +01:00
|
|
|
} else {
|
2016-10-12 15:52:00 +02:00
|
|
|
try {
|
|
|
|
if ($object->supportsImports() && $object->imports()->count() > 0) {
|
2017-07-03 07:04:31 +02:00
|
|
|
$this->actions()->add(Link::create(
|
2016-10-12 15:52:00 +02:00
|
|
|
$this->translate('Show resolved'),
|
|
|
|
$this->getRequest()->getUrl()->with('resolved', true),
|
|
|
|
null,
|
2017-07-03 07:04:31 +02:00
|
|
|
['class' => 'icon-resize-full']
|
|
|
|
));
|
2016-10-12 15:52:00 +02:00
|
|
|
}
|
|
|
|
} catch (NestingError $e) {
|
|
|
|
// No resolve link with nesting errors
|
2016-02-28 14:15:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-03 07:04:31 +02:00
|
|
|
$content = $this->content();
|
|
|
|
if ($object->isDisabled()) {
|
|
|
|
$content->add(Html::p(
|
|
|
|
['class' => 'error'],
|
|
|
|
$this->translate('This object will not be deployed as it has been disabled')
|
|
|
|
));
|
|
|
|
}
|
|
|
|
if ($object->isExternal()) {
|
|
|
|
$content->add(Html::p($this->translate((
|
|
|
|
'This is an external object. It has been imported from Icinga 2 throught the'
|
|
|
|
. ' Core API and cannot be managed with the Icinga Director. It is however'
|
|
|
|
. ' perfectly valid to create objects using this or referring to this object.'
|
|
|
|
. ' You might also want to define related Fields to make work based on this'
|
|
|
|
. ' object more enjoyable'
|
|
|
|
))));
|
|
|
|
}
|
|
|
|
$config = $object->toSingleIcingaConfig();
|
2016-05-03 09:07:28 +02:00
|
|
|
|
2017-07-03 07:04:31 +02:00
|
|
|
foreach ($config->getFiles() as $filename => $file) {
|
|
|
|
if (! $object->isExternal()) {
|
|
|
|
$content->add(Html::h2($filename));
|
|
|
|
}
|
|
|
|
|
|
|
|
$classes = array();
|
|
|
|
if ($object->isDisabled()) {
|
|
|
|
$classes[] = 'disabled';
|
|
|
|
} elseif ($object->isExternal()) {
|
|
|
|
$classes[] = 'logfile';
|
|
|
|
}
|
|
|
|
|
|
|
|
$content->add(Html::pre(['class' => $classes], $file->getContent()));
|
|
|
|
}
|
2015-06-30 11:19:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function editAction()
|
|
|
|
{
|
2015-08-02 15:03:35 +02:00
|
|
|
$object = $this->object;
|
2017-07-03 07:04:31 +02:00
|
|
|
$this->addTitle($object->object_name);
|
|
|
|
$this->tabs()->activate('modify');
|
2015-06-30 11:19:31 +02:00
|
|
|
|
2017-07-03 07:04:31 +02:00
|
|
|
$formName = 'icinga' . ucfirst($this->getType());
|
2017-07-04 06:24:09 +02:00
|
|
|
$this->content()->add(
|
|
|
|
$form = $this->loadForm($formName)
|
|
|
|
->setDb($this->db())
|
|
|
|
->setAuth($this->Auth())
|
|
|
|
->setApi($this->getApiIfAvailable())
|
|
|
|
->setObject($object)
|
|
|
|
->setAuth($this->Auth())
|
|
|
|
->handleRequest()
|
2017-07-03 07:04:31 +02:00
|
|
|
);
|
2015-08-02 15:03:35 +02:00
|
|
|
|
2017-07-03 07:04:31 +02:00
|
|
|
$this->actions()->add($this->createCloneLink());
|
2016-03-20 12:14:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function createCloneLink()
|
|
|
|
{
|
2017-07-03 07:04:31 +02:00
|
|
|
return Link::create(
|
2016-03-18 13:37:15 +01:00
|
|
|
$this->translate('Clone'),
|
2016-03-20 12:14:48 +01:00
|
|
|
'director/' . $this->getType() .'/clone',
|
|
|
|
$this->object->getUrlParams(),
|
2016-03-03 09:45:33 +01:00
|
|
|
array('class' => 'icon-paste')
|
|
|
|
);
|
2015-06-30 11:19:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function addAction()
|
|
|
|
{
|
2017-07-03 07:04:31 +02:00
|
|
|
$this->tabs()->activate('add');
|
2015-06-30 11:19:31 +02:00
|
|
|
$type = $this->getType();
|
|
|
|
$ltype = strtolower($type);
|
|
|
|
|
|
|
|
$url = sprintf('director/%ss', $ltype);
|
2016-11-16 16:02:28 +01:00
|
|
|
/** @var DirectorObjectForm $form */
|
2015-07-30 09:26:02 +02:00
|
|
|
$form = $this->view->form = $this->loadForm('icinga' . ucfirst($type))
|
2015-06-30 11:19:31 +02:00
|
|
|
->setDb($this->db())
|
2017-07-03 07:04:31 +02:00
|
|
|
->setAuth($this->Auth())
|
2016-11-16 16:02:28 +01:00
|
|
|
->presetImports($this->params->shift('imports'))
|
2016-03-29 19:27:54 +02:00
|
|
|
->setApi($this->getApiIfAvailable())
|
2015-06-30 11:19:31 +02:00
|
|
|
->setSuccessUrl($url);
|
|
|
|
|
2017-07-03 07:04:31 +02:00
|
|
|
if ($oType = $this->params->shift('type')) {
|
|
|
|
$form->setPreferredObjectType($oType);
|
2016-10-13 13:53:01 +02:00
|
|
|
}
|
|
|
|
|
2017-07-03 07:04:31 +02:00
|
|
|
if ($oType === 'template') {
|
|
|
|
$this->assertPermission('director/admin');
|
|
|
|
$this->addTitle(
|
2016-10-13 13:53:01 +02:00
|
|
|
$this->translate('Add new Icinga %s template'),
|
|
|
|
ucfirst($ltype)
|
|
|
|
);
|
|
|
|
} else {
|
2017-07-03 07:04:31 +02:00
|
|
|
$this->assertPermission('director/' . $ltype);
|
|
|
|
$this->addTitle(
|
2016-10-13 13:53:01 +02:00
|
|
|
$this->translate('Add new Icinga %s'),
|
|
|
|
ucfirst($ltype)
|
|
|
|
);
|
|
|
|
}
|
2015-07-30 09:26:02 +02:00
|
|
|
|
2016-03-29 19:32:50 +02:00
|
|
|
$this->beforeHandlingAddRequest($form);
|
2015-07-30 09:26:02 +02:00
|
|
|
$form->handleRequest();
|
2017-07-03 07:04:31 +02:00
|
|
|
$this->content()->add($form);
|
2015-12-10 13:19:16 +01:00
|
|
|
}
|
|
|
|
|
2016-03-29 19:32:50 +02:00
|
|
|
protected function beforeHandlingAddRequest($form)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-12-10 13:19:16 +01:00
|
|
|
public function cloneAction()
|
|
|
|
{
|
|
|
|
$type = $this->getType();
|
2015-12-17 19:52:10 +01:00
|
|
|
$ltype = strtolower($type);
|
2017-07-03 07:04:31 +02:00
|
|
|
$this->assertPermission('director/' . $ltype);
|
|
|
|
$this->tabs()->activate('modify');
|
|
|
|
$this->addTitle($this->translate('Clone Icinga %s'), ucfirst($type));
|
|
|
|
$form = $this->loadForm('icingaCloneObject')->setObject($this->object);
|
|
|
|
$form->handleRequest();
|
|
|
|
$this->content()->add($form);
|
|
|
|
$this->actions()->add(Link::create(
|
2016-03-07 18:37:34 +01:00
|
|
|
$this->translate('back'),
|
2016-03-07 08:59:17 +01:00
|
|
|
'director/' . $ltype,
|
|
|
|
array('name' => $this->object->object_name),
|
|
|
|
array('class' => 'icon-left-big')
|
2017-07-03 07:04:31 +02:00
|
|
|
));
|
2015-06-30 11:19:31 +02:00
|
|
|
}
|
|
|
|
|
2015-08-02 15:01:47 +02:00
|
|
|
public function fieldsAction()
|
|
|
|
{
|
2016-11-03 16:06:18 +01:00
|
|
|
$this->hasPermission('director/admin');
|
2015-08-02 15:01:47 +02:00
|
|
|
$object = $this->object;
|
|
|
|
$type = $this->getType();
|
|
|
|
|
2017-07-03 07:04:31 +02:00
|
|
|
$this->tabs()->activate('fields');
|
2016-05-03 09:09:01 +02:00
|
|
|
|
2017-07-06 09:04:32 +02:00
|
|
|
$this->addTitle(
|
2016-05-03 09:09:01 +02:00
|
|
|
$this->translate('Custom fields: %s'),
|
2015-08-02 15:01:47 +02:00
|
|
|
$object->object_name
|
|
|
|
);
|
|
|
|
|
2017-07-06 09:04:32 +02:00
|
|
|
$form = IcingaObjectFieldForm::load()
|
2017-06-22 00:44:31 +02:00
|
|
|
->setDb($this->db())
|
2015-09-14 16:54:43 +02:00
|
|
|
->setIcingaObject($object);
|
|
|
|
|
|
|
|
if ($id = $this->params->get('field_id')) {
|
2015-10-13 17:58:50 +02:00
|
|
|
$form->loadObject(array(
|
|
|
|
$type . '_id' => $object->id,
|
|
|
|
'datafield_id' => $id
|
|
|
|
));
|
2016-02-28 10:58:42 +01:00
|
|
|
|
2017-07-06 09:04:32 +02:00
|
|
|
$this->actions()->add(Link::create(
|
2016-02-28 10:58:42 +01:00
|
|
|
$this->translate('back'),
|
2017-07-06 09:04:32 +02:00
|
|
|
$this->url()->without('field_id'),
|
2016-02-28 10:58:42 +01:00
|
|
|
null,
|
2017-07-06 09:04:32 +02:00
|
|
|
['class' => 'icon-left-big']
|
|
|
|
));
|
2015-09-14 16:54:43 +02:00
|
|
|
}
|
|
|
|
$form->handleRequest();
|
2015-08-02 15:01:47 +02:00
|
|
|
|
2017-07-06 09:04:32 +02:00
|
|
|
$table = $this->loadTable('icingaObjectDatafield')->setObject($object);
|
|
|
|
$this->content()->add([$form, $table]);
|
2015-08-02 15:01:47 +02:00
|
|
|
}
|
|
|
|
|
2015-06-30 11:19:31 +02:00
|
|
|
public function historyAction()
|
|
|
|
{
|
2016-11-03 16:06:18 +01:00
|
|
|
$this->hasPermission('director/audit');
|
2016-03-16 23:22:43 +01:00
|
|
|
$this->setAutorefreshInterval(10);
|
2016-03-16 22:50:35 +01:00
|
|
|
$db = $this->db();
|
2015-06-30 11:19:31 +02:00
|
|
|
$type = $this->getType();
|
2017-07-03 07:04:31 +02:00
|
|
|
$this->tabs()->activate('history');
|
2017-07-06 09:04:32 +02:00
|
|
|
$this->addTitle(
|
2016-03-20 13:14:49 +01:00
|
|
|
$this->translate('Activity Log: %s'),
|
|
|
|
$this->object->object_name
|
|
|
|
);
|
2016-03-16 22:50:35 +01:00
|
|
|
$lastDeployedId = $db->getLastDeploymentActivityLogId();
|
2017-07-06 09:04:32 +02:00
|
|
|
$this->content()->add(
|
|
|
|
$this->applyPaginationLimits(
|
|
|
|
$this->loadTable('activityLog')
|
|
|
|
->setConnection($db)
|
|
|
|
->setLastDeployedId($lastDeployedId)
|
|
|
|
->filterObject('icinga_' . $type, $this->object->object_name)
|
|
|
|
)
|
|
|
|
)->addAttributes(['data-base-target' => '_next']);
|
2015-06-30 11:19:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function getType()
|
|
|
|
{
|
|
|
|
// Strip final 's' and upcase an eventual 'group'
|
|
|
|
return preg_replace(
|
2016-10-13 11:13:21 +02:00
|
|
|
array('/group$/', '/period$/', '/argument$/', '/apiuser$/', '/set$/'),
|
|
|
|
array('Group', 'Period', 'Argument', 'ApiUser', 'Set'),
|
2015-06-30 11:19:31 +02:00
|
|
|
$this->getRequest()->getControllerName()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-08-02 15:01:07 +02:00
|
|
|
protected function loadObject()
|
2015-06-30 11:19:31 +02:00
|
|
|
{
|
2016-03-06 14:20:49 +01:00
|
|
|
if ($this->object === null) {
|
|
|
|
if ($name = $this->params->get('name')) {
|
|
|
|
$this->object = IcingaObject::loadByType(
|
|
|
|
$this->getType(),
|
|
|
|
$name,
|
|
|
|
$this->db()
|
|
|
|
);
|
2017-03-06 21:46:22 +01:00
|
|
|
|
|
|
|
if (! $this->allowsObject($this->object)) {
|
|
|
|
$this->object = null;
|
|
|
|
throw new NotFoundError('No such object available');
|
|
|
|
}
|
2016-03-06 14:20:49 +01:00
|
|
|
} elseif ($id = $this->params->get('id')) {
|
|
|
|
$this->object = IcingaObject::loadByType(
|
|
|
|
$this->getType(),
|
|
|
|
(int) $id,
|
|
|
|
$this->db()
|
|
|
|
);
|
2016-03-06 20:24:47 +01:00
|
|
|
} elseif ($this->getRequest()->isApiRequest()) {
|
2016-03-12 01:36:58 +01:00
|
|
|
if ($this->getRequest()->isGet()) {
|
|
|
|
$this->getResponse()->setHttpResponseCode(422);
|
2016-03-06 20:24:47 +01:00
|
|
|
|
2016-03-12 01:36:58 +01:00
|
|
|
throw new InvalidPropertyException(
|
|
|
|
'Cannot load object, missing parameters'
|
|
|
|
);
|
|
|
|
}
|
2016-03-06 14:20:49 +01:00
|
|
|
}
|
2016-02-27 12:23:00 +01:00
|
|
|
|
|
|
|
$this->view->undeployedChanges = $this->countUndeployedChanges();
|
|
|
|
$this->view->totalUndeployedChanges = $this->db()
|
|
|
|
->countActivitiesSinceLastDeployedConfig();
|
2015-06-30 11:19:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->object;
|
|
|
|
}
|
2016-02-03 00:54:00 +01:00
|
|
|
|
2016-05-25 08:11:53 +02:00
|
|
|
protected function hasFields()
|
|
|
|
{
|
|
|
|
if (! ($object = $this->object)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $object->hasBeenLoadedFromDb()
|
|
|
|
&& $object->supportsFields()
|
|
|
|
&& ($object->isTemplate() || $this->getType() === 'command');
|
|
|
|
}
|
|
|
|
|
2016-02-03 00:54:00 +01:00
|
|
|
protected function handleApiRequest()
|
|
|
|
{
|
|
|
|
$request = $this->getRequest();
|
|
|
|
$db = $this->db();
|
|
|
|
|
|
|
|
switch ($request->getMethod()) {
|
|
|
|
case 'DELETE':
|
2016-02-17 19:22:36 +01:00
|
|
|
$this->requireObject();
|
2016-02-03 00:54:00 +01:00
|
|
|
$obj = $this->object->toPlainObject(false, true);
|
2017-06-22 00:44:31 +02:00
|
|
|
$this->loadForm(
|
2016-02-03 00:54:00 +01:00
|
|
|
'icingaDeleteObject'
|
|
|
|
)->setObject($this->object)->setRequest($request)->onSuccess();
|
|
|
|
|
2017-06-22 00:44:31 +02:00
|
|
|
$this->sendJson($this->getResponse(), $obj);
|
|
|
|
break;
|
2016-02-03 00:54:00 +01:00
|
|
|
|
|
|
|
case 'POST':
|
|
|
|
case 'PUT':
|
|
|
|
$type = $this->getType();
|
2016-03-01 11:24:24 +01:00
|
|
|
$data = json_decode($request->getRawBody());
|
|
|
|
|
|
|
|
if ($data === null) {
|
|
|
|
$this->getResponse()->setHttpResponseCode(400);
|
|
|
|
throw new IcingaException(
|
|
|
|
'Invalid JSON: %s' . $request->getRawBody(),
|
|
|
|
$this->getLastJsonError()
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$data = (array) $data;
|
|
|
|
}
|
2016-02-03 00:54:00 +01:00
|
|
|
if ($object = $this->object) {
|
|
|
|
if ($request->getMethod() === 'POST') {
|
|
|
|
$object->setProperties($data);
|
|
|
|
} else {
|
2016-02-23 09:09:10 +01:00
|
|
|
$data = array_merge(
|
|
|
|
array(
|
|
|
|
'object_type' => $object->object_type,
|
|
|
|
'object_name' => $object->object_name
|
|
|
|
),
|
|
|
|
$data
|
|
|
|
);
|
2016-02-03 00:54:00 +01:00
|
|
|
$object->replaceWith(
|
2016-02-03 10:28:01 +01:00
|
|
|
IcingaObject::createByType($type, $data, $db)
|
2016-02-03 00:54:00 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$object = IcingaObject::createByType($type, $data, $db);
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = $this->getResponse();
|
|
|
|
if ($object->hasBeenModified()) {
|
2016-03-01 11:24:24 +01:00
|
|
|
$status = $object->hasBeenLoadedFromDb() ? 200 : 201;
|
2016-02-03 00:54:00 +01:00
|
|
|
$object->store();
|
2016-03-01 11:24:24 +01:00
|
|
|
$response->setHttpResponseCode($status);
|
2016-02-03 00:54:00 +01:00
|
|
|
} else {
|
|
|
|
$response->setHttpResponseCode(304);
|
|
|
|
}
|
|
|
|
|
2017-06-22 00:44:31 +02:00
|
|
|
$this->sendJson($response, $object->toPlainObject(false, true));
|
|
|
|
break;
|
2016-02-03 00:54:00 +01:00
|
|
|
|
|
|
|
case 'GET':
|
2016-02-17 19:22:36 +01:00
|
|
|
$this->requireObject();
|
2017-06-22 00:44:31 +02:00
|
|
|
$this->sendJson(
|
|
|
|
$this->getResponse(),
|
2016-02-03 00:54:00 +01:00
|
|
|
$this->object->toPlainObject(
|
|
|
|
$this->params->shift('resolved'),
|
|
|
|
! $this->params->shift('withNull'),
|
|
|
|
$this->params->shift('properties')
|
|
|
|
)
|
|
|
|
);
|
2017-06-22 00:44:31 +02:00
|
|
|
break;
|
2016-02-03 00:54:00 +01:00
|
|
|
|
|
|
|
default:
|
2016-03-01 11:24:24 +01:00
|
|
|
$request->getResponse()->setHttpResponseCode(400);
|
2016-02-03 00:54:00 +01:00
|
|
|
throw new Exception('Unsupported method ' . $request->getMethod());
|
|
|
|
}
|
|
|
|
}
|
2016-02-17 19:22:36 +01:00
|
|
|
|
2016-02-27 12:23:00 +01:00
|
|
|
protected function countUndeployedChanges()
|
|
|
|
{
|
|
|
|
if ($this->object === null) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->db()->countActivitiesSinceLastDeployedConfig($this->object);
|
|
|
|
}
|
|
|
|
|
2016-02-17 19:22:36 +01:00
|
|
|
protected function requireObject()
|
|
|
|
{
|
|
|
|
if (! $this->object) {
|
|
|
|
$this->getResponse()->setHttpResponseCode(404);
|
|
|
|
if (! $this->params->get('name')) {
|
|
|
|
throw new NotFoundError('You need to pass a "name" parameter to access a specific object');
|
|
|
|
} else {
|
|
|
|
throw new NotFoundError('No such object available');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-18 11:44:48 +01:00
|
|
|
|
2016-05-25 08:11:53 +02:00
|
|
|
protected function gracefullyActivateTab($name)
|
|
|
|
{
|
|
|
|
$tabs = $this->getTabs();
|
|
|
|
|
|
|
|
if ($tabs->has($name)) {
|
|
|
|
return $tabs->activate($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
$req = $this->getRequest();
|
|
|
|
$this->redirectNow(
|
|
|
|
$req->getUrl()->setPath('director/' . $req->getControllerName())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-03-18 11:44:48 +01:00
|
|
|
protected function beforeTabs()
|
|
|
|
{
|
|
|
|
}
|
2015-06-30 11:19:31 +02:00
|
|
|
}
|