18600: Merge branch 'main'
[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_select_param
12   skip_before_action :load_read_auths
13   skip_before_action :load_where_param
14   skip_before_action :render_404_if_no_object
15   skip_before_action :require_auth_scope
16
17   include DbCurrentTime
18
19   def index
20     expires_in 24.hours, public: true
21     send_json discovery_doc
22   end
23
24   protected
25
26   def discovery_doc
27     Rails.cache.fetch 'arvados_v1_rest_discovery' do
28       Rails.application.eager_load!
29       remoteHosts = {}
30       Rails.configuration.RemoteClusters.each {|k,v| if k != :"*" then remoteHosts[k] = v["Host"] end }
31       discovery = {
32         kind: "discovery#restDescription",
33         discoveryVersion: "v1",
34         id: "arvados:v1",
35         name: "arvados",
36         version: "v1",
37         # format is YYYYMMDD, must be fixed width (needs to be lexically
38         # sortable), updated manually, may be used by clients to
39         # determine availability of API server features.
40         revision: "20220222",
41         source_version: AppVersion.hash,
42         sourceVersion: AppVersion.hash, # source_version should be deprecated in the future
43         packageVersion: AppVersion.package_version,
44         generatedAt: db_current_time.iso8601,
45         title: "Arvados API",
46         description: "The API to interact with Arvados.",
47         documentationLink: "http://doc.arvados.org/api/index.html",
48         defaultCollectionReplication: Rails.configuration.Collections.DefaultReplication,
49         protocol: "rest",
50         baseUrl: root_url + "arvados/v1/",
51         basePath: "/arvados/v1/",
52         rootUrl: root_url,
53         servicePath: "arvados/v1/",
54         batchPath: "batch",
55         uuidPrefix: Rails.configuration.ClusterID,
56         defaultTrashLifetime: Rails.configuration.Collections.DefaultTrashLifetime,
57         blobSignatureTtl: Rails.configuration.Collections.BlobSigningTTL,
58         maxRequestSize: Rails.configuration.API.MaxRequestSize,
59         maxItemsPerResponse: Rails.configuration.API.MaxItemsPerResponse,
60         dockerImageFormats: Rails.configuration.Containers.SupportedDockerImageFormats.keys,
61         crunchLogBytesPerEvent: Rails.configuration.Containers.Logging.LogBytesPerEvent,
62         crunchLogSecondsBetweenEvents: Rails.configuration.Containers.Logging.LogSecondsBetweenEvents,
63         crunchLogThrottlePeriod: Rails.configuration.Containers.Logging.LogThrottlePeriod,
64         crunchLogThrottleBytes: Rails.configuration.Containers.Logging.LogThrottleBytes,
65         crunchLogThrottleLines: Rails.configuration.Containers.Logging.LogThrottleLines,
66         crunchLimitLogBytesPerJob: Rails.configuration.Containers.Logging.LimitLogBytesPerJob,
67         crunchLogPartialLineThrottlePeriod: Rails.configuration.Containers.Logging.LogPartialLineThrottlePeriod,
68         crunchLogUpdatePeriod: Rails.configuration.Containers.Logging.LogUpdatePeriod,
69         crunchLogUpdateSize: Rails.configuration.Containers.Logging.LogUpdateSize,
70         remoteHosts: remoteHosts,
71         remoteHostsViaDNS: Rails.configuration.RemoteClusters["*"].Proxy,
72         websocketUrl: Rails.configuration.Services.Websocket.ExternalURL.to_s,
73         workbenchUrl: Rails.configuration.Services.Workbench1.ExternalURL.to_s,
74         workbench2Url: Rails.configuration.Services.Workbench2.ExternalURL.to_s,
75         keepWebServiceUrl: Rails.configuration.Services.WebDAV.ExternalURL.to_s,
76         gitUrl: Rails.configuration.Services.GitHTTP.ExternalURL.to_s,
77         parameters: {
78           alt: {
79             type: "string",
80             description: "Data format for the response.",
81             default: "json",
82             enum: [
83                    "json"
84                   ],
85             enumDescriptions: [
86                                "Responses with Content-Type of application/json"
87                               ],
88             location: "query"
89           },
90           fields: {
91             type: "string",
92             description: "Selector specifying which fields to include in a partial response.",
93             location: "query"
94           },
95           key: {
96             type: "string",
97             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.",
98             location: "query"
99           },
100           oauth_token: {
101             type: "string",
102             description: "OAuth 2.0 token for the current user.",
103             location: "query"
104           }
105         },
106         auth: {
107           oauth2: {
108             scopes: {
109               "https://api.arvados.org/auth/arvados" => {
110                 description: "View and manage objects"
111               },
112               "https://api.arvados.org/auth/arvados.readonly" => {
113                 description: "View objects"
114               }
115             }
116           }
117         },
118         schemas: {},
119         resources: {}
120       }
121
122       ActiveRecord::Base.descendants.reject(&:abstract_class?).each do |k|
123         begin
124           ctl_class = "Arvados::V1::#{k.to_s.pluralize}Controller".constantize
125         rescue
126           # No controller -> no discovery.
127           next
128         end
129         object_properties = {}
130         k.columns.
131           select { |col| col.name != 'id' && !col.name.start_with?('secret_') }.
132           collect do |col|
133           if k.serialized_attributes.has_key? col.name
134             object_properties[col.name] = {
135               type: k.serialized_attributes[col.name].object_class.to_s
136             }
137           elsif k.attribute_types[col.name].is_a? JsonbType::Hash
138             object_properties[col.name] = {
139               type: Hash.to_s
140             }
141           elsif k.attribute_types[col.name].is_a? JsonbType::Array
142             object_properties[col.name] = {
143               type: Array.to_s
144             }
145           else
146             object_properties[col.name] = {
147               type: col.type
148             }
149           end
150         end
151         discovery[:schemas][k.to_s + 'List'] = {
152           id: k.to_s + 'List',
153           description: k.to_s + ' list',
154           type: "object",
155           properties: {
156             kind: {
157               type: "string",
158               description: "Object type. Always arvados##{k.to_s.camelcase(:lower)}List.",
159               default: "arvados##{k.to_s.camelcase(:lower)}List"
160             },
161             etag: {
162               type: "string",
163               description: "List version."
164             },
165             items: {
166               type: "array",
167               description: "The list of #{k.to_s.pluralize}.",
168               items: {
169                 "$ref" => k.to_s
170               }
171             },
172             next_link: {
173               type: "string",
174               description: "A link to the next page of #{k.to_s.pluralize}."
175             },
176             next_page_token: {
177               type: "string",
178               description: "The page token for the next page of #{k.to_s.pluralize}."
179             },
180             selfLink: {
181               type: "string",
182               description: "A link back to this list."
183             }
184           }
185         }
186         discovery[:schemas][k.to_s] = {
187           id: k.to_s,
188           description: k.to_s,
189           type: "object",
190           uuidPrefix: (k.respond_to?(:uuid_prefix) ? k.uuid_prefix : nil),
191           properties: {
192             uuid: {
193               type: "string",
194               description: "Object ID."
195             },
196             etag: {
197               type: "string",
198               description: "Object version."
199             }
200           }.merge(object_properties)
201         }
202         discovery[:resources][k.to_s.underscore.pluralize] = {
203           methods: {
204             get: {
205               id: "arvados.#{k.to_s.underscore.pluralize}.get",
206               path: "#{k.to_s.underscore.pluralize}/{uuid}",
207               httpMethod: "GET",
208               description: "Gets a #{k.to_s}'s metadata by UUID.",
209               parameters: {
210                 uuid: {
211                   type: "string",
212                   description: "The UUID of the #{k.to_s} in question.",
213                   required: true,
214                   location: "path"
215                 }
216               },
217               parameterOrder: [
218                                "uuid"
219                               ],
220               response: {
221                 "$ref" => k.to_s
222               },
223               scopes: [
224                        "https://api.arvados.org/auth/arvados",
225                        "https://api.arvados.org/auth/arvados.readonly"
226                       ]
227             },
228             index: {
229               id: "arvados.#{k.to_s.underscore.pluralize}.index",
230               path: k.to_s.underscore.pluralize,
231               httpMethod: "GET",
232               description:
233                  %|Index #{k.to_s.pluralize}.
234
235                    The <code>index</code> method returns a
236                    <a href="/api/resources.html">resource list</a> of
237                    matching #{k.to_s.pluralize}. For example:
238
239                    <pre>
240                    {
241                     "kind":"arvados##{k.to_s.camelcase(:lower)}List",
242                     "etag":"",
243                     "self_link":"",
244                     "next_page_token":"",
245                     "next_link":"",
246                     "items":[
247                        ...
248                     ],
249                     "items_available":745,
250                     "_profile":{
251                      "request_time":0.157236317
252                     }
253                     </pre>|,
254               parameters: {
255               },
256               response: {
257                 "$ref" => "#{k.to_s}List"
258               },
259               scopes: [
260                        "https://api.arvados.org/auth/arvados",
261                        "https://api.arvados.org/auth/arvados.readonly"
262                       ]
263             },
264             create: {
265               id: "arvados.#{k.to_s.underscore.pluralize}.create",
266               path: "#{k.to_s.underscore.pluralize}",
267               httpMethod: "POST",
268               description: "Create a new #{k.to_s}.",
269               parameters: {},
270               request: {
271                 required: true,
272                 properties: {
273                   k.to_s.underscore => {
274                     "$ref" => k.to_s
275                   }
276                 }
277               },
278               response: {
279                 "$ref" => k.to_s
280               },
281               scopes: [
282                        "https://api.arvados.org/auth/arvados"
283                       ]
284             },
285             update: {
286               id: "arvados.#{k.to_s.underscore.pluralize}.update",
287               path: "#{k.to_s.underscore.pluralize}/{uuid}",
288               httpMethod: "PUT",
289               description: "Update attributes of an existing #{k.to_s}.",
290               parameters: {
291                 uuid: {
292                   type: "string",
293                   description: "The UUID of the #{k.to_s} in question.",
294                   required: true,
295                   location: "path"
296                 }
297               },
298               request: {
299                 required: true,
300                 properties: {
301                   k.to_s.underscore => {
302                     "$ref" => k.to_s
303                   }
304                 }
305               },
306               response: {
307                 "$ref" => k.to_s
308               },
309               scopes: [
310                        "https://api.arvados.org/auth/arvados"
311                       ]
312             },
313             delete: {
314               id: "arvados.#{k.to_s.underscore.pluralize}.delete",
315               path: "#{k.to_s.underscore.pluralize}/{uuid}",
316               httpMethod: "DELETE",
317               description: "Delete an existing #{k.to_s}.",
318               parameters: {
319                 uuid: {
320                   type: "string",
321                   description: "The UUID of the #{k.to_s} in question.",
322                   required: true,
323                   location: "path"
324                 }
325               },
326               response: {
327                 "$ref" => k.to_s
328               },
329               scopes: [
330                        "https://api.arvados.org/auth/arvados"
331                       ]
332             }
333           }
334         }
335         # Check for Rails routes that don't match the usual actions
336         # listed above
337         d_methods = discovery[:resources][k.to_s.underscore.pluralize][:methods]
338         Rails.application.routes.routes.each do |route|
339           action = route.defaults[:action]
340           httpMethod = ['GET', 'POST', 'PUT', 'DELETE'].map { |method|
341             method if route.verb.match(method)
342           }.compact.first
343           if httpMethod and
344               route.defaults[:controller] == 'arvados/v1/' + k.to_s.underscore.pluralize and
345               ctl_class.action_methods.include? action
346             if !d_methods[action.to_sym]
347               method = {
348                 id: "arvados.#{k.to_s.underscore.pluralize}.#{action}",
349                 path: route.path.spec.to_s.sub('/arvados/v1/','').sub('(.:format)','').sub(/:(uu)?id/,'{uuid}'),
350                 httpMethod: httpMethod,
351                 description: "#{action} #{k.to_s.underscore.pluralize}",
352                 parameters: {},
353                 response: {
354                   "$ref" => (action == 'index' ? "#{k.to_s}List" : k.to_s)
355                 },
356                 scopes: [
357                          "https://api.arvados.org/auth/arvados"
358                         ]
359               }
360               route.segment_keys.each do |key|
361                 if key != :format
362                   key = :uuid if key == :id
363                   method[:parameters][key] = {
364                     type: "string",
365                     description: "",
366                     required: true,
367                     location: "path"
368                   }
369                 end
370               end
371             else
372               # We already built a generic method description, but we
373               # might find some more required parameters through
374               # introspection.
375               method = d_methods[action.to_sym]
376             end
377             if ctl_class.respond_to? "_#{action}_requires_parameters".to_sym
378               ctl_class.send("_#{action}_requires_parameters".to_sym).each do |l, v|
379                 if v.is_a? Hash
380                   method[:parameters][l] = v
381                 else
382                   method[:parameters][l] = {}
383                 end
384                 if !method[:parameters][l][:default].nil?
385                   # The JAVA SDK is sensitive to all values being strings
386                   method[:parameters][l][:default] = method[:parameters][l][:default].to_s
387                 end
388                 method[:parameters][l][:type] ||= 'string'
389                 method[:parameters][l][:description] ||= ''
390                 method[:parameters][l][:location] = (route.segment_keys.include?(l) ? 'path' : 'query')
391                 if method[:parameters][l][:required].nil?
392                   method[:parameters][l][:required] = v != false
393                 end
394               end
395             end
396             d_methods[action.to_sym] = method
397
398             if action == 'index'
399               list_method = method.dup
400               list_method[:id].sub!('index', 'list')
401               list_method[:description].sub!('Index', 'List')
402               list_method[:description].sub!('index', 'list')
403               d_methods[:list] = list_method
404             end
405           end
406         end
407       end
408
409       # The 'replace_files' option is implemented in lib/controller,
410       # not Rails -- we just need to add it here so discovery-aware
411       # clients know how to validate it.
412       [:create, :update].each do |action|
413         discovery[:resources]['collections'][:methods][action][:parameters]['replace_files'] = {
414           type: 'object',
415           description: 'Files and directories to initialize/replace with content from other collections.',
416           required: false,
417           location: 'query',
418           properties: {},
419           additionalProperties: {type: 'string'},
420         }
421       end
422
423       discovery[:resources]['configs'] = {
424         methods: {
425           get: {
426             id: "arvados.configs.get",
427             path: "config",
428             httpMethod: "GET",
429             description: "Get public config",
430             parameters: {
431             },
432             parameterOrder: [
433             ],
434             response: {
435             },
436             scopes: [
437               "https://api.arvados.org/auth/arvados",
438               "https://api.arvados.org/auth/arvados.readonly"
439             ]
440           },
441         }
442       }
443
444       discovery[:resources]['vocabularies'] = {
445         methods: {
446           get: {
447             id: "arvados.vocabularies.get",
448             path: "vocabulary",
449             httpMethod: "GET",
450             description: "Get vocabulary definition",
451             parameters: {
452             },
453             parameterOrder: [
454             ],
455             response: {
456             },
457             scopes: [
458               "https://api.arvados.org/auth/arvados",
459               "https://api.arvados.org/auth/arvados.readonly"
460             ]
461           },
462         }
463       }
464
465       discovery[:resources]['sys'] = {
466         methods: {
467           get: {
468             id: "arvados.sys.trash_sweep",
469             path: "sys/trash_sweep",
470             httpMethod: "POST",
471             description: "apply scheduled trash and delete operations",
472             parameters: {
473             },
474             parameterOrder: [
475             ],
476             response: {
477             },
478             scopes: [
479               "https://api.arvados.org/auth/arvados",
480               "https://api.arvados.org/auth/arvados.readonly"
481             ]
482           },
483         }
484       }
485
486       Rails.configuration.API.DisabledAPIs.each do |method, _|
487         ctrl, action = method.to_s.split('.', 2)
488         discovery[:resources][ctrl][:methods].delete(action.to_sym)
489       end
490       discovery
491     end
492   end
493 end