elasticsearch-api 7.4.0 → 7.5.0.pre.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (185) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +16 -23
  3. data/lib/elasticsearch/api/actions/abort_benchmark.rb +1 -2
  4. data/lib/elasticsearch/api/actions/benchmark.rb +2 -3
  5. data/lib/elasticsearch/api/actions/bulk.rb +35 -68
  6. data/lib/elasticsearch/api/actions/cat/aliases.rb +30 -52
  7. data/lib/elasticsearch/api/actions/cat/allocation.rb +36 -50
  8. data/lib/elasticsearch/api/actions/cat/count.rb +25 -46
  9. data/lib/elasticsearch/api/actions/cat/fielddata.rb +31 -33
  10. data/lib/elasticsearch/api/actions/cat/health.rb +27 -37
  11. data/lib/elasticsearch/api/actions/cat/help.rb +14 -10
  12. data/lib/elasticsearch/api/actions/cat/indices.rb +45 -62
  13. data/lib/elasticsearch/api/actions/cat/master.rb +24 -34
  14. data/lib/elasticsearch/api/actions/cat/nodeattrs.rb +20 -16
  15. data/lib/elasticsearch/api/actions/cat/nodes.rb +34 -46
  16. data/lib/elasticsearch/api/actions/cat/params_registry.rb +0 -1
  17. data/lib/elasticsearch/api/actions/cat/pending_tasks.rb +29 -35
  18. data/lib/elasticsearch/api/actions/cat/plugins.rb +21 -18
  19. data/lib/elasticsearch/api/actions/cat/recovery.rb +40 -56
  20. data/lib/elasticsearch/api/actions/cat/repositories.rb +21 -24
  21. data/lib/elasticsearch/api/actions/cat/segments.rb +29 -22
  22. data/lib/elasticsearch/api/actions/cat/shards.rb +38 -59
  23. data/lib/elasticsearch/api/actions/cat/snapshots.rb +32 -27
  24. data/lib/elasticsearch/api/actions/cat/tasks.rb +25 -19
  25. data/lib/elasticsearch/api/actions/cat/templates.rb +26 -18
  26. data/lib/elasticsearch/api/actions/cat/thread_pool.rb +36 -46
  27. data/lib/elasticsearch/api/actions/clear_scroll.rb +21 -8
  28. data/lib/elasticsearch/api/actions/cluster/allocation_explain.rb +15 -13
  29. data/lib/elasticsearch/api/actions/cluster/get_settings.rb +18 -18
  30. data/lib/elasticsearch/api/actions/cluster/health.rb +42 -44
  31. data/lib/elasticsearch/api/actions/cluster/params_registry.rb +0 -1
  32. data/lib/elasticsearch/api/actions/cluster/pending_tasks.rb +15 -17
  33. data/lib/elasticsearch/api/actions/cluster/put_settings.rb +20 -17
  34. data/lib/elasticsearch/api/actions/cluster/remote_info.rb +8 -6
  35. data/lib/elasticsearch/api/actions/cluster/reroute.rb +25 -37
  36. data/lib/elasticsearch/api/actions/cluster/state.rb +37 -41
  37. data/lib/elasticsearch/api/actions/cluster/stats.rb +20 -12
  38. data/lib/elasticsearch/api/actions/count.rb +52 -39
  39. data/lib/elasticsearch/api/actions/create.rb +25 -26
  40. data/lib/elasticsearch/api/actions/delete.rb +43 -36
  41. data/lib/elasticsearch/api/actions/delete_by_query.rb +74 -62
  42. data/lib/elasticsearch/api/actions/delete_by_query_rethrottle.rb +39 -0
  43. data/lib/elasticsearch/api/actions/delete_by_rethrottle.rb +4 -3
  44. data/lib/elasticsearch/api/actions/delete_script.rb +19 -13
  45. data/lib/elasticsearch/api/actions/exists.rb +44 -34
  46. data/lib/elasticsearch/api/actions/exists_source.rb +43 -25
  47. data/lib/elasticsearch/api/actions/explain.rb +46 -51
  48. data/lib/elasticsearch/api/actions/field_caps.rb +27 -20
  49. data/lib/elasticsearch/api/actions/get.rb +42 -41
  50. data/lib/elasticsearch/api/actions/get_script.rb +19 -14
  51. data/lib/elasticsearch/api/actions/get_source.rb +38 -44
  52. data/lib/elasticsearch/api/actions/index.rb +60 -89
  53. data/lib/elasticsearch/api/actions/indices/analyze.rb +19 -48
  54. data/lib/elasticsearch/api/actions/indices/clear_cache.rb +29 -41
  55. data/lib/elasticsearch/api/actions/indices/clone.rb +21 -21
  56. data/lib/elasticsearch/api/actions/indices/close.rb +28 -32
  57. data/lib/elasticsearch/api/actions/indices/create.rb +22 -70
  58. data/lib/elasticsearch/api/actions/indices/delete.rb +23 -35
  59. data/lib/elasticsearch/api/actions/indices/delete_alias.rb +24 -20
  60. data/lib/elasticsearch/api/actions/indices/delete_template.rb +18 -20
  61. data/lib/elasticsearch/api/actions/indices/exists.rb +32 -34
  62. data/lib/elasticsearch/api/actions/indices/exists_alias.rb +33 -29
  63. data/lib/elasticsearch/api/actions/indices/exists_template.rb +21 -16
  64. data/lib/elasticsearch/api/actions/indices/exists_type.rb +29 -29
  65. data/lib/elasticsearch/api/actions/indices/flush.rb +28 -28
  66. data/lib/elasticsearch/api/actions/indices/flush_synced.rb +23 -12
  67. data/lib/elasticsearch/api/actions/indices/forcemerge.rb +26 -41
  68. data/lib/elasticsearch/api/actions/indices/freeze.rb +7 -8
  69. data/lib/elasticsearch/api/actions/indices/get.rb +25 -19
  70. data/lib/elasticsearch/api/actions/indices/get_alias.rb +31 -23
  71. data/lib/elasticsearch/api/actions/indices/get_field_mapping.rb +42 -38
  72. data/lib/elasticsearch/api/actions/indices/get_mapping.rb +39 -31
  73. data/lib/elasticsearch/api/actions/indices/get_settings.rb +34 -37
  74. data/lib/elasticsearch/api/actions/indices/get_template.rb +21 -23
  75. data/lib/elasticsearch/api/actions/indices/get_upgrade.rb +48 -0
  76. data/lib/elasticsearch/api/actions/indices/open.rb +24 -21
  77. data/lib/elasticsearch/api/actions/indices/params_registry.rb +0 -1
  78. data/lib/elasticsearch/api/actions/indices/put_alias.rb +25 -27
  79. data/lib/elasticsearch/api/actions/indices/put_mapping.rb +40 -39
  80. data/lib/elasticsearch/api/actions/indices/put_settings.rb +30 -37
  81. data/lib/elasticsearch/api/actions/indices/put_template.rb +23 -23
  82. data/lib/elasticsearch/api/actions/indices/recovery.rb +20 -23
  83. data/lib/elasticsearch/api/actions/indices/refresh.rb +22 -26
  84. data/lib/elasticsearch/api/actions/indices/rollover.rb +28 -19
  85. data/lib/elasticsearch/api/actions/indices/segments.rb +24 -17
  86. data/lib/elasticsearch/api/actions/indices/shard_stores.rb +27 -15
  87. data/lib/elasticsearch/api/actions/indices/shrink.rb +23 -17
  88. data/lib/elasticsearch/api/actions/indices/split.rb +23 -17
  89. data/lib/elasticsearch/api/actions/indices/stats.rb +53 -104
  90. data/lib/elasticsearch/api/actions/indices/unfreeze.rb +7 -7
  91. data/lib/elasticsearch/api/actions/indices/update_aliases.rb +15 -32
  92. data/lib/elasticsearch/api/actions/indices/upgrade.rb +26 -16
  93. data/lib/elasticsearch/api/actions/indices/validate_query.rb +46 -59
  94. data/lib/elasticsearch/api/actions/info.rb +9 -6
  95. data/lib/elasticsearch/api/actions/ingest/delete_pipeline.rb +18 -12
  96. data/lib/elasticsearch/api/actions/ingest/get_pipeline.rb +21 -11
  97. data/lib/elasticsearch/api/actions/ingest/params_registry.rb +0 -1
  98. data/lib/elasticsearch/api/actions/ingest/processor_grok.rb +12 -13
  99. data/lib/elasticsearch/api/actions/ingest/put_pipeline.rb +19 -14
  100. data/lib/elasticsearch/api/actions/ingest/simulate.rb +23 -13
  101. data/lib/elasticsearch/api/actions/mget.rb +37 -44
  102. data/lib/elasticsearch/api/actions/msearch.rb +54 -54
  103. data/lib/elasticsearch/api/actions/msearch_template.rb +46 -35
  104. data/lib/elasticsearch/api/actions/mtermvectors.rb +39 -35
  105. data/lib/elasticsearch/api/actions/nodes/hot_threads.rb +30 -25
  106. data/lib/elasticsearch/api/actions/nodes/info.rb +29 -63
  107. data/lib/elasticsearch/api/actions/nodes/params_registry.rb +0 -1
  108. data/lib/elasticsearch/api/actions/nodes/reload_secure_settings.rb +24 -22
  109. data/lib/elasticsearch/api/actions/nodes/shutdown.rb +4 -4
  110. data/lib/elasticsearch/api/actions/nodes/stats.rb +43 -36
  111. data/lib/elasticsearch/api/actions/nodes/usage.rb +31 -10
  112. data/lib/elasticsearch/api/actions/params_registry.rb +0 -1
  113. data/lib/elasticsearch/api/actions/ping.rb +12 -13
  114. data/lib/elasticsearch/api/actions/put_script.rb +26 -30
  115. data/lib/elasticsearch/api/actions/rank_eval.rb +24 -15
  116. data/lib/elasticsearch/api/actions/reindex.rb +25 -48
  117. data/lib/elasticsearch/api/actions/reindex_rethrottle.rb +18 -12
  118. data/lib/elasticsearch/api/actions/remote/info.rb +1 -2
  119. data/lib/elasticsearch/api/actions/render_search_template.rb +17 -15
  120. data/lib/elasticsearch/api/actions/scripts_painless_execute.rb +8 -6
  121. data/lib/elasticsearch/api/actions/scroll.rb +28 -51
  122. data/lib/elasticsearch/api/actions/search.rb +104 -165
  123. data/lib/elasticsearch/api/actions/search_shards.rb +31 -28
  124. data/lib/elasticsearch/api/actions/search_template.rb +48 -40
  125. data/lib/elasticsearch/api/actions/snapshot/cleanup_repository.rb +44 -0
  126. data/lib/elasticsearch/api/actions/snapshot/create.rb +22 -29
  127. data/lib/elasticsearch/api/actions/snapshot/create_repository.rb +19 -24
  128. data/lib/elasticsearch/api/actions/snapshot/delete.rb +20 -22
  129. data/lib/elasticsearch/api/actions/snapshot/delete_repository.rb +16 -18
  130. data/lib/elasticsearch/api/actions/snapshot/get.rb +22 -31
  131. data/lib/elasticsearch/api/actions/snapshot/get_repository.rb +21 -25
  132. data/lib/elasticsearch/api/actions/snapshot/params_registry.rb +0 -1
  133. data/lib/elasticsearch/api/actions/snapshot/restore.rb +21 -32
  134. data/lib/elasticsearch/api/actions/snapshot/status.rb +23 -24
  135. data/lib/elasticsearch/api/actions/snapshot/verify_repository.rb +18 -13
  136. data/lib/elasticsearch/api/actions/tasks/cancel.rb +19 -13
  137. data/lib/elasticsearch/api/actions/tasks/get.rb +16 -13
  138. data/lib/elasticsearch/api/actions/tasks/list.rb +20 -18
  139. data/lib/elasticsearch/api/actions/tasks/params_registry.rb +0 -1
  140. data/lib/elasticsearch/api/actions/termvectors.rb +52 -70
  141. data/lib/elasticsearch/api/actions/update.rb +53 -98
  142. data/lib/elasticsearch/api/actions/update_by_query.rb +77 -66
  143. data/lib/elasticsearch/api/actions/update_by_query_rethrottle.rb +17 -11
  144. data/lib/elasticsearch/api/version.rb +1 -1
  145. data/spec/elasticsearch/api/actions/cat/segments_spec.rb +9 -11
  146. data/spec/elasticsearch/api/actions/cluster/put_settings_spec.rb +1 -1
  147. data/spec/elasticsearch/api/actions/count_spec.rb +8 -8
  148. data/spec/elasticsearch/api/actions/delete_by_query_spec.rb +12 -7
  149. data/spec/elasticsearch/api/actions/delete_script_spec.rb +0 -14
  150. data/spec/elasticsearch/api/actions/exists_document_spec.rb +1 -1
  151. data/spec/elasticsearch/api/actions/get_document_source_spec.rb +1 -1
  152. data/spec/elasticsearch/api/actions/index_document_spec.rb +6 -15
  153. data/spec/elasticsearch/api/actions/indices/analyze_spec.rb +0 -4
  154. data/spec/elasticsearch/api/actions/indices/clear_cache_spec.rb +1 -5
  155. data/spec/elasticsearch/api/actions/indices/clone_spec.rb +1 -1
  156. data/spec/elasticsearch/api/actions/indices/delete_alias_spec.rb +2 -2
  157. data/spec/elasticsearch/api/actions/indices/get_spec.rb +0 -11
  158. data/spec/elasticsearch/api/actions/indices/put_alias_spec.rb +3 -3
  159. data/spec/elasticsearch/api/actions/indices/put_mapping_spec.rb +3 -3
  160. data/spec/elasticsearch/api/actions/indices/stats_spec.rb +0 -1
  161. data/spec/elasticsearch/api/actions/nodes/hot_threads_spec.rb +3 -3
  162. data/spec/elasticsearch/api/actions/put_script_spec.rb +20 -15
  163. data/spec/elasticsearch/api/actions/render_search_template_spec.rb +23 -7
  164. data/spec/elasticsearch/api/actions/scroll_spec.rb +37 -0
  165. data/spec/elasticsearch/api/actions/search_spec.rb +0 -15
  166. data/spec/elasticsearch/api/actions/tasks/list_spec.rb +0 -11
  167. data/spec/elasticsearch/api/actions/update_document_spec.rb +3 -7
  168. data/spec/rest_yaml_tests_helper.rb +12 -0
  169. data/utils/Gemfile +1 -0
  170. data/utils/Thorfile +0 -1
  171. data/utils/thor/generate_source.rb +192 -85
  172. data/utils/thor/generator/endpoint_specifics.rb +157 -0
  173. data/utils/thor/generator/files_helper.rb +37 -0
  174. data/utils/thor/lister.rb +3 -4
  175. data/utils/thor/templates/_documentation_top.erb +27 -0
  176. data/utils/thor/templates/_method_setup.erb +35 -0
  177. data/utils/thor/templates/_params_registry.erb +12 -0
  178. data/utils/thor/templates/_perform_request.erb +37 -0
  179. data/utils/thor/templates/method.erb +59 -0
  180. data/utils/thor/templates/{ruby/test.erb → test.erb} +0 -0
  181. data/utils/thor/templates/{ruby/test_helper.rb → test_helper.rb} +0 -0
  182. metadata +19 -11
  183. data/spec/elasticsearch/api/actions/scoll_spec.rb +0 -21
  184. data/utils/thor/generate_api.rb +0 -193
  185. data/utils/thor/templates/ruby/method.erb +0 -62
@@ -5,28 +5,33 @@
5
5
  require 'spec_helper'
6
6
 
7
7
  describe 'client#put_script' do
8
+ let(:url) do
9
+ '_scripts/foo'
10
+ end
8
11
 
9
- let(:expected_args) do
10
- [
12
+ context 'when the lang parameter is provided' do
13
+ let(:expected_args) do
14
+ [
11
15
  'PUT',
12
16
  url,
13
- { },
14
- { script: 'bar' }
15
- ]
16
- end
17
-
18
- let(:url) do
19
- '_scripts/groovy/foo'
20
- end
17
+ {},
18
+ { script: 'bar', lang: 'groovy' }
19
+ ]
20
+ end
21
21
 
22
- it 'performs the request' do
23
- expect(client_double.put_script(lang: 'groovy', id: 'foo', body: { script: 'bar' })).to eq({})
22
+ it 'performs the request' do
23
+ expect(client_double.put_script(id: 'foo', body: { script: 'bar', lang: 'groovy' })).to eq({})
24
+ end
24
25
  end
25
26
 
26
27
  context 'when the lang parameter is not provided' do
27
-
28
- let(:url) do
29
- '_scripts/foo'
28
+ let(:expected_args) do
29
+ [
30
+ 'PUT',
31
+ url,
32
+ {},
33
+ { script: 'bar' }
34
+ ]
30
35
  end
31
36
 
32
37
  it 'performs the request' do
@@ -5,17 +5,33 @@
5
5
  require 'spec_helper'
6
6
 
7
7
  describe 'client#render_search_template' do
8
-
9
- let(:expected_args) do
10
- [
8
+ context 'when no id is specified' do
9
+ let(:expected_args) do
10
+ [
11
11
  'GET',
12
12
  '_render/template',
13
- { id: 'foo' },
13
+ {},
14
14
  { foo: 'bar' }
15
- ]
15
+ ]
16
+ end
17
+
18
+ it 'performs the request' do
19
+ expect(client_double.render_search_template(body: { foo: 'bar' })).to eq({})
20
+ end
16
21
  end
17
22
 
18
- it 'performs the request' do
19
- expect(client_double.render_search_template(id: 'foo', body: { foo: 'bar' })).to eq({})
23
+ context 'when id is specified' do
24
+ let(:expected_args) do
25
+ [
26
+ 'GET',
27
+ '_render/template/foo',
28
+ {},
29
+ { foo: 'bar' }
30
+ ]
31
+ end
32
+
33
+ it 'performs the request' do
34
+ expect(client_double.render_search_template(id: 'foo', body: { foo: 'bar' })).to eq({})
35
+ end
20
36
  end
21
37
  end
@@ -0,0 +1,37 @@
1
+ # Licensed to Elasticsearch B.V under one or more agreements.
2
+ # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3
+ # See the LICENSE file in the project root for more information
4
+
5
+ require 'spec_helper'
6
+
7
+ describe 'client#scroll' do
8
+ context 'with scroll_id as a param' do
9
+ let(:expected_args) do
10
+ [
11
+ 'GET',
12
+ '_search/scroll/cXVlcn...',
13
+ {},
14
+ nil
15
+ ]
16
+ end
17
+
18
+ it 'performs the request' do
19
+ expect(client_double.scroll(scroll_id: 'cXVlcn...')).to eq({})
20
+ end
21
+ end
22
+
23
+ context 'with scroll_id in the body' do
24
+ let(:expected_args) do
25
+ [
26
+ 'GET',
27
+ '_search/scroll',
28
+ {},
29
+ { scroll_id: 'cXVlcn...' }
30
+ ]
31
+ end
32
+
33
+ it 'performs the request' do
34
+ expect(client_double.scroll(body: { scroll_id: 'cXVlcn...' })).to eq({})
35
+ end
36
+ end
37
+ end
@@ -117,19 +117,4 @@ describe 'client#search' do
117
117
  }.to raise_exception(ArgumentError)
118
118
  end
119
119
  end
120
-
121
- context 'when the request field params are not URL-encoded' do
122
-
123
- let(:params) do
124
- { fields: 'foo^bar'}
125
- end
126
-
127
- let(:url) do
128
- 'foo/bar/_search'
129
- end
130
-
131
- it 'does not encode the field params' do
132
- expect(client_double.search(index: 'foo', type: 'bar', fields: 'foo^bar'))
133
- end
134
- end
135
120
  end
@@ -23,15 +23,4 @@ describe 'client.tasks#list' do
23
23
  it 'performs the request' do
24
24
  expect(client_double.tasks.list).to eq({})
25
25
  end
26
-
27
- context 'when a task id is specified' do
28
-
29
- let(:url) do
30
- '_tasks/foo'
31
- end
32
-
33
- it 'performs the request' do
34
- expect(client_double.tasks.list(task_id: 'foo')).to eq({})
35
- end
36
- end
37
26
  end
@@ -53,16 +53,12 @@ describe 'client#update' do
53
53
  'foo/bar/1/_update'
54
54
  end
55
55
 
56
- let(:params) do
57
- { version: 100 }
58
- end
59
-
60
56
  let(:body) do
61
57
  {}
62
58
  end
63
59
 
64
60
  it 'performs the request' do
65
- expect(client_double.update(index: 'foo', type: 'bar', id: '1', version: 100, body: {}))
61
+ expect(client_double.update(index: 'foo', type: 'bar', id: '1', body: {}))
66
62
  end
67
63
  end
68
64
 
@@ -98,14 +94,14 @@ describe 'client#update' do
98
94
 
99
95
  it 'raises it to the user' do
100
96
  expect {
101
- client.update(index: 'foo', type: 'bar', id: 'XXX')
97
+ client.update(index: 'foo', type: 'bar', id: 'XXX', body: {})
102
98
  }.to raise_exception(NotFound)
103
99
  end
104
100
 
105
101
  context 'when the :ignore parameter is specified' do
106
102
 
107
103
  it 'does not raise the error to the user' do
108
- expect(client.update(index: 'foo', type: 'bar', id: 'XXX', ignore: 404)).to eq(false)
104
+ expect(client.update(index: 'foo', type: 'bar', id: 'XXX', body: {}, ignore: 404)).to eq(false)
109
105
  end
110
106
  end
111
107
  end
@@ -72,6 +72,18 @@ skipped_tests << { file: 'delete/70_mix_typeless_typeful.yml',
72
72
  skipped_tests << { file: 'cat.templates/10_basic.yml',
73
73
  description: '*' }
74
74
 
75
+ # node_selector is not supported yet
76
+ skipped_tests << { file: 'cat.aliases/10_basic.yml',
77
+ description: '*' }
78
+
79
+ # Responses are there but not equal (eg.: yellow status)
80
+ skipped_tests << { file: 'cluster.health/10_basic.yml',
81
+ description: 'cluster health with closed index (pre 7.2.0)' }
82
+
83
+ # Regular expression not catching exact match:
84
+ skipped_tests << { file: 'cat.indices/10_basic.yml',
85
+ description: 'Test cat indices output for closed index (pre 7.2.0)' }
86
+
75
87
  SKIPPED_TESTS = skipped_tests
76
88
 
77
89
  # The directory of rest api YAML files.
data/utils/Gemfile CHANGED
@@ -11,3 +11,4 @@ gem 'multi_json'
11
11
  gem 'thor'
12
12
  gem 'json'
13
13
  gem 'pry'
14
+ gem 'rubocop'
data/utils/Thorfile CHANGED
@@ -2,6 +2,5 @@
2
2
  # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3
3
  # See the LICENSE file in the project root for more information
4
4
 
5
- require File.expand_path('./thor/generate_api')
6
5
  require File.expand_path('./thor/generate_source')
7
6
  require File.expand_path('./thor/lister')
@@ -5,138 +5,245 @@
5
5
  # encoding: UTF-8
6
6
 
7
7
  require 'thor'
8
-
9
8
  require 'pathname'
10
9
  require 'active_support/core_ext/hash/deep_merge'
11
10
  require 'active_support/inflector'
12
11
  require 'multi_json'
13
12
  require 'coderay'
14
13
  require 'pry'
14
+ require_relative 'generator/files_helper'
15
+ require_relative 'generator/endpoint_specifics'
15
16
 
16
17
  module Elasticsearch
17
-
18
18
  module API
19
-
20
19
  # A command line application based on [Thor](https://github.com/wycats/thor),
21
20
  # which will read the JSON API spec file(s), and generate
22
21
  # the Ruby source code (one file per API endpoint) with correct
23
22
  # module namespace, method names, and RDoc documentation,
24
23
  # as well as test files for each endpoint.
25
24
  #
26
- # Currently it only generates Ruby source, but can easily be
27
- # extended and adapted to generate source code for other
28
- # programming languages.
25
+ # Specific exceptions and code snippets that need to be included are written
26
+ # in EndpointSpecifics (generator/endpoint_specifics) and the module is included
27
+ # here.
29
28
  #
30
29
  class SourceGenerator < Thor
31
30
  namespace 'api:code'
32
-
33
31
  include Thor::Actions
32
+ include EndpointSpecifics
34
33
 
35
- __root = Pathname( File.expand_path('../../..', __FILE__) )
36
-
37
- desc "generate", "Generate source code and tests from the REST API JSON specification"
38
- method_option :language, default: 'ruby', desc: 'Programming language'
39
- method_option :force, type: :boolean, default: false, desc: 'Overwrite the output'
40
- method_option :verbose, type: :boolean, default: false, desc: 'Output more information'
41
- method_option :input, default: File.expand_path('../../../../tmp/elasticsearch/rest-api-spec/api/**/*.json', __FILE__), desc: 'Path to directory with JSON API specs'
42
- method_option :output, default: File.expand_path('../../../tmp/out', __FILE__), desc: 'Path to output directory'
43
-
44
- def generate(*files)
45
- self.class.source_root File.expand_path('../', __FILE__)
46
-
47
- @input = Pathname(options[:input])
48
- @output = Pathname(options[:output])
34
+ __root = Pathname(File.expand_path('../../..', __FILE__))
49
35
 
50
- # -- Test helper
51
- copy_file "templates/ruby/test_helper.rb", @output.join('test').join('test_helper.rb') if options[:language] == 'ruby'
36
+ desc 'generate', 'Generate source code and tests from the REST API JSON specification'
37
+ method_option :verbose, type: :boolean, default: false, desc: 'Output more information'
38
+ method_option :tests, type: :boolean, default: false, desc: 'Generate test files'
39
+ method_option :xpack, type: :boolean, default: false, desc: 'Generate X-Pack'
52
40
 
53
- files = Dir.entries(@input.to_s).reject { |f| f.start_with?('.') || f.start_with?('_') }
41
+ def generate
42
+ self.class.source_root File.expand_path(__dir__)
43
+ @xpack = options[:xpack]
54
44
 
55
- files.each do |filepath|
56
- file = @input.join(filepath)
45
+ @input = FilesHelper.input_dir(@xpack)
46
+ @output = FilesHelper.output_dir(@xpack)
57
47
 
58
- @path = Pathname(file)
59
- @json = MultiJson.load( File.read(@path) )
60
- @spec = @json.values.first
48
+ FilesHelper.files(@xpack).each do |filepath|
49
+ @path = Pathname(@input.join(filepath))
50
+ @json = MultiJson.load(File.read(@path))
51
+ @spec = @json.values.first
61
52
  say_status 'json', @path, :yellow
62
53
 
63
54
  @spec['url'] ||= {}
64
- @spec['url']['parts'] ||= []
65
- @spec['url']['params'] ||= {}
66
55
 
67
- # say_status 'JSON', @spec.inspect, options[:verbose]
68
-
69
- @full_namespace = @json.keys.first.split('.')
70
- @namespace_depth = @full_namespace.size > 0 ? @full_namespace.size-1 : 0
56
+ @endpoint_name = @json.keys.first
57
+ @full_namespace = __full_namespace
58
+ @namespace_depth = @full_namespace.size > 0 ? @full_namespace.size - 1 : 0
71
59
  @module_namespace = @full_namespace[0, @namespace_depth]
72
60
  @method_name = @full_namespace.last
73
- @http_method = "Elasticsearch::API::HTTP_#{@spec['methods'].first}"
74
- @http_path = unless @spec['url']['parts'].empty?
75
- @spec['url']['path']
76
- .split('/')
77
- .compact
78
- .reject { |p| p =~ /^\s*$/ }
79
- .map { |p| p =~ /\{/ ? "\#\{arguments[:#{p.tr('{}', '')}]\}" : p }
80
- .join('/')
81
- .gsub(/^\//, '')
82
- else
83
- @spec['url']['path'].gsub(/^\//, '')
84
- end
85
-
86
- # -- Ruby files
87
-
88
- @path_to_file = @output.join('api').join( @module_namespace.join('/') ).join("#{@method_name}.rb")
89
-
90
- empty_directory @output.join('api').join( @module_namespace.join('/') )
91
-
92
- template "templates/#{options[:language]}/method.erb", @path_to_file
93
-
94
- if options[:verbose]
95
- colorized_output = CodeRay.scan_file(@path_to_file, :ruby).terminal
96
- lines = colorized_output.split("\n")
97
- say_status options[:language].downcase,
98
- lines.first + "\n" + lines[1, lines.size].map { |l| ' '*14 + l }.join("\n"),
99
- :yellow
100
- end
61
+ @parts = __endpoint_parts
62
+ @params = @spec['params'] || {}
63
+ @specific_params = specific_params(@module_namespace.first) # See EndpointSpecifics
64
+ @http_method = __http_method
65
+ @paths = @spec['url']['paths'].map { |b| b['path'] }
66
+ # Using Ruby's safe operator on array:
67
+ @deprecation_note = @spec['url']['paths'].last&.[]('deprecated')
68
+ @http_path = __http_path
69
+ @required_parts = __required_parts
101
70
 
102
- # --- Test files
71
+ @module_namespace.shift if @module_namespace.first == 'xpack'
72
+ @path_to_file = @output.join(@module_namespace.join('/')).join("#{@method_name}.rb")
73
+ dir = @output.join(@module_namespace.join('/'))
74
+ empty_directory(dir, verbose: false)
103
75
 
104
- @test_directory = @output.join('test/api').join( @module_namespace.join('/') )
105
- @test_file = @test_directory.join("#{@method_name}_test.rb")
76
+ # Write the file with the ERB template:
77
+ template('templates/method.erb', @path_to_file, force: true)
106
78
 
107
- empty_directory @test_directory
108
- template "templates/#{options[:language]}/test.erb", @test_file
79
+ print_source_code(@path_to_file) if options[:verbose]
109
80
 
110
- if options[:verbose]
111
- colorized_output = colorized_output = CodeRay.scan_file(@test_file, :ruby).terminal
112
- lines = colorized_output.split("\n")
113
- say_status options[:language].downcase,
114
- lines.first + "\n" + lines[1, lines.size].map { |l| ' '*14 + l }.join("\n"),
115
- :yellow
116
- say '▬'*terminal_width
117
- end
81
+ generate_tests if options[:tests]
82
+
83
+ puts
118
84
  end
119
85
 
120
- # -- Tree output
86
+ run_rubocop
121
87
 
122
- if options[:verbose] && `which tree > /dev/null 2>&1; echo $?`.to_i < 1
123
- lines = `tree #{@output}`.split("\n")
124
- say_status 'tree',
125
- lines.first + "\n" + lines[1, lines.size].map { |l| ' '*14 + l }.join("\n")
126
- end
88
+ # -- Tree output
89
+ print_tree if options[:verbose]
127
90
  end
128
91
 
129
92
  private
130
93
 
94
+ def __full_namespace
95
+ names = @endpoint_name.split('.')
96
+ if @xpack
97
+ names = (names.first == 'xpack' ? names : ['xpack', names].flatten)
98
+ # Return an array with 'ml' renamed to 'machine_learning' and 'ilm' to
99
+ # 'index_lifecycle_management'
100
+ names.map do |name|
101
+ name
102
+ .gsub(/^ml$/, 'machine_learning')
103
+ .gsub(/^ilm$/, 'index_lifecycle_management')
104
+ end
105
+ else
106
+ names
107
+ end
108
+ end
109
+
131
110
  # Create the hierarchy of directories based on API namespaces
132
111
  #
133
112
  def __create_directories(key, value)
134
- unless value['documentation']
135
- empty_directory @output.join(key)
136
- create_directory_hierarchy *value.to_a.first
113
+ return if value['documentation']
114
+
115
+ empty_directory @output.join(key)
116
+ create_directory_hierarchy * value.to_a.first
117
+ end
118
+
119
+ # Extract parts from each path
120
+ #
121
+ def __endpoint_parts
122
+ parts = @spec['url']['paths'].select do |a|
123
+ a.keys.include?('parts')
124
+ end.map do |path|
125
+ path&.[]('parts')
126
+ end
127
+ (parts.inject(&:merge) || [])
128
+ end
129
+
130
+ def __http_method
131
+ case @endpoint_name
132
+ when 'index'
133
+ '_id ? Elasticsearch::API::HTTP_PUT : Elasticsearch::API::HTTP_POST'
134
+ when 'count'
135
+ <<~SRC
136
+ if arguments[:body]
137
+ Elasticsearch::API::HTTP_POST
138
+ else
139
+ Elasticsearch::API::HTTP_GET
140
+ end
141
+ SRC
142
+ else
143
+ "Elasticsearch::API::HTTP_#{@spec['url']['paths'].map { |a| a['methods'] }.flatten.first}"
144
+ end
145
+ end
146
+
147
+ def __http_path
148
+ return "\"#{__parse_path(@paths.first)}\"" if @paths.size == 1
149
+ return termvectors_path if @method_name == 'termvectors'
150
+
151
+ result = ''
152
+ anchor_string = ''
153
+ @paths.sort { |a, b| b.length <=> a.length }.each_with_index do |path, i|
154
+ var_string = __extract_path_variables(path).map { |var| "_#{var}" }.join(' && ')
155
+ next if anchor_string == var_string
156
+
157
+ anchor_string = var_string
158
+ result += if i.zero?
159
+ "if #{var_string}\n"
160
+ elsif (i == @paths.size - 1) || var_string.empty?
161
+ "else\n"
162
+ else
163
+ "elsif #{var_string}\n"
164
+ end
165
+ result += "\"#{__parse_path(path)}\"\n"
137
166
  end
167
+ result += 'end'
168
+ result
169
+ end
170
+
171
+ def __parse_path(path)
172
+ path.gsub(/^\//, '')
173
+ .gsub(/\/$/, '')
174
+ .gsub('{', "\#{#{__utils}.__listify(_")
175
+ .gsub('}', ')}')
176
+ end
177
+
178
+ def __path_variables
179
+ @paths.map do |path|
180
+ __extract_path_variables(path)
181
+ end
182
+ end
183
+
184
+ # extract values that are in the {var} format:
185
+ def __extract_path_variables(path)
186
+ path.scan(/{(\w+)}/).flatten
187
+ end
188
+
189
+ # Find parts that are definitely required and should raise an error if
190
+ # they're not present
191
+ #
192
+ def __required_parts
193
+ required = []
194
+ return required if @endpoint_name == 'tasks.get'
195
+
196
+ required << 'body' if (@spec['body'] && @spec['body']['required'])
197
+ # Get required variables from paths:
198
+ req_variables = __path_variables.inject(:&) # find intersection
199
+ required << req_variables unless req_variables.empty?
200
+ required.flatten
138
201
  end
139
202
 
203
+ def docs_helper(name, info)
204
+ info['type'] = 'String' if info['type'] == 'enum' # Rename 'enums' to 'strings'
205
+ tipo = info['type'] ? info['type'].capitalize : 'String'
206
+ description = info['description'] ? info['description'].strip : '[TODO]'
207
+ options = info['options'] ? "\n # (options: #{info['options'].join(', '.strip)})\n" : ''
208
+ required = info['required'] ? ' (*Required*)' : ''
209
+ deprecated = info['deprecated'] ? ' *Deprecated*' : ''
210
+ "# @option arguments [#{tipo}] :#{name} #{description} #{required} #{deprecated} #{options}\n"
211
+ end
212
+
213
+ def generate_tests
214
+ copy_file 'templates/test_helper.rb', @output.join('test').join('test_helper.rb')
215
+
216
+ @test_directory = @output.join('test/api').join(@module_namespace.join('/'))
217
+ @test_file = @test_directory.join("#{@method_name}_test.rb")
218
+
219
+ empty_directory @test_directory
220
+ template 'templates/test.erb', @test_file
221
+
222
+ print_source_code(@test_file) if options[:verbose]
223
+ end
224
+
225
+ def print_source_code(path_to_file)
226
+ colorized_output = CodeRay.scan_file(path_to_file, :ruby).terminal
227
+ lines = colorized_output.split("\n")
228
+ formatted = lines.first + "\n" + lines[1, lines.size].map { |l| ' ' * 14 + l }.join("\n")
229
+
230
+ say_status('ruby', formatted, :yellow)
231
+ end
232
+
233
+ def print_tree
234
+ return unless `which tree > /dev/null 2>&1; echo $?`.to_i < 1
235
+
236
+ lines = `tree #{@output}`.split("\n")
237
+ say_status('tree', lines.first + "\n" + lines[1, lines.size].map { |l| ' ' * 14 + l }.join("\n"))
238
+ end
239
+
240
+ def __utils
241
+ @xpack ? 'Elasticsearch::API::Utils' : 'Utils'
242
+ end
243
+
244
+ def run_rubocop
245
+ system("rubocop --format autogenconf -x #{FilesHelper::output_dir(@xpack)}")
246
+ end
140
247
  end
141
248
  end
142
249
  end