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