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