Skip to content
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
7 changes: 1 addition & 6 deletions src/Plugin/EmulateElastic/AddTemplateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,7 @@ public function run(): Task {
throw new \Exception('Cannot parse request');
}
$patterns = json_encode($request['index_patterns']);
foreach (['index_patterns', 'settings', 'mappings'] as $removeProp) {
if (!isset($request[$removeProp])) {
continue;
}
unset($request[$removeProp]);
}
unset($request['index_patterns']);
$content = json_encode($request);
$templateTable = self::TEMPLATE_TABLE;

Expand Down
26 changes: 20 additions & 6 deletions src/Plugin/EmulateElastic/CatHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
use Manticoresearch\Buddy\Core\Plugin\BaseHandlerWithClient;
use Manticoresearch\Buddy\Core\Task\Task;
use Manticoresearch\Buddy\Core\Task\TaskResult;

use RuntimeException;

/**
* Handling requests to extract and build info on Kibana entities, like templates, dashboards, etc.
*/
class CatHandler extends BaseHandlerWithClient {

const CAT_ENTITIES = ['templates'];
const CAT_ENTITIES = ['templates', 'plugins'];

/**
* Initialize the executor
Expand Down Expand Up @@ -58,11 +59,10 @@ public function run(): Task {

$catInfo = [];
foreach ($queryResult[0]['data'] as $entityInfo) {
$catInfo[] = [
'name' => $entityInfo['name'],
'order' => 0,
'index_patterns' => simdjson_decode($entityInfo['patterns'], true),
] + simdjson_decode($entityInfo['content'], true);
$catInfo[] = match ($entityTable) {
'_templates' => self::buildCatTemplateRow($entityInfo),
default => [],
};
}

return TaskResult::raw($catInfo);
Expand All @@ -72,4 +72,18 @@ public function run(): Task {
$taskFn, [$this->payload, $this->manticoreClient]
)->run();
}

/**
*
* @param array{name:string,patterns:string,content:string} $entityInfo
* @return array<string,mixed>
*/
private static function buildCatTemplateRow(array $entityInfo): array {
return [
'name' => $entityInfo['name'],
'order' => 0,
'index_patterns' => simdjson_decode($entityInfo['patterns'], true),
] + simdjson_decode($entityInfo['content'], true);
}

}
24 changes: 15 additions & 9 deletions src/Plugin/EmulateElastic/InitKibanaHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Manticoresearch\Buddy\Base\Plugin\EmulateElastic;

use Manticoresearch\Buddy\Core\Error\GenericError;
use Manticoresearch\Buddy\Core\ManticoreSearch\Client as HTTPClient;
use Manticoresearch\Buddy\Core\Task\Task;
use Manticoresearch\Buddy\Core\Task\TaskResult;
Expand Down Expand Up @@ -65,15 +66,20 @@ public function run(): Task {
],
'status' => 404,
];
} else {
$resp = [];
foreach ($queryResult[0]['data'] as $entity) {
$resp[$entity['_index']] = [
'aliases' => [
$alias => [],
],
] + simdjson_decode($entity['_source'], true);
}
$customError = GenericError::create('', false);
$customError->setResponseErrorBody($resp);
$customError->setResponseErrorCode(404);

throw $customError;
}

$resp = [];
foreach ($queryResult[0]['data'] as $entity) {
$resp[$entity['_index']] = [
'aliases' => [
$alias => [],
],
] + simdjson_decode($entity['_source'], true);
}

return TaskResult::raw($resp);
Expand Down
4 changes: 3 additions & 1 deletion src/Plugin/EmulateElastic/NodesInfoKibanaHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/
class NodesInfoKibanaHandler extends BaseHandlerWithClient {

const DEFAULT_KIBANA_VERSION = '7.6.0';

/**
* Initialize the executor
*
Expand Down Expand Up @@ -58,7 +60,7 @@ public function run(): Task {
'publish_address' => "$ip:$port",
],
'ip' => $ip,
'version' => '7.6.0',
'version' => $settings->searchdKibanaVersionString ?? self::DEFAULT_KIBANA_VERSION,
],
],
]
Expand Down
48 changes: 21 additions & 27 deletions src/Plugin/EmulateElastic/Payload.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class Payload extends BasePayload {

// Endpoint position in Kibana request path
const KIBANA_ENDPOINT_PATH_POS = [
0 => ['_aliases', '_alias', '_cat', '_field_caps', '_template'],
0 => ['_aliases', '_alias', '_cat', '_field_caps', '_template', '_index_template'],
1 => ['_create', '_doc', '_update', '_field_caps'],
];

Expand Down Expand Up @@ -73,22 +73,6 @@ public static function fromRequest(Request $request): static {
$self->path = $request->path;
self::detectRequestTarget($pathParts, $self);
switch (static::$requestTarget) {
case '_cat':
case '_count':
case '_license':
case '_nodes':
case '_xpack':
case '.kibana':
case '.kibana_task_manager':
case '_update_by_query':
case 'metric':
case 'config':
case 'space':
case 'index-pattern':
case 'settings':
case 'telemetry':
case 'stats':
break;
case '_doc':
static::$requestTarget .= '_' . strtolower($request->httpMethod);
$self->body = $request->payload;
Expand All @@ -107,6 +91,17 @@ public static function fromRequest(Request $request): static {
$self->table = static::$requestTarget;
$self->body = $request->payload;
break;
case '_index_template':
case '_template':
if ($request->httpMethod !== 'PUT') {
// Need this to avoid sending the 404 response for Elasticdump's requests which causes its failure
$customError = InvalidNetworkRequestError::create('', true);
$customError->setResponseErrorCode(200);
throw $customError;
}
$self->table = end($pathParts);
$self->body = $request->payload;
break;
case '_mapping':
/**
* @var array{
Expand All @@ -125,18 +120,17 @@ public static function fromRequest(Request $request): static {
$self->table = $pathParts[0];
$self->body = $request->payload;
break;
case '_template':
$self->table = end($pathParts);
$self->body = $request->payload;
break;
default:
if ($pathParts[0] === '_index_template') {
// Need this to avoid sending the 404 response for Elasticdump's requests which causes its failure
$customError = InvalidNetworkRequestError::create('', true);
$customError->setResponseErrorCode(200);
throw $customError;
if (!in_array(
static::$requestTarget,
[
'_cat', '_count', '_license', '_nodes', '_xpack', '.kibana', '.kibana_task_manager',
'_update_by_query', 'metric', 'config', 'space', 'index-pattern', 'settings', 'telemetry',
'stats',
]
)) {
throw new Exception("Unsupported request type in {$request->path}: " . static::$requestTarget);
}
throw new Exception("Unsupported request type in {$request->path}: " . static::$requestTarget);
}

return $self;
Expand Down