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