Source: api/api/put_script.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. const { handleError, snakeCaseKeys, normalizeArguments, kConfigurationError } = require('../utils');
  32. const acceptedQuerystring = [
  33. 'cluster_manager_timeout',
  34. 'timeout',
  35. 'master_timeout',
  36. 'context',
  37. 'pretty',
  38. 'human',
  39. 'error_trace',
  40. 'source',
  41. 'filter_path',
  42. ];
  43. const snakeCase = {
  44. clusterManagerTimeout: 'cluster_manager_timeout',
  45. masterTimeout: 'master_timeout',
  46. errorTrace: 'error_trace',
  47. filterPath: 'filter_path',
  48. };
  49. /**
  50. * Creates or updates a script.
  51. * <br/> See also: {@link https://opensearch.org/docs/latest/api-reference/script-apis/create-stored-script/ OpenSearch - Create or update stored script}
  52. * @memberOf API-Script
  53. *
  54. * @param {Object} params
  55. * @param {string} params.id - Stored script or search template name
  56. * @param {string} params.body - The script
  57. * @param {string} [params.context] - Context in which the script or search template is to run. To prevent errors, the API immediately compiles the script or template in this context.
  58. * @param {string} [params.timeout=30s] - Explicit operation timeout
  59. * @param {string} [params.cluster_manager_timeout] - Specify timeout for connection to cluster_manager
  60. *
  61. * @param {Object} [options] - Options for {@link Transport#request}
  62. * @param {function} [callback] - Callback that handles errors and response
  63. *
  64. * @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*}
  65. */
  66. function putScriptApi(params, options, callback) {
  67. [params, options, callback] = normalizeArguments(params, options, callback);
  68. // check required parameters
  69. if (params.id == null) {
  70. const err = new this[kConfigurationError]('Missing required parameter: id');
  71. return handleError(err, callback);
  72. }
  73. if (params.body == null) {
  74. const err = new this[kConfigurationError]('Missing required parameter: body');
  75. return handleError(err, callback);
  76. }
  77. // check required url components
  78. if (params.context != null && params.id == null) {
  79. const err = new this[kConfigurationError]('Missing required parameter of the url: id');
  80. return handleError(err, callback);
  81. }
  82. let { method, body, id, context, ...querystring } = params;
  83. querystring = snakeCaseKeys(acceptedQuerystring, snakeCase, querystring);
  84. let path = '';
  85. if (id != null && context != null) {
  86. if (method == null) method = 'PUT';
  87. path = '/' + '_scripts' + '/' + encodeURIComponent(id) + '/' + encodeURIComponent(context);
  88. } else {
  89. if (method == null) method = 'PUT';
  90. path = '/' + '_scripts' + '/' + encodeURIComponent(id);
  91. }
  92. // build request object
  93. const request = {
  94. method,
  95. path,
  96. body: body || '',
  97. querystring,
  98. };
  99. return this.transport.request(request, options, callback);
  100. }
  101. module.exports = putScriptApi;