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