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