generate_api.rb 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. require 'thor'
  2. require 'pathname'
  3. require 'active_support/core_ext/hash/deep_merge'
  4. require 'active_support/inflector/methods'
  5. require 'rest_client'
  6. require 'json'
  7. require 'pry'
  8. module Elasticsearch
  9. module API
  10. module Utils
  11. # controller.registerHandler(RestRequest.Method.GET, "/_cluster/health", this);
  12. PATTERN_REST = /.*controller.registerHandler\(.*(?<method>GET|POST|PUT|DELETE|HEAD|OPTIONS|PATCH)\s*,\s*"(?<url>.*)"\s*,\s*.+\);/
  13. # request.param("index"), request.paramAsBoolean("docs", indicesStatsRequest.docs()), etc
  14. PATTERN_URL_PARAMS = /request.param.*\("(?<param>[a-z_]+)".*/
  15. # controller.registerHandler(GET, "/{index}/_refresh", this)
  16. PATTERN_URL_PARTS = /\{(?<part>[a-zA-Z0-9\_\-]+)\}/
  17. # request.hasContent()
  18. PATTERN_HAS_BODY = /request\.hasContent()/
  19. # Parses the Elasticsearch source code and returns a Hash of REST API information/specs.
  20. #
  21. # Example:
  22. #
  23. # {
  24. # "cluster.health" => [
  25. # { "method" => "GET",
  26. # "path" => "/_cluster/health",
  27. # "parts" => ["index"],
  28. # "params" => ["index", "local", ... ],
  29. # "body" => false
  30. # }
  31. #
  32. def __parse_java_source(path)
  33. path += '/' unless path =~ /\/$/ # Add trailing slash if missing
  34. prefix = "src/main/java/org/elasticsearch/rest/action"
  35. java_rest_files = Dir["#{path}#{prefix}/**/*.java"]
  36. map = {}
  37. java_rest_files.sort.each do |file|
  38. content = File.read(file)
  39. parts = file.gsub(path+prefix, '').split('/')
  40. name = parts[0, parts.size-1].reject { |p| p =~ /^\s*$/ }.join('.')
  41. # Remove the `admin` namespace
  42. name.gsub! /admin\./, ''
  43. # Extract params
  44. url_params = content.scan(PATTERN_URL_PARAMS).map { |n| n.first }.sort
  45. # Extract parts
  46. url_parts = content.scan(PATTERN_URL_PARTS).map { |n| n.first }.sort
  47. # Extract if body allowed
  48. has_body = !!content.match(PATTERN_HAS_BODY)
  49. # Extract HTTP method and path
  50. content.scan(PATTERN_REST) do |method, path|
  51. (map[name] ||= []) << { 'method' => method,
  52. 'path' => path,
  53. 'parts' => url_parts,
  54. 'params' => url_params,
  55. 'body' => has_body }
  56. end
  57. end
  58. map
  59. end
  60. extend self
  61. end
  62. # Contains a generator which will parse the Elasticsearch *.java source files,
  63. # extract information about REST API endpoints (URLs, HTTP methods, URL parameters, etc),
  64. # and create a skeleton of the JSON API specification file for each endpoint.
  65. #
  66. # Usage:
  67. #
  68. # $ thor help api:generate:spec
  69. #
  70. # Example:
  71. #
  72. # time thor api:generate:spec \
  73. # --force \
  74. # --verbose \
  75. # --crawl \
  76. # --elasticsearch=/path/to/elasticsearch/source/code
  77. #
  78. # Features:
  79. #
  80. # * Extract the API name from the source filename (eg. `admin/cluster/health/RestClusterHealthAction.java` -> `cluster.health`)
  81. # * Extract the URLs from the `registerHandler` statements
  82. # * Extract the URL parts (eg. `{index}`) from the URLs
  83. # * Extract the URL parameters (eg. `{timeout}`) from the `request.param("ABC")` statements
  84. # * Detect whether HTTP body is allowed for the API from `request.hasContent()` statements
  85. # * Search the <http://elasticsearch.org> website to get proper documentation URLs
  86. # * Assemble the JSON format for the API spec
  87. #
  88. class JsonGenerator < Thor
  89. namespace 'api:spec'
  90. include Thor::Actions
  91. __root = Pathname( File.expand_path('../../..', __FILE__) )
  92. # Usage: thor help api:generate:spec
  93. #
  94. desc "generate", "Generate JSON API spec files from Elasticsearch source code"
  95. method_option :force, type: :boolean, default: false, desc: 'Overwrite the output'
  96. method_option :verbose, type: :boolean, default: false, desc: 'Output more information'
  97. method_option :output, default: __root.join('tmp/out'), desc: 'Path to output directory'
  98. method_option :elasticsearch, default: __root.join('tmp/elasticsearch'), desc: 'Path to directory with Elasticsearch source code'
  99. method_option :crawl, type: :boolean, default: false, desc: 'Extract URLs from Elasticsearch website'
  100. def generate
  101. self.class.source_root File.expand_path('../', __FILE__)
  102. @output = Pathname(options[:output])
  103. rest_actions = Utils.__parse_java_source(options[:elasticsearch].to_s)
  104. if rest_actions.empty?
  105. say_status 'ERROR', 'Cannot find Elasticsearch source in ' + options[:elasticsearch].to_s, :red
  106. exit(1)
  107. end
  108. rest_actions.each do |name, info|
  109. doc_url = ""
  110. parts = info.reduce([]) { |sum, n| sum |= n['parts']; sum }.reduce({}) { |sum, n| sum[n] = {}; sum }
  111. params = info.reduce([]) { |sum, n| sum |= n['params']; sum }.reduce({}) { |sum, n| sum[n] = {}; sum }
  112. if options[:crawl]
  113. begin
  114. response = RestClient.get "http://search.elasticsearch.org/elastic-search-website/guide/_search?q=#{URI.escape(name.gsub(/\./, ' '))}"
  115. hits = JSON.load(response)['hits']['hits']
  116. if hit = hits.first
  117. if hit['_score'] > 0.2
  118. doc_title = hit['fields']['title']
  119. doc_url = "http://elasticsearch.org" + hit['fields']['url']
  120. end
  121. end
  122. rescue Exception => e
  123. puts "[!] ERROR: #{e.inspect}"
  124. end
  125. end
  126. spec = {
  127. name => {
  128. 'documentation' => doc_url,
  129. 'methods' => info.map { |n| n['method'] }.uniq,
  130. 'url' => {
  131. 'path' => info.first['path'],
  132. 'paths' => info.map { |n| n['path'] }.uniq,
  133. 'parts' => parts,
  134. 'params' => params
  135. },
  136. 'body' => info.first['body'] ? {} : nil
  137. }
  138. }
  139. json = JSON.pretty_generate(spec, indent: ' ', array_nl: '', object_nl: "\n", space: ' ', space_before: ' ')
  140. # Fix JSON array formatting
  141. json.gsub!(/\[\s+/, '[')
  142. json.gsub!(/, {2,}"/, ', "')
  143. create_file @output.join( "#{name}.json" ), json + "\n"
  144. if options[:verbose]
  145. lines = json.split("\n")
  146. say_status 'JSON',
  147. lines.first + "\n" + lines[1, lines.size].map { |l| ' '*14 + l }.join("\n")
  148. end
  149. end
  150. end
  151. private
  152. end
  153. end
  154. end