Source: api/api/tasks.js

  1. /*
  2. * Copyright OpenSearch Contributors
  3. * SPDX-License-Identifier: Apache-2.0
  4. *
  5. * The OpenSearch Contributors require contributions made to
  6. * this file be licensed under the Apache-2.0 license or a
  7. * compatible open source license.
  8. *
  9. */
  10. /*
  11. * Licensed to Elasticsearch B.V. under one or more contributor
  12. * license agreements. See the NOTICE file distributed with
  13. * this work for additional information regarding copyright
  14. * ownership. Elasticsearch B.V. licenses this file to you under
  15. * the Apache License, Version 2.0 (the "License"); you may
  16. * not use this file except in compliance with the License.
  17. * You may obtain a copy of the License at
  18. *
  19. * http://www.apache.org/licenses/LICENSE-2.0
  20. *
  21. * Unless required by applicable law or agreed to in writing,
  22. * software distributed under the License is distributed on an
  23. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  24. * KIND, either express or implied. See the License for the
  25. * specific language governing permissions and limitations
  26. * under the License.
  27. */
  28. 'use strict';
  29. /* eslint camelcase: 0 */
  30. /* eslint no-unused-vars: 0 */
  31. /** @namespace API-Tasks */
  32. const { handleError, snakeCaseKeys, normalizeArguments, kConfigurationError } = require('../utils');
  33. const acceptedQuerystring = [
  34. 'nodes',
  35. 'actions',
  36. 'parent_task_id',
  37. 'wait_for_completion',
  38. 'pretty',
  39. 'human',
  40. 'error_trace',
  41. 'source',
  42. 'filter_path',
  43. 'timeout',
  44. 'detailed',
  45. 'group_by',
  46. ];
  47. const snakeCase = {
  48. parentTaskId: 'parent_task_id',
  49. waitForCompletion: 'wait_for_completion',
  50. errorTrace: 'error_trace',
  51. filterPath: 'filter_path',
  52. groupBy: 'group_by',
  53. };
  54. function TasksApi(transport, ConfigurationError) {
  55. this.transport = transport;
  56. this[kConfigurationError] = ConfigurationError;
  57. }
  58. /**
  59. * Cancels a task, if it can be cancelled through an API.
  60. * <br/> See Also: {@link https://opensearch.org/docs/latest/api-reference/tasks/#task-canceling OpenSearch - Task Cancelling}
  61. *
  62. * @memberOf API-Tasks
  63. *
  64. * @param {Object} params
  65. * @param {string} [params.task_id] - Cancel the task with specified task id (node_id:task_number)
  66. * @param {string} [params.nodes] - A comma-separated list of node IDs or names to limit the returned information; use `_local` to return information from the node you're connecting to, leave empty to get information from all nodes
  67. * @param {string} [params.actions] - A comma-separated list of actions that should be cancelled. Leave empty to cancel all.
  68. * @param {string} [params.parent_task_id] - Cancel tasks with specified parent task id (node_id:task_number). Set to -1 to cancel all.
  69. * @param {boolean} [params.wait_for_completion] - Should the request block until the cancellation of the task and its descendant tasks is completed. Defaults to false
  70. *
  71. * @param {Object} options - Options for {@link Transport#request}
  72. * @param {function} callback - Callback that handles errors and response
  73. *
  74. * @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*}
  75. */
  76. TasksApi.prototype.cancel = function tasksCancelApi(params, options, callback) {
  77. [params, options, callback] = normalizeArguments(params, options, callback);
  78. let { method, body, taskId, task_id, ...querystring } = params;
  79. querystring = snakeCaseKeys(acceptedQuerystring, snakeCase, querystring);
  80. let path = '';
  81. if ((task_id || taskId) != null) {
  82. if (method == null) method = 'POST';
  83. path = '/' + '_tasks' + '/' + encodeURIComponent(task_id || taskId) + '/' + '_cancel';
  84. } else {
  85. if (method == null) method = 'POST';
  86. path = '/' + '_tasks' + '/' + '_cancel';
  87. }
  88. // build request object
  89. const request = {
  90. method,
  91. path,
  92. body: body || '',
  93. querystring,
  94. };
  95. return this.transport.request(request, options, callback);
  96. };
  97. /**
  98. * Returns information about a task.
  99. * <br/> See Also: {@link https://opensearch.org/docs/latest/api-reference/tasks OpenSearch - Tasks}
  100. *
  101. * @memberOf API-Tasks
  102. *
  103. * @param {Object} params
  104. * @param {string} [params.task_id] - Return the task with specified id (node_id:task_number)
  105. * @param {boolean} [params.wait_for_completion] - Wait for the matching tasks to complete (default: false)
  106. * @param {string} [params.timeout] - Explicit operation timeoutompletion] - Should the request block until the cancellation of the task and its descendant tasks is completed. Defaults to false
  107. *
  108. * @param {Object} options - Options for {@link Transport#request}
  109. * @param {function} callback - Callback that handles errors and response
  110. *
  111. * @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*}
  112. */
  113. TasksApi.prototype.get = function tasksGetApi(params, options, callback) {
  114. [params, options, callback] = normalizeArguments(params, options, callback);
  115. // check required parameters
  116. if (params.task_id == null && params.taskId == null) {
  117. const err = new this[kConfigurationError]('Missing required parameter: task_id or taskId');
  118. return handleError(err, callback);
  119. }
  120. let { method, body, taskId, task_id, ...querystring } = params;
  121. querystring = snakeCaseKeys(acceptedQuerystring, snakeCase, querystring);
  122. let path = '';
  123. if (method == null) method = 'GET';
  124. path = '/' + '_tasks' + '/' + encodeURIComponent(task_id || taskId);
  125. // build request object
  126. const request = {
  127. method,
  128. path,
  129. body: null,
  130. querystring,
  131. };
  132. return this.transport.request(request, options, callback);
  133. };
  134. /**
  135. * Returns a list of tasks.
  136. * <br/> See Also: {@link https://opensearch.org/docs/latest/api-reference/tasks OpenSearch - Tasks}
  137. *
  138. * @memberOf API-Tasks
  139. *
  140. * @param {Object} params
  141. * @param {string} [params.nodes] - A comma-separated list of node IDs or names to limit the returned information; use `_local` to return information from the node you're connecting to, leave empty to get information from all nodes
  142. * @param {string} [params.actions] - A comma-separated list of actions that should be returned. Leave empty to return all.
  143. * @param {boolean} [params.detailed] - Return detailed task information (default: false)
  144. * @param {string} [params.parent_task_id] - Return tasks with specified parent task id (node_id:task_number). Set to -1 to return all.
  145. * @param {boolean} [params.wait_for_completion] - Wait for the matching tasks to complete (default: false)
  146. * @param {string} [params.group_by] - Group tasks by nodes or parent/child relationships (options: nodes, parents, none)
  147. * @param {string} [params.timeout] - Explicit operation timeout
  148. *
  149. * @param {Object} options - Options for {@link Transport#request}
  150. * @param {function} callback - Callback that handles errors and response
  151. *
  152. * @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*}
  153. */
  154. TasksApi.prototype.list = function tasksListApi(params, options, callback) {
  155. [params, options, callback] = normalizeArguments(params, options, callback);
  156. let { method, body, ...querystring } = params;
  157. querystring = snakeCaseKeys(acceptedQuerystring, snakeCase, querystring);
  158. let path = '';
  159. if (method == null) method = 'GET';
  160. path = '/' + '_tasks';
  161. // build request object
  162. const request = {
  163. method,
  164. path,
  165. body: null,
  166. querystring,
  167. };
  168. return this.transport.request(request, options, callback);
  169. };
  170. module.exports = TasksApi;