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