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