13558: Merge branch 'master' into wtsi-hgi-13558-debug-log-tag-req-id
[arvados.git] / services / api / app / controllers / arvados / v1 / schema_controller.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class Arvados::V1::SchemaController < ApplicationController
6   skip_before_filter :catch_redirect_hint
7   skip_before_filter :find_objects_for_index
8   skip_before_filter :find_object_by_uuid
9   skip_before_filter :load_filters_param
10   skip_before_filter :load_limit_offset_order_params
11   skip_before_filter :load_read_auths
12   skip_before_filter :load_where_param
13   skip_before_filter :render_404_if_no_object
14   skip_before_filter :require_auth_scope
15
16   include DbCurrentTime
17
18   def index
19     expires_in 24.hours, public: true
20     send_json discovery_doc
21   end
22
23   protected
24
25   def discovery_doc
26     Rails.cache.fetch 'arvados_v1_rest_discovery' do
27       Rails.application.eager_load!
28       discovery = {
29         kind: "discovery#restDescription",
30         discoveryVersion: "v1",
31         id: "arvados:v1",
32         name: "arvados",
33         version: "v1",
34         revision: "20131114",
35         source_version: AppVersion.hash,
36         sourceVersion: AppVersion.hash, # source_version should be deprecated in the future
37         packageVersion: AppVersion.package_version,
38         generatedAt: db_current_time.iso8601,
39         title: "Arvados API",
40         description: "The API to interact with Arvados.",
41         documentationLink: "http://doc.arvados.org/api/index.html",
42         defaultCollectionReplication: Rails.configuration.default_collection_replication,
43         protocol: "rest",
44         baseUrl: root_url + "arvados/v1/",
45         basePath: "/arvados/v1/",
46         rootUrl: root_url,
47         servicePath: "arvados/v1/",
48         batchPath: "batch",
49         uuidPrefix: Rails.application.config.uuid_prefix,
50         defaultTrashLifetime: Rails.application.config.default_trash_lifetime,
51         blobSignatureTtl: Rails.application.config.blob_signature_ttl,
52         maxRequestSize: Rails.application.config.max_request_size,
53         dockerImageFormats: Rails.application.config.docker_image_formats,
54         crunchLogBytesPerEvent: Rails.application.config.crunch_log_bytes_per_event,
55         crunchLogSecondsBetweenEvents: Rails.application.config.crunch_log_seconds_between_events,
56         crunchLogThrottlePeriod: Rails.application.config.crunch_log_throttle_period,
57         crunchLogThrottleBytes: Rails.application.config.crunch_log_throttle_bytes,
58         crunchLogThrottleLines: Rails.application.config.crunch_log_throttle_lines,
59         crunchLimitLogBytesPerJob: Rails.application.config.crunch_limit_log_bytes_per_job,
60         crunchLogPartialLineThrottlePeriod: Rails.application.config.crunch_log_partial_line_throttle_period,
61         remoteHosts: Rails.configuration.remote_hosts,
62         remoteHostsViaDNS: Rails.configuration.remote_hosts_via_dns,
63         websocketUrl: Rails.application.config.websocket_address,
64         workbenchUrl: Rails.application.config.workbench_address,
65         keepWebServiceUrl: Rails.application.config.keep_web_service_url,
66         gitUrl: case Rails.application.config.git_repo_https_base
67                 when false
68                   ''
69                 when true
70                   'https://git.%s.arvadosapi.com/' % Rails.configuration.uuid_prefix
71                 else
72                   Rails.application.config.git_repo_https_base
73                 end,
74         parameters: {
75           alt: {
76             type: "string",
77             description: "Data format for the response.",
78             default: "json",
79             enum: [
80                    "json"
81                   ],
82             enumDescriptions: [
83                                "Responses with Content-Type of application/json"
84                               ],
85             location: "query"
86           },
87           fields: {
88             type: "string",
89             description: "Selector specifying which fields to include in a partial response.",
90             location: "query"
91           },
92           key: {
93             type: "string",
94             description: "API key. Your API key identifies your project and provides you with API access, quota, and reports. Required unless you provide an OAuth 2.0 token.",
95             location: "query"
96           },
97           oauth_token: {
98             type: "string",
99             description: "OAuth 2.0 token for the current user.",
100             location: "query"
101           }
102         },
103         auth: {
104           oauth2: {
105             scopes: {
106               "https://api.curoverse.com/auth/arvados" => {
107                 description: "View and manage objects"
108               },
109               "https://api.curoverse.com/auth/arvados.readonly" => {
110                 description: "View objects"
111               }
112             }
113           }
114         },
115         schemas: {},
116         resources: {}
117       }
118
119       ActiveRecord::Base.descendants.reject(&:abstract_class?).each do |k|
120         begin
121           ctl_class = "Arvados::V1::#{k.to_s.pluralize}Controller".constantize
122         rescue
123           # No controller -> no discovery.
124           next
125         end
126         object_properties = {}
127         k.columns.
128           select { |col| col.name != 'id' && !col.name.start_with?('secret_') }.
129           collect do |col|
130           if k.serialized_attributes.has_key? col.name
131             object_properties[col.name] = {
132               type: k.serialized_attributes[col.name].object_class.to_s
133             }
134           else
135             object_properties[col.name] = {
136               type: col.type
137             }
138           end
139         end
140         discovery[:schemas][k.to_s + 'List'] = {
141           id: k.to_s + 'List',
142           description: k.to_s + ' list',
143           type: "object",
144           properties: {
145             kind: {
146               type: "string",
147               description: "Object type. Always arvados##{k.to_s.camelcase(:lower)}List.",
148               default: "arvados##{k.to_s.camelcase(:lower)}List"
149             },
150             etag: {
151               type: "string",
152               description: "List version."
153             },
154             items: {
155               type: "array",
156               description: "The list of #{k.to_s.pluralize}.",
157               items: {
158                 "$ref" => k.to_s
159               }
160             },
161             next_link: {
162               type: "string",
163               description: "A link to the next page of #{k.to_s.pluralize}."
164             },
165             next_page_token: {
166               type: "string",
167               description: "The page token for the next page of #{k.to_s.pluralize}."
168             },
169             selfLink: {
170               type: "string",
171               description: "A link back to this list."
172             }
173           }
174         }
175         discovery[:schemas][k.to_s] = {
176           id: k.to_s,
177           description: k.to_s,
178           type: "object",
179           uuidPrefix: (k.respond_to?(:uuid_prefix) ? k.uuid_prefix : nil),
180           properties: {
181             uuid: {
182               type: "string",
183               description: "Object ID."
184             },
185             etag: {
186               type: "string",
187               description: "Object version."
188             }
189           }.merge(object_properties)
190         }
191         discovery[:resources][k.to_s.underscore.pluralize] = {
192           methods: {
193             get: {
194               id: "arvados.#{k.to_s.underscore.pluralize}.get",
195               path: "#{k.to_s.underscore.pluralize}/{uuid}",
196               httpMethod: "GET",
197               description: "Gets a #{k.to_s}'s metadata by UUID.",
198               parameters: {
199                 uuid: {
200                   type: "string",
201                   description: "The UUID of the #{k.to_s} in question.",
202                   required: true,
203                   location: "path"
204                 }
205               },
206               parameterOrder: [
207                                "uuid"
208                               ],
209               response: {
210                 "$ref" => k.to_s
211               },
212               scopes: [
213                        "https://api.curoverse.com/auth/arvados",
214                        "https://api.curoverse.com/auth/arvados.readonly"
215                       ]
216             },
217             index: {
218               id: "arvados.#{k.to_s.underscore.pluralize}.index",
219               path: k.to_s.underscore.pluralize,
220               httpMethod: "GET",
221               description:
222                  %|Index #{k.to_s.pluralize}.
223
224                    The <code>index</code> method returns a
225                    <a href="/api/resources.html">resource list</a> of
226                    matching #{k.to_s.pluralize}. For example:
227
228                    <pre>
229                    {
230                     "kind":"arvados##{k.to_s.camelcase(:lower)}List",
231                     "etag":"",
232                     "self_link":"",
233                     "next_page_token":"",
234                     "next_link":"",
235                     "items":[
236                        ...
237                     ],
238                     "items_available":745,
239                     "_profile":{
240                      "request_time":0.157236317
241                     }
242                     </pre>|,
243               parameters: {
244               },
245               response: {
246                 "$ref" => "#{k.to_s}List"
247               },
248               scopes: [
249                        "https://api.curoverse.com/auth/arvados",
250                        "https://api.curoverse.com/auth/arvados.readonly"
251                       ]
252             },
253             create: {
254               id: "arvados.#{k.to_s.underscore.pluralize}.create",
255               path: "#{k.to_s.underscore.pluralize}",
256               httpMethod: "POST",
257               description: "Create a new #{k.to_s}.",
258               parameters: {},
259               request: {
260                 required: true,
261                 properties: {
262                   k.to_s.underscore => {
263                     "$ref" => k.to_s
264                   }
265                 }
266               },
267               response: {
268                 "$ref" => k.to_s
269               },
270               scopes: [
271                        "https://api.curoverse.com/auth/arvados"
272                       ]
273             },
274             update: {
275               id: "arvados.#{k.to_s.underscore.pluralize}.update",
276               path: "#{k.to_s.underscore.pluralize}/{uuid}",
277               httpMethod: "PUT",
278               description: "Update attributes of an existing #{k.to_s}.",
279               parameters: {
280                 uuid: {
281                   type: "string",
282                   description: "The UUID of the #{k.to_s} in question.",
283                   required: true,
284                   location: "path"
285                 }
286               },
287               request: {
288                 required: true,
289                 properties: {
290                   k.to_s.underscore => {
291                     "$ref" => k.to_s
292                   }
293                 }
294               },
295               response: {
296                 "$ref" => k.to_s
297               },
298               scopes: [
299                        "https://api.curoverse.com/auth/arvados"
300                       ]
301             },
302             delete: {
303               id: "arvados.#{k.to_s.underscore.pluralize}.delete",
304               path: "#{k.to_s.underscore.pluralize}/{uuid}",
305               httpMethod: "DELETE",
306               description: "Delete an existing #{k.to_s}.",
307               parameters: {
308                 uuid: {
309                   type: "string",
310                   description: "The UUID of the #{k.to_s} in question.",
311                   required: true,
312                   location: "path"
313                 }
314               },
315               response: {
316                 "$ref" => k.to_s
317               },
318               scopes: [
319                        "https://api.curoverse.com/auth/arvados"
320                       ]
321             }
322           }
323         }
324         # Check for Rails routes that don't match the usual actions
325         # listed above
326         d_methods = discovery[:resources][k.to_s.underscore.pluralize][:methods]
327         Rails.application.routes.routes.each do |route|
328           action = route.defaults[:action]
329           httpMethod = ['GET', 'POST', 'PUT', 'DELETE'].map { |method|
330             method if route.verb.match(method)
331           }.compact.first
332           if httpMethod and
333               route.defaults[:controller] == 'arvados/v1/' + k.to_s.underscore.pluralize and
334               ctl_class.action_methods.include? action
335             if !d_methods[action.to_sym]
336               method = {
337                 id: "arvados.#{k.to_s.underscore.pluralize}.#{action}",
338                 path: route.path.spec.to_s.sub('/arvados/v1/','').sub('(.:format)','').sub(/:(uu)?id/,'{uuid}'),
339                 httpMethod: httpMethod,
340                 description: "#{action} #{k.to_s.underscore.pluralize}",
341                 parameters: {},
342                 response: {
343                   "$ref" => (action == 'index' ? "#{k.to_s}List" : k.to_s)
344                 },
345                 scopes: [
346                          "https://api.curoverse.com/auth/arvados"
347                         ]
348               }
349               route.segment_keys.each do |key|
350                 if key != :format
351                   key = :uuid if key == :id
352                   method[:parameters][key] = {
353                     type: "string",
354                     description: "",
355                     required: true,
356                     location: "path"
357                   }
358                 end
359               end
360             else
361               # We already built a generic method description, but we
362               # might find some more required parameters through
363               # introspection.
364               method = d_methods[action.to_sym]
365             end
366             if ctl_class.respond_to? "_#{action}_requires_parameters".to_sym
367               ctl_class.send("_#{action}_requires_parameters".to_sym).each do |l, v|
368                 if v.is_a? Hash
369                   method[:parameters][l] = v
370                 else
371                   method[:parameters][l] = {}
372                 end
373                 if !method[:parameters][l][:default].nil?
374                   # The JAVA SDK is sensitive to all values being strings
375                   method[:parameters][l][:default] = method[:parameters][l][:default].to_s
376                 end
377                 method[:parameters][l][:type] ||= 'string'
378                 method[:parameters][l][:description] ||= ''
379                 method[:parameters][l][:location] = (route.segment_keys.include?(l) ? 'path' : 'query')
380                 if method[:parameters][l][:required].nil?
381                   method[:parameters][l][:required] = v != false
382                 end
383               end
384             end
385             d_methods[action.to_sym] = method
386
387             if action == 'index'
388               list_method = method.dup
389               list_method[:id].sub!('index', 'list')
390               list_method[:description].sub!('Index', 'List')
391               list_method[:description].sub!('index', 'list')
392               d_methods[:list] = list_method
393             end
394           end
395         end
396       end
397       Rails.configuration.disable_api_methods.each do |method|
398         ctrl, action = method.split('.', 2)
399         discovery[:resources][ctrl][:methods].delete(action.to_sym)
400       end
401       discovery
402     end
403   end
404 end