Merge branch 'master' into 3654-combine-selections
[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               request: {
262                 required: true,
263                 properties: {
264                   k.to_s.underscore => {
265                     "$ref" => k.to_s
266                   }
267                 }
268               },
269               response: {
270                 "$ref" => k.to_s
271               },
272               scopes: [
273                        "https://api.curoverse.com/auth/arvados"
274                       ]
275             },
276             update: {
277               id: "arvados.#{k.to_s.underscore.pluralize}.update",
278               path: "#{k.to_s.underscore.pluralize}/{uuid}",
279               httpMethod: "PUT",
280               description: "Update attributes of an existing #{k.to_s}.",
281               parameters: {
282                 uuid: {
283                   type: "string",
284                   description: "The UUID of the #{k.to_s} in question.",
285                   required: true,
286                   location: "path"
287                 }
288               },
289               request: {
290                 required: true,
291                 properties: {
292                   k.to_s.underscore => {
293                     "$ref" => k.to_s
294                   }
295                 }
296               },
297               response: {
298                 "$ref" => k.to_s
299               },
300               scopes: [
301                        "https://api.curoverse.com/auth/arvados"
302                       ]
303             },
304             delete: {
305               id: "arvados.#{k.to_s.underscore.pluralize}.delete",
306               path: "#{k.to_s.underscore.pluralize}/{uuid}",
307               httpMethod: "DELETE",
308               description: "Delete an existing #{k.to_s}.",
309               parameters: {
310                 uuid: {
311                   type: "string",
312                   description: "The UUID of the #{k.to_s} in question.",
313                   required: true,
314                   location: "path"
315                 }
316               },
317               response: {
318                 "$ref" => k.to_s
319               },
320               scopes: [
321                        "https://api.curoverse.com/auth/arvados"
322                       ]
323             }
324           }
325         }
326         # Check for Rails routes that don't match the usual actions
327         # listed above
328         d_methods = discovery[:resources][k.to_s.underscore.pluralize][:methods]
329         Rails.application.routes.routes.each do |route|
330           action = route.defaults[:action]
331           httpMethod = ['GET', 'POST', 'PUT', 'DELETE'].map { |method|
332             method if route.verb.match(method)
333           }.compact.first
334           if httpMethod and
335               route.defaults[:controller] == 'arvados/v1/' + k.to_s.underscore.pluralize and
336               ctl_class.action_methods.include? action
337             if !d_methods[action.to_sym]
338               method = {
339                 id: "arvados.#{k.to_s.underscore.pluralize}.#{action}",
340                 path: route.path.spec.to_s.sub('/arvados/v1/','').sub('(.:format)','').sub(/:(uu)?id/,'{uuid}'),
341                 httpMethod: httpMethod,
342                 description: "#{action} #{k.to_s.underscore.pluralize}",
343                 parameters: {},
344                 response: {
345                   "$ref" => (action == 'index' ? "#{k.to_s}List" : k.to_s)
346                 },
347                 scopes: [
348                          "https://api.curoverse.com/auth/arvados"
349                         ]
350               }
351               route.segment_keys.each do |key|
352                 if key != :format
353                   key = :uuid if key == :id
354                   method[:parameters][key] = {
355                     type: "string",
356                     description: "",
357                     required: true,
358                     location: "path"
359                   }
360                 end
361               end
362             else
363               # We already built a generic method description, but we
364               # might find some more required parameters through
365               # introspection.
366               method = d_methods[action.to_sym]
367             end
368             if ctl_class.respond_to? "_#{action}_requires_parameters".to_sym
369               ctl_class.send("_#{action}_requires_parameters".to_sym).each do |k, v|
370                 if v.is_a? Hash
371                   method[:parameters][k] = v
372                 else
373                   method[:parameters][k] = {}
374                 end
375                 if !method[:parameters][k][:default].nil?
376                   # The JAVA SDK is sensitive to all values being strings
377                   method[:parameters][k][:default] = method[:parameters][k][:default].to_s
378                 end
379                 method[:parameters][k][:type] ||= 'string'
380                 method[:parameters][k][:description] ||= ''
381                 method[:parameters][k][:location] = (route.segment_keys.include?(k) ? 'path' : 'query')
382                 if method[:parameters][k][:required].nil?
383                   method[:parameters][k][:required] = v != false
384                 end
385               end
386             end
387             d_methods[action.to_sym] = method
388           end
389         end
390       end
391       discovery
392     end
393     render json: discovery
394   end
395 end