diff --git a/docs/cli/schema.json b/docs/cli/schema.json index 223af572..11b82501 100644 --- a/docs/cli/schema.json +++ b/docs/cli/schema.json @@ -15000,12 +15000,78 @@ ], "name": "stats", "parameters": [ + { + "role": "flag", + "name": "include-remotes", + "type": "boolean", + "required": false, + "summary": "Include remote cluster data into the response" + }, + { + "role": "flag", + "name": "timeout", + "type": "string", + "required": false, + "summary": "Period to wait for each node to respond.\nIf a node does not respond before its timeout expires, the response does not include its stats.\nHowever, timed out nodes are included in the response’s `_nodes.failed` property. Defaults to no timeout." + }, + { + "role": "flag", + "name": "node-id", + "type": "string", + "required": false, + "summary": "Comma-separated list of node filters used to limit returned information. Defaults to all nodes in the cluster.", + "repeatable": true, + "separator": "," + }, + { + "role": "flag", + "name": "error-trace", + "type": "boolean", + "required": false, + "summary": "When set to `true` Elasticsearch will include the full stack trace of errors\nwhen they occur." + }, + { + "role": "flag", + "name": "filter-path", + "type": "string", + "required": false, + "summary": "Comma-separated list of filters in dot notation which reduce the response\nreturned by Elasticsearch.", + "repeatable": true + }, + { + "role": "flag", + "name": "human", + "type": "boolean", + "required": false, + "summary": "When set to `true` will return statistics in a format suitable for humans.\nFor example `\"exists_time\": \"1h\"` for humans and\n`\"exists_time_in_millis\": 3600000` for computers. When disabled the human\nreadable values will be omitted. This makes sense for responses being consumed\nonly by machines." + }, + { + "role": "flag", + "name": "pretty", + "type": "boolean", + "required": false, + "summary": "If set to `true` the returned JSON will be \"pretty-formatted\". Only use\nthis option for debugging only." + }, + { + "role": "flag", + "name": "input-file", + "type": "string", + "required": false, + "summary": "path to a JSON file to use as command input" + }, { "role": "dryRun", "name": "dry-run", "type": "boolean", "required": false, "summary": "validate all inputs and exit without performing any action" + }, + { + "role": "flag", + "name": "no-validate", + "type": "boolean", + "required": false, + "summary": "skip input validation and send the request as-is" } ], "summary": "Get cluster statistics.", diff --git a/src/es/apis.ts b/src/es/apis.ts index dee673c9..31675c00 100644 --- a/src/es/apis.ts +++ b/src/es/apis.ts @@ -58,17 +58,40 @@ export async function loadEsApisInFile (namespaceFile: string): Promise { + return resolveDefinition(await attachMissingRequestSchema(namespaceFile, def)) + })) })() moduleCache.set(namespaceFile, cached) return cached } +/** + * The tool definition for `cluster.stats` ships without `input` because an + * optional path param is not on the selected URL. The request schema is still + * published next to it, and that schema has the query params (`filter_path`, + * `human`, `timeout`). + */ +async function attachMissingRequestSchema (namespaceFile: string, def: EsApiDefinition): Promise { + if (def.input != null) return def + try { + const input = await requireSchemaModule>( + `@elastic/schemas/es/json/${namespaceFile}.request.json`, + ) + if (input == null || typeof input !== 'object' || Array.isArray(input) || input['properties'] == null) return def + return { ...def, input } + } catch (err) { + const code = (err as NodeJS.ErrnoException).code + if (code === 'MODULE_NOT_FOUND' || code === 'ERR_MODULE_NOT_FOUND') return def + throw err + } +} + /** Locates a single `EsApiDefinition` by its manifest entry. */ export async function loadEsApi (meta: EsApiMeta): Promise { const defs = await loadEsApisInFile(meta.namespaceFile) const found = defs.find( - (d) => d.name === meta.name && (d.namespace ?? null) === meta.namespace + (d) => d.name === meta.name && (d.namespace ?? null) === (meta.namespace ?? null) ) if (found == null) { const label = meta.namespace != null ? `${meta.namespace} ${meta.name}` : meta.name diff --git a/test/es/apis.test.ts b/test/es/apis.test.ts index af284556..ed96bcdb 100644 --- a/test/es/apis.test.ts +++ b/test/es/apis.test.ts @@ -6,6 +6,7 @@ import { describe, it } from 'node:test' import assert from 'node:assert/strict' import { apiManifest, loadEsApi, loadEsApisInFile } from '../../src/es/apis.ts' +import { extractSchemaArgs } from '../../src/lib/json-schema-args.ts' describe('loadEsApisInFile', () => { it('returns the cached promise on a repeated call for the same namespace file', async () => { @@ -18,6 +19,16 @@ describe('loadEsApisInFile', () => { assert.equal(first, second) assert.ok(first.length > 0) }) + + it('uses the request schema when the tool definition has no input', async () => { + const defs = await loadEsApisInFile('cluster.stats') + const input = defs.find((d) => d.name === 'stats')?.input + assert.ok(input != null) + const flags = new Set(extractSchemaArgs(input).map((a) => a.cliFlag)) + for (const flag of ['filter-path', 'human', 'timeout']) { + assert.ok(flags.has(flag), flag) + } + }) }) describe('loadEsApi', () => {