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