monitoring/api: Use JsonResponse in the ActionsController

refs #9606
This commit is contained in:
Eric Lippmann 2015-09-07 13:37:18 +02:00
parent 26d1a04e48
commit 91b9a83be5

View File

@ -14,26 +14,36 @@ use Icinga\Module\Monitoring\Object\ServiceList;
*/ */
class Monitoring_ActionsController extends Controller class Monitoring_ActionsController extends Controller
{ {
/**
* Get the filter from URL parameters or exit immediately if the filter is empty
*
* @return Filter
*/
protected function getFilterOrExitIfEmpty()
{
$filter = Filter::fromQueryString((string) $this->params);
if ($filter->isEmpty()) {
$this->getResponse()->json()
->setFailData(array('filter' => 'Filter is required and must not be empty'))
->sendResponse();
}
return $filter;
}
/** /**
* Schedule host downtimes * Schedule host downtimes
*/ */
public function scheduleHostDowntimeAction() public function scheduleHostDowntimeAction()
{ {
$filter = Filter::fromQueryString((string) $this->params); $filter = $this->getFilterOrExitIfEmpty();
/** @var Filter $filter */
if ($filter->isEmpty()) {
$this->httpBadRequest('Filter must not be empty');
}
// @TODO(el): $this->backend->list('host')->handleRequest()->fetchAll()
$hostList = new HostList($this->backend); $hostList = new HostList($this->backend);
$this->applyRestriction('monitoring/filter/objects', $hostList); $hostList
$hostList->addFilter($filter); ->applyFilter($this->getRestriction('monitoring/filter/objects'))
->applyFilter($filter);
if (! $hostList->count()) { if (! $hostList->count()) {
// @TODO(el): Use ApiResponse class for unified response handling. $this->getResponse()->json()
$this->getResponse()->sendJson(array( ->setFailData(array('filter' => 'No hosts found matching the filter'))
'status' => 'fail', ->sendResponse();
'message' => 'No hosts found matching the given filter'
));
} }
$form = new ScheduleHostDowntimeCommandForm(); $form = new ScheduleHostDowntimeCommandForm();
$form $form
@ -47,25 +57,22 @@ class Monitoring_ActionsController extends Controller
*/ */
public function removeHostDowntimeAction() public function removeHostDowntimeAction()
{ {
$filter = $this->getFilterOrExitIfEmpty();
$downtimes = $this->backend $downtimes = $this->backend
->select() ->select()
->from('downtime', array('host_name', 'id' => 'downtime_internal_id')) ->from('downtime', array('host_name', 'id' => 'downtime_internal_id'))
->where('object_type', 'host') ->where('object_type', 'host')
->applyFilter($this->getRestriction('monitoring/filter/objects')) ->applyFilter($this->getRestriction('monitoring/filter/objects'))
->setRequiresFilter(true) ->applyFilter($filter);
->handleRequest($this->getRequest()) if (! $downtimes->count()) {
->fetchAll(); $this->getResponse()->json()
if (empty($downtimes)) { ->setFailData(array('filter' => 'No downtimes found matching the filter'))
// @TODO(el): Use ApiResponse class for unified response handling. ->sendResponse();
$this->getResponse()->sendJson(array(
'status' => 'fail',
'message' => 'No downtimes found matching the given filter'
));
} }
$form = new DeleteDowntimesCommandForm(); $form = new DeleteDowntimesCommandForm();
$form $form
->setIsApiTarget(true) ->setIsApiTarget(true)
->setDowntimes($downtimes) ->setDowntimes($downtimes->fetchAll())
->handleRequest($this->getRequest()); ->handleRequest($this->getRequest());
// @TODO(el): Respond w/ the downtimes deleted instead of the notifiaction added by // @TODO(el): Respond w/ the downtimes deleted instead of the notifiaction added by
// DeleteDowntimesCommandForm::onSuccess(). // DeleteDowntimesCommandForm::onSuccess().
@ -76,21 +83,15 @@ class Monitoring_ActionsController extends Controller
*/ */
public function scheduleServiceDowntimeAction() public function scheduleServiceDowntimeAction()
{ {
$filter = Filter::fromQueryString((string) $this->params); $filter = $this->getFilterOrExitIfEmpty();
/** @var Filter $filter */
if ($filter->isEmpty()) {
$this->httpBadRequest('Filter must not be empty');
}
// @TODO(el): $this->backend->list('service')->handleRequest()->fetchAll()
$serviceList = new ServiceList($this->backend); $serviceList = new ServiceList($this->backend);
$this->applyRestriction('monitoring/filter/objects', $serviceList); $serviceList
$serviceList->addFilter($filter); ->applyFilter($this->getRestriction('monitoring/filter/objects'))
->applyFilter($filter);
if (! $serviceList->count()) { if (! $serviceList->count()) {
// @TODO(el): Use ApiResponse class for unified response handling. $this->getResponse()->json()
$this->getResponse()->sendJson(array( ->setFailData(array('filter' => 'No services found matching the filter'))
'status' => 'fail', ->sendResponse();
'message' => 'No services found matching the given filter'
));
} }
$form = new ScheduleServiceDowntimeCommandForm(); $form = new ScheduleServiceDowntimeCommandForm();
$form $form
@ -104,25 +105,22 @@ class Monitoring_ActionsController extends Controller
*/ */
public function removeServiceDowntimeAction() public function removeServiceDowntimeAction()
{ {
$filter = $this->getFilterOrExitIfEmpty();
$downtimes = $this->backend $downtimes = $this->backend
->select() ->select()
->from('downtime', array('host_name', 'service_description', 'id' => 'downtime_internal_id')) ->from('downtime', array('host_name', 'service_description', 'id' => 'downtime_internal_id'))
->where('object_type', 'service') ->where('object_type', 'service')
->applyFilter($this->getRestriction('monitoring/filter/objects')) ->applyFilter($this->getRestriction('monitoring/filter/objects'))
->setRequiresFilter(true) ->applyFilter($filter);
->handleRequest($this->getRequest()) if (! $downtimes->count()) {
->fetchAll(); $this->getResponse()->json()
if (empty($downtimes)) { ->setFailData(array('filter' => 'No downtimes found matching the filter'))
// @TODO(el): Use ApiResponse class for unified response handling. ->sendResponse();
$this->getResponse()->sendJson(array(
'status' => 'fail',
'message' => 'No downtimes found matching the given filter'
));
} }
$form = new DeleteDowntimesCommandForm(); $form = new DeleteDowntimesCommandForm();
$form $form
->setIsApiTarget(true) ->setIsApiTarget(true)
->setDowntimes($downtimes) ->setDowntimes($downtimes->fetchAll())
->handleRequest($this->getRequest()); ->handleRequest($this->getRequest());
// @TODO(el): Respond w/ the downtimes deleted instead of the notifiaction added by // @TODO(el): Respond w/ the downtimes deleted instead of the notifiaction added by
// DeleteDowntimesCommandForm::onSuccess(). // DeleteDowntimesCommandForm::onSuccess().