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