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