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