Files
elasticsearch-js/api/api/indices.rollover.js
2019-02-12 16:39:49 +01:00

145 lines
4.3 KiB
JavaScript

'use strict'
/* eslint camelcase: 0 */
/* eslint no-unused-vars: 0 */
function buildIndicesRollover (opts) {
// eslint-disable-next-line no-unused-vars
const { makeRequest, ConfigurationError, result } = opts
/**
* Perform a [indices.rollover](http://www.elastic.co/guide/en/elasticsearch/reference/master/indices-rollover-index.html) request
*
* @param {string} alias - The name of the alias to rollover
* @param {string} new_index - The name of the rollover index
* @param {time} timeout - Explicit operation timeout
* @param {boolean} dry_run - If set to true the rollover action will only be validated but not actually performed even if a condition matches. The default is false
* @param {time} master_timeout - Specify timeout for connection to master
* @param {string} wait_for_active_shards - Set the number of active shards to wait for on the newly created rollover index before the operation returns.
* @param {object} body - The conditions that needs to be met for executing rollover
*/
const acceptedQuerystring = [
'timeout',
'dry_run',
'master_timeout',
'wait_for_active_shards',
'pretty',
'human',
'error_trace',
'source',
'filter_path'
]
const snakeCase = {
dryRun: 'dry_run',
masterTimeout: 'master_timeout',
waitForActiveShards: 'wait_for_active_shards',
errorTrace: 'error_trace',
filterPath: 'filter_path'
}
return function indicesRollover (params, options, callback) {
options = options || {}
if (typeof options === 'function') {
callback = options
options = {}
}
if (typeof params === 'function' || params == null) {
callback = params
params = {}
options = {}
}
// promises support
if (callback == null) {
return new Promise((resolve, reject) => {
indicesRollover(params, options, (err, body) => {
err ? reject(err) : resolve(body)
})
})
}
// check required parameters
if (params['alias'] == null) {
return callback(
new ConfigurationError('Missing required parameter: alias'),
result
)
}
// check required url components
if ((params['new_index'] != null || params['newIndex'] != null) && (params['alias'] == null)) {
return callback(
new ConfigurationError('Missing required parameter of the url: alias'),
result
)
}
// validate headers object
if (options.headers != null && typeof options.headers !== 'object') {
return callback(
new ConfigurationError(`Headers should be an object, instead got: ${typeof options.headers}`),
result
)
}
var warnings = null
var { method, body, alias, newIndex, new_index } = params
var querystring = semicopy(params, ['method', 'body', 'alias', 'newIndex', 'new_index'])
if (method == null) {
method = 'POST'
}
var ignore = options.ignore || null
if (typeof ignore === 'number') {
ignore = [ignore]
}
var path = ''
if ((alias) != null && (new_index || newIndex) != null) {
path = '/' + encodeURIComponent(alias) + '/' + '_rollover' + '/' + encodeURIComponent(new_index || newIndex)
} else {
path = '/' + encodeURIComponent(alias) + '/' + '_rollover'
}
// build request object
const request = {
method,
path,
body: body || '',
querystring
}
const requestOptions = {
ignore,
requestTimeout: options.requestTimeout || null,
maxRetries: options.maxRetries || null,
asStream: options.asStream || false,
headers: options.headers || null,
compression: options.compression || false,
warnings
}
return makeRequest(request, requestOptions, callback)
function semicopy (obj, exclude) {
var target = {}
var keys = Object.keys(obj)
for (var i = 0, len = keys.length; i < len; i++) {
var key = keys[i]
if (exclude.indexOf(key) === -1) {
target[snakeCase[key] || key] = obj[key]
if (acceptedQuerystring.indexOf(snakeCase[key] || key) === -1) {
warnings = warnings || []
warnings.push('Client - Unknown parameter: "' + key + '", sending it as query parameter')
}
}
}
return target
}
}
}
module.exports = buildIndicesRollover