|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# |
| 3 | +# The OpenSearch Contributors require contributions made to |
| 4 | +# this file be licensed under the Apache-2.0 license or a |
| 5 | +# compatible open source license. |
| 6 | + |
| 7 | +# frozen_string_literal: true |
| 8 | + |
| 9 | +require_relative 'base_generator' |
| 10 | +require_relative 'action' |
| 11 | + |
| 12 | +# Generate an API Action via Mustache |
| 13 | +class ActionGenerator < BaseGenerator |
| 14 | + self.template_file = './templates/action.mustache' |
| 15 | + attr_reader :module_name, :method_name, :valid_params_constant_name, |
| 16 | + :method_description, :argument_descriptions, :external_docs |
| 17 | + |
| 18 | + # Actions that use perform_request_simple_ignore_404 |
| 19 | + SIMPLE_IGNORE_404 = %w[exists |
| 20 | + indices.exists |
| 21 | + indices.exists_alias |
| 22 | + indices.exists_template |
| 23 | + indices.exists_type].to_set.freeze |
| 24 | + |
| 25 | + # Actions that use perform_request_complex_ignore_404 |
| 26 | + COMPLEX_IGNORE_404 = %w[delete |
| 27 | + get |
| 28 | + indices.flush_synced |
| 29 | + indices.delete_template |
| 30 | + indices.delete |
| 31 | + security.get_role |
| 32 | + security.get_user |
| 33 | + snapshot.status |
| 34 | + snapshot.get |
| 35 | + snapshot.get_repository |
| 36 | + snapshot.delete_repository |
| 37 | + snapshot.delete |
| 38 | + update |
| 39 | + watcher.delete_watch].to_set.freeze |
| 40 | + |
| 41 | + # Actions that use perform_request_ping |
| 42 | + PING = %w[ping].to_set.freeze |
| 43 | + |
| 44 | + # @param [Pathname] output_folder |
| 45 | + # @param [Action] action |
| 46 | + def initialize(output_folder, action) |
| 47 | + super(output_folder) |
| 48 | + @action = action |
| 49 | + @urls = action.urls.map { |u| u.split('/').select(&:present?) }.uniq |
| 50 | + @external_docs = action.external_docs |
| 51 | + @module_name = action.namespace&.camelize |
| 52 | + @method_name = action.name.underscore |
| 53 | + @valid_params_constant_name = "#{action.name.upcase}_QUERY_PARAMS" |
| 54 | + @method_description = action.description |
| 55 | + @argument_descriptions = params_desc + [body_desc].compact |
| 56 | + end |
| 57 | + |
| 58 | + def url_components |
| 59 | + @urls.max_by(&:length) |
| 60 | + .map { |e| e.starts_with?('{') ? "_#{e[/{(.+)}/, 1]}" : "'#{e}'" } |
| 61 | + .join(', ') |
| 62 | + end |
| 63 | + |
| 64 | + def http_verb |
| 65 | + case @action.http_verbs.sort |
| 66 | + when %w[get post] |
| 67 | + 'body ? OpenSearch::API::HTTP_POST : OpenSearch::API::HTTP_GET' |
| 68 | + when %w[post put] |
| 69 | + diff_param = @urls.map(&:to_set).sort_by(&:size).reverse.reduce(&:difference).first |
| 70 | + "_#{diff_param[/{(.+)}/, 1]} ? OpenSearch::API::HTTP_PUT : OpenSearch::API::HTTP_POST" |
| 71 | + else |
| 72 | + "OpenSearch::API::HTTP_#{@action.http_verbs.first.upcase}" |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + def required_args |
| 77 | + @action.required_components.map { |arg| { arg: } } |
| 78 | + .tap { |args| args.last&.[]=('_blank_line', true) } |
| 79 | + end |
| 80 | + |
| 81 | + def path_params |
| 82 | + @action.path_params.map { |p| { name: p.name, listify: p.is_array } } |
| 83 | + .tap { |args| args.last&.[]=('_blank_line', true) } |
| 84 | + end |
| 85 | + |
| 86 | + def query_params |
| 87 | + @action.query_params.map { |p| { name: p.name } } |
| 88 | + end |
| 89 | + |
| 90 | + def listify_query_params |
| 91 | + @action.query_params.select(&:is_array).map { |p| { name: p.name } } |
| 92 | + .tap { |args| args.first&.[]=('_blank_line', true) } |
| 93 | + end |
| 94 | + |
| 95 | + def perform_request |
| 96 | + args = 'method, url, params, body, headers' |
| 97 | + return "perform_request_simple_ignore_404(#{args})" if SIMPLE_IGNORE_404.include?(@action.group) |
| 98 | + return "perform_request_complex_ignore_404(#{args}, arguments)" if COMPLEX_IGNORE_404.include?(@action.group) |
| 99 | + return "perform_request_ping(#{args})" if PING.include?(@action.group) |
| 100 | + "perform_request(#{args}).body" |
| 101 | + end |
| 102 | + |
| 103 | + private |
| 104 | + |
| 105 | + def output_file |
| 106 | + create_folder(*[@output_folder, @action.namespace].compact).join("#{@action.name}.rb") |
| 107 | + end |
| 108 | + |
| 109 | + def params_desc |
| 110 | + @action.parameters.map do |p| |
| 111 | + { data_type: p.ruby_type, |
| 112 | + name: p.name, |
| 113 | + required: p.required?, |
| 114 | + description: p.description, |
| 115 | + default: p.default, |
| 116 | + deprecated: p.deprecated? } |
| 117 | + end |
| 118 | + end |
| 119 | + |
| 120 | + def body_desc |
| 121 | + return unless @action.body.present? |
| 122 | + { data_type: :Hash, |
| 123 | + name: :body, |
| 124 | + description: @action.body_description, |
| 125 | + required: @action.body_required } |
| 126 | + end |
| 127 | +end |
0 commit comments