Merge branch 'master' into 14723-cwl-multiple-file-targets
[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_action :catch_redirect_hint
7   skip_before_action :find_objects_for_index
8   skip_before_action :find_object_by_uuid
9   skip_before_action :load_filters_param
10   skip_before_action :load_limit_offset_order_params
11   skip_before_action :load_read_auths
12   skip_before_action :load_where_param
13   skip_before_action :render_404_if_no_object
14   skip_before_action :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       remoteHosts = {}
29       Rails.configuration.RemoteClusters.each {|k,v| if k != "*" then remoteHosts[k] = v["Host"] end }
30       discovery = {
31         kind: "discovery#restDescription",
32         discoveryVersion: "v1",
33         id: "arvados:v1",
34         name: "arvados",
35         version: "v1",
36         revision: "20131114",
37         source_version: AppVersion.hash,
38         sourceVersion: AppVersion.hash, # source_version should be deprecated in the future
39         packageVersion: AppVersion.package_version,
40         generatedAt: db_current_time.iso8601,
41         title: "Arvados API",
42         description: "The API to interact with Arvados.",
43         documentationLink: "http://doc.arvados.org/api/index.html",
44         defaultCollectionReplication: Rails.configuration.Collections.DefaultReplication,
45         protocol: "rest",
46         baseUrl: root_url + "arvados/v1/",
47         basePath: "/arvados/v1/",
48         rootUrl: root_url,
49         servicePath: "arvados/v1/",
50         batchPath: "batch",
51         uuidPrefix: Rails.configuration.ClusterID,
52         defaultTrashLifetime: Rails.configuration.Collections.DefaultTrashLifetime,
53         blobSignatureTtl: Rails.configuration.Collections.BlobSigningTTL,
54         maxRequestSize: Rails.configuration.API.MaxRequestSize,
55         maxItemsPerResponse: Rails.configuration.API.MaxItemsPerResponse,
56         dockerImageFormats: Rails.configuration.Containers.SupportedDockerImageFormats,
57         crunchLogBytesPerEvent: Rails.configuration.Containers.Logging.LogBytesPerEvent,
58         crunchLogSecondsBetweenEvents: Rails.configuration.Containers.Logging.LogSecondsBetweenEvents,
59         crunchLogThrottlePeriod: Rails.configuration.Containers.Logging.LogThrottlePeriod,
60         crunchLogThrottleBytes: Rails.configuration.Containers.Logging.LogThrottleBytes,
61         crunchLogThrottleLines: Rails.configuration.Containers.Logging.LogThrottleLines,
62         crunchLimitLogBytesPerJob: Rails.configuration.Containers.Logging.LimitLogBytesPerJob,
63         crunchLogPartialLineThrottlePeriod: Rails.configuration.Containers.Logging.LogPartialLineThrottlePeriod,
64         crunchLogUpdatePeriod: Rails.configuration.Containers.Logging.LogUpdatePeriod,
65         crunchLogUpdateSize: Rails.configuration.Containers.Logging.LogUpdateSize,
66         remoteHosts: remoteHosts,
67         remoteHostsViaDNS: Rails.configuration.RemoteClusters["*"].Proxy,
68         websocketUrl: Rails.configuration.Services.Websocket.ExternalURL.to_s,
69         workbenchUrl: Rails.configuration.Services.Workbench1.ExternalURL.to_s,
70         keepWebServiceUrl: Rails.configuration.Services.WebDAV.ExternalURL.to_s,
71         gitUrl: Rails.configuration.Services.GitHTTP.ExternalURL.to_s,
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' && !col.name.start_with?('secret_') }.
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           elsif k.attribute_types[col.name].is_a? JsonbType::Hash
133             object_properties[col.name] = {
134               type: Hash.to_s
135             }
136           elsif k.attribute_types[col.name].is_a? JsonbType::Array
137             object_properties[col.name] = {
138               type: Array.to_s
139             }
140           else
141             object_properties[col.name] = {
142               type: col.type
143             }
144           end
145         end
146         discovery[:schemas][k.to_s + 'List'] = {
147           id: k.to_s + 'List',
148           description: k.to_s + ' list',
149           type: "object",
150           properties: {
151             kind: {
152               type: "string",
153               description: "Object type. Always arvados##{k.to_s.camelcase(:lower)}List.",
154               default: "arvados##{k.to_s.camelcase(:lower)}List"
155             },
156             etag: {
157               type: "string",
158               description: "List version."
159             },
160             items: {
161               type: "array",
162               description: "The list of #{k.to_s.pluralize}.",
163               items: {
164                 "$ref" => k.to_s
165               }
166             },
167             next_link: {
168               type: "string",
169               description: "A link to the next page of #{k.to_s.pluralize}."
170             },
171             next_page_token: {
172               type: "string",
173               description: "The page token for the next page of #{k.to_s.pluralize}."
174             },
175             selfLink: {
176               type: "string",
177               description: "A link back to this list."
178             }
179           }
180         }
181         discovery[:schemas][k.to_s] = {
182           id: k.to_s,
183           description: k.to_s,
184           type: "object",
185           uuidPrefix: (k.respond_to?(:uuid_prefix) ? k.uuid_prefix : nil),
186           properties: {
187             uuid: {
188               type: "string",
189               description: "Object ID."
190             },
191             etag: {
192               type: "string",
193               description: "Object version."
194             }
195           }.merge(object_properties)
196         }
197         discovery[:resources][k.to_s.underscore.pluralize] = {
198           methods: {
199             get: {
200               id: "arvados.#{k.to_s.underscore.pluralize}.get",
201               path: "#{k.to_s.underscore.pluralize}/{uuid}",
202               httpMethod: "GET",
203               description: "Gets a #{k.to_s}'s metadata by UUID.",
204               parameters: {
205                 uuid: {
206                   type: "string",
207                   description: "The UUID of the #{k.to_s} in question.",
208                   required: true,
209                   location: "path"
210                 }
211               },
212               parameterOrder: [
213                                "uuid"
214                               ],
215               response: {
216                 "$ref" => k.to_s
217               },
218               scopes: [
219                        "https://api.curoverse.com/auth/arvados",
220                        "https://api.curoverse.com/auth/arvados.readonly"
221                       ]
222             },
223             index: {
224               id: "arvados.#{k.to_s.underscore.pluralize}.index",
225               path: k.to_s.underscore.pluralize,
226               httpMethod: "GET",
227               description:
228                  %|Index #{k.to_s.pluralize}.
229
230                    The <code>index</code> method returns a
231                    <a href="/api/resources.html">resource list</a> of
232                    matching #{k.to_s.pluralize}. For example:
233
234                    <pre>
235                    {
236                     "kind":"arvados##{k.to_s.camelcase(:lower)}List",
237                     "etag":"",
238                     "self_link":"",
239                     "next_page_token":"",
240                     "next_link":"",
241                     "items":[
242                        ...
243                     ],
244                     "items_available":745,
245                     "_profile":{
246                      "request_time":0.157236317
247                     }
248                     </pre>|,
249               parameters: {
250               },
251               response: {
252                 "$ref" => "#{k.to_s}List"
253               },
254               scopes: [
255                        "https://api.curoverse.com/auth/arvados",
256                        "https://api.curoverse.com/auth/arvados.readonly"
257                       ]
258             },
259             create: {
260               id: "arvados.#{k.to_s.underscore.pluralize}.create",
261               path: "#{k.to_s.underscore.pluralize}",
262               httpMethod: "POST",
263               description: "Create a new #{k.to_s}.",
264               parameters: {},
265               request: {
266                 required: true,
267                 properties: {
268                   k.to_s.underscore => {
269                     "$ref" => k.to_s
270                   }
271                 }
272               },
273               response: {
274                 "$ref" => k.to_s
275               },
276               scopes: [
277                        "https://api.curoverse.com/auth/arvados"
278                       ]
279             },
280             update: {
281               id: "arvados.#{k.to_s.underscore.pluralize}.update",
282               path: "#{k.to_s.underscore.pluralize}/{uuid}",
283               httpMethod: "PUT",
284               description: "Update attributes of an existing #{k.to_s}.",
285               parameters: {
286                 uuid: {
287                   type: "string",
288                   description: "The UUID of the #{k.to_s} in question.",
289                   required: true,
290                   location: "path"
291                 }
292               },
293               request: {
294                 required: true,
295                 properties: {
296                   k.to_s.underscore => {
297                     "$ref" => k.to_s
298                   }
299                 }
300               },
301               response: {
302                 "$ref" => k.to_s
303               },
304               scopes: [
305                        "https://api.curoverse.com/auth/arvados"
306                       ]
307             },
308             delete: {
309               id: "arvados.#{k.to_s.underscore.pluralize}.delete",
310               path: "#{k.to_s.underscore.pluralize}/{uuid}",
311               httpMethod: "DELETE",
312               description: "Delete an existing #{k.to_s}.",
313               parameters: {
314                 uuid: {
315                   type: "string",
316                   description: "The UUID of the #{k.to_s} in question.",
317                   required: true,
318                   location: "path"
319                 }
320               },
321               response: {
322                 "$ref" => k.to_s
323               },
324               scopes: [
325                        "https://api.curoverse.com/auth/arvados"
326                       ]
327             }
328           }
329         }
330         # Check for Rails routes that don't match the usual actions
331         # listed above
332         d_methods = discovery[:resources][k.to_s.underscore.pluralize][:methods]
333         Rails.application.routes.routes.each do |route|
334           action = route.defaults[:action]
335           httpMethod = ['GET', 'POST', 'PUT', 'DELETE'].map { |method|
336             method if route.verb.match(method)
337           }.compact.first
338           if httpMethod and
339               route.defaults[:controller] == 'arvados/v1/' + k.to_s.underscore.pluralize and
340               ctl_class.action_methods.include? action
341             if !d_methods[action.to_sym]
342               method = {
343                 id: "arvados.#{k.to_s.underscore.pluralize}.#{action}",
344                 path: route.path.spec.to_s.sub('/arvados/v1/','').sub('(.:format)','').sub(/:(uu)?id/,'{uuid}'),
345                 httpMethod: httpMethod,
346                 description: "#{action} #{k.to_s.underscore.pluralize}",
347                 parameters: {},
348                 response: {
349                   "$ref" => (action == 'index' ? "#{k.to_s}List" : k.to_s)
350                 },
351                 scopes: [
352                          "https://api.curoverse.com/auth/arvados"
353                         ]
354               }
355               route.segment_keys.each do |key|
356                 if key != :format
357                   key = :uuid if key == :id
358                   method[:parameters][key] = {
359                     type: "string",
360                     description: "",
361                     required: true,
362                     location: "path"
363                   }
364                 end
365               end
366             else
367               # We already built a generic method description, but we
368               # might find some more required parameters through
369               # introspection.
370               method = d_methods[action.to_sym]
371             end
372             if ctl_class.respond_to? "_#{action}_requires_parameters".to_sym
373               ctl_class.send("_#{action}_requires_parameters".to_sym).each do |l, v|
374                 if v.is_a? Hash
375                   method[:parameters][l] = v
376                 else
377                   method[:parameters][l] = {}
378                 end
379                 if !method[:parameters][l][:default].nil?
380                   # The JAVA SDK is sensitive to all values being strings
381                   method[:parameters][l][:default] = method[:parameters][l][:default].to_s
382                 end
383                 method[:parameters][l][:type] ||= 'string'
384                 method[:parameters][l][:description] ||= ''
385                 method[:parameters][l][:location] = (route.segment_keys.include?(l) ? 'path' : 'query')
386                 if method[:parameters][l][:required].nil?
387                   method[:parameters][l][:required] = v != false
388                 end
389               end
390             end
391             d_methods[action.to_sym] = method
392
393             if action == 'index'
394               list_method = method.dup
395               list_method[:id].sub!('index', 'list')
396               list_method[:description].sub!('Index', 'List')
397               list_method[:description].sub!('index', 'list')
398               d_methods[:list] = list_method
399             end
400           end
401         end
402       end
403       Rails.configuration.API.DisabledAPIs.each do |method|
404         ctrl, action = method.split('.', 2)
405         discovery[:resources][ctrl][:methods].delete(action.to_sym)
406       end
407       discovery
408     end
409   end
410 end