Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP API to configure contacts and contactgroups #199

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions application/controllers/ApiV1ChannelsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

/* Icinga Notifications Web | (c) 2024 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Notifications\Controllers;

use Exception;
use Icinga\Module\Notifications\Common\Database;
use Icinga\Util\Environment;
use Icinga\Util\Json;
use ipl\Sql\Compat\FilterProcessor;
use ipl\Sql\Select;
use ipl\Stdlib\Filter;
use ipl\Web\Compat\CompatController;
use ipl\Web\Filter\QueryString;
use ipl\Web\Url;
use Ramsey\Uuid\Uuid;
use stdClass;

class ApiV1ChannelsController extends CompatController
{
public function indexAction(): void
{
$this->assertPermission('notifications/api/v1');

$request = $this->getRequest();
if (! $request->isApiRequest()) {
$this->httpBadRequest('No API request');
}

$method = $request->getMethod();
if ($method !== 'GET') {
$this->httpBadRequest('Only GET method supported');
}

$db = Database::get();

/** @var ?string $identifier */
$identifier = $request->getParam('identifier');

if ($identifier && ! Uuid::isValid($identifier)) {
$this->httpBadRequest('The given identifier is not a valid UUID');
}

try {
$filterRule = QueryString::fromString(Url::fromRequest()->getQueryString())
->on(
QueryString::ON_CONDITION,
function (Filter\Condition $condition) {
$column = $condition->getColumn();
if (! in_array($column, ['id', 'name', 'type'])) {
$this->httpBadRequest(sprintf(
'Invalid filter column %s given, only id, name and type are allowed',
$column
));
}

if ($column === 'id') {
if (! Uuid::isValid($condition->getValue())) {

Check failure on line 59 in application/controllers/ApiV1ChannelsController.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Parameter #1 $uuid of static method Ramsey\Uuid\Uuid::isValid() expects string, mixed given.

Check failure on line 59 in application/controllers/ApiV1ChannelsController.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Parameter #1 $uuid of static method Ramsey\Uuid\Uuid::isValid() expects string, mixed given.

Check failure on line 59 in application/controllers/ApiV1ChannelsController.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Parameter #1 $uuid of static method Ramsey\Uuid\Uuid::isValid() expects string, mixed given.

Check failure on line 59 in application/controllers/ApiV1ChannelsController.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Parameter #1 $uuid of static method Ramsey\Uuid\Uuid::isValid() expects string, mixed given.

Check failure on line 59 in application/controllers/ApiV1ChannelsController.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Parameter #1 $uuid of static method Ramsey\Uuid\Uuid::isValid() expects string, mixed given.

Check failure on line 59 in application/controllers/ApiV1ChannelsController.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Parameter #1 $uuid of static method Ramsey\Uuid\Uuid::isValid() expects string, mixed given.

Check failure on line 59 in application/controllers/ApiV1ChannelsController.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Parameter #1 $uuid of static method Ramsey\Uuid\Uuid::isValid() expects string, mixed given.
$this->httpBadRequest('The given filter id is not a valid UUID');
}

$condition->setColumn('external_uuid');
}
}
)->parse();

$filter = FilterProcessor::assembleFilter($filterRule);
} catch (Exception $e) {
$this->httpBadRequest($e->getMessage());
}

$stmt = (new Select())
->distinct()
->from('channel ch')
->columns([
'channel_id' => 'ch.id',
'id' => 'ch.external_uuid',
'name',
'type',
'config'
]);

if ($identifier !== null) {
$stmt->where(['external_uuid = ?' => $identifier]);

/** @var stdClass|false $result */
$result = $db->fetchOne($stmt);

if ($result === false) {
$this->httpNotFound('Channel not found');
}

unset($result->channel_id);

$this->getResponse()
->setHttpResponseCode(200)
->json()
->setSuccessData((array) $result)
->sendResponse();
} else {
if ($filter !== null) {
$stmt->where($filter);
}

$stmt->limit(500);
$offset = 0;

ob_end_clean();
Environment::raiseExecutionTime();

$this->getResponse()
->setHeader('Content-Type', 'application/json')
->setHeader('Cache-Control', 'no-store')
->sendResponse();

echo '[';

$res = $db->select($stmt->offset($offset));
do {
/** @var stdClass $row */
foreach ($res as $i => $row) {
if ($i > 0 || $offset !== 0) {
echo ",\n";
}

unset($row->channel_id);

echo Json::sanitize($row);
}

$offset += 500;
$res = $db->select($stmt->offset($offset));
} while ($res->rowCount());

echo ']';
}

exit;
}
}
Loading
Loading