From b36a6590e83d43b3366e45076db2dcc058d52ea3 Mon Sep 17 00:00:00 2001 From: Spencer Alger Date: Thu, 16 Jan 2014 15:43:33 -0700 Subject: [PATCH] Stopped overriding the ping method provided by the 1.0 API, but it didn't match the "castExists" regex so... Fixed the lack of castExists for ping/1.0 by removing castExists all together from the API. It was the case anyway that all HEAD requests needed to be cast, so now the clientAction module will set that param when the spec is set to make HEAD requests. The transport.request still expects that parameter. Switched the apiVersion implementation to use funcEnum, and exposed the options on the Client constructor. Docs to come. --- scripts/generate/js_api.js | 15 +--- src/elasticsearch | 2 +- src/lib/api.js | 119 ++++++++++++++++++++++++++++++-- src/lib/api_0_90.js | 4 -- src/lib/client.js | 37 +++++----- src/lib/client_action.js | 9 ++- test/unit/test_client_action.js | 4 +- 7 files changed, 143 insertions(+), 47 deletions(-) diff --git a/scripts/generate/js_api.js b/scripts/generate/js_api.js index 0a4b805e6..7e9bdc1d2 100644 --- a/scripts/generate/js_api.js +++ b/scripts/generate/js_api.js @@ -9,7 +9,6 @@ module.exports = function (branch, done) { var chalk = require('chalk'); var path = require('path'); var templates = require('./templates'); - var castExistsRE = /exists/; var usesBulkBodyRE = /^(bulk|msearch)$/; var urlParamRE = /\{(\w+)\}/g; @@ -167,10 +166,6 @@ module.exports = function (branch, done) { spec.bulkBody = true; } - if (castExistsRE.test(name)) { - spec.castExists = true; - } - var urls = _.difference(def.url.paths, aliases[name]); var urlSignatures = []; urls = _.map(urls, function (url) { @@ -240,9 +235,7 @@ module.exports = function (branch, done) { 'url', 'urls', 'needBody', - 'bulkBody', - 'castExists', - 'castNotFound' + 'bulkBody' ]), location: location, docUrl: def.documentation, @@ -283,11 +276,7 @@ module.exports = function (branch, done) { method = 'POST'; } else if (methodsAre('GET', 'HEAD')) { - if (action.spec.castExists) { - method = 'HEAD'; - } else { - method = 'GET'; - } + method = 'GET'; } } diff --git a/src/elasticsearch b/src/elasticsearch index 258c49ea2..612044b38 160000 --- a/src/elasticsearch +++ b/src/elasticsearch @@ -1 +1 @@ -Subproject commit 258c49ea235e1d30f9679e5921c7bee68a5d6f77 +Subproject commit 612044b3863451421acd8de717d0d083bae65670 diff --git a/src/lib/api.js b/src/lib/api.js index e2bfac718..c5856cb02 100644 --- a/src/lib/api.js +++ b/src/lib/api.js @@ -184,6 +184,53 @@ api.cat.prototype.allocation = ca({ ] }); +/** + * Perform a [cat.count](http://www.elasticsearch.org/guide/en/elasticsearch/reference/master/cat-count.html) request + * + * @param {Object} params - An object with parameters used to carry out this action + * @param {Boolean} params.local - Return local information, do not retrieve the state from master node (default: false) + * @param {Date, Number} params.masterTimeout - Explicit operation timeout for connection to master node + * @param {String, String[], Boolean} params.h - Comma-separated list of column names to display + * @param {Boolean} params.help - Return help information + * @param {Boolean} params.v - Verbose mode. Display column headers + * @param {String, String[], Boolean} params.index - A comma-separated list of index names to limit the returned information + */ +api.cat.prototype.count = ca({ + params: { + local: { + type: 'boolean' + }, + masterTimeout: { + type: 'time', + name: 'master_timeout' + }, + h: { + type: 'list' + }, + help: { + type: 'boolean', + 'default': false + }, + v: { + type: 'boolean', + 'default': false + } + }, + urls: [ + { + fmt: '/_cat/count/<%=index%>', + req: { + index: { + type: 'list' + } + } + }, + { + fmt: '/_cat/count' + } + ] +}); + /** * Perform a [cat.health](http://www.elasticsearch.org/guide/en/elasticsearch/reference/master/cat-health.html) request * @@ -192,6 +239,7 @@ api.cat.prototype.allocation = ca({ * @param {Date, Number} params.masterTimeout - Explicit operation timeout for connection to master node * @param {String, String[], Boolean} params.h - Comma-separated list of column names to display * @param {Boolean} params.help - Return help information + * @param {Boolean} [params.ts=true] - Set to false to disable timestamping * @param {Boolean} params.v - Verbose mode. Display column headers */ api.cat.prototype.health = ca({ @@ -210,6 +258,10 @@ api.cat.prototype.health = ca({ type: 'boolean', 'default': false }, + ts: { + type: 'boolean', + 'default': true + }, v: { type: 'boolean', 'default': false @@ -247,6 +299,7 @@ api.cat.prototype.help = ca({ * @param {Date, Number} params.masterTimeout - Explicit operation timeout for connection to master node * @param {String, String[], Boolean} params.h - Comma-separated list of column names to display * @param {Boolean} params.help - Return help information + * @param {Boolean} params.pri - Set to true to return stats only for primary shards * @param {Boolean} params.v - Verbose mode. Display column headers * @param {String, String[], Boolean} params.index - A comma-separated list of index names to limit the returned information */ @@ -275,6 +328,10 @@ api.cat.prototype.indices = ca({ type: 'boolean', 'default': false }, + pri: { + type: 'boolean', + 'default': false + }, v: { type: 'boolean', 'default': false @@ -403,6 +460,63 @@ api.cat.prototype.pendingTasks = ca({ } }); +/** + * Perform a [cat.recovery](http://www.elasticsearch.org/guide/en/elasticsearch/reference/master/cat-recovery.html) request + * + * @param {Object} params - An object with parameters used to carry out this action + * @param {String} params.bytes - The unit in which to display byte values + * @param {Boolean} params.local - Return local information, do not retrieve the state from master node (default: false) + * @param {Date, Number} params.masterTimeout - Explicit operation timeout for connection to master node + * @param {String, String[], Boolean} params.h - Comma-separated list of column names to display + * @param {Boolean} params.help - Return help information + * @param {Boolean} params.v - Verbose mode. Display column headers + * @param {String, String[], Boolean} params.index - A comma-separated list of index names to limit the returned information + */ +api.cat.prototype.recovery = ca({ + params: { + bytes: { + type: 'enum', + options: [ + 'b', + 'k', + 'm', + 'g' + ] + }, + local: { + type: 'boolean' + }, + masterTimeout: { + type: 'time', + name: 'master_timeout' + }, + h: { + type: 'list' + }, + help: { + type: 'boolean', + 'default': false + }, + v: { + type: 'boolean', + 'default': false + } + }, + urls: [ + { + fmt: '/_cat/recovery/<%=index%>', + req: { + index: { + type: 'list' + } + } + }, + { + fmt: '/_cat/recovery' + } + ] +}); + /** * Perform a [cat.shards](http://www.elasticsearch.org/guide/en/elasticsearch/reference/master/cat-shards.html) request * @@ -1607,7 +1721,6 @@ api.exists = ca({ }, sortOrder: -3 }, - castExists: true, method: 'HEAD' }); @@ -2415,7 +2528,6 @@ api.indices.prototype.exists = ca({ }, sortOrder: -1 }, - castExists: true, method: 'HEAD' }); @@ -2481,7 +2593,6 @@ api.indices.prototype.existsAlias = ca({ } } ], - castExists: true, method: 'HEAD' }); @@ -2501,7 +2612,6 @@ api.indices.prototype.existsTemplate = ca({ }, sortOrder: -1 }, - castExists: true, method: 'HEAD' }); @@ -2547,7 +2657,6 @@ api.indices.prototype.existsType = ca({ }, sortOrder: -2 }, - castExists: true, method: 'HEAD' }); diff --git a/src/lib/api_0_90.js b/src/lib/api_0_90.js index 0fd470ac2..bf15edcd7 100644 --- a/src/lib/api_0_90.js +++ b/src/lib/api_0_90.js @@ -775,7 +775,6 @@ api.exists = ca({ }, sortOrder: -3 }, - castExists: true, method: 'HEAD' }); @@ -1512,7 +1511,6 @@ api.indices.prototype.exists = ca({ }, sortOrder: -1 }, - castExists: true, method: 'HEAD' }); @@ -1557,7 +1555,6 @@ api.indices.prototype.existsAlias = ca({ } } ], - castExists: true, method: 'HEAD' }); @@ -1593,7 +1590,6 @@ api.indices.prototype.existsType = ca({ }, sortOrder: -2 }, - castExists: true, method: 'HEAD' }); diff --git a/src/lib/client.js b/src/lib/client.js index 6a0903360..6aec73220 100755 --- a/src/lib/client.js +++ b/src/lib/client.js @@ -29,11 +29,6 @@ module.exports = Client; var Transport = require('./transport'); var ca = require('./client_action'); var _ = require('./utils'); -var defaultApi = 'master'; -var apis = { - '0.90': require('./api_0_90'), - 'master': require('./api') -}; function Client(config) { config = config || {}; @@ -48,14 +43,17 @@ function Client(config) { config.host = 'http://localhost:9200'; } - this.ping = ca({ - method: 'HEAD', - url: { - fmt: '/' - }, - castExists: true, - requestTimeout: 100 - }); + if (!this.ping) { + // 0.90 api does not include ping + this.ping = ca({ + method: 'HEAD', + url: { + fmt: '/' + }, + castExists: true, + requestTimeout: 100 + }); + } this.close = function () { this.transport.close(); @@ -71,12 +69,11 @@ function Client(config) { delete this._namespaces; } - var apiVersion = config.apiVersion || defaultApi; - if (apis.hasOwnProperty(apiVersion)) { - EsApiClient.prototype = apis[apiVersion]; - } else { - throw new Error('Invalid apiVersion "' + apiVersion + '", expected one of ' + _.keys(apis).join(', ')); - } - + EsApiClient.prototype = _.funcEnum(config, 'apiVersion', Client.apis, 'master'); return new EsApiClient(config); } + +Client.apis = { + 'master': require('./api'), + '0.90': require('./api_0_90') +}; \ No newline at end of file diff --git a/src/lib/client_action.js b/src/lib/client_action.js index 85fb0cae9..f3e85b3f6 100644 --- a/src/lib/client_action.js +++ b/src/lib/client_action.js @@ -196,8 +196,13 @@ function exec(transport, spec, params, cb) { } // control params - spec.bulkBody && (request.bulkBody = true); - spec.castExists && (request.castExists = true); + if (spec.bulkBody) { + request.bulkBody = true; + } + + if (spec.method === 'HEAD') { + request.castExists = true; + } // pick the url if (spec.url) { diff --git a/test/unit/test_client_action.js b/test/unit/test_client_action.js index 3dbe00727..97c9bb265 100644 --- a/test/unit/test_client_action.js +++ b/test/unit/test_client_action.js @@ -559,9 +559,9 @@ describe('Client Action runner', function () { }); }); - it('passes castExists', function (done) { + it('sets castExists when the method in the spec is HEAD', function (done) { var action = makeClientAction({ - castExists: true + method: 'HEAD' }); action({}, function (err, params) {