Merge branch '3663-collection-reader-performance'
[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         parameters: {
30           alt: {
31             type: "string",
32             description: "Data format for the response.",
33             default: "json",
34             enum: [
35                    "json"
36                   ],
37             enumDescriptions: [
38                                "Responses with Content-Type of application/json"
39                               ],
40             location: "query"
41           },
42           fields: {
43             type: "string",
44             description: "Selector specifying which fields to include in a partial response.",
45             location: "query"
46           },
47           key: {
48             type: "string",
49             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.",
50             location: "query"
51           },
52           oauth_token: {
53             type: "string",
54             description: "OAuth 2.0 token for the current user.",
55             location: "query"
56           }
57         },
58         auth: {
59           oauth2: {
60             scopes: {
61               "https://api.curoverse.com/auth/arvados" => {
62                 description: "View and manage objects"
63               },
64               "https://api.curoverse.com/auth/arvados.readonly" => {
65                 description: "View objects"
66               }
67             }
68           }
69         },
70         schemas: {},
71         resources: {}
72       }
73
74       if Rails.application.config.websocket_address
75         discovery[:websocketUrl] = Rails.application.config.websocket_address
76       elsif ENV['ARVADOS_WEBSOCKETS']
77         discovery[:websocketUrl] = (root_url.sub /^http/, 'ws') + "websocket"
78       end
79
80       ActiveRecord::Base.descendants.reject(&:abstract_class?).each do |k|
81         begin
82           ctl_class = "Arvados::V1::#{k.to_s.pluralize}Controller".constantize
83         rescue
84           # No controller -> no discovery.
85           next
86         end
87         object_properties = {}
88         k.columns.
89           select { |col| col.name != 'id' }.
90           collect do |col|
91           if k.serialized_attributes.has_key? col.name
92             object_properties[col.name] = {
93               type: k.serialized_attributes[col.name].object_class.to_s
94             }
95           else
96             object_properties[col.name] = {
97               type: col.type
98             }
99           end
100         end
101         discovery[:schemas][k.to_s + 'List'] = {
102           id: k.to_s + 'List',
103           description: k.to_s + ' list',
104           type: "object",
105           properties: {
106             kind: {
107               type: "string",
108               description: "Object type. Always arvados##{k.to_s.camelcase(:lower)}List.",
109               default: "arvados##{k.to_s.camelcase(:lower)}List"
110             },
111             etag: {
112               type: "string",
113               description: "List version."
114             },
115             items: {
116               type: "array",
117               description: "The list of #{k.to_s.pluralize}.",
118               items: {
119                 "$ref" => k.to_s
120               }
121             },
122             next_link: {
123               type: "string",
124               description: "A link to the next page of #{k.to_s.pluralize}."
125             },
126             next_page_token: {
127               type: "string",
128               description: "The page token for the next page of #{k.to_s.pluralize}."
129             },
130             selfLink: {
131               type: "string",
132               description: "A link back to this list."
133             }
134           }
135         }
136         discovery[:schemas][k.to_s] = {
137           id: k.to_s,
138           description: k.to_s,
139           type: "object",
140           uuidPrefix: (k.respond_to?(:uuid_prefix) ? k.uuid_prefix : nil),
141           properties: {
142             uuid: {
143               type: "string",
144               description: "Object ID."
145             },
146             etag: {
147               type: "string",
148               description: "Object version."
149             }
150           }.merge(object_properties)
151         }
152         discovery[:resources][k.to_s.underscore.pluralize] = {
153           methods: {
154             get: {
155               id: "arvados.#{k.to_s.underscore.pluralize}.get",
156               path: "#{k.to_s.underscore.pluralize}/{uuid}",
157               httpMethod: "GET",
158               description: "Gets a #{k.to_s}'s metadata by UUID.",
159               parameters: {
160                 uuid: {
161                   type: "string",
162                   description: "The UUID of the #{k.to_s} in question.",
163                   required: true,
164                   location: "path"
165                 }
166               },
167               parameterOrder: [
168                                "uuid"
169                               ],
170               response: {
171                 "$ref" => k.to_s
172               },
173               scopes: [
174                        "https://api.curoverse.com/auth/arvados",
175                        "https://api.curoverse.com/auth/arvados.readonly"
176                       ]
177             },
178             list: {
179               id: "arvados.#{k.to_s.underscore.pluralize}.list",
180               path: k.to_s.underscore.pluralize,
181               httpMethod: "GET",
182               description:
183                  %|List #{k.to_s.pluralize}.
184
185                    The <code>list</code> method returns a
186                    <a href="/api/resources.html">resource list</a> of
187                    matching #{k.to_s.pluralize}. For example:
188
189                    <pre>
190                    {
191                     "kind":"arvados##{k.to_s.camelcase(:lower)}List",
192                     "etag":"",
193                     "self_link":"",
194                     "next_page_token":"",
195                     "next_link":"",
196                     "items":[
197                        ...
198                     ],
199                     "items_available":745,
200                     "_profile":{
201                      "request_time":0.157236317
202                     }
203                     </pre>|,
204               parameters: {
205                 limit: {
206                   type: "integer",
207                   description: "Maximum number of #{k.to_s.underscore.pluralize} to return.",
208                   default: "100",
209                   format: "int32",
210                   minimum: "0",
211                   location: "query",
212                 },
213                 offset: {
214                   type: "integer",
215                   description: "Number of #{k.to_s.underscore.pluralize} to skip before first returned record.",
216                   default: "0",
217                   format: "int32",
218                   minimum: "0",
219                   location: "query",
220                   },
221                 filters: {
222                   type: "array",
223                   description: "Conditions for filtering #{k.to_s.underscore.pluralize}.",
224                   location: "query"
225                 },
226                 where: {
227                   type: "object",
228                   description: "Conditions for filtering #{k.to_s.underscore.pluralize}. (Deprecated. Use filters instead.)",
229                   location: "query"
230                 },
231                 order: {
232                   type: "string",
233                   description: "Order in which to return matching #{k.to_s.underscore.pluralize}.",
234                   location: "query"
235                 },
236                 select: {
237                   type: "array",
238                   description: "Select which fields to return",
239                   location: "query"
240                 },
241                 distinct: {
242                   type: "boolean",
243                   description: "Return each distinct object",
244                   location: "query"
245                 }
246               },
247               response: {
248                 "$ref" => "#{k.to_s}List"
249               },
250               scopes: [
251                        "https://api.curoverse.com/auth/arvados",
252                        "https://api.curoverse.com/auth/arvados.readonly"
253                       ]
254             },
255             create: {
256               id: "arvados.#{k.to_s.underscore.pluralize}.create",
257               path: "#{k.to_s.underscore.pluralize}",
258               httpMethod: "POST",
259               description: "Create a new #{k.to_s}.",
260               parameters: {
261                 ensure_unique_name: {
262                   type: "boolean",
263                   description: "Adjust name to ensure uniqueness instead of returning an error on (owner_uuid, name) collision.",
264                   location: "query"
265                 }
266               },
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     render json: discovery
400   end
401 end